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
36 /* define to dump received RTCP packets */
59 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
60 #define GST_CAT_DEFAULT rtsp_media_debug
62 static void gst_rtsp_media_get_property (GObject * object, guint propid,
63 GValue * value, GParamSpec * pspec);
64 static void gst_rtsp_media_set_property (GObject * object, guint propid,
65 const GValue * value, GParamSpec * pspec);
66 static void gst_rtsp_media_finalize (GObject * obj);
68 static gpointer do_loop (GstRTSPMediaClass * klass);
69 static gboolean default_handle_message (GstRTSPMedia * media,
70 GstMessage * message);
71 static void finish_unprepare (GstRTSPMedia * media);
72 static gboolean default_unprepare (GstRTSPMedia * media);
74 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
76 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
79 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
81 GObjectClass *gobject_class;
83 gobject_class = G_OBJECT_CLASS (klass);
85 gobject_class->get_property = gst_rtsp_media_get_property;
86 gobject_class->set_property = gst_rtsp_media_set_property;
87 gobject_class->finalize = gst_rtsp_media_finalize;
89 g_object_class_install_property (gobject_class, PROP_SHARED,
90 g_param_spec_boolean ("shared", "Shared",
91 "If this media pipeline can be shared", DEFAULT_SHARED,
92 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
94 g_object_class_install_property (gobject_class, PROP_REUSABLE,
95 g_param_spec_boolean ("reusable", "Reusable",
96 "If this media pipeline can be reused after an unprepare",
97 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
99 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
100 g_param_spec_flags ("protocols", "Protocols",
101 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
102 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
104 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
105 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
106 "Send an EOS event to the pipeline before unpreparing",
107 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
109 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
110 g_param_spec_uint ("buffer-size", "Buffer Size",
111 "The kernel UDP buffer size to use", 0, G_MAXUINT,
112 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
114 g_object_class_install_property (gobject_class, PROP_MTU,
115 g_param_spec_uint ("mtu", "MTU",
116 "The MTU for the payloaders (0 = default)",
117 0, G_MAXUINT, DEFAULT_MTU,
118 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
120 gst_rtsp_media_signals[SIGNAL_PREPARED] =
121 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
122 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
123 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
125 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
126 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
127 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
128 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
130 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
131 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
132 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
133 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 0, G_TYPE_INT);
135 klass->context = g_main_context_new ();
136 klass->loop = g_main_loop_new (klass->context, TRUE);
138 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
140 klass->thread = g_thread_new ("Bus Thread", (GThreadFunc) do_loop, klass);
142 klass->handle_message = default_handle_message;
143 klass->unprepare = default_unprepare;
147 gst_rtsp_media_init (GstRTSPMedia * media)
149 media->streams = g_ptr_array_new_with_free_func (g_object_unref);
150 g_mutex_init (&media->lock);
151 g_cond_init (&media->cond);
152 g_rec_mutex_init (&media->state_lock);
154 media->shared = DEFAULT_SHARED;
155 media->reusable = DEFAULT_REUSABLE;
156 media->protocols = DEFAULT_PROTOCOLS;
157 media->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
158 media->buffer_size = DEFAULT_BUFFER_SIZE;
162 gst_rtsp_media_finalize (GObject * obj)
166 media = GST_RTSP_MEDIA (obj);
168 GST_INFO ("finalize media %p", media);
170 gst_rtsp_media_unprepare (media);
172 g_ptr_array_unref (media->streams);
174 g_list_free_full (media->dynamic, gst_object_unref);
177 g_source_destroy (media->source);
178 g_source_unref (media->source);
181 g_object_unref (media->auth);
183 g_object_unref (media->pool);
184 g_mutex_clear (&media->lock);
185 g_cond_clear (&media->cond);
186 g_rec_mutex_clear (&media->state_lock);
188 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
192 gst_rtsp_media_get_property (GObject * object, guint propid,
193 GValue * value, GParamSpec * pspec)
195 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
199 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
202 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
205 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
207 case PROP_EOS_SHUTDOWN:
208 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
210 case PROP_BUFFER_SIZE:
211 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
214 g_value_set_uint (value, gst_rtsp_media_get_mtu (media));
217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
222 gst_rtsp_media_set_property (GObject * object, guint propid,
223 const GValue * value, GParamSpec * pspec)
225 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
229 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
232 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
235 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
237 case PROP_EOS_SHUTDOWN:
238 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
240 case PROP_BUFFER_SIZE:
241 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
244 gst_rtsp_media_set_mtu (media, g_value_get_uint (value));
247 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
252 do_loop (GstRTSPMediaClass * klass)
254 GST_INFO ("enter mainloop");
255 g_main_loop_run (klass->loop);
256 GST_INFO ("exit mainloop");
261 /* must be called with state lock */
263 collect_media_stats (GstRTSPMedia * media)
265 gint64 position, duration;
267 media->range.unit = GST_RTSP_RANGE_NPT;
269 GST_INFO ("collect media stats");
271 if (media->is_live) {
272 media->range.min.type = GST_RTSP_TIME_NOW;
273 media->range.min.seconds = -1;
274 media->range.max.type = GST_RTSP_TIME_END;
275 media->range.max.seconds = -1;
277 /* get the position */
278 if (!gst_element_query_position (media->pipeline, GST_FORMAT_TIME,
280 GST_INFO ("position query failed");
284 /* get the duration */
285 if (!gst_element_query_duration (media->pipeline, GST_FORMAT_TIME,
287 GST_INFO ("duration query failed");
291 GST_INFO ("stats: position %" GST_TIME_FORMAT ", duration %"
292 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (duration));
294 if (position == -1) {
295 media->range.min.type = GST_RTSP_TIME_NOW;
296 media->range.min.seconds = -1;
298 media->range.min.type = GST_RTSP_TIME_SECONDS;
299 media->range.min.seconds = ((gdouble) position) / GST_SECOND;
301 if (duration == -1) {
302 media->range.max.type = GST_RTSP_TIME_END;
303 media->range.max.seconds = -1;
305 media->range.max.type = GST_RTSP_TIME_SECONDS;
306 media->range.max.seconds = ((gdouble) duration) / GST_SECOND;
312 * gst_rtsp_media_new:
314 * Create a new #GstRTSPMedia instance. The #GstRTSPMedia object contains the
315 * element to produce RTP data for one or more related (audio/video/..)
318 * Returns: a new #GstRTSPMedia object.
321 gst_rtsp_media_new (void)
323 GstRTSPMedia *result;
325 result = g_object_new (GST_TYPE_RTSP_MEDIA, NULL);
331 * gst_rtsp_media_set_shared:
332 * @media: a #GstRTSPMedia
333 * @shared: the new value
335 * Set or unset if the pipeline for @media can be shared will multiple clients.
336 * When @shared is %TRUE, client requests for this media will share the media
340 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
342 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
344 g_mutex_lock (&media->lock);
345 media->shared = shared;
346 g_mutex_unlock (&media->lock);
350 * gst_rtsp_media_is_shared:
351 * @media: a #GstRTSPMedia
353 * Check if the pipeline for @media can be shared between multiple clients.
355 * Returns: %TRUE if the media can be shared between clients.
358 gst_rtsp_media_is_shared (GstRTSPMedia * media)
362 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
364 g_mutex_lock (&media->lock);
366 g_mutex_unlock (&media->lock);
372 * gst_rtsp_media_set_reusable:
373 * @media: a #GstRTSPMedia
374 * @reusable: the new value
376 * Set or unset if the pipeline for @media can be reused after the pipeline has
380 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
382 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
384 g_mutex_lock (&media->lock);
385 media->reusable = reusable;
386 g_mutex_unlock (&media->lock);
390 * gst_rtsp_media_is_reusable:
391 * @media: a #GstRTSPMedia
393 * Check if the pipeline for @media can be reused after an unprepare.
395 * Returns: %TRUE if the media can be reused
398 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
402 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
404 g_mutex_lock (&media->lock);
405 res = media->reusable;
406 g_mutex_unlock (&media->lock);
412 * gst_rtsp_media_set_protocols:
413 * @media: a #GstRTSPMedia
414 * @protocols: the new flags
416 * Configure the allowed lower transport for @media.
419 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
421 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
423 g_mutex_lock (&media->lock);
424 media->protocols = protocols;
425 g_mutex_unlock (&media->lock);
429 * gst_rtsp_media_get_protocols:
430 * @media: a #GstRTSPMedia
432 * Get the allowed protocols of @media.
434 * Returns: a #GstRTSPLowerTrans
437 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
439 GstRTSPLowerTrans res;
441 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
442 GST_RTSP_LOWER_TRANS_UNKNOWN);
444 g_mutex_lock (&media->lock);
445 res = media->protocols;
446 g_mutex_unlock (&media->lock);
452 * gst_rtsp_media_set_eos_shutdown:
453 * @media: a #GstRTSPMedia
454 * @eos_shutdown: the new value
456 * Set or unset if an EOS event will be sent to the pipeline for @media before
460 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
462 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
464 g_mutex_lock (&media->lock);
465 media->eos_shutdown = eos_shutdown;
466 g_mutex_unlock (&media->lock);
470 * gst_rtsp_media_is_eos_shutdown:
471 * @media: a #GstRTSPMedia
473 * Check if the pipeline for @media will send an EOS down the pipeline before
476 * Returns: %TRUE if the media will send EOS before unpreparing.
479 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
483 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
485 g_mutex_lock (&media->lock);
486 res = media->eos_shutdown;
487 g_mutex_unlock (&media->lock);
493 * gst_rtsp_media_set_buffer_size:
494 * @media: a #GstRTSPMedia
495 * @size: the new value
497 * Set the kernel UDP buffer size.
500 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
502 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
504 g_mutex_lock (&media->lock);
505 media->buffer_size = size;
506 g_mutex_unlock (&media->lock);
510 * gst_rtsp_media_get_buffer_size:
511 * @media: a #GstRTSPMedia
513 * Get the kernel UDP buffer size.
515 * Returns: the kernel UDP buffer size.
518 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
522 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
524 g_mutex_unlock (&media->lock);
525 res = media->buffer_size;
526 g_mutex_unlock (&media->lock);
532 * gst_rtsp_media_set_auth:
533 * @media: a #GstRTSPMedia
534 * @auth: a #GstRTSPAuth
536 * configure @auth to be used as the authentication manager of @media.
539 gst_rtsp_media_set_auth (GstRTSPMedia * media, GstRTSPAuth * auth)
543 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
545 g_mutex_lock (&media->lock);
546 if ((old = media->auth) != auth)
547 media->auth = auth ? g_object_ref (auth) : NULL;
550 g_mutex_unlock (&media->lock);
553 g_object_unref (old);
557 * gst_rtsp_media_get_auth:
558 * @media: a #GstRTSPMedia
560 * Get the #GstRTSPAuth used as the authentication manager of @media.
562 * Returns: (transfer full): the #GstRTSPAuth of @media. g_object_unref() after
566 gst_rtsp_media_get_auth (GstRTSPMedia * media)
570 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
572 g_mutex_lock (&media->lock);
573 if ((result = media->auth))
574 g_object_ref (result);
575 g_mutex_unlock (&media->lock);
581 * gst_rtsp_media_set_address_pool:
582 * @media: a #GstRTSPMedia
583 * @pool: a #GstRTSPAddressPool
585 * configure @pool to be used as the address pool of @media.
588 gst_rtsp_media_set_address_pool (GstRTSPMedia * media,
589 GstRTSPAddressPool * pool)
591 GstRTSPAddressPool *old;
593 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
595 g_mutex_lock (&media->lock);
596 if ((old = media->pool) != pool)
597 media->pool = pool ? g_object_ref (pool) : NULL;
600 g_mutex_unlock (&media->lock);
603 g_object_unref (old);
607 * gst_rtsp_media_get_address_pool:
608 * @media: a #GstRTSPMedia
610 * Get the #GstRTSPAddressPool used as the address pool of @media.
612 * Returns: (transfer full): the #GstRTSPAddressPool of @media. g_object_unref() after
616 gst_rtsp_media_get_address_pool (GstRTSPMedia * media)
618 GstRTSPAddressPool *result;
620 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
622 g_mutex_lock (&media->lock);
623 if ((result = media->pool))
624 g_object_ref (result);
625 g_mutex_unlock (&media->lock);
631 * gst_rtsp_media_set_mtu:
632 * @media: a #GstRTSPMedia
635 * Set maximum size of one RTP packet on the payloaders.
636 * The @mtu will be set on all streams.
639 gst_rtsp_media_set_mtu (GstRTSPMedia * media, guint mtu)
643 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
645 g_mutex_lock (&media->lock);
647 for (i = 0; i < media->streams->len; i++) {
648 GstRTSPStream *stream;
650 GST_INFO ("Setting mtu %u for stream %d", mtu, i);
652 stream = g_ptr_array_index (media->streams, i);
653 gst_rtsp_stream_set_mtu (stream, mtu);
655 g_mutex_unlock (&media->lock);
659 * gst_rtsp_media_get_mtu:
660 * @media: a #GstRTSPMedia
662 * Get the configured MTU.
665 gst_rtsp_media_get_mtu (GstRTSPMedia * media)
669 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
671 g_mutex_lock (&media->lock);
673 g_mutex_unlock (&media->lock);
679 * gst_rtsp_media_collect_streams:
680 * @media: a #GstRTSPMedia
682 * Find all payloader elements, they should be named pay%d in the
683 * element of @media, and create #GstRTSPStreams for them.
685 * Collect all dynamic elements, named dynpay%d, and add them to
686 * the list of dynamic elements.
689 gst_rtsp_media_collect_streams (GstRTSPMedia * media)
691 GstElement *element, *elem;
696 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
698 element = media->element;
701 for (i = 0; have_elem; i++) {
706 name = g_strdup_printf ("pay%d", i);
707 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
708 GST_INFO ("found stream %d with payloader %p", i, elem);
710 /* take the pad of the payloader */
711 pad = gst_element_get_static_pad (elem, "src");
712 /* create the stream */
713 gst_rtsp_media_create_stream (media, elem, pad);
714 g_object_unref (pad);
716 gst_object_unref (elem);
722 name = g_strdup_printf ("dynpay%d", i);
723 if ((elem = gst_bin_get_by_name (GST_BIN (element), name))) {
724 /* a stream that will dynamically create pads to provide RTP packets */
726 GST_INFO ("found dynamic element %d, %p", i, elem);
728 g_mutex_lock (&media->lock);
729 media->dynamic = g_list_prepend (media->dynamic, elem);
730 g_mutex_unlock (&media->lock);
739 * gst_rtsp_media_create_stream:
740 * @media: a #GstRTSPMedia
741 * @payloader: a #GstElement
742 * @srcpad: a source #GstPad
744 * Create a new stream in @media that provides RTP data on @srcpad.
745 * @srcpad should be a pad of an element inside @media->element.
747 * Returns: (transfer none): a new #GstRTSPStream that remains valid for as long
751 gst_rtsp_media_create_stream (GstRTSPMedia * media, GstElement * payloader,
754 GstRTSPStream *stream;
759 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
760 g_return_val_if_fail (GST_IS_ELEMENT (payloader), NULL);
761 g_return_val_if_fail (GST_IS_PAD (pad), NULL);
762 g_return_val_if_fail (GST_PAD_IS_SRC (pad), NULL);
764 g_mutex_lock (&media->lock);
765 idx = media->streams->len;
767 name = g_strdup_printf ("src_%u", idx);
768 srcpad = gst_ghost_pad_new (name, pad);
769 gst_pad_set_active (srcpad, TRUE);
770 gst_element_add_pad (media->element, srcpad);
773 stream = gst_rtsp_stream_new (idx, payloader, srcpad);
775 gst_rtsp_stream_set_mtu (stream, media->mtu);
777 g_ptr_array_add (media->streams, stream);
778 g_mutex_unlock (&media->lock);
784 * gst_rtsp_media_n_streams:
785 * @media: a #GstRTSPMedia
787 * Get the number of streams in this media.
789 * Returns: The number of streams.
792 gst_rtsp_media_n_streams (GstRTSPMedia * media)
796 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
798 g_mutex_lock (&media->lock);
799 res = media->streams->len;
800 g_mutex_unlock (&media->lock);
806 * gst_rtsp_media_get_stream:
807 * @media: a #GstRTSPMedia
808 * @idx: the stream index
810 * Retrieve the stream with index @idx from @media.
812 * Returns: (transfer none): the #GstRTSPStream at index @idx or %NULL when a stream with
813 * that index did not exist.
816 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
820 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
822 g_mutex_lock (&media->lock);
823 if (idx < media->streams->len)
824 res = g_ptr_array_index (media->streams, idx);
827 g_mutex_unlock (&media->lock);
833 * gst_rtsp_media_get_range_string:
834 * @media: a #GstRTSPMedia
835 * @play: for the PLAY request
837 * Get the current range as a string.
839 * Returns: The range as a string, g_free() after usage.
842 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play)
845 GstRTSPTimeRange range;
847 g_mutex_lock (&media->lock);
849 range = media->range;
851 if (!play && media->n_active > 0) {
852 range.min.type = GST_RTSP_TIME_NOW;
853 range.min.seconds = -1;
855 g_mutex_unlock (&media->lock);
857 result = gst_rtsp_range_to_string (&range);
863 * gst_rtsp_media_seek:
864 * @media: a #GstRTSPMedia
865 * @range: a #GstRTSPTimeRange
867 * Seek the pipeline to @range.
869 * Returns: %TRUE on success.
872 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
877 GstSeekType start_type, stop_type;
879 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
880 g_return_val_if_fail (range != NULL, FALSE);
882 g_rec_mutex_lock (&media->state_lock);
883 if (!media->seekable)
886 if (range->unit != GST_RTSP_RANGE_NPT)
889 /* depends on the current playing state of the pipeline. We might need to
890 * queue this until we get EOS. */
891 flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_KEY_UNIT;
893 start_type = stop_type = GST_SEEK_TYPE_NONE;
895 switch (range->min.type) {
896 case GST_RTSP_TIME_NOW:
899 case GST_RTSP_TIME_SECONDS:
900 /* only seek when something changed */
901 if (media->range.min.seconds == range->min.seconds) {
904 start = range->min.seconds * GST_SECOND;
905 start_type = GST_SEEK_TYPE_SET;
908 case GST_RTSP_TIME_END:
912 switch (range->max.type) {
913 case GST_RTSP_TIME_SECONDS:
914 /* only seek when something changed */
915 if (media->range.max.seconds == range->max.seconds) {
918 stop = range->max.seconds * GST_SECOND;
919 stop_type = GST_SEEK_TYPE_SET;
922 case GST_RTSP_TIME_END:
924 stop_type = GST_SEEK_TYPE_SET;
926 case GST_RTSP_TIME_NOW:
931 if (start != -1 || stop != -1) {
932 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
933 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
935 res = gst_element_seek (media->pipeline, 1.0, GST_FORMAT_TIME,
936 flags, start_type, start, stop_type, stop);
938 /* and block for the seek to complete */
939 GST_INFO ("done seeking %d", res);
940 gst_element_get_state (media->pipeline, NULL, NULL, -1);
941 GST_INFO ("prerolled again");
943 collect_media_stats (media);
945 GST_INFO ("no seek needed");
948 g_rec_mutex_unlock (&media->state_lock);
955 g_rec_mutex_unlock (&media->state_lock);
956 GST_INFO ("pipeline is not seekable");
961 g_rec_mutex_unlock (&media->state_lock);
962 GST_WARNING ("seek unit %d not supported", range->unit);
967 g_rec_mutex_unlock (&media->state_lock);
968 GST_WARNING ("weird range type %d not supported", range->min.type);
974 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
976 g_mutex_lock (&media->lock);
977 /* never overwrite the error status */
978 if (media->status != GST_RTSP_MEDIA_STATUS_ERROR)
979 media->status = status;
980 GST_DEBUG ("setting new status to %d", status);
981 g_cond_broadcast (&media->cond);
982 g_mutex_unlock (&media->lock);
985 static GstRTSPMediaStatus
986 gst_rtsp_media_get_status (GstRTSPMedia * media)
988 GstRTSPMediaStatus result;
991 g_mutex_lock (&media->lock);
992 end_time = g_get_monotonic_time () + 20 * G_TIME_SPAN_SECOND;
993 /* while we are preparing, wait */
994 while (media->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
995 GST_DEBUG ("waiting for status change");
996 if (!g_cond_wait_until (&media->cond, &media->lock, end_time)) {
997 GST_DEBUG ("timeout, assuming error status");
998 media->status = GST_RTSP_MEDIA_STATUS_ERROR;
1001 /* could be success or error */
1002 result = media->status;
1003 GST_DEBUG ("got status %d", result);
1004 g_mutex_unlock (&media->lock);
1009 /* called with state-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 if (percent == 100) {
1031 /* a 100% message means buffering is done */
1032 media->buffering = FALSE;
1033 /* if the desired state is playing, go back */
1034 if (media->target_state == GST_STATE_PLAYING) {
1035 GST_INFO ("Buffering done, setting pipeline to PLAYING");
1036 gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1038 GST_INFO ("Buffering done");
1041 /* buffering busy */
1042 if (media->buffering == FALSE) {
1043 if (media->target_state == GST_STATE_PLAYING) {
1044 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1045 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1046 gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1048 GST_INFO ("Buffering ...");
1051 media->buffering = TRUE;
1055 case GST_MESSAGE_LATENCY:
1057 gst_bin_recalculate_latency (GST_BIN_CAST (media->pipeline));
1060 case GST_MESSAGE_ERROR:
1065 gst_message_parse_error (message, &gerror, &debug);
1066 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1067 g_error_free (gerror);
1070 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1073 case GST_MESSAGE_WARNING:
1078 gst_message_parse_warning (message, &gerror, &debug);
1079 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1080 g_error_free (gerror);
1084 case GST_MESSAGE_ELEMENT:
1086 case GST_MESSAGE_STREAM_STATUS:
1088 case GST_MESSAGE_ASYNC_DONE:
1089 if (!media->adding) {
1090 /* when we are dynamically adding pads, the addition of the udpsrc will
1091 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1092 * wait for the final ASYNC_DONE after everything prerolled */
1093 GST_INFO ("%p: got ASYNC_DONE", media);
1094 collect_media_stats (media);
1096 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1098 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1101 case GST_MESSAGE_EOS:
1102 GST_INFO ("%p: got EOS", media);
1104 if (media->status == GST_RTSP_MEDIA_STATUS_UNPREPARING) {
1105 GST_DEBUG ("shutting down after EOS");
1106 finish_unprepare (media);
1107 g_object_unref (media);
1111 GST_INFO ("%p: got message type %s", media,
1112 gst_message_type_get_name (type));
1119 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1121 GstRTSPMediaClass *klass;
1124 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1126 g_rec_mutex_lock (&media->state_lock);
1127 if (klass->handle_message)
1128 ret = klass->handle_message (media, message);
1131 g_rec_mutex_unlock (&media->state_lock);
1136 /* called from streaming threads */
1138 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1140 GstRTSPStream *stream;
1142 /* FIXME, element is likely not a payloader, find the payloader here */
1143 stream = gst_rtsp_media_create_stream (media, element, pad);
1145 GST_INFO ("pad added %s:%s, stream %d", GST_DEBUG_PAD_NAME (pad),
1148 g_rec_mutex_lock (&media->state_lock);
1149 /* we will be adding elements below that will cause ASYNC_DONE to be
1150 * posted in the bus. We want to ignore those messages until the
1151 * pipeline really prerolled. */
1152 media->adding = TRUE;
1154 /* join the element in the PAUSED state because this callback is
1155 * called from the streaming thread and it is PAUSED */
1156 gst_rtsp_stream_join_bin (stream, GST_BIN (media->pipeline),
1157 media->rtpbin, GST_STATE_PAUSED);
1159 media->adding = FALSE;
1160 g_rec_mutex_unlock (&media->state_lock);
1164 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1166 GstElement *fakesink;
1168 g_mutex_lock (&media->lock);
1169 GST_INFO ("no more pads");
1170 if ((fakesink = media->fakesink)) {
1171 gst_object_ref (fakesink);
1172 media->fakesink = NULL;
1173 g_mutex_unlock (&media->lock);
1175 gst_bin_remove (GST_BIN (media->pipeline), fakesink);
1176 gst_element_set_state (fakesink, GST_STATE_NULL);
1177 gst_object_unref (fakesink);
1178 GST_INFO ("removed fakesink");
1183 * gst_rtsp_media_prepare:
1184 * @media: a #GstRTSPMedia
1186 * Prepare @media for streaming. This function will create the pipeline and
1187 * other objects to manage the streaming.
1189 * It will preroll the pipeline and collect vital information about the streams
1190 * such as the duration.
1192 * Returns: %TRUE on success.
1195 gst_rtsp_media_prepare (GstRTSPMedia * media)
1197 GstStateChangeReturn ret;
1198 GstRTSPMediaStatus status;
1200 GstRTSPMediaClass *klass;
1204 g_rec_mutex_lock (&media->state_lock);
1205 if (media->status == GST_RTSP_MEDIA_STATUS_PREPARED)
1208 if (media->status == GST_RTSP_MEDIA_STATUS_PREPARING)
1211 if (media->status != GST_RTSP_MEDIA_STATUS_UNPREPARED)
1212 goto not_unprepared;
1214 if (!media->reusable && media->reused)
1217 media->rtpbin = gst_element_factory_make ("rtpbin", NULL);
1218 if (media->rtpbin == NULL)
1221 GST_INFO ("preparing media %p", media);
1223 /* reset some variables */
1224 media->is_live = FALSE;
1225 media->seekable = FALSE;
1226 media->buffering = FALSE;
1227 /* we're preparing now */
1228 media->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1230 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (media->pipeline));
1232 /* add the pipeline bus to our custom mainloop */
1233 media->source = gst_bus_create_watch (bus);
1234 gst_object_unref (bus);
1236 g_source_set_callback (media->source, (GSourceFunc) bus_message, media, NULL);
1238 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1239 media->id = g_source_attach (media->source, klass->context);
1241 /* add stuff to the bin */
1242 gst_bin_add (GST_BIN (media->pipeline), media->rtpbin);
1244 /* link streams we already have, other streams might appear when we have
1245 * dynamic elements */
1246 for (i = 0; i < media->streams->len; i++) {
1247 GstRTSPStream *stream;
1249 stream = g_ptr_array_index (media->streams, i);
1251 gst_rtsp_stream_join_bin (stream, GST_BIN (media->pipeline),
1252 media->rtpbin, GST_STATE_NULL);
1255 for (walk = media->dynamic; walk; walk = g_list_next (walk)) {
1256 GstElement *elem = walk->data;
1258 GST_INFO ("adding callbacks for dynamic element %p", elem);
1260 g_signal_connect (elem, "pad-added", (GCallback) pad_added_cb, media);
1261 g_signal_connect (elem, "no-more-pads", (GCallback) no_more_pads_cb, media);
1263 /* we add a fakesink here in order to make the state change async. We remove
1264 * the fakesink again in the no-more-pads callback. */
1265 media->fakesink = gst_element_factory_make ("fakesink", "fakesink");
1266 gst_bin_add (GST_BIN (media->pipeline), media->fakesink);
1269 GST_INFO ("setting pipeline to PAUSED for media %p", media);
1270 /* first go to PAUSED */
1271 ret = gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1272 media->target_state = GST_STATE_PAUSED;
1275 case GST_STATE_CHANGE_SUCCESS:
1276 GST_INFO ("SUCCESS state change for media %p", media);
1277 media->seekable = TRUE;
1279 case GST_STATE_CHANGE_ASYNC:
1280 GST_INFO ("ASYNC state change for media %p", media);
1281 media->seekable = TRUE;
1283 case GST_STATE_CHANGE_NO_PREROLL:
1284 /* we need to go to PLAYING */
1285 GST_INFO ("NO_PREROLL state change: live media %p", media);
1286 /* FIXME we disable seeking for live streams for now. We should perform a
1287 * seeking query in preroll instead */
1288 media->seekable = FALSE;
1289 media->is_live = TRUE;
1290 ret = gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1291 if (ret == GST_STATE_CHANGE_FAILURE)
1294 case GST_STATE_CHANGE_FAILURE:
1298 g_rec_mutex_unlock (&media->state_lock);
1300 /* now wait for all pads to be prerolled, FIXME, we should somehow be
1301 * able to do this async so that we don't block the server thread. */
1302 status = gst_rtsp_media_get_status (media);
1303 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1306 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
1308 GST_INFO ("object %p is prerolled", media);
1315 GST_LOG ("media %p was prepared", media);
1316 g_rec_mutex_unlock (&media->state_lock);
1322 GST_WARNING ("media %p was not unprepared", media);
1323 g_rec_mutex_unlock (&media->state_lock);
1328 g_rec_mutex_unlock (&media->state_lock);
1329 GST_WARNING ("can not reuse media %p", media);
1334 g_rec_mutex_unlock (&media->state_lock);
1335 GST_WARNING ("no rtpbin element");
1336 g_warning ("failed to create element 'rtpbin', check your installation");
1341 GST_WARNING ("failed to preroll pipeline");
1342 gst_rtsp_media_unprepare (media);
1343 g_rec_mutex_unlock (&media->state_lock);
1348 /* must be called with state-lock */
1350 finish_unprepare (GstRTSPMedia * media)
1354 GST_DEBUG ("shutting down");
1356 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1358 for (i = 0; i < media->streams->len; i++) {
1359 GstRTSPStream *stream;
1361 GST_INFO ("Removing elements of stream %d from pipeline", i);
1363 stream = g_ptr_array_index (media->streams, i);
1365 gst_rtsp_stream_leave_bin (stream, GST_BIN (media->pipeline),
1368 g_ptr_array_set_size (media->streams, 0);
1370 gst_bin_remove (GST_BIN (media->pipeline), media->rtpbin);
1371 media->rtpbin = NULL;
1373 gst_object_unref (media->pipeline);
1374 media->pipeline = NULL;
1376 media->reused = TRUE;
1377 media->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
1379 /* when the media is not reusable, this will effectively unref the media and
1381 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
1384 /* called with state-lock */
1386 default_unprepare (GstRTSPMedia * media)
1388 if (media->eos_shutdown) {
1389 GST_DEBUG ("sending EOS for shutdown");
1390 /* ref so that we don't disappear */
1391 g_object_ref (media);
1392 gst_element_send_event (media->pipeline, gst_event_new_eos ());
1393 /* we need to go to playing again for the EOS to propagate, normally in this
1394 * state, nothing is receiving data from us anymore so this is ok. */
1395 gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1396 media->status = GST_RTSP_MEDIA_STATUS_UNPREPARING;
1398 finish_unprepare (media);
1404 * gst_rtsp_media_unprepare:
1405 * @media: a #GstRTSPMedia
1407 * Unprepare @media. After this call, the media should be prepared again before
1408 * it can be used again. If the media is set to be non-reusable, a new instance
1411 * Returns: %TRUE on success.
1414 gst_rtsp_media_unprepare (GstRTSPMedia * media)
1418 g_rec_mutex_lock (&media->state_lock);
1419 if (media->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
1420 goto was_unprepared;
1422 GST_INFO ("unprepare media %p", media);
1423 media->target_state = GST_STATE_NULL;
1426 if (media->status == GST_RTSP_MEDIA_STATUS_PREPARED) {
1427 GstRTSPMediaClass *klass;
1429 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1430 if (klass->unprepare)
1431 success = klass->unprepare (media);
1433 finish_unprepare (media);
1435 g_rec_mutex_unlock (&media->state_lock);
1441 g_rec_mutex_unlock (&media->state_lock);
1442 GST_INFO ("media %p was already unprepared", media);
1448 * gst_rtsp_media_set_state:
1449 * @media: a #GstRTSPMedia
1450 * @state: the target state of the media
1451 * @transports: a #GPtrArray of #GstRTSPStreamTransport pointers
1453 * Set the state of @media to @state and for the transports in @transports.
1455 * Returns: %TRUE on success.
1458 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
1459 GPtrArray * transports)
1462 gboolean add, remove, do_state;
1465 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1466 g_return_val_if_fail (transports != NULL, FALSE);
1468 g_rec_mutex_lock (&media->state_lock);
1470 /* NULL and READY are the same */
1471 if (state == GST_STATE_READY)
1472 state = GST_STATE_NULL;
1474 add = remove = FALSE;
1476 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
1480 case GST_STATE_NULL:
1481 case GST_STATE_PAUSED:
1482 /* we're going from PLAYING to PAUSED, READY or NULL, remove */
1483 if (media->target_state == GST_STATE_PLAYING)
1486 case GST_STATE_PLAYING:
1487 /* we're going to PLAYING, add */
1493 old_active = media->n_active;
1495 for (i = 0; i < transports->len; i++) {
1496 GstRTSPStreamTransport *trans;
1498 /* we need a non-NULL entry in the array */
1499 trans = g_ptr_array_index (transports, i);
1503 /* we need a transport */
1504 if (!trans->transport)
1508 if (gst_rtsp_stream_add_transport (trans->stream, trans))
1510 } else if (remove) {
1511 if (gst_rtsp_stream_remove_transport (trans->stream, trans))
1516 /* we just added the first media, do the playing state change */
1517 if (old_active == 0 && add)
1519 /* if we have no more active media, do the downward state changes */
1520 else if (media->n_active == 0)
1525 GST_INFO ("state %d active %d media %p do_state %d", state, media->n_active,
1528 if (media->target_state != state) {
1530 if (state == GST_STATE_NULL) {
1531 gst_rtsp_media_unprepare (media);
1533 GST_INFO ("state %s media %p", gst_element_state_get_name (state),
1535 media->target_state = state;
1536 gst_element_set_state (media->pipeline, state);
1539 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
1543 /* remember where we are */
1544 if (state != GST_STATE_NULL && (state == GST_STATE_PAUSED ||
1545 old_active != media->n_active))
1546 collect_media_stats (media);
1548 g_rec_mutex_unlock (&media->state_lock);