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-funnel.h"
27 #include "rtsp-media.h"
29 #define DEFAULT_SHARED FALSE
30 #define DEFAULT_REUSABLE FALSE
31 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_TCP
32 //#define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP_MCAST
33 #define DEFAULT_EOS_SHUTDOWN FALSE
34 #define DEFAULT_BUFFER_SIZE 0x800000
36 /* define to dump received RTCP packets */
58 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
59 #define GST_CAT_DEFAULT rtsp_media_debug
61 static GQuark ssrc_stream_map_key;
63 static void gst_rtsp_media_get_property (GObject * object, guint propid,
64 GValue * value, GParamSpec * pspec);
65 static void gst_rtsp_media_set_property (GObject * object, guint propid,
66 const GValue * value, GParamSpec * pspec);
67 static void gst_rtsp_media_finalize (GObject * obj);
69 static gpointer do_loop (GstRTSPMediaClass * klass);
70 static gboolean default_handle_message (GstRTSPMedia * media,
71 GstMessage * message);
72 static gboolean default_unprepare (GstRTSPMedia * media);
73 static void unlock_streams (GstRTSPMedia * media);
75 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
77 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
80 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
82 GObjectClass *gobject_class;
85 gobject_class = G_OBJECT_CLASS (klass);
87 gobject_class->get_property = gst_rtsp_media_get_property;
88 gobject_class->set_property = gst_rtsp_media_set_property;
89 gobject_class->finalize = gst_rtsp_media_finalize;
91 g_object_class_install_property (gobject_class, PROP_SHARED,
92 g_param_spec_boolean ("shared", "Shared",
93 "If this media pipeline can be shared", DEFAULT_SHARED,
94 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
96 g_object_class_install_property (gobject_class, PROP_REUSABLE,
97 g_param_spec_boolean ("reusable", "Reusable",
98 "If this media pipeline can be reused after an unprepare",
99 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
101 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
102 g_param_spec_flags ("protocols", "Protocols",
103 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
104 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
106 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
107 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
108 "Send an EOS event to the pipeline before unpreparing",
109 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
111 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
112 g_param_spec_uint ("buffer-size", "Buffer Size",
113 "The kernel UDP buffer size to use", 0, G_MAXUINT,
114 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
116 gst_rtsp_media_signals[SIGNAL_PREPARED] =
117 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
118 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
119 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
121 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
122 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
123 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
124 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
126 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
127 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
128 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
129 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 0, G_TYPE_INT);
131 klass->context = g_main_context_new ();
132 klass->loop = g_main_loop_new (klass->context, TRUE);
134 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
136 klass->thread = g_thread_create ((GThreadFunc) do_loop, klass, TRUE, &error);
138 g_critical ("could not start bus thread: %s", error->message);
140 klass->handle_message = default_handle_message;
141 klass->unprepare = default_unprepare;
143 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
145 gst_element_register (NULL, "rtspfunnel", GST_RANK_NONE, RTSP_TYPE_FUNNEL);
150 gst_rtsp_media_init (GstRTSPMedia * media)
152 media->streams = g_array_new (FALSE, TRUE, sizeof (GstRTSPMediaStream *));
153 media->lock = g_mutex_new ();
154 media->cond = g_cond_new ();
156 media->shared = DEFAULT_SHARED;
157 media->reusable = DEFAULT_REUSABLE;
158 media->protocols = DEFAULT_PROTOCOLS;
159 media->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
160 media->buffer_size = DEFAULT_BUFFER_SIZE;
163 /* FIXME. this should be done in multiudpsink */
172 dest_compare (RTSPDestination * a, RTSPDestination * b)
174 if ((a->min == b->min) && (a->max == b->max)
175 && (strcmp (a->dest, b->dest) == 0))
181 static RTSPDestination *
182 create_destination (const gchar * dest, gint min, gint max)
184 RTSPDestination *res;
186 res = g_slice_new (RTSPDestination);
188 res->dest = g_strdup (dest);
196 free_destination (RTSPDestination * dest)
199 g_slice_free (RTSPDestination, dest);
203 gst_rtsp_media_trans_cleanup (GstRTSPMediaTrans * trans)
205 if (trans->transport) {
206 gst_rtsp_transport_free (trans->transport);
207 trans->transport = NULL;
209 if (trans->rtpsource) {
210 g_object_set_qdata (trans->rtpsource, ssrc_stream_map_key, NULL);
211 trans->rtpsource = NULL;
216 gst_rtsp_media_stream_free (GstRTSPMediaStream * stream)
219 g_object_unref (stream->session);
222 gst_caps_unref (stream->caps);
224 if (stream->send_rtp_sink)
225 gst_object_unref (stream->send_rtp_sink);
226 if (stream->send_rtp_src)
227 gst_object_unref (stream->send_rtp_src);
228 if (stream->send_rtcp_src)
229 gst_object_unref (stream->send_rtcp_src);
230 if (stream->recv_rtcp_sink)
231 gst_object_unref (stream->recv_rtcp_sink);
232 if (stream->recv_rtp_sink)
233 gst_object_unref (stream->recv_rtp_sink);
235 g_list_free (stream->transports);
237 g_list_foreach (stream->destinations, (GFunc) free_destination, NULL);
238 g_list_free (stream->destinations);
244 gst_rtsp_media_finalize (GObject * obj)
249 media = GST_RTSP_MEDIA (obj);
251 GST_INFO ("finalize media %p", media);
253 if (media->pipeline) {
254 unlock_streams (media);
255 gst_element_set_state (media->pipeline, GST_STATE_NULL);
256 gst_object_unref (media->pipeline);
259 for (i = 0; i < media->streams->len; i++) {
260 GstRTSPMediaStream *stream;
262 stream = g_array_index (media->streams, GstRTSPMediaStream *, i);
264 gst_rtsp_media_stream_free (stream);
266 g_array_free (media->streams, TRUE);
268 g_list_foreach (media->dynamic, (GFunc) gst_object_unref, NULL);
269 g_list_free (media->dynamic);
272 g_source_destroy (media->source);
273 g_source_unref (media->source);
275 g_mutex_free (media->lock);
276 g_cond_free (media->cond);
278 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
282 gst_rtsp_media_get_property (GObject * object, guint propid,
283 GValue * value, GParamSpec * pspec)
285 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
289 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
292 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
295 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
297 case PROP_EOS_SHUTDOWN:
298 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
300 case PROP_BUFFER_SIZE:
301 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
304 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
309 gst_rtsp_media_set_property (GObject * object, guint propid,
310 const GValue * value, GParamSpec * pspec)
312 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
316 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
319 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
322 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
324 case PROP_EOS_SHUTDOWN:
325 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
327 case PROP_BUFFER_SIZE:
328 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
331 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
336 do_loop (GstRTSPMediaClass * klass)
338 GST_INFO ("enter mainloop");
339 g_main_loop_run (klass->loop);
340 GST_INFO ("exit mainloop");
346 collect_media_stats (GstRTSPMedia * media)
349 gint64 position, duration;
351 media->range.unit = GST_RTSP_RANGE_NPT;
353 if (media->is_live) {
354 media->range.min.type = GST_RTSP_TIME_NOW;
355 media->range.min.seconds = -1;
356 media->range.max.type = GST_RTSP_TIME_END;
357 media->range.max.seconds = -1;
359 /* get the position */
360 format = GST_FORMAT_TIME;
361 if (!gst_element_query_position (media->pipeline, &format, &position)) {
362 GST_INFO ("position query failed");
366 /* get the duration */
367 format = GST_FORMAT_TIME;
368 if (!gst_element_query_duration (media->pipeline, &format, &duration)) {
369 GST_INFO ("duration query failed");
373 GST_INFO ("stats: position %" GST_TIME_FORMAT ", duration %"
374 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (duration));
376 if (position == -1) {
377 media->range.min.type = GST_RTSP_TIME_NOW;
378 media->range.min.seconds = -1;
380 media->range.min.type = GST_RTSP_TIME_SECONDS;
381 media->range.min.seconds = ((gdouble) position) / GST_SECOND;
383 if (duration == -1) {
384 media->range.max.type = GST_RTSP_TIME_END;
385 media->range.max.seconds = -1;
387 media->range.max.type = GST_RTSP_TIME_SECONDS;
388 media->range.max.seconds = ((gdouble) duration) / GST_SECOND;
394 * gst_rtsp_media_new:
396 * Create a new #GstRTSPMedia instance. The #GstRTSPMedia object contains the
397 * element to produde RTP data for one or more related (audio/video/..)
400 * Returns: a new #GstRTSPMedia object.
403 gst_rtsp_media_new (void)
405 GstRTSPMedia *result;
407 result = g_object_new (GST_TYPE_RTSP_MEDIA, NULL);
413 * gst_rtsp_media_set_shared:
414 * @media: a #GstRTSPMedia
415 * @shared: the new value
417 * Set or unset if the pipeline for @media can be shared will multiple clients.
418 * When @shared is %TRUE, client requests for this media will share the media
422 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
424 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
426 media->shared = shared;
430 * gst_rtsp_media_is_shared:
431 * @media: a #GstRTSPMedia
433 * Check if the pipeline for @media can be shared between multiple clients.
435 * Returns: %TRUE if the media can be shared between clients.
438 gst_rtsp_media_is_shared (GstRTSPMedia * media)
440 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
442 return media->shared;
446 * gst_rtsp_media_set_reusable:
447 * @media: a #GstRTSPMedia
448 * @reusable: the new value
450 * Set or unset if the pipeline for @media can be reused after the pipeline has
454 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
456 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
458 media->reusable = reusable;
462 * gst_rtsp_media_is_reusable:
463 * @media: a #GstRTSPMedia
465 * Check if the pipeline for @media can be reused after an unprepare.
467 * Returns: %TRUE if the media can be reused
470 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
472 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
474 return media->reusable;
478 * gst_rtsp_media_set_protocols:
479 * @media: a #GstRTSPMedia
480 * @protocols: the new flags
482 * Configure the allowed lower transport for @media.
485 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
487 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
489 media->protocols = protocols;
493 * gst_rtsp_media_get_protocols:
494 * @media: a #GstRTSPMedia
496 * Get the allowed protocols of @media.
498 * Returns: a #GstRTSPLowerTrans
501 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
503 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
504 GST_RTSP_LOWER_TRANS_UNKNOWN);
506 return media->protocols;
510 * gst_rtsp_media_set_eos_shutdown:
511 * @media: a #GstRTSPMedia
512 * @eos_shutdown: the new value
514 * Set or unset if an EOS event will be sent to the pipeline for @media before
518 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
520 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
522 media->eos_shutdown = eos_shutdown;
526 * gst_rtsp_media_is_eos_shutdown:
527 * @media: a #GstRTSPMedia
529 * Check if the pipeline for @media will send an EOS down the pipeline before
532 * Returns: %TRUE if the media will send EOS before unpreparing.
535 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
537 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
539 return media->eos_shutdown;
543 * gst_rtsp_media_set_buffer_size:
544 * @media: a #GstRTSPMedia
545 * @size: the new value
547 * Set the kernel UDP buffer size.
550 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
552 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
554 media->buffer_size = size;
558 * gst_rtsp_media_get_buffer_size:
559 * @media: a #GstRTSPMedia
561 * Get the kernel UDP buffer size.
563 * Returns: the kernel UDP buffer size.
566 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
568 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
570 return media->buffer_size;
574 * gst_rtsp_media_set_auth:
575 * @media: a #GstRTSPMedia
576 * @auth: a #GstRTSPAuth
578 * configure @auth to be used as the authentication manager of @media.
581 gst_rtsp_media_set_auth (GstRTSPMedia * media, GstRTSPAuth * auth)
585 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
594 g_object_unref (old);
599 * gst_rtsp_media_get_auth:
600 * @media: a #GstRTSPMedia
602 * Get the #GstRTSPAuth used as the authentication manager of @media.
604 * Returns: the #GstRTSPAuth of @media. g_object_unref() after
608 gst_rtsp_media_get_auth (GstRTSPMedia * media)
612 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
614 if ((result = media->auth))
615 g_object_ref (result);
622 * gst_rtsp_media_n_streams:
623 * @media: a #GstRTSPMedia
625 * Get the number of streams in this media.
627 * Returns: The number of streams.
630 gst_rtsp_media_n_streams (GstRTSPMedia * media)
632 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
634 return media->streams->len;
638 * gst_rtsp_media_get_stream:
639 * @media: a #GstRTSPMedia
640 * @idx: the stream index
642 * Retrieve the stream with index @idx from @media.
644 * Returns: the #GstRTSPMediaStream at index @idx or %NULL when a stream with
645 * that index did not exist.
648 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
650 GstRTSPMediaStream *res;
652 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
654 if (idx < media->streams->len)
655 res = g_array_index (media->streams, GstRTSPMediaStream *, idx);
663 * gst_rtsp_media_get_range_string:
664 * @media: a #GstRTSPMedia
665 * @play: for the PLAY request
667 * Get the current range as a string.
669 * Returns: The range as a string, g_free() after usage.
672 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play)
675 GstRTSPTimeRange range;
678 range = media->range;
680 if (!play && media->active > 0) {
681 range.min.type = GST_RTSP_TIME_NOW;
682 range.min.seconds = -1;
685 result = gst_rtsp_range_to_string (&range);
691 * gst_rtsp_media_seek:
692 * @media: a #GstRTSPMedia
693 * @range: a #GstRTSPTimeRange
695 * Seek the pipeline to @range.
697 * Returns: %TRUE on success.
700 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
705 GstSeekType start_type, stop_type;
707 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
708 g_return_val_if_fail (range != NULL, FALSE);
710 if (range->unit != GST_RTSP_RANGE_NPT)
713 /* depends on the current playing state of the pipeline. We might need to
714 * queue this until we get EOS. */
715 flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_KEY_UNIT;
717 start_type = stop_type = GST_SEEK_TYPE_NONE;
719 switch (range->min.type) {
720 case GST_RTSP_TIME_NOW:
723 case GST_RTSP_TIME_SECONDS:
724 /* only seek when something changed */
725 if (media->range.min.seconds == range->min.seconds) {
728 start = range->min.seconds * GST_SECOND;
729 start_type = GST_SEEK_TYPE_SET;
732 case GST_RTSP_TIME_END:
736 switch (range->max.type) {
737 case GST_RTSP_TIME_SECONDS:
738 /* only seek when something changed */
739 if (media->range.max.seconds == range->max.seconds) {
742 stop = range->max.seconds * GST_SECOND;
743 stop_type = GST_SEEK_TYPE_SET;
746 case GST_RTSP_TIME_END:
748 stop_type = GST_SEEK_TYPE_SET;
750 case GST_RTSP_TIME_NOW:
755 if (start != -1 || stop != -1) {
756 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
757 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
759 res = gst_element_seek (media->pipeline, 1.0, GST_FORMAT_TIME,
760 flags, start_type, start, stop_type, stop);
762 /* and block for the seek to complete */
763 GST_INFO ("done seeking %d", res);
764 gst_element_get_state (media->pipeline, NULL, NULL, -1);
765 GST_INFO ("prerolled again");
767 collect_media_stats (media);
769 GST_INFO ("no seek needed");
778 GST_WARNING ("seek unit %d not supported", range->unit);
783 GST_WARNING ("weird range type %d not supported", range->min.type);
789 * gst_rtsp_media_stream_rtp:
790 * @stream: a #GstRTSPMediaStream
791 * @buffer: a #GstBuffer
793 * Handle an RTP buffer for the stream. This method is usually called when a
794 * message has been received from a client using the TCP transport.
796 * This function takes ownership of @buffer.
798 * Returns: a GstFlowReturn.
801 gst_rtsp_media_stream_rtp (GstRTSPMediaStream * stream, GstBuffer * buffer)
805 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (stream->appsrc[0]), buffer);
811 * gst_rtsp_media_stream_rtcp:
812 * @stream: a #GstRTSPMediaStream
813 * @buffer: a #GstBuffer
815 * Handle an RTCP buffer for the stream. This method is usually called when a
816 * message has been received from a client using the TCP transport.
818 * This function takes ownership of @buffer.
820 * Returns: a GstFlowReturn.
823 gst_rtsp_media_stream_rtcp (GstRTSPMediaStream * stream, GstBuffer * buffer)
827 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (stream->appsrc[1]), buffer);
832 /* Allocate the udp ports and sockets */
834 alloc_udp_ports (GstRTSPMedia * media, GstRTSPMediaStream * stream)
836 GstStateChangeReturn ret;
837 GstElement *udpsrc0, *udpsrc1;
838 GstElement *udpsink0, *udpsink1;
839 gint tmp_rtp, tmp_rtcp;
841 gint rtpport, rtcpport, sockfd;
850 /* Start with random port */
854 host = "udp://[::0]";
856 host = "udp://0.0.0.0";
858 /* try to allocate 2 UDP ports, the RTP port should be an even
859 * number and the RTCP port should be the next (uneven) port */
861 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
863 goto no_udp_protocol;
864 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, NULL);
866 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
867 if (ret == GST_STATE_CHANGE_FAILURE) {
873 gst_element_set_state (udpsrc0, GST_STATE_NULL);
874 gst_object_unref (udpsrc0);
878 goto no_udp_protocol;
881 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
883 /* check if port is even */
884 if ((tmp_rtp & 1) != 0) {
885 /* port not even, close and allocate another */
889 gst_element_set_state (udpsrc0, GST_STATE_NULL);
890 gst_object_unref (udpsrc0);
896 /* allocate port+1 for RTCP now */
897 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
899 goto no_udp_rtcp_protocol;
902 tmp_rtcp = tmp_rtp + 1;
903 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, NULL);
905 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
906 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
907 if (ret == GST_STATE_CHANGE_FAILURE) {
912 gst_element_set_state (udpsrc0, GST_STATE_NULL);
913 gst_object_unref (udpsrc0);
915 gst_element_set_state (udpsrc1, GST_STATE_NULL);
916 gst_object_unref (udpsrc1);
922 /* all fine, do port check */
923 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
924 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
926 /* this should not happen... */
927 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
930 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
932 goto no_udp_protocol;
934 g_object_get (G_OBJECT (udpsrc0), "sock", &sockfd, NULL);
935 g_object_set (G_OBJECT (udpsink0), "sockfd", sockfd, NULL);
936 g_object_set (G_OBJECT (udpsink0), "closefd", FALSE, NULL);
938 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
940 goto no_udp_protocol;
942 if (g_object_class_find_property (G_OBJECT_GET_CLASS (udpsink0),
943 "send-duplicates")) {
944 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
945 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
946 stream->filter_duplicates = FALSE;
948 GST_WARNING ("multiudpsink version found without send-duplicates property");
949 stream->filter_duplicates = TRUE;
952 if (g_object_class_find_property (G_OBJECT_GET_CLASS (udpsink0),
954 g_object_set (G_OBJECT (udpsink0), "buffer-size", media->buffer_size, NULL);
956 GST_WARNING ("multiudpsink version found without buffer-size property");
959 g_object_get (G_OBJECT (udpsrc1), "sock", &sockfd, NULL);
960 g_object_set (G_OBJECT (udpsink1), "sockfd", sockfd, NULL);
961 g_object_set (G_OBJECT (udpsink1), "closefd", FALSE, NULL);
962 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
963 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
965 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
966 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
967 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
968 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
970 /* we keep these elements, we configure all in configure_transport when the
971 * server told us to really use the UDP ports. */
972 stream->udpsrc[0] = udpsrc0;
973 stream->udpsrc[1] = udpsrc1;
974 stream->udpsink[0] = udpsink0;
975 stream->udpsink[1] = udpsink1;
976 stream->server_port.min = rtpport;
977 stream->server_port.max = rtcpport;
990 no_udp_rtcp_protocol:
1001 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1002 gst_object_unref (udpsrc0);
1005 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1006 gst_object_unref (udpsrc1);
1009 gst_element_set_state (udpsink0, GST_STATE_NULL);
1010 gst_object_unref (udpsink0);
1013 gst_element_set_state (udpsink1, GST_STATE_NULL);
1014 gst_object_unref (udpsink1);
1020 /* executed from streaming thread */
1022 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPMediaStream * stream)
1025 GstCaps *newcaps, *oldcaps;
1027 if ((newcaps = GST_PAD_CAPS (pad)))
1028 gst_caps_ref (newcaps);
1030 oldcaps = stream->caps;
1031 stream->caps = newcaps;
1034 gst_caps_unref (oldcaps);
1036 capsstr = gst_caps_to_string (newcaps);
1037 GST_INFO ("stream %p received caps %p, %s", stream, newcaps, capsstr);
1042 dump_structure (const GstStructure * s)
1046 sstr = gst_structure_to_string (s);
1047 GST_INFO ("structure: %s", sstr);
1051 static GstRTSPMediaTrans *
1052 find_transport (GstRTSPMediaStream * stream, const gchar * rtcp_from)
1055 GstRTSPMediaTrans *result = NULL;
1060 if (rtcp_from == NULL)
1063 tmp = g_strrstr (rtcp_from, ":");
1067 port = atoi (tmp + 1);
1068 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1070 GST_INFO ("finding %s:%d", dest, port);
1072 for (walk = stream->transports; walk; walk = g_list_next (walk)) {
1073 GstRTSPMediaTrans *trans = walk->data;
1076 min = trans->transport->client_port.min;
1077 max = trans->transport->client_port.max;
1079 if ((strcmp (trans->transport->destination, dest) == 0) && (min == port
1091 on_new_ssrc (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1093 GstStructure *stats;
1094 GstRTSPMediaTrans *trans;
1096 GST_INFO ("%p: new source %p", stream, source);
1098 /* see if we have a stream to match with the origin of the RTCP packet */
1099 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1100 if (trans == NULL) {
1101 g_object_get (source, "stats", &stats, NULL);
1103 const gchar *rtcp_from;
1105 dump_structure (stats);
1107 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1108 if ((trans = find_transport (stream, rtcp_from))) {
1109 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1112 /* keep ref to the source */
1113 trans->rtpsource = source;
1115 g_object_set_qdata (source, ssrc_stream_map_key, trans);
1117 gst_structure_free (stats);
1120 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1125 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1127 GST_INFO ("%p: new SDES %p", stream, source);
1131 on_ssrc_active (GObject * session, GObject * source,
1132 GstRTSPMediaStream * stream)
1134 GstRTSPMediaTrans *trans;
1136 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1138 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1140 if (trans && trans->keep_alive)
1141 trans->keep_alive (trans->ka_user_data);
1145 GstStructure *stats;
1146 g_object_get (source, "stats", &stats, NULL);
1148 dump_structure (stats);
1149 gst_structure_free (stats);
1156 on_bye_ssrc (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1158 GST_INFO ("%p: source %p bye", stream, source);
1162 on_bye_timeout (GObject * session, GObject * source,
1163 GstRTSPMediaStream * stream)
1165 GstRTSPMediaTrans *trans;
1167 GST_INFO ("%p: source %p bye timeout", stream, source);
1169 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1170 trans->rtpsource = NULL;
1171 trans->timeout = TRUE;
1176 on_timeout (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1178 GstRTSPMediaTrans *trans;
1180 GST_INFO ("%p: source %p timeout", stream, source);
1182 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1183 trans->rtpsource = NULL;
1184 trans->timeout = TRUE;
1188 static GstFlowReturn
1189 handle_new_buffer (GstAppSink * sink, gpointer user_data)
1193 GstRTSPMediaStream *stream;
1195 buffer = gst_app_sink_pull_buffer (sink);
1199 stream = (GstRTSPMediaStream *) user_data;
1201 for (walk = stream->transports; walk; walk = g_list_next (walk)) {
1202 GstRTSPMediaTrans *tr = (GstRTSPMediaTrans *) walk->data;
1204 if (GST_ELEMENT_CAST (sink) == stream->appsink[0]) {
1206 tr->send_rtp (buffer, tr->transport->interleaved.min, tr->user_data);
1209 tr->send_rtcp (buffer, tr->transport->interleaved.max, tr->user_data);
1212 gst_buffer_unref (buffer);
1217 static GstFlowReturn
1218 handle_new_buffer_list (GstAppSink * sink, gpointer user_data)
1221 GstBufferList *blist;
1222 GstRTSPMediaStream *stream;
1224 blist = gst_app_sink_pull_buffer_list (sink);
1228 stream = (GstRTSPMediaStream *) user_data;
1230 for (walk = stream->transports; walk; walk = g_list_next (walk)) {
1231 GstRTSPMediaTrans *tr = (GstRTSPMediaTrans *) walk->data;
1233 if (GST_ELEMENT_CAST (sink) == stream->appsink[0]) {
1234 if (tr->send_rtp_list)
1235 tr->send_rtp_list (blist, tr->transport->interleaved.min,
1238 if (tr->send_rtcp_list)
1239 tr->send_rtcp_list (blist, tr->transport->interleaved.max,
1243 gst_buffer_list_unref (blist);
1248 static GstAppSinkCallbacks sink_cb = {
1249 NULL, /* not interested in EOS */
1250 NULL, /* not interested in preroll buffers */
1252 handle_new_buffer_list
1255 /* prepare the pipeline objects to handle @stream in @media */
1257 setup_stream (GstRTSPMediaStream * stream, guint idx, GstRTSPMedia * media)
1260 GstPad *pad, *teepad, *selpad;
1261 GstPadLinkReturn ret;
1264 /* allocate udp ports, we will have 4 of them, 2 for receiving RTP/RTCP and 2
1265 * for sending RTP/RTCP. The sender and receiver ports are shared between the
1267 if (!alloc_udp_ports (media, stream))
1270 /* add the ports to the pipeline */
1271 for (i = 0; i < 2; i++) {
1272 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->udpsink[i]);
1273 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->udpsrc[i]);
1276 /* create elements for the TCP transfer */
1277 for (i = 0; i < 2; i++) {
1278 stream->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
1279 stream->appsink[i] = gst_element_factory_make ("appsink", NULL);
1280 g_object_set (stream->appsink[i], "async", FALSE, "sync", FALSE, NULL);
1281 g_object_set (stream->appsink[i], "emit-signals", FALSE, NULL);
1282 g_object_set (stream->appsink[i], "preroll-queue-len", 1, NULL);
1283 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->appsink[i]);
1284 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->appsrc[i]);
1285 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (stream->appsink[i]),
1286 &sink_cb, stream, NULL);
1289 /* hook up the stream to the RTP session elements. */
1290 name = g_strdup_printf ("send_rtp_sink_%d", idx);
1291 stream->send_rtp_sink = gst_element_get_request_pad (media->rtpbin, name);
1293 name = g_strdup_printf ("send_rtp_src_%d", idx);
1294 stream->send_rtp_src = gst_element_get_static_pad (media->rtpbin, name);
1296 name = g_strdup_printf ("send_rtcp_src_%d", idx);
1297 stream->send_rtcp_src = gst_element_get_request_pad (media->rtpbin, name);
1299 name = g_strdup_printf ("recv_rtcp_sink_%d", idx);
1300 stream->recv_rtcp_sink = gst_element_get_request_pad (media->rtpbin, name);
1302 name = g_strdup_printf ("recv_rtp_sink_%d", idx);
1303 stream->recv_rtp_sink = gst_element_get_request_pad (media->rtpbin, name);
1306 /* get the session */
1307 g_signal_emit_by_name (media->rtpbin, "get-internal-session", idx,
1310 g_signal_connect (stream->session, "on-new-ssrc", (GCallback) on_new_ssrc,
1312 g_signal_connect (stream->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
1314 g_signal_connect (stream->session, "on-ssrc-active",
1315 (GCallback) on_ssrc_active, stream);
1316 g_signal_connect (stream->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
1318 g_signal_connect (stream->session, "on-bye-timeout",
1319 (GCallback) on_bye_timeout, stream);
1320 g_signal_connect (stream->session, "on-timeout", (GCallback) on_timeout,
1323 /* link the RTP pad to the session manager */
1324 ret = gst_pad_link (stream->srcpad, stream->send_rtp_sink);
1325 if (ret != GST_PAD_LINK_OK)
1328 /* make tee for RTP and link to stream */
1329 stream->tee[0] = gst_element_factory_make ("tee", NULL);
1330 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->tee[0]);
1332 pad = gst_element_get_static_pad (stream->tee[0], "sink");
1333 gst_pad_link (stream->send_rtp_src, pad);
1334 gst_object_unref (pad);
1336 /* link RTP sink, we're pretty sure this will work. */
1337 teepad = gst_element_get_request_pad (stream->tee[0], "src%d");
1338 pad = gst_element_get_static_pad (stream->udpsink[0], "sink");
1339 gst_pad_link (teepad, pad);
1340 gst_object_unref (pad);
1341 gst_object_unref (teepad);
1343 teepad = gst_element_get_request_pad (stream->tee[0], "src%d");
1344 pad = gst_element_get_static_pad (stream->appsink[0], "sink");
1345 gst_pad_link (teepad, pad);
1346 gst_object_unref (pad);
1347 gst_object_unref (teepad);
1349 /* make tee for RTCP */
1350 stream->tee[1] = gst_element_factory_make ("tee", NULL);
1351 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->tee[1]);
1353 pad = gst_element_get_static_pad (stream->tee[1], "sink");
1354 gst_pad_link (stream->send_rtcp_src, pad);
1355 gst_object_unref (pad);
1357 /* link RTCP elements */
1358 teepad = gst_element_get_request_pad (stream->tee[1], "src%d");
1359 pad = gst_element_get_static_pad (stream->udpsink[1], "sink");
1360 gst_pad_link (teepad, pad);
1361 gst_object_unref (pad);
1362 gst_object_unref (teepad);
1364 teepad = gst_element_get_request_pad (stream->tee[1], "src%d");
1365 pad = gst_element_get_static_pad (stream->appsink[1], "sink");
1366 gst_pad_link (teepad, pad);
1367 gst_object_unref (pad);
1368 gst_object_unref (teepad);
1370 /* make selector for the RTP receivers */
1371 stream->selector[0] = gst_element_factory_make ("rtspfunnel", NULL);
1372 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->selector[0]);
1374 pad = gst_element_get_static_pad (stream->selector[0], "src");
1375 gst_pad_link (pad, stream->recv_rtp_sink);
1376 gst_object_unref (pad);
1378 selpad = gst_element_get_request_pad (stream->selector[0], "sink%d");
1379 pad = gst_element_get_static_pad (stream->udpsrc[0], "src");
1380 gst_pad_link (pad, selpad);
1381 gst_object_unref (pad);
1382 gst_object_unref (selpad);
1384 selpad = gst_element_get_request_pad (stream->selector[0], "sink%d");
1385 pad = gst_element_get_static_pad (stream->appsrc[0], "src");
1386 gst_pad_link (pad, selpad);
1387 gst_object_unref (pad);
1388 gst_object_unref (selpad);
1390 /* make selector for the RTCP receivers */
1391 stream->selector[1] = gst_element_factory_make ("rtspfunnel", NULL);
1392 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->selector[1]);
1394 pad = gst_element_get_static_pad (stream->selector[1], "src");
1395 gst_pad_link (pad, stream->recv_rtcp_sink);
1396 gst_object_unref (pad);
1398 selpad = gst_element_get_request_pad (stream->selector[1], "sink%d");
1399 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
1400 gst_pad_link (pad, selpad);
1401 gst_object_unref (pad);
1402 gst_object_unref (selpad);
1404 selpad = gst_element_get_request_pad (stream->selector[1], "sink%d");
1405 pad = gst_element_get_static_pad (stream->appsrc[1], "src");
1406 gst_pad_link (pad, selpad);
1407 gst_object_unref (pad);
1408 gst_object_unref (selpad);
1410 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1412 gst_element_set_state (stream->udpsrc[0], GST_STATE_PLAYING);
1413 gst_element_set_state (stream->udpsrc[1], GST_STATE_PLAYING);
1414 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
1415 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
1417 /* be notified of caps changes */
1418 stream->caps_sig = g_signal_connect (stream->send_rtp_sink, "notify::caps",
1419 (GCallback) caps_notify, stream);
1421 stream->prepared = TRUE;
1428 GST_WARNING ("failed to link stream %d", idx);
1434 unlock_streams (GstRTSPMedia * media)
1438 /* unlock the udp src elements */
1439 n_streams = gst_rtsp_media_n_streams (media);
1440 for (i = 0; i < n_streams; i++) {
1441 GstRTSPMediaStream *stream;
1443 stream = gst_rtsp_media_get_stream (media, i);
1445 gst_element_set_locked_state (stream->udpsrc[0], FALSE);
1446 gst_element_set_locked_state (stream->udpsrc[1], FALSE);
1451 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1453 g_mutex_lock (media->lock);
1454 /* never overwrite the error status */
1455 if (media->status != GST_RTSP_MEDIA_STATUS_ERROR)
1456 media->status = status;
1457 GST_DEBUG ("setting new status to %d", status);
1458 g_cond_broadcast (media->cond);
1459 g_mutex_unlock (media->lock);
1462 static GstRTSPMediaStatus
1463 gst_rtsp_media_get_status (GstRTSPMedia * media)
1465 GstRTSPMediaStatus result;
1468 g_mutex_lock (media->lock);
1469 g_get_current_time (&timeout);
1470 g_time_val_add (&timeout, 20 * G_USEC_PER_SEC);
1471 /* while we are preparing, wait */
1472 while (media->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1473 GST_DEBUG ("waiting for status change");
1474 if (!g_cond_timed_wait (media->cond, media->lock, &timeout)) {
1475 GST_DEBUG ("timeout, assuming error status");
1476 media->status = GST_RTSP_MEDIA_STATUS_ERROR;
1479 /* could be success or error */
1480 result = media->status;
1481 GST_DEBUG ("got status %d", result);
1482 g_mutex_unlock (media->lock);
1488 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1490 GstMessageType type;
1492 type = GST_MESSAGE_TYPE (message);
1495 case GST_MESSAGE_STATE_CHANGED:
1497 case GST_MESSAGE_BUFFERING:
1501 gst_message_parse_buffering (message, &percent);
1503 /* no state management needed for live pipelines */
1507 if (percent == 100) {
1508 /* a 100% message means buffering is done */
1509 media->buffering = FALSE;
1510 /* if the desired state is playing, go back */
1511 if (media->target_state == GST_STATE_PLAYING) {
1512 GST_INFO ("Buffering done, setting pipeline to PLAYING");
1513 gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1515 GST_INFO ("Buffering done");
1518 /* buffering busy */
1519 if (media->buffering == FALSE) {
1520 if (media->target_state == GST_STATE_PLAYING) {
1521 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1522 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1523 gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1525 GST_INFO ("Buffering ...");
1528 media->buffering = TRUE;
1532 case GST_MESSAGE_LATENCY:
1534 gst_bin_recalculate_latency (GST_BIN_CAST (media->pipeline));
1537 case GST_MESSAGE_ERROR:
1542 gst_message_parse_error (message, &gerror, &debug);
1543 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1544 g_error_free (gerror);
1547 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1550 case GST_MESSAGE_WARNING:
1555 gst_message_parse_warning (message, &gerror, &debug);
1556 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1557 g_error_free (gerror);
1561 case GST_MESSAGE_ELEMENT:
1563 case GST_MESSAGE_STREAM_STATUS:
1565 case GST_MESSAGE_ASYNC_DONE:
1566 if (!media->adding) {
1567 /* when we are dynamically adding pads, the addition of the udpsrc will
1568 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1569 * wait for the final ASYNC_DONE after everything prerolled */
1570 GST_INFO ("%p: got ASYNC_DONE", media);
1571 collect_media_stats (media);
1573 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1575 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1578 case GST_MESSAGE_EOS:
1579 GST_INFO ("%p: got EOS", media);
1580 if (media->eos_pending) {
1581 GST_DEBUG ("shutting down after EOS");
1582 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1583 media->eos_pending = FALSE;
1584 g_object_unref (media);
1588 GST_INFO ("%p: got message type %s", media,
1589 gst_message_type_get_name (type));
1596 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1598 GstRTSPMediaClass *klass;
1601 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1603 if (klass->handle_message)
1604 ret = klass->handle_message (media, message);
1611 /* called from streaming threads */
1613 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1615 GstRTSPMediaStream *stream;
1619 i = media->streams->len + 1;
1621 GST_INFO ("pad added %s:%s, stream %d", GST_DEBUG_PAD_NAME (pad), i);
1623 stream = g_new0 (GstRTSPMediaStream, 1);
1624 stream->payloader = element;
1626 name = g_strdup_printf ("dynpay%d", i);
1628 media->adding = TRUE;
1630 /* ghost the pad of the payloader to the element */
1631 stream->srcpad = gst_ghost_pad_new (name, pad);
1632 gst_pad_set_active (stream->srcpad, TRUE);
1633 gst_element_add_pad (media->element, stream->srcpad);
1636 /* add stream now */
1637 g_array_append_val (media->streams, stream);
1639 setup_stream (stream, i, media);
1641 for (i = 0; i < 2; i++) {
1642 gst_element_set_state (stream->udpsink[i], GST_STATE_PAUSED);
1643 gst_element_set_state (stream->appsink[i], GST_STATE_PAUSED);
1644 gst_element_set_state (stream->tee[i], GST_STATE_PAUSED);
1645 gst_element_set_state (stream->selector[i], GST_STATE_PAUSED);
1646 gst_element_set_state (stream->appsrc[i], GST_STATE_PAUSED);
1648 media->adding = FALSE;
1652 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1654 GST_INFO ("no more pads");
1655 if (media->fakesink) {
1656 gst_object_ref (media->fakesink);
1657 gst_bin_remove (GST_BIN (media->pipeline), media->fakesink);
1658 gst_element_set_state (media->fakesink, GST_STATE_NULL);
1659 gst_object_unref (media->fakesink);
1660 media->fakesink = NULL;
1661 GST_INFO ("removed fakesink");
1666 * gst_rtsp_media_prepare:
1667 * @media: a #GstRTSPMedia
1669 * Prepare @media for streaming. This function will create the pipeline and
1670 * other objects to manage the streaming.
1672 * It will preroll the pipeline and collect vital information about the streams
1673 * such as the duration.
1675 * Returns: %TRUE on success.
1678 gst_rtsp_media_prepare (GstRTSPMedia * media)
1680 GstStateChangeReturn ret;
1681 GstRTSPMediaStatus status;
1683 GstRTSPMediaClass *klass;
1687 if (media->status == GST_RTSP_MEDIA_STATUS_PREPARED)
1690 if (!media->reusable && media->reused)
1693 GST_INFO ("preparing media %p", media);
1695 /* reset some variables */
1696 media->is_live = 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 media->rtpbin = gst_element_factory_make ("gstrtpbin", NULL);
1714 /* add stuff to the bin */
1715 gst_bin_add (GST_BIN (media->pipeline), media->rtpbin);
1717 /* link streams we already have, other streams might appear when we have
1718 * dynamic elements */
1719 n_streams = gst_rtsp_media_n_streams (media);
1720 for (i = 0; i < n_streams; i++) {
1721 GstRTSPMediaStream *stream;
1723 stream = gst_rtsp_media_get_stream (media, i);
1725 setup_stream (stream, i, media);
1728 for (walk = media->dynamic; walk; walk = g_list_next (walk)) {
1729 GstElement *elem = walk->data;
1731 GST_INFO ("adding callbacks for dynamic element %p", elem);
1733 g_signal_connect (elem, "pad-added", (GCallback) pad_added_cb, media);
1734 g_signal_connect (elem, "no-more-pads", (GCallback) no_more_pads_cb, media);
1736 /* we add a fakesink here in order to make the state change async. We remove
1737 * the fakesink again in the no-more-pads callback. */
1738 media->fakesink = gst_element_factory_make ("fakesink", "fakesink");
1739 gst_bin_add (GST_BIN (media->pipeline), media->fakesink);
1742 GST_INFO ("setting pipeline to PAUSED for media %p", media);
1743 /* first go to PAUSED */
1744 ret = gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1745 media->target_state = GST_STATE_PAUSED;
1748 case GST_STATE_CHANGE_SUCCESS:
1749 GST_INFO ("SUCCESS state change for media %p", media);
1751 case GST_STATE_CHANGE_ASYNC:
1752 GST_INFO ("ASYNC state change for media %p", media);
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 media->is_live = TRUE;
1758 ret = gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1759 if (ret == GST_STATE_CHANGE_FAILURE)
1762 case GST_STATE_CHANGE_FAILURE:
1766 /* now wait for all pads to be prerolled */
1767 status = gst_rtsp_media_get_status (media);
1768 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1771 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
1773 GST_INFO ("object %p is prerolled", media);
1785 GST_WARNING ("can not reuse media %p", media);
1790 GST_WARNING ("failed to preroll pipeline");
1791 unlock_streams (media);
1792 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1793 gst_rtsp_media_unprepare (media);
1799 * gst_rtsp_media_unprepare:
1800 * @media: a #GstRTSPMedia
1802 * Unprepare @media. After this call, the media should be prepared again before
1803 * it can be used again. If the media is set to be non-reusable, a new instance
1806 * Returns: %TRUE on success.
1809 gst_rtsp_media_unprepare (GstRTSPMedia * media)
1811 GstRTSPMediaClass *klass;
1814 if (media->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
1817 GST_INFO ("unprepare media %p", media);
1818 media->target_state = GST_STATE_NULL;
1820 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1821 if (klass->unprepare)
1822 success = klass->unprepare (media);
1826 media->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
1827 media->reused = TRUE;
1829 /* when the media is not reusable, this will effectively unref the media and
1831 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
1837 default_unprepare (GstRTSPMedia * media)
1839 if (media->eos_shutdown) {
1840 GST_DEBUG ("sending EOS for shutdown");
1841 /* ref so that we don't disappear */
1842 g_object_ref (media);
1843 media->eos_pending = TRUE;
1844 gst_element_send_event (media->pipeline, gst_event_new_eos ());
1845 /* we need to go to playing again for the EOS to propagate, normally in this
1846 * state, nothing is receiving data from us anymore so this is ok. */
1847 gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1849 GST_DEBUG ("shutting down");
1850 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1856 add_udp_destination (GstRTSPMedia * media, GstRTSPMediaStream * stream,
1857 gchar * dest, gint min, gint max)
1859 gboolean do_add = TRUE;
1860 RTSPDestination *ndest;
1862 if (stream->filter_duplicates) {
1863 RTSPDestination fdest;
1870 /* first see if we already added this destination */
1872 g_list_find_custom (stream->destinations, &fdest,
1873 (GCompareFunc) dest_compare);
1875 ndest = (RTSPDestination *) find->data;
1877 GST_INFO ("already streaming to %s:%d-%d with %d clients", dest, min, max,
1885 GST_INFO ("adding %s:%d-%d", dest, min, max);
1886 g_signal_emit_by_name (stream->udpsink[0], "add", dest, min, NULL);
1887 g_signal_emit_by_name (stream->udpsink[1], "add", dest, max, NULL);
1889 if (stream->filter_duplicates) {
1890 ndest = create_destination (dest, min, max);
1891 stream->destinations = g_list_prepend (stream->destinations, ndest);
1897 remove_udp_destination (GstRTSPMedia * media, GstRTSPMediaStream * stream,
1898 gchar * dest, gint min, gint max)
1900 gboolean do_remove = TRUE;
1901 RTSPDestination *ndest = NULL;
1904 if (stream->filter_duplicates) {
1905 RTSPDestination fdest;
1911 /* first see if we already added this destination */
1913 g_list_find_custom (stream->destinations, &fdest,
1914 (GCompareFunc) dest_compare);
1918 ndest = (RTSPDestination *) find->data;
1919 if (--ndest->count > 0) {
1921 GST_INFO ("still streaming to %s:%d-%d with %d clients", dest, min, max,
1927 GST_INFO ("removing %s:%d-%d", dest, min, max);
1928 g_signal_emit_by_name (stream->udpsink[0], "remove", dest, min, NULL);
1929 g_signal_emit_by_name (stream->udpsink[1], "remove", dest, max, NULL);
1931 if (stream->filter_duplicates) {
1932 stream->destinations = g_list_delete_link (stream->destinations, find);
1933 free_destination (ndest);
1939 * gst_rtsp_media_set_state:
1940 * @media: a #GstRTSPMedia
1941 * @state: the target state of the media
1942 * @transports: a #GArray of #GstRTSPMediaTrans pointers
1944 * Set the state of @media to @state and for the transports in @transports.
1946 * Returns: %TRUE on success.
1949 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
1950 GArray * transports)
1953 GstStateChangeReturn ret;
1954 gboolean add, remove, do_state;
1957 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1958 g_return_val_if_fail (transports != NULL, FALSE);
1960 /* NULL and READY are the same */
1961 if (state == GST_STATE_READY)
1962 state = GST_STATE_NULL;
1964 add = remove = FALSE;
1966 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
1970 case GST_STATE_NULL:
1971 /* unlock the streams so that they follow the state changes from now on */
1972 unlock_streams (media);
1974 case GST_STATE_PAUSED:
1975 /* we're going from PLAYING to PAUSED, READY or NULL, remove */
1976 if (media->target_state == GST_STATE_PLAYING)
1979 case GST_STATE_PLAYING:
1980 /* we're going to PLAYING, add */
1986 old_active = media->active;
1988 for (i = 0; i < transports->len; i++) {
1989 GstRTSPMediaTrans *tr;
1990 GstRTSPMediaStream *stream;
1991 GstRTSPTransport *trans;
1993 /* we need a non-NULL entry in the array */
1994 tr = g_array_index (transports, GstRTSPMediaTrans *, i);
1998 /* we need a transport */
1999 if (!(trans = tr->transport))
2002 /* get the stream and add the destinations */
2003 stream = gst_rtsp_media_get_stream (media, tr->idx);
2004 switch (trans->lower_transport) {
2005 case GST_RTSP_LOWER_TRANS_UDP:
2006 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
2011 dest = trans->destination;
2012 if (trans->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2013 min = trans->port.min;
2014 max = trans->port.max;
2016 min = trans->client_port.min;
2017 max = trans->client_port.max;
2020 if (add && !tr->active) {
2021 add_udp_destination (media, stream, dest, min, max);
2022 stream->transports = g_list_prepend (stream->transports, tr);
2025 } else if (remove && tr->active) {
2026 remove_udp_destination (media, stream, dest, min, max);
2027 stream->transports = g_list_remove (stream->transports, tr);
2033 case GST_RTSP_LOWER_TRANS_TCP:
2034 if (add && !tr->active) {
2035 GST_INFO ("adding TCP %s", trans->destination);
2036 stream->transports = g_list_prepend (stream->transports, tr);
2039 } else if (remove && tr->active) {
2040 GST_INFO ("removing TCP %s", trans->destination);
2041 stream->transports = g_list_remove (stream->transports, tr);
2047 GST_INFO ("Unknown transport %d", trans->lower_transport);
2052 /* we just added the first media, do the playing state change */
2053 if (old_active == 0 && add)
2055 /* if we have no more active media, do the downward state changes */
2056 else if (media->active == 0)
2061 GST_INFO ("state %d active %d media %p do_state %d", state, media->active,
2064 if (media->target_state != state) {
2066 if (state == GST_STATE_NULL) {
2067 gst_rtsp_media_unprepare (media);
2069 GST_INFO ("state %s media %p", gst_element_state_get_name (state),
2071 media->target_state = state;
2072 ret = gst_element_set_state (media->pipeline, state);
2075 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
2079 /* remember where we are */
2080 if (state == GST_STATE_PAUSED || old_active != media->active)
2081 collect_media_stats (media);
2087 * gst_rtsp_media_remove_elements:
2088 * @media: a #GstRTSPMedia
2090 * Remove all elements and the pipeline controlled by @media.
2093 gst_rtsp_media_remove_elements (GstRTSPMedia * media)
2097 unlock_streams (media);
2099 for (i = 0; i < media->streams->len; i++) {
2100 GstRTSPMediaStream *stream;
2102 GST_INFO ("Removing elements of stream %d from pipeline", i);
2104 stream = g_array_index (media->streams, GstRTSPMediaStream *, i);
2106 gst_pad_unlink (stream->srcpad, stream->send_rtp_sink);
2108 g_signal_handler_disconnect (stream->send_rtp_sink, stream->caps_sig);
2110 for (j = 0; j < 2; j++) {
2111 gst_element_set_state (stream->udpsrc[j], GST_STATE_NULL);
2112 gst_element_set_state (stream->udpsink[j], GST_STATE_NULL);
2113 gst_element_set_state (stream->appsrc[j], GST_STATE_NULL);
2114 gst_element_set_state (stream->appsink[j], GST_STATE_NULL);
2115 gst_element_set_state (stream->tee[j], GST_STATE_NULL);
2116 gst_element_set_state (stream->selector[j], GST_STATE_NULL);
2118 gst_bin_remove (GST_BIN (media->pipeline), stream->udpsrc[j]);
2119 gst_bin_remove (GST_BIN (media->pipeline), stream->udpsink[j]);
2120 gst_bin_remove (GST_BIN (media->pipeline), stream->appsrc[j]);
2121 gst_bin_remove (GST_BIN (media->pipeline), stream->appsink[j]);
2122 gst_bin_remove (GST_BIN (media->pipeline), stream->tee[j]);
2123 gst_bin_remove (GST_BIN (media->pipeline), stream->selector[j]);
2126 gst_caps_unref (stream->caps);
2127 stream->caps = NULL;
2128 gst_rtsp_media_stream_free (stream);
2130 g_array_remove_range (media->streams, 0, media->streams->len);
2132 gst_element_set_state (media->rtpbin, GST_STATE_NULL);
2133 gst_bin_remove (GST_BIN (media->pipeline), media->rtpbin);
2135 gst_object_unref (media->pipeline);
2136 media->pipeline = NULL;