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, sockfd;
863 /* Start with random port */
867 host = "udp://[::0]";
869 host = "udp://0.0.0.0";
871 /* try to allocate 2 UDP ports, the RTP port should be an even
872 * number and the RTCP port should be the next (uneven) port */
874 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
876 goto no_udp_protocol;
877 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, NULL);
879 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
880 if (ret == GST_STATE_CHANGE_FAILURE) {
886 gst_element_set_state (udpsrc0, GST_STATE_NULL);
887 gst_object_unref (udpsrc0);
891 goto no_udp_protocol;
894 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
896 /* check if port is even */
897 if ((tmp_rtp & 1) != 0) {
898 /* port not even, close and allocate another */
902 gst_element_set_state (udpsrc0, GST_STATE_NULL);
903 gst_object_unref (udpsrc0);
909 /* allocate port+1 for RTCP now */
910 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
912 goto no_udp_rtcp_protocol;
915 tmp_rtcp = tmp_rtp + 1;
916 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, NULL);
918 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
919 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
920 if (ret == GST_STATE_CHANGE_FAILURE) {
925 gst_element_set_state (udpsrc0, GST_STATE_NULL);
926 gst_object_unref (udpsrc0);
928 gst_element_set_state (udpsrc1, GST_STATE_NULL);
929 gst_object_unref (udpsrc1);
935 /* all fine, do port check */
936 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
937 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
939 /* this should not happen... */
940 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
943 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
945 goto no_udp_protocol;
947 g_object_get (G_OBJECT (udpsrc0), "sock", &sockfd, NULL);
948 g_object_set (G_OBJECT (udpsink0), "sockfd", sockfd, NULL);
949 g_object_set (G_OBJECT (udpsink0), "closefd", FALSE, NULL);
951 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
953 goto no_udp_protocol;
955 if (g_object_class_find_property (G_OBJECT_GET_CLASS (udpsink0),
956 "send-duplicates")) {
957 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
958 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
961 ("old multiudpsink version found without send-duplicates property");
964 if (g_object_class_find_property (G_OBJECT_GET_CLASS (udpsink0),
966 g_object_set (G_OBJECT (udpsink0), "buffer-size", media->buffer_size, NULL);
968 GST_WARNING ("multiudpsink version found without buffer-size property");
971 g_object_get (G_OBJECT (udpsrc1), "sock", &sockfd, NULL);
972 g_object_set (G_OBJECT (udpsink1), "sockfd", sockfd, NULL);
973 g_object_set (G_OBJECT (udpsink1), "closefd", FALSE, NULL);
974 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
975 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
977 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
978 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
979 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
980 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
982 /* we keep these elements, we configure all in configure_transport when the
983 * server told us to really use the UDP ports. */
984 stream->udpsrc[0] = udpsrc0;
985 stream->udpsrc[1] = udpsrc1;
986 stream->udpsink[0] = udpsink0;
987 stream->udpsink[1] = udpsink1;
988 stream->server_port.min = rtpport;
989 stream->server_port.max = rtcpport;
1002 no_udp_rtcp_protocol:
1013 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1014 gst_object_unref (udpsrc0);
1017 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1018 gst_object_unref (udpsrc1);
1021 gst_element_set_state (udpsink0, GST_STATE_NULL);
1022 gst_object_unref (udpsink0);
1025 gst_element_set_state (udpsink1, GST_STATE_NULL);
1026 gst_object_unref (udpsink1);
1032 /* executed from streaming thread */
1034 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPMediaStream * stream)
1037 GstCaps *newcaps, *oldcaps;
1039 newcaps = gst_pad_get_current_caps (pad);
1041 oldcaps = stream->caps;
1042 stream->caps = newcaps;
1045 gst_caps_unref (oldcaps);
1047 capsstr = gst_caps_to_string (newcaps);
1048 GST_INFO ("stream %p received caps %p, %s", stream, newcaps, capsstr);
1053 dump_structure (const GstStructure * s)
1057 sstr = gst_structure_to_string (s);
1058 GST_INFO ("structure: %s", sstr);
1062 static GstRTSPMediaTrans *
1063 find_transport (GstRTSPMediaStream * stream, const gchar * rtcp_from)
1066 GstRTSPMediaTrans *result = NULL;
1071 if (rtcp_from == NULL)
1074 tmp = g_strrstr (rtcp_from, ":");
1078 port = atoi (tmp + 1);
1079 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1081 GST_INFO ("finding %s:%d", dest, port);
1083 for (walk = stream->transports; walk; walk = g_list_next (walk)) {
1084 GstRTSPMediaTrans *trans = walk->data;
1087 min = trans->transport->client_port.min;
1088 max = trans->transport->client_port.max;
1090 if ((strcmp (trans->transport->destination, dest) == 0) && (min == port
1102 on_new_ssrc (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1104 GstStructure *stats;
1105 GstRTSPMediaTrans *trans;
1107 GST_INFO ("%p: new source %p", stream, source);
1109 /* see if we have a stream to match with the origin of the RTCP packet */
1110 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1111 if (trans == NULL) {
1112 g_object_get (source, "stats", &stats, NULL);
1114 const gchar *rtcp_from;
1116 dump_structure (stats);
1118 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1119 if ((trans = find_transport (stream, rtcp_from))) {
1120 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1123 /* keep ref to the source */
1124 trans->rtpsource = source;
1126 g_object_set_qdata (source, ssrc_stream_map_key, trans);
1128 gst_structure_free (stats);
1131 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1136 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1138 GST_INFO ("%p: new SDES %p", stream, source);
1142 on_ssrc_active (GObject * session, GObject * source,
1143 GstRTSPMediaStream * stream)
1145 GstRTSPMediaTrans *trans;
1147 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1149 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1151 if (trans && trans->keep_alive)
1152 trans->keep_alive (trans->ka_user_data);
1156 GstStructure *stats;
1157 g_object_get (source, "stats", &stats, NULL);
1159 dump_structure (stats);
1160 gst_structure_free (stats);
1167 on_bye_ssrc (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1169 GST_INFO ("%p: source %p bye", stream, source);
1173 on_bye_timeout (GObject * session, GObject * source,
1174 GstRTSPMediaStream * stream)
1176 GstRTSPMediaTrans *trans;
1178 GST_INFO ("%p: source %p bye timeout", stream, source);
1180 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1181 trans->rtpsource = NULL;
1182 trans->timeout = TRUE;
1187 on_timeout (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1189 GstRTSPMediaTrans *trans;
1191 GST_INFO ("%p: source %p timeout", stream, source);
1193 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1194 trans->rtpsource = NULL;
1195 trans->timeout = TRUE;
1199 static GstFlowReturn
1200 handle_new_sample (GstAppSink * sink, gpointer user_data)
1205 GstRTSPMediaStream *stream;
1207 sample = gst_app_sink_pull_sample (sink);
1211 stream = (GstRTSPMediaStream *) user_data;
1212 buffer = gst_sample_get_buffer (sample);
1214 for (walk = stream->transports; walk; walk = g_list_next (walk)) {
1215 GstRTSPMediaTrans *tr = (GstRTSPMediaTrans *) walk->data;
1217 if (GST_ELEMENT_CAST (sink) == stream->appsink[0]) {
1219 tr->send_rtp (buffer, tr->transport->interleaved.min, tr->user_data);
1222 tr->send_rtcp (buffer, tr->transport->interleaved.max, tr->user_data);
1225 gst_sample_unref (sample);
1230 static GstAppSinkCallbacks sink_cb = {
1231 NULL, /* not interested in EOS */
1232 NULL, /* not interested in preroll samples */
1236 /* prepare the pipeline objects to handle @stream in @media */
1238 setup_stream (GstRTSPMediaStream * stream, guint idx, GstRTSPMedia * media)
1241 GstPad *pad, *teepad, *queuepad, *selpad;
1242 GstPadLinkReturn ret;
1245 /* allocate udp ports, we will have 4 of them, 2 for receiving RTP/RTCP and 2
1246 * for sending RTP/RTCP. The sender and receiver ports are shared between the
1248 if (!alloc_udp_ports (media, stream))
1251 /* add the ports to the pipeline */
1252 for (i = 0; i < 2; i++) {
1253 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->udpsink[i]);
1254 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->udpsrc[i]);
1257 /* create elements for the TCP transfer */
1258 for (i = 0; i < 2; i++) {
1259 stream->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
1260 stream->appqueue[i] = gst_element_factory_make ("queue", NULL);
1261 stream->appsink[i] = gst_element_factory_make ("appsink", NULL);
1262 g_object_set (stream->appsink[i], "async", FALSE, "sync", FALSE, NULL);
1263 g_object_set (stream->appsink[i], "emit-signals", FALSE, NULL);
1264 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->appqueue[i]);
1265 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->appsink[i]);
1266 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->appsrc[i]);
1267 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (stream->appsink[i]),
1268 &sink_cb, stream, NULL);
1271 /* hook up the stream to the RTP session elements. */
1272 name = g_strdup_printf ("send_rtp_sink_%u", idx);
1273 stream->send_rtp_sink = gst_element_get_request_pad (media->rtpbin, name);
1275 name = g_strdup_printf ("send_rtp_src_%u", idx);
1276 stream->send_rtp_src = gst_element_get_static_pad (media->rtpbin, name);
1278 name = g_strdup_printf ("send_rtcp_src_%u", idx);
1279 stream->send_rtcp_src = gst_element_get_request_pad (media->rtpbin, name);
1281 name = g_strdup_printf ("recv_rtcp_sink_%u", idx);
1282 stream->recv_rtcp_sink = gst_element_get_request_pad (media->rtpbin, name);
1284 name = g_strdup_printf ("recv_rtp_sink_%u", idx);
1285 stream->recv_rtp_sink = gst_element_get_request_pad (media->rtpbin, name);
1288 /* get the session */
1289 g_signal_emit_by_name (media->rtpbin, "get-internal-session", idx,
1292 g_signal_connect (stream->session, "on-new-ssrc", (GCallback) on_new_ssrc,
1294 g_signal_connect (stream->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
1296 g_signal_connect (stream->session, "on-ssrc-active",
1297 (GCallback) on_ssrc_active, stream);
1298 g_signal_connect (stream->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
1300 g_signal_connect (stream->session, "on-bye-timeout",
1301 (GCallback) on_bye_timeout, stream);
1302 g_signal_connect (stream->session, "on-timeout", (GCallback) on_timeout,
1305 /* link the RTP pad to the session manager */
1306 ret = gst_pad_link (stream->srcpad, stream->send_rtp_sink);
1307 if (ret != GST_PAD_LINK_OK)
1310 /* make tee for RTP and link to stream */
1311 stream->tee[0] = gst_element_factory_make ("tee", NULL);
1312 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->tee[0]);
1314 pad = gst_element_get_static_pad (stream->tee[0], "sink");
1315 gst_pad_link (stream->send_rtp_src, pad);
1316 gst_object_unref (pad);
1318 /* link RTP sink, we're pretty sure this will work. */
1319 teepad = gst_element_get_request_pad (stream->tee[0], "src_%u");
1320 pad = gst_element_get_static_pad (stream->udpsink[0], "sink");
1321 gst_pad_link (teepad, pad);
1322 gst_object_unref (pad);
1323 gst_object_unref (teepad);
1325 teepad = gst_element_get_request_pad (stream->tee[0], "src_%u");
1326 pad = gst_element_get_static_pad (stream->appqueue[0], "sink");
1327 gst_pad_link (teepad, pad);
1328 gst_object_unref (pad);
1329 gst_object_unref (teepad);
1331 queuepad = gst_element_get_static_pad (stream->appqueue[0], "src");
1332 pad = gst_element_get_static_pad (stream->appsink[0], "sink");
1333 gst_pad_link (queuepad, pad);
1334 gst_object_unref (pad);
1335 gst_object_unref (queuepad);
1337 /* make tee for RTCP */
1338 stream->tee[1] = gst_element_factory_make ("tee", NULL);
1339 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->tee[1]);
1341 pad = gst_element_get_static_pad (stream->tee[1], "sink");
1342 gst_pad_link (stream->send_rtcp_src, pad);
1343 gst_object_unref (pad);
1345 /* link RTCP elements */
1346 teepad = gst_element_get_request_pad (stream->tee[1], "src_%u");
1347 pad = gst_element_get_static_pad (stream->udpsink[1], "sink");
1348 gst_pad_link (teepad, pad);
1349 gst_object_unref (pad);
1350 gst_object_unref (teepad);
1352 teepad = gst_element_get_request_pad (stream->tee[1], "src_%u");
1353 pad = gst_element_get_static_pad (stream->appqueue[1], "sink");
1354 gst_pad_link (teepad, pad);
1355 gst_object_unref (pad);
1356 gst_object_unref (teepad);
1358 queuepad = gst_element_get_static_pad (stream->appqueue[1], "src");
1359 pad = gst_element_get_static_pad (stream->appsink[1], "sink");
1360 gst_pad_link (queuepad, pad);
1361 gst_object_unref (pad);
1362 gst_object_unref (queuepad);
1364 /* make selector for the RTP receivers */
1365 stream->selector[0] = gst_element_factory_make ("funnel", NULL);
1366 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->selector[0]);
1368 pad = gst_element_get_static_pad (stream->selector[0], "src");
1369 gst_pad_link (pad, stream->recv_rtp_sink);
1370 gst_object_unref (pad);
1372 selpad = gst_element_get_request_pad (stream->selector[0], "sink_%u");
1373 pad = gst_element_get_static_pad (stream->udpsrc[0], "src");
1374 gst_pad_link (pad, selpad);
1375 gst_object_unref (pad);
1376 gst_object_unref (selpad);
1378 selpad = gst_element_get_request_pad (stream->selector[0], "sink_%u");
1379 pad = gst_element_get_static_pad (stream->appsrc[0], "src");
1380 gst_pad_link (pad, selpad);
1381 gst_object_unref (pad);
1382 gst_object_unref (selpad);
1384 /* make selector for the RTCP receivers */
1385 stream->selector[1] = gst_element_factory_make ("funnel", NULL);
1386 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->selector[1]);
1388 pad = gst_element_get_static_pad (stream->selector[1], "src");
1389 gst_pad_link (pad, stream->recv_rtcp_sink);
1390 gst_object_unref (pad);
1392 selpad = gst_element_get_request_pad (stream->selector[1], "sink_%u");
1393 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
1394 gst_pad_link (pad, selpad);
1395 gst_object_unref (pad);
1396 gst_object_unref (selpad);
1398 selpad = gst_element_get_request_pad (stream->selector[1], "sink_%u");
1399 pad = gst_element_get_static_pad (stream->appsrc[1], "src");
1400 gst_pad_link (pad, selpad);
1401 gst_object_unref (pad);
1402 gst_object_unref (selpad);
1404 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1406 gst_element_set_state (stream->udpsrc[0], GST_STATE_PLAYING);
1407 gst_element_set_state (stream->udpsrc[1], GST_STATE_PLAYING);
1408 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
1409 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
1411 /* be notified of caps changes */
1412 stream->caps_sig = g_signal_connect (stream->send_rtp_sink, "notify::caps",
1413 (GCallback) caps_notify, stream);
1415 stream->prepared = TRUE;
1422 GST_WARNING ("failed to link stream %d", idx);
1428 unlock_streams (GstRTSPMedia * media)
1432 /* unlock the udp src elements */
1433 n_streams = gst_rtsp_media_n_streams (media);
1434 for (i = 0; i < n_streams; i++) {
1435 GstRTSPMediaStream *stream;
1437 stream = gst_rtsp_media_get_stream (media, i);
1439 gst_element_set_locked_state (stream->udpsrc[0], FALSE);
1440 gst_element_set_locked_state (stream->udpsrc[1], FALSE);
1445 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1447 g_mutex_lock (media->lock);
1448 /* never overwrite the error status */
1449 if (media->status != GST_RTSP_MEDIA_STATUS_ERROR)
1450 media->status = status;
1451 GST_DEBUG ("setting new status to %d", status);
1452 g_cond_broadcast (media->cond);
1453 g_mutex_unlock (media->lock);
1456 static GstRTSPMediaStatus
1457 gst_rtsp_media_get_status (GstRTSPMedia * media)
1459 GstRTSPMediaStatus result;
1462 g_mutex_lock (media->lock);
1463 g_get_current_time (&timeout);
1464 g_time_val_add (&timeout, 20 * G_USEC_PER_SEC);
1465 /* while we are preparing, wait */
1466 while (media->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1467 GST_DEBUG ("waiting for status change");
1468 if (!g_cond_timed_wait (media->cond, media->lock, &timeout)) {
1469 GST_DEBUG ("timeout, assuming error status");
1470 media->status = GST_RTSP_MEDIA_STATUS_ERROR;
1473 /* could be success or error */
1474 result = media->status;
1475 GST_DEBUG ("got status %d", result);
1476 g_mutex_unlock (media->lock);
1482 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1484 GstMessageType type;
1486 type = GST_MESSAGE_TYPE (message);
1489 case GST_MESSAGE_STATE_CHANGED:
1491 case GST_MESSAGE_BUFFERING:
1495 gst_message_parse_buffering (message, &percent);
1497 /* no state management needed for live pipelines */
1501 if (percent == 100) {
1502 /* a 100% message means buffering is done */
1503 media->buffering = FALSE;
1504 /* if the desired state is playing, go back */
1505 if (media->target_state == GST_STATE_PLAYING) {
1506 GST_INFO ("Buffering done, setting pipeline to PLAYING");
1507 gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1509 GST_INFO ("Buffering done");
1512 /* buffering busy */
1513 if (media->buffering == FALSE) {
1514 if (media->target_state == GST_STATE_PLAYING) {
1515 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1516 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1517 gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1519 GST_INFO ("Buffering ...");
1522 media->buffering = TRUE;
1526 case GST_MESSAGE_LATENCY:
1528 gst_bin_recalculate_latency (GST_BIN_CAST (media->pipeline));
1531 case GST_MESSAGE_ERROR:
1536 gst_message_parse_error (message, &gerror, &debug);
1537 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1538 g_error_free (gerror);
1541 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1544 case GST_MESSAGE_WARNING:
1549 gst_message_parse_warning (message, &gerror, &debug);
1550 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1551 g_error_free (gerror);
1555 case GST_MESSAGE_ELEMENT:
1557 case GST_MESSAGE_STREAM_STATUS:
1559 case GST_MESSAGE_ASYNC_DONE:
1560 if (!media->adding) {
1561 /* when we are dynamically adding pads, the addition of the udpsrc will
1562 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1563 * wait for the final ASYNC_DONE after everything prerolled */
1564 GST_INFO ("%p: got ASYNC_DONE", media);
1565 collect_media_stats (media);
1567 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1569 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1572 case GST_MESSAGE_EOS:
1573 GST_INFO ("%p: got EOS", media);
1574 if (media->eos_pending) {
1575 GST_DEBUG ("shutting down after EOS");
1576 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1577 media->eos_pending = FALSE;
1578 g_object_unref (media);
1582 GST_INFO ("%p: got message type %s", media,
1583 gst_message_type_get_name (type));
1590 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1592 GstRTSPMediaClass *klass;
1595 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1597 if (klass->handle_message)
1598 ret = klass->handle_message (media, message);
1605 /* called from streaming threads */
1607 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1609 GstRTSPMediaStream *stream;
1613 i = media->streams->len + 1;
1615 GST_INFO ("pad added %s:%s, stream %d", GST_DEBUG_PAD_NAME (pad), i);
1617 stream = g_new0 (GstRTSPMediaStream, 1);
1618 stream->payloader = element;
1620 name = g_strdup_printf ("dynpay%d", i);
1622 media->adding = TRUE;
1624 /* ghost the pad of the payloader to the element */
1625 stream->srcpad = gst_ghost_pad_new (name, pad);
1626 gst_pad_set_active (stream->srcpad, TRUE);
1627 gst_element_add_pad (media->element, stream->srcpad);
1630 /* add stream now */
1631 g_array_append_val (media->streams, stream);
1633 setup_stream (stream, i, media);
1635 for (i = 0; i < 2; i++) {
1636 gst_element_set_state (stream->udpsink[i], GST_STATE_PAUSED);
1637 gst_element_set_state (stream->appsink[i], GST_STATE_PAUSED);
1638 gst_element_set_state (stream->tee[i], GST_STATE_PAUSED);
1639 gst_element_set_state (stream->selector[i], GST_STATE_PAUSED);
1640 gst_element_set_state (stream->appsrc[i], GST_STATE_PAUSED);
1642 media->adding = FALSE;
1646 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1648 GST_INFO ("no more pads");
1649 if (media->fakesink) {
1650 gst_object_ref (media->fakesink);
1651 gst_bin_remove (GST_BIN (media->pipeline), media->fakesink);
1652 gst_element_set_state (media->fakesink, GST_STATE_NULL);
1653 gst_object_unref (media->fakesink);
1654 media->fakesink = NULL;
1655 GST_INFO ("removed fakesink");
1660 * gst_rtsp_media_prepare:
1661 * @media: a #GstRTSPMedia
1663 * Prepare @media for streaming. This function will create the pipeline and
1664 * other objects to manage the streaming.
1666 * It will preroll the pipeline and collect vital information about the streams
1667 * such as the duration.
1669 * Returns: %TRUE on success.
1672 gst_rtsp_media_prepare (GstRTSPMedia * media)
1674 GstStateChangeReturn ret;
1675 GstRTSPMediaStatus status;
1677 GstRTSPMediaClass *klass;
1681 if (media->status == GST_RTSP_MEDIA_STATUS_PREPARED)
1684 if (!media->reusable && media->reused)
1687 media->rtpbin = gst_element_factory_make ("rtpbin", NULL);
1688 if (media->rtpbin == NULL)
1691 GST_INFO ("preparing media %p", media);
1693 /* reset some variables */
1694 media->is_live = FALSE;
1695 media->seekable = FALSE;
1696 media->buffering = FALSE;
1697 /* we're preparing now */
1698 media->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1700 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (media->pipeline));
1702 /* add the pipeline bus to our custom mainloop */
1703 media->source = gst_bus_create_watch (bus);
1704 gst_object_unref (bus);
1706 g_source_set_callback (media->source, (GSourceFunc) bus_message, media, NULL);
1708 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1709 media->id = g_source_attach (media->source, klass->context);
1711 /* add stuff to the bin */
1712 gst_bin_add (GST_BIN (media->pipeline), media->rtpbin);
1714 /* link streams we already have, other streams might appear when we have
1715 * dynamic elements */
1716 n_streams = gst_rtsp_media_n_streams (media);
1717 for (i = 0; i < n_streams; i++) {
1718 GstRTSPMediaStream *stream;
1720 stream = gst_rtsp_media_get_stream (media, i);
1722 setup_stream (stream, i, media);
1725 for (walk = media->dynamic; walk; walk = g_list_next (walk)) {
1726 GstElement *elem = walk->data;
1728 GST_INFO ("adding callbacks for dynamic element %p", elem);
1730 g_signal_connect (elem, "pad-added", (GCallback) pad_added_cb, media);
1731 g_signal_connect (elem, "no-more-pads", (GCallback) no_more_pads_cb, media);
1733 /* we add a fakesink here in order to make the state change async. We remove
1734 * the fakesink again in the no-more-pads callback. */
1735 media->fakesink = gst_element_factory_make ("fakesink", "fakesink");
1736 gst_bin_add (GST_BIN (media->pipeline), media->fakesink);
1739 GST_INFO ("setting pipeline to PAUSED for media %p", media);
1740 /* first go to PAUSED */
1741 ret = gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1742 media->target_state = GST_STATE_PAUSED;
1745 case GST_STATE_CHANGE_SUCCESS:
1746 GST_INFO ("SUCCESS state change for media %p", media);
1747 media->seekable = TRUE;
1749 case GST_STATE_CHANGE_ASYNC:
1750 GST_INFO ("ASYNC state change for media %p", media);
1751 media->seekable = TRUE;
1753 case GST_STATE_CHANGE_NO_PREROLL:
1754 /* we need to go to PLAYING */
1755 GST_INFO ("NO_PREROLL state change: live media %p", media);
1756 /* FIXME we disable seeking for live streams for now. We should perform a
1757 * seeking query in preroll instead and do a seeking query. */
1758 media->seekable = FALSE;
1759 media->is_live = TRUE;
1760 ret = gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1761 if (ret == GST_STATE_CHANGE_FAILURE)
1764 case GST_STATE_CHANGE_FAILURE:
1768 /* now wait for all pads to be prerolled */
1769 status = gst_rtsp_media_get_status (media);
1770 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1773 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
1775 GST_INFO ("object %p is prerolled", media);
1787 GST_WARNING ("can not reuse media %p", media);
1792 GST_WARNING ("no rtpbin element");
1793 g_warning ("failed to create element 'rtpbin', check your installation");
1798 GST_WARNING ("failed to preroll pipeline");
1799 unlock_streams (media);
1800 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1801 gst_rtsp_media_unprepare (media);
1807 * gst_rtsp_media_unprepare:
1808 * @media: a #GstRTSPMedia
1810 * Unprepare @media. After this call, the media should be prepared again before
1811 * it can be used again. If the media is set to be non-reusable, a new instance
1814 * Returns: %TRUE on success.
1817 gst_rtsp_media_unprepare (GstRTSPMedia * media)
1819 GstRTSPMediaClass *klass;
1822 if (media->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
1825 GST_INFO ("unprepare media %p", media);
1826 media->target_state = GST_STATE_NULL;
1828 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1829 if (klass->unprepare)
1830 success = klass->unprepare (media);
1834 media->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
1835 media->reused = TRUE;
1837 /* when the media is not reusable, this will effectively unref the media and
1839 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
1845 default_unprepare (GstRTSPMedia * media)
1847 if (media->eos_shutdown) {
1848 GST_DEBUG ("sending EOS for shutdown");
1849 /* ref so that we don't disappear */
1850 g_object_ref (media);
1851 media->eos_pending = TRUE;
1852 gst_element_send_event (media->pipeline, gst_event_new_eos ());
1853 /* we need to go to playing again for the EOS to propagate, normally in this
1854 * state, nothing is receiving data from us anymore so this is ok. */
1855 gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1857 GST_DEBUG ("shutting down");
1858 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1864 add_udp_destination (GstRTSPMedia * media, GstRTSPMediaStream * stream,
1865 gchar * dest, gint min, gint max)
1867 GST_INFO ("adding %s:%d-%d", dest, min, max);
1868 g_signal_emit_by_name (stream->udpsink[0], "add", dest, min, NULL);
1869 g_signal_emit_by_name (stream->udpsink[1], "add", dest, max, NULL);
1873 remove_udp_destination (GstRTSPMedia * media, GstRTSPMediaStream * stream,
1874 gchar * dest, gint min, gint max)
1876 GST_INFO ("removing %s:%d-%d", dest, min, max);
1877 g_signal_emit_by_name (stream->udpsink[0], "remove", dest, min, NULL);
1878 g_signal_emit_by_name (stream->udpsink[1], "remove", dest, max, NULL);
1882 * gst_rtsp_media_set_state:
1883 * @media: a #GstRTSPMedia
1884 * @state: the target state of the media
1885 * @transports: a #GArray of #GstRTSPMediaTrans pointers
1887 * Set the state of @media to @state and for the transports in @transports.
1889 * Returns: %TRUE on success.
1892 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
1893 GArray * transports)
1896 gboolean add, remove, do_state;
1899 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1900 g_return_val_if_fail (transports != NULL, FALSE);
1902 /* NULL and READY are the same */
1903 if (state == GST_STATE_READY)
1904 state = GST_STATE_NULL;
1906 add = remove = FALSE;
1908 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
1912 case GST_STATE_NULL:
1913 /* unlock the streams so that they follow the state changes from now on */
1914 unlock_streams (media);
1916 case GST_STATE_PAUSED:
1917 /* we're going from PLAYING to PAUSED, READY or NULL, remove */
1918 if (media->target_state == GST_STATE_PLAYING)
1921 case GST_STATE_PLAYING:
1922 /* we're going to PLAYING, add */
1928 old_active = media->active;
1930 for (i = 0; i < transports->len; i++) {
1931 GstRTSPMediaTrans *tr;
1932 GstRTSPMediaStream *stream;
1933 GstRTSPTransport *trans;
1935 /* we need a non-NULL entry in the array */
1936 tr = g_array_index (transports, GstRTSPMediaTrans *, i);
1940 /* we need a transport */
1941 if (!(trans = tr->transport))
1944 /* get the stream and add the destinations */
1945 stream = gst_rtsp_media_get_stream (media, tr->idx);
1946 switch (trans->lower_transport) {
1947 case GST_RTSP_LOWER_TRANS_UDP:
1948 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1953 dest = trans->destination;
1954 if (trans->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1955 min = trans->port.min;
1956 max = trans->port.max;
1958 min = trans->client_port.min;
1959 max = trans->client_port.max;
1962 if (add && !tr->active) {
1963 add_udp_destination (media, stream, dest, min, max);
1964 stream->transports = g_list_prepend (stream->transports, tr);
1967 } else if (remove && tr->active) {
1968 remove_udp_destination (media, stream, dest, min, max);
1969 stream->transports = g_list_remove (stream->transports, tr);
1975 case GST_RTSP_LOWER_TRANS_TCP:
1976 if (add && !tr->active) {
1977 GST_INFO ("adding TCP %s", trans->destination);
1978 stream->transports = g_list_prepend (stream->transports, tr);
1981 } else if (remove && tr->active) {
1982 GST_INFO ("removing TCP %s", trans->destination);
1983 stream->transports = g_list_remove (stream->transports, tr);
1989 GST_INFO ("Unknown transport %d", trans->lower_transport);
1994 /* we just added the first media, do the playing state change */
1995 if (old_active == 0 && add)
1997 /* if we have no more active media, do the downward state changes */
1998 else if (media->active == 0)
2003 GST_INFO ("state %d active %d media %p do_state %d", state, media->active,
2006 if (media->target_state != state) {
2008 if (state == GST_STATE_NULL) {
2009 gst_rtsp_media_unprepare (media);
2011 GST_INFO ("state %s media %p", gst_element_state_get_name (state),
2013 media->target_state = state;
2014 gst_element_set_state (media->pipeline, state);
2017 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
2021 /* remember where we are */
2022 if (state == GST_STATE_PAUSED || old_active != media->active)
2023 collect_media_stats (media);
2029 * gst_rtsp_media_remove_elements:
2030 * @media: a #GstRTSPMedia
2032 * Remove all elements and the pipeline controlled by @media.
2035 gst_rtsp_media_remove_elements (GstRTSPMedia * media)
2039 unlock_streams (media);
2041 for (i = 0; i < media->streams->len; i++) {
2042 GstRTSPMediaStream *stream;
2044 GST_INFO ("Removing elements of stream %d from pipeline", i);
2046 stream = g_array_index (media->streams, GstRTSPMediaStream *, i);
2048 gst_pad_unlink (stream->srcpad, stream->send_rtp_sink);
2050 g_signal_handler_disconnect (stream->send_rtp_sink, stream->caps_sig);
2052 for (j = 0; j < 2; j++) {
2053 gst_element_set_state (stream->udpsrc[j], GST_STATE_NULL);
2054 gst_element_set_state (stream->udpsink[j], GST_STATE_NULL);
2055 gst_element_set_state (stream->appsrc[j], GST_STATE_NULL);
2056 gst_element_set_state (stream->appsink[j], GST_STATE_NULL);
2057 gst_element_set_state (stream->tee[j], GST_STATE_NULL);
2058 gst_element_set_state (stream->selector[j], GST_STATE_NULL);
2060 gst_bin_remove (GST_BIN (media->pipeline), stream->udpsrc[j]);
2061 gst_bin_remove (GST_BIN (media->pipeline), stream->udpsink[j]);
2062 gst_bin_remove (GST_BIN (media->pipeline), stream->appsrc[j]);
2063 gst_bin_remove (GST_BIN (media->pipeline), stream->appsink[j]);
2064 gst_bin_remove (GST_BIN (media->pipeline), stream->tee[j]);
2065 gst_bin_remove (GST_BIN (media->pipeline), stream->selector[j]);
2068 gst_caps_unref (stream->caps);
2069 stream->caps = NULL;
2070 gst_rtsp_media_stream_free (stream);
2072 g_array_remove_range (media->streams, 0, media->streams->len);
2074 gst_element_set_state (media->rtpbin, GST_STATE_NULL);
2075 gst_bin_remove (GST_BIN (media->pipeline), media->rtpbin);
2077 gst_object_unref (media->pipeline);
2078 media->pipeline = NULL;