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
35 /* define to dump received RTCP packets */
58 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
59 #define GST_CAT_DEFAULT rtsp_media_debug
61 static void gst_rtsp_media_get_property (GObject * object, guint propid,
62 GValue * value, GParamSpec * pspec);
63 static void gst_rtsp_media_set_property (GObject * object, guint propid,
64 const GValue * value, GParamSpec * pspec);
65 static void gst_rtsp_media_finalize (GObject * obj);
67 static gpointer do_loop (GstRTSPMediaClass * klass);
68 static gboolean default_handle_message (GstRTSPMedia * media,
69 GstMessage * message);
70 static void finish_unprepare (GstRTSPMedia * media);
71 static gboolean default_unprepare (GstRTSPMedia * media);
73 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
75 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
78 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
80 GObjectClass *gobject_class;
82 gobject_class = G_OBJECT_CLASS (klass);
84 gobject_class->get_property = gst_rtsp_media_get_property;
85 gobject_class->set_property = gst_rtsp_media_set_property;
86 gobject_class->finalize = gst_rtsp_media_finalize;
88 g_object_class_install_property (gobject_class, PROP_SHARED,
89 g_param_spec_boolean ("shared", "Shared",
90 "If this media pipeline can be shared", DEFAULT_SHARED,
91 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
93 g_object_class_install_property (gobject_class, PROP_REUSABLE,
94 g_param_spec_boolean ("reusable", "Reusable",
95 "If this media pipeline can be reused after an unprepare",
96 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
98 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
99 g_param_spec_flags ("protocols", "Protocols",
100 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
101 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
103 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
104 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
105 "Send an EOS event to the pipeline before unpreparing",
106 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
108 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
109 g_param_spec_uint ("buffer-size", "Buffer Size",
110 "The kernel UDP buffer size to use", 0, G_MAXUINT,
111 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
113 gst_rtsp_media_signals[SIGNAL_NEW_STREAM] =
114 g_signal_new ("new-stream", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
115 G_STRUCT_OFFSET (GstRTSPMediaClass, new_stream), NULL, NULL,
116 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_RTSP_STREAM);
118 gst_rtsp_media_signals[SIGNAL_PREPARED] =
119 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
120 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
121 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
123 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
124 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
125 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
126 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
128 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
129 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
130 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
131 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 0, G_TYPE_INT);
133 klass->context = g_main_context_new ();
134 klass->loop = g_main_loop_new (klass->context, TRUE);
136 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
138 klass->thread = g_thread_new ("Bus Thread", (GThreadFunc) do_loop, klass);
140 klass->handle_message = default_handle_message;
141 klass->unprepare = default_unprepare;
145 gst_rtsp_media_init (GstRTSPMedia * media)
147 media->streams = g_ptr_array_new_with_free_func (g_object_unref);
148 g_mutex_init (&media->lock);
149 g_cond_init (&media->cond);
150 g_rec_mutex_init (&media->state_lock);
152 media->shared = DEFAULT_SHARED;
153 media->reusable = DEFAULT_REUSABLE;
154 media->protocols = DEFAULT_PROTOCOLS;
155 media->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
156 media->buffer_size = DEFAULT_BUFFER_SIZE;
160 gst_rtsp_media_finalize (GObject * obj)
164 media = GST_RTSP_MEDIA (obj);
166 GST_INFO ("finalize media %p", media);
168 gst_rtsp_media_unprepare (media);
170 g_ptr_array_unref (media->streams);
172 g_list_free_full (media->dynamic, gst_object_unref);
175 g_object_unref (media->auth);
177 g_object_unref (media->pool);
178 g_mutex_clear (&media->lock);
179 g_cond_clear (&media->cond);
180 g_rec_mutex_clear (&media->state_lock);
182 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
186 gst_rtsp_media_get_property (GObject * object, guint propid,
187 GValue * value, GParamSpec * pspec)
189 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
193 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
196 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
199 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
201 case PROP_EOS_SHUTDOWN:
202 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
204 case PROP_BUFFER_SIZE:
205 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
213 gst_rtsp_media_set_property (GObject * object, guint propid,
214 const GValue * value, GParamSpec * pspec)
216 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
220 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
223 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
226 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
228 case PROP_EOS_SHUTDOWN:
229 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
231 case PROP_BUFFER_SIZE:
232 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
240 do_loop (GstRTSPMediaClass * klass)
242 GST_INFO ("enter mainloop");
243 g_main_loop_run (klass->loop);
244 GST_INFO ("exit mainloop");
249 /* must be called with state lock */
251 collect_media_stats (GstRTSPMedia * media)
253 gint64 position, duration;
255 media->range.unit = GST_RTSP_RANGE_NPT;
257 GST_INFO ("collect media stats");
259 if (media->is_live) {
260 media->range.min.type = GST_RTSP_TIME_NOW;
261 media->range.min.seconds = -1;
262 media->range_start = -1;
263 media->range.max.type = GST_RTSP_TIME_END;
264 media->range.max.seconds = -1;
265 media->range_stop = -1;
267 /* get the position */
268 if (!gst_element_query_position (media->pipeline, GST_FORMAT_TIME,
270 GST_INFO ("position query failed");
274 /* get the duration */
275 if (!gst_element_query_duration (media->pipeline, GST_FORMAT_TIME,
277 GST_INFO ("duration query failed");
281 GST_INFO ("stats: position %" GST_TIME_FORMAT ", duration %"
282 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (duration));
284 if (position == -1) {
285 media->range.min.type = GST_RTSP_TIME_NOW;
286 media->range.min.seconds = -1;
287 media->range_start = -1;
289 media->range.min.type = GST_RTSP_TIME_SECONDS;
290 media->range.min.seconds = ((gdouble) position) / GST_SECOND;
291 media->range_start = position;
293 if (duration == -1) {
294 media->range.max.type = GST_RTSP_TIME_END;
295 media->range.max.seconds = -1;
296 media->range_stop = -1;
298 media->range.max.type = GST_RTSP_TIME_SECONDS;
299 media->range.max.seconds = ((gdouble) duration) / GST_SECOND;
300 media->range_stop = duration;
306 * gst_rtsp_media_new:
308 * Create a new #GstRTSPMedia instance. The #GstRTSPMedia object contains the
309 * element to produce RTP data for one or more related (audio/video/..)
312 * Returns: a new #GstRTSPMedia object.
315 gst_rtsp_media_new (void)
317 GstRTSPMedia *result;
319 result = g_object_new (GST_TYPE_RTSP_MEDIA, NULL);
325 * gst_rtsp_media_set_shared:
326 * @media: a #GstRTSPMedia
327 * @shared: the new value
329 * Set or unset if the pipeline for @media can be shared will multiple clients.
330 * When @shared is %TRUE, client requests for this media will share the media
334 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
336 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
338 g_mutex_lock (&media->lock);
339 media->shared = shared;
340 g_mutex_unlock (&media->lock);
344 * gst_rtsp_media_is_shared:
345 * @media: a #GstRTSPMedia
347 * Check if the pipeline for @media can be shared between multiple clients.
349 * Returns: %TRUE if the media can be shared between clients.
352 gst_rtsp_media_is_shared (GstRTSPMedia * media)
356 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
358 g_mutex_lock (&media->lock);
360 g_mutex_unlock (&media->lock);
366 * gst_rtsp_media_set_reusable:
367 * @media: a #GstRTSPMedia
368 * @reusable: the new value
370 * Set or unset if the pipeline for @media can be reused after the pipeline has
374 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
376 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
378 g_mutex_lock (&media->lock);
379 media->reusable = reusable;
380 g_mutex_unlock (&media->lock);
384 * gst_rtsp_media_is_reusable:
385 * @media: a #GstRTSPMedia
387 * Check if the pipeline for @media can be reused after an unprepare.
389 * Returns: %TRUE if the media can be reused
392 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
396 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
398 g_mutex_lock (&media->lock);
399 res = media->reusable;
400 g_mutex_unlock (&media->lock);
406 * gst_rtsp_media_set_protocols:
407 * @media: a #GstRTSPMedia
408 * @protocols: the new flags
410 * Configure the allowed lower transport for @media.
413 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
415 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
417 g_mutex_lock (&media->lock);
418 media->protocols = protocols;
419 g_mutex_unlock (&media->lock);
423 * gst_rtsp_media_get_protocols:
424 * @media: a #GstRTSPMedia
426 * Get the allowed protocols of @media.
428 * Returns: a #GstRTSPLowerTrans
431 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
433 GstRTSPLowerTrans res;
435 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
436 GST_RTSP_LOWER_TRANS_UNKNOWN);
438 g_mutex_lock (&media->lock);
439 res = media->protocols;
440 g_mutex_unlock (&media->lock);
446 * gst_rtsp_media_set_eos_shutdown:
447 * @media: a #GstRTSPMedia
448 * @eos_shutdown: the new value
450 * Set or unset if an EOS event will be sent to the pipeline for @media before
454 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
456 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
458 g_mutex_lock (&media->lock);
459 media->eos_shutdown = eos_shutdown;
460 g_mutex_unlock (&media->lock);
464 * gst_rtsp_media_is_eos_shutdown:
465 * @media: a #GstRTSPMedia
467 * Check if the pipeline for @media will send an EOS down the pipeline before
470 * Returns: %TRUE if the media will send EOS before unpreparing.
473 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
477 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
479 g_mutex_lock (&media->lock);
480 res = media->eos_shutdown;
481 g_mutex_unlock (&media->lock);
487 * gst_rtsp_media_set_buffer_size:
488 * @media: a #GstRTSPMedia
489 * @size: the new value
491 * Set the kernel UDP buffer size.
494 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
496 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
498 GST_LOG_OBJECT (media, "set buffer size %u", size);
500 g_mutex_lock (&media->lock);
501 media->buffer_size = size;
502 g_mutex_unlock (&media->lock);
506 * gst_rtsp_media_get_buffer_size:
507 * @media: a #GstRTSPMedia
509 * Get the kernel UDP buffer size.
511 * Returns: the kernel UDP buffer size.
514 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
518 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
520 g_mutex_unlock (&media->lock);
521 res = media->buffer_size;
522 g_mutex_unlock (&media->lock);
528 * gst_rtsp_media_set_auth:
529 * @media: a #GstRTSPMedia
530 * @auth: a #GstRTSPAuth
532 * configure @auth to be used as the authentication manager of @media.
535 gst_rtsp_media_set_auth (GstRTSPMedia * media, GstRTSPAuth * auth)
539 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
541 GST_LOG_OBJECT (media, "set auth %p", auth);
543 g_mutex_lock (&media->lock);
544 if ((old = media->auth) != auth)
545 media->auth = auth ? g_object_ref (auth) : NULL;
548 g_mutex_unlock (&media->lock);
551 g_object_unref (old);
555 * gst_rtsp_media_get_auth:
556 * @media: a #GstRTSPMedia
558 * Get the #GstRTSPAuth used as the authentication manager of @media.
560 * Returns: (transfer full): the #GstRTSPAuth of @media. g_object_unref() after
564 gst_rtsp_media_get_auth (GstRTSPMedia * media)
568 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
570 g_mutex_lock (&media->lock);
571 if ((result = media->auth))
572 g_object_ref (result);
573 g_mutex_unlock (&media->lock);
579 * gst_rtsp_media_set_address_pool:
580 * @media: a #GstRTSPMedia
581 * @pool: a #GstRTSPAddressPool
583 * configure @pool to be used as the address pool of @media.
586 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
587 GstRTSPAddressPool * pool)
589 GstRTSPAddressPool *old;
591 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
593 GST_LOG_OBJECT (media, "set address pool %p", pool);
595 g_mutex_lock (&media->lock);
596 if ((old = media->pool) != pool)
597 media->pool = pool ? g_object_ref (pool) : NULL;
600 g_ptr_array_foreach (media->streams, (GFunc) gst_rtsp_stream_set_address_pool,
602 g_mutex_unlock (&media->lock);
605 g_object_unref (old);
609 * gst_rtsp_media_get_address_pool:
610 * @media: a #GstRTSPMedia
612 * Get the #GstRTSPAddressPool used as the address pool of @media.
614 * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
618 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
620 GstRTSPAddressPool *result;
622 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
624 g_mutex_lock (&media->lock);
625 if ((result = media->pool))
626 g_object_ref (result);
627 g_mutex_unlock (&media->lock);
633 * gst_rtsp_media_collect_streams:
634 * @media: a #GstRTSPMedia
636 * Find all payloader elements, they should be named pay%d in the
637 * element of @media, and create #GstRTSPStreams for them.
639 * Collect all dynamic elements, named dynpay%d, and add them to
640 * the list of dynamic elements.
643 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
645 GstElement *element, *elem;
650 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
652 element = media->element;
655 for (i = 0; have_elem; i++) {
660 name = g_strdup_printf ("pay%d", i);
661 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
662 GST_INFO ("found stream %d with payloader %p", i, elem);
664 /* take the pad of the payloader */
665 pad = gst_element_get_static_pad (elem, "src");
666 /* create the stream */
667 gst_rtsp_media_create_stream (media, elem, pad);
668 g_object_unref (pad);
670 gst_object_unref (elem);
676 name = g_strdup_printf ("dynpay%d", i);
677 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
678 /* a stream that will dynamically create pads to provide RTP packets */
680 GST_INFO ("found dynamic element %d, %p", i, elem);
682 g_mutex_lock (&media->lock);
683 media->dynamic = g_list_prepend (media->dynamic, elem);
684 g_mutex_unlock (&media->lock);
693 * gst_rtsp_media_create_stream:
694 * @media: a #GstRTSPMedia
695 * @payloader: a #GstElement
696 * @srcpad: a source #GstPad
698 * Create a new stream in @media that provides RTP data on @srcpad.
699 * @srcpad should be a pad of an element inside @media->element.
701 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
705 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
708 GstRTSPStream *stream;
713 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
714 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
715 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
716 g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
718 g_mutex_lock (&media->lock);
719 idx = media->streams->len;
721 name = g_strdup_printf ("src_%u", idx);
722 srcpad = gst_ghost_pad_new (name, pad);
723 gst_pad_set_active (srcpad, TRUE);
724 gst_element_add_pad (media->element, srcpad);
727 stream = gst_rtsp_stream_new (idx, payloader, srcpad);
729 gst_rtsp_stream_set_address_pool (stream, media->pool);
731 g_ptr_array_add (media->streams, stream);
732 g_mutex_unlock (&media->lock);
734 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STREAM], 0, stream,
741 * gst_rtsp_media_n_streams:
742 * @media: a #GstRTSPMedia
744 * Get the number of streams in this media.
746 * Returns: The number of streams.
749 gst_rtsp_media_n_streams (GstRTSPMedia * media)
753 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
755 g_mutex_lock (&media->lock);
756 res = media->streams->len;
757 g_mutex_unlock (&media->lock);
763 * gst_rtsp_media_get_stream:
764 * @media: a #GstRTSPMedia
765 * @idx: the stream index
767 * Retrieve the stream with index @idx from @media.
769 * Returns: (transfer none): the #GstRTSPStream at index @idx or %NULL when a stream with
770 * that index did not exist.
773 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
777 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
779 g_mutex_lock (&media->lock);
780 if (idx < media->streams->len)
781 res = g_ptr_array_index (media->streams, idx);
784 g_mutex_unlock (&media->lock);
790 * gst_rtsp_media_get_range_string:
791 * @media: a #GstRTSPMedia
792 * @play: for the PLAY request
794 * Get the current range as a string.
796 * Returns: The range as a string, g_free() after usage.
799 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play)
802 GstRTSPTimeRange range;
804 g_mutex_lock (&media->lock);
806 range = media->range;
808 if (!play && media->n_active > 0) {
809 range.min.type = GST_RTSP_TIME_NOW;
810 range.min.seconds = -1;
812 g_mutex_unlock (&media->lock);
814 result = gst_rtsp_range_to_string (&range);
820 * gst_rtsp_media_seek:
821 * @media: a #GstRTSPMedia
822 * @range: a #GstRTSPTimeRange
824 * Seek the pipeline to @range.
826 * Returns: %TRUE on success.
829 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
833 GstClockTime start, stop;
834 GstSeekType start_type, stop_type;
836 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
837 g_return_val_if_fail (range != NULL, FALSE);
839 g_rec_mutex_lock (&media->state_lock);
840 if (!media->seekable)
843 /* depends on the current playing state of the pipeline. We might need to
844 * queue this until we get EOS. */
845 flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_KEY_UNIT;
847 start_type = stop_type = GST_SEEK_TYPE_NONE;
849 if (!gst_rtsp_range_get_times (range, &start, &stop))
852 GST_INFO ("got %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
853 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
854 GST_INFO ("current %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
855 GST_TIME_ARGS (media->range_start), GST_TIME_ARGS (media->range_stop));
857 if (media->range_start == start)
858 start = GST_CLOCK_TIME_NONE;
859 else if (start != GST_CLOCK_TIME_NONE)
860 start_type = GST_SEEK_TYPE_SET;
862 if (media->range_stop == stop)
863 stop = GST_CLOCK_TIME_NONE;
864 else if (stop != GST_CLOCK_TIME_NONE)
865 stop_type = GST_SEEK_TYPE_SET;
867 if (start != GST_CLOCK_TIME_NONE || stop != GST_CLOCK_TIME_NONE) {
868 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
869 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
871 res = gst_element_seek (media->pipeline, 1.0, GST_FORMAT_TIME,
872 flags, start_type, start, stop_type, stop);
874 /* and block for the seek to complete */
875 GST_INFO ("done seeking %d", res);
876 gst_element_get_state (media->pipeline, NULL, NULL, -1);
877 GST_INFO ("prerolled again");
879 collect_media_stats (media);
881 GST_INFO ("no seek needed");
884 g_rec_mutex_unlock (&media->state_lock);
891 g_rec_mutex_unlock (&media->state_lock);
892 GST_INFO ("pipeline is not seekable");
897 g_rec_mutex_unlock (&media->state_lock);
898 GST_WARNING ("seek unit %d not supported", range->unit);
904 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
906 g_mutex_lock (&media->lock);
907 /* never overwrite the error status */
908 if (media->status != GST_RTSP_MEDIA_STATUS_ERROR)
909 media->status = status;
910 GST_DEBUG ("setting new status to %d", status);
911 g_cond_broadcast (&media->cond);
912 g_mutex_unlock (&media->lock);
915 static GstRTSPMediaStatus
916 gst_rtsp_media_get_status (GstRTSPMedia * media)
918 GstRTSPMediaStatus result;
921 g_mutex_lock (&media->lock);
922 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
923 /* while we are preparing, wait */
924 while (media->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
925 GST_DEBUG ("waiting for status change");
926 if (!g_cond_wait_until (&media->cond, &media->lock, end_time)) {
927 GST_DEBUG ("timeout, assuming error status");
928 media->status = GST_RTSP_MEDIA_STATUS_ERROR;
931 /* could be success or error */
932 result = media->status;
933 GST_DEBUG ("got status %d", result);
934 g_mutex_unlock (&media->lock);
939 /* called with state-lock */
941 default_handle_message (GstRTSPMedia * media, GstMessage * message)
945 type = GST_MESSAGE_TYPE (message);
948 case GST_MESSAGE_STATE_CHANGED:
950 case GST_MESSAGE_BUFFERING:
954 gst_message_parse_buffering (message, &percent);
956 /* no state management needed for live pipelines */
960 if (percent == 100) {
961 /* a 100% message means buffering is done */
962 media->buffering = FALSE;
963 /* if the desired state is playing, go back */
964 if (media->target_state == GST_STATE_PLAYING) {
965 GST_INFO ("Buffering done, setting pipeline to PLAYING");
966 gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
968 GST_INFO ("Buffering done");
972 if (media->buffering == FALSE) {
973 if (media->target_state == GST_STATE_PLAYING) {
974 /* we were not buffering but PLAYING, PAUSE the pipeline. */
975 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
976 gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
978 GST_INFO ("Buffering ...");
981 media->buffering = TRUE;
985 case GST_MESSAGE_LATENCY:
987 gst_bin_recalculate_latency (GST_BIN_CAST (media->pipeline));
990 case GST_MESSAGE_ERROR:
995 gst_message_parse_error (message, &gerror, &debug);
996 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
997 g_error_free (gerror);
1000 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1003 case GST_MESSAGE_WARNING:
1008 gst_message_parse_warning (message, &gerror, &debug);
1009 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1010 g_error_free (gerror);
1014 case GST_MESSAGE_ELEMENT:
1016 case GST_MESSAGE_STREAM_STATUS:
1018 case GST_MESSAGE_ASYNC_DONE:
1019 if (!media->adding) {
1020 /* when we are dynamically adding pads, the addition of the udpsrc will
1021 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1022 * wait for the final ASYNC_DONE after everything prerolled */
1023 GST_INFO ("%p: got ASYNC_DONE", media);
1024 collect_media_stats (media);
1026 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1028 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1031 case GST_MESSAGE_EOS:
1032 GST_INFO ("%p: got EOS", media);
1034 if (media->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
1035 GST_DEBUG ("shutting down after EOS");
1036 finish_unprepare (media);
1037 g_object_unref (media);
1041 GST_INFO ("%p: got message type %s", media,
1042 gst_message_type_get_name (type));
1049 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1051 GstRTSPMediaClass *klass;
1054 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1056 g_rec_mutex_lock (&media->state_lock);
1057 if (klass->handle_message)
1058 ret = klass->handle_message (media, message);
1061 g_rec_mutex_unlock (&media->state_lock);
1067 watch_destroyed (GstRTSPMedia * media)
1069 GST_DEBUG_OBJECT (media, "source destroyed");
1070 gst_object_unref (media);
1073 /* called from streaming threads */
1075 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1077 GstRTSPStream *stream;
1079 /* FIXME, element is likely not a payloader, find the payloader here */
1080 stream = gst_rtsp_media_create_stream (media, element, pad);
1082 GST_INFO ("pad added %s:%s, stream %d", GST_DEBUG_PAD_NAME (pad),
1085 g_rec_mutex_lock (&media->state_lock);
1086 /* we will be adding elements below that will cause ASYNC_DONE to be
1087 * posted in the bus. We want to ignore those messages until the
1088 * pipeline really prerolled. */
1089 media->adding = TRUE;
1091 /* join the element in the PAUSED state because this callback is
1092 * called from the streaming thread and it is PAUSED */
1093 gst_rtsp_stream_join_bin (stream, GST_BIN (media->pipeline),
1094 media->rtpbin, GST_STATE_PAUSED);
1096 media->adding = FALSE;
1097 g_rec_mutex_unlock (&media->state_lock);
1101 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1103 GstElement *fakesink;
1105 g_mutex_lock (&media->lock);
1106 GST_INFO ("no more pads");
1107 if ((fakesink = media->fakesink)) {
1108 gst_object_ref (fakesink);
1109 media->fakesink = NULL;
1110 g_mutex_unlock (&media->lock);
1112 gst_bin_remove (GST_BIN (media->pipeline), fakesink);
1113 gst_element_set_state (fakesink, GST_STATE_NULL);
1114 gst_object_unref (fakesink);
1115 GST_INFO ("removed fakesink");
1120 * gst_rtsp_media_prepare:
1121 * @media: a #GstRTSPMedia
1123 * Prepare @media for streaming. This function will create the pipeline and
1124 * other objects to manage the streaming.
1126 * It will preroll the pipeline and collect vital information about the streams
1127 * such as the duration.
1129 * Returns: %TRUE on success.
1132 gst_rtsp_media_prepare (GstRTSPMedia * media)
1134 GstStateChangeReturn ret;
1135 GstRTSPMediaStatus status;
1137 GstRTSPMediaClass *klass;
1141 g_rec_mutex_lock (&media->state_lock);
1142 if (media->status == GST_RTSP_MEDIA_STATUS_PREPARED)
1145 if (media->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1148 if (media->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
1149 goto not_unprepared;
1151 if (!media->reusable && media->reused)
1154 media->rtpbin = gst_element_factory_make ("rtpbin", NULL);
1155 if (media->rtpbin == NULL)
1158 GST_INFO ("preparing media %p", media);
1160 /* reset some variables */
1161 media->is_live = FALSE;
1162 media->seekable = FALSE;
1163 media->buffering = FALSE;
1164 /* we're preparing now */
1165 media->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1167 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (media->pipeline));
1169 /* add the pipeline bus to our custom mainloop */
1170 media->source = gst_bus_create_watch (bus);
1171 gst_object_unref (bus);
1173 g_source_set_callback (media->source, (GSourceFunc) bus_message,
1174 gst_object_ref (media), (GDestroyNotify) watch_destroyed);
1176 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1177 media->id = g_source_attach (media->source, klass->context);
1179 /* add stuff to the bin */
1180 gst_bin_add (GST_BIN (media->pipeline), media->rtpbin);
1182 /* link streams we already have, other streams might appear when we have
1183 * dynamic elements */
1184 for (i = 0; i < media->streams->len; i++) {
1185 GstRTSPStream *stream;
1187 stream = g_ptr_array_index (media->streams, i);
1189 gst_rtsp_stream_join_bin (stream, GST_BIN (media->pipeline),
1190 media->rtpbin, GST_STATE_NULL);
1193 for (walk = media->dynamic; walk; walk = g_list_next (walk)) {
1194 GstElement *elem = walk->data;
1196 GST_INFO ("adding callbacks for dynamic element %p", elem);
1198 g_signal_connect (elem, "pad-added", (GCallback) pad_added_cb, media);
1199 g_signal_connect (elem, "no-more-pads", (GCallback) no_more_pads_cb, media);
1201 /* we add a fakesink here in order to make the state change async. We remove
1202 * the fakesink again in the no-more-pads callback. */
1203 media->fakesink = gst_element_factory_make ("fakesink", "fakesink");
1204 gst_bin_add (GST_BIN (media->pipeline), media->fakesink);
1207 GST_INFO ("setting pipeline to PAUSED for media %p", media);
1208 /* first go to PAUSED */
1209 ret = gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1210 media->target_state = GST_STATE_PAUSED;
1213 case GST_STATE_CHANGE_SUCCESS:
1214 GST_INFO ("SUCCESS state change for media %p", media);
1215 media->seekable = TRUE;
1217 case GST_STATE_CHANGE_ASYNC:
1218 GST_INFO ("ASYNC state change for media %p", media);
1219 media->seekable = TRUE;
1221 case GST_STATE_CHANGE_NO_PREROLL:
1222 /* we need to go to PLAYING */
1223 GST_INFO ("NO_PREROLL state change: live media %p", media);
1224 /* FIXME we disable seeking for live streams for now. We should perform a
1225 * seeking query in preroll instead */
1226 media->seekable = FALSE;
1227 media->is_live = TRUE;
1228 ret = gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1229 if (ret == GST_STATE_CHANGE_FAILURE)
1232 case GST_STATE_CHANGE_FAILURE:
1236 g_rec_mutex_unlock (&media->state_lock);
1238 /* now wait for all pads to be prerolled, FIXME, we should somehow be
1239 * able to do this async so that we don't block the server thread. */
1240 status = gst_rtsp_media_get_status (media);
1241 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1244 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
1246 GST_INFO ("object %p is prerolled", media);
1253 GST_LOG ("media %p was prepared", media);
1254 g_rec_mutex_unlock (&media->state_lock);
1260 GST_WARNING ("media %p was not unprepared", media);
1261 g_rec_mutex_unlock (&media->state_lock);
1266 g_rec_mutex_unlock (&media->state_lock);
1267 GST_WARNING ("can not reuse media %p", media);
1272 g_rec_mutex_unlock (&media->state_lock);
1273 GST_WARNING ("no rtpbin element");
1274 g_warning ("failed to create element 'rtpbin', check your installation");
1279 GST_WARNING ("failed to preroll pipeline");
1280 gst_rtsp_media_unprepare (media);
1281 g_rec_mutex_unlock (&media->state_lock);
1286 /* must be called with state-lock */
1288 finish_unprepare (GstRTSPMedia * media)
1292 GST_DEBUG ("shutting down");
1294 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1296 for (i = 0; i < media->streams->len; i++) {
1297 GstRTSPStream *stream;
1299 GST_INFO ("Removing elements of stream %d from pipeline", i);
1301 stream = g_ptr_array_index (media->streams, i);
1303 gst_rtsp_stream_leave_bin (stream, GST_BIN (media->pipeline),
1306 g_ptr_array_set_size (media->streams, 0);
1308 gst_bin_remove (GST_BIN (media->pipeline), media->rtpbin);
1309 media->rtpbin = NULL;
1311 gst_object_unref (media->pipeline);
1312 media->pipeline = NULL;
1314 media->reused = TRUE;
1315 media->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
1317 if (media->source) {
1318 g_source_destroy (media->source);
1319 g_source_unref (media->source);
1320 media->source = NULL;
1323 /* when the media is not reusable, this will effectively unref the media and
1325 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
1328 /* called with state-lock */
1330 default_unprepare (GstRTSPMedia * media)
1332 if (media->eos_shutdown) {
1333 GST_DEBUG ("sending EOS for shutdown");
1334 /* ref so that we don't disappear */
1335 g_object_ref (media);
1336 gst_element_send_event (media->pipeline, gst_event_new_eos ());
1337 /* we need to go to playing again for the EOS to propagate, normally in this
1338 * state, nothing is receiving data from us anymore so this is ok. */
1339 gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1340 media->status = GST_RTSP_MEDIA_STATUS_UNPREPARING;
1342 finish_unprepare (media);
1348 * gst_rtsp_media_unprepare:
1349 * @media: a #GstRTSPMedia
1351 * Unprepare @media. After this call, the media should be prepared again before
1352 * it can be used again. If the media is set to be non-reusable, a new instance
1355 * Returns: %TRUE on success.
1358 gst_rtsp_media_unprepare (GstRTSPMedia * media)
1362 g_rec_mutex_lock (&media->state_lock);
1363 if (media->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
1364 goto was_unprepared;
1366 GST_INFO ("unprepare media %p", media);
1367 media->target_state = GST_STATE_NULL;
1370 if (media->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
1371 GstRTSPMediaClass *klass;
1373 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1374 if (klass->unprepare)
1375 success = klass->unprepare (media);
1377 finish_unprepare (media);
1379 g_rec_mutex_unlock (&media->state_lock);
1385 g_rec_mutex_unlock (&media->state_lock);
1386 GST_INFO ("media %p was already unprepared", media);
1392 * gst_rtsp_media_set_state:
1393 * @media: a #GstRTSPMedia
1394 * @state: the target state of the media
1395 * @transports: a #GPtrArray of #GstRTSPStreamTransport pointers
1397 * Set the state of @media to @state and for the transports in @transports.
1399 * Returns: %TRUE on success.
1402 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
1403 GPtrArray * transports)
1406 gboolean add, remove, do_state;
1409 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1410 g_return_val_if_fail (transports != NULL, FALSE);
1412 g_rec_mutex_lock (&media->state_lock);
1414 /* NULL and READY are the same */
1415 if (state == GST_STATE_READY)
1416 state = GST_STATE_NULL;
1418 add = remove = FALSE;
1420 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
1424 case GST_STATE_NULL:
1425 case GST_STATE_PAUSED:
1426 /* we're going from PLAYING to PAUSED, READY or NULL, remove */
1427 if (media->target_state == GST_STATE_PLAYING)
1430 case GST_STATE_PLAYING:
1431 /* we're going to PLAYING, add */
1437 old_active = media->n_active;
1439 for (i = 0; i < transports->len; i++) {
1440 GstRTSPStreamTransport *trans;
1442 /* we need a non-NULL entry in the array */
1443 trans = g_ptr_array_index (transports, i);
1447 /* we need a transport */
1448 if (!trans->transport)
1452 if (gst_rtsp_stream_add_transport (trans->stream, trans))
1454 } else if (remove) {
1455 if (gst_rtsp_stream_remove_transport (trans->stream, trans))
1460 /* we just added the first media, do the playing state change */
1461 if (old_active == 0 && add)
1463 /* if we have no more active media, do the downward state changes */
1464 else if (media->n_active == 0)
1469 GST_INFO ("state %d active %d media %p do_state %d", state, media->n_active,
1472 if (media->target_state != state) {
1474 if (state == GST_STATE_NULL) {
1475 gst_rtsp_media_unprepare (media);
1477 GST_INFO ("state %s media %p", gst_element_state_get_name (state),
1479 media->target_state = state;
1480 gst_element_set_state (media->pipeline, state);
1483 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
1487 /* remember where we are */
1488 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
1489 old_active != media->n_active))
1490 collect_media_stats (media);
1492 g_rec_mutex_unlock (&media->state_lock);