2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3 * Copyright (C) 2015 Centricular Ltd
4 * Author: Sebastian Dröge <sebastian@centricular.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 * @short_description: The media pipeline
24 * @see_also: #GstRTSPMediaFactory, #GstRTSPStream, #GstRTSPSession,
25 * #GstRTSPSessionMedia
27 * a #GstRTSPMedia contains the complete GStreamer pipeline to manage the
28 * streaming to the clients. The actual data transfer is done by the
29 * #GstRTSPStream objects that are created and exposed by the #GstRTSPMedia.
31 * The #GstRTSPMedia is usually created from a #GstRTSPMediaFactory when the
32 * client does a DESCRIBE or SETUP of a resource.
34 * A media is created with gst_rtsp_media_new() that takes the element that will
35 * provide the streaming elements. For each of the streams, a new #GstRTSPStream
36 * object needs to be made with the gst_rtsp_media_create_stream() which takes
37 * the payloader element and the source pad that produces the RTP stream.
39 * The pipeline of the media is set to PAUSED with gst_rtsp_media_prepare(). The
40 * prepare method will add rtpbin and sinks and sources to send and receive RTP
41 * and RTCP packets from the clients. Each stream srcpad is connected to an
42 * input into the internal rtpbin.
44 * It is also possible to dynamically create #GstRTSPStream objects during the
45 * prepare phase. With gst_rtsp_media_get_status() you can check the status of
48 * After the media is prepared, it is ready for streaming. It will usually be
49 * managed in a session with gst_rtsp_session_manage_media(). See
50 * #GstRTSPSession and #GstRTSPSessionMedia.
52 * The state of the media can be controlled with gst_rtsp_media_set_state ().
53 * Seeking can be done with gst_rtsp_media_seek().
55 * With gst_rtsp_media_unprepare() the pipeline is stopped and shut down. When
56 * gst_rtsp_media_set_eos_shutdown() an EOS will be sent to the pipeline to
59 * With gst_rtsp_media_set_shared(), the media can be shared between multiple
60 * clients. With gst_rtsp_media_set_reusable() you can control if the pipeline
61 * can be prepared again after an unprepare.
63 * Last reviewed on 2013-07-11 (1.0.0)
70 #include <gst/app/gstappsrc.h>
71 #include <gst/app/gstappsink.h>
73 #include <gst/sdp/gstmikey.h>
74 #include <gst/rtp/gstrtppayloads.h>
76 #define AES_128_KEY_LEN 16
77 #define AES_256_KEY_LEN 32
79 #define HMAC_32_KEY_LEN 4
80 #define HMAC_80_KEY_LEN 10
82 #include "rtsp-media.h"
84 #define GST_RTSP_MEDIA_GET_PRIVATE(obj) \
85 (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GST_TYPE_RTSP_MEDIA, GstRTSPMediaPrivate))
87 struct _GstRTSPMediaPrivate
92 /* protected by lock */
93 GstRTSPPermissions *permissions;
95 gboolean suspend_mode;
97 GstRTSPProfile profiles;
98 GstRTSPLowerTrans protocols;
100 gboolean eos_shutdown;
102 GstRTSPAddressPool *pool;
104 GstRTSPTransportMode transport_mode;
107 GRecMutex state_lock; /* locking order: state lock, lock */
108 GPtrArray *streams; /* protected by lock */
109 GList *dynamic; /* protected by lock */
110 GstRTSPMediaStatus status; /* protected by lock */
115 /* the pipeline for the media */
116 GstElement *pipeline;
117 GstElement *fakesink; /* protected by lock */
120 GstRTSPThread *thread;
122 gboolean time_provider;
123 GstNetTimeProvider *nettime;
128 GstState target_state;
130 /* RTP session manager */
133 /* the range of media */
134 GstRTSPTimeRange range; /* protected by lock */
135 GstClockTime range_start;
136 GstClockTime range_stop;
138 GList *payloads; /* protected by lock */
139 GstClockTime rtx_time; /* protected by lock */
140 guint latency; /* protected by lock */
143 #define DEFAULT_SHARED FALSE
144 #define DEFAULT_SUSPEND_MODE GST_RTSP_SUSPEND_MODE_NONE
145 #define DEFAULT_REUSABLE FALSE
146 #define DEFAULT_PROFILES GST_RTSP_PROFILE_AVP
147 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | \
148 GST_RTSP_LOWER_TRANS_TCP
149 #define DEFAULT_EOS_SHUTDOWN FALSE
150 #define DEFAULT_BUFFER_SIZE 0x80000
151 #define DEFAULT_TIME_PROVIDER FALSE
152 #define DEFAULT_LATENCY 200
153 #define DEFAULT_TRANSPORT_MODE GST_RTSP_TRANSPORT_MODE_PLAY
155 /* define to dump received RTCP packets */
178 SIGNAL_REMOVED_STREAM,
186 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
187 #define GST_CAT_DEFAULT rtsp_media_debug
189 static void gst_rtsp_media_get_property (GObject * object, guint propid,
190 GValue * value, GParamSpec * pspec);
191 static void gst_rtsp_media_set_property (GObject * object, guint propid,
192 const GValue * value, GParamSpec * pspec);
193 static void gst_rtsp_media_finalize (GObject * obj);
195 static gboolean default_handle_message (GstRTSPMedia * media,
196 GstMessage * message);
197 static void finish_unprepare (GstRTSPMedia * media);
198 static gboolean default_prepare (GstRTSPMedia * media, GstRTSPThread * thread);
199 static gboolean default_unprepare (GstRTSPMedia * media);
200 static gboolean default_suspend (GstRTSPMedia * media);
201 static gboolean default_unsuspend (GstRTSPMedia * media);
202 static gboolean default_convert_range (GstRTSPMedia * media,
203 GstRTSPTimeRange * range, GstRTSPRangeUnit unit);
204 static gboolean default_query_position (GstRTSPMedia * media,
206 static gboolean default_query_stop (GstRTSPMedia * media, gint64 * stop);
207 static GstElement *default_create_rtpbin (GstRTSPMedia * media);
208 static gboolean default_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp,
210 static gboolean default_handle_sdp (GstRTSPMedia * media, GstSDPMessage * sdp);
212 static gboolean wait_preroll (GstRTSPMedia * media);
214 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
216 #define C_ENUM(v) ((gint) v)
219 gst_rtsp_suspend_mode_get_type (void)
222 static const GEnumValue values[] = {
223 {C_ENUM (GST_RTSP_SUSPEND_MODE_NONE), "GST_RTSP_SUSPEND_MODE_NONE", "none"},
224 {C_ENUM (GST_RTSP_SUSPEND_MODE_PAUSE), "GST_RTSP_SUSPEND_MODE_PAUSE",
226 {C_ENUM (GST_RTSP_SUSPEND_MODE_RESET), "GST_RTSP_SUSPEND_MODE_RESET",
231 if (g_once_init_enter (&id)) {
232 GType tmp = g_enum_register_static ("GstRTSPSuspendMode", values);
233 g_once_init_leave (&id, tmp);
238 #define C_FLAGS(v) ((guint) v)
241 gst_rtsp_transport_mode_get_type (void)
244 static const GFlagsValue values[] = {
245 {C_FLAGS (GST_RTSP_TRANSPORT_MODE_PLAY), "GST_RTSP_TRANSPORT_MODE_PLAY",
247 {C_FLAGS (GST_RTSP_TRANSPORT_MODE_RECORD), "GST_RTSP_TRANSPORT_MODE_RECORD",
252 if (g_once_init_enter (&id)) {
253 GType tmp = g_flags_register_static ("GstRTSPTransportMode", values);
254 g_once_init_leave (&id, tmp);
259 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
262 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
264 GObjectClass *gobject_class;
266 g_type_class_add_private (klass, sizeof (GstRTSPMediaPrivate));
268 gobject_class = G_OBJECT_CLASS (klass);
270 gobject_class->get_property = gst_rtsp_media_get_property;
271 gobject_class->set_property = gst_rtsp_media_set_property;
272 gobject_class->finalize = gst_rtsp_media_finalize;
274 g_object_class_install_property (gobject_class, PROP_SHARED,
275 g_param_spec_boolean ("shared", "Shared",
276 "If this media pipeline can be shared", DEFAULT_SHARED,
277 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
279 g_object_class_install_property (gobject_class, PROP_SUSPEND_MODE,
280 g_param_spec_enum ("suspend-mode", "Suspend Mode",
281 "How to suspend the media in PAUSED", GST_TYPE_RTSP_SUSPEND_MODE,
282 DEFAULT_SUSPEND_MODE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
284 g_object_class_install_property (gobject_class, PROP_REUSABLE,
285 g_param_spec_boolean ("reusable", "Reusable",
286 "If this media pipeline can be reused after an unprepare",
287 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289 g_object_class_install_property (gobject_class, PROP_PROFILES,
290 g_param_spec_flags ("profiles", "Profiles",
291 "Allowed transfer profiles", GST_TYPE_RTSP_PROFILE,
292 DEFAULT_PROFILES, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
294 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
295 g_param_spec_flags ("protocols", "Protocols",
296 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
297 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
299 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
300 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
301 "Send an EOS event to the pipeline before unpreparing",
302 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
304 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
305 g_param_spec_uint ("buffer-size", "Buffer Size",
306 "The kernel UDP buffer size to use", 0, G_MAXUINT,
307 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
309 g_object_class_install_property (gobject_class, PROP_ELEMENT,
310 g_param_spec_object ("element", "The Element",
311 "The GstBin to use for streaming the media", GST_TYPE_ELEMENT,
312 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
314 g_object_class_install_property (gobject_class, PROP_TIME_PROVIDER,
315 g_param_spec_boolean ("time-provider", "Time Provider",
316 "Use a NetTimeProvider for clients",
317 DEFAULT_TIME_PROVIDER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
319 g_object_class_install_property (gobject_class, PROP_LATENCY,
320 g_param_spec_uint ("latency", "Latency",
321 "Latency used for receiving media in milliseconds", 0, G_MAXUINT,
322 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
324 g_object_class_install_property (gobject_class, PROP_TRANSPORT_MODE,
325 g_param_spec_flags ("transport-mode", "Transport Mode",
326 "If this media pipeline can be used for PLAY or RECORD",
327 GST_TYPE_RTSP_TRANSPORT_MODE, DEFAULT_TRANSPORT_MODE,
328 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
330 gst_rtsp_media_signals[SIGNAL_NEW_STREAM] =
331 g_signal_new ("new-stream", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
332 G_STRUCT_OFFSET (GstRTSPMediaClass, new_stream), NULL, NULL,
333 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_STREAM);
335 gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM] =
336 g_signal_new ("removed-stream", G_TYPE_FROM_CLASS (klass),
337 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, removed_stream),
338 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
339 GST_TYPE_RTSP_STREAM);
341 gst_rtsp_media_signals[SIGNAL_PREPARED] =
342 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
343 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
344 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
346 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
347 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
348 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
349 g_cclosure_marshal_generic, G_TYPE_NONE, 0, G_TYPE_NONE);
351 gst_rtsp_media_signals[SIGNAL_TARGET_STATE] =
352 g_signal_new ("target-state", G_TYPE_FROM_CLASS (klass),
353 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstRTSPMediaClass, target_state),
354 NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
356 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
357 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
358 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
359 g_cclosure_marshal_generic, G_TYPE_NONE, 1, G_TYPE_INT);
361 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
363 klass->handle_message = default_handle_message;
364 klass->prepare = default_prepare;
365 klass->unprepare = default_unprepare;
366 klass->suspend = default_suspend;
367 klass->unsuspend = default_unsuspend;
368 klass->convert_range = default_convert_range;
369 klass->query_position = default_query_position;
370 klass->query_stop = default_query_stop;
371 klass->create_rtpbin = default_create_rtpbin;
372 klass->setup_sdp = default_setup_sdp;
373 klass->handle_sdp = default_handle_sdp;
377 gst_rtsp_media_init (GstRTSPMedia * media)
379 GstRTSPMediaPrivate *priv = GST_RTSP_MEDIA_GET_PRIVATE (media);
383 priv->streams = g_ptr_array_new_with_free_func (g_object_unref);
384 g_mutex_init (&priv->lock);
385 g_cond_init (&priv->cond);
386 g_rec_mutex_init (&priv->state_lock);
388 priv->shared = DEFAULT_SHARED;
389 priv->suspend_mode = DEFAULT_SUSPEND_MODE;
390 priv->reusable = DEFAULT_REUSABLE;
391 priv->profiles = DEFAULT_PROFILES;
392 priv->protocols = DEFAULT_PROTOCOLS;
393 priv->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
394 priv->buffer_size = DEFAULT_BUFFER_SIZE;
395 priv->time_provider = DEFAULT_TIME_PROVIDER;
396 priv->transport_mode = DEFAULT_TRANSPORT_MODE;
400 gst_rtsp_media_finalize (GObject * obj)
402 GstRTSPMediaPrivate *priv;
405 media = GST_RTSP_MEDIA (obj);
408 GST_INFO ("finalize media %p", media);
410 if (priv->permissions)
411 gst_rtsp_permissions_unref (priv->permissions);
413 g_ptr_array_unref (priv->streams);
415 g_list_free_full (priv->dynamic, gst_object_unref);
418 gst_object_unref (priv->pipeline);
420 gst_object_unref (priv->nettime);
421 gst_object_unref (priv->element);
423 g_object_unref (priv->pool);
425 g_list_free (priv->payloads);
426 g_mutex_clear (&priv->lock);
427 g_cond_clear (&priv->cond);
428 g_rec_mutex_clear (&priv->state_lock);
430 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
434 gst_rtsp_media_get_property (GObject * object, guint propid,
435 GValue * value, GParamSpec * pspec)
437 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
441 g_value_set_object (value, media->priv->element);
444 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
446 case PROP_SUSPEND_MODE:
447 g_value_set_enum (value, gst_rtsp_media_get_suspend_mode (media));
450 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
453 g_value_set_flags (value, gst_rtsp_media_get_profiles (media));
456 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
458 case PROP_EOS_SHUTDOWN:
459 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
461 case PROP_BUFFER_SIZE:
462 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
464 case PROP_TIME_PROVIDER:
465 g_value_set_boolean (value, gst_rtsp_media_is_time_provider (media));
468 g_value_set_uint (value, gst_rtsp_media_get_latency (media));
470 case PROP_TRANSPORT_MODE:
471 g_value_set_flags (value, gst_rtsp_media_get_transport_mode (media));
474 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
479 gst_rtsp_media_set_property (GObject * object, guint propid,
480 const GValue * value, GParamSpec * pspec)
482 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
486 media->priv->element = g_value_get_object (value);
487 gst_object_ref_sink (media->priv->element);
490 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
492 case PROP_SUSPEND_MODE:
493 gst_rtsp_media_set_suspend_mode (media, g_value_get_enum (value));
496 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
499 gst_rtsp_media_set_profiles (media, g_value_get_flags (value));
502 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
504 case PROP_EOS_SHUTDOWN:
505 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
507 case PROP_BUFFER_SIZE:
508 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
510 case PROP_TIME_PROVIDER:
511 gst_rtsp_media_use_time_provider (media, g_value_get_boolean (value));
514 gst_rtsp_media_set_latency (media, g_value_get_uint (value));
516 case PROP_TRANSPORT_MODE:
517 gst_rtsp_media_set_transport_mode (media, g_value_get_flags (value));
520 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
528 } DoQueryPositionData;
531 do_query_position (GstRTSPStream * stream, DoQueryPositionData * data)
535 if (gst_rtsp_stream_query_position (stream, &tmp)) {
536 data->position = MAX (data->position, tmp);
542 default_query_position (GstRTSPMedia * media, gint64 * position)
544 GstRTSPMediaPrivate *priv;
545 DoQueryPositionData data;
552 g_ptr_array_foreach (priv->streams, (GFunc) do_query_position, &data);
554 *position = data.position;
566 do_query_stop (GstRTSPStream * stream, DoQueryStopData * data)
570 if (gst_rtsp_stream_query_stop (stream, &tmp)) {
571 data->stop = MAX (data->stop, tmp);
577 default_query_stop (GstRTSPMedia * media, gint64 * stop)
579 GstRTSPMediaPrivate *priv;
580 DoQueryStopData data;
587 g_ptr_array_foreach (priv->streams, (GFunc) do_query_stop, &data);
595 default_create_rtpbin (GstRTSPMedia * media)
599 rtpbin = gst_element_factory_make ("rtpbin", NULL);
604 /* must be called with state lock */
606 collect_media_stats (GstRTSPMedia * media)
608 GstRTSPMediaPrivate *priv = media->priv;
609 gint64 position = 0, stop = -1;
611 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
612 priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
615 priv->range.unit = GST_RTSP_RANGE_NPT;
617 GST_INFO ("collect media stats");
620 priv->range.min.type = GST_RTSP_TIME_NOW;
621 priv->range.min.seconds = -1;
622 priv->range_start = -1;
623 priv->range.max.type = GST_RTSP_TIME_END;
624 priv->range.max.seconds = -1;
625 priv->range_stop = -1;
627 GstRTSPMediaClass *klass;
630 klass = GST_RTSP_MEDIA_GET_CLASS (media);
632 /* get the position */
634 if (klass->query_position)
635 ret = klass->query_position (media, &position);
638 GST_INFO ("position query failed");
642 /* get the current segment stop */
644 if (klass->query_stop)
645 ret = klass->query_stop (media, &stop);
648 GST_INFO ("stop query failed");
652 GST_INFO ("stats: position %" GST_TIME_FORMAT ", stop %"
653 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (stop));
655 if (position == -1) {
656 priv->range.min.type = GST_RTSP_TIME_NOW;
657 priv->range.min.seconds = -1;
658 priv->range_start = -1;
660 priv->range.min.type = GST_RTSP_TIME_SECONDS;
661 priv->range.min.seconds = ((gdouble) position) / GST_SECOND;
662 priv->range_start = position;
665 priv->range.max.type = GST_RTSP_TIME_END;
666 priv->range.max.seconds = -1;
667 priv->range_stop = -1;
669 priv->range.max.type = GST_RTSP_TIME_SECONDS;
670 priv->range.max.seconds = ((gdouble) stop) / GST_SECOND;
671 priv->range_stop = stop;
677 * gst_rtsp_media_new:
678 * @element: (transfer full): a #GstElement
680 * Create a new #GstRTSPMedia instance. @element is the bin element that
681 * provides the different streams. The #GstRTSPMedia object contains the
682 * element to produce RTP data for one or more related (audio/video/..)
685 * Ownership is taken of @element.
687 * Returns: (transfer full): a new #GstRTSPMedia object.
690 gst_rtsp_media_new (GstElement * element)
692 GstRTSPMedia *result;
694 g_return_val_if_fail (GST_IS_ELEMENT (element), NULL);
696 result = g_object_new (GST_TYPE_RTSP_MEDIA, "element", element, NULL);
702 * gst_rtsp_media_get_element:
703 * @media: a #GstRTSPMedia
705 * Get the element that was used when constructing @media.
707 * Returns: (transfer full): a #GstElement. Unref after usage.
710 gst_rtsp_media_get_element (GstRTSPMedia * media)
712 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
714 return gst_object_ref (media->priv->element);
718 * gst_rtsp_media_take_pipeline:
719 * @media: a #GstRTSPMedia
720 * @pipeline: (transfer full): a #GstPipeline
722 * Set @pipeline as the #GstPipeline for @media. Ownership is
723 * taken of @pipeline.
726 gst_rtsp_media_take_pipeline (GstRTSPMedia * media, GstPipeline * pipeline)
728 GstRTSPMediaPrivate *priv;
730 GstNetTimeProvider *nettime;
732 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
733 g_return_if_fail (GST_IS_PIPELINE (pipeline));
737 g_mutex_lock (&priv->lock);
738 old = priv->pipeline;
739 priv->pipeline = GST_ELEMENT_CAST (pipeline);
740 nettime = priv->nettime;
741 priv->nettime = NULL;
742 g_mutex_unlock (&priv->lock);
745 gst_object_unref (old);
748 gst_object_unref (nettime);
750 gst_bin_add (GST_BIN_CAST (pipeline), priv->element);
754 * gst_rtsp_media_set_permissions:
755 * @media: a #GstRTSPMedia
756 * @permissions: (transfer none): a #GstRTSPPermissions
758 * Set @permissions on @media.
761 gst_rtsp_media_set_permissions (GstRTSPMedia * media,
762 GstRTSPPermissions * permissions)
764 GstRTSPMediaPrivate *priv;
766 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
770 g_mutex_lock (&priv->lock);
771 if (priv->permissions)
772 gst_rtsp_permissions_unref (priv->permissions);
773 if ((priv->permissions = permissions))
774 gst_rtsp_permissions_ref (permissions);
775 g_mutex_unlock (&priv->lock);
779 * gst_rtsp_media_get_permissions:
780 * @media: a #GstRTSPMedia
782 * Get the permissions object from @media.
784 * Returns: (transfer full): a #GstRTSPPermissions object, unref after usage.
787 gst_rtsp_media_get_permissions (GstRTSPMedia * media)
789 GstRTSPMediaPrivate *priv;
790 GstRTSPPermissions *result;
792 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
796 g_mutex_lock (&priv->lock);
797 if ((result = priv->permissions))
798 gst_rtsp_permissions_ref (result);
799 g_mutex_unlock (&priv->lock);
805 * gst_rtsp_media_set_suspend_mode:
806 * @media: a #GstRTSPMedia
807 * @mode: the new #GstRTSPSuspendMode
809 * Control how @ media will be suspended after the SDP has been generated and
810 * after a PAUSE request has been performed.
812 * Media must be unprepared when setting the suspend mode.
815 gst_rtsp_media_set_suspend_mode (GstRTSPMedia * media, GstRTSPSuspendMode mode)
817 GstRTSPMediaPrivate *priv;
819 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
823 g_rec_mutex_lock (&priv->state_lock);
824 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED)
826 priv->suspend_mode = mode;
827 g_rec_mutex_unlock (&priv->state_lock);
834 GST_WARNING ("media %p was prepared", media);
835 g_rec_mutex_unlock (&priv->state_lock);
840 * gst_rtsp_media_get_suspend_mode:
841 * @media: a #GstRTSPMedia
843 * Get how @media will be suspended.
845 * Returns: #GstRTSPSuspendMode.
848 gst_rtsp_media_get_suspend_mode (GstRTSPMedia * media)
850 GstRTSPMediaPrivate *priv;
851 GstRTSPSuspendMode res;
853 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_RTSP_SUSPEND_MODE_NONE);
857 g_rec_mutex_lock (&priv->state_lock);
858 res = priv->suspend_mode;
859 g_rec_mutex_unlock (&priv->state_lock);
865 * gst_rtsp_media_set_shared:
866 * @media: a #GstRTSPMedia
867 * @shared: the new value
869 * Set or unset if the pipeline for @media can be shared will multiple clients.
870 * When @shared is %TRUE, client requests for this media will share the media
874 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
876 GstRTSPMediaPrivate *priv;
878 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
882 g_mutex_lock (&priv->lock);
883 priv->shared = shared;
884 g_mutex_unlock (&priv->lock);
888 * gst_rtsp_media_is_shared:
889 * @media: a #GstRTSPMedia
891 * Check if the pipeline for @media can be shared between multiple clients.
893 * Returns: %TRUE if the media can be shared between clients.
896 gst_rtsp_media_is_shared (GstRTSPMedia * media)
898 GstRTSPMediaPrivate *priv;
901 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
905 g_mutex_lock (&priv->lock);
907 g_mutex_unlock (&priv->lock);
913 * gst_rtsp_media_set_reusable:
914 * @media: a #GstRTSPMedia
915 * @reusable: the new value
917 * Set or unset if the pipeline for @media can be reused after the pipeline has
921 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
923 GstRTSPMediaPrivate *priv;
925 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
929 g_mutex_lock (&priv->lock);
930 priv->reusable = reusable;
931 g_mutex_unlock (&priv->lock);
935 * gst_rtsp_media_is_reusable:
936 * @media: a #GstRTSPMedia
938 * Check if the pipeline for @media can be reused after an unprepare.
940 * Returns: %TRUE if the media can be reused
943 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
945 GstRTSPMediaPrivate *priv;
948 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
952 g_mutex_lock (&priv->lock);
953 res = priv->reusable;
954 g_mutex_unlock (&priv->lock);
960 do_set_profiles (GstRTSPStream * stream, GstRTSPProfile * profiles)
962 gst_rtsp_stream_set_profiles (stream, *profiles);
966 * gst_rtsp_media_set_profiles:
967 * @media: a #GstRTSPMedia
968 * @profiles: the new flags
970 * Configure the allowed lower transport for @media.
973 gst_rtsp_media_set_profiles (GstRTSPMedia * media, GstRTSPProfile profiles)
975 GstRTSPMediaPrivate *priv;
977 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
981 g_mutex_lock (&priv->lock);
982 priv->profiles = profiles;
983 g_ptr_array_foreach (priv->streams, (GFunc) do_set_profiles, &profiles);
984 g_mutex_unlock (&priv->lock);
988 * gst_rtsp_media_get_profiles:
989 * @media: a #GstRTSPMedia
991 * Get the allowed profiles of @media.
993 * Returns: a #GstRTSPProfile
996 gst_rtsp_media_get_profiles (GstRTSPMedia * media)
998 GstRTSPMediaPrivate *priv;
1001 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_RTSP_PROFILE_UNKNOWN);
1005 g_mutex_lock (&priv->lock);
1006 res = priv->profiles;
1007 g_mutex_unlock (&priv->lock);
1013 do_set_protocols (GstRTSPStream * stream, GstRTSPLowerTrans * protocols)
1015 gst_rtsp_stream_set_protocols (stream, *protocols);
1019 * gst_rtsp_media_set_protocols:
1020 * @media: a #GstRTSPMedia
1021 * @protocols: the new flags
1023 * Configure the allowed lower transport for @media.
1026 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
1028 GstRTSPMediaPrivate *priv;
1030 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1034 g_mutex_lock (&priv->lock);
1035 priv->protocols = protocols;
1036 g_ptr_array_foreach (priv->streams, (GFunc) do_set_protocols, &protocols);
1037 g_mutex_unlock (&priv->lock);
1041 * gst_rtsp_media_get_protocols:
1042 * @media: a #GstRTSPMedia
1044 * Get the allowed protocols of @media.
1046 * Returns: a #GstRTSPLowerTrans
1049 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
1051 GstRTSPMediaPrivate *priv;
1052 GstRTSPLowerTrans res;
1054 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
1055 GST_RTSP_LOWER_TRANS_UNKNOWN);
1059 g_mutex_lock (&priv->lock);
1060 res = priv->protocols;
1061 g_mutex_unlock (&priv->lock);
1067 * gst_rtsp_media_set_eos_shutdown:
1068 * @media: a #GstRTSPMedia
1069 * @eos_shutdown: the new value
1071 * Set or unset if an EOS event will be sent to the pipeline for @media before
1075 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
1077 GstRTSPMediaPrivate *priv;
1079 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1083 g_mutex_lock (&priv->lock);
1084 priv->eos_shutdown = eos_shutdown;
1085 g_mutex_unlock (&priv->lock);
1089 * gst_rtsp_media_is_eos_shutdown:
1090 * @media: a #GstRTSPMedia
1092 * Check if the pipeline for @media will send an EOS down the pipeline before
1095 * Returns: %TRUE if the media will send EOS before unpreparing.
1098 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
1100 GstRTSPMediaPrivate *priv;
1103 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1107 g_mutex_lock (&priv->lock);
1108 res = priv->eos_shutdown;
1109 g_mutex_unlock (&priv->lock);
1115 * gst_rtsp_media_set_buffer_size:
1116 * @media: a #GstRTSPMedia
1117 * @size: the new value
1119 * Set the kernel UDP buffer size.
1122 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
1124 GstRTSPMediaPrivate *priv;
1126 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1128 GST_LOG_OBJECT (media, "set buffer size %u", size);
1132 g_mutex_lock (&priv->lock);
1133 priv->buffer_size = size;
1134 g_mutex_unlock (&priv->lock);
1138 * gst_rtsp_media_get_buffer_size:
1139 * @media: a #GstRTSPMedia
1141 * Get the kernel UDP buffer size.
1143 * Returns: the kernel UDP buffer size.
1146 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
1148 GstRTSPMediaPrivate *priv;
1151 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1155 g_mutex_lock (&priv->lock);
1156 res = priv->buffer_size;
1157 g_mutex_unlock (&priv->lock);
1163 * gst_rtsp_media_set_retransmission_time:
1164 * @media: a #GstRTSPMedia
1165 * @time: the new value
1167 * Set the amount of time to store retransmission packets.
1170 gst_rtsp_media_set_retransmission_time (GstRTSPMedia * media, GstClockTime time)
1172 GstRTSPMediaPrivate *priv;
1175 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1177 GST_LOG_OBJECT (media, "set retransmission time %" G_GUINT64_FORMAT, time);
1181 g_mutex_lock (&priv->lock);
1182 priv->rtx_time = time;
1183 for (i = 0; i < priv->streams->len; i++) {
1184 GstRTSPStream *stream = g_ptr_array_index (priv->streams, i);
1186 gst_rtsp_stream_set_retransmission_time (stream, time);
1190 g_object_set (priv->rtpbin, "do-retransmission", time > 0, NULL);
1191 g_mutex_unlock (&priv->lock);
1195 * gst_rtsp_media_get_retransmission_time:
1196 * @media: a #GstRTSPMedia
1198 * Get the amount of time to store retransmission data.
1200 * Returns: the amount of time to store retransmission data.
1203 gst_rtsp_media_get_retransmission_time (GstRTSPMedia * media)
1205 GstRTSPMediaPrivate *priv;
1208 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1212 g_mutex_unlock (&priv->lock);
1213 res = priv->rtx_time;
1214 g_mutex_unlock (&priv->lock);
1220 * gst_rtsp_media_set_latncy:
1221 * @media: a #GstRTSPMedia
1222 * @latency: latency in milliseconds
1224 * Configure the latency used for receiving media.
1227 gst_rtsp_media_set_latency (GstRTSPMedia * media, guint latency)
1229 GstRTSPMediaPrivate *priv;
1231 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1233 GST_LOG_OBJECT (media, "set latency %ums", latency);
1237 g_mutex_lock (&priv->lock);
1238 priv->latency = latency;
1240 g_object_set (priv->rtpbin, "latency", latency, NULL);
1241 g_mutex_unlock (&priv->lock);
1245 * gst_rtsp_media_get_latency:
1246 * @media: a #GstRTSPMedia
1248 * Get the latency that is used for receiving media.
1250 * Returns: latency in milliseconds
1253 gst_rtsp_media_get_latency (GstRTSPMedia * media)
1255 GstRTSPMediaPrivate *priv;
1258 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1262 g_mutex_unlock (&priv->lock);
1263 res = priv->latency;
1264 g_mutex_unlock (&priv->lock);
1270 * gst_rtsp_media_use_time_provider:
1271 * @media: a #GstRTSPMedia
1272 * @time_provider: if a #GstNetTimeProvider should be used
1274 * Set @media to provide a #GstNetTimeProvider.
1277 gst_rtsp_media_use_time_provider (GstRTSPMedia * media, gboolean time_provider)
1279 GstRTSPMediaPrivate *priv;
1281 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1285 g_mutex_lock (&priv->lock);
1286 priv->time_provider = time_provider;
1287 g_mutex_unlock (&priv->lock);
1291 * gst_rtsp_media_is_time_provider:
1292 * @media: a #GstRTSPMedia
1294 * Check if @media can provide a #GstNetTimeProvider for its pipeline clock.
1296 * Use gst_rtsp_media_get_time_provider() to get the network clock.
1298 * Returns: %TRUE if @media can provide a #GstNetTimeProvider.
1301 gst_rtsp_media_is_time_provider (GstRTSPMedia * media)
1303 GstRTSPMediaPrivate *priv;
1306 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1310 g_mutex_unlock (&priv->lock);
1311 res = priv->time_provider;
1312 g_mutex_unlock (&priv->lock);
1318 * gst_rtsp_media_set_address_pool:
1319 * @media: a #GstRTSPMedia
1320 * @pool: (transfer none): a #GstRTSPAddressPool
1322 * configure @pool to be used as the address pool of @media.
1325 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
1326 GstRTSPAddressPool * pool)
1328 GstRTSPMediaPrivate *priv;
1329 GstRTSPAddressPool *old;
1331 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1335 GST_LOG_OBJECT (media, "set address pool %p", pool);
1337 g_mutex_lock (&priv->lock);
1338 if ((old = priv->pool) != pool)
1339 priv->pool = pool ? g_object_ref (pool) : NULL;
1342 g_ptr_array_foreach (priv->streams, (GFunc) gst_rtsp_stream_set_address_pool,
1344 g_mutex_unlock (&priv->lock);
1347 g_object_unref (old);
1351 * gst_rtsp_media_get_address_pool:
1352 * @media: a #GstRTSPMedia
1354 * Get the #GstRTSPAddressPool used as the address pool of @media.
1356 * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
1359 GstRTSPAddressPool *
1360 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
1362 GstRTSPMediaPrivate *priv;
1363 GstRTSPAddressPool *result;
1365 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1369 g_mutex_lock (&priv->lock);
1370 if ((result = priv->pool))
1371 g_object_ref (result);
1372 g_mutex_unlock (&priv->lock);
1378 _find_payload_types (GstRTSPMedia * media)
1381 GQueue queue = G_QUEUE_INIT;
1383 n = media->priv->streams->len;
1384 for (i = 0; i < n; i++) {
1385 GstRTSPStream *stream = g_ptr_array_index (media->priv->streams, i);
1386 guint pt = gst_rtsp_stream_get_pt (stream);
1388 g_queue_push_tail (&queue, GUINT_TO_POINTER (pt));
1395 _next_available_pt (GList * payloads)
1399 for (i = 96; i <= 127; i++) {
1400 GList *iter = g_list_find (payloads, GINT_TO_POINTER (i));
1402 return GPOINTER_TO_UINT (i);
1409 * gst_rtsp_media_collect_streams:
1410 * @media: a #GstRTSPMedia
1412 * Find all payloader elements, they should be named pay\%d in the
1413 * element of @media, and create #GstRTSPStreams for them.
1415 * Collect all dynamic elements, named dynpay\%d, and add them to
1416 * the list of dynamic elements.
1418 * Find all depayloader elements, they should be named depay\%d in the
1419 * element of @media, and create #GstRTSPStreams for them.
1422 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
1424 GstRTSPMediaPrivate *priv;
1425 GstElement *element, *elem;
1429 gboolean more_elem_remaining = TRUE;
1430 GstRTSPTransportMode mode = 0;
1432 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
1435 element = priv->element;
1438 for (i = 0; more_elem_remaining; i++) {
1441 more_elem_remaining = FALSE;
1443 name = g_strdup_printf ("pay%d", i);
1444 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1445 GST_INFO ("found stream %d with payloader %p", i, elem);
1447 /* take the pad of the payloader */
1448 pad = gst_element_get_static_pad (elem, "src");
1449 /* create the stream */
1450 gst_rtsp_media_create_stream (media, elem, pad);
1451 gst_object_unref (pad);
1452 gst_object_unref (elem);
1455 more_elem_remaining = TRUE;
1456 mode |= GST_RTSP_TRANSPORT_MODE_PLAY;
1460 name = g_strdup_printf ("dynpay%d", i);
1461 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1462 /* a stream that will dynamically create pads to provide RTP packets */
1463 GST_INFO ("found dynamic element %d, %p", i, elem);
1465 g_mutex_lock (&priv->lock);
1466 priv->dynamic = g_list_prepend (priv->dynamic, elem);
1467 g_mutex_unlock (&priv->lock);
1470 more_elem_remaining = TRUE;
1471 mode |= GST_RTSP_TRANSPORT_MODE_PLAY;
1475 name = g_strdup_printf ("depay%d", i);
1476 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
1477 GST_INFO ("found stream %d with depayloader %p", i, elem);
1479 /* take the pad of the payloader */
1480 pad = gst_element_get_static_pad (elem, "sink");
1481 /* create the stream */
1482 gst_rtsp_media_create_stream (media, elem, pad);
1483 gst_object_unref (pad);
1484 gst_object_unref (elem);
1487 more_elem_remaining = TRUE;
1488 mode |= GST_RTSP_TRANSPORT_MODE_RECORD;
1494 if (priv->transport_mode != mode)
1495 GST_WARNING ("found different mode than expected (0x%02x != 0x%02d)",
1496 priv->transport_mode, mode);
1501 * gst_rtsp_media_create_stream:
1502 * @media: a #GstRTSPMedia
1503 * @payloader: a #GstElement
1506 * Create a new stream in @media that provides RTP data on @pad.
1507 * @pad should be a pad of an element inside @media->element.
1509 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
1513 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
1516 GstRTSPMediaPrivate *priv;
1517 GstRTSPStream *stream;
1522 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1523 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
1524 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
1528 g_mutex_lock (&priv->lock);
1529 idx = priv->streams->len;
1531 GST_DEBUG ("media %p: creating stream with index %d", media, idx);
1533 if (GST_PAD_IS_SRC (pad))
1534 name = g_strdup_printf ("src_%u", idx);
1536 name = g_strdup_printf ("sink_%u", idx);
1538 ghostpad = gst_ghost_pad_new (name, pad);
1539 gst_pad_set_active (ghostpad, TRUE);
1540 gst_element_add_pad (priv->element, ghostpad);
1543 stream = gst_rtsp_stream_new (idx, payloader, ghostpad);
1545 gst_rtsp_stream_set_address_pool (stream, priv->pool);
1546 gst_rtsp_stream_set_profiles (stream, priv->profiles);
1547 gst_rtsp_stream_set_protocols (stream, priv->protocols);
1548 gst_rtsp_stream_set_retransmission_time (stream, priv->rtx_time);
1550 g_ptr_array_add (priv->streams, stream);
1552 if (GST_PAD_IS_SRC (pad)) {
1556 g_list_free (priv->payloads);
1557 priv->payloads = _find_payload_types (media);
1559 n = priv->streams->len;
1560 for (i = 0; i < n; i++) {
1561 GstRTSPStream *stream = g_ptr_array_index (priv->streams, i);
1562 guint rtx_pt = _next_available_pt (priv->payloads);
1565 GST_WARNING ("Ran out of space of dynamic payload types");
1569 gst_rtsp_stream_set_retransmission_pt (stream, rtx_pt);
1572 g_list_append (priv->payloads, GUINT_TO_POINTER (rtx_pt));
1575 g_mutex_unlock (&priv->lock);
1577 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STREAM], 0, stream,
1584 gst_rtsp_media_remove_stream (GstRTSPMedia * media, GstRTSPStream * stream)
1586 GstRTSPMediaPrivate *priv;
1591 g_mutex_lock (&priv->lock);
1592 /* remove the ghostpad */
1593 srcpad = gst_rtsp_stream_get_srcpad (stream);
1594 gst_element_remove_pad (priv->element, srcpad);
1595 gst_object_unref (srcpad);
1596 /* now remove the stream */
1597 g_object_ref (stream);
1598 g_ptr_array_remove (priv->streams, stream);
1599 g_mutex_unlock (&priv->lock);
1601 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_REMOVED_STREAM], 0,
1604 g_object_unref (stream);
1608 * gst_rtsp_media_n_streams:
1609 * @media: a #GstRTSPMedia
1611 * Get the number of streams in this media.
1613 * Returns: The number of streams.
1616 gst_rtsp_media_n_streams (GstRTSPMedia * media)
1618 GstRTSPMediaPrivate *priv;
1621 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
1625 g_mutex_lock (&priv->lock);
1626 res = priv->streams->len;
1627 g_mutex_unlock (&priv->lock);
1633 * gst_rtsp_media_get_stream:
1634 * @media: a #GstRTSPMedia
1635 * @idx: the stream index
1637 * Retrieve the stream with index @idx from @media.
1639 * Returns: (nullable) (transfer none): the #GstRTSPStream at index
1640 * @idx or %NULL when a stream with that index did not exist.
1643 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
1645 GstRTSPMediaPrivate *priv;
1648 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1652 g_mutex_lock (&priv->lock);
1653 if (idx < priv->streams->len)
1654 res = g_ptr_array_index (priv->streams, idx);
1657 g_mutex_unlock (&priv->lock);
1663 * gst_rtsp_media_find_stream:
1664 * @media: a #GstRTSPMedia
1665 * @control: the control of the stream
1667 * Find a stream in @media with @control as the control uri.
1669 * Returns: (nullable) (transfer none): the #GstRTSPStream with
1670 * control uri @control or %NULL when a stream with that control did
1674 gst_rtsp_media_find_stream (GstRTSPMedia * media, const gchar * control)
1676 GstRTSPMediaPrivate *priv;
1680 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1681 g_return_val_if_fail (control != NULL, NULL);
1687 g_mutex_lock (&priv->lock);
1688 for (i = 0; i < priv->streams->len; i++) {
1689 GstRTSPStream *test;
1691 test = g_ptr_array_index (priv->streams, i);
1692 if (gst_rtsp_stream_has_control (test, control)) {
1697 g_mutex_unlock (&priv->lock);
1702 /* called with state-lock */
1704 default_convert_range (GstRTSPMedia * media, GstRTSPTimeRange * range,
1705 GstRTSPRangeUnit unit)
1707 return gst_rtsp_range_convert_units (range, unit);
1711 * gst_rtsp_media_get_range_string:
1712 * @media: a #GstRTSPMedia
1713 * @play: for the PLAY request
1714 * @unit: the unit to use for the string
1716 * Get the current range as a string. @media must be prepared with
1717 * gst_rtsp_media_prepare ().
1719 * Returns: (transfer full): The range as a string, g_free() after usage.
1722 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play,
1723 GstRTSPRangeUnit unit)
1725 GstRTSPMediaClass *klass;
1726 GstRTSPMediaPrivate *priv;
1728 GstRTSPTimeRange range;
1730 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1731 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
1732 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1736 g_rec_mutex_lock (&priv->state_lock);
1737 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
1738 priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
1741 g_mutex_lock (&priv->lock);
1743 /* Update the range value with current position/duration */
1744 collect_media_stats (media);
1747 range = priv->range;
1749 if (!play && priv->n_active > 0) {
1750 range.min.type = GST_RTSP_TIME_NOW;
1751 range.min.seconds = -1;
1753 g_mutex_unlock (&priv->lock);
1754 g_rec_mutex_unlock (&priv->state_lock);
1756 if (!klass->convert_range (media, &range, unit))
1757 goto conversion_failed;
1759 result = gst_rtsp_range_to_string (&range);
1766 GST_WARNING ("media %p was not prepared", media);
1767 g_rec_mutex_unlock (&priv->state_lock);
1772 GST_WARNING ("range conversion to unit %d failed", unit);
1778 stream_update_blocked (GstRTSPStream * stream, GstRTSPMedia * media)
1780 gst_rtsp_stream_set_blocked (stream, media->priv->blocked);
1784 media_streams_set_blocked (GstRTSPMedia * media, gboolean blocked)
1786 GstRTSPMediaPrivate *priv = media->priv;
1788 GST_DEBUG ("media %p set blocked %d", media, blocked);
1789 priv->blocked = blocked;
1790 g_ptr_array_foreach (priv->streams, (GFunc) stream_update_blocked, media);
1794 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1796 GstRTSPMediaPrivate *priv = media->priv;
1798 g_mutex_lock (&priv->lock);
1799 priv->status = status;
1800 GST_DEBUG ("setting new status to %d", status);
1801 g_cond_broadcast (&priv->cond);
1802 g_mutex_unlock (&priv->lock);
1806 * gst_rtsp_media_get_status:
1807 * @media: a #GstRTSPMedia
1809 * Get the status of @media. When @media is busy preparing, this function waits
1810 * until @media is prepared or in error.
1812 * Returns: the status of @media.
1815 gst_rtsp_media_get_status (GstRTSPMedia * media)
1817 GstRTSPMediaPrivate *priv = media->priv;
1818 GstRTSPMediaStatus result;
1821 g_mutex_lock (&priv->lock);
1822 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
1823 /* while we are preparing, wait */
1824 while (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1825 GST_DEBUG ("waiting for status change");
1826 if (!g_cond_wait_until (&priv->cond, &priv->lock, end_time)) {
1827 GST_DEBUG ("timeout, assuming error status");
1828 priv->status = GST_RTSP_MEDIA_STATUS_ERROR;
1831 /* could be success or error */
1832 result = priv->status;
1833 GST_DEBUG ("got status %d", result);
1834 g_mutex_unlock (&priv->lock);
1840 * gst_rtsp_media_seek:
1841 * @media: a #GstRTSPMedia
1842 * @range: (transfer none): a #GstRTSPTimeRange
1844 * Seek the pipeline of @media to @range. @media must be prepared with
1845 * gst_rtsp_media_prepare().
1847 * Returns: %TRUE on success.
1850 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
1852 GstRTSPMediaClass *klass;
1853 GstRTSPMediaPrivate *priv;
1855 GstClockTime start, stop;
1856 GstSeekType start_type, stop_type;
1858 gint64 current_position;
1860 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1862 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1863 g_return_val_if_fail (range != NULL, FALSE);
1864 g_return_val_if_fail (klass->convert_range != NULL, FALSE);
1868 g_rec_mutex_lock (&priv->state_lock);
1869 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
1872 /* Update the seekable state of the pipeline in case it changed */
1873 if ((priv->transport_mode & GST_RTSP_TRANSPORT_MODE_RECORD)) {
1874 /* TODO: Seeking for RECORD? */
1875 priv->seekable = FALSE;
1877 query = gst_query_new_seeking (GST_FORMAT_TIME);
1878 if (gst_element_query (priv->pipeline, query)) {
1883 gst_query_parse_seeking (query, &format, &seekable, &start, &end);
1884 priv->seekable = seekable;
1886 gst_query_unref (query);
1889 if (!priv->seekable)
1892 start_type = stop_type = GST_SEEK_TYPE_NONE;
1894 if (!klass->convert_range (media, range, GST_RTSP_RANGE_NPT))
1896 gst_rtsp_range_get_times (range, &start, &stop);
1898 GST_INFO ("got %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1899 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1900 GST_INFO ("current %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1901 GST_TIME_ARGS (priv->range_start), GST_TIME_ARGS (priv->range_stop));
1903 current_position = -1;
1904 if (klass->query_position)
1905 klass->query_position (media, ¤t_position);
1906 GST_INFO ("current media position %" GST_TIME_FORMAT,
1907 GST_TIME_ARGS (current_position));
1909 if (start != GST_CLOCK_TIME_NONE)
1910 start_type = GST_SEEK_TYPE_SET;
1912 if (priv->range_stop == stop)
1913 stop = GST_CLOCK_TIME_NONE;
1914 else if (stop != GST_CLOCK_TIME_NONE)
1915 stop_type = GST_SEEK_TYPE_SET;
1917 if (start != GST_CLOCK_TIME_NONE || stop != GST_CLOCK_TIME_NONE) {
1920 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
1921 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
1923 /* depends on the current playing state of the pipeline. We might need to
1924 * queue this until we get EOS. */
1925 flags = GST_SEEK_FLAG_FLUSH;
1927 /* if range start was not supplied we must continue from current position.
1928 * but since we're doing a flushing seek, let us query the current position
1929 * so we end up at exactly the same position after the seek. */
1930 if (range->min.type == GST_RTSP_TIME_END) { /* Yepp, that's right! */
1931 if (current_position == -1) {
1932 GST_WARNING ("current position unknown");
1934 GST_DEBUG ("doing accurate seek to %" GST_TIME_FORMAT,
1935 GST_TIME_ARGS (current_position));
1936 start = current_position;
1937 start_type = GST_SEEK_TYPE_SET;
1938 flags |= GST_SEEK_FLAG_ACCURATE;
1941 /* only set keyframe flag when modifying start */
1942 if (start_type != GST_SEEK_TYPE_NONE)
1943 flags |= GST_SEEK_FLAG_KEY_UNIT;
1946 if (start == current_position && stop_type == GST_SEEK_TYPE_NONE) {
1947 GST_DEBUG ("not seeking because no position change");
1950 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
1952 media_streams_set_blocked (media, TRUE);
1954 /* FIXME, we only do forwards playback, no trick modes yet */
1955 res = gst_element_seek (priv->pipeline, 1.0, GST_FORMAT_TIME,
1956 flags, start_type, start, stop_type, stop);
1958 /* and block for the seek to complete */
1959 GST_INFO ("done seeking %d", res);
1963 g_rec_mutex_unlock (&priv->state_lock);
1965 /* wait until pipeline is prerolled again, this will also collect stats */
1966 if (!wait_preroll (media))
1967 goto preroll_failed;
1969 g_rec_mutex_lock (&priv->state_lock);
1970 GST_INFO ("prerolled again");
1973 GST_INFO ("no seek needed");
1976 g_rec_mutex_unlock (&priv->state_lock);
1983 g_rec_mutex_unlock (&priv->state_lock);
1984 GST_INFO ("media %p is not prepared", media);
1989 g_rec_mutex_unlock (&priv->state_lock);
1990 GST_INFO ("pipeline is not seekable");
1995 g_rec_mutex_unlock (&priv->state_lock);
1996 GST_WARNING ("conversion to npt not supported");
2001 g_rec_mutex_unlock (&priv->state_lock);
2002 GST_INFO ("seeking failed");
2003 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2008 GST_WARNING ("failed to preroll after seek");
2014 stream_collect_blocking (GstRTSPStream * stream, gboolean * blocked)
2016 *blocked &= gst_rtsp_stream_is_blocking (stream);
2020 media_streams_blocking (GstRTSPMedia * media)
2022 gboolean blocking = TRUE;
2024 g_ptr_array_foreach (media->priv->streams, (GFunc) stream_collect_blocking,
2030 static GstStateChangeReturn
2031 set_state (GstRTSPMedia * media, GstState state)
2033 GstRTSPMediaPrivate *priv = media->priv;
2034 GstStateChangeReturn ret;
2036 GST_INFO ("set state to %s for media %p", gst_element_state_get_name (state),
2038 ret = gst_element_set_state (priv->pipeline, state);
2043 static GstStateChangeReturn
2044 set_target_state (GstRTSPMedia * media, GstState state, gboolean do_state)
2046 GstRTSPMediaPrivate *priv = media->priv;
2047 GstStateChangeReturn ret;
2049 GST_INFO ("set target state to %s for media %p",
2050 gst_element_state_get_name (state), media);
2051 priv->target_state = state;
2053 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_TARGET_STATE], 0,
2054 priv->target_state, NULL);
2057 ret = set_state (media, state);
2059 ret = GST_STATE_CHANGE_SUCCESS;
2064 /* called with state-lock */
2066 default_handle_message (GstRTSPMedia * media, GstMessage * message)
2068 GstRTSPMediaPrivate *priv = media->priv;
2069 GstMessageType type;
2071 type = GST_MESSAGE_TYPE (message);
2074 case GST_MESSAGE_STATE_CHANGED:
2076 GstState old, new, pending;
2078 if (GST_MESSAGE_SRC (message) != GST_OBJECT (priv->pipeline))
2081 gst_message_parse_state_changed (message, &old, &new, &pending);
2083 GST_DEBUG ("%p: went from %s to %s (pending %s)", media,
2084 gst_element_state_get_name (old), gst_element_state_get_name (new),
2085 gst_element_state_get_name (pending));
2086 if ((priv->transport_mode & GST_RTSP_TRANSPORT_MODE_RECORD)
2087 && old == GST_STATE_READY && new == GST_STATE_PAUSED) {
2088 GST_INFO ("%p: went to PAUSED, prepared now", media);
2089 collect_media_stats (media);
2091 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2092 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2097 case GST_MESSAGE_BUFFERING:
2101 gst_message_parse_buffering (message, &percent);
2103 /* no state management needed for live pipelines */
2107 if (percent == 100) {
2108 /* a 100% message means buffering is done */
2109 priv->buffering = FALSE;
2110 /* if the desired state is playing, go back */
2111 if (priv->target_state == GST_STATE_PLAYING) {
2112 GST_INFO ("Buffering done, setting pipeline to PLAYING");
2113 set_state (media, GST_STATE_PLAYING);
2115 GST_INFO ("Buffering done");
2118 /* buffering busy */
2119 if (priv->buffering == FALSE) {
2120 if (priv->target_state == GST_STATE_PLAYING) {
2121 /* we were not buffering but PLAYING, PAUSE the pipeline. */
2122 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
2123 set_state (media, GST_STATE_PAUSED);
2125 GST_INFO ("Buffering ...");
2128 priv->buffering = TRUE;
2132 case GST_MESSAGE_LATENCY:
2134 gst_bin_recalculate_latency (GST_BIN_CAST (priv->pipeline));
2137 case GST_MESSAGE_ERROR:
2142 gst_message_parse_error (message, &gerror, &debug);
2143 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
2144 g_error_free (gerror);
2147 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2150 case GST_MESSAGE_WARNING:
2155 gst_message_parse_warning (message, &gerror, &debug);
2156 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
2157 g_error_free (gerror);
2161 case GST_MESSAGE_ELEMENT:
2163 const GstStructure *s;
2165 s = gst_message_get_structure (message);
2166 if (gst_structure_has_name (s, "GstRTSPStreamBlocking")) {
2167 GST_DEBUG ("media received blocking message");
2168 if (priv->blocked && media_streams_blocking (media)) {
2169 GST_DEBUG ("media is blocking");
2170 collect_media_stats (media);
2172 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2173 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2178 case GST_MESSAGE_STREAM_STATUS:
2180 case GST_MESSAGE_ASYNC_DONE:
2182 /* when we are dynamically adding pads, the addition of the udpsrc will
2183 * temporarily produce ASYNC_DONE messages. We have to ignore them and
2184 * wait for the final ASYNC_DONE after everything prerolled */
2185 GST_INFO ("%p: ignoring ASYNC_DONE", media);
2187 GST_INFO ("%p: got ASYNC_DONE", media);
2188 collect_media_stats (media);
2190 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2191 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
2194 case GST_MESSAGE_EOS:
2195 GST_INFO ("%p: got EOS", media);
2197 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
2198 GST_DEBUG ("shutting down after EOS");
2199 finish_unprepare (media);
2203 GST_INFO ("%p: got message type %d (%s)", media, type,
2204 gst_message_type_get_name (type));
2211 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
2213 GstRTSPMediaPrivate *priv = media->priv;
2214 GstRTSPMediaClass *klass;
2217 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2219 g_rec_mutex_lock (&priv->state_lock);
2220 if (klass->handle_message)
2221 ret = klass->handle_message (media, message);
2224 g_rec_mutex_unlock (&priv->state_lock);
2230 watch_destroyed (GstRTSPMedia * media)
2232 GST_DEBUG_OBJECT (media, "source destroyed");
2233 g_object_unref (media);
2237 find_payload_element (GstElement * payloader)
2239 GstElement *pay = NULL;
2241 if (GST_IS_BIN (payloader)) {
2243 GValue item = { 0 };
2245 iter = gst_bin_iterate_recurse (GST_BIN (payloader));
2246 while (gst_iterator_next (iter, &item) == GST_ITERATOR_OK) {
2247 GstElement *element = (GstElement *) g_value_get_object (&item);
2248 GstElementClass *eclass = GST_ELEMENT_GET_CLASS (element);
2252 gst_element_class_get_metadata (eclass, GST_ELEMENT_METADATA_KLASS);
2256 if (strstr (klass, "Payloader") && strstr (klass, "RTP")) {
2257 pay = gst_object_ref (element);
2258 g_value_unset (&item);
2261 g_value_unset (&item);
2263 gst_iterator_free (iter);
2265 pay = g_object_ref (payloader);
2271 /* called from streaming threads */
2273 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
2275 GstRTSPMediaPrivate *priv = media->priv;
2276 GstRTSPStream *stream;
2279 /* find the real payload element */
2280 pay = find_payload_element (element);
2281 stream = gst_rtsp_media_create_stream (media, pay, pad);
2282 gst_object_unref (pay);
2284 GST_INFO ("pad added %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
2286 g_rec_mutex_lock (&priv->state_lock);
2287 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARING)
2290 g_object_set_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream", stream);
2292 /* we will be adding elements below that will cause ASYNC_DONE to be
2293 * posted in the bus. We want to ignore those messages until the
2294 * pipeline really prerolled. */
2295 priv->adding = TRUE;
2297 /* join the element in the PAUSED state because this callback is
2298 * called from the streaming thread and it is PAUSED */
2299 if (!gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
2300 priv->rtpbin, GST_STATE_PAUSED)) {
2301 GST_WARNING ("failed to join bin element");
2304 priv->adding = FALSE;
2305 g_rec_mutex_unlock (&priv->state_lock);
2312 gst_rtsp_media_remove_stream (media, stream);
2313 g_rec_mutex_unlock (&priv->state_lock);
2314 GST_INFO ("ignore pad because we are not preparing");
2320 pad_removed_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
2322 GstRTSPMediaPrivate *priv = media->priv;
2323 GstRTSPStream *stream;
2325 stream = g_object_get_data (G_OBJECT (pad), "gst-rtsp-dynpad-stream");
2329 GST_INFO ("pad removed %s:%s, stream %p", GST_DEBUG_PAD_NAME (pad), stream);
2331 g_rec_mutex_lock (&priv->state_lock);
2332 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
2333 g_rec_mutex_unlock (&priv->state_lock);
2335 gst_rtsp_media_remove_stream (media, stream);
2339 remove_fakesink (GstRTSPMediaPrivate * priv)
2341 GstElement *fakesink;
2343 g_mutex_lock (&priv->lock);
2344 if ((fakesink = priv->fakesink))
2345 gst_object_ref (fakesink);
2346 priv->fakesink = NULL;
2347 g_mutex_unlock (&priv->lock);
2350 gst_bin_remove (GST_BIN (priv->pipeline), fakesink);
2351 gst_element_set_state (fakesink, GST_STATE_NULL);
2352 gst_object_unref (fakesink);
2353 GST_INFO ("removed fakesink");
2358 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
2360 GstRTSPMediaPrivate *priv = media->priv;
2362 GST_INFO ("no more pads");
2363 remove_fakesink (priv);
2366 typedef struct _DynPaySignalHandlers DynPaySignalHandlers;
2368 struct _DynPaySignalHandlers
2370 gulong pad_added_handler;
2371 gulong pad_removed_handler;
2372 gulong no_more_pads_handler;
2376 start_preroll (GstRTSPMedia * media)
2378 GstRTSPMediaPrivate *priv = media->priv;
2379 GstStateChangeReturn ret;
2381 GST_INFO ("setting pipeline to PAUSED for media %p", media);
2382 /* first go to PAUSED */
2383 ret = set_target_state (media, GST_STATE_PAUSED, TRUE);
2386 case GST_STATE_CHANGE_SUCCESS:
2387 GST_INFO ("SUCCESS state change for media %p", media);
2388 priv->seekable = TRUE;
2390 case GST_STATE_CHANGE_ASYNC:
2391 GST_INFO ("ASYNC state change for media %p", media);
2392 priv->seekable = TRUE;
2394 case GST_STATE_CHANGE_NO_PREROLL:
2395 /* we need to go to PLAYING */
2396 GST_INFO ("NO_PREROLL state change: live media %p", media);
2397 /* FIXME we disable seeking for live streams for now. We should perform a
2398 * seeking query in preroll instead */
2399 priv->seekable = FALSE;
2400 priv->is_live = TRUE;
2401 if (!(priv->transport_mode & GST_RTSP_TRANSPORT_MODE_RECORD)) {
2402 /* start blocked to make sure nothing goes to the sink */
2403 media_streams_set_blocked (media, TRUE);
2405 ret = set_state (media, GST_STATE_PLAYING);
2406 if (ret == GST_STATE_CHANGE_FAILURE)
2409 case GST_STATE_CHANGE_FAILURE:
2417 GST_WARNING ("failed to preroll pipeline");
2423 wait_preroll (GstRTSPMedia * media)
2425 GstRTSPMediaStatus status;
2427 GST_DEBUG ("wait to preroll pipeline");
2429 /* wait until pipeline is prerolled */
2430 status = gst_rtsp_media_get_status (media);
2431 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
2432 goto preroll_failed;
2438 GST_WARNING ("failed to preroll pipeline");
2444 request_aux_sender (GstElement * rtpbin, guint sessid, GstRTSPMedia * media)
2446 GstRTSPMediaPrivate *priv = media->priv;
2447 GstRTSPStream *stream = NULL;
2450 g_mutex_lock (&priv->lock);
2451 for (i = 0; i < priv->streams->len; i++) {
2452 stream = g_ptr_array_index (priv->streams, i);
2454 if (sessid == gst_rtsp_stream_get_index (stream))
2457 g_mutex_unlock (&priv->lock);
2459 return gst_rtsp_stream_request_aux_sender (stream, sessid);
2463 start_prepare (GstRTSPMedia * media)
2465 GstRTSPMediaPrivate *priv = media->priv;
2469 /* link streams we already have, other streams might appear when we have
2470 * dynamic elements */
2471 for (i = 0; i < priv->streams->len; i++) {
2472 GstRTSPStream *stream;
2474 stream = g_ptr_array_index (priv->streams, i);
2476 if (priv->rtx_time > 0) {
2477 /* enable retransmission by setting rtprtxsend as the "aux" element of rtpbin */
2478 g_signal_connect (priv->rtpbin, "request-aux-sender",
2479 (GCallback) request_aux_sender, media);
2482 if (!gst_rtsp_stream_join_bin (stream, GST_BIN (priv->pipeline),
2483 priv->rtpbin, GST_STATE_NULL)) {
2484 goto join_bin_failed;
2489 g_object_set (priv->rtpbin, "do-retransmission", priv->rtx_time > 0, NULL);
2491 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2492 GstElement *elem = walk->data;
2493 DynPaySignalHandlers *handlers = g_slice_new (DynPaySignalHandlers);
2495 GST_INFO ("adding callbacks for dynamic element %p", elem);
2497 handlers->pad_added_handler = g_signal_connect (elem, "pad-added",
2498 (GCallback) pad_added_cb, media);
2499 handlers->pad_removed_handler = g_signal_connect (elem, "pad-removed",
2500 (GCallback) pad_removed_cb, media);
2501 handlers->no_more_pads_handler = g_signal_connect (elem, "no-more-pads",
2502 (GCallback) no_more_pads_cb, media);
2504 g_object_set_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers", handlers);
2506 /* we add a fakesink here in order to make the state change async. We remove
2507 * the fakesink again in the no-more-pads callback. */
2508 priv->fakesink = gst_element_factory_make ("fakesink", "fakesink");
2509 gst_bin_add (GST_BIN (priv->pipeline), priv->fakesink);
2512 if (!start_preroll (media))
2513 goto preroll_failed;
2519 GST_WARNING ("failed to join bin element");
2520 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2525 GST_WARNING ("failed to preroll pipeline");
2526 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
2532 default_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
2534 GstRTSPMediaPrivate *priv;
2535 GstRTSPMediaClass *klass;
2537 GMainContext *context;
2542 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2544 if (!klass->create_rtpbin)
2545 goto no_create_rtpbin;
2547 priv->rtpbin = klass->create_rtpbin (media);
2548 if (priv->rtpbin != NULL) {
2549 gboolean success = TRUE;
2551 g_object_set (priv->rtpbin, "latency", priv->latency, NULL);
2553 if (klass->setup_rtpbin)
2554 success = klass->setup_rtpbin (media, priv->rtpbin);
2556 if (success == FALSE) {
2557 gst_object_unref (priv->rtpbin);
2558 priv->rtpbin = NULL;
2561 if (priv->rtpbin == NULL)
2564 priv->thread = thread;
2565 context = (thread != NULL) ? (thread->context) : NULL;
2567 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (priv->pipeline));
2569 /* add the pipeline bus to our custom mainloop */
2570 priv->source = gst_bus_create_watch (bus);
2571 gst_object_unref (bus);
2573 g_source_set_callback (priv->source, (GSourceFunc) bus_message,
2574 g_object_ref (media), (GDestroyNotify) watch_destroyed);
2576 priv->id = g_source_attach (priv->source, context);
2578 /* add stuff to the bin */
2579 gst_bin_add (GST_BIN (priv->pipeline), priv->rtpbin);
2581 /* do remainder in context */
2582 source = g_idle_source_new ();
2583 g_source_set_callback (source, (GSourceFunc) start_prepare, media, NULL);
2584 g_source_attach (source, context);
2585 g_source_unref (source);
2592 GST_ERROR ("no create_rtpbin function");
2593 g_critical ("no create_rtpbin vmethod function set");
2598 GST_WARNING ("no rtpbin element");
2599 g_warning ("failed to create element 'rtpbin', check your installation");
2605 * gst_rtsp_media_prepare:
2606 * @media: a #GstRTSPMedia
2607 * @thread: (transfer full) (allow-none): a #GstRTSPThread to run the
2608 * bus handler or %NULL
2610 * Prepare @media for streaming. This function will create the objects
2611 * to manage the streaming. A pipeline must have been set on @media with
2612 * gst_rtsp_media_take_pipeline().
2614 * It will preroll the pipeline and collect vital information about the streams
2615 * such as the duration.
2617 * Returns: %TRUE on success.
2620 gst_rtsp_media_prepare (GstRTSPMedia * media, GstRTSPThread * thread)
2622 GstRTSPMediaPrivate *priv;
2623 GstRTSPMediaClass *klass;
2625 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2629 g_rec_mutex_lock (&priv->state_lock);
2630 priv->prepare_count++;
2632 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED ||
2633 priv->status == GST_RTSP_MEDIA_STATUS_SUSPENDED)
2636 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARING)
2639 if (priv->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
2640 goto not_unprepared;
2642 if (!priv->reusable && priv->reused)
2645 GST_INFO ("preparing media %p", media);
2647 /* reset some variables */
2648 priv->is_live = FALSE;
2649 priv->seekable = FALSE;
2650 priv->buffering = FALSE;
2652 /* we're preparing now */
2653 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
2655 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2656 if (klass->prepare) {
2657 if (!klass->prepare (media, thread))
2658 goto prepare_failed;
2662 g_rec_mutex_unlock (&priv->state_lock);
2664 /* now wait for all pads to be prerolled, FIXME, we should somehow be
2665 * able to do this async so that we don't block the server thread. */
2666 if (!wait_preroll (media))
2667 goto preroll_failed;
2669 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
2671 GST_INFO ("object %p is prerolled", media);
2678 /* we are not going to use the giving thread, so stop it. */
2680 gst_rtsp_thread_stop (thread);
2685 GST_LOG ("media %p was prepared", media);
2686 /* we are not going to use the giving thread, so stop it. */
2688 gst_rtsp_thread_stop (thread);
2689 g_rec_mutex_unlock (&priv->state_lock);
2695 /* we are not going to use the giving thread, so stop it. */
2697 gst_rtsp_thread_stop (thread);
2698 GST_WARNING ("media %p was not unprepared", media);
2699 priv->prepare_count--;
2700 g_rec_mutex_unlock (&priv->state_lock);
2705 /* we are not going to use the giving thread, so stop it. */
2707 gst_rtsp_thread_stop (thread);
2708 priv->prepare_count--;
2709 g_rec_mutex_unlock (&priv->state_lock);
2710 GST_WARNING ("can not reuse media %p", media);
2715 /* we are not going to use the giving thread, so stop it. */
2717 gst_rtsp_thread_stop (thread);
2718 priv->prepare_count--;
2719 g_rec_mutex_unlock (&priv->state_lock);
2720 GST_ERROR ("failed to prepare media");
2725 GST_WARNING ("failed to preroll pipeline");
2726 gst_rtsp_media_unprepare (media);
2731 /* must be called with state-lock */
2733 finish_unprepare (GstRTSPMedia * media)
2735 GstRTSPMediaPrivate *priv = media->priv;
2739 GST_DEBUG ("shutting down");
2741 /* release the lock on shutdown, otherwise pad_added_cb might try to
2742 * acquire the lock and then we deadlock */
2743 g_rec_mutex_unlock (&priv->state_lock);
2744 set_state (media, GST_STATE_NULL);
2745 g_rec_mutex_lock (&priv->state_lock);
2746 remove_fakesink (priv);
2748 for (i = 0; i < priv->streams->len; i++) {
2749 GstRTSPStream *stream;
2751 GST_INFO ("Removing elements of stream %d from pipeline", i);
2753 stream = g_ptr_array_index (priv->streams, i);
2755 gst_rtsp_stream_leave_bin (stream, GST_BIN (priv->pipeline), priv->rtpbin);
2758 /* remove the pad signal handlers */
2759 for (walk = priv->dynamic; walk; walk = g_list_next (walk)) {
2760 GstElement *elem = walk->data;
2761 DynPaySignalHandlers *handlers;
2764 g_object_steal_data (G_OBJECT (elem), "gst-rtsp-dynpay-handlers");
2765 g_assert (handlers != NULL);
2767 g_signal_handler_disconnect (G_OBJECT (elem), handlers->pad_added_handler);
2768 g_signal_handler_disconnect (G_OBJECT (elem),
2769 handlers->pad_removed_handler);
2770 g_signal_handler_disconnect (G_OBJECT (elem),
2771 handlers->no_more_pads_handler);
2773 g_slice_free (DynPaySignalHandlers, handlers);
2776 gst_bin_remove (GST_BIN (priv->pipeline), priv->rtpbin);
2777 priv->rtpbin = NULL;
2780 gst_object_unref (priv->nettime);
2781 priv->nettime = NULL;
2783 priv->reused = TRUE;
2784 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_UNPREPARED);
2786 /* when the media is not reusable, this will effectively unref the media and
2788 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
2790 /* the source has the last ref to the media */
2792 GST_DEBUG ("destroy source");
2793 g_source_destroy (priv->source);
2794 g_source_unref (priv->source);
2797 GST_DEBUG ("stop thread");
2798 gst_rtsp_thread_stop (priv->thread);
2802 /* called with state-lock */
2804 default_unprepare (GstRTSPMedia * media)
2806 GstRTSPMediaPrivate *priv = media->priv;
2808 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_UNPREPARING);
2810 if (priv->eos_shutdown) {
2811 GST_DEBUG ("sending EOS for shutdown");
2812 /* ref so that we don't disappear */
2813 gst_element_send_event (priv->pipeline, gst_event_new_eos ());
2814 /* we need to go to playing again for the EOS to propagate, normally in this
2815 * state, nothing is receiving data from us anymore so this is ok. */
2816 set_state (media, GST_STATE_PLAYING);
2818 finish_unprepare (media);
2824 * gst_rtsp_media_unprepare:
2825 * @media: a #GstRTSPMedia
2827 * Unprepare @media. After this call, the media should be prepared again before
2828 * it can be used again. If the media is set to be non-reusable, a new instance
2831 * Returns: %TRUE on success.
2834 gst_rtsp_media_unprepare (GstRTSPMedia * media)
2836 GstRTSPMediaPrivate *priv;
2839 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
2843 g_rec_mutex_lock (&priv->state_lock);
2844 if (priv->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
2845 goto was_unprepared;
2847 priv->prepare_count--;
2848 if (priv->prepare_count > 0)
2851 GST_INFO ("unprepare media %p", media);
2853 media_streams_set_blocked (media, FALSE);
2854 set_target_state (media, GST_STATE_NULL, FALSE);
2857 if (priv->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
2858 GstRTSPMediaClass *klass;
2860 klass = GST_RTSP_MEDIA_GET_CLASS (media);
2861 if (klass->unprepare)
2862 success = klass->unprepare (media);
2864 finish_unprepare (media);
2866 g_rec_mutex_unlock (&priv->state_lock);
2872 g_rec_mutex_unlock (&priv->state_lock);
2873 GST_INFO ("media %p was already unprepared", media);
2878 GST_INFO ("media %p still prepared %d times", media, priv->prepare_count);
2879 g_rec_mutex_unlock (&priv->state_lock);
2884 /* should be called with state-lock */
2886 get_clock_unlocked (GstRTSPMedia * media)
2888 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED) {
2889 GST_DEBUG_OBJECT (media, "media was not prepared");
2892 return gst_pipeline_get_clock (GST_PIPELINE_CAST (media->priv->pipeline));
2896 * gst_rtsp_media_get_clock:
2897 * @media: a #GstRTSPMedia
2899 * Get the clock that is used by the pipeline in @media.
2901 * @media must be prepared before this method returns a valid clock object.
2903 * Returns: (transfer full): the #GstClock used by @media. unref after usage.
2906 gst_rtsp_media_get_clock (GstRTSPMedia * media)
2909 GstRTSPMediaPrivate *priv;
2911 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2915 g_rec_mutex_lock (&priv->state_lock);
2916 clock = get_clock_unlocked (media);
2917 g_rec_mutex_unlock (&priv->state_lock);
2923 * gst_rtsp_media_get_base_time:
2924 * @media: a #GstRTSPMedia
2926 * Get the base_time that is used by the pipeline in @media.
2928 * @media must be prepared before this method returns a valid base_time.
2930 * Returns: the base_time used by @media.
2933 gst_rtsp_media_get_base_time (GstRTSPMedia * media)
2935 GstClockTime result;
2936 GstRTSPMediaPrivate *priv;
2938 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), GST_CLOCK_TIME_NONE);
2942 g_rec_mutex_lock (&priv->state_lock);
2943 if (media->priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
2946 result = gst_element_get_base_time (media->priv->pipeline);
2947 g_rec_mutex_unlock (&priv->state_lock);
2954 g_rec_mutex_unlock (&priv->state_lock);
2955 GST_DEBUG_OBJECT (media, "media was not prepared");
2956 return GST_CLOCK_TIME_NONE;
2961 * gst_rtsp_media_get_time_provider:
2962 * @media: a #GstRTSPMedia
2963 * @address: (allow-none): an address or %NULL
2964 * @port: a port or 0
2966 * Get the #GstNetTimeProvider for the clock used by @media. The time provider
2967 * will listen on @address and @port for client time requests.
2969 * Returns: (transfer full): the #GstNetTimeProvider of @media.
2971 GstNetTimeProvider *
2972 gst_rtsp_media_get_time_provider (GstRTSPMedia * media, const gchar * address,
2975 GstRTSPMediaPrivate *priv;
2976 GstNetTimeProvider *provider = NULL;
2978 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
2982 g_rec_mutex_lock (&priv->state_lock);
2983 if (priv->time_provider) {
2984 if ((provider = priv->nettime) == NULL) {
2987 if (priv->time_provider && (clock = get_clock_unlocked (media))) {
2988 provider = gst_net_time_provider_new (clock, address, port);
2989 gst_object_unref (clock);
2991 priv->nettime = provider;
2995 g_rec_mutex_unlock (&priv->state_lock);
2998 gst_object_ref (provider);
3004 default_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp, GstSDPInfo * info)
3006 return gst_rtsp_sdp_from_media (sdp, info, media);
3010 * gst_rtsp_media_setup_sdp:
3011 * @media: a #GstRTSPMedia
3012 * @sdp: (transfer none): a #GstSDPMessage
3013 * @info: (transfer none): a #GstSDPInfo
3015 * Add @media specific info to @sdp. @info is used to configure the connection
3016 * information in the SDP.
3018 * Returns: TRUE on success.
3021 gst_rtsp_media_setup_sdp (GstRTSPMedia * media, GstSDPMessage * sdp,
3024 GstRTSPMediaPrivate *priv;
3025 GstRTSPMediaClass *klass;
3028 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3029 g_return_val_if_fail (sdp != NULL, FALSE);
3030 g_return_val_if_fail (info != NULL, FALSE);
3034 g_rec_mutex_lock (&priv->state_lock);
3036 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3038 if (!klass->setup_sdp)
3041 res = klass->setup_sdp (media, sdp, info);
3043 g_rec_mutex_unlock (&priv->state_lock);
3050 g_rec_mutex_unlock (&priv->state_lock);
3051 GST_ERROR ("no setup_sdp function");
3052 g_critical ("no setup_sdp vmethod function set");
3057 static const gchar *
3058 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
3067 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
3070 if (sscanf (attr, "%d ", &val) != 1)
3079 #define PARSE_INT(p, del, res) \
3082 p = strstr (p, del); \
3092 #define PARSE_STRING(p, del, res) \
3095 p = strstr (p, del); \
3107 #define SKIP_SPACES(p) \
3108 while (*p && g_ascii_isspace (*p)) \
3113 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
3116 parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
3117 gint * rate, gchar ** params)
3121 p = (gchar *) rtpmap;
3123 PARSE_INT (p, " ", *payload);
3131 PARSE_STRING (p, "/", *name);
3132 if (*name == NULL) {
3133 GST_DEBUG ("no rate, name %s", p);
3134 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
3135 * streams seem to omit the rate. */
3142 p = strstr (p, "/");
3160 * Mapping of caps to and from SDP fields:
3162 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
3163 * a=framesize:<payload> <width>-<height>
3164 * a=fmtp:<payload> <param>[=<value>];...
3167 media_to_caps (gint pt, const GstSDPMedia * media)
3170 const gchar *rtpmap;
3172 const gchar *framesize;
3175 gchar *params = NULL;
3181 /* get and parse rtpmap */
3182 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
3185 ret = parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
3187 g_warning ("error parsing rtpmap, ignoring");
3191 /* dynamic payloads need rtpmap or we fail */
3192 if (rtpmap == NULL && pt >= 96)
3195 /* check if we have a rate, if not, we need to look up the rate from the
3196 * default rates based on the payload types. */
3198 const GstRTPPayloadInfo *info;
3200 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
3201 /* dynamic types, use media and encoding_name */
3202 tmp = g_ascii_strdown (media->media, -1);
3203 info = gst_rtp_payload_info_for_name (tmp, name);
3206 /* static types, use payload type */
3207 info = gst_rtp_payload_info_for_pt (pt);
3211 if ((rate = info->clock_rate) == 0)
3214 /* we fail if we cannot find one */
3219 tmp = g_ascii_strdown (media->media, -1);
3220 caps = gst_caps_new_simple ("application/x-unknown",
3221 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
3223 s = gst_caps_get_structure (caps, 0);
3225 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
3227 /* encoding name must be upper case */
3229 tmp = g_ascii_strup (name, -1);
3230 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
3234 /* params must be lower case */
3235 if (params != NULL) {
3236 tmp = g_ascii_strdown (params, -1);
3237 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
3241 /* parse optional fmtp: field */
3242 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
3248 /* p is now of the format <payload> <param>[=<value>];... */
3249 PARSE_INT (p, " ", payload);
3250 if (payload != -1 && payload == pt) {
3254 /* <param>[=<value>] are separated with ';' */
3255 pairs = g_strsplit (p, ";", 0);
3256 for (i = 0; pairs[i]; i++) {
3258 const gchar *val, *key;
3260 /* the key may not have a '=', the value can have other '='s */
3261 valpos = strstr (pairs[i], "=");
3263 /* we have a '=' and thus a value, remove the '=' with \0 */
3265 /* value is everything between '=' and ';'. We split the pairs at ;
3266 * boundaries so we can take the remainder of the value. Some servers
3267 * put spaces around the value which we strip off here. Alternatively
3268 * we could strip those spaces in the depayloaders should these spaces
3269 * actually carry any meaning in the future. */
3270 val = g_strstrip (valpos + 1);
3272 /* simple <param>;.. is translated into <param>=1;... */
3275 /* strip the key of spaces, convert key to lowercase but not the value. */
3276 key = g_strstrip (pairs[i]);
3277 if (strlen (key) > 1) {
3278 tmp = g_ascii_strdown (key, -1);
3279 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
3287 /* parse framesize: field */
3288 if ((framesize = gst_sdp_media_get_attribute_val (media, "framesize"))) {
3291 /* p is now of the format <payload> <width>-<height> */
3292 p = (gchar *) framesize;
3294 PARSE_INT (p, " ", payload);
3295 if (payload != -1 && payload == pt) {
3296 gst_structure_set (s, "a-framesize", G_TYPE_STRING, p, NULL);
3304 g_warning ("rtpmap type not given for dynamic payload %d", pt);
3309 g_warning ("rate unknown for payload type %d", pt);
3315 parse_keymgmt (const gchar * keymgmt, GstCaps * caps)
3317 gboolean res = FALSE;
3321 GstMIKEYMessage *msg;
3322 const GstMIKEYPayload *payload;
3323 const gchar *srtp_cipher;
3324 const gchar *srtp_auth;
3326 p = (gchar *) keymgmt;
3332 PARSE_STRING (p, " ", kmpid);
3333 if (!g_str_equal (kmpid, "mikey"))
3336 data = g_base64_decode (p, &size);
3340 msg = gst_mikey_message_new_from_data (data, size, NULL, NULL);
3345 srtp_cipher = "aes-128-icm";
3346 srtp_auth = "hmac-sha1-80";
3348 /* check the Security policy if any */
3349 if ((payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, 0))) {
3350 GstMIKEYPayloadSP *p = (GstMIKEYPayloadSP *) payload;
3353 if (p->proto != GST_MIKEY_SEC_PROTO_SRTP)
3356 len = gst_mikey_payload_sp_get_n_params (payload);
3357 for (i = 0; i < len; i++) {
3358 const GstMIKEYPayloadSPParam *param =
3359 gst_mikey_payload_sp_get_param (payload, i);
3361 switch (param->type) {
3362 case GST_MIKEY_SP_SRTP_ENC_ALG:
3363 switch (param->val[0]) {
3365 srtp_cipher = "null";
3369 srtp_cipher = "aes-128-icm";
3375 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
3376 switch (param->val[0]) {
3377 case AES_128_KEY_LEN:
3378 srtp_cipher = "aes-128-icm";
3380 case AES_256_KEY_LEN:
3381 srtp_cipher = "aes-256-icm";
3387 case GST_MIKEY_SP_SRTP_AUTH_ALG:
3388 switch (param->val[0]) {
3394 srtp_auth = "hmac-sha1-80";
3400 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
3401 switch (param->val[0]) {
3402 case HMAC_32_KEY_LEN:
3403 srtp_auth = "hmac-sha1-32";
3405 case HMAC_80_KEY_LEN:
3406 srtp_auth = "hmac-sha1-80";
3412 case GST_MIKEY_SP_SRTP_SRTP_ENC:
3414 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
3422 if (!(payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_KEMAC, 0)))
3425 GstMIKEYPayloadKEMAC *p = (GstMIKEYPayloadKEMAC *) payload;
3426 const GstMIKEYPayload *sub;
3427 GstMIKEYPayloadKeyData *pkd;
3430 if (p->enc_alg != GST_MIKEY_ENC_NULL || p->mac_alg != GST_MIKEY_MAC_NULL)
3433 if (!(sub = gst_mikey_payload_kemac_get_sub (payload, 0)))
3436 if (sub->type != GST_MIKEY_PT_KEY_DATA)
3439 pkd = (GstMIKEYPayloadKeyData *) sub;
3441 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
3443 gst_caps_set_simple (caps, "srtp-key", GST_TYPE_BUFFER, buf, NULL);
3446 gst_caps_set_simple (caps,
3447 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
3448 "srtp-auth", G_TYPE_STRING, srtp_auth,
3449 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
3450 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
3454 gst_mikey_message_unref (msg);
3460 * Mapping SDP attributes to caps
3462 * prepend 'a-' to IANA registered sdp attributes names
3463 * (ie: not prefixed with 'x-') in order to avoid
3464 * collision with gstreamer standard caps properties names
3467 sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
3469 if (attributes->len > 0) {
3473 s = gst_caps_get_structure (caps, 0);
3475 for (i = 0; i < attributes->len; i++) {
3476 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
3477 gchar *tofree, *key;
3481 /* skip some of the attribute we already handle */
3482 if (!strcmp (key, "fmtp"))
3484 if (!strcmp (key, "rtpmap"))
3486 if (!strcmp (key, "control"))
3488 if (!strcmp (key, "range"))
3490 if (!strcmp (key, "framesize"))
3492 if (g_str_equal (key, "key-mgmt")) {
3493 parse_keymgmt (attr->value, caps);
3497 /* string must be valid UTF8 */
3498 if (!g_utf8_validate (attr->value, -1, NULL))
3501 if (!g_str_has_prefix (key, "x-"))
3502 tofree = key = g_strdup_printf ("a-%s", key);
3506 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
3507 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
3514 default_handle_sdp (GstRTSPMedia * media, GstSDPMessage * sdp)
3516 GstRTSPMediaPrivate *priv = media->priv;
3519 medias_len = gst_sdp_message_medias_len (sdp);
3520 if (medias_len != priv->streams->len) {
3521 GST_ERROR ("%p: Media has more or less streams than SDP (%d /= %d)", media,
3522 priv->streams->len, medias_len);
3526 for (i = 0; i < medias_len; i++) {
3527 const gchar *proto, *media_type;
3528 const GstSDPMedia *sdp_media = gst_sdp_message_get_media (sdp, i);
3529 GstRTSPStream *stream;
3530 gint j, formats_len;
3531 const gchar *control;
3532 GstRTSPProfile profile, profiles;
3534 stream = g_ptr_array_index (priv->streams, i);
3536 /* TODO: Should we do something with the other SDP information? */
3539 proto = gst_sdp_media_get_proto (sdp_media);
3540 if (proto == NULL) {
3541 GST_ERROR ("%p: SDP media %d has no proto", media, i);
3545 if (g_str_equal (proto, "RTP/AVP")) {
3546 media_type = "application/x-rtp";
3547 profile = GST_RTSP_PROFILE_AVP;
3548 } else if (g_str_equal (proto, "RTP/SAVP")) {
3549 media_type = "application/x-srtp";
3550 profile = GST_RTSP_PROFILE_SAVP;
3551 } else if (g_str_equal (proto, "RTP/AVPF")) {
3552 media_type = "application/x-rtp";
3553 profile = GST_RTSP_PROFILE_AVPF;
3554 } else if (g_str_equal (proto, "RTP/SAVPF")) {
3555 media_type = "application/x-srtp";
3556 profile = GST_RTSP_PROFILE_SAVPF;
3558 GST_ERROR ("%p: unsupported profile '%s' for stream %d", media, proto, i);
3562 profiles = gst_rtsp_stream_get_profiles (stream);
3563 if ((profiles & profile) == 0) {
3564 GST_ERROR ("%p: unsupported profile '%s' for stream %d", media, proto, i);
3568 formats_len = gst_sdp_media_formats_len (sdp_media);
3569 for (j = 0; j < formats_len; j++) {
3574 pt = atoi (gst_sdp_media_get_format (sdp_media, j));
3576 GST_DEBUG (" looking at %d pt: %d", j, pt);
3579 caps = media_to_caps (pt, sdp_media);
3581 GST_WARNING (" skipping pt %d without caps", pt);
3585 /* do some tweaks */
3586 GST_DEBUG ("mapping sdp session level attributes to caps");
3587 sdp_attributes_to_caps (sdp->attributes, caps);
3588 GST_DEBUG ("mapping sdp media level attributes to caps");
3589 sdp_attributes_to_caps (sdp_media->attributes, caps);
3591 s = gst_caps_get_structure (caps, 0);
3592 gst_structure_set_name (s, media_type);
3594 gst_rtsp_stream_set_pt_map (stream, pt, caps);
3595 gst_caps_unref (caps);
3598 control = gst_sdp_media_get_attribute_val (sdp_media, "control");
3600 gst_rtsp_stream_set_control (stream, control);
3608 * gst_rtsp_media_handle_sdp:
3609 * @media: a #GstRTSPMedia
3610 * @sdp: (transfer none): a #GstSDPMessage
3612 * Configure an SDP on @media for receiving streams
3614 * Returns: TRUE on success.
3617 gst_rtsp_media_handle_sdp (GstRTSPMedia * media, GstSDPMessage * sdp)
3619 GstRTSPMediaPrivate *priv;
3620 GstRTSPMediaClass *klass;
3623 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3624 g_return_val_if_fail (sdp != NULL, FALSE);
3628 g_rec_mutex_lock (&priv->state_lock);
3630 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3632 if (!klass->handle_sdp)
3635 res = klass->handle_sdp (media, sdp);
3637 g_rec_mutex_unlock (&priv->state_lock);
3644 g_rec_mutex_unlock (&priv->state_lock);
3645 GST_ERROR ("no handle_sdp function");
3646 g_critical ("no handle_sdp vmethod function set");
3652 do_set_seqnum (GstRTSPStream * stream)
3655 seq_num = gst_rtsp_stream_get_current_seqnum (stream);
3656 gst_rtsp_stream_set_seqnum_offset (stream, seq_num + 1);
3659 /* call with state_lock */
3661 default_suspend (GstRTSPMedia * media)
3663 GstRTSPMediaPrivate *priv = media->priv;
3664 GstStateChangeReturn ret;
3665 gboolean unblock = FALSE;
3667 switch (priv->suspend_mode) {
3668 case GST_RTSP_SUSPEND_MODE_NONE:
3669 GST_DEBUG ("media %p no suspend", media);
3671 case GST_RTSP_SUSPEND_MODE_PAUSE:
3672 GST_DEBUG ("media %p suspend to PAUSED", media);
3673 ret = set_target_state (media, GST_STATE_PAUSED, TRUE);
3674 if (ret == GST_STATE_CHANGE_FAILURE)
3678 case GST_RTSP_SUSPEND_MODE_RESET:
3679 GST_DEBUG ("media %p suspend to NULL", media);
3680 ret = set_target_state (media, GST_STATE_NULL, TRUE);
3681 if (ret == GST_STATE_CHANGE_FAILURE)
3683 /* Because payloader needs to set the sequence number as
3684 * monotonic, we need to preserve the sequence number
3685 * after pause. (otherwise going from pause to play, which
3686 * is actually from NULL to PLAY will create a new sequence
3688 g_ptr_array_foreach (priv->streams, (GFunc) do_set_seqnum, NULL);
3695 /* let the streams do the state changes freely, if any */
3697 media_streams_set_blocked (media, FALSE);
3704 GST_WARNING ("failed changing pipeline's state for media %p", media);
3710 * gst_rtsp_media_suspend:
3711 * @media: a #GstRTSPMedia
3713 * Suspend @media. The state of the pipeline managed by @media is set to
3714 * GST_STATE_NULL but all streams are kept. @media can be prepared again
3715 * with gst_rtsp_media_unsuspend()
3717 * @media must be prepared with gst_rtsp_media_prepare();
3719 * Returns: %TRUE on success.
3722 gst_rtsp_media_suspend (GstRTSPMedia * media)
3724 GstRTSPMediaPrivate *priv = media->priv;
3725 GstRTSPMediaClass *klass;
3727 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3729 GST_FIXME ("suspend for dynamic pipelines needs fixing");
3731 g_rec_mutex_lock (&priv->state_lock);
3732 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED)
3735 /* don't attempt to suspend when something is busy */
3736 if (priv->n_active > 0)
3739 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3740 if (klass->suspend) {
3741 if (!klass->suspend (media))
3742 goto suspend_failed;
3745 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_SUSPENDED);
3747 g_rec_mutex_unlock (&priv->state_lock);
3754 g_rec_mutex_unlock (&priv->state_lock);
3755 GST_WARNING ("media %p was not prepared", media);
3760 g_rec_mutex_unlock (&priv->state_lock);
3761 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
3762 GST_WARNING ("failed to suspend media %p", media);
3767 /* call with state_lock */
3769 default_unsuspend (GstRTSPMedia * media)
3771 GstRTSPMediaPrivate *priv = media->priv;
3773 switch (priv->suspend_mode) {
3774 case GST_RTSP_SUSPEND_MODE_NONE:
3775 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
3777 case GST_RTSP_SUSPEND_MODE_PAUSE:
3778 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
3780 case GST_RTSP_SUSPEND_MODE_RESET:
3782 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARING);
3783 if (!start_preroll (media))
3785 g_rec_mutex_unlock (&priv->state_lock);
3787 if (!wait_preroll (media))
3788 goto preroll_failed;
3790 g_rec_mutex_lock (&priv->state_lock);
3801 GST_WARNING ("failed to preroll pipeline");
3806 GST_WARNING ("failed to preroll pipeline");
3812 * gst_rtsp_media_unsuspend:
3813 * @media: a #GstRTSPMedia
3815 * Unsuspend @media if it was in a suspended state. This method does nothing
3816 * when the media was not in the suspended state.
3818 * Returns: %TRUE on success.
3821 gst_rtsp_media_unsuspend (GstRTSPMedia * media)
3823 GstRTSPMediaPrivate *priv = media->priv;
3824 GstRTSPMediaClass *klass;
3826 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3828 g_rec_mutex_lock (&priv->state_lock);
3829 if (priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
3832 klass = GST_RTSP_MEDIA_GET_CLASS (media);
3833 if (klass->unsuspend) {
3834 if (!klass->unsuspend (media))
3835 goto unsuspend_failed;
3839 g_rec_mutex_unlock (&priv->state_lock);
3846 g_rec_mutex_unlock (&priv->state_lock);
3847 GST_WARNING ("failed to unsuspend media %p", media);
3848 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
3853 /* must be called with state-lock */
3855 media_set_pipeline_state_locked (GstRTSPMedia * media, GstState state)
3857 GstRTSPMediaPrivate *priv = media->priv;
3859 if (state == GST_STATE_NULL) {
3860 gst_rtsp_media_unprepare (media);
3862 GST_INFO ("state %s media %p", gst_element_state_get_name (state), media);
3863 set_target_state (media, state, FALSE);
3864 /* when we are buffering, don't update the state yet, this will be done
3865 * when buffering finishes */
3866 if (priv->buffering) {
3867 GST_INFO ("Buffering busy, delay state change");
3869 if (state == GST_STATE_PLAYING)
3870 /* make sure pads are not blocking anymore when going to PLAYING */
3871 media_streams_set_blocked (media, FALSE);
3873 set_state (media, state);
3875 /* and suspend after pause */
3876 if (state == GST_STATE_PAUSED)
3877 gst_rtsp_media_suspend (media);
3883 * gst_rtsp_media_set_pipeline_state:
3884 * @media: a #GstRTSPMedia
3885 * @state: the target state of the pipeline
3887 * Set the state of the pipeline managed by @media to @state
3890 gst_rtsp_media_set_pipeline_state (GstRTSPMedia * media, GstState state)
3892 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
3894 g_rec_mutex_lock (&media->priv->state_lock);
3895 media_set_pipeline_state_locked (media, state);
3896 g_rec_mutex_unlock (&media->priv->state_lock);
3900 * gst_rtsp_media_set_state:
3901 * @media: a #GstRTSPMedia
3902 * @state: the target state of the media
3903 * @transports: (transfer none) (element-type GstRtspServer.RTSPStreamTransport):
3904 * a #GPtrArray of #GstRTSPStreamTransport pointers
3906 * Set the state of @media to @state and for the transports in @transports.
3908 * @media must be prepared with gst_rtsp_media_prepare();
3910 * Returns: %TRUE on success.
3913 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
3914 GPtrArray * transports)
3916 GstRTSPMediaPrivate *priv;
3918 gboolean activate, deactivate, do_state;
3921 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
3922 g_return_val_if_fail (transports != NULL, FALSE);
3926 g_rec_mutex_lock (&priv->state_lock);
3927 if (priv->status == GST_RTSP_MEDIA_STATUS_ERROR)
3929 if (priv->status != GST_RTSP_MEDIA_STATUS_PREPARED &&
3930 priv->status != GST_RTSP_MEDIA_STATUS_SUSPENDED)
3933 /* NULL and READY are the same */
3934 if (state == GST_STATE_READY)
3935 state = GST_STATE_NULL;
3937 activate = deactivate = FALSE;
3939 GST_INFO ("going to state %s media %p, target state %s",
3940 gst_element_state_get_name (state), media,
3941 gst_element_state_get_name (priv->target_state));
3944 case GST_STATE_NULL:
3945 /* we're going from PLAYING or PAUSED to READY or NULL, deactivate */
3946 if (priv->target_state >= GST_STATE_PAUSED)
3949 case GST_STATE_PAUSED:
3950 /* we're going from PLAYING to PAUSED, READY or NULL, deactivate */
3951 if (priv->target_state == GST_STATE_PLAYING)
3954 case GST_STATE_PLAYING:
3955 /* we're going to PLAYING, activate */
3961 old_active = priv->n_active;
3963 GST_DEBUG ("%d transports, activate %d, deactivate %d", transports->len,
3964 activate, deactivate);
3965 for (i = 0; i < transports->len; i++) {
3966 GstRTSPStreamTransport *trans;
3968 /* we need a non-NULL entry in the array */
3969 trans = g_ptr_array_index (transports, i);
3974 if (gst_rtsp_stream_transport_set_active (trans, TRUE))
3976 } else if (deactivate) {
3977 if (gst_rtsp_stream_transport_set_active (trans, FALSE))
3982 /* we just activated the first media, do the playing state change */
3983 if (old_active == 0 && activate)
3985 /* if we have no more active media, do the downward state changes */
3986 else if (priv->n_active == 0)
3991 GST_INFO ("state %d active %d media %p do_state %d", state, priv->n_active,
3994 if (priv->target_state != state) {
3996 media_set_pipeline_state_locked (media, state);
3998 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
4002 /* remember where we are */
4003 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
4004 old_active != priv->n_active))
4005 collect_media_stats (media);
4007 g_rec_mutex_unlock (&priv->state_lock);
4014 GST_WARNING ("media %p was not prepared", media);
4015 g_rec_mutex_unlock (&priv->state_lock);
4020 GST_WARNING ("media %p in error status while changing to state %d",
4022 if (state == GST_STATE_NULL) {
4023 for (i = 0; i < transports->len; i++) {
4024 GstRTSPStreamTransport *trans;
4026 /* we need a non-NULL entry in the array */
4027 trans = g_ptr_array_index (transports, i);
4031 gst_rtsp_stream_transport_set_active (trans, FALSE);
4035 g_rec_mutex_unlock (&priv->state_lock);
4041 * gst_rtsp_media_set_transport_mode:
4042 * @media: a #GstRTSPMedia
4043 * @mode: the new value
4045 * Sets if the media pipeline can work in PLAY or RECORD mode
4048 gst_rtsp_media_set_transport_mode (GstRTSPMedia * media,
4049 GstRTSPTransportMode mode)
4051 GstRTSPMediaPrivate *priv;
4053 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
4057 g_mutex_lock (&priv->lock);
4058 priv->transport_mode = mode;
4059 g_mutex_unlock (&priv->lock);
4063 * gst_rtsp_media_get_transport_mode:
4064 * @media: a #GstRTSPMedia
4066 * Check if the pipeline for @media can be used for PLAY or RECORD methods.
4068 * Returns: The transport mode.
4070 GstRTSPTransportMode
4071 gst_rtsp_media_get_transport_mode (GstRTSPMedia * media)
4073 GstRTSPMediaPrivate *priv;
4074 GstRTSPTransportMode res;
4076 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
4080 g_mutex_lock (&priv->lock);
4081 res = priv->transport_mode;
4082 g_mutex_unlock (&priv->lock);