2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
23 #include <gst/app/gstappsrc.h>
24 #include <gst/app/gstappsink.h>
26 #include "rtsp-media.h"
28 #define DEFAULT_SHARED FALSE
29 #define DEFAULT_REUSABLE FALSE
30 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_TCP
31 //#define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP_MCAST
32 #define DEFAULT_EOS_SHUTDOWN FALSE
33 #define DEFAULT_BUFFER_SIZE 0x80000
34 #define DEFAULT_MULTICAST_GROUP "224.2.0.1"
37 /* define to dump received RTCP packets */
61 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
62 #define GST_CAT_DEFAULT rtsp_media_debug
64 static void gst_rtsp_media_get_property (GObject * object, guint propid,
65 GValue * value, GParamSpec * pspec);
66 static void gst_rtsp_media_set_property (GObject * object, guint propid,
67 const GValue * value, GParamSpec * pspec);
68 static void gst_rtsp_media_finalize (GObject * obj);
70 static gpointer do_loop (GstRTSPMediaClass * klass);
71 static gboolean default_handle_message (GstRTSPMedia * media,
72 GstMessage * message);
73 static void finish_unprepare (GstRTSPMedia * media);
74 static gboolean default_unprepare (GstRTSPMedia * media);
76 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
78 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
81 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
83 GObjectClass *gobject_class;
85 gobject_class = G_OBJECT_CLASS (klass);
87 gobject_class->get_property = gst_rtsp_media_get_property;
88 gobject_class->set_property = gst_rtsp_media_set_property;
89 gobject_class->finalize = gst_rtsp_media_finalize;
91 g_object_class_install_property (gobject_class, PROP_SHARED,
92 g_param_spec_boolean ("shared", "Shared",
93 "If this media pipeline can be shared", DEFAULT_SHARED,
94 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
96 g_object_class_install_property (gobject_class, PROP_REUSABLE,
97 g_param_spec_boolean ("reusable", "Reusable",
98 "If this media pipeline can be reused after an unprepare",
99 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
101 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
102 g_param_spec_flags ("protocols", "Protocols",
103 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
104 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
106 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
107 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
108 "Send an EOS event to the pipeline before unpreparing",
109 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
111 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
112 g_param_spec_uint ("buffer-size", "Buffer Size",
113 "The kernel UDP buffer size to use", 0, G_MAXUINT,
114 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
116 g_object_class_install_property (gobject_class, PROP_MULTICAST_GROUP,
117 g_param_spec_string ("multicast-group", "Multicast Group",
118 "The Multicast group to send media to",
119 DEFAULT_MULTICAST_GROUP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
121 g_object_class_install_property (gobject_class, PROP_MTU,
122 g_param_spec_uint ("mtu", "MTU",
123 "The MTU for the payloaders (0 = default)",
124 0, G_MAXUINT, DEFAULT_MTU,
125 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
127 gst_rtsp_media_signals[SIGNAL_PREPARED] =
128 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
129 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
130 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
132 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
133 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
134 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
135 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
137 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
138 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
139 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
140 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 0, G_TYPE_INT);
142 klass->context = g_main_context_new ();
143 klass->loop = g_main_loop_new (klass->context, TRUE);
145 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
147 klass->thread = g_thread_new ("Bus Thread", (GThreadFunc) do_loop, klass);
149 klass->handle_message = default_handle_message;
150 klass->unprepare = default_unprepare;
154 gst_rtsp_media_init (GstRTSPMedia * media)
156 media->streams = g_ptr_array_new_with_free_func (g_object_unref);
157 g_mutex_init (&media->lock);
158 g_cond_init (&media->cond);
159 g_rec_mutex_init (&media->state_lock);
161 media->shared = DEFAULT_SHARED;
162 media->reusable = DEFAULT_REUSABLE;
163 media->protocols = DEFAULT_PROTOCOLS;
164 media->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
165 media->buffer_size = DEFAULT_BUFFER_SIZE;
166 media->multicast_group = g_strdup (DEFAULT_MULTICAST_GROUP);
170 gst_rtsp_media_finalize (GObject * obj)
174 media = GST_RTSP_MEDIA (obj);
176 GST_INFO ("finalize media %p", media);
178 gst_rtsp_media_unprepare (media);
180 g_ptr_array_unref (media->streams);
182 g_list_free_full (media->dynamic, gst_object_unref);
185 g_source_destroy (media->source);
186 g_source_unref (media->source);
188 g_free (media->multicast_group);
189 g_mutex_clear (&media->lock);
190 g_cond_clear (&media->cond);
191 g_rec_mutex_clear (&media->state_lock);
193 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
197 gst_rtsp_media_get_property (GObject * object, guint propid,
198 GValue * value, GParamSpec * pspec)
200 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
204 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
207 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
210 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
212 case PROP_EOS_SHUTDOWN:
213 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
215 case PROP_BUFFER_SIZE:
216 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
218 case PROP_MULTICAST_GROUP:
219 g_value_take_string (value, gst_rtsp_media_get_multicast_group (media));
222 g_value_set_uint (value, gst_rtsp_media_get_mtu (media));
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
230 gst_rtsp_media_set_property (GObject * object, guint propid,
231 const GValue * value, GParamSpec * pspec)
233 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
237 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
240 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
243 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
245 case PROP_EOS_SHUTDOWN:
246 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
248 case PROP_BUFFER_SIZE:
249 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
251 case PROP_MULTICAST_GROUP:
252 gst_rtsp_media_set_multicast_group (media, g_value_get_string (value));
255 gst_rtsp_media_set_mtu (media, g_value_get_uint (value));
258 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
263 do_loop (GstRTSPMediaClass * klass)
265 GST_INFO ("enter mainloop");
266 g_main_loop_run (klass->loop);
267 GST_INFO ("exit mainloop");
272 /* must be called with state lock */
274 collect_media_stats (GstRTSPMedia * media)
276 gint64 position, duration;
278 media->range.unit = GST_RTSP_RANGE_NPT;
280 GST_INFO ("collect media stats");
282 if (media->is_live) {
283 media->range.min.type = GST_RTSP_TIME_NOW;
284 media->range.min.seconds = -1;
285 media->range.max.type = GST_RTSP_TIME_END;
286 media->range.max.seconds = -1;
288 /* get the position */
289 if (!gst_element_query_position (media->pipeline, GST_FORMAT_TIME,
291 GST_INFO ("position query failed");
295 /* get the duration */
296 if (!gst_element_query_duration (media->pipeline, GST_FORMAT_TIME,
298 GST_INFO ("duration query failed");
302 GST_INFO ("stats: position %" GST_TIME_FORMAT ", duration %"
303 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (duration));
305 if (position == -1) {
306 media->range.min.type = GST_RTSP_TIME_NOW;
307 media->range.min.seconds = -1;
309 media->range.min.type = GST_RTSP_TIME_SECONDS;
310 media->range.min.seconds = ((gdouble) position) / GST_SECOND;
312 if (duration == -1) {
313 media->range.max.type = GST_RTSP_TIME_END;
314 media->range.max.seconds = -1;
316 media->range.max.type = GST_RTSP_TIME_SECONDS;
317 media->range.max.seconds = ((gdouble) duration) / GST_SECOND;
323 * gst_rtsp_media_new:
325 * Create a new #GstRTSPMedia instance. The #GstRTSPMedia object contains the
326 * element to produce RTP data for one or more related (audio/video/..)
329 * Returns: a new #GstRTSPMedia object.
332 gst_rtsp_media_new (void)
334 GstRTSPMedia *result;
336 result = g_object_new (GST_TYPE_RTSP_MEDIA, NULL);
342 * gst_rtsp_media_set_shared:
343 * @media: a #GstRTSPMedia
344 * @shared: the new value
346 * Set or unset if the pipeline for @media can be shared will multiple clients.
347 * When @shared is %TRUE, client requests for this media will share the media
351 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
353 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
355 g_mutex_lock (&media->lock);
356 media->shared = shared;
357 g_mutex_unlock (&media->lock);
361 * gst_rtsp_media_is_shared:
362 * @media: a #GstRTSPMedia
364 * Check if the pipeline for @media can be shared between multiple clients.
366 * Returns: %TRUE if the media can be shared between clients.
369 gst_rtsp_media_is_shared (GstRTSPMedia * media)
373 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
375 g_mutex_lock (&media->lock);
377 g_mutex_unlock (&media->lock);
383 * gst_rtsp_media_set_reusable:
384 * @media: a #GstRTSPMedia
385 * @reusable: the new value
387 * Set or unset if the pipeline for @media can be reused after the pipeline has
391 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
393 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
395 g_mutex_lock (&media->lock);
396 media->reusable = reusable;
397 g_mutex_unlock (&media->lock);
401 * gst_rtsp_media_is_reusable:
402 * @media: a #GstRTSPMedia
404 * Check if the pipeline for @media can be reused after an unprepare.
406 * Returns: %TRUE if the media can be reused
409 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
413 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
415 g_mutex_lock (&media->lock);
416 res = media->reusable;
417 g_mutex_unlock (&media->lock);
423 * gst_rtsp_media_set_protocols:
424 * @media: a #GstRTSPMedia
425 * @protocols: the new flags
427 * Configure the allowed lower transport for @media.
430 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
432 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
434 g_mutex_lock (&media->lock);
435 media->protocols = protocols;
436 g_mutex_unlock (&media->lock);
440 * gst_rtsp_media_get_protocols:
441 * @media: a #GstRTSPMedia
443 * Get the allowed protocols of @media.
445 * Returns: a #GstRTSPLowerTrans
448 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
450 GstRTSPLowerTrans res;
452 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
453 GST_RTSP_LOWER_TRANS_UNKNOWN);
455 g_mutex_lock (&media->lock);
456 res = media->protocols;
457 g_mutex_unlock (&media->lock);
463 * gst_rtsp_media_set_eos_shutdown:
464 * @media: a #GstRTSPMedia
465 * @eos_shutdown: the new value
467 * Set or unset if an EOS event will be sent to the pipeline for @media before
471 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
473 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
475 g_mutex_lock (&media->lock);
476 media->eos_shutdown = eos_shutdown;
477 g_mutex_unlock (&media->lock);
481 * gst_rtsp_media_is_eos_shutdown:
482 * @media: a #GstRTSPMedia
484 * Check if the pipeline for @media will send an EOS down the pipeline before
487 * Returns: %TRUE if the media will send EOS before unpreparing.
490 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
494 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
496 g_mutex_lock (&media->lock);
497 res = media->eos_shutdown;
498 g_mutex_unlock (&media->lock);
504 * gst_rtsp_media_set_buffer_size:
505 * @media: a #GstRTSPMedia
506 * @size: the new value
508 * Set the kernel UDP buffer size.
511 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
513 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
515 g_mutex_lock (&media->lock);
516 media->buffer_size = size;
517 g_mutex_unlock (&media->lock);
521 * gst_rtsp_media_get_buffer_size:
522 * @media: a #GstRTSPMedia
524 * Get the kernel UDP buffer size.
526 * Returns: the kernel UDP buffer size.
529 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
533 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
535 g_mutex_unlock (&media->lock);
536 res = media->buffer_size;
537 g_mutex_unlock (&media->lock);
543 * gst_rtsp_media_set_multicast_group:
544 * @media: a #GstRTSPMedia
545 * @mc: the new multicast group
547 * Set the multicast group that media from @media will be streamed to.
550 gst_rtsp_media_set_multicast_group (GstRTSPMedia * media, const gchar * mc)
552 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
554 g_mutex_lock (&media->lock);
555 g_free (media->multicast_group);
556 media->multicast_group = g_strdup (mc);
557 g_mutex_unlock (&media->lock);
561 * gst_rtsp_media_get_multicast_group:
562 * @media: (transfer full): a #GstRTSPMedia
564 * Get the multicast group that media from @media will be streamed to.
566 * Returns: the multicast group, g_free after usage.
569 gst_rtsp_media_get_multicast_group (GstRTSPMedia * media)
573 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
575 g_mutex_lock (&media->lock);
576 result = g_strdup (media->multicast_group);
577 g_mutex_unlock (&media->lock);
583 * gst_rtsp_media_set_auth:
584 * @media: a #GstRTSPMedia
585 * @auth: a #GstRTSPAuth
587 * configure @auth to be used as the authentication manager of @media.
590 gst_rtsp_media_set_auth (GstRTSPMedia * media, GstRTSPAuth * auth)
594 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
596 g_mutex_lock (&media->lock);
597 if ((old = media->auth) != auth)
598 media->auth = auth ? g_object_ref (auth) : NULL;
601 g_mutex_unlock (&media->lock);
604 g_object_unref (old);
608 * gst_rtsp_media_get_auth:
609 * @media: a #GstRTSPMedia
611 * Get the #GstRTSPAuth used as the authentication manager of @media.
613 * Returns: (transfer full): the #GstRTSPAuth of @media. g_object_unref() after
617 gst_rtsp_media_get_auth (GstRTSPMedia * media)
621 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
623 g_mutex_lock (&media->lock);
624 if ((result = media->auth))
625 g_object_ref (result);
626 g_mutex_unlock (&media->lock);
632 * gst_rtsp_media_set_mtu:
633 * @media: a #GstRTSPMedia
636 * Set maximum size of one RTP packet on the payloaders.
637 * The @mtu will be set on all streams.
640 gst_rtsp_media_set_mtu (GstRTSPMedia * media, guint mtu)
644 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
646 g_mutex_lock (&media->lock);
648 for (i = 0; i < media->streams->len; i++) {
649 GstRTSPStream *stream;
651 GST_INFO ("Setting mtu %u for stream %d", mtu, i);
653 stream = g_ptr_array_index (media->streams, i);
654 gst_rtsp_stream_set_mtu (stream, mtu);
656 g_mutex_unlock (&media->lock);
660 * gst_rtsp_media_get_mtu:
661 * @media: a #GstRTSPMedia
663 * Get the configured MTU.
666 gst_rtsp_media_get_mtu (GstRTSPMedia * media)
670 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
672 g_mutex_lock (&media->lock);
674 g_mutex_unlock (&media->lock);
680 * gst_rtsp_media_collect_streams:
681 * @media: a #GstRTSPMedia
683 * Find all payloader elements, they should be named pay%d in the
684 * element of @media, and create #GstRTSPStreams for them.
686 * Collect all dynamic elements, named dynpay%d, and add them to
687 * the list of dynamic elements.
690 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
692 GstElement *element, *elem;
697 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
699 element = media->element;
702 for (i = 0; have_elem; i++) {
707 name = g_strdup_printf ("pay%d", i);
708 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
709 GST_INFO ("found stream %d with payloader %p", i, elem);
711 /* take the pad of the payloader */
712 pad = gst_element_get_static_pad (elem, "src");
713 /* create the stream */
714 gst_rtsp_media_create_stream (media, elem, pad);
715 g_object_unref (pad);
717 gst_object_unref (elem);
723 name = g_strdup_printf ("dynpay%d", i);
724 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
725 /* a stream that will dynamically create pads to provide RTP packets */
727 GST_INFO ("found dynamic element %d, %p", i, elem);
729 g_mutex_lock (&media->lock);
730 media->dynamic = g_list_prepend (media->dynamic, elem);
731 g_mutex_unlock (&media->lock);
740 * gst_rtsp_media_create_stream:
741 * @media: a #GstRTSPMedia
742 * @payloader: a #GstElement
743 * @srcpad: a source #GstPad
745 * Create a new stream in @media that provides RTP data on @srcpad.
746 * @srcpad should be a pad of an element inside @media->element.
748 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
752 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
755 GstRTSPStream *stream;
760 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
761 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
762 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
763 g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
765 g_mutex_lock (&media->lock);
766 idx = media->streams->len;
768 name = g_strdup_printf ("src_%u", idx);
769 srcpad = gst_ghost_pad_new (name, pad);
770 gst_pad_set_active (srcpad, TRUE);
771 gst_element_add_pad (media->element, srcpad);
774 stream = gst_rtsp_stream_new (idx, payloader, srcpad);
776 gst_rtsp_stream_set_mtu (stream, media->mtu);
778 g_ptr_array_add (media->streams, stream);
779 g_mutex_unlock (&media->lock);
785 * gst_rtsp_media_n_streams:
786 * @media: a #GstRTSPMedia
788 * Get the number of streams in this media.
790 * Returns: The number of streams.
793 gst_rtsp_media_n_streams (GstRTSPMedia * media)
797 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
799 g_mutex_lock (&media->lock);
800 res = media->streams->len;
801 g_mutex_unlock (&media->lock);
807 * gst_rtsp_media_get_stream:
808 * @media: a #GstRTSPMedia
809 * @idx: the stream index
811 * Retrieve the stream with index @idx from @media.
813 * Returns: (transfer none): the #GstRTSPStream at index @idx or %NULL when a stream with
814 * that index did not exist.
817 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
821 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
823 g_mutex_lock (&media->lock);
824 if (idx < media->streams->len)
825 res = g_ptr_array_index (media->streams, idx);
828 g_mutex_unlock (&media->lock);
834 * gst_rtsp_media_get_range_string:
835 * @media: a #GstRTSPMedia
836 * @play: for the PLAY request
838 * Get the current range as a string.
840 * Returns: The range as a string, g_free() after usage.
843 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play)
846 GstRTSPTimeRange range;
848 g_mutex_lock (&media->lock);
850 range = media->range;
852 if (!play && media->n_active > 0) {
853 range.min.type = GST_RTSP_TIME_NOW;
854 range.min.seconds = -1;
856 g_mutex_unlock (&media->lock);
858 result = gst_rtsp_range_to_string (&range);
864 * gst_rtsp_media_seek:
865 * @media: a #GstRTSPMedia
866 * @range: a #GstRTSPTimeRange
868 * Seek the pipeline to @range.
870 * Returns: %TRUE on success.
873 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
878 GstSeekType start_type, stop_type;
880 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
881 g_return_val_if_fail (range != NULL, FALSE);
883 g_rec_mutex_lock (&media->state_lock);
884 if (!media->seekable)
887 if (range->unit != GST_RTSP_RANGE_NPT)
890 /* depends on the current playing state of the pipeline. We might need to
891 * queue this until we get EOS. */
892 flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_KEY_UNIT;
894 start_type = stop_type = GST_SEEK_TYPE_NONE;
896 switch (range->min.type) {
897 case GST_RTSP_TIME_NOW:
900 case GST_RTSP_TIME_SECONDS:
901 /* only seek when something changed */
902 if (media->range.min.seconds == range->min.seconds) {
905 start = range->min.seconds * GST_SECOND;
906 start_type = GST_SEEK_TYPE_SET;
909 case GST_RTSP_TIME_END:
913 switch (range->max.type) {
914 case GST_RTSP_TIME_SECONDS:
915 /* only seek when something changed */
916 if (media->range.max.seconds == range->max.seconds) {
919 stop = range->max.seconds * GST_SECOND;
920 stop_type = GST_SEEK_TYPE_SET;
923 case GST_RTSP_TIME_END:
925 stop_type = GST_SEEK_TYPE_SET;
927 case GST_RTSP_TIME_NOW:
932 if (start != -1 || stop != -1) {
933 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
934 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
936 res = gst_element_seek (media->pipeline, 1.0, GST_FORMAT_TIME,
937 flags, start_type, start, stop_type, stop);
939 /* and block for the seek to complete */
940 GST_INFO ("done seeking %d", res);
941 gst_element_get_state (media->pipeline, NULL, NULL, -1);
942 GST_INFO ("prerolled again");
944 collect_media_stats (media);
946 GST_INFO ("no seek needed");
949 g_rec_mutex_unlock (&media->state_lock);
956 g_rec_mutex_unlock (&media->state_lock);
957 GST_INFO ("pipeline is not seekable");
962 g_rec_mutex_unlock (&media->state_lock);
963 GST_WARNING ("seek unit %d not supported", range->unit);
968 g_rec_mutex_unlock (&media->state_lock);
969 GST_WARNING ("weird range type %d not supported", range->min.type);
975 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
977 g_mutex_lock (&media->lock);
978 /* never overwrite the error status */
979 if (media->status != GST_RTSP_MEDIA_STATUS_ERROR)
980 media->status = status;
981 GST_DEBUG ("setting new status to %d", status);
982 g_cond_broadcast (&media->cond);
983 g_mutex_unlock (&media->lock);
986 static GstRTSPMediaStatus
987 gst_rtsp_media_get_status (GstRTSPMedia * media)
989 GstRTSPMediaStatus result;
992 g_mutex_lock (&media->lock);
993 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
994 /* while we are preparing, wait */
995 while (media->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
996 GST_DEBUG ("waiting for status change");
997 if (!g_cond_wait_until (&media->cond, &media->lock, end_time)) {
998 GST_DEBUG ("timeout, assuming error status");
999 media->status = GST_RTSP_MEDIA_STATUS_ERROR;
1002 /* could be success or error */
1003 result = media->status;
1004 GST_DEBUG ("got status %d", result);
1005 g_mutex_unlock (&media->lock);
1011 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1013 GstMessageType type;
1015 type = GST_MESSAGE_TYPE (message);
1018 case GST_MESSAGE_STATE_CHANGED:
1020 case GST_MESSAGE_BUFFERING:
1024 gst_message_parse_buffering (message, &percent);
1026 /* no state management needed for live pipelines */
1030 g_rec_mutex_lock (&media->state_lock);
1031 if (percent == 100) {
1032 /* a 100% message means buffering is done */
1033 media->buffering = FALSE;
1034 /* if the desired state is playing, go back */
1035 if (media->target_state == GST_STATE_PLAYING) {
1036 GST_INFO ("Buffering done, setting pipeline to PLAYING");
1037 gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1039 GST_INFO ("Buffering done");
1042 /* buffering busy */
1043 if (media->buffering == FALSE) {
1044 if (media->target_state == GST_STATE_PLAYING) {
1045 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1046 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1047 gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1049 GST_INFO ("Buffering ...");
1052 media->buffering = TRUE;
1053 g_rec_mutex_unlock (&media->state_lock);
1057 case GST_MESSAGE_LATENCY:
1059 gst_bin_recalculate_latency (GST_BIN_CAST (media->pipeline));
1062 case GST_MESSAGE_ERROR:
1067 gst_message_parse_error (message, &gerror, &debug);
1068 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1069 g_error_free (gerror);
1072 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1075 case GST_MESSAGE_WARNING:
1080 gst_message_parse_warning (message, &gerror, &debug);
1081 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1082 g_error_free (gerror);
1086 case GST_MESSAGE_ELEMENT:
1088 case GST_MESSAGE_STREAM_STATUS:
1090 case GST_MESSAGE_ASYNC_DONE:
1091 g_rec_mutex_lock (&media->state_lock);
1092 if (!media->adding) {
1093 /* when we are dynamically adding pads, the addition of the udpsrc will
1094 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1095 * wait for the final ASYNC_DONE after everything prerolled */
1096 GST_INFO ("%p: got ASYNC_DONE", media);
1097 collect_media_stats (media);
1099 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1101 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1103 g_rec_mutex_unlock (&media->state_lock);
1105 case GST_MESSAGE_EOS:
1106 GST_INFO ("%p: got EOS", media);
1108 g_rec_mutex_lock (&media->state_lock);
1109 if (media->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
1110 GST_DEBUG ("shutting down after EOS");
1111 finish_unprepare (media);
1112 g_object_unref (media);
1114 g_rec_mutex_unlock (&media->state_lock);
1117 GST_INFO ("%p: got message type %s", media,
1118 gst_message_type_get_name (type));
1125 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1127 GstRTSPMediaClass *klass;
1130 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1132 if (klass->handle_message)
1133 ret = klass->handle_message (media, message);
1140 /* called from streaming threads */
1142 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1144 GstRTSPStream *stream;
1146 /* FIXME, element is likely not a payloader, find the payloader here */
1147 stream = gst_rtsp_media_create_stream (media, element, pad);
1149 GST_INFO ("pad added %s:%s, stream %d", GST_DEBUG_PAD_NAME (pad),
1152 g_rec_mutex_lock (&media->state_lock);
1153 /* we will be adding elements below that will cause ASYNC_DONE to be
1154 * posted in the bus. We want to ignore those messages until the
1155 * pipeline really prerolled. */
1156 media->adding = TRUE;
1158 /* join the element in the PAUSED state because this callback is
1159 * called from the streaming thread and it is PAUSED */
1160 gst_rtsp_stream_join_bin (stream, GST_BIN (media->pipeline),
1161 media->rtpbin, GST_STATE_PAUSED);
1163 media->adding = FALSE;
1164 g_rec_mutex_unlock (&media->state_lock);
1168 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1170 GstElement *fakesink;
1172 g_mutex_lock (&media->lock);
1173 GST_INFO ("no more pads");
1174 if ((fakesink = media->fakesink)) {
1175 gst_object_ref (fakesink);
1176 media->fakesink = NULL;
1177 g_mutex_unlock (&media->lock);
1179 gst_bin_remove (GST_BIN (media->pipeline), fakesink);
1180 gst_element_set_state (fakesink, GST_STATE_NULL);
1181 gst_object_unref (fakesink);
1182 GST_INFO ("removed fakesink");
1187 * gst_rtsp_media_prepare:
1188 * @media: a #GstRTSPMedia
1190 * Prepare @media for streaming. This function will create the pipeline and
1191 * other objects to manage the streaming.
1193 * It will preroll the pipeline and collect vital information about the streams
1194 * such as the duration.
1196 * Returns: %TRUE on success.
1199 gst_rtsp_media_prepare (GstRTSPMedia * media)
1201 GstStateChangeReturn ret;
1202 GstRTSPMediaStatus status;
1204 GstRTSPMediaClass *klass;
1208 g_rec_mutex_lock (&media->state_lock);
1209 if (media->status == GST_RTSP_MEDIA_STATUS_PREPARED)
1212 if (!media->reusable && media->reused)
1215 media->rtpbin = gst_element_factory_make ("rtpbin", NULL);
1216 if (media->rtpbin == NULL)
1219 GST_INFO ("preparing media %p", media);
1221 /* reset some variables */
1222 media->is_live = FALSE;
1223 media->seekable = FALSE;
1224 media->buffering = FALSE;
1225 /* we're preparing now */
1226 media->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1228 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (media->pipeline));
1230 /* add the pipeline bus to our custom mainloop */
1231 media->source = gst_bus_create_watch (bus);
1232 gst_object_unref (bus);
1234 g_source_set_callback (media->source, (GSourceFunc) bus_message, media, NULL);
1236 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1237 media->id = g_source_attach (media->source, klass->context);
1239 /* add stuff to the bin */
1240 gst_bin_add (GST_BIN (media->pipeline), media->rtpbin);
1242 /* link streams we already have, other streams might appear when we have
1243 * dynamic elements */
1244 for (i = 0; i < media->streams->len; i++) {
1245 GstRTSPStream *stream;
1247 stream = g_ptr_array_index (media->streams, i);
1249 gst_rtsp_stream_join_bin (stream, GST_BIN (media->pipeline),
1250 media->rtpbin, GST_STATE_NULL);
1253 for (walk = media->dynamic; walk; walk = g_list_next (walk)) {
1254 GstElement *elem = walk->data;
1256 GST_INFO ("adding callbacks for dynamic element %p", elem);
1258 g_signal_connect (elem, "pad-added", (GCallback) pad_added_cb, media);
1259 g_signal_connect (elem, "no-more-pads", (GCallback) no_more_pads_cb, media);
1261 /* we add a fakesink here in order to make the state change async. We remove
1262 * the fakesink again in the no-more-pads callback. */
1263 media->fakesink = gst_element_factory_make ("fakesink", "fakesink");
1264 gst_bin_add (GST_BIN (media->pipeline), media->fakesink);
1267 GST_INFO ("setting pipeline to PAUSED for media %p", media);
1268 /* first go to PAUSED */
1269 ret = gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1270 media->target_state = GST_STATE_PAUSED;
1273 case GST_STATE_CHANGE_SUCCESS:
1274 GST_INFO ("SUCCESS state change for media %p", media);
1275 media->seekable = TRUE;
1277 case GST_STATE_CHANGE_ASYNC:
1278 GST_INFO ("ASYNC state change for media %p", media);
1279 media->seekable = TRUE;
1281 case GST_STATE_CHANGE_NO_PREROLL:
1282 /* we need to go to PLAYING */
1283 GST_INFO ("NO_PREROLL state change: live media %p", media);
1284 /* FIXME we disable seeking for live streams for now. We should perform a
1285 * seeking query in preroll instead */
1286 media->seekable = FALSE;
1287 media->is_live = TRUE;
1288 ret = gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1289 if (ret == GST_STATE_CHANGE_FAILURE)
1292 case GST_STATE_CHANGE_FAILURE:
1295 g_rec_mutex_unlock (&media->state_lock);
1297 /* now wait for all pads to be prerolled, FIXME, we should somehow be
1298 * able to do this async so that we don't block the server thread. */
1299 status = gst_rtsp_media_get_status (media);
1300 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1303 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
1305 GST_INFO ("object %p is prerolled", media);
1312 g_rec_mutex_unlock (&media->state_lock);
1318 g_rec_mutex_unlock (&media->state_lock);
1319 GST_WARNING ("can not reuse media %p", media);
1324 g_rec_mutex_unlock (&media->state_lock);
1325 GST_WARNING ("no rtpbin element");
1326 g_warning ("failed to create element 'rtpbin', check your installation");
1331 GST_WARNING ("failed to preroll pipeline");
1332 gst_rtsp_media_unprepare (media);
1333 g_rec_mutex_unlock (&media->state_lock);
1338 /* must be called with state-lock */
1340 finish_unprepare (GstRTSPMedia * media)
1344 GST_DEBUG ("shutting down");
1346 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1348 for (i = 0; i < media->streams->len; i++) {
1349 GstRTSPStream *stream;
1351 GST_INFO ("Removing elements of stream %d from pipeline", i);
1353 stream = g_ptr_array_index (media->streams, i);
1355 gst_rtsp_stream_leave_bin (stream, GST_BIN (media->pipeline),
1358 g_ptr_array_set_size (media->streams, 0);
1360 gst_bin_remove (GST_BIN (media->pipeline), media->rtpbin);
1361 media->rtpbin = NULL;
1363 gst_object_unref (media->pipeline);
1364 media->pipeline = NULL;
1366 media->reused = TRUE;
1367 media->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
1369 /* when the media is not reusable, this will effectively unref the media and
1371 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
1374 /* called with state-lock */
1376 default_unprepare (GstRTSPMedia * media)
1378 if (media->eos_shutdown) {
1379 GST_DEBUG ("sending EOS for shutdown");
1380 /* ref so that we don't disappear */
1381 g_object_ref (media);
1382 gst_element_send_event (media->pipeline, gst_event_new_eos ());
1383 /* we need to go to playing again for the EOS to propagate, normally in this
1384 * state, nothing is receiving data from us anymore so this is ok. */
1385 gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1386 media->status = GST_RTSP_MEDIA_STATUS_UNPREPARING;
1388 finish_unprepare (media);
1394 * gst_rtsp_media_unprepare:
1395 * @media: a #GstRTSPMedia
1397 * Unprepare @media. After this call, the media should be prepared again before
1398 * it can be used again. If the media is set to be non-reusable, a new instance
1401 * Returns: %TRUE on success.
1404 gst_rtsp_media_unprepare (GstRTSPMedia * media)
1408 g_rec_mutex_lock (&media->state_lock);
1409 if (media->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
1410 goto was_unprepared;
1412 GST_INFO ("unprepare media %p", media);
1413 media->target_state = GST_STATE_NULL;
1416 if (media->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
1417 GstRTSPMediaClass *klass;
1419 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1420 if (klass->unprepare)
1421 success = klass->unprepare (media);
1423 finish_unprepare (media);
1425 g_rec_mutex_unlock (&media->state_lock);
1431 g_rec_mutex_unlock (&media->state_lock);
1432 GST_INFO ("media %p was already unprepared", media);
1438 * gst_rtsp_media_set_state:
1439 * @media: a #GstRTSPMedia
1440 * @state: the target state of the media
1441 * @transports: a #GPtrArray of #GstRTSPStreamTransport pointers
1443 * Set the state of @media to @state and for the transports in @transports.
1445 * Returns: %TRUE on success.
1448 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
1449 GPtrArray * transports)
1452 gboolean add, remove, do_state;
1455 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1456 g_return_val_if_fail (transports != NULL, FALSE);
1458 g_rec_mutex_lock (&media->state_lock);
1460 /* NULL and READY are the same */
1461 if (state == GST_STATE_READY)
1462 state = GST_STATE_NULL;
1464 add = remove = FALSE;
1466 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
1470 case GST_STATE_NULL:
1471 case GST_STATE_PAUSED:
1472 /* we're going from PLAYING to PAUSED, READY or NULL, remove */
1473 if (media->target_state == GST_STATE_PLAYING)
1476 case GST_STATE_PLAYING:
1477 /* we're going to PLAYING, add */
1483 old_active = media->n_active;
1485 for (i = 0; i < transports->len; i++) {
1486 GstRTSPStreamTransport *trans;
1488 /* we need a non-NULL entry in the array */
1489 trans = g_ptr_array_index (transports, i);
1493 /* we need a transport */
1494 if (!trans->transport)
1498 if (gst_rtsp_stream_add_transport (trans->stream, trans))
1500 } else if (remove) {
1501 if (gst_rtsp_stream_remove_transport (trans->stream, trans))
1506 /* we just added the first media, do the playing state change */
1507 if (old_active == 0 && add)
1509 /* if we have no more active media, do the downward state changes */
1510 else if (media->n_active == 0)
1515 GST_INFO ("state %d active %d media %p do_state %d", state, media->n_active,
1518 if (media->target_state != state) {
1520 if (state == GST_STATE_NULL) {
1521 gst_rtsp_media_unprepare (media);
1523 GST_INFO ("state %s media %p", gst_element_state_get_name (state),
1525 media->target_state = state;
1526 gst_element_set_state (media->pipeline, state);
1529 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
1533 /* remember where we are */
1534 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
1535 old_active != media->n_active))
1536 collect_media_stats (media);
1538 g_rec_mutex_unlock (&media->state_lock);