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_source_destroy (media->source);
176 g_source_unref (media->source);
179 g_object_unref (media->auth);
181 g_object_unref (media->pool);
182 g_mutex_clear (&media->lock);
183 g_cond_clear (&media->cond);
184 g_rec_mutex_clear (&media->state_lock);
186 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
190 gst_rtsp_media_get_property (GObject * object, guint propid,
191 GValue * value, GParamSpec * pspec)
193 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
197 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
200 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
203 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
205 case PROP_EOS_SHUTDOWN:
206 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
208 case PROP_BUFFER_SIZE:
209 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
217 gst_rtsp_media_set_property (GObject * object, guint propid,
218 const GValue * value, GParamSpec * pspec)
220 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
224 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
227 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
230 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
232 case PROP_EOS_SHUTDOWN:
233 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
235 case PROP_BUFFER_SIZE:
236 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
239 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
244 do_loop (GstRTSPMediaClass * klass)
246 GST_INFO ("enter mainloop");
247 g_main_loop_run (klass->loop);
248 GST_INFO ("exit mainloop");
253 /* must be called with state lock */
255 collect_media_stats (GstRTSPMedia * media)
257 gint64 position, duration;
259 media->range.unit = GST_RTSP_RANGE_NPT;
261 GST_INFO ("collect media stats");
263 if (media->is_live) {
264 media->range.min.type = GST_RTSP_TIME_NOW;
265 media->range.min.seconds = -1;
266 media->range.max.type = GST_RTSP_TIME_END;
267 media->range.max.seconds = -1;
269 /* get the position */
270 if (!gst_element_query_position (media->pipeline, GST_FORMAT_TIME,
272 GST_INFO ("position query failed");
276 /* get the duration */
277 if (!gst_element_query_duration (media->pipeline, GST_FORMAT_TIME,
279 GST_INFO ("duration query failed");
283 GST_INFO ("stats: position %" GST_TIME_FORMAT ", duration %"
284 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (duration));
286 if (position == -1) {
287 media->range.min.type = GST_RTSP_TIME_NOW;
288 media->range.min.seconds = -1;
290 media->range.min.type = GST_RTSP_TIME_SECONDS;
291 media->range.min.seconds = ((gdouble) position) / GST_SECOND;
293 if (duration == -1) {
294 media->range.max.type = GST_RTSP_TIME_END;
295 media->range.max.seconds = -1;
297 media->range.max.type = GST_RTSP_TIME_SECONDS;
298 media->range.max.seconds = ((gdouble) duration) / GST_SECOND;
304 * gst_rtsp_media_new:
306 * Create a new #GstRTSPMedia instance. The #GstRTSPMedia object contains the
307 * element to produce RTP data for one or more related (audio/video/..)
310 * Returns: a new #GstRTSPMedia object.
313 gst_rtsp_media_new (void)
315 GstRTSPMedia *result;
317 result = g_object_new (GST_TYPE_RTSP_MEDIA, NULL);
323 * gst_rtsp_media_set_shared:
324 * @media: a #GstRTSPMedia
325 * @shared: the new value
327 * Set or unset if the pipeline for @media can be shared will multiple clients.
328 * When @shared is %TRUE, client requests for this media will share the media
332 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
334 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
336 g_mutex_lock (&media->lock);
337 media->shared = shared;
338 g_mutex_unlock (&media->lock);
342 * gst_rtsp_media_is_shared:
343 * @media: a #GstRTSPMedia
345 * Check if the pipeline for @media can be shared between multiple clients.
347 * Returns: %TRUE if the media can be shared between clients.
350 gst_rtsp_media_is_shared (GstRTSPMedia * media)
354 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
356 g_mutex_lock (&media->lock);
358 g_mutex_unlock (&media->lock);
364 * gst_rtsp_media_set_reusable:
365 * @media: a #GstRTSPMedia
366 * @reusable: the new value
368 * Set or unset if the pipeline for @media can be reused after the pipeline has
372 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
374 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
376 g_mutex_lock (&media->lock);
377 media->reusable = reusable;
378 g_mutex_unlock (&media->lock);
382 * gst_rtsp_media_is_reusable:
383 * @media: a #GstRTSPMedia
385 * Check if the pipeline for @media can be reused after an unprepare.
387 * Returns: %TRUE if the media can be reused
390 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
394 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
396 g_mutex_lock (&media->lock);
397 res = media->reusable;
398 g_mutex_unlock (&media->lock);
404 * gst_rtsp_media_set_protocols:
405 * @media: a #GstRTSPMedia
406 * @protocols: the new flags
408 * Configure the allowed lower transport for @media.
411 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
413 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
415 g_mutex_lock (&media->lock);
416 media->protocols = protocols;
417 g_mutex_unlock (&media->lock);
421 * gst_rtsp_media_get_protocols:
422 * @media: a #GstRTSPMedia
424 * Get the allowed protocols of @media.
426 * Returns: a #GstRTSPLowerTrans
429 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
431 GstRTSPLowerTrans res;
433 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
434 GST_RTSP_LOWER_TRANS_UNKNOWN);
436 g_mutex_lock (&media->lock);
437 res = media->protocols;
438 g_mutex_unlock (&media->lock);
444 * gst_rtsp_media_set_eos_shutdown:
445 * @media: a #GstRTSPMedia
446 * @eos_shutdown: the new value
448 * Set or unset if an EOS event will be sent to the pipeline for @media before
452 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
454 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
456 g_mutex_lock (&media->lock);
457 media->eos_shutdown = eos_shutdown;
458 g_mutex_unlock (&media->lock);
462 * gst_rtsp_media_is_eos_shutdown:
463 * @media: a #GstRTSPMedia
465 * Check if the pipeline for @media will send an EOS down the pipeline before
468 * Returns: %TRUE if the media will send EOS before unpreparing.
471 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
475 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
477 g_mutex_lock (&media->lock);
478 res = media->eos_shutdown;
479 g_mutex_unlock (&media->lock);
485 * gst_rtsp_media_set_buffer_size:
486 * @media: a #GstRTSPMedia
487 * @size: the new value
489 * Set the kernel UDP buffer size.
492 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
494 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
496 GST_LOG_OBJECT (media, "set buffer size %u", size);
498 g_mutex_lock (&media->lock);
499 media->buffer_size = size;
500 g_mutex_unlock (&media->lock);
504 * gst_rtsp_media_get_buffer_size:
505 * @media: a #GstRTSPMedia
507 * Get the kernel UDP buffer size.
509 * Returns: the kernel UDP buffer size.
512 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
516 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
518 g_mutex_unlock (&media->lock);
519 res = media->buffer_size;
520 g_mutex_unlock (&media->lock);
526 * gst_rtsp_media_set_auth:
527 * @media: a #GstRTSPMedia
528 * @auth: a #GstRTSPAuth
530 * configure @auth to be used as the authentication manager of @media.
533 gst_rtsp_media_set_auth (GstRTSPMedia * media, GstRTSPAuth * auth)
537 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
539 GST_LOG_OBJECT (media, "set auth %p", auth);
541 g_mutex_lock (&media->lock);
542 if ((old = media->auth) != auth)
543 media->auth = auth ? g_object_ref (auth) : NULL;
546 g_mutex_unlock (&media->lock);
549 g_object_unref (old);
553 * gst_rtsp_media_get_auth:
554 * @media: a #GstRTSPMedia
556 * Get the #GstRTSPAuth used as the authentication manager of @media.
558 * Returns: (transfer full): the #GstRTSPAuth of @media. g_object_unref() after
562 gst_rtsp_media_get_auth (GstRTSPMedia * media)
566 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
568 g_mutex_lock (&media->lock);
569 if ((result = media->auth))
570 g_object_ref (result);
571 g_mutex_unlock (&media->lock);
577 * gst_rtsp_media_set_address_pool:
578 * @media: a #GstRTSPMedia
579 * @pool: a #GstRTSPAddressPool
581 * configure @pool to be used as the address pool of @media.
584 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
585 GstRTSPAddressPool * pool)
587 GstRTSPAddressPool *old;
589 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
591 GST_LOG_OBJECT (media, "set address pool %p", pool);
593 g_mutex_lock (&media->lock);
594 if ((old = media->pool) != pool)
595 media->pool = pool ? g_object_ref (pool) : NULL;
598 g_mutex_unlock (&media->lock);
601 g_object_unref (old);
605 * gst_rtsp_media_get_address_pool:
606 * @media: a #GstRTSPMedia
608 * Get the #GstRTSPAddressPool used as the address pool of @media.
610 * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
614 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
616 GstRTSPAddressPool *result;
618 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
620 g_mutex_lock (&media->lock);
621 if ((result = media->pool))
622 g_object_ref (result);
623 g_mutex_unlock (&media->lock);
629 * gst_rtsp_media_collect_streams:
630 * @media: a #GstRTSPMedia
632 * Find all payloader elements, they should be named pay%d in the
633 * element of @media, and create #GstRTSPStreams for them.
635 * Collect all dynamic elements, named dynpay%d, and add them to
636 * the list of dynamic elements.
639 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
641 GstElement *element, *elem;
646 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
648 element = media->element;
651 for (i = 0; have_elem; i++) {
656 name = g_strdup_printf ("pay%d", i);
657 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
658 GST_INFO ("found stream %d with payloader %p", i, elem);
660 /* take the pad of the payloader */
661 pad = gst_element_get_static_pad (elem, "src");
662 /* create the stream */
663 gst_rtsp_media_create_stream (media, elem, pad);
664 g_object_unref (pad);
666 gst_object_unref (elem);
672 name = g_strdup_printf ("dynpay%d", i);
673 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
674 /* a stream that will dynamically create pads to provide RTP packets */
676 GST_INFO ("found dynamic element %d, %p", i, elem);
678 g_mutex_lock (&media->lock);
679 media->dynamic = g_list_prepend (media->dynamic, elem);
680 g_mutex_unlock (&media->lock);
689 * gst_rtsp_media_create_stream:
690 * @media: a #GstRTSPMedia
691 * @payloader: a #GstElement
692 * @srcpad: a source #GstPad
694 * Create a new stream in @media that provides RTP data on @srcpad.
695 * @srcpad should be a pad of an element inside @media->element.
697 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
701 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
704 GstRTSPStream *stream;
709 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
710 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
711 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
712 g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
714 g_mutex_lock (&media->lock);
715 idx = media->streams->len;
717 name = g_strdup_printf ("src_%u", idx);
718 srcpad = gst_ghost_pad_new (name, pad);
719 gst_pad_set_active (srcpad, TRUE);
720 gst_element_add_pad (media->element, srcpad);
723 stream = gst_rtsp_stream_new (idx, payloader, srcpad);
725 gst_rtsp_stream_set_address_pool (stream, media->pool);
727 g_ptr_array_add (media->streams, stream);
728 g_mutex_unlock (&media->lock);
730 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STREAM], 0, stream,
737 * gst_rtsp_media_n_streams:
738 * @media: a #GstRTSPMedia
740 * Get the number of streams in this media.
742 * Returns: The number of streams.
745 gst_rtsp_media_n_streams (GstRTSPMedia * media)
749 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
751 g_mutex_lock (&media->lock);
752 res = media->streams->len;
753 g_mutex_unlock (&media->lock);
759 * gst_rtsp_media_get_stream:
760 * @media: a #GstRTSPMedia
761 * @idx: the stream index
763 * Retrieve the stream with index @idx from @media.
765 * Returns: (transfer none): the #GstRTSPStream at index @idx or %NULL when a stream with
766 * that index did not exist.
769 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
773 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
775 g_mutex_lock (&media->lock);
776 if (idx < media->streams->len)
777 res = g_ptr_array_index (media->streams, idx);
780 g_mutex_unlock (&media->lock);
786 * gst_rtsp_media_get_range_string:
787 * @media: a #GstRTSPMedia
788 * @play: for the PLAY request
790 * Get the current range as a string.
792 * Returns: The range as a string, g_free() after usage.
795 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play)
798 GstRTSPTimeRange range;
800 g_mutex_lock (&media->lock);
802 range = media->range;
804 if (!play && media->n_active > 0) {
805 range.min.type = GST_RTSP_TIME_NOW;
806 range.min.seconds = -1;
808 g_mutex_unlock (&media->lock);
810 result = gst_rtsp_range_to_string (&range);
816 * gst_rtsp_media_seek:
817 * @media: a #GstRTSPMedia
818 * @range: a #GstRTSPTimeRange
820 * Seek the pipeline to @range.
822 * Returns: %TRUE on success.
825 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
830 GstSeekType start_type, stop_type;
832 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
833 g_return_val_if_fail (range != NULL, FALSE);
835 g_rec_mutex_lock (&media->state_lock);
836 if (!media->seekable)
839 if (range->unit != GST_RTSP_RANGE_NPT)
842 /* depends on the current playing state of the pipeline. We might need to
843 * queue this until we get EOS. */
844 flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_KEY_UNIT;
846 start_type = stop_type = GST_SEEK_TYPE_NONE;
848 switch (range->min.type) {
849 case GST_RTSP_TIME_NOW:
852 case GST_RTSP_TIME_SECONDS:
853 /* only seek when something changed */
854 if (media->range.min.seconds == range->min.seconds) {
857 start = range->min.seconds * GST_SECOND;
858 start_type = GST_SEEK_TYPE_SET;
861 case GST_RTSP_TIME_END:
865 switch (range->max.type) {
866 case GST_RTSP_TIME_SECONDS:
867 /* only seek when something changed */
868 if (media->range.max.seconds == range->max.seconds) {
871 stop = range->max.seconds * GST_SECOND;
872 stop_type = GST_SEEK_TYPE_SET;
875 case GST_RTSP_TIME_END:
877 stop_type = GST_SEEK_TYPE_SET;
879 case GST_RTSP_TIME_NOW:
884 if (start != -1 || stop != -1) {
885 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
886 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
888 res = gst_element_seek (media->pipeline, 1.0, GST_FORMAT_TIME,
889 flags, start_type, start, stop_type, stop);
891 /* and block for the seek to complete */
892 GST_INFO ("done seeking %d", res);
893 gst_element_get_state (media->pipeline, NULL, NULL, -1);
894 GST_INFO ("prerolled again");
896 collect_media_stats (media);
898 GST_INFO ("no seek needed");
901 g_rec_mutex_unlock (&media->state_lock);
908 g_rec_mutex_unlock (&media->state_lock);
909 GST_INFO ("pipeline is not seekable");
914 g_rec_mutex_unlock (&media->state_lock);
915 GST_WARNING ("seek unit %d not supported", range->unit);
920 g_rec_mutex_unlock (&media->state_lock);
921 GST_WARNING ("weird range type %d not supported", range->min.type);
927 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
929 g_mutex_lock (&media->lock);
930 /* never overwrite the error status */
931 if (media->status != GST_RTSP_MEDIA_STATUS_ERROR)
932 media->status = status;
933 GST_DEBUG ("setting new status to %d", status);
934 g_cond_broadcast (&media->cond);
935 g_mutex_unlock (&media->lock);
938 static GstRTSPMediaStatus
939 gst_rtsp_media_get_status (GstRTSPMedia * media)
941 GstRTSPMediaStatus result;
944 g_mutex_lock (&media->lock);
945 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
946 /* while we are preparing, wait */
947 while (media->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
948 GST_DEBUG ("waiting for status change");
949 if (!g_cond_wait_until (&media->cond, &media->lock, end_time)) {
950 GST_DEBUG ("timeout, assuming error status");
951 media->status = GST_RTSP_MEDIA_STATUS_ERROR;
954 /* could be success or error */
955 result = media->status;
956 GST_DEBUG ("got status %d", result);
957 g_mutex_unlock (&media->lock);
962 /* called with state-lock */
964 default_handle_message (GstRTSPMedia * media, GstMessage * message)
968 type = GST_MESSAGE_TYPE (message);
971 case GST_MESSAGE_STATE_CHANGED:
973 case GST_MESSAGE_BUFFERING:
977 gst_message_parse_buffering (message, &percent);
979 /* no state management needed for live pipelines */
983 if (percent == 100) {
984 /* a 100% message means buffering is done */
985 media->buffering = FALSE;
986 /* if the desired state is playing, go back */
987 if (media->target_state == GST_STATE_PLAYING) {
988 GST_INFO ("Buffering done, setting pipeline to PLAYING");
989 gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
991 GST_INFO ("Buffering done");
995 if (media->buffering == FALSE) {
996 if (media->target_state == GST_STATE_PLAYING) {
997 /* we were not buffering but PLAYING, PAUSE the pipeline. */
998 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
999 gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1001 GST_INFO ("Buffering ...");
1004 media->buffering = TRUE;
1008 case GST_MESSAGE_LATENCY:
1010 gst_bin_recalculate_latency (GST_BIN_CAST (media->pipeline));
1013 case GST_MESSAGE_ERROR:
1018 gst_message_parse_error (message, &gerror, &debug);
1019 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1020 g_error_free (gerror);
1023 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1026 case GST_MESSAGE_WARNING:
1031 gst_message_parse_warning (message, &gerror, &debug);
1032 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1033 g_error_free (gerror);
1037 case GST_MESSAGE_ELEMENT:
1039 case GST_MESSAGE_STREAM_STATUS:
1041 case GST_MESSAGE_ASYNC_DONE:
1042 if (!media->adding) {
1043 /* when we are dynamically adding pads, the addition of the udpsrc will
1044 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1045 * wait for the final ASYNC_DONE after everything prerolled */
1046 GST_INFO ("%p: got ASYNC_DONE", media);
1047 collect_media_stats (media);
1049 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1051 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1054 case GST_MESSAGE_EOS:
1055 GST_INFO ("%p: got EOS", media);
1057 if (media->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
1058 GST_DEBUG ("shutting down after EOS");
1059 finish_unprepare (media);
1060 g_object_unref (media);
1064 GST_INFO ("%p: got message type %s", media,
1065 gst_message_type_get_name (type));
1072 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1074 GstRTSPMediaClass *klass;
1077 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1079 g_rec_mutex_lock (&media->state_lock);
1080 if (klass->handle_message)
1081 ret = klass->handle_message (media, message);
1084 g_rec_mutex_unlock (&media->state_lock);
1089 /* called from streaming threads */
1091 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1093 GstRTSPStream *stream;
1095 /* FIXME, element is likely not a payloader, find the payloader here */
1096 stream = gst_rtsp_media_create_stream (media, element, pad);
1098 GST_INFO ("pad added %s:%s, stream %d", GST_DEBUG_PAD_NAME (pad),
1101 g_rec_mutex_lock (&media->state_lock);
1102 /* we will be adding elements below that will cause ASYNC_DONE to be
1103 * posted in the bus. We want to ignore those messages until the
1104 * pipeline really prerolled. */
1105 media->adding = TRUE;
1107 /* join the element in the PAUSED state because this callback is
1108 * called from the streaming thread and it is PAUSED */
1109 gst_rtsp_stream_join_bin (stream, GST_BIN (media->pipeline),
1110 media->rtpbin, GST_STATE_PAUSED);
1112 media->adding = FALSE;
1113 g_rec_mutex_unlock (&media->state_lock);
1117 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1119 GstElement *fakesink;
1121 g_mutex_lock (&media->lock);
1122 GST_INFO ("no more pads");
1123 if ((fakesink = media->fakesink)) {
1124 gst_object_ref (fakesink);
1125 media->fakesink = NULL;
1126 g_mutex_unlock (&media->lock);
1128 gst_bin_remove (GST_BIN (media->pipeline), fakesink);
1129 gst_element_set_state (fakesink, GST_STATE_NULL);
1130 gst_object_unref (fakesink);
1131 GST_INFO ("removed fakesink");
1136 * gst_rtsp_media_prepare:
1137 * @media: a #GstRTSPMedia
1139 * Prepare @media for streaming. This function will create the pipeline and
1140 * other objects to manage the streaming.
1142 * It will preroll the pipeline and collect vital information about the streams
1143 * such as the duration.
1145 * Returns: %TRUE on success.
1148 gst_rtsp_media_prepare (GstRTSPMedia * media)
1150 GstStateChangeReturn ret;
1151 GstRTSPMediaStatus status;
1153 GstRTSPMediaClass *klass;
1157 g_rec_mutex_lock (&media->state_lock);
1158 if (media->status == GST_RTSP_MEDIA_STATUS_PREPARED)
1161 if (media->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1164 if (media->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
1165 goto not_unprepared;
1167 if (!media->reusable && media->reused)
1170 media->rtpbin = gst_element_factory_make ("rtpbin", NULL);
1171 if (media->rtpbin == NULL)
1174 GST_INFO ("preparing media %p", media);
1176 /* reset some variables */
1177 media->is_live = FALSE;
1178 media->seekable = FALSE;
1179 media->buffering = FALSE;
1180 /* we're preparing now */
1181 media->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1183 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (media->pipeline));
1185 /* add the pipeline bus to our custom mainloop */
1186 media->source = gst_bus_create_watch (bus);
1187 gst_object_unref (bus);
1189 g_source_set_callback (media->source, (GSourceFunc) bus_message, media, NULL);
1191 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1192 media->id = g_source_attach (media->source, klass->context);
1194 /* add stuff to the bin */
1195 gst_bin_add (GST_BIN (media->pipeline), media->rtpbin);
1197 /* link streams we already have, other streams might appear when we have
1198 * dynamic elements */
1199 for (i = 0; i < media->streams->len; i++) {
1200 GstRTSPStream *stream;
1202 stream = g_ptr_array_index (media->streams, i);
1204 gst_rtsp_stream_join_bin (stream, GST_BIN (media->pipeline),
1205 media->rtpbin, GST_STATE_NULL);
1208 for (walk = media->dynamic; walk; walk = g_list_next (walk)) {
1209 GstElement *elem = walk->data;
1211 GST_INFO ("adding callbacks for dynamic element %p", elem);
1213 g_signal_connect (elem, "pad-added", (GCallback) pad_added_cb, media);
1214 g_signal_connect (elem, "no-more-pads", (GCallback) no_more_pads_cb, media);
1216 /* we add a fakesink here in order to make the state change async. We remove
1217 * the fakesink again in the no-more-pads callback. */
1218 media->fakesink = gst_element_factory_make ("fakesink", "fakesink");
1219 gst_bin_add (GST_BIN (media->pipeline), media->fakesink);
1222 GST_INFO ("setting pipeline to PAUSED for media %p", media);
1223 /* first go to PAUSED */
1224 ret = gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1225 media->target_state = GST_STATE_PAUSED;
1228 case GST_STATE_CHANGE_SUCCESS:
1229 GST_INFO ("SUCCESS state change for media %p", media);
1230 media->seekable = TRUE;
1232 case GST_STATE_CHANGE_ASYNC:
1233 GST_INFO ("ASYNC state change for media %p", media);
1234 media->seekable = TRUE;
1236 case GST_STATE_CHANGE_NO_PREROLL:
1237 /* we need to go to PLAYING */
1238 GST_INFO ("NO_PREROLL state change: live media %p", media);
1239 /* FIXME we disable seeking for live streams for now. We should perform a
1240 * seeking query in preroll instead */
1241 media->seekable = FALSE;
1242 media->is_live = TRUE;
1243 ret = gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1244 if (ret == GST_STATE_CHANGE_FAILURE)
1247 case GST_STATE_CHANGE_FAILURE:
1251 g_rec_mutex_unlock (&media->state_lock);
1253 /* now wait for all pads to be prerolled, FIXME, we should somehow be
1254 * able to do this async so that we don't block the server thread. */
1255 status = gst_rtsp_media_get_status (media);
1256 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1259 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
1261 GST_INFO ("object %p is prerolled", media);
1268 GST_LOG ("media %p was prepared", media);
1269 g_rec_mutex_unlock (&media->state_lock);
1275 GST_WARNING ("media %p was not unprepared", media);
1276 g_rec_mutex_unlock (&media->state_lock);
1281 g_rec_mutex_unlock (&media->state_lock);
1282 GST_WARNING ("can not reuse media %p", media);
1287 g_rec_mutex_unlock (&media->state_lock);
1288 GST_WARNING ("no rtpbin element");
1289 g_warning ("failed to create element 'rtpbin', check your installation");
1294 GST_WARNING ("failed to preroll pipeline");
1295 gst_rtsp_media_unprepare (media);
1296 g_rec_mutex_unlock (&media->state_lock);
1301 /* must be called with state-lock */
1303 finish_unprepare (GstRTSPMedia * media)
1307 GST_DEBUG ("shutting down");
1309 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1311 for (i = 0; i < media->streams->len; i++) {
1312 GstRTSPStream *stream;
1314 GST_INFO ("Removing elements of stream %d from pipeline", i);
1316 stream = g_ptr_array_index (media->streams, i);
1318 gst_rtsp_stream_leave_bin (stream, GST_BIN (media->pipeline),
1321 g_ptr_array_set_size (media->streams, 0);
1323 gst_bin_remove (GST_BIN (media->pipeline), media->rtpbin);
1324 media->rtpbin = NULL;
1326 gst_object_unref (media->pipeline);
1327 media->pipeline = NULL;
1329 media->reused = TRUE;
1330 media->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
1332 /* when the media is not reusable, this will effectively unref the media and
1334 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
1337 /* called with state-lock */
1339 default_unprepare (GstRTSPMedia * media)
1341 if (media->eos_shutdown) {
1342 GST_DEBUG ("sending EOS for shutdown");
1343 /* ref so that we don't disappear */
1344 g_object_ref (media);
1345 gst_element_send_event (media->pipeline, gst_event_new_eos ());
1346 /* we need to go to playing again for the EOS to propagate, normally in this
1347 * state, nothing is receiving data from us anymore so this is ok. */
1348 gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1349 media->status = GST_RTSP_MEDIA_STATUS_UNPREPARING;
1351 finish_unprepare (media);
1357 * gst_rtsp_media_unprepare:
1358 * @media: a #GstRTSPMedia
1360 * Unprepare @media. After this call, the media should be prepared again before
1361 * it can be used again. If the media is set to be non-reusable, a new instance
1364 * Returns: %TRUE on success.
1367 gst_rtsp_media_unprepare (GstRTSPMedia * media)
1371 g_rec_mutex_lock (&media->state_lock);
1372 if (media->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
1373 goto was_unprepared;
1375 GST_INFO ("unprepare media %p", media);
1376 media->target_state = GST_STATE_NULL;
1379 if (media->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
1380 GstRTSPMediaClass *klass;
1382 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1383 if (klass->unprepare)
1384 success = klass->unprepare (media);
1386 finish_unprepare (media);
1388 g_rec_mutex_unlock (&media->state_lock);
1394 g_rec_mutex_unlock (&media->state_lock);
1395 GST_INFO ("media %p was already unprepared", media);
1401 * gst_rtsp_media_set_state:
1402 * @media: a #GstRTSPMedia
1403 * @state: the target state of the media
1404 * @transports: a #GPtrArray of #GstRTSPStreamTransport pointers
1406 * Set the state of @media to @state and for the transports in @transports.
1408 * Returns: %TRUE on success.
1411 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
1412 GPtrArray * transports)
1415 gboolean add, remove, do_state;
1418 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1419 g_return_val_if_fail (transports != NULL, FALSE);
1421 g_rec_mutex_lock (&media->state_lock);
1423 /* NULL and READY are the same */
1424 if (state == GST_STATE_READY)
1425 state = GST_STATE_NULL;
1427 add = remove = FALSE;
1429 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
1433 case GST_STATE_NULL:
1434 case GST_STATE_PAUSED:
1435 /* we're going from PLAYING to PAUSED, READY or NULL, remove */
1436 if (media->target_state == GST_STATE_PLAYING)
1439 case GST_STATE_PLAYING:
1440 /* we're going to PLAYING, add */
1446 old_active = media->n_active;
1448 for (i = 0; i < transports->len; i++) {
1449 GstRTSPStreamTransport *trans;
1451 /* we need a non-NULL entry in the array */
1452 trans = g_ptr_array_index (transports, i);
1456 /* we need a transport */
1457 if (!trans->transport)
1461 if (gst_rtsp_stream_add_transport (trans->stream, trans))
1463 } else if (remove) {
1464 if (gst_rtsp_stream_remove_transport (trans->stream, trans))
1469 /* we just added the first media, do the playing state change */
1470 if (old_active == 0 && add)
1472 /* if we have no more active media, do the downward state changes */
1473 else if (media->n_active == 0)
1478 GST_INFO ("state %d active %d media %p do_state %d", state, media->n_active,
1481 if (media->target_state != state) {
1483 if (state == GST_STATE_NULL) {
1484 gst_rtsp_media_unprepare (media);
1486 GST_INFO ("state %s media %p", gst_element_state_get_name (state),
1488 media->target_state = state;
1489 gst_element_set_state (media->pipeline, state);
1492 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
1496 /* remember where we are */
1497 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
1498 old_active != media->n_active))
1499 collect_media_stats (media);
1501 g_rec_mutex_unlock (&media->state_lock);