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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 #include <gst/app/gstappsrc.h>
24 #include <gst/app/gstappsink.h>
26 #include "rtsp-media.h"
28 #define DEFAULT_SHARED FALSE
29 #define DEFAULT_REUSABLE FALSE
30 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_TCP
31 //#define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP_MCAST
32 #define DEFAULT_EOS_SHUTDOWN FALSE
33 #define DEFAULT_BUFFER_SIZE 0x80000
34 #define DEFAULT_MULTICAST_GROUP "224.2.0.1"
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 GQuark ssrc_stream_map_key;
64 static void gst_rtsp_media_get_property (GObject * object, guint propid,
65 GValue * value, GParamSpec * pspec);
66 static void gst_rtsp_media_set_property (GObject * object, guint propid,
67 const GValue * value, GParamSpec * pspec);
68 static void gst_rtsp_media_finalize (GObject * obj);
70 static gpointer do_loop (GstRTSPMediaClass * klass);
71 static gboolean default_handle_message (GstRTSPMedia * media,
72 GstMessage * message);
73 static gboolean default_unprepare (GstRTSPMedia * media);
74 static void unlock_streams (GstRTSPMedia * media);
76 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
78 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
81 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
83 GObjectClass *gobject_class;
86 gobject_class = G_OBJECT_CLASS (klass);
88 gobject_class->get_property = gst_rtsp_media_get_property;
89 gobject_class->set_property = gst_rtsp_media_set_property;
90 gobject_class->finalize = gst_rtsp_media_finalize;
92 g_object_class_install_property (gobject_class, PROP_SHARED,
93 g_param_spec_boolean ("shared", "Shared",
94 "If this media pipeline can be shared", DEFAULT_SHARED,
95 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
97 g_object_class_install_property (gobject_class, PROP_REUSABLE,
98 g_param_spec_boolean ("reusable", "Reusable",
99 "If this media pipeline can be reused after an unprepare",
100 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
102 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
103 g_param_spec_flags ("protocols", "Protocols",
104 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
105 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
107 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
108 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
109 "Send an EOS event to the pipeline before unpreparing",
110 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
112 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
113 g_param_spec_uint ("buffer-size", "Buffer Size",
114 "The kernel UDP buffer size to use", 0, G_MAXUINT,
115 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
117 g_object_class_install_property (gobject_class, PROP_MULTICAST_GROUP,
118 g_param_spec_string ("multicast-group", "Multicast Group",
119 "The Multicast group to send media to",
120 DEFAULT_MULTICAST_GROUP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
122 gst_rtsp_media_signals[SIGNAL_PREPARED] =
123 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
124 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
125 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
127 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
128 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
129 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
130 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
132 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
133 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
134 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
135 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 0, G_TYPE_INT);
137 klass->context = g_main_context_new ();
138 klass->loop = g_main_loop_new (klass->context, TRUE);
140 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
142 klass->thread = g_thread_create ((GThreadFunc) do_loop, klass, TRUE, &error);
144 g_critical ("could not start bus thread: %s", error->message);
146 klass->handle_message = default_handle_message;
147 klass->unprepare = default_unprepare;
149 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
153 gst_rtsp_media_init (GstRTSPMedia * media)
155 media->streams = g_array_new (FALSE, TRUE, sizeof (GstRTSPMediaStream *));
156 media->lock = g_mutex_new ();
157 media->cond = g_cond_new ();
159 media->shared = DEFAULT_SHARED;
160 media->reusable = DEFAULT_REUSABLE;
161 media->protocols = DEFAULT_PROTOCOLS;
162 media->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
163 media->buffer_size = DEFAULT_BUFFER_SIZE;
164 media->multicast_group = g_strdup (DEFAULT_MULTICAST_GROUP);
168 gst_rtsp_media_trans_cleanup (GstRTSPMediaTrans * trans)
170 if (trans->transport) {
171 gst_rtsp_transport_free (trans->transport);
172 trans->transport = NULL;
174 if (trans->rtpsource) {
175 g_object_set_qdata (trans->rtpsource, ssrc_stream_map_key, NULL);
176 trans->rtpsource = NULL;
181 gst_rtsp_media_stream_free (GstRTSPMediaStream * stream)
184 g_object_unref (stream->session);
187 gst_caps_unref (stream->caps);
189 if (stream->send_rtp_sink)
190 gst_object_unref (stream->send_rtp_sink);
191 if (stream->send_rtp_src)
192 gst_object_unref (stream->send_rtp_src);
193 if (stream->send_rtcp_src)
194 gst_object_unref (stream->send_rtcp_src);
195 if (stream->recv_rtcp_sink)
196 gst_object_unref (stream->recv_rtcp_sink);
197 if (stream->recv_rtp_sink)
198 gst_object_unref (stream->recv_rtp_sink);
200 g_list_free (stream->transports);
206 gst_rtsp_media_finalize (GObject * obj)
211 media = GST_RTSP_MEDIA (obj);
213 GST_INFO ("finalize media %p", media);
215 if (media->pipeline) {
216 unlock_streams (media);
217 gst_element_set_state (media->pipeline, GST_STATE_NULL);
218 gst_object_unref (media->pipeline);
221 for (i = 0; i < media->streams->len; i++) {
222 GstRTSPMediaStream *stream;
224 stream = g_array_index (media->streams, GstRTSPMediaStream *, i);
226 gst_rtsp_media_stream_free (stream);
228 g_array_free (media->streams, TRUE);
230 g_list_foreach (media->dynamic, (GFunc) gst_object_unref, NULL);
231 g_list_free (media->dynamic);
234 g_source_destroy (media->source);
235 g_source_unref (media->source);
237 g_free (media->multicast_group);
238 g_mutex_free (media->lock);
239 g_cond_free (media->cond);
241 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
245 gst_rtsp_media_get_property (GObject * object, guint propid,
246 GValue * value, GParamSpec * pspec)
248 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
252 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
255 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
258 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
260 case PROP_EOS_SHUTDOWN:
261 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
263 case PROP_BUFFER_SIZE:
264 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
266 case PROP_MULTICAST_GROUP:
267 g_value_take_string (value, gst_rtsp_media_get_multicast_group (media));
270 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
275 gst_rtsp_media_set_property (GObject * object, guint propid,
276 const GValue * value, GParamSpec * pspec)
278 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
282 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
285 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
288 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
290 case PROP_EOS_SHUTDOWN:
291 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
293 case PROP_BUFFER_SIZE:
294 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
296 case PROP_MULTICAST_GROUP:
297 gst_rtsp_media_set_multicast_group (media, g_value_get_string (value));
300 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
305 do_loop (GstRTSPMediaClass * klass)
307 GST_INFO ("enter mainloop");
308 g_main_loop_run (klass->loop);
309 GST_INFO ("exit mainloop");
315 collect_media_stats (GstRTSPMedia * media)
317 gint64 position, duration;
319 media->range.unit = GST_RTSP_RANGE_NPT;
321 if (media->is_live) {
322 media->range.min.type = GST_RTSP_TIME_NOW;
323 media->range.min.seconds = -1;
324 media->range.max.type = GST_RTSP_TIME_END;
325 media->range.max.seconds = -1;
327 /* get the position */
328 if (!gst_element_query_position (media->pipeline, GST_FORMAT_TIME,
330 GST_INFO ("position query failed");
334 /* get the duration */
335 if (!gst_element_query_duration (media->pipeline, GST_FORMAT_TIME,
337 GST_INFO ("duration query failed");
341 GST_INFO ("stats: position %" GST_TIME_FORMAT ", duration %"
342 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (duration));
344 if (position == -1) {
345 media->range.min.type = GST_RTSP_TIME_NOW;
346 media->range.min.seconds = -1;
348 media->range.min.type = GST_RTSP_TIME_SECONDS;
349 media->range.min.seconds = ((gdouble) position) / GST_SECOND;
351 if (duration == -1) {
352 media->range.max.type = GST_RTSP_TIME_END;
353 media->range.max.seconds = -1;
355 media->range.max.type = GST_RTSP_TIME_SECONDS;
356 media->range.max.seconds = ((gdouble) duration) / GST_SECOND;
362 * gst_rtsp_media_new:
364 * Create a new #GstRTSPMedia instance. The #GstRTSPMedia object contains the
365 * element to produde RTP data for one or more related (audio/video/..)
368 * Returns: a new #GstRTSPMedia object.
371 gst_rtsp_media_new (void)
373 GstRTSPMedia *result;
375 result = g_object_new (GST_TYPE_RTSP_MEDIA, NULL);
381 * gst_rtsp_media_set_shared:
382 * @media: a #GstRTSPMedia
383 * @shared: the new value
385 * Set or unset if the pipeline for @media can be shared will multiple clients.
386 * When @shared is %TRUE, client requests for this media will share the media
390 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
392 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
394 media->shared = shared;
398 * gst_rtsp_media_is_shared:
399 * @media: a #GstRTSPMedia
401 * Check if the pipeline for @media can be shared between multiple clients.
403 * Returns: %TRUE if the media can be shared between clients.
406 gst_rtsp_media_is_shared (GstRTSPMedia * media)
408 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
410 return media->shared;
414 * gst_rtsp_media_set_reusable:
415 * @media: a #GstRTSPMedia
416 * @reusable: the new value
418 * Set or unset if the pipeline for @media can be reused after the pipeline has
422 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
424 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
426 media->reusable = reusable;
430 * gst_rtsp_media_is_reusable:
431 * @media: a #GstRTSPMedia
433 * Check if the pipeline for @media can be reused after an unprepare.
435 * Returns: %TRUE if the media can be reused
438 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
440 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
442 return media->reusable;
446 * gst_rtsp_media_set_protocols:
447 * @media: a #GstRTSPMedia
448 * @protocols: the new flags
450 * Configure the allowed lower transport for @media.
453 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
455 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
457 media->protocols = protocols;
461 * gst_rtsp_media_get_protocols:
462 * @media: a #GstRTSPMedia
464 * Get the allowed protocols of @media.
466 * Returns: a #GstRTSPLowerTrans
469 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
471 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
472 GST_RTSP_LOWER_TRANS_UNKNOWN);
474 return media->protocols;
478 * gst_rtsp_media_set_eos_shutdown:
479 * @media: a #GstRTSPMedia
480 * @eos_shutdown: the new value
482 * Set or unset if an EOS event will be sent to the pipeline for @media before
486 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
488 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
490 media->eos_shutdown = eos_shutdown;
494 * gst_rtsp_media_is_eos_shutdown:
495 * @media: a #GstRTSPMedia
497 * Check if the pipeline for @media will send an EOS down the pipeline before
500 * Returns: %TRUE if the media will send EOS before unpreparing.
503 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
505 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
507 return media->eos_shutdown;
511 * gst_rtsp_media_set_buffer_size:
512 * @media: a #GstRTSPMedia
513 * @size: the new value
515 * Set the kernel UDP buffer size.
518 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
520 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
522 media->buffer_size = size;
526 * gst_rtsp_media_get_buffer_size:
527 * @media: a #GstRTSPMedia
529 * Get the kernel UDP buffer size.
531 * Returns: the kernel UDP buffer size.
534 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
536 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
538 return media->buffer_size;
542 * gst_rtsp_media_set_multicast_group:
543 * @media: a #GstRTSPMedia
544 * @mc: the new multicast group
546 * Set the multicast group that media from @media will be streamed to.
549 gst_rtsp_media_set_multicast_group (GstRTSPMedia * media, const gchar * mc)
551 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
553 g_mutex_lock (media->lock);
554 g_free (media->multicast_group);
555 media->multicast_group = g_strdup (mc);
556 g_mutex_unlock (media->lock);
560 * gst_rtsp_media_get_multicast_group:
561 * @media: a #GstRTSPMedia
563 * Get the multicast group that media from @media will be streamed to.
565 * Returns: the multicast group
568 gst_rtsp_media_get_multicast_group (GstRTSPMedia * media)
572 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
574 g_mutex_lock (media->lock);
575 result = g_strdup (media->multicast_group);
576 g_mutex_unlock (media->lock);
582 * gst_rtsp_media_set_auth:
583 * @media: a #GstRTSPMedia
584 * @auth: a #GstRTSPAuth
586 * configure @auth to be used as the authentication manager of @media.
589 gst_rtsp_media_set_auth (GstRTSPMedia * media, GstRTSPAuth * auth)
593 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
602 g_object_unref (old);
607 * gst_rtsp_media_get_auth:
608 * @media: a #GstRTSPMedia
610 * Get the #GstRTSPAuth used as the authentication manager of @media.
612 * Returns: the #GstRTSPAuth of @media. g_object_unref() after
616 gst_rtsp_media_get_auth (GstRTSPMedia * media)
620 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
622 if ((result = media->auth))
623 g_object_ref (result);
630 * gst_rtsp_media_n_streams:
631 * @media: a #GstRTSPMedia
633 * Get the number of streams in this media.
635 * Returns: The number of streams.
638 gst_rtsp_media_n_streams (GstRTSPMedia * media)
640 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
642 return media->streams->len;
646 * gst_rtsp_media_get_stream:
647 * @media: a #GstRTSPMedia
648 * @idx: the stream index
650 * Retrieve the stream with index @idx from @media.
652 * Returns: the #GstRTSPMediaStream at index @idx or %NULL when a stream with
653 * that index did not exist.
656 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
658 GstRTSPMediaStream *res;
660 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
662 if (idx < media->streams->len)
663 res = g_array_index (media->streams, GstRTSPMediaStream *, idx);
671 * gst_rtsp_media_get_range_string:
672 * @media: a #GstRTSPMedia
673 * @play: for the PLAY request
675 * Get the current range as a string.
677 * Returns: The range as a string, g_free() after usage.
680 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play)
683 GstRTSPTimeRange range;
686 range = media->range;
688 if (!play && media->active > 0) {
689 range.min.type = GST_RTSP_TIME_NOW;
690 range.min.seconds = -1;
693 result = gst_rtsp_range_to_string (&range);
699 * gst_rtsp_media_seek:
700 * @media: a #GstRTSPMedia
701 * @range: a #GstRTSPTimeRange
703 * Seek the pipeline to @range.
705 * Returns: %TRUE on success.
708 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
713 GstSeekType start_type, stop_type;
715 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
716 g_return_val_if_fail (range != NULL, FALSE);
718 if (media->seekable) {
719 GST_INFO ("pipeline is not seekable");
723 if (range->unit != GST_RTSP_RANGE_NPT)
726 /* depends on the current playing state of the pipeline. We might need to
727 * queue this until we get EOS. */
728 flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_KEY_UNIT;
730 start_type = stop_type = GST_SEEK_TYPE_NONE;
732 switch (range->min.type) {
733 case GST_RTSP_TIME_NOW:
736 case GST_RTSP_TIME_SECONDS:
737 /* only seek when something changed */
738 if (media->range.min.seconds == range->min.seconds) {
741 start = range->min.seconds * GST_SECOND;
742 start_type = GST_SEEK_TYPE_SET;
745 case GST_RTSP_TIME_END:
749 switch (range->max.type) {
750 case GST_RTSP_TIME_SECONDS:
751 /* only seek when something changed */
752 if (media->range.max.seconds == range->max.seconds) {
755 stop = range->max.seconds * GST_SECOND;
756 stop_type = GST_SEEK_TYPE_SET;
759 case GST_RTSP_TIME_END:
761 stop_type = GST_SEEK_TYPE_SET;
763 case GST_RTSP_TIME_NOW:
768 if (start != -1 || stop != -1) {
769 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
770 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
772 res = gst_element_seek (media->pipeline, 1.0, GST_FORMAT_TIME,
773 flags, start_type, start, stop_type, stop);
775 /* and block for the seek to complete */
776 GST_INFO ("done seeking %d", res);
777 gst_element_get_state (media->pipeline, NULL, NULL, -1);
778 GST_INFO ("prerolled again");
780 collect_media_stats (media);
782 GST_INFO ("no seek needed");
791 GST_WARNING ("seek unit %d not supported", range->unit);
796 GST_WARNING ("weird range type %d not supported", range->min.type);
802 * gst_rtsp_media_stream_rtp:
803 * @stream: a #GstRTSPMediaStream
804 * @buffer: a #GstBuffer
806 * Handle an RTP buffer for the stream. This method is usually called when a
807 * message has been received from a client using the TCP transport.
809 * This function takes ownership of @buffer.
811 * Returns: a GstFlowReturn.
814 gst_rtsp_media_stream_rtp (GstRTSPMediaStream * stream, GstBuffer * buffer)
818 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (stream->appsrc[0]), buffer);
824 * gst_rtsp_media_stream_rtcp:
825 * @stream: a #GstRTSPMediaStream
826 * @buffer: a #GstBuffer
828 * Handle an RTCP buffer for the stream. This method is usually called when a
829 * message has been received from a client using the TCP transport.
831 * This function takes ownership of @buffer.
833 * Returns: a GstFlowReturn.
836 gst_rtsp_media_stream_rtcp (GstRTSPMediaStream * stream, GstBuffer * buffer)
840 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (stream->appsrc[1]), buffer);
845 /* Allocate the udp ports and sockets */
847 alloc_udp_ports (GstRTSPMedia * media, GstRTSPMediaStream * stream)
849 GstStateChangeReturn ret;
850 GstElement *udpsrc0, *udpsrc1;
851 GstElement *udpsink0, *udpsink1;
852 gint tmp_rtp, tmp_rtcp;
854 gint rtpport, rtcpport;
864 /* Start with random port */
868 host = "udp://[::0]";
870 host = "udp://0.0.0.0";
872 /* try to allocate 2 UDP ports, the RTP port should be an even
873 * number and the RTCP port should be the next (uneven) port */
875 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
877 goto no_udp_protocol;
878 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, NULL);
880 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
881 if (ret == GST_STATE_CHANGE_FAILURE) {
887 gst_element_set_state (udpsrc0, GST_STATE_NULL);
888 gst_object_unref (udpsrc0);
892 goto no_udp_protocol;
895 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
897 /* check if port is even */
898 if ((tmp_rtp & 1) != 0) {
899 /* port not even, close and allocate another */
903 gst_element_set_state (udpsrc0, GST_STATE_NULL);
904 gst_object_unref (udpsrc0);
910 /* allocate port+1 for RTCP now */
911 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
913 goto no_udp_rtcp_protocol;
916 tmp_rtcp = tmp_rtp + 1;
917 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, NULL);
919 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
920 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
921 if (ret == GST_STATE_CHANGE_FAILURE) {
926 gst_element_set_state (udpsrc0, GST_STATE_NULL);
927 gst_object_unref (udpsrc0);
929 gst_element_set_state (udpsrc1, GST_STATE_NULL);
930 gst_object_unref (udpsrc1);
936 /* all fine, do port check */
937 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
938 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
940 /* this should not happen... */
941 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
944 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
946 goto no_udp_protocol;
948 g_object_get (G_OBJECT (udpsrc0), "socket", &socket, NULL);
949 g_object_set (G_OBJECT (udpsink0), "socket", socket, NULL);
950 g_object_set (G_OBJECT (udpsink0), "close-socket", FALSE, NULL);
952 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
954 goto no_udp_protocol;
956 if (g_object_class_find_property (G_OBJECT_GET_CLASS (udpsink0),
957 "send-duplicates")) {
958 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
959 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
962 ("old multiudpsink version found without send-duplicates property");
965 if (g_object_class_find_property (G_OBJECT_GET_CLASS (udpsink0),
967 g_object_set (G_OBJECT (udpsink0), "buffer-size", media->buffer_size, NULL);
969 GST_WARNING ("multiudpsink version found without buffer-size property");
972 g_object_get (G_OBJECT (udpsrc1), "socket", &socket, NULL);
973 g_object_set (G_OBJECT (udpsink1), "socket", socket, NULL);
974 g_object_set (G_OBJECT (udpsink1), "close-socket", FALSE, NULL);
975 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
976 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
978 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
979 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
980 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
981 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
983 /* we keep these elements, we configure all in configure_transport when the
984 * server told us to really use the UDP ports. */
985 stream->udpsrc[0] = udpsrc0;
986 stream->udpsrc[1] = udpsrc1;
987 stream->udpsink[0] = udpsink0;
988 stream->udpsink[1] = udpsink1;
989 stream->server_port.min = rtpport;
990 stream->server_port.max = rtcpport;
1003 no_udp_rtcp_protocol:
1014 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1015 gst_object_unref (udpsrc0);
1018 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1019 gst_object_unref (udpsrc1);
1022 gst_element_set_state (udpsink0, GST_STATE_NULL);
1023 gst_object_unref (udpsink0);
1026 gst_element_set_state (udpsink1, GST_STATE_NULL);
1027 gst_object_unref (udpsink1);
1033 /* executed from streaming thread */
1035 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPMediaStream * stream)
1038 GstCaps *newcaps, *oldcaps;
1040 newcaps = gst_pad_get_current_caps (pad);
1042 oldcaps = stream->caps;
1043 stream->caps = newcaps;
1046 gst_caps_unref (oldcaps);
1048 capsstr = gst_caps_to_string (newcaps);
1049 GST_INFO ("stream %p received caps %p, %s", stream, newcaps, capsstr);
1054 dump_structure (const GstStructure * s)
1058 sstr = gst_structure_to_string (s);
1059 GST_INFO ("structure: %s", sstr);
1063 static GstRTSPMediaTrans *
1064 find_transport (GstRTSPMediaStream * stream, const gchar * rtcp_from)
1067 GstRTSPMediaTrans *result = NULL;
1072 if (rtcp_from == NULL)
1075 tmp = g_strrstr (rtcp_from, ":");
1079 port = atoi (tmp + 1);
1080 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1082 GST_INFO ("finding %s:%d", dest, port);
1084 for (walk = stream->transports; walk; walk = g_list_next (walk)) {
1085 GstRTSPMediaTrans *trans = walk->data;
1088 min = trans->transport->client_port.min;
1089 max = trans->transport->client_port.max;
1091 if ((strcmp (trans->transport->destination, dest) == 0) && (min == port
1103 on_new_ssrc (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1105 GstStructure *stats;
1106 GstRTSPMediaTrans *trans;
1108 GST_INFO ("%p: new source %p", stream, source);
1110 /* see if we have a stream to match with the origin of the RTCP packet */
1111 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1112 if (trans == NULL) {
1113 g_object_get (source, "stats", &stats, NULL);
1115 const gchar *rtcp_from;
1117 dump_structure (stats);
1119 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1120 if ((trans = find_transport (stream, rtcp_from))) {
1121 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1124 /* keep ref to the source */
1125 trans->rtpsource = source;
1127 g_object_set_qdata (source, ssrc_stream_map_key, trans);
1129 gst_structure_free (stats);
1132 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1137 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1139 GST_INFO ("%p: new SDES %p", stream, source);
1143 on_ssrc_active (GObject * session, GObject * source,
1144 GstRTSPMediaStream * stream)
1146 GstRTSPMediaTrans *trans;
1148 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1150 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1152 if (trans && trans->keep_alive)
1153 trans->keep_alive (trans->ka_user_data);
1157 GstStructure *stats;
1158 g_object_get (source, "stats", &stats, NULL);
1160 dump_structure (stats);
1161 gst_structure_free (stats);
1168 on_bye_ssrc (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1170 GST_INFO ("%p: source %p bye", stream, source);
1174 on_bye_timeout (GObject * session, GObject * source,
1175 GstRTSPMediaStream * stream)
1177 GstRTSPMediaTrans *trans;
1179 GST_INFO ("%p: source %p bye timeout", stream, source);
1181 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1182 trans->rtpsource = NULL;
1183 trans->timeout = TRUE;
1188 on_timeout (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1190 GstRTSPMediaTrans *trans;
1192 GST_INFO ("%p: source %p timeout", stream, source);
1194 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1195 trans->rtpsource = NULL;
1196 trans->timeout = TRUE;
1200 static GstFlowReturn
1201 handle_new_sample (GstAppSink * sink, gpointer user_data)
1206 GstRTSPMediaStream *stream;
1208 sample = gst_app_sink_pull_sample (sink);
1212 stream = (GstRTSPMediaStream *) user_data;
1213 buffer = gst_sample_get_buffer (sample);
1215 for (walk = stream->transports; walk; walk = g_list_next (walk)) {
1216 GstRTSPMediaTrans *tr = (GstRTSPMediaTrans *) walk->data;
1218 if (GST_ELEMENT_CAST (sink) == stream->appsink[0]) {
1220 tr->send_rtp (buffer, tr->transport->interleaved.min, tr->user_data);
1223 tr->send_rtcp (buffer, tr->transport->interleaved.max, tr->user_data);
1226 gst_sample_unref (sample);
1231 static GstAppSinkCallbacks sink_cb = {
1232 NULL, /* not interested in EOS */
1233 NULL, /* not interested in preroll samples */
1237 /* prepare the pipeline objects to handle @stream in @media */
1239 setup_stream (GstRTSPMediaStream * stream, guint idx, GstRTSPMedia * media)
1242 GstPad *pad, *teepad, *queuepad, *selpad;
1243 GstPadLinkReturn ret;
1246 /* allocate udp ports, we will have 4 of them, 2 for receiving RTP/RTCP and 2
1247 * for sending RTP/RTCP. The sender and receiver ports are shared between the
1249 if (!alloc_udp_ports (media, stream))
1252 /* add the ports to the pipeline */
1253 for (i = 0; i < 2; i++) {
1254 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->udpsink[i]);
1255 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->udpsrc[i]);
1258 /* create elements for the TCP transfer */
1259 for (i = 0; i < 2; i++) {
1260 stream->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
1261 stream->appqueue[i] = gst_element_factory_make ("queue", NULL);
1262 stream->appsink[i] = gst_element_factory_make ("appsink", NULL);
1263 g_object_set (stream->appsink[i], "async", FALSE, "sync", FALSE, NULL);
1264 g_object_set (stream->appsink[i], "emit-signals", FALSE, NULL);
1265 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->appqueue[i]);
1266 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->appsink[i]);
1267 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->appsrc[i]);
1268 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (stream->appsink[i]),
1269 &sink_cb, stream, NULL);
1272 /* hook up the stream to the RTP session elements. */
1273 name = g_strdup_printf ("send_rtp_sink_%u", idx);
1274 stream->send_rtp_sink = gst_element_get_request_pad (media->rtpbin, name);
1276 name = g_strdup_printf ("send_rtp_src_%u", idx);
1277 stream->send_rtp_src = gst_element_get_static_pad (media->rtpbin, name);
1279 name = g_strdup_printf ("send_rtcp_src_%u", idx);
1280 stream->send_rtcp_src = gst_element_get_request_pad (media->rtpbin, name);
1282 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
1283 stream->recv_rtcp_sink = gst_element_get_request_pad (media->rtpbin, name);
1285 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
1286 stream->recv_rtp_sink = gst_element_get_request_pad (media->rtpbin, name);
1289 /* get the session */
1290 g_signal_emit_by_name (media->rtpbin, "get-internal-session", idx,
1293 g_signal_connect (stream->session, "on-new-ssrc", (GCallback) on_new_ssrc,
1295 g_signal_connect (stream->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
1297 g_signal_connect (stream->session, "on-ssrc-active",
1298 (GCallback) on_ssrc_active, stream);
1299 g_signal_connect (stream->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
1301 g_signal_connect (stream->session, "on-bye-timeout",
1302 (GCallback) on_bye_timeout, stream);
1303 g_signal_connect (stream->session, "on-timeout", (GCallback) on_timeout,
1306 /* link the RTP pad to the session manager */
1307 ret = gst_pad_link (stream->srcpad, stream->send_rtp_sink);
1308 if (ret != GST_PAD_LINK_OK)
1311 /* make tee for RTP and link to stream */
1312 stream->tee[0] = gst_element_factory_make ("tee", NULL);
1313 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->tee[0]);
1315 pad = gst_element_get_static_pad (stream->tee[0], "sink");
1316 gst_pad_link (stream->send_rtp_src, pad);
1317 gst_object_unref (pad);
1319 /* link RTP sink, we're pretty sure this will work. */
1320 teepad = gst_element_get_request_pad (stream->tee[0], "src_%u");
1321 pad = gst_element_get_static_pad (stream->udpsink[0], "sink");
1322 gst_pad_link (teepad, pad);
1323 gst_object_unref (pad);
1324 gst_object_unref (teepad);
1326 teepad = gst_element_get_request_pad (stream->tee[0], "src_%u");
1327 pad = gst_element_get_static_pad (stream->appqueue[0], "sink");
1328 gst_pad_link (teepad, pad);
1329 gst_object_unref (pad);
1330 gst_object_unref (teepad);
1332 queuepad = gst_element_get_static_pad (stream->appqueue[0], "src");
1333 pad = gst_element_get_static_pad (stream->appsink[0], "sink");
1334 gst_pad_link (queuepad, pad);
1335 gst_object_unref (pad);
1336 gst_object_unref (queuepad);
1338 /* make tee for RTCP */
1339 stream->tee[1] = gst_element_factory_make ("tee", NULL);
1340 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->tee[1]);
1342 pad = gst_element_get_static_pad (stream->tee[1], "sink");
1343 gst_pad_link (stream->send_rtcp_src, pad);
1344 gst_object_unref (pad);
1346 /* link RTCP elements */
1347 teepad = gst_element_get_request_pad (stream->tee[1], "src_%u");
1348 pad = gst_element_get_static_pad (stream->udpsink[1], "sink");
1349 gst_pad_link (teepad, pad);
1350 gst_object_unref (pad);
1351 gst_object_unref (teepad);
1353 teepad = gst_element_get_request_pad (stream->tee[1], "src_%u");
1354 pad = gst_element_get_static_pad (stream->appqueue[1], "sink");
1355 gst_pad_link (teepad, pad);
1356 gst_object_unref (pad);
1357 gst_object_unref (teepad);
1359 queuepad = gst_element_get_static_pad (stream->appqueue[1], "src");
1360 pad = gst_element_get_static_pad (stream->appsink[1], "sink");
1361 gst_pad_link (queuepad, pad);
1362 gst_object_unref (pad);
1363 gst_object_unref (queuepad);
1365 /* make selector for the RTP receivers */
1366 stream->selector[0] = gst_element_factory_make ("funnel", NULL);
1367 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->selector[0]);
1369 pad = gst_element_get_static_pad (stream->selector[0], "src");
1370 gst_pad_link (pad, stream->recv_rtp_sink);
1371 gst_object_unref (pad);
1373 selpad = gst_element_get_request_pad (stream->selector[0], "sink_%u");
1374 pad = gst_element_get_static_pad (stream->udpsrc[0], "src");
1375 gst_pad_link (pad, selpad);
1376 gst_object_unref (pad);
1377 gst_object_unref (selpad);
1379 selpad = gst_element_get_request_pad (stream->selector[0], "sink_%u");
1380 pad = gst_element_get_static_pad (stream->appsrc[0], "src");
1381 gst_pad_link (pad, selpad);
1382 gst_object_unref (pad);
1383 gst_object_unref (selpad);
1385 /* make selector for the RTCP receivers */
1386 stream->selector[1] = gst_element_factory_make ("funnel", NULL);
1387 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->selector[1]);
1389 pad = gst_element_get_static_pad (stream->selector[1], "src");
1390 gst_pad_link (pad, stream->recv_rtcp_sink);
1391 gst_object_unref (pad);
1393 selpad = gst_element_get_request_pad (stream->selector[1], "sink_%u");
1394 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
1395 gst_pad_link (pad, selpad);
1396 gst_object_unref (pad);
1397 gst_object_unref (selpad);
1399 selpad = gst_element_get_request_pad (stream->selector[1], "sink_%u");
1400 pad = gst_element_get_static_pad (stream->appsrc[1], "src");
1401 gst_pad_link (pad, selpad);
1402 gst_object_unref (pad);
1403 gst_object_unref (selpad);
1405 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1407 gst_element_set_state (stream->udpsrc[0], GST_STATE_PLAYING);
1408 gst_element_set_state (stream->udpsrc[1], GST_STATE_PLAYING);
1409 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
1410 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
1412 /* be notified of caps changes */
1413 stream->caps_sig = g_signal_connect (stream->send_rtp_sink, "notify::caps",
1414 (GCallback) caps_notify, stream);
1416 stream->prepared = TRUE;
1423 GST_WARNING ("failed to link stream %d", idx);
1429 unlock_streams (GstRTSPMedia * media)
1433 /* unlock the udp src elements */
1434 n_streams = gst_rtsp_media_n_streams (media);
1435 for (i = 0; i < n_streams; i++) {
1436 GstRTSPMediaStream *stream;
1438 stream = gst_rtsp_media_get_stream (media, i);
1440 gst_element_set_locked_state (stream->udpsrc[0], FALSE);
1441 gst_element_set_locked_state (stream->udpsrc[1], FALSE);
1446 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1448 g_mutex_lock (media->lock);
1449 /* never overwrite the error status */
1450 if (media->status != GST_RTSP_MEDIA_STATUS_ERROR)
1451 media->status = status;
1452 GST_DEBUG ("setting new status to %d", status);
1453 g_cond_broadcast (media->cond);
1454 g_mutex_unlock (media->lock);
1457 static GstRTSPMediaStatus
1458 gst_rtsp_media_get_status (GstRTSPMedia * media)
1460 GstRTSPMediaStatus result;
1463 g_mutex_lock (media->lock);
1464 g_get_current_time (&timeout);
1465 g_time_val_add (&timeout, 20 * G_USEC_PER_SEC);
1466 /* while we are preparing, wait */
1467 while (media->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1468 GST_DEBUG ("waiting for status change");
1469 if (!g_cond_timed_wait (media->cond, media->lock, &timeout)) {
1470 GST_DEBUG ("timeout, assuming error status");
1471 media->status = GST_RTSP_MEDIA_STATUS_ERROR;
1474 /* could be success or error */
1475 result = media->status;
1476 GST_DEBUG ("got status %d", result);
1477 g_mutex_unlock (media->lock);
1483 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1485 GstMessageType type;
1487 type = GST_MESSAGE_TYPE (message);
1490 case GST_MESSAGE_STATE_CHANGED:
1492 case GST_MESSAGE_BUFFERING:
1496 gst_message_parse_buffering (message, &percent);
1498 /* no state management needed for live pipelines */
1502 if (percent == 100) {
1503 /* a 100% message means buffering is done */
1504 media->buffering = FALSE;
1505 /* if the desired state is playing, go back */
1506 if (media->target_state == GST_STATE_PLAYING) {
1507 GST_INFO ("Buffering done, setting pipeline to PLAYING");
1508 gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1510 GST_INFO ("Buffering done");
1513 /* buffering busy */
1514 if (media->buffering == FALSE) {
1515 if (media->target_state == GST_STATE_PLAYING) {
1516 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1517 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1518 gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1520 GST_INFO ("Buffering ...");
1523 media->buffering = TRUE;
1527 case GST_MESSAGE_LATENCY:
1529 gst_bin_recalculate_latency (GST_BIN_CAST (media->pipeline));
1532 case GST_MESSAGE_ERROR:
1537 gst_message_parse_error (message, &gerror, &debug);
1538 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1539 g_error_free (gerror);
1542 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1545 case GST_MESSAGE_WARNING:
1550 gst_message_parse_warning (message, &gerror, &debug);
1551 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1552 g_error_free (gerror);
1556 case GST_MESSAGE_ELEMENT:
1558 case GST_MESSAGE_STREAM_STATUS:
1560 case GST_MESSAGE_ASYNC_DONE:
1561 if (!media->adding) {
1562 /* when we are dynamically adding pads, the addition of the udpsrc will
1563 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1564 * wait for the final ASYNC_DONE after everything prerolled */
1565 GST_INFO ("%p: got ASYNC_DONE", media);
1566 collect_media_stats (media);
1568 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1570 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1573 case GST_MESSAGE_EOS:
1574 GST_INFO ("%p: got EOS", media);
1575 if (media->eos_pending) {
1576 GST_DEBUG ("shutting down after EOS");
1577 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1578 media->eos_pending = FALSE;
1579 g_object_unref (media);
1583 GST_INFO ("%p: got message type %s", media,
1584 gst_message_type_get_name (type));
1591 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1593 GstRTSPMediaClass *klass;
1596 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1598 if (klass->handle_message)
1599 ret = klass->handle_message (media, message);
1606 /* called from streaming threads */
1608 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1610 GstRTSPMediaStream *stream;
1614 i = media->streams->len + 1;
1616 GST_INFO ("pad added %s:%s, stream %d", GST_DEBUG_PAD_NAME (pad), i);
1618 stream = g_new0 (GstRTSPMediaStream, 1);
1619 stream->payloader = element;
1621 name = g_strdup_printf ("dynpay%d", i);
1623 media->adding = TRUE;
1625 /* ghost the pad of the payloader to the element */
1626 stream->srcpad = gst_ghost_pad_new (name, pad);
1627 gst_pad_set_active (stream->srcpad, TRUE);
1628 gst_element_add_pad (media->element, stream->srcpad);
1631 /* add stream now */
1632 g_array_append_val (media->streams, stream);
1634 setup_stream (stream, i, media);
1636 for (i = 0; i < 2; i++) {
1637 gst_element_set_state (stream->udpsink[i], GST_STATE_PAUSED);
1638 gst_element_set_state (stream->appsink[i], GST_STATE_PAUSED);
1639 gst_element_set_state (stream->tee[i], GST_STATE_PAUSED);
1640 gst_element_set_state (stream->selector[i], GST_STATE_PAUSED);
1641 gst_element_set_state (stream->appsrc[i], GST_STATE_PAUSED);
1643 media->adding = FALSE;
1647 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1649 GST_INFO ("no more pads");
1650 if (media->fakesink) {
1651 gst_object_ref (media->fakesink);
1652 gst_bin_remove (GST_BIN (media->pipeline), media->fakesink);
1653 gst_element_set_state (media->fakesink, GST_STATE_NULL);
1654 gst_object_unref (media->fakesink);
1655 media->fakesink = NULL;
1656 GST_INFO ("removed fakesink");
1661 * gst_rtsp_media_prepare:
1662 * @media: a #GstRTSPMedia
1664 * Prepare @media for streaming. This function will create the pipeline and
1665 * other objects to manage the streaming.
1667 * It will preroll the pipeline and collect vital information about the streams
1668 * such as the duration.
1670 * Returns: %TRUE on success.
1673 gst_rtsp_media_prepare (GstRTSPMedia * media)
1675 GstStateChangeReturn ret;
1676 GstRTSPMediaStatus status;
1678 GstRTSPMediaClass *klass;
1682 if (media->status == GST_RTSP_MEDIA_STATUS_PREPARED)
1685 if (!media->reusable && media->reused)
1688 media->rtpbin = gst_element_factory_make ("rtpbin", NULL);
1689 if (media->rtpbin == NULL)
1692 GST_INFO ("preparing media %p", media);
1694 /* reset some variables */
1695 media->is_live = FALSE;
1696 media->seekable = FALSE;
1697 media->buffering = FALSE;
1698 /* we're preparing now */
1699 media->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1701 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (media->pipeline));
1703 /* add the pipeline bus to our custom mainloop */
1704 media->source = gst_bus_create_watch (bus);
1705 gst_object_unref (bus);
1707 g_source_set_callback (media->source, (GSourceFunc) bus_message, media, NULL);
1709 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1710 media->id = g_source_attach (media->source, klass->context);
1712 /* add stuff to the bin */
1713 gst_bin_add (GST_BIN (media->pipeline), media->rtpbin);
1715 /* link streams we already have, other streams might appear when we have
1716 * dynamic elements */
1717 n_streams = gst_rtsp_media_n_streams (media);
1718 for (i = 0; i < n_streams; i++) {
1719 GstRTSPMediaStream *stream;
1721 stream = gst_rtsp_media_get_stream (media, i);
1723 setup_stream (stream, i, media);
1726 for (walk = media->dynamic; walk; walk = g_list_next (walk)) {
1727 GstElement *elem = walk->data;
1729 GST_INFO ("adding callbacks for dynamic element %p", elem);
1731 g_signal_connect (elem, "pad-added", (GCallback) pad_added_cb, media);
1732 g_signal_connect (elem, "no-more-pads", (GCallback) no_more_pads_cb, media);
1734 /* we add a fakesink here in order to make the state change async. We remove
1735 * the fakesink again in the no-more-pads callback. */
1736 media->fakesink = gst_element_factory_make ("fakesink", "fakesink");
1737 gst_bin_add (GST_BIN (media->pipeline), media->fakesink);
1740 GST_INFO ("setting pipeline to PAUSED for media %p", media);
1741 /* first go to PAUSED */
1742 ret = gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1743 media->target_state = GST_STATE_PAUSED;
1746 case GST_STATE_CHANGE_SUCCESS:
1747 GST_INFO ("SUCCESS state change for media %p", media);
1748 media->seekable = TRUE;
1750 case GST_STATE_CHANGE_ASYNC:
1751 GST_INFO ("ASYNC state change for media %p", media);
1752 media->seekable = TRUE;
1754 case GST_STATE_CHANGE_NO_PREROLL:
1755 /* we need to go to PLAYING */
1756 GST_INFO ("NO_PREROLL state change: live media %p", media);
1757 /* FIXME we disable seeking for live streams for now. We should perform a
1758 * seeking query in preroll instead and do a seeking query. */
1759 media->seekable = FALSE;
1760 media->is_live = TRUE;
1761 ret = gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1762 if (ret == GST_STATE_CHANGE_FAILURE)
1765 case GST_STATE_CHANGE_FAILURE:
1769 /* now wait for all pads to be prerolled */
1770 status = gst_rtsp_media_get_status (media);
1771 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1774 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
1776 GST_INFO ("object %p is prerolled", media);
1788 GST_WARNING ("can not reuse media %p", media);
1793 GST_WARNING ("no rtpbin element");
1794 g_warning ("failed to create element 'rtpbin', check your installation");
1799 GST_WARNING ("failed to preroll pipeline");
1800 unlock_streams (media);
1801 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1802 gst_rtsp_media_unprepare (media);
1808 * gst_rtsp_media_unprepare:
1809 * @media: a #GstRTSPMedia
1811 * Unprepare @media. After this call, the media should be prepared again before
1812 * it can be used again. If the media is set to be non-reusable, a new instance
1815 * Returns: %TRUE on success.
1818 gst_rtsp_media_unprepare (GstRTSPMedia * media)
1820 GstRTSPMediaClass *klass;
1823 if (media->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
1826 GST_INFO ("unprepare media %p", media);
1827 media->target_state = GST_STATE_NULL;
1829 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1830 if (klass->unprepare)
1831 success = klass->unprepare (media);
1835 media->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
1836 media->reused = TRUE;
1838 /* when the media is not reusable, this will effectively unref the media and
1840 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
1846 default_unprepare (GstRTSPMedia * media)
1848 if (media->eos_shutdown) {
1849 GST_DEBUG ("sending EOS for shutdown");
1850 /* ref so that we don't disappear */
1851 g_object_ref (media);
1852 media->eos_pending = TRUE;
1853 gst_element_send_event (media->pipeline, gst_event_new_eos ());
1854 /* we need to go to playing again for the EOS to propagate, normally in this
1855 * state, nothing is receiving data from us anymore so this is ok. */
1856 gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1858 GST_DEBUG ("shutting down");
1859 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1865 add_udp_destination (GstRTSPMedia * media, GstRTSPMediaStream * stream,
1866 gchar * dest, gint min, gint max)
1868 GST_INFO ("adding %s:%d-%d", dest, min, max);
1869 g_signal_emit_by_name (stream->udpsink[0], "add", dest, min, NULL);
1870 g_signal_emit_by_name (stream->udpsink[1], "add", dest, max, NULL);
1874 remove_udp_destination (GstRTSPMedia * media, GstRTSPMediaStream * stream,
1875 gchar * dest, gint min, gint max)
1877 GST_INFO ("removing %s:%d-%d", dest, min, max);
1878 g_signal_emit_by_name (stream->udpsink[0], "remove", dest, min, NULL);
1879 g_signal_emit_by_name (stream->udpsink[1], "remove", dest, max, NULL);
1883 * gst_rtsp_media_set_state:
1884 * @media: a #GstRTSPMedia
1885 * @state: the target state of the media
1886 * @transports: a #GArray of #GstRTSPMediaTrans pointers
1888 * Set the state of @media to @state and for the transports in @transports.
1890 * Returns: %TRUE on success.
1893 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
1894 GArray * transports)
1897 gboolean add, remove, do_state;
1900 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1901 g_return_val_if_fail (transports != NULL, FALSE);
1903 /* NULL and READY are the same */
1904 if (state == GST_STATE_READY)
1905 state = GST_STATE_NULL;
1907 add = remove = FALSE;
1909 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
1913 case GST_STATE_NULL:
1914 /* unlock the streams so that they follow the state changes from now on */
1915 unlock_streams (media);
1917 case GST_STATE_PAUSED:
1918 /* we're going from PLAYING to PAUSED, READY or NULL, remove */
1919 if (media->target_state == GST_STATE_PLAYING)
1922 case GST_STATE_PLAYING:
1923 /* we're going to PLAYING, add */
1929 old_active = media->active;
1931 for (i = 0; i < transports->len; i++) {
1932 GstRTSPMediaTrans *tr;
1933 GstRTSPMediaStream *stream;
1934 GstRTSPTransport *trans;
1936 /* we need a non-NULL entry in the array */
1937 tr = g_array_index (transports, GstRTSPMediaTrans *, i);
1941 /* we need a transport */
1942 if (!(trans = tr->transport))
1945 /* get the stream and add the destinations */
1946 stream = gst_rtsp_media_get_stream (media, tr->idx);
1947 switch (trans->lower_transport) {
1948 case GST_RTSP_LOWER_TRANS_UDP:
1949 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1954 dest = trans->destination;
1955 if (trans->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1956 min = trans->port.min;
1957 max = trans->port.max;
1959 min = trans->client_port.min;
1960 max = trans->client_port.max;
1963 if (add && !tr->active) {
1964 add_udp_destination (media, stream, dest, min, max);
1965 stream->transports = g_list_prepend (stream->transports, tr);
1968 } else if (remove && tr->active) {
1969 remove_udp_destination (media, stream, dest, min, max);
1970 stream->transports = g_list_remove (stream->transports, tr);
1976 case GST_RTSP_LOWER_TRANS_TCP:
1977 if (add && !tr->active) {
1978 GST_INFO ("adding TCP %s", trans->destination);
1979 stream->transports = g_list_prepend (stream->transports, tr);
1982 } else if (remove && tr->active) {
1983 GST_INFO ("removing TCP %s", trans->destination);
1984 stream->transports = g_list_remove (stream->transports, tr);
1990 GST_INFO ("Unknown transport %d", trans->lower_transport);
1995 /* we just added the first media, do the playing state change */
1996 if (old_active == 0 && add)
1998 /* if we have no more active media, do the downward state changes */
1999 else if (media->active == 0)
2004 GST_INFO ("state %d active %d media %p do_state %d", state, media->active,
2007 if (media->target_state != state) {
2009 if (state == GST_STATE_NULL) {
2010 gst_rtsp_media_unprepare (media);
2012 GST_INFO ("state %s media %p", gst_element_state_get_name (state),
2014 media->target_state = state;
2015 gst_element_set_state (media->pipeline, state);
2018 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
2022 /* remember where we are */
2023 if (state == GST_STATE_PAUSED || old_active != media->active)
2024 collect_media_stats (media);
2030 * gst_rtsp_media_remove_elements:
2031 * @media: a #GstRTSPMedia
2033 * Remove all elements and the pipeline controlled by @media.
2036 gst_rtsp_media_remove_elements (GstRTSPMedia * media)
2040 unlock_streams (media);
2042 for (i = 0; i < media->streams->len; i++) {
2043 GstRTSPMediaStream *stream;
2045 GST_INFO ("Removing elements of stream %d from pipeline", i);
2047 stream = g_array_index (media->streams, GstRTSPMediaStream *, i);
2049 gst_pad_unlink (stream->srcpad, stream->send_rtp_sink);
2051 g_signal_handler_disconnect (stream->send_rtp_sink, stream->caps_sig);
2053 for (j = 0; j < 2; j++) {
2054 gst_element_set_state (stream->udpsrc[j], GST_STATE_NULL);
2055 gst_element_set_state (stream->udpsink[j], GST_STATE_NULL);
2056 gst_element_set_state (stream->appsrc[j], GST_STATE_NULL);
2057 gst_element_set_state (stream->appsink[j], GST_STATE_NULL);
2058 gst_element_set_state (stream->tee[j], GST_STATE_NULL);
2059 gst_element_set_state (stream->selector[j], GST_STATE_NULL);
2061 gst_bin_remove (GST_BIN (media->pipeline), stream->udpsrc[j]);
2062 gst_bin_remove (GST_BIN (media->pipeline), stream->udpsink[j]);
2063 gst_bin_remove (GST_BIN (media->pipeline), stream->appsrc[j]);
2064 gst_bin_remove (GST_BIN (media->pipeline), stream->appsink[j]);
2065 gst_bin_remove (GST_BIN (media->pipeline), stream->tee[j]);
2066 gst_bin_remove (GST_BIN (media->pipeline), stream->selector[j]);
2069 gst_caps_unref (stream->caps);
2070 stream->caps = NULL;
2071 gst_rtsp_media_stream_free (stream);
2073 g_array_remove_range (media->streams, 0, media->streams->len);
2075 gst_element_set_state (media->rtpbin, GST_STATE_NULL);
2076 gst_bin_remove (GST_BIN (media->pipeline), media->rtpbin);
2078 gst_object_unref (media->pipeline);
2079 media->pipeline = NULL;