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 GstElement *find_payload_element (GstElement * payloader);
216 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
218 #define C_ENUM(v) ((gint) v)
221 gst_rtsp_suspend_mode_get_type (void)
224 static const GEnumValue values[] = {
225 {C_ENUM (GST_RTSP_SUSPEND_MODE_NONE), "GST_RTSP_SUSPEND_MODE_NONE", "none"},
226 {C_ENUM (GST_RTSP_SUSPEND_MODE_PAUSE), "GST_RTSP_SUSPEND_MODE_PAUSE",
228 {C_ENUM (GST_RTSP_SUSPEND_MODE_RESET), "GST_RTSP_SUSPEND_MODE_RESET",
233 if (g_once_init_enter (&id)) {
234 GType tmp = g_enum_register_static ("GstRTSPSuspendMode", values);
235 g_once_init_leave (&id, tmp);
240 #define C_FLAGS(v) ((guint) v)
243 gst_rtsp_transport_mode_get_type (void)
246 static const GFlagsValue values[] = {
247 {C_FLAGS (GST_RTSP_TRANSPORT_MODE_PLAY), "GST_RTSP_TRANSPORT_MODE_PLAY",
249 {C_FLAGS (GST_RTSP_TRANSPORT_MODE_RECORD), "GST_RTSP_TRANSPORT_MODE_RECORD",
254 if (g_once_init_enter (&id)) {
255 GType tmp = g_flags_register_static ("GstRTSPTransportMode", values);
256 g_once_init_leave (&id, tmp);
261 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
264 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
266 GObjectClass *gobject_class;
268 g_type_class_add_private (klass, sizeof (GstRTSPMediaPrivate));
270 gobject_class = G_OBJECT_CLASS (klass);
272 gobject_class->get_property = gst_rtsp_media_get_property;
273 gobject_class->set_property = gst_rtsp_media_set_property;
274 gobject_class->finalize = gst_rtsp_media_finalize;
276 g_object_class_install_property (gobject_class, PROP_SHARED,
277 g_param_spec_boolean ("shared", "Shared",
278 "If this media pipeline can be shared", DEFAULT_SHARED,
279 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
281 g_object_class_install_property (gobject_class, PROP_SUSPEND_MODE,
282 g_param_spec_enum ("suspend-mode", "Suspend Mode",
283 "How to suspend the media in PAUSED", GST_TYPE_RTSP_SUSPEND_MODE,
284 DEFAULT_SUSPEND_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
286 g_object_class_install_property (gobject_class, PROP_REUSABLE,
287 g_param_spec_boolean ("reusable", "Reusable",
288 "If this media pipeline can be reused after an unprepare",
289 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
291 g_object_class_install_property (gobject_class, PROP_PROFILES,
292 g_param_spec_flags ("profiles", "Profiles",
293 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
294 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
296 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
297 g_param_spec_flags ("protocols", "Protocols",
298 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
299 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
301 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
302 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
303 "Send an EOS event to the pipeline before unpreparing",
304 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
306 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
307 g_param_spec_uint ("buffer-size", "Buffer Size",
308 "The kernel UDP buffer size to use", 0, G_MAXUINT,
309 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
311 g_object_class_install_property (gobject_class, PROP_ELEMENT,
312 g_param_spec_object ("element", "The Element",
313 "The GstBin to use for streaming the media", GST_TYPE_ELEMENT,
314 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
316 g_object_class_install_property (gobject_class, PROP_TIME_PROVIDER,
317 g_param_spec_boolean ("time-provider", "Time Provider",
318 "Use a NetTimeProvider for clients",
319 DEFAULT_TIME_PROVIDER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
321 g_object_class_install_property (gobject_class, PROP_LATENCY,
322 g_param_spec_uint ("latency", "Latency",
323 "Latency used for receiving media in milliseconds", 0, G_MAXUINT,
324 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326 g_object_class_install_property (gobject_class, PROP_TRANSPORT_MODE,
327 g_param_spec_flags ("transport-mode", "Transport Mode",
328 "If this media pipeline can be used for PLAY or RECORD",
329 GST_TYPE_RTSP_TRANSPORT_MODE, DEFAULT_TRANSPORT_MODE,
330 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
332 gst_rtsp_media_signals[SIGNAL_NEW_STREAM] =
333 g_signal_new ("new-stream", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
334 G_STRUCT_OFFSET (GstRTSPMediaClass, new_stream), NULL, NULL,
335 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_STREAM);
337 gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM] =
338 g_signal_new ("removed-stream", G_TYPE_FROM_CLASS (klass),
339 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, removed_stream),
340 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
341 GST_TYPE_RTSP_STREAM);
343 gst_rtsp_media_signals[SIGNAL_PREPARED] =
344 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
345 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
346 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
348 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
349 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
350 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
351 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
353 gst_rtsp_media_signals[SIGNAL_TARGET_STATE] =
354 g_signal_new ("target-state", G_TYPE_FROM_CLASS (klass),
355 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, target_state),
356 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
358 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
359 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
360 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
361 g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
363 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
365 klass->handle_message = default_handle_message;
366 klass->prepare = default_prepare;
367 klass->unprepare = default_unprepare;
368 klass->suspend = default_suspend;
369 klass->unsuspend = default_unsuspend;
370 klass->convert_range = default_convert_range;
371 klass->query_position = default_query_position;
372 klass->query_stop = default_query_stop;
373 klass->create_rtpbin = default_create_rtpbin;
374 klass->setup_sdp = default_setup_sdp;
375 klass->handle_sdp = default_handle_sdp;
379 gst_rtsp_media_init (GstRTSPMedia * media)
381 GstRTSPMediaPrivate *priv = GST_RTSP_MEDIA_GET_PRIVATE (media);
385 priv->streams = g_ptr_array_new_with_free_func (g_object_unref);
386 g_mutex_init (&priv->lock);
387 g_cond_init (&priv->cond);
388 g_rec_mutex_init (&priv->state_lock);
390 priv->shared = DEFAULT_SHARED;
391 priv->suspend_mode = DEFAULT_SUSPEND_MODE;
392 priv->reusable = DEFAULT_REUSABLE;
393 priv->profiles = DEFAULT_PROFILES;
394 priv->protocols = DEFAULT_PROTOCOLS;
395 priv->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
396 priv->buffer_size = DEFAULT_BUFFER_SIZE;
397 priv->time_provider = DEFAULT_TIME_PROVIDER;
398 priv->transport_mode = DEFAULT_TRANSPORT_MODE;
402 gst_rtsp_media_finalize (GObject * obj)
404 GstRTSPMediaPrivate *priv;
407 media = GST_RTSP_MEDIA (obj);
410 GST_INFO ("finalize media %p", media);
412 if (priv->permissions)
413 gst_rtsp_permissions_unref (priv->permissions);
415 g_ptr_array_unref (priv->streams);
417 g_list_free_full (priv->dynamic, gst_object_unref);
420 gst_object_unref (priv->pipeline);
422 gst_object_unref (priv->nettime);
423 gst_object_unref (priv->element);
425 g_object_unref (priv->pool);
427 g_list_free (priv->payloads);
428 g_mutex_clear (&priv->lock);
429 g_cond_clear (&priv->cond);
430 g_rec_mutex_clear (&priv->state_lock);
432 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
436 gst_rtsp_media_get_property (GObject * object, guint propid,
437 GValue * value, GParamSpec * pspec)
439 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
443 g_value_set_object (value, media->priv->element);
446 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
448 case PROP_SUSPEND_MODE:
449 g_value_set_enum (value, gst_rtsp_media_get_suspend_mode (media));
452 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
455 g_value_set_flags (value, gst_rtsp_media_get_profiles (media));
458 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
460 case PROP_EOS_SHUTDOWN:
461 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
463 case PROP_BUFFER_SIZE:
464 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
466 case PROP_TIME_PROVIDER:
467 g_value_set_boolean (value, gst_rtsp_media_is_time_provider (media));
470 g_value_set_uint (value, gst_rtsp_media_get_latency (media));
472 case PROP_TRANSPORT_MODE:
473 g_value_set_flags (value, gst_rtsp_media_get_transport_mode (media));
476 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
481 gst_rtsp_media_set_property (GObject * object, guint propid,
482 const GValue * value, GParamSpec * pspec)
484 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
488 media->priv->element = g_value_get_object (value);
489 gst_object_ref_sink (media->priv->element);
492 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
494 case PROP_SUSPEND_MODE:
495 gst_rtsp_media_set_suspend_mode (media, g_value_get_enum (value));
498 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
501 gst_rtsp_media_set_profiles (media, g_value_get_flags (value));
504 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
506 case PROP_EOS_SHUTDOWN:
507 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
509 case PROP_BUFFER_SIZE:
510 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
512 case PROP_TIME_PROVIDER:
513 gst_rtsp_media_use_time_provider (media, g_value_get_boolean (value));
516 gst_rtsp_media_set_latency (media, g_value_get_uint (value));
518 case PROP_TRANSPORT_MODE:
519 gst_rtsp_media_set_transport_mode (media, g_value_get_flags (value));
522 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
530 } DoQueryPositionData;
533 do_query_position (GstRTSPStream * stream, DoQueryPositionData * data)
537 if (gst_rtsp_stream_query_position (stream, &tmp)) {
538 data->position = MAX (data->position, tmp);
544 default_query_position (GstRTSPMedia * media, gint64 * position)
546 GstRTSPMediaPrivate *priv;
547 DoQueryPositionData data;
554 g_ptr_array_foreach (priv->streams, (GFunc) do_query_position, &data);
556 *position = data.position;
568 do_query_stop (GstRTSPStream * stream, DoQueryStopData * data)
572 if (gst_rtsp_stream_query_stop (stream, &tmp)) {
573 data->stop = MAX (data->stop, tmp);
579 default_query_stop (GstRTSPMedia * media, gint64 * stop)
581 GstRTSPMediaPrivate *priv;
582 DoQueryStopData data;
589 g_ptr_array_foreach (priv->streams, (GFunc) do_query_stop, &data);
597 default_create_rtpbin (GstRTSPMedia * media)
601 rtpbin = gst_element_factory_make ("rtpbin", NULL);
606 /* must be called with state lock */
608 collect_media_stats (GstRTSPMedia * media)
610 GstRTSPMediaPrivate *priv = media->priv;
611 gint64 position = 0, stop = -1;
613 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
614 priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
617 priv->range.unit = GST_RTSP_RANGE_NPT;
619 GST_INFO ("collect media stats");
622 priv->range.min.type = GST_RTSP_TIME_NOW;
623 priv->range.min.seconds = -1;
624 priv->range_start = -1;
625 priv->range.max.type = GST_RTSP_TIME_END;
626 priv->range.max.seconds = -1;
627 priv->range_stop = -1;
629 GstRTSPMediaClass *klass;
632 klass = GST_RTSP_MEDIA_GET_CLASS (media);
634 /* get the position */
636 if (klass->query_position)
637 ret = klass->query_position (media, &position);
640 GST_INFO ("position query failed");
644 /* get the current segment stop */
646 if (klass->query_stop)
647 ret = klass->query_stop (media, &stop);
650 GST_INFO ("stop query failed");
654 GST_INFO ("stats: position %" GST_TIME_FORMAT ", stop %"
655 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (stop));
657 if (position == -1) {
658 priv->range.min.type = GST_RTSP_TIME_NOW;
659 priv->range.min.seconds = -1;
660 priv->range_start = -1;
662 priv->range.min.type = GST_RTSP_TIME_SECONDS;
663 priv->range.min.seconds = ((gdouble) position) / GST_SECOND;
664 priv->range_start = position;
667 priv->range.max.type = GST_RTSP_TIME_END;
668 priv->range.max.seconds = -1;
669 priv->range_stop = -1;
671 priv->range.max.type = GST_RTSP_TIME_SECONDS;
672 priv->range.max.seconds = ((gdouble) stop) / GST_SECOND;
673 priv->range_stop = stop;
679 * gst_rtsp_media_new:
680 * @element: (transfer full): a #GstElement
682 * Create a new #GstRTSPMedia instance. @element is the bin element that
683 * provides the different streams. The #GstRTSPMedia object contains the
684 * element to produce RTP data for one or more related (audio/video/..)
687 * Ownership is taken of @element.
689 * Returns: (transfer full): a new #GstRTSPMedia object.
692 gst_rtsp_media_new (GstElement * element)
694 GstRTSPMedia *result;
696 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
698 result = g_object_new (GST_TYPE_RTSP_MEDIA, "element", element, NULL);
704 * gst_rtsp_media_get_element:
705 * @media: a #GstRTSPMedia
707 * Get the element that was used when constructing @media.
709 * Returns: (transfer full): a #GstElement. Unref after usage.
712 gst_rtsp_media_get_element (GstRTSPMedia * media)
714 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
716 return gst_object_ref (media->priv->element);
720 * gst_rtsp_media_take_pipeline:
721 * @media: a #GstRTSPMedia
722 * @pipeline: (transfer full): a #GstPipeline
724 * Set @pipeline as the #GstPipeline for @media. Ownership is
725 * taken of @pipeline.
728 gst_rtsp_media_take_pipeline (GstRTSPMedia * media, GstPipeline * pipeline)
730 GstRTSPMediaPrivate *priv;
732 GstNetTimeProvider *nettime;
734 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
735 g_return_if_fail (GST_IS_PIPELINE (pipeline));
739 g_mutex_lock (&priv->lock);
740 old = priv->pipeline;
741 priv->pipeline = GST_ELEMENT_CAST (pipeline);
742 nettime = priv->nettime;
743 priv->nettime = NULL;
744 g_mutex_unlock (&priv->lock);
747 gst_object_unref (old);
750 gst_object_unref (nettime);
752 gst_bin_add (GST_BIN_CAST (pipeline), priv->element);
756 * gst_rtsp_media_set_permissions:
757 * @media: a #GstRTSPMedia
758 * @permissions: (transfer none): a #GstRTSPPermissions
760 * Set @permissions on @media.
763 gst_rtsp_media_set_permissions (GstRTSPMedia * media,
764 GstRTSPPermissions * permissions)
766 GstRTSPMediaPrivate *priv;
768 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
772 g_mutex_lock (&priv->lock);
773 if (priv->permissions)
774 gst_rtsp_permissions_unref (priv->permissions);
775 if ((priv->permissions = permissions))
776 gst_rtsp_permissions_ref (permissions);
777 g_mutex_unlock (&priv->lock);
781 * gst_rtsp_media_get_permissions:
782 * @media: a #GstRTSPMedia
784 * Get the permissions object from @media.
786 * Returns: (transfer full): a #GstRTSPPermissions object, unref after usage.
789 gst_rtsp_media_get_permissions (GstRTSPMedia * media)
791 GstRTSPMediaPrivate *priv;
792 GstRTSPPermissions *result;
794 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
798 g_mutex_lock (&priv->lock);
799 if ((result = priv->permissions))
800 gst_rtsp_permissions_ref (result);
801 g_mutex_unlock (&priv->lock);
807 * gst_rtsp_media_set_suspend_mode:
808 * @media: a #GstRTSPMedia
809 * @mode: the new #GstRTSPSuspendMode
811 * Control how @ media will be suspended after the SDP has been generated and
812 * after a PAUSE request has been performed.
814 * Media must be unprepared when setting the suspend mode.
817 gst_rtsp_media_set_suspend_mode (GstRTSPMedia * media, GstRTSPSuspendMode mode)
819 GstRTSPMediaPrivate *priv;
821 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
825 g_rec_mutex_lock (&priv->state_lock);
826 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED)
828 priv->suspend_mode = mode;
829 g_rec_mutex_unlock (&priv->state_lock);
836 GST_WARNING ("media %p was prepared", media);
837 g_rec_mutex_unlock (&priv->state_lock);
842 * gst_rtsp_media_get_suspend_mode:
843 * @media: a #GstRTSPMedia
845 * Get how @media will be suspended.
847 * Returns: #GstRTSPSuspendMode.
850 gst_rtsp_media_get_suspend_mode (GstRTSPMedia * media)
852 GstRTSPMediaPrivate *priv;
853 GstRTSPSuspendMode res;
855 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_RTSP_SUSPEND_MODE_NONE);
859 g_rec_mutex_lock (&priv->state_lock);
860 res = priv->suspend_mode;
861 g_rec_mutex_unlock (&priv->state_lock);
867 * gst_rtsp_media_set_shared:
868 * @media: a #GstRTSPMedia
869 * @shared: the new value
871 * Set or unset if the pipeline for @media can be shared will multiple clients.
872 * When @shared is %TRUE, client requests for this media will share the media
876 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
878 GstRTSPMediaPrivate *priv;
880 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
884 g_mutex_lock (&priv->lock);
885 priv->shared = shared;
886 g_mutex_unlock (&priv->lock);
890 * gst_rtsp_media_is_shared:
891 * @media: a #GstRTSPMedia
893 * Check if the pipeline for @media can be shared between multiple clients.
895 * Returns: %TRUE if the media can be shared between clients.
898 gst_rtsp_media_is_shared (GstRTSPMedia * media)
900 GstRTSPMediaPrivate *priv;
903 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
907 g_mutex_lock (&priv->lock);
909 g_mutex_unlock (&priv->lock);
915 * gst_rtsp_media_set_reusable:
916 * @media: a #GstRTSPMedia
917 * @reusable: the new value
919 * Set or unset if the pipeline for @media can be reused after the pipeline has
923 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
925 GstRTSPMediaPrivate *priv;
927 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
931 g_mutex_lock (&priv->lock);
932 priv->reusable = reusable;
933 g_mutex_unlock (&priv->lock);
937 * gst_rtsp_media_is_reusable:
938 * @media: a #GstRTSPMedia
940 * Check if the pipeline for @media can be reused after an unprepare.
942 * Returns: %TRUE if the media can be reused
945 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
947 GstRTSPMediaPrivate *priv;
950 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
954 g_mutex_lock (&priv->lock);
955 res = priv->reusable;
956 g_mutex_unlock (&priv->lock);
962 do_set_profiles (GstRTSPStream * stream, GstRTSPProfile * profiles)
964 gst_rtsp_stream_set_profiles (stream, *profiles);
968 * gst_rtsp_media_set_profiles:
969 * @media: a #GstRTSPMedia
970 * @profiles: the new flags
972 * Configure the allowed lower transport for @media.
975 gst_rtsp_media_set_profiles (GstRTSPMedia * media, GstRTSPProfile profiles)
977 GstRTSPMediaPrivate *priv;
979 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
983 g_mutex_lock (&priv->lock);
984 priv->profiles = profiles;
985 g_ptr_array_foreach (priv->streams, (GFunc) do_set_profiles, &profiles);
986 g_mutex_unlock (&priv->lock);
990 * gst_rtsp_media_get_profiles:
991 * @media: a #GstRTSPMedia
993 * Get the allowed profiles of @media.
995 * Returns: a #GstRTSPProfile
998 gst_rtsp_media_get_profiles (GstRTSPMedia * media)
1000 GstRTSPMediaPrivate *priv;
1003 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_RTSP_PROFILE_UNKNOWN);
1007 g_mutex_lock (&priv->lock);
1008 res = priv->profiles;
1009 g_mutex_unlock (&priv->lock);
1015 do_set_protocols (GstRTSPStream * stream, GstRTSPLowerTrans * protocols)
1017 gst_rtsp_stream_set_protocols (stream, *protocols);
1021 * gst_rtsp_media_set_protocols:
1022 * @media: a #GstRTSPMedia
1023 * @protocols: the new flags
1025 * Configure the allowed lower transport for @media.
1028 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
1030 GstRTSPMediaPrivate *priv;
1032 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1036 g_mutex_lock (&priv->lock);
1037 priv->protocols = protocols;
1038 g_ptr_array_foreach (priv->streams, (GFunc) do_set_protocols, &protocols);
1039 g_mutex_unlock (&priv->lock);
1043 * gst_rtsp_media_get_protocols:
1044 * @media: a #GstRTSPMedia
1046 * Get the allowed protocols of @media.
1048 * Returns: a #GstRTSPLowerTrans
1051 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
1053 GstRTSPMediaPrivate *priv;
1054 GstRTSPLowerTrans res;
1056 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
1057 GST_RTSP_LOWER_TRANS_UNKNOWN);
1061 g_mutex_lock (&priv->lock);
1062 res = priv->protocols;
1063 g_mutex_unlock (&priv->lock);
1069 * gst_rtsp_media_set_eos_shutdown:
1070 * @media: a #GstRTSPMedia
1071 * @eos_shutdown: the new value
1073 * Set or unset if an EOS event will be sent to the pipeline for @media before
1077 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
1079 GstRTSPMediaPrivate *priv;
1081 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1085 g_mutex_lock (&priv->lock);
1086 priv->eos_shutdown = eos_shutdown;
1087 g_mutex_unlock (&priv->lock);
1091 * gst_rtsp_media_is_eos_shutdown:
1092 * @media: a #GstRTSPMedia
1094 * Check if the pipeline for @media will send an EOS down the pipeline before
1097 * Returns: %TRUE if the media will send EOS before unpreparing.
1100 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
1102 GstRTSPMediaPrivate *priv;
1105 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1109 g_mutex_lock (&priv->lock);
1110 res = priv->eos_shutdown;
1111 g_mutex_unlock (&priv->lock);
1117 * gst_rtsp_media_set_buffer_size:
1118 * @media: a #GstRTSPMedia
1119 * @size: the new value
1121 * Set the kernel UDP buffer size.
1124 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
1126 GstRTSPMediaPrivate *priv;
1129 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1131 GST_LOG_OBJECT (media, "set buffer size %u", size);
1135 g_mutex_lock (&priv->lock);
1136 priv->buffer_size = size;
1138 for (i = 0; i < priv->streams->len; i++) {
1139 GstRTSPStream *stream = g_ptr_array_index (priv->streams, i);
1140 gst_rtsp_stream_set_buffer_size (stream, size);
1142 g_mutex_unlock (&priv->lock);
1146 * gst_rtsp_media_get_buffer_size:
1147 * @media: a #GstRTSPMedia
1149 * Get the kernel UDP buffer size.
1151 * Returns: the kernel UDP buffer size.
1154 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
1156 GstRTSPMediaPrivate *priv;
1159 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1163 g_mutex_lock (&priv->lock);
1164 res = priv->buffer_size;
1165 g_mutex_unlock (&priv->lock);
1171 * gst_rtsp_media_set_retransmission_time:
1172 * @media: a #GstRTSPMedia
1173 * @time: the new value
1175 * Set the amount of time to store retransmission packets.
1178 gst_rtsp_media_set_retransmission_time (GstRTSPMedia * media, GstClockTime time)
1180 GstRTSPMediaPrivate *priv;
1183 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1185 GST_LOG_OBJECT (media, "set retransmission time %" G_GUINT64_FORMAT, time);
1189 g_mutex_lock (&priv->lock);
1190 priv->rtx_time = time;
1191 for (i = 0; i < priv->streams->len; i++) {
1192 GstRTSPStream *stream = g_ptr_array_index (priv->streams, i);
1194 gst_rtsp_stream_set_retransmission_time (stream, time);
1198 g_object_set (priv->rtpbin, "do-retransmission", time > 0, NULL);
1199 g_mutex_unlock (&priv->lock);
1203 * gst_rtsp_media_get_retransmission_time:
1204 * @media: a #GstRTSPMedia
1206 * Get the amount of time to store retransmission data.
1208 * Returns: the amount of time to store retransmission data.
1211 gst_rtsp_media_get_retransmission_time (GstRTSPMedia * media)
1213 GstRTSPMediaPrivate *priv;
1216 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1220 g_mutex_unlock (&priv->lock);
1221 res = priv->rtx_time;
1222 g_mutex_unlock (&priv->lock);
1228 * gst_rtsp_media_set_latncy:
1229 * @media: a #GstRTSPMedia
1230 * @latency: latency in milliseconds
1232 * Configure the latency used for receiving media.
1235 gst_rtsp_media_set_latency (GstRTSPMedia * media, guint latency)
1237 GstRTSPMediaPrivate *priv;
1239 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1241 GST_LOG_OBJECT (media, "set latency %ums", latency);
1245 g_mutex_lock (&priv->lock);
1246 priv->latency = latency;
1248 g_object_set (priv->rtpbin, "latency", latency, NULL);
1249 g_mutex_unlock (&priv->lock);
1253 * gst_rtsp_media_get_latency:
1254 * @media: a #GstRTSPMedia
1256 * Get the latency that is used for receiving media.
1258 * Returns: latency in milliseconds
1261 gst_rtsp_media_get_latency (GstRTSPMedia * media)
1263 GstRTSPMediaPrivate *priv;
1266 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1270 g_mutex_unlock (&priv->lock);
1271 res = priv->latency;
1272 g_mutex_unlock (&priv->lock);
1278 * gst_rtsp_media_use_time_provider:
1279 * @media: a #GstRTSPMedia
1280 * @time_provider: if a #GstNetTimeProvider should be used
1282 * Set @media to provide a #GstNetTimeProvider.
1285 gst_rtsp_media_use_time_provider (GstRTSPMedia * media, gboolean time_provider)
1287 GstRTSPMediaPrivate *priv;
1289 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1293 g_mutex_lock (&priv->lock);
1294 priv->time_provider = time_provider;
1295 g_mutex_unlock (&priv->lock);
1299 * gst_rtsp_media_is_time_provider:
1300 * @media: a #GstRTSPMedia
1302 * Check if @media can provide a #GstNetTimeProvider for its pipeline clock.
1304 * Use gst_rtsp_media_get_time_provider() to get the network clock.
1306 * Returns: %TRUE if @media can provide a #GstNetTimeProvider.
1309 gst_rtsp_media_is_time_provider (GstRTSPMedia * media)
1311 GstRTSPMediaPrivate *priv;
1314 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1318 g_mutex_unlock (&priv->lock);
1319 res = priv->time_provider;
1320 g_mutex_unlock (&priv->lock);
1326 * gst_rtsp_media_set_address_pool:
1327 * @media: a #GstRTSPMedia
1328 * @pool: (transfer none): a #GstRTSPAddressPool
1330 * configure @pool to be used as the address pool of @media.
1333 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
1334 GstRTSPAddressPool * pool)
1336 GstRTSPMediaPrivate *priv;
1337 GstRTSPAddressPool *old;
1339 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1343 GST_LOG_OBJECT (media, "set address pool %p", pool);
1345 g_mutex_lock (&priv->lock);
1346 if ((old = priv->pool) != pool)
1347 priv->pool = pool ? g_object_ref (pool) : NULL;
1350 g_ptr_array_foreach (priv->streams, (GFunc) gst_rtsp_stream_set_address_pool,
1352 g_mutex_unlock (&priv->lock);
1355 g_object_unref (old);
1359 * gst_rtsp_media_get_address_pool:
1360 * @media: a #GstRTSPMedia
1362 * Get the #GstRTSPAddressPool used as the address pool of @media.
1364 * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
1367 GstRTSPAddressPool *
1368 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
1370 GstRTSPMediaPrivate *priv;
1371 GstRTSPAddressPool *result;
1373 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1377 g_mutex_lock (&priv->lock);
1378 if ((result = priv->pool))
1379 g_object_ref (result);
1380 g_mutex_unlock (&priv->lock);
1386 _find_payload_types (GstRTSPMedia * media)
1389 GQueue queue = G_QUEUE_INIT;
1391 n = media->priv->streams->len;
1392 for (i = 0; i < n; i++) {
1393 GstRTSPStream *stream = g_ptr_array_index (media->priv->streams, i);
1394 guint pt = gst_rtsp_stream_get_pt (stream);
1396 g_queue_push_tail (&queue, GUINT_TO_POINTER (pt));
1403 _next_available_pt (GList * payloads)
1407 for (i = 96; i <= 127; i++) {
1408 GList *iter = g_list_find (payloads, GINT_TO_POINTER (i));
1410 return GPOINTER_TO_UINT (i);
1417 * gst_rtsp_media_collect_streams:
1418 * @media: a #GstRTSPMedia
1420 * Find all payloader elements, they should be named pay\%d in the
1421 * element of @media, and create #GstRTSPStreams for them.
1423 * Collect all dynamic elements, named dynpay\%d, and add them to
1424 * the list of dynamic elements.
1426 * Find all depayloader elements, they should be named depay\%d in the
1427 * element of @media, and create #GstRTSPStreams for them.
1430 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
1432 GstRTSPMediaPrivate *priv;
1433 GstElement *element, *elem;
1437 gboolean more_elem_remaining = TRUE;
1438 GstRTSPTransportMode mode = 0;
1440 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1443 element = priv->element;
1446 for (i = 0; more_elem_remaining; i++) {
1449 more_elem_remaining = FALSE;
1451 name = g_strdup_printf ("pay%d", i);
1452 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1454 GST_INFO ("found stream %d with payloader %p", i, elem);
1456 /* take the pad of the payloader */
1457 pad = gst_element_get_static_pad (elem, "src");
1459 /* find the real payload element in case elem is a GstBin */
1460 pay = find_payload_element (elem);
1462 /* create the stream */
1464 GST_WARNING ("could not find real payloader, using bin");
1465 gst_rtsp_media_create_stream (media, elem, pad);
1467 gst_rtsp_media_create_stream (media, pay, pad);
1468 gst_object_unref (pay);
1471 gst_object_unref (pad);
1472 gst_object_unref (elem);
1475 more_elem_remaining = TRUE;
1476 mode |= GST_RTSP_TRANSPORT_MODE_PLAY;
1480 name = g_strdup_printf ("dynpay%d", i);
1481 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1482 /* a stream that will dynamically create pads to provide RTP packets */
1483 GST_INFO ("found dynamic element %d, %p", i, elem);
1485 g_mutex_lock (&priv->lock);
1486 priv->dynamic = g_list_prepend (priv->dynamic, elem);
1487 g_mutex_unlock (&priv->lock);
1490 more_elem_remaining = TRUE;
1491 mode |= GST_RTSP_TRANSPORT_MODE_PLAY;
1495 name = g_strdup_printf ("depay%d", i);
1496 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1497 GST_INFO ("found stream %d with depayloader %p", i, elem);
1499 /* take the pad of the payloader */
1500 pad = gst_element_get_static_pad (elem, "sink");
1501 /* create the stream */
1502 gst_rtsp_media_create_stream (media, elem, pad);
1503 gst_object_unref (pad);
1504 gst_object_unref (elem);
1507 more_elem_remaining = TRUE;
1508 mode |= GST_RTSP_TRANSPORT_MODE_RECORD;
1514 if (priv->transport_mode != mode)
1515 GST_WARNING ("found different mode than expected (0x%02x != 0x%02d)",
1516 priv->transport_mode, mode);
1521 * gst_rtsp_media_create_stream:
1522 * @media: a #GstRTSPMedia
1523 * @payloader: a #GstElement
1526 * Create a new stream in @media that provides RTP data on @pad.
1527 * @pad should be a pad of an element inside @media->element.
1529 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
1533 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
1536 GstRTSPMediaPrivate *priv;
1537 GstRTSPStream *stream;
1542 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1543 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
1544 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1548 g_mutex_lock (&priv->lock);
1549 idx = priv->streams->len;
1551 GST_DEBUG ("media %p: creating stream with index %d", media, idx);
1553 if (GST_PAD_IS_SRC (pad))
1554 name = g_strdup_printf ("src_%u", idx);
1556 name = g_strdup_printf ("sink_%u", idx);
1558 ghostpad = gst_ghost_pad_new (name, pad);
1559 gst_pad_set_active (ghostpad, TRUE);
1560 gst_element_add_pad (priv->element, ghostpad);
1563 stream = gst_rtsp_stream_new (idx, payloader, ghostpad);
1565 gst_rtsp_stream_set_address_pool (stream, priv->pool);
1566 gst_rtsp_stream_set_profiles (stream, priv->profiles);
1567 gst_rtsp_stream_set_protocols (stream, priv->protocols);
1568 gst_rtsp_stream_set_retransmission_time (stream, priv->rtx_time);
1569 gst_rtsp_stream_set_buffer_size (stream, priv->buffer_size);
1571 g_ptr_array_add (priv->streams, stream);
1573 if (GST_PAD_IS_SRC (pad)) {
1577 g_list_free (priv->payloads);
1578 priv->payloads = _find_payload_types (media);
1580 n = priv->streams->len;
1581 for (i = 0; i < n; i++) {
1582 GstRTSPStream *stream = g_ptr_array_index (priv->streams, i);
1583 guint rtx_pt = _next_available_pt (priv->payloads);
1586 GST_WARNING ("Ran out of space of dynamic payload types");
1590 gst_rtsp_stream_set_retransmission_pt (stream, rtx_pt);
1593 g_list_append (priv->payloads, GUINT_TO_POINTER (rtx_pt));
1596 g_mutex_unlock (&priv->lock);
1598 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STREAM], 0, stream,
1605 gst_rtsp_media_remove_stream (GstRTSPMedia * media, GstRTSPStream * stream)
1607 GstRTSPMediaPrivate *priv;
1612 g_mutex_lock (&priv->lock);
1613 /* remove the ghostpad */
1614 srcpad = gst_rtsp_stream_get_srcpad (stream);
1615 gst_element_remove_pad (priv->element, srcpad);
1616 gst_object_unref (srcpad);
1617 /* now remove the stream */
1618 g_object_ref (stream);
1619 g_ptr_array_remove (priv->streams, stream);
1620 g_mutex_unlock (&priv->lock);
1622 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM], 0,
1625 g_object_unref (stream);
1629 * gst_rtsp_media_n_streams:
1630 * @media: a #GstRTSPMedia
1632 * Get the number of streams in this media.
1634 * Returns: The number of streams.
1637 gst_rtsp_media_n_streams (GstRTSPMedia * media)
1639 GstRTSPMediaPrivate *priv;
1642 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
1646 g_mutex_lock (&priv->lock);
1647 res = priv->streams->len;
1648 g_mutex_unlock (&priv->lock);
1654 * gst_rtsp_media_get_stream:
1655 * @media: a #GstRTSPMedia
1656 * @idx: the stream index
1658 * Retrieve the stream with index @idx from @media.
1660 * Returns: (nullable) (transfer none): the #GstRTSPStream at index
1661 * @idx or %NULL when a stream with that index did not exist.
1664 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
1666 GstRTSPMediaPrivate *priv;
1669 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1673 g_mutex_lock (&priv->lock);
1674 if (idx < priv->streams->len)
1675 res = g_ptr_array_index (priv->streams, idx);
1678 g_mutex_unlock (&priv->lock);
1684 * gst_rtsp_media_find_stream:
1685 * @media: a #GstRTSPMedia
1686 * @control: the control of the stream
1688 * Find a stream in @media with @control as the control uri.
1690 * Returns: (nullable) (transfer none): the #GstRTSPStream with
1691 * control uri @control or %NULL when a stream with that control did
1695 gst_rtsp_media_find_stream (GstRTSPMedia * media, const gchar * control)
1697 GstRTSPMediaPrivate *priv;
1701 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1702 g_return_val_if_fail (control != NULL, NULL);
1708 g_mutex_lock (&priv->lock);
1709 for (i = 0; i < priv->streams->len; i++) {
1710 GstRTSPStream *test;
1712 test = g_ptr_array_index (priv->streams, i);
1713 if (gst_rtsp_stream_has_control (test, control)) {
1718 g_mutex_unlock (&priv->lock);
1723 /* called with state-lock */
1725 default_convert_range (GstRTSPMedia * media, GstRTSPTimeRange * range,
1726 GstRTSPRangeUnit unit)
1728 return gst_rtsp_range_convert_units (range, unit);
1732 * gst_rtsp_media_get_range_string:
1733 * @media: a #GstRTSPMedia
1734 * @play: for the PLAY request
1735 * @unit: the unit to use for the string
1737 * Get the current range as a string. @media must be prepared with
1738 * gst_rtsp_media_prepare ().
1740 * Returns: (transfer full): The range as a string, g_free() after usage.
1743 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play,
1744 GstRTSPRangeUnit unit)
1746 GstRTSPMediaClass *klass;
1747 GstRTSPMediaPrivate *priv;
1749 GstRTSPTimeRange range;
1751 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1752 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1753 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1757 g_rec_mutex_lock (&priv->state_lock);
1758 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
1759 priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
1762 g_mutex_lock (&priv->lock);
1764 /* Update the range value with current position/duration */
1765 collect_media_stats (media);
1768 range = priv->range;
1770 if (!play && priv->n_active > 0) {
1771 range.min.type = GST_RTSP_TIME_NOW;
1772 range.min.seconds = -1;
1774 g_mutex_unlock (&priv->lock);
1775 g_rec_mutex_unlock (&priv->state_lock);
1777 if (!klass->convert_range (media, &range, unit))
1778 goto conversion_failed;
1780 result = gst_rtsp_range_to_string (&range);
1787 GST_WARNING ("media %p was not prepared", media);
1788 g_rec_mutex_unlock (&priv->state_lock);
1793 GST_WARNING ("range conversion to unit %d failed", unit);
1799 stream_update_blocked (GstRTSPStream * stream, GstRTSPMedia * media)
1801 gst_rtsp_stream_set_blocked (stream, media->priv->blocked);
1805 media_streams_set_blocked (GstRTSPMedia * media, gboolean blocked)
1807 GstRTSPMediaPrivate *priv = media->priv;
1809 GST_DEBUG ("media %p set blocked %d", media, blocked);
1810 priv->blocked = blocked;
1811 g_ptr_array_foreach (priv->streams, (GFunc) stream_update_blocked, media);
1815 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1817 GstRTSPMediaPrivate *priv = media->priv;
1819 g_mutex_lock (&priv->lock);
1820 priv->status = status;
1821 GST_DEBUG ("setting new status to %d", status);
1822 g_cond_broadcast (&priv->cond);
1823 g_mutex_unlock (&priv->lock);
1827 * gst_rtsp_media_get_status:
1828 * @media: a #GstRTSPMedia
1830 * Get the status of @media. When @media is busy preparing, this function waits
1831 * until @media is prepared or in error.
1833 * Returns: the status of @media.
1836 gst_rtsp_media_get_status (GstRTSPMedia * media)
1838 GstRTSPMediaPrivate *priv = media->priv;
1839 GstRTSPMediaStatus result;
1842 g_mutex_lock (&priv->lock);
1843 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
1844 /* while we are preparing, wait */
1845 while (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1846 GST_DEBUG ("waiting for status change");
1847 if (!g_cond_wait_until (&priv->cond, &priv->lock, end_time)) {
1848 GST_DEBUG ("timeout, assuming error status");
1849 priv->status = GST_RTSP_MEDIA_STATUS_ERROR;
1852 /* could be success or error */
1853 result = priv->status;
1854 GST_DEBUG ("got status %d", result);
1855 g_mutex_unlock (&priv->lock);
1861 * gst_rtsp_media_seek:
1862 * @media: a #GstRTSPMedia
1863 * @range: (transfer none): a #GstRTSPTimeRange
1865 * Seek the pipeline of @media to @range. @media must be prepared with
1866 * gst_rtsp_media_prepare().
1868 * Returns: %TRUE on success.
1871 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
1873 GstRTSPMediaClass *klass;
1874 GstRTSPMediaPrivate *priv;
1876 GstClockTime start, stop;
1877 GstSeekType start_type, stop_type;
1879 gint64 current_position;
1881 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1883 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1884 g_return_val_if_fail (range != NULL, FALSE);
1885 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1889 g_rec_mutex_lock (&priv->state_lock);
1890 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1893 /* Update the seekable state of the pipeline in case it changed */
1894 if ((priv->transport_mode & GST_RTSP_TRANSPORT_MODE_RECORD)) {
1895 /* TODO: Seeking for RECORD? */
1896 priv->seekable = FALSE;
1898 query = gst_query_new_seeking (GST_FORMAT_TIME);
1899 if (gst_element_query (priv->pipeline, query)) {
1904 gst_query_parse_seeking (query, &format, &seekable, &start, &end);
1905 priv->seekable = seekable;
1907 gst_query_unref (query);
1910 if (!priv->seekable)
1913 start_type = stop_type = GST_SEEK_TYPE_NONE;
1915 if (!klass->convert_range (media, range, GST_RTSP_RANGE_NPT))
1917 gst_rtsp_range_get_times (range, &start, &stop);
1919 GST_INFO ("got %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1920 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1921 GST_INFO ("current %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1922 GST_TIME_ARGS (priv->range_start), GST_TIME_ARGS (priv->range_stop));
1924 current_position = -1;
1925 if (klass->query_position)
1926 klass->query_position (media, ¤t_position);
1927 GST_INFO ("current media position %" GST_TIME_FORMAT,
1928 GST_TIME_ARGS (current_position));
1930 if (start != GST_CLOCK_TIME_NONE)
1931 start_type = GST_SEEK_TYPE_SET;
1933 if (priv->range_stop == stop)
1934 stop = GST_CLOCK_TIME_NONE;
1935 else if (stop != GST_CLOCK_TIME_NONE)
1936 stop_type = GST_SEEK_TYPE_SET;
1938 if (start != GST_CLOCK_TIME_NONE || stop != GST_CLOCK_TIME_NONE) {
1941 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1942 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1944 /* depends on the current playing state of the pipeline. We might need to
1945 * queue this until we get EOS. */
1946 flags = GST_SEEK_FLAG_FLUSH;
1948 /* if range start was not supplied we must continue from current position.
1949 * but since we're doing a flushing seek, let us query the current position
1950 * so we end up at exactly the same position after the seek. */
1951 if (range->min.type == GST_RTSP_TIME_END) { /* Yepp, that's right! */
1952 if (current_position == -1) {
1953 GST_WARNING ("current position unknown");
1955 GST_DEBUG ("doing accurate seek to %" GST_TIME_FORMAT,
1956 GST_TIME_ARGS (current_position));
1957 start = current_position;
1958 start_type = GST_SEEK_TYPE_SET;
1959 flags |= GST_SEEK_FLAG_ACCURATE;
1962 /* only set keyframe flag when modifying start */
1963 if (start_type != GST_SEEK_TYPE_NONE)
1964 flags |= GST_SEEK_FLAG_KEY_UNIT;
1967 if (start == current_position && stop_type == GST_SEEK_TYPE_NONE) {
1968 GST_DEBUG ("not seeking because no position change");
1971 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
1973 media_streams_set_blocked (media, TRUE);
1975 /* FIXME, we only do forwards playback, no trick modes yet */
1976 res = gst_element_seek (priv->pipeline, 1.0, GST_FORMAT_TIME,
1977 flags, start_type, start, stop_type, stop);
1979 /* and block for the seek to complete */
1980 GST_INFO ("done seeking %d", res);
1984 g_rec_mutex_unlock (&priv->state_lock);
1986 /* wait until pipeline is prerolled again, this will also collect stats */
1987 if (!wait_preroll (media))
1988 goto preroll_failed;
1990 g_rec_mutex_lock (&priv->state_lock);
1991 GST_INFO ("prerolled again");
1994 GST_INFO ("no seek needed");
1997 g_rec_mutex_unlock (&priv->state_lock);
2004 g_rec_mutex_unlock (&priv->state_lock);
2005 GST_INFO ("media %p is not prepared", media);
2010 g_rec_mutex_unlock (&priv->state_lock);
2011 GST_INFO ("pipeline is not seekable");
2016 g_rec_mutex_unlock (&priv->state_lock);
2017 GST_WARNING ("conversion to npt not supported");
2022 g_rec_mutex_unlock (&priv->state_lock);
2023 GST_INFO ("seeking failed");
2024 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2029 GST_WARNING ("failed to preroll after seek");
2035 stream_collect_blocking (GstRTSPStream * stream, gboolean * blocked)
2037 *blocked &= gst_rtsp_stream_is_blocking (stream);
2041 media_streams_blocking (GstRTSPMedia * media)
2043 gboolean blocking = TRUE;
2045 g_ptr_array_foreach (media->priv->streams, (GFunc) stream_collect_blocking,
2051 static GstStateChangeReturn
2052 set_state (GstRTSPMedia * media, GstState state)
2054 GstRTSPMediaPrivate *priv = media->priv;
2055 GstStateChangeReturn ret;
2057 GST_INFO ("set state to %s for media %p", gst_element_state_get_name (state),
2059 ret = gst_element_set_state (priv->pipeline, state);
2064 static GstStateChangeReturn
2065 set_target_state (GstRTSPMedia * media, GstState state, gboolean do_state)
2067 GstRTSPMediaPrivate *priv = media->priv;
2068 GstStateChangeReturn ret;
2070 GST_INFO ("set target state to %s for media %p",
2071 gst_element_state_get_name (state), media);
2072 priv->target_state = state;
2074 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_TARGET_STATE], 0,
2075 priv->target_state, NULL);
2078 ret = set_state (media, state);
2080 ret = GST_STATE_CHANGE_SUCCESS;
2085 /* called with state-lock */
2087 default_handle_message (GstRTSPMedia * media, GstMessage * message)
2089 GstRTSPMediaPrivate *priv = media->priv;
2090 GstMessageType type;
2092 type = GST_MESSAGE_TYPE (message);
2095 case GST_MESSAGE_STATE_CHANGED:
2097 GstState old, new, pending;
2099 if (GST_MESSAGE_SRC (message) != GST_OBJECT (priv->pipeline))
2102 gst_message_parse_state_changed (message, &old, &new, &pending);
2104 GST_DEBUG ("%p: went from %s to %s (pending %s)", media,
2105 gst_element_state_get_name (old), gst_element_state_get_name (new),
2106 gst_element_state_get_name (pending));
2107 if ((priv->transport_mode & GST_RTSP_TRANSPORT_MODE_RECORD)
2108 && old == GST_STATE_READY && new == GST_STATE_PAUSED) {
2109 GST_INFO ("%p: went to PAUSED, prepared now", media);
2110 collect_media_stats (media);
2112 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2113 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2118 case GST_MESSAGE_BUFFERING:
2122 gst_message_parse_buffering (message, &percent);
2124 /* no state management needed for live pipelines */
2128 if (percent == 100) {
2129 /* a 100% message means buffering is done */
2130 priv->buffering = FALSE;
2131 /* if the desired state is playing, go back */
2132 if (priv->target_state == GST_STATE_PLAYING) {
2133 GST_INFO ("Buffering done, setting pipeline to PLAYING");
2134 set_state (media, GST_STATE_PLAYING);
2136 GST_INFO ("Buffering done");
2139 /* buffering busy */
2140 if (priv->buffering == FALSE) {
2141 if (priv->target_state == GST_STATE_PLAYING) {
2142 /* we were not buffering but PLAYING, PAUSE the pipeline. */
2143 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
2144 set_state (media, GST_STATE_PAUSED);
2146 GST_INFO ("Buffering ...");
2149 priv->buffering = TRUE;
2153 case GST_MESSAGE_LATENCY:
2155 gst_bin_recalculate_latency (GST_BIN_CAST (priv->pipeline));
2158 case GST_MESSAGE_ERROR:
2163 gst_message_parse_error (message, &gerror, &debug);
2164 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
2165 g_error_free (gerror);
2168 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2171 case GST_MESSAGE_WARNING:
2176 gst_message_parse_warning (message, &gerror, &debug);
2177 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
2178 g_error_free (gerror);
2182 case GST_MESSAGE_ELEMENT:
2184 const GstStructure *s;
2186 s = gst_message_get_structure (message);
2187 if (gst_structure_has_name (s, "GstRTSPStreamBlocking")) {
2188 GST_DEBUG ("media received blocking message");
2189 if (priv->blocked && media_streams_blocking (media)) {
2190 GST_DEBUG ("media is blocking");
2191 collect_media_stats (media);
2193 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2194 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2199 case GST_MESSAGE_STREAM_STATUS:
2201 case GST_MESSAGE_ASYNC_DONE:
2203 /* when we are dynamically adding pads, the addition of the udpsrc will
2204 * temporarily produce ASYNC_DONE messages. We have to ignore them and
2205 * wait for the final ASYNC_DONE after everything prerolled */
2206 GST_INFO ("%p: ignoring ASYNC_DONE", media);
2208 GST_INFO ("%p: got ASYNC_DONE", media);
2209 collect_media_stats (media);
2211 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2212 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2215 case GST_MESSAGE_EOS:
2216 GST_INFO ("%p: got EOS", media);
2218 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
2219 GST_DEBUG ("shutting down after EOS");
2220 finish_unprepare (media);
2224 GST_INFO ("%p: got message type %d (%s)", media, type,
2225 gst_message_type_get_name (type));
2232 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
2234 GstRTSPMediaPrivate *priv = media->priv;
2235 GstRTSPMediaClass *klass;
2238 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2240 g_rec_mutex_lock (&priv->state_lock);
2241 if (klass->handle_message)
2242 ret = klass->handle_message (media, message);
2245 g_rec_mutex_unlock (&priv->state_lock);
2251 watch_destroyed (GstRTSPMedia * media)
2253 GST_DEBUG_OBJECT (media, "source destroyed");
2254 g_object_unref (media);
2258 find_payload_element (GstElement * payloader)
2260 GstElement *pay = NULL;
2262 if (GST_IS_BIN (payloader)) {
2264 GValue item = { 0 };
2266 iter = gst_bin_iterate_recurse (GST_BIN (payloader));
2267 while (gst_iterator_next (iter, &item) == GST_ITERATOR_OK) {
2268 GstElement *element = (GstElement *) g_value_get_object (&item);
2269 GstElementClass *eclass = GST_ELEMENT_GET_CLASS (element);
2273 gst_element_class_get_metadata (eclass, GST_ELEMENT_METADATA_KLASS);
2277 if (strstr (klass, "Payloader") && strstr (klass, "RTP")) {
2278 pay = gst_object_ref (element);
2279 g_value_unset (&item);
2282 g_value_unset (&item);
2284 gst_iterator_free (iter);
2286 pay = g_object_ref (payloader);
2292 /* called from streaming threads */
2294 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
2296 GstRTSPMediaPrivate *priv = media->priv;
2297 GstRTSPStream *stream;
2300 /* find the real payload element */
2301 pay = find_payload_element (element);
2302 stream = gst_rtsp_media_create_stream (media, pay, pad);
2303 gst_object_unref (pay);
2305 GST_INFO ("pad added %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
2307 g_rec_mutex_lock (&priv->state_lock);
2308 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
2311 g_object_set_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream", stream);
2313 /* we will be adding elements below that will cause ASYNC_DONE to be
2314 * posted in the bus. We want to ignore those messages until the
2315 * pipeline really prerolled. */
2316 priv->adding = TRUE;
2318 /* join the element in the PAUSED state because this callback is
2319 * called from the streaming thread and it is PAUSED */
2320 if (!gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
2321 priv->rtpbin, GST_STATE_PAUSED)) {
2322 GST_WARNING ("failed to join bin element");
2325 priv->adding = FALSE;
2326 g_rec_mutex_unlock (&priv->state_lock);
2333 gst_rtsp_media_remove_stream (media, stream);
2334 g_rec_mutex_unlock (&priv->state_lock);
2335 GST_INFO ("ignore pad because we are not preparing");
2341 pad_removed_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
2343 GstRTSPMediaPrivate *priv = media->priv;
2344 GstRTSPStream *stream;
2346 stream = g_object_get_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream");
2350 GST_INFO ("pad removed %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
2352 g_rec_mutex_lock (&priv->state_lock);
2353 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
2354 g_rec_mutex_unlock (&priv->state_lock);
2356 gst_rtsp_media_remove_stream (media, stream);
2360 remove_fakesink (GstRTSPMediaPrivate * priv)
2362 GstElement *fakesink;
2364 g_mutex_lock (&priv->lock);
2365 if ((fakesink = priv->fakesink))
2366 gst_object_ref (fakesink);
2367 priv->fakesink = NULL;
2368 g_mutex_unlock (&priv->lock);
2371 gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
2372 gst_element_set_state (fakesink, GST_STATE_NULL);
2373 gst_object_unref (fakesink);
2374 GST_INFO ("removed fakesink");
2379 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
2381 GstRTSPMediaPrivate *priv = media->priv;
2383 GST_INFO ("no more pads");
2384 remove_fakesink (priv);
2387 typedef struct _DynPaySignalHandlers DynPaySignalHandlers;
2389 struct _DynPaySignalHandlers
2391 gulong pad_added_handler;
2392 gulong pad_removed_handler;
2393 gulong no_more_pads_handler;
2397 start_preroll (GstRTSPMedia * media)
2399 GstRTSPMediaPrivate *priv = media->priv;
2400 GstStateChangeReturn ret;
2402 GST_INFO ("setting pipeline to PAUSED for media %p", media);
2403 /* first go to PAUSED */
2404 ret = set_target_state (media, GST_STATE_PAUSED, TRUE);
2407 case GST_STATE_CHANGE_SUCCESS:
2408 GST_INFO ("SUCCESS state change for media %p", media);
2409 priv->seekable = TRUE;
2411 case GST_STATE_CHANGE_ASYNC:
2412 GST_INFO ("ASYNC state change for media %p", media);
2413 priv->seekable = TRUE;
2415 case GST_STATE_CHANGE_NO_PREROLL:
2416 /* we need to go to PLAYING */
2417 GST_INFO ("NO_PREROLL state change: live media %p", media);
2418 /* FIXME we disable seeking for live streams for now. We should perform a
2419 * seeking query in preroll instead */
2420 priv->seekable = FALSE;
2421 priv->is_live = TRUE;
2422 if (!(priv->transport_mode & GST_RTSP_TRANSPORT_MODE_RECORD)) {
2423 /* start blocked to make sure nothing goes to the sink */
2424 media_streams_set_blocked (media, TRUE);
2426 ret = set_state (media, GST_STATE_PLAYING);
2427 if (ret == GST_STATE_CHANGE_FAILURE)
2430 case GST_STATE_CHANGE_FAILURE:
2438 GST_WARNING ("failed to preroll pipeline");
2444 wait_preroll (GstRTSPMedia * media)
2446 GstRTSPMediaStatus status;
2448 GST_DEBUG ("wait to preroll pipeline");
2450 /* wait until pipeline is prerolled */
2451 status = gst_rtsp_media_get_status (media);
2452 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
2453 goto preroll_failed;
2459 GST_WARNING ("failed to preroll pipeline");
2465 request_aux_sender (GstElement * rtpbin, guint sessid, GstRTSPMedia * media)
2467 GstRTSPMediaPrivate *priv = media->priv;
2468 GstRTSPStream *stream = NULL;
2471 g_mutex_lock (&priv->lock);
2472 for (i = 0; i < priv->streams->len; i++) {
2473 stream = g_ptr_array_index (priv->streams, i);
2475 if (sessid == gst_rtsp_stream_get_index (stream))
2478 g_mutex_unlock (&priv->lock);
2480 return gst_rtsp_stream_request_aux_sender (stream, sessid);
2484 start_prepare (GstRTSPMedia * media)
2486 GstRTSPMediaPrivate *priv = media->priv;
2490 /* link streams we already have, other streams might appear when we have
2491 * dynamic elements */
2492 for (i = 0; i < priv->streams->len; i++) {
2493 GstRTSPStream *stream;
2495 stream = g_ptr_array_index (priv->streams, i);
2497 if (priv->rtx_time > 0) {
2498 /* enable retransmission by setting rtprtxsend as the "aux" element of rtpbin */
2499 g_signal_connect (priv->rtpbin, "request-aux-sender",
2500 (GCallback) request_aux_sender, media);
2503 if (!gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
2504 priv->rtpbin, GST_STATE_NULL)) {
2505 goto join_bin_failed;
2510 g_object_set (priv->rtpbin, "do-retransmission", priv->rtx_time > 0, NULL);
2512 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2513 GstElement *elem = walk->data;
2514 DynPaySignalHandlers *handlers = g_slice_new (DynPaySignalHandlers);
2516 GST_INFO ("adding callbacks for dynamic element %p", elem);
2518 handlers->pad_added_handler = g_signal_connect (elem, "pad-added",
2519 (GCallback) pad_added_cb, media);
2520 handlers->pad_removed_handler = g_signal_connect (elem, "pad-removed",
2521 (GCallback) pad_removed_cb, media);
2522 handlers->no_more_pads_handler = g_signal_connect (elem, "no-more-pads",
2523 (GCallback) no_more_pads_cb, media);
2525 g_object_set_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers", handlers);
2527 if (!priv->fakesink) {
2528 /* we add a fakesink here in order to make the state change async. We remove
2529 * the fakesink again in the no-more-pads callback. */
2530 priv->fakesink = gst_element_factory_make ("fakesink", "fakesink");
2531 gst_bin_add (GST_BIN (priv->pipeline), priv->fakesink);
2535 if (!start_preroll (media))
2536 goto preroll_failed;
2542 GST_WARNING ("failed to join bin element");
2543 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2548 GST_WARNING ("failed to preroll pipeline");
2549 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2555 default_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
2557 GstRTSPMediaPrivate *priv;
2558 GstRTSPMediaClass *klass;
2560 GMainContext *context;
2565 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2567 if (!klass->create_rtpbin)
2568 goto no_create_rtpbin;
2570 priv->rtpbin = klass->create_rtpbin (media);
2571 if (priv->rtpbin != NULL) {
2572 gboolean success = TRUE;
2574 g_object_set (priv->rtpbin, "latency", priv->latency, NULL);
2576 if (klass->setup_rtpbin)
2577 success = klass->setup_rtpbin (media, priv->rtpbin);
2579 if (success == FALSE) {
2580 gst_object_unref (priv->rtpbin);
2581 priv->rtpbin = NULL;
2584 if (priv->rtpbin == NULL)
2587 priv->thread = thread;
2588 context = (thread != NULL) ? (thread->context) : NULL;
2590 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (priv->pipeline));
2592 /* add the pipeline bus to our custom mainloop */
2593 priv->source = gst_bus_create_watch (bus);
2594 gst_object_unref (bus);
2596 g_source_set_callback (priv->source, (GSourceFunc) bus_message,
2597 g_object_ref (media), (GDestroyNotify) watch_destroyed);
2599 priv->id = g_source_attach (priv->source, context);
2601 /* add stuff to the bin */
2602 gst_bin_add (GST_BIN (priv->pipeline), priv->rtpbin);
2604 /* do remainder in context */
2605 source = g_idle_source_new ();
2606 g_source_set_callback (source, (GSourceFunc) start_prepare,
2607 g_object_ref (media), (GDestroyNotify) g_object_unref);
2608 g_source_attach (source, context);
2609 g_source_unref (source);
2616 GST_ERROR ("no create_rtpbin function");
2617 g_critical ("no create_rtpbin vmethod function set");
2622 GST_WARNING ("no rtpbin element");
2623 g_warning ("failed to create element 'rtpbin', check your installation");
2629 * gst_rtsp_media_prepare:
2630 * @media: a #GstRTSPMedia
2631 * @thread: (transfer full) (allow-none): a #GstRTSPThread to run the
2632 * bus handler or %NULL
2634 * Prepare @media for streaming. This function will create the objects
2635 * to manage the streaming. A pipeline must have been set on @media with
2636 * gst_rtsp_media_take_pipeline().
2638 * It will preroll the pipeline and collect vital information about the streams
2639 * such as the duration.
2641 * Returns: %TRUE on success.
2644 gst_rtsp_media_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
2646 GstRTSPMediaPrivate *priv;
2647 GstRTSPMediaClass *klass;
2649 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2653 g_rec_mutex_lock (&priv->state_lock);
2654 priv->prepare_count++;
2656 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED ||
2657 priv->status == GST_RTSP_MEDIA_STATUS_SUSPENDED)
2660 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2663 if (priv->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
2664 goto not_unprepared;
2666 if (!priv->reusable && priv->reused)
2669 GST_INFO ("preparing media %p", media);
2671 /* reset some variables */
2672 priv->is_live = FALSE;
2673 priv->seekable = FALSE;
2674 priv->buffering = FALSE;
2676 /* we're preparing now */
2677 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
2679 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2680 if (klass->prepare) {
2681 if (!klass->prepare (media, thread))
2682 goto prepare_failed;
2686 g_rec_mutex_unlock (&priv->state_lock);
2688 /* now wait for all pads to be prerolled, FIXME, we should somehow be
2689 * able to do this async so that we don't block the server thread. */
2690 if (!wait_preroll (media))
2691 goto preroll_failed;
2693 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
2695 GST_INFO ("object %p is prerolled", media);
2702 /* we are not going to use the giving thread, so stop it. */
2704 gst_rtsp_thread_stop (thread);
2709 GST_LOG ("media %p was prepared", media);
2710 /* we are not going to use the giving thread, so stop it. */
2712 gst_rtsp_thread_stop (thread);
2713 g_rec_mutex_unlock (&priv->state_lock);
2719 /* we are not going to use the giving thread, so stop it. */
2721 gst_rtsp_thread_stop (thread);
2722 GST_WARNING ("media %p was not unprepared", media);
2723 priv->prepare_count--;
2724 g_rec_mutex_unlock (&priv->state_lock);
2729 /* we are not going to use the giving thread, so stop it. */
2731 gst_rtsp_thread_stop (thread);
2732 priv->prepare_count--;
2733 g_rec_mutex_unlock (&priv->state_lock);
2734 GST_WARNING ("can not reuse media %p", media);
2739 /* we are not going to use the giving thread, so stop it. */
2741 gst_rtsp_thread_stop (thread);
2742 priv->prepare_count--;
2743 g_rec_mutex_unlock (&priv->state_lock);
2744 GST_ERROR ("failed to prepare media");
2749 GST_WARNING ("failed to preroll pipeline");
2750 gst_rtsp_media_unprepare (media);
2755 /* must be called with state-lock */
2757 finish_unprepare (GstRTSPMedia * media)
2759 GstRTSPMediaPrivate *priv = media->priv;
2763 GST_DEBUG ("shutting down");
2765 /* release the lock on shutdown, otherwise pad_added_cb might try to
2766 * acquire the lock and then we deadlock */
2767 g_rec_mutex_unlock (&priv->state_lock);
2768 set_state (media, GST_STATE_NULL);
2769 g_rec_mutex_lock (&priv->state_lock);
2770 remove_fakesink (priv);
2772 for (i = 0; i < priv->streams->len; i++) {
2773 GstRTSPStream *stream;
2775 GST_INFO ("Removing elements of stream %d from pipeline", i);
2777 stream = g_ptr_array_index (priv->streams, i);
2779 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
2782 /* remove the pad signal handlers */
2783 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2784 GstElement *elem = walk->data;
2785 DynPaySignalHandlers *handlers;
2788 g_object_steal_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers");
2789 g_assert (handlers != NULL);
2791 g_signal_handler_disconnect (G_OBJECT (elem), handlers->pad_added_handler);
2792 g_signal_handler_disconnect (G_OBJECT (elem),
2793 handlers->pad_removed_handler);
2794 g_signal_handler_disconnect (G_OBJECT (elem),
2795 handlers->no_more_pads_handler);
2797 g_slice_free (DynPaySignalHandlers, handlers);
2800 gst_bin_remove (GST_BIN (priv->pipeline), priv->rtpbin);
2801 priv->rtpbin = NULL;
2804 gst_object_unref (priv->nettime);
2805 priv->nettime = NULL;
2807 priv->reused = TRUE;
2808 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_UNPREPARED);
2810 /* when the media is not reusable, this will effectively unref the media and
2812 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
2814 /* the source has the last ref to the media */
2816 GST_DEBUG ("destroy source");
2817 g_source_destroy (priv->source);
2818 g_source_unref (priv->source);
2821 GST_DEBUG ("stop thread");
2822 gst_rtsp_thread_stop (priv->thread);
2826 /* called with state-lock */
2828 default_unprepare (GstRTSPMedia * media)
2830 GstRTSPMediaPrivate *priv = media->priv;
2832 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_UNPREPARING);
2834 if (priv->eos_shutdown) {
2835 GST_DEBUG ("sending EOS for shutdown");
2836 /* ref so that we don't disappear */
2837 gst_element_send_event (priv->pipeline, gst_event_new_eos ());
2838 /* we need to go to playing again for the EOS to propagate, normally in this
2839 * state, nothing is receiving data from us anymore so this is ok. */
2840 set_state (media, GST_STATE_PLAYING);
2842 finish_unprepare (media);
2848 * gst_rtsp_media_unprepare:
2849 * @media: a #GstRTSPMedia
2851 * Unprepare @media. After this call, the media should be prepared again before
2852 * it can be used again. If the media is set to be non-reusable, a new instance
2855 * Returns: %TRUE on success.
2858 gst_rtsp_media_unprepare (GstRTSPMedia * media)
2860 GstRTSPMediaPrivate *priv;
2863 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2867 g_rec_mutex_lock (&priv->state_lock);
2868 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
2869 goto was_unprepared;
2871 priv->prepare_count--;
2872 if (priv->prepare_count > 0)
2875 GST_INFO ("unprepare media %p", media);
2877 media_streams_set_blocked (media, FALSE);
2878 set_target_state (media, GST_STATE_NULL, FALSE);
2881 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
2882 GstRTSPMediaClass *klass;
2884 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2885 if (klass->unprepare)
2886 success = klass->unprepare (media);
2888 finish_unprepare (media);
2890 g_rec_mutex_unlock (&priv->state_lock);
2896 g_rec_mutex_unlock (&priv->state_lock);
2897 GST_INFO ("media %p was already unprepared", media);
2902 GST_INFO ("media %p still prepared %d times", media, priv->prepare_count);
2903 g_rec_mutex_unlock (&priv->state_lock);
2908 /* should be called with state-lock */
2910 get_clock_unlocked (GstRTSPMedia * media)
2912 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED) {
2913 GST_DEBUG_OBJECT (media, "media was not prepared");
2916 return gst_pipeline_get_clock (GST_PIPELINE_CAST (media->priv->pipeline));
2920 * gst_rtsp_media_get_clock:
2921 * @media: a #GstRTSPMedia
2923 * Get the clock that is used by the pipeline in @media.
2925 * @media must be prepared before this method returns a valid clock object.
2927 * Returns: (transfer full): the #GstClock used by @media. unref after usage.
2930 gst_rtsp_media_get_clock (GstRTSPMedia * media)
2933 GstRTSPMediaPrivate *priv;
2935 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2939 g_rec_mutex_lock (&priv->state_lock);
2940 clock = get_clock_unlocked (media);
2941 g_rec_mutex_unlock (&priv->state_lock);
2947 * gst_rtsp_media_get_base_time:
2948 * @media: a #GstRTSPMedia
2950 * Get the base_time that is used by the pipeline in @media.
2952 * @media must be prepared before this method returns a valid base_time.
2954 * Returns: the base_time used by @media.
2957 gst_rtsp_media_get_base_time (GstRTSPMedia * media)
2959 GstClockTime result;
2960 GstRTSPMediaPrivate *priv;
2962 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_CLOCK_TIME_NONE);
2966 g_rec_mutex_lock (&priv->state_lock);
2967 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2970 result = gst_element_get_base_time (media->priv->pipeline);
2971 g_rec_mutex_unlock (&priv->state_lock);
2978 g_rec_mutex_unlock (&priv->state_lock);
2979 GST_DEBUG_OBJECT (media, "media was not prepared");
2980 return GST_CLOCK_TIME_NONE;
2985 * gst_rtsp_media_get_time_provider:
2986 * @media: a #GstRTSPMedia
2987 * @address: (allow-none): an address or %NULL
2988 * @port: a port or 0
2990 * Get the #GstNetTimeProvider for the clock used by @media. The time provider
2991 * will listen on @address and @port for client time requests.
2993 * Returns: (transfer full): the #GstNetTimeProvider of @media.
2995 GstNetTimeProvider *
2996 gst_rtsp_media_get_time_provider (GstRTSPMedia * media, const gchar * address,
2999 GstRTSPMediaPrivate *priv;
3000 GstNetTimeProvider *provider = NULL;
3002 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
3006 g_rec_mutex_lock (&priv->state_lock);
3007 if (priv->time_provider) {
3008 if ((provider = priv->nettime) == NULL) {
3011 if (priv->time_provider && (clock = get_clock_unlocked (media))) {
3012 provider = gst_net_time_provider_new (clock, address, port);
3013 gst_object_unref (clock);
3015 priv->nettime = provider;
3019 g_rec_mutex_unlock (&priv->state_lock);
3022 gst_object_ref (provider);
3028 default_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp, GstSDPInfo * info)
3030 return gst_rtsp_sdp_from_media (sdp, info, media);
3034 * gst_rtsp_media_setup_sdp:
3035 * @media: a #GstRTSPMedia
3036 * @sdp: (transfer none): a #GstSDPMessage
3037 * @info: (transfer none): a #GstSDPInfo
3039 * Add @media specific info to @sdp. @info is used to configure the connection
3040 * information in the SDP.
3042 * Returns: TRUE on success.
3045 gst_rtsp_media_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp,
3048 GstRTSPMediaPrivate *priv;
3049 GstRTSPMediaClass *klass;
3052 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3053 g_return_val_if_fail (sdp != NULL, FALSE);
3054 g_return_val_if_fail (info != NULL, FALSE);
3058 g_rec_mutex_lock (&priv->state_lock);
3060 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3062 if (!klass->setup_sdp)
3065 res = klass->setup_sdp (media, sdp, info);
3067 g_rec_mutex_unlock (&priv->state_lock);
3074 g_rec_mutex_unlock (&priv->state_lock);
3075 GST_ERROR ("no setup_sdp function");
3076 g_critical ("no setup_sdp vmethod function set");
3081 static const gchar *
3082 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
3091 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
3094 if (sscanf (attr, "%d ", &val) != 1)
3103 #define PARSE_INT(p, del, res) \
3106 p = strstr (p, del); \
3116 #define PARSE_STRING(p, del, res) \
3119 p = strstr (p, del); \
3131 #define SKIP_SPACES(p) \
3132 while (*p && g_ascii_isspace (*p)) \
3137 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
3140 parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
3141 gint * rate, gchar ** params)
3145 p = (gchar *) rtpmap;
3147 PARSE_INT (p, " ", *payload);
3155 PARSE_STRING (p, "/", *name);
3156 if (*name == NULL) {
3157 GST_DEBUG ("no rate, name %s", p);
3158 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
3159 * streams seem to omit the rate. */
3166 p = strstr (p, "/");
3184 * Mapping of caps to and from SDP fields:
3186 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
3187 * a=framesize:<payload> <width>-<height>
3188 * a=fmtp:<payload> <param>[=<value>];...
3191 media_to_caps (gint pt, const GstSDPMedia * media)
3194 const gchar *rtpmap;
3196 const gchar *framesize;
3199 gchar *params = NULL;
3205 /* get and parse rtpmap */
3206 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
3209 ret = parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
3211 g_warning ("error parsing rtpmap, ignoring");
3215 /* dynamic payloads need rtpmap or we fail */
3216 if (rtpmap == NULL && pt >= 96)
3219 /* check if we have a rate, if not, we need to look up the rate from the
3220 * default rates based on the payload types. */
3222 const GstRTPPayloadInfo *info;
3224 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
3225 /* dynamic types, use media and encoding_name */
3226 tmp = g_ascii_strdown (media->media, -1);
3227 info = gst_rtp_payload_info_for_name (tmp, name);
3230 /* static types, use payload type */
3231 info = gst_rtp_payload_info_for_pt (pt);
3235 if ((rate = info->clock_rate) == 0)
3238 /* we fail if we cannot find one */
3243 tmp = g_ascii_strdown (media->media, -1);
3244 caps = gst_caps_new_simple ("application/x-unknown",
3245 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
3247 s = gst_caps_get_structure (caps, 0);
3249 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
3251 /* encoding name must be upper case */
3253 tmp = g_ascii_strup (name, -1);
3254 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
3258 /* params must be lower case */
3259 if (params != NULL) {
3260 tmp = g_ascii_strdown (params, -1);
3261 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
3265 /* parse optional fmtp: field */
3266 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
3272 /* p is now of the format <payload> <param>[=<value>];... */
3273 PARSE_INT (p, " ", payload);
3274 if (payload != -1 && payload == pt) {
3278 /* <param>[=<value>] are separated with ';' */
3279 pairs = g_strsplit (p, ";", 0);
3280 for (i = 0; pairs[i]; i++) {
3282 const gchar *val, *key;
3284 const gchar *reserved_keys[] =
3285 { "media", "payload", "clock-rate", "encoding-name",
3289 /* the key may not have a '=', the value can have other '='s */
3290 valpos = strstr (pairs[i], "=");
3292 /* we have a '=' and thus a value, remove the '=' with \0 */
3294 /* value is everything between '=' and ';'. We split the pairs at ;
3295 * boundaries so we can take the remainder of the value. Some servers
3296 * put spaces around the value which we strip off here. Alternatively
3297 * we could strip those spaces in the depayloaders should these spaces
3298 * actually carry any meaning in the future. */
3299 val = g_strstrip (valpos + 1);
3301 /* simple <param>;.. is translated into <param>=1;... */
3304 /* strip the key of spaces, convert key to lowercase but not the value. */
3305 key = g_strstrip (pairs[i]);
3307 /* skip keys from the fmtp, which we already use ourselves for the
3308 * caps. Some software is adding random things like clock-rate into
3309 * the fmtp, and we would otherwise here set a string-typed clock-rate
3310 * in the caps... and thus fail to create valid RTP caps
3312 for (j = 0; j < G_N_ELEMENTS (reserved_keys); j++) {
3313 if (g_ascii_strcasecmp (reserved_keys[j], key) == 0) {
3319 if (strlen (key) > 1) {
3320 tmp = g_ascii_strdown (key, -1);
3321 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
3329 /* parse framesize: field */
3330 if ((framesize = gst_sdp_media_get_attribute_val (media, "framesize"))) {
3333 /* p is now of the format <payload> <width>-<height> */
3334 p = (gchar *) framesize;
3336 PARSE_INT (p, " ", payload);
3337 if (payload != -1 && payload == pt) {
3338 gst_structure_set (s, "a-framesize", G_TYPE_STRING, p, NULL);
3346 g_warning ("rtpmap type not given for dynamic payload %d", pt);
3351 g_warning ("rate unknown for payload type %d", pt);
3357 parse_keymgmt (const gchar * keymgmt, GstCaps * caps)
3359 gboolean res = FALSE;
3362 GstMIKEYMessage *msg;
3363 const GstMIKEYPayload *payload;
3364 const gchar *srtp_cipher;
3365 const gchar *srtp_auth;
3371 p = orig_value = g_strdup (keymgmt);
3375 g_free (orig_value);
3379 PARSE_STRING (p, " ", kmpid);
3380 if (kmpid == NULL || !g_str_equal (kmpid, "mikey")) {
3381 g_free (orig_value);
3384 data = g_base64_decode (p, &size);
3386 g_free (orig_value); /* Don't need this any more */
3392 msg = gst_mikey_message_new_from_data (data, size, NULL, NULL);
3397 srtp_cipher = "aes-128-icm";
3398 srtp_auth = "hmac-sha1-80";
3400 /* check the Security policy if any */
3401 if ((payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, 0))) {
3402 GstMIKEYPayloadSP *p = (GstMIKEYPayloadSP *) payload;
3405 if (p->proto != GST_MIKEY_SEC_PROTO_SRTP)
3408 len = gst_mikey_payload_sp_get_n_params (payload);
3409 for (i = 0; i < len; i++) {
3410 const GstMIKEYPayloadSPParam *param =
3411 gst_mikey_payload_sp_get_param (payload, i);
3413 switch (param->type) {
3414 case GST_MIKEY_SP_SRTP_ENC_ALG:
3415 switch (param->val[0]) {
3417 srtp_cipher = "null";
3421 srtp_cipher = "aes-128-icm";
3427 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
3428 switch (param->val[0]) {
3429 case AES_128_KEY_LEN:
3430 srtp_cipher = "aes-128-icm";
3432 case AES_256_KEY_LEN:
3433 srtp_cipher = "aes-256-icm";
3439 case GST_MIKEY_SP_SRTP_AUTH_ALG:
3440 switch (param->val[0]) {
3446 srtp_auth = "hmac-sha1-80";
3452 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
3453 switch (param->val[0]) {
3454 case HMAC_32_KEY_LEN:
3455 srtp_auth = "hmac-sha1-32";
3457 case HMAC_80_KEY_LEN:
3458 srtp_auth = "hmac-sha1-80";
3464 case GST_MIKEY_SP_SRTP_SRTP_ENC:
3466 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
3474 if (!(payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_KEMAC, 0)))
3477 GstMIKEYPayloadKEMAC *p = (GstMIKEYPayloadKEMAC *) payload;
3478 const GstMIKEYPayload *sub;
3479 GstMIKEYPayloadKeyData *pkd;
3482 if (p->enc_alg != GST_MIKEY_ENC_NULL || p->mac_alg != GST_MIKEY_MAC_NULL)
3485 if (!(sub = gst_mikey_payload_kemac_get_sub (payload, 0)))
3488 if (sub->type != GST_MIKEY_PT_KEY_DATA)
3491 pkd = (GstMIKEYPayloadKeyData *) sub;
3493 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
3495 gst_caps_set_simple (caps, "srtp-key", GST_TYPE_BUFFER, buf, NULL);
3498 gst_caps_set_simple (caps,
3499 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
3500 "srtp-auth", G_TYPE_STRING, srtp_auth,
3501 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
3502 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
3506 gst_mikey_message_unref (msg);
3512 * Mapping SDP attributes to caps
3514 * prepend 'a-' to IANA registered sdp attributes names
3515 * (ie: not prefixed with 'x-') in order to avoid
3516 * collision with gstreamer standard caps properties names
3519 sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
3521 if (attributes->len > 0) {
3525 s = gst_caps_get_structure (caps, 0);
3527 for (i = 0; i < attributes->len; i++) {
3528 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
3529 gchar *tofree, *key;
3533 /* skip some of the attribute we already handle */
3534 if (!strcmp (key, "fmtp"))
3536 if (!strcmp (key, "rtpmap"))
3538 if (!strcmp (key, "control"))
3540 if (!strcmp (key, "range"))
3542 if (!strcmp (key, "framesize"))
3544 if (g_str_equal (key, "key-mgmt")) {
3545 parse_keymgmt (attr->value, caps);
3549 /* string must be valid UTF8 */
3550 if (!g_utf8_validate (attr->value, -1, NULL))
3553 if (!g_str_has_prefix (key, "x-"))
3554 tofree = key = g_strdup_printf ("a-%s", key);
3558 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
3559 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
3566 default_handle_sdp (GstRTSPMedia * media, GstSDPMessage * sdp)
3568 GstRTSPMediaPrivate *priv = media->priv;
3571 medias_len = gst_sdp_message_medias_len (sdp);
3572 if (medias_len != priv->streams->len) {
3573 GST_ERROR ("%p: Media has more or less streams than SDP (%d /= %d)", media,
3574 priv->streams->len, medias_len);
3578 for (i = 0; i < medias_len; i++) {
3579 const gchar *proto, *media_type;
3580 const GstSDPMedia *sdp_media = gst_sdp_message_get_media (sdp, i);
3581 GstRTSPStream *stream;
3582 gint j, formats_len;
3583 const gchar *control;
3584 GstRTSPProfile profile, profiles;
3586 stream = g_ptr_array_index (priv->streams, i);
3588 /* TODO: Should we do something with the other SDP information? */
3591 proto = gst_sdp_media_get_proto (sdp_media);
3592 if (proto == NULL) {
3593 GST_ERROR ("%p: SDP media %d has no proto", media, i);
3597 if (g_str_equal (proto, "RTP/AVP")) {
3598 media_type = "application/x-rtp";
3599 profile = GST_RTSP_PROFILE_AVP;
3600 } else if (g_str_equal (proto, "RTP/SAVP")) {
3601 media_type = "application/x-srtp";
3602 profile = GST_RTSP_PROFILE_SAVP;
3603 } else if (g_str_equal (proto, "RTP/AVPF")) {
3604 media_type = "application/x-rtp";
3605 profile = GST_RTSP_PROFILE_AVPF;
3606 } else if (g_str_equal (proto, "RTP/SAVPF")) {
3607 media_type = "application/x-srtp";
3608 profile = GST_RTSP_PROFILE_SAVPF;
3610 GST_ERROR ("%p: unsupported profile '%s' for stream %d", media, proto, i);
3614 profiles = gst_rtsp_stream_get_profiles (stream);
3615 if ((profiles & profile) == 0) {
3616 GST_ERROR ("%p: unsupported profile '%s' for stream %d", media, proto, i);
3620 formats_len = gst_sdp_media_formats_len (sdp_media);
3621 for (j = 0; j < formats_len; j++) {
3626 pt = atoi (gst_sdp_media_get_format (sdp_media, j));
3628 GST_DEBUG (" looking at %d pt: %d", j, pt);
3631 caps = media_to_caps (pt, sdp_media);
3633 GST_WARNING (" skipping pt %d without caps", pt);
3637 /* do some tweaks */
3638 GST_DEBUG ("mapping sdp session level attributes to caps");
3639 sdp_attributes_to_caps (sdp->attributes, caps);
3640 GST_DEBUG ("mapping sdp media level attributes to caps");
3641 sdp_attributes_to_caps (sdp_media->attributes, caps);
3643 s = gst_caps_get_structure (caps, 0);
3644 gst_structure_set_name (s, media_type);
3646 gst_rtsp_stream_set_pt_map (stream, pt, caps);
3647 gst_caps_unref (caps);
3650 control = gst_sdp_media_get_attribute_val (sdp_media, "control");
3652 gst_rtsp_stream_set_control (stream, control);
3660 * gst_rtsp_media_handle_sdp:
3661 * @media: a #GstRTSPMedia
3662 * @sdp: (transfer none): a #GstSDPMessage
3664 * Configure an SDP on @media for receiving streams
3666 * Returns: TRUE on success.
3669 gst_rtsp_media_handle_sdp (GstRTSPMedia * media, GstSDPMessage * sdp)
3671 GstRTSPMediaPrivate *priv;
3672 GstRTSPMediaClass *klass;
3675 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3676 g_return_val_if_fail (sdp != NULL, FALSE);
3680 g_rec_mutex_lock (&priv->state_lock);
3682 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3684 if (!klass->handle_sdp)
3687 res = klass->handle_sdp (media, sdp);
3689 g_rec_mutex_unlock (&priv->state_lock);
3696 g_rec_mutex_unlock (&priv->state_lock);
3697 GST_ERROR ("no handle_sdp function");
3698 g_critical ("no handle_sdp vmethod function set");
3704 do_set_seqnum (GstRTSPStream * stream)
3707 seq_num = gst_rtsp_stream_get_current_seqnum (stream);
3708 gst_rtsp_stream_set_seqnum_offset (stream, seq_num + 1);
3711 /* call with state_lock */
3713 default_suspend (GstRTSPMedia * media)
3715 GstRTSPMediaPrivate *priv = media->priv;
3716 GstStateChangeReturn ret;
3717 gboolean unblock = FALSE;
3719 switch (priv->suspend_mode) {
3720 case GST_RTSP_SUSPEND_MODE_NONE:
3721 GST_DEBUG ("media %p no suspend", media);
3723 case GST_RTSP_SUSPEND_MODE_PAUSE:
3724 GST_DEBUG ("media %p suspend to PAUSED", media);
3725 ret = set_target_state (media, GST_STATE_PAUSED, TRUE);
3726 if (ret == GST_STATE_CHANGE_FAILURE)
3730 case GST_RTSP_SUSPEND_MODE_RESET:
3731 GST_DEBUG ("media %p suspend to NULL", media);
3732 ret = set_target_state (media, GST_STATE_NULL, TRUE);
3733 if (ret == GST_STATE_CHANGE_FAILURE)
3735 /* Because payloader needs to set the sequence number as
3736 * monotonic, we need to preserve the sequence number
3737 * after pause. (otherwise going from pause to play, which
3738 * is actually from NULL to PLAY will create a new sequence
3740 g_ptr_array_foreach (priv->streams, (GFunc) do_set_seqnum, NULL);
3747 /* let the streams do the state changes freely, if any */
3749 media_streams_set_blocked (media, FALSE);
3756 GST_WARNING ("failed changing pipeline's state for media %p", media);
3762 * gst_rtsp_media_suspend:
3763 * @media: a #GstRTSPMedia
3765 * Suspend @media. The state of the pipeline managed by @media is set to
3766 * GST_STATE_NULL but all streams are kept. @media can be prepared again
3767 * with gst_rtsp_media_unsuspend()
3769 * @media must be prepared with gst_rtsp_media_prepare();
3771 * Returns: %TRUE on success.
3774 gst_rtsp_media_suspend (GstRTSPMedia * media)
3776 GstRTSPMediaPrivate *priv = media->priv;
3777 GstRTSPMediaClass *klass;
3779 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3781 GST_FIXME ("suspend for dynamic pipelines needs fixing");
3783 g_rec_mutex_lock (&priv->state_lock);
3784 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
3787 /* don't attempt to suspend when something is busy */
3788 if (priv->n_active > 0)
3791 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3792 if (klass->suspend) {
3793 if (!klass->suspend (media))
3794 goto suspend_failed;
3797 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_SUSPENDED);
3799 g_rec_mutex_unlock (&priv->state_lock);
3806 g_rec_mutex_unlock (&priv->state_lock);
3807 GST_WARNING ("media %p was not prepared", media);
3812 g_rec_mutex_unlock (&priv->state_lock);
3813 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
3814 GST_WARNING ("failed to suspend media %p", media);
3819 /* call with state_lock */
3821 default_unsuspend (GstRTSPMedia * media)
3823 GstRTSPMediaPrivate *priv = media->priv;
3825 switch (priv->suspend_mode) {
3826 case GST_RTSP_SUSPEND_MODE_NONE:
3827 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
3829 case GST_RTSP_SUSPEND_MODE_PAUSE:
3830 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
3832 case GST_RTSP_SUSPEND_MODE_RESET:
3834 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
3835 if (!start_preroll (media))
3837 g_rec_mutex_unlock (&priv->state_lock);
3839 if (!wait_preroll (media))
3840 goto preroll_failed;
3842 g_rec_mutex_lock (&priv->state_lock);
3853 GST_WARNING ("failed to preroll pipeline");
3858 GST_WARNING ("failed to preroll pipeline");
3864 * gst_rtsp_media_unsuspend:
3865 * @media: a #GstRTSPMedia
3867 * Unsuspend @media if it was in a suspended state. This method does nothing
3868 * when the media was not in the suspended state.
3870 * Returns: %TRUE on success.
3873 gst_rtsp_media_unsuspend (GstRTSPMedia * media)
3875 GstRTSPMediaPrivate *priv = media->priv;
3876 GstRTSPMediaClass *klass;
3878 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3880 g_rec_mutex_lock (&priv->state_lock);
3881 if (priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
3884 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3885 if (klass->unsuspend) {
3886 if (!klass->unsuspend (media))
3887 goto unsuspend_failed;
3891 g_rec_mutex_unlock (&priv->state_lock);
3898 g_rec_mutex_unlock (&priv->state_lock);
3899 GST_WARNING ("failed to unsuspend media %p", media);
3900 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
3905 /* must be called with state-lock */
3907 media_set_pipeline_state_locked (GstRTSPMedia * media, GstState state)
3909 GstRTSPMediaPrivate *priv = media->priv;
3911 if (state == GST_STATE_NULL) {
3912 gst_rtsp_media_unprepare (media);
3914 GST_INFO ("state %s media %p", gst_element_state_get_name (state), media);
3915 set_target_state (media, state, FALSE);
3916 /* when we are buffering, don't update the state yet, this will be done
3917 * when buffering finishes */
3918 if (priv->buffering) {
3919 GST_INFO ("Buffering busy, delay state change");
3921 if (state == GST_STATE_PLAYING)
3922 /* make sure pads are not blocking anymore when going to PLAYING */
3923 media_streams_set_blocked (media, FALSE);
3925 set_state (media, state);
3927 /* and suspend after pause */
3928 if (state == GST_STATE_PAUSED)
3929 gst_rtsp_media_suspend (media);
3935 * gst_rtsp_media_set_pipeline_state:
3936 * @media: a #GstRTSPMedia
3937 * @state: the target state of the pipeline
3939 * Set the state of the pipeline managed by @media to @state
3942 gst_rtsp_media_set_pipeline_state (GstRTSPMedia * media, GstState state)
3944 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
3946 g_rec_mutex_lock (&media->priv->state_lock);
3947 media_set_pipeline_state_locked (media, state);
3948 g_rec_mutex_unlock (&media->priv->state_lock);
3952 * gst_rtsp_media_set_state:
3953 * @media: a #GstRTSPMedia
3954 * @state: the target state of the media
3955 * @transports: (transfer none) (element-type GstRtspServer.RTSPStreamTransport):
3956 * a #GPtrArray of #GstRTSPStreamTransport pointers
3958 * Set the state of @media to @state and for the transports in @transports.
3960 * @media must be prepared with gst_rtsp_media_prepare();
3962 * Returns: %TRUE on success.
3965 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
3966 GPtrArray * transports)
3968 GstRTSPMediaPrivate *priv;
3970 gboolean activate, deactivate, do_state;
3973 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3974 g_return_val_if_fail (transports != NULL, FALSE);
3978 g_rec_mutex_lock (&priv->state_lock);
3979 if (priv->status == GST_RTSP_MEDIA_STATUS_ERROR)
3981 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
3982 priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
3985 /* NULL and READY are the same */
3986 if (state == GST_STATE_READY)
3987 state = GST_STATE_NULL;
3989 activate = deactivate = FALSE;
3991 GST_INFO ("going to state %s media %p, target state %s",
3992 gst_element_state_get_name (state), media,
3993 gst_element_state_get_name (priv->target_state));
3996 case GST_STATE_NULL:
3997 /* we're going from PLAYING or PAUSED to READY or NULL, deactivate */
3998 if (priv->target_state >= GST_STATE_PAUSED)
4001 case GST_STATE_PAUSED:
4002 /* we're going from PLAYING to PAUSED, READY or NULL, deactivate */
4003 if (priv->target_state == GST_STATE_PLAYING)
4006 case GST_STATE_PLAYING:
4007 /* we're going to PLAYING, activate */
4013 old_active = priv->n_active;
4015 GST_DEBUG ("%d transports, activate %d, deactivate %d", transports->len,
4016 activate, deactivate);
4017 for (i = 0; i < transports->len; i++) {
4018 GstRTSPStreamTransport *trans;
4020 /* we need a non-NULL entry in the array */
4021 trans = g_ptr_array_index (transports, i);
4026 if (gst_rtsp_stream_transport_set_active (trans, TRUE))
4028 } else if (deactivate) {
4029 if (gst_rtsp_stream_transport_set_active (trans, FALSE))
4034 /* we just activated the first media, do the playing state change */
4035 if (old_active == 0 && activate)
4037 /* if we have no more active media, do the downward state changes */
4038 else if (priv->n_active == 0)
4043 GST_INFO ("state %d active %d media %p do_state %d", state, priv->n_active,
4046 if (priv->target_state != state) {
4048 media_set_pipeline_state_locked (media, state);
4050 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
4054 /* remember where we are */
4055 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
4056 old_active != priv->n_active))
4057 collect_media_stats (media);
4059 g_rec_mutex_unlock (&priv->state_lock);
4066 GST_WARNING ("media %p was not prepared", media);
4067 g_rec_mutex_unlock (&priv->state_lock);
4072 GST_WARNING ("media %p in error status while changing to state %d",
4074 if (state == GST_STATE_NULL) {
4075 for (i = 0; i < transports->len; i++) {
4076 GstRTSPStreamTransport *trans;
4078 /* we need a non-NULL entry in the array */
4079 trans = g_ptr_array_index (transports, i);
4083 gst_rtsp_stream_transport_set_active (trans, FALSE);
4087 g_rec_mutex_unlock (&priv->state_lock);
4093 * gst_rtsp_media_set_transport_mode:
4094 * @media: a #GstRTSPMedia
4095 * @mode: the new value
4097 * Sets if the media pipeline can work in PLAY or RECORD mode
4100 gst_rtsp_media_set_transport_mode (GstRTSPMedia * media,
4101 GstRTSPTransportMode mode)
4103 GstRTSPMediaPrivate *priv;
4105 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
4109 g_mutex_lock (&priv->lock);
4110 priv->transport_mode = mode;
4111 g_mutex_unlock (&priv->lock);
4115 * gst_rtsp_media_get_transport_mode:
4116 * @media: a #GstRTSPMedia
4118 * Check if the pipeline for @media can be used for PLAY or RECORD methods.
4120 * Returns: The transport mode.
4122 GstRTSPTransportMode
4123 gst_rtsp_media_get_transport_mode (GstRTSPMedia * media)
4125 GstRTSPMediaPrivate *priv;
4126 GstRTSPTransportMode res;
4128 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
4132 g_mutex_lock (&priv->lock);
4133 res = priv->transport_mode;
4134 g_mutex_unlock (&priv->lock);