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
35 /* define to dump received RTCP packets */
56 GST_DEBUG_CATEGORY_EXTERN (rtsp_media_debug);
57 #define GST_CAT_DEFAULT rtsp_media_debug
59 static GQuark ssrc_stream_map_key;
61 static void gst_rtsp_media_get_property (GObject * object, guint propid,
62 GValue * value, GParamSpec * pspec);
63 static void gst_rtsp_media_set_property (GObject * object, guint propid,
64 const GValue * value, GParamSpec * pspec);
65 static void gst_rtsp_media_finalize (GObject * obj);
67 static gpointer do_loop (GstRTSPMediaClass * klass);
68 static gboolean default_handle_message (GstRTSPMedia * media,
69 GstMessage * message);
70 static gboolean default_unprepare (GstRTSPMedia * media);
71 static void unlock_streams (GstRTSPMedia * media);
73 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
75 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
78 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
80 GObjectClass *gobject_class;
83 gobject_class = G_OBJECT_CLASS (klass);
85 gobject_class->get_property = gst_rtsp_media_get_property;
86 gobject_class->set_property = gst_rtsp_media_set_property;
87 gobject_class->finalize = gst_rtsp_media_finalize;
89 g_object_class_install_property (gobject_class, PROP_SHARED,
90 g_param_spec_boolean ("shared", "Shared",
91 "If this media pipeline can be shared", DEFAULT_SHARED,
92 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
94 g_object_class_install_property (gobject_class, PROP_REUSABLE,
95 g_param_spec_boolean ("reusable", "Reusable",
96 "If this media pipeline can be reused after an unprepare",
97 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
99 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
100 g_param_spec_flags ("protocols", "Protocols",
101 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
102 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
104 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
105 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
106 "Send an EOS event to the pipeline before unpreparing",
107 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
109 gst_rtsp_media_signals[SIGNAL_PREPARED] =
110 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
111 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
112 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
114 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
115 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
116 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
117 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
119 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
120 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
121 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
122 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 0, G_TYPE_INT);
124 klass->context = g_main_context_new ();
125 klass->loop = g_main_loop_new (klass->context, TRUE);
127 klass->thread = g_thread_create ((GThreadFunc) do_loop, klass, TRUE, &error);
129 g_critical ("could not start bus thread: %s", error->message);
131 klass->handle_message = default_handle_message;
132 klass->unprepare = default_unprepare;
134 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
136 gst_element_register (NULL, "rtspfunnel", GST_RANK_NONE, RTSP_TYPE_FUNNEL);
140 gst_rtsp_media_init (GstRTSPMedia * media)
142 media->streams = g_array_new (FALSE, TRUE, sizeof (GstRTSPMediaStream *));
143 media->lock = g_mutex_new ();
144 media->cond = g_cond_new ();
146 media->shared = DEFAULT_SHARED;
147 media->reusable = DEFAULT_REUSABLE;
148 media->protocols = DEFAULT_PROTOCOLS;
149 media->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
152 /* FIXME. this should be done in multiudpsink */
161 dest_compare (RTSPDestination * a, RTSPDestination * b)
163 if ((a->min == b->min) && (a->max == b->max)
164 && (strcmp (a->dest, b->dest) == 0))
170 static RTSPDestination *
171 create_destination (const gchar * dest, gint min, gint max)
173 RTSPDestination *res;
175 res = g_slice_new (RTSPDestination);
177 res->dest = g_strdup (dest);
185 free_destination (RTSPDestination * dest)
188 g_slice_free (RTSPDestination, dest);
192 gst_rtsp_media_trans_cleanup (GstRTSPMediaTrans * trans)
194 if (trans->transport) {
195 gst_rtsp_transport_free (trans->transport);
196 trans->transport = NULL;
198 if (trans->rtpsource) {
199 g_object_set_qdata (trans->rtpsource, ssrc_stream_map_key, NULL);
200 trans->rtpsource = NULL;
205 gst_rtsp_media_stream_free (GstRTSPMediaStream * stream)
208 g_object_unref (stream->session);
211 gst_caps_unref (stream->caps);
213 if (stream->send_rtp_sink)
214 gst_object_unref (stream->send_rtp_sink);
215 if (stream->send_rtp_src)
216 gst_object_unref (stream->send_rtp_src);
217 if (stream->send_rtcp_src)
218 gst_object_unref (stream->send_rtcp_src);
219 if (stream->recv_rtcp_sink)
220 gst_object_unref (stream->recv_rtcp_sink);
221 if (stream->recv_rtp_sink)
222 gst_object_unref (stream->recv_rtp_sink);
224 g_list_free (stream->transports);
226 g_list_foreach (stream->destinations, (GFunc) free_destination, NULL);
227 g_list_free (stream->destinations);
233 gst_rtsp_media_finalize (GObject * obj)
238 media = GST_RTSP_MEDIA (obj);
240 GST_INFO ("finalize media %p", media);
242 if (media->pipeline) {
243 unlock_streams (media);
244 gst_element_set_state (media->pipeline, GST_STATE_NULL);
245 gst_object_unref (media->pipeline);
248 for (i = 0; i < media->streams->len; i++) {
249 GstRTSPMediaStream *stream;
251 stream = g_array_index (media->streams, GstRTSPMediaStream *, i);
253 gst_rtsp_media_stream_free (stream);
255 g_array_free (media->streams, TRUE);
257 g_list_foreach (media->dynamic, (GFunc) gst_object_unref, NULL);
258 g_list_free (media->dynamic);
261 g_source_destroy (media->source);
262 g_source_unref (media->source);
264 g_mutex_free (media->lock);
265 g_cond_free (media->cond);
267 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
271 gst_rtsp_media_get_property (GObject * object, guint propid,
272 GValue * value, GParamSpec * pspec)
274 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
278 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
281 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
284 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
286 case PROP_EOS_SHUTDOWN:
287 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
290 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
295 gst_rtsp_media_set_property (GObject * object, guint propid,
296 const GValue * value, GParamSpec * pspec)
298 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
302 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
305 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
308 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
310 case PROP_EOS_SHUTDOWN:
311 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
314 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
319 do_loop (GstRTSPMediaClass * klass)
321 GST_INFO ("enter mainloop");
322 g_main_loop_run (klass->loop);
323 GST_INFO ("exit mainloop");
329 collect_media_stats (GstRTSPMedia * media)
332 gint64 position, duration;
334 media->range.unit = GST_RTSP_RANGE_NPT;
336 if (media->is_live) {
337 media->range.min.type = GST_RTSP_TIME_NOW;
338 media->range.min.seconds = -1;
339 media->range.max.type = GST_RTSP_TIME_END;
340 media->range.max.seconds = -1;
342 /* get the position */
343 format = GST_FORMAT_TIME;
344 if (!gst_element_query_position (media->pipeline, &format, &position)) {
345 GST_INFO ("position query failed");
349 /* get the duration */
350 format = GST_FORMAT_TIME;
351 if (!gst_element_query_duration (media->pipeline, &format, &duration)) {
352 GST_INFO ("duration query failed");
356 GST_INFO ("stats: position %" GST_TIME_FORMAT ", duration %"
357 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (duration));
359 if (position == -1) {
360 media->range.min.type = GST_RTSP_TIME_NOW;
361 media->range.min.seconds = -1;
363 media->range.min.type = GST_RTSP_TIME_SECONDS;
364 media->range.min.seconds = ((gdouble) position) / GST_SECOND;
366 if (duration == -1) {
367 media->range.max.type = GST_RTSP_TIME_END;
368 media->range.max.seconds = -1;
370 media->range.max.type = GST_RTSP_TIME_SECONDS;
371 media->range.max.seconds = ((gdouble) duration) / GST_SECOND;
377 * gst_rtsp_media_new:
379 * Create a new #GstRTSPMedia instance. The #GstRTSPMedia object contains the
380 * element to produde RTP data for one or more related (audio/video/..)
383 * Returns: a new #GstRTSPMedia object.
386 gst_rtsp_media_new (void)
388 GstRTSPMedia *result;
390 result = g_object_new (GST_TYPE_RTSP_MEDIA, NULL);
396 * gst_rtsp_media_set_shared:
397 * @media: a #GstRTSPMedia
398 * @shared: the new value
400 * Set or unset if the pipeline for @media can be shared will multiple clients.
401 * When @shared is %TRUE, client requests for this media will share the media
405 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
407 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
409 media->shared = shared;
413 * gst_rtsp_media_is_shared:
414 * @media: a #GstRTSPMedia
416 * Check if the pipeline for @media can be shared between multiple clients.
418 * Returns: %TRUE if the media can be shared between clients.
421 gst_rtsp_media_is_shared (GstRTSPMedia * media)
423 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
425 return media->shared;
429 * gst_rtsp_media_set_reusable:
430 * @media: a #GstRTSPMedia
431 * @reusable: the new value
433 * Set or unset if the pipeline for @media can be reused after the pipeline has
437 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
439 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
441 media->reusable = reusable;
445 * gst_rtsp_media_is_reusable:
446 * @media: a #GstRTSPMedia
448 * Check if the pipeline for @media can be reused after an unprepare.
450 * Returns: %TRUE if the media can be reused
453 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
455 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
457 return media->reusable;
461 * gst_rtsp_media_set_protocols:
462 * @media: a #GstRTSPMedia
463 * @protocols: the new flags
465 * Configure the allowed lower transport for @media.
468 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
470 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
472 media->protocols = protocols;
476 * gst_rtsp_media_get_protocols:
477 * @media: a #GstRTSPMedia
479 * Get the allowed protocols of @media.
481 * Returns: a #GstRTSPLowerTrans
484 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
486 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
487 GST_RTSP_LOWER_TRANS_UNKNOWN);
489 return media->protocols;
493 * gst_rtsp_media_set_eos_shutdown:
494 * @media: a #GstRTSPMedia
495 * @eos_shutdown: the new value
497 * Set or unset if an EOS event will be sent to the pipeline for @media before
501 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
503 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
505 media->eos_shutdown = eos_shutdown;
509 * gst_rtsp_media_is_eos_shutdown:
510 * @media: a #GstRTSPMedia
512 * Check if the pipeline for @media will send an EOS down the pipeline before
515 * Returns: %TRUE if the media will send EOS before unpreparing.
518 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
520 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
522 return media->eos_shutdown;
526 * gst_rtsp_media_set_auth:
527 * @media: a #GstRTSPMedia
528 * @auth: a #GstRTSPAuth
530 * configure @auth to be used as the authentication manager of @media.
533 gst_rtsp_media_set_auth (GstRTSPMedia * media, GstRTSPAuth * auth)
537 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
546 g_object_unref (old);
551 * gst_rtsp_media_get_auth:
552 * @media: a #GstRTSPMedia
554 * Get the #GstRTSPAuth used as the authentication manager of @media.
556 * Returns: the #GstRTSPAuth of @media. g_object_unref() after
560 gst_rtsp_media_get_auth (GstRTSPMedia * media)
564 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
566 if ((result = media->auth))
567 g_object_ref (result);
574 * gst_rtsp_media_n_streams:
575 * @media: a #GstRTSPMedia
577 * Get the number of streams in this media.
579 * Returns: The number of streams.
582 gst_rtsp_media_n_streams (GstRTSPMedia * media)
584 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
586 return media->streams->len;
590 * gst_rtsp_media_get_stream:
591 * @media: a #GstRTSPMedia
592 * @idx: the stream index
594 * Retrieve the stream with index @idx from @media.
596 * Returns: the #GstRTSPMediaStream at index @idx or %NULL when a stream with
597 * that index did not exist.
600 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
602 GstRTSPMediaStream *res;
604 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
606 if (idx < media->streams->len)
607 res = g_array_index (media->streams, GstRTSPMediaStream *, idx);
615 * gst_rtsp_media_get_range_string:
616 * @media: a #GstRTSPMedia
617 * @play: for the PLAY request
619 * Get the current range as a string.
621 * Returns: The range as a string, g_free() after usage.
624 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play)
627 GstRTSPTimeRange range;
630 range = media->range;
632 if (!play && media->active > 0) {
633 range.min.type = GST_RTSP_TIME_NOW;
634 range.min.seconds = -1;
637 result = gst_rtsp_range_to_string (&range);
643 * gst_rtsp_media_seek:
644 * @media: a #GstRTSPMedia
645 * @range: a #GstRTSPTimeRange
647 * Seek the pipeline to @range.
649 * Returns: %TRUE on success.
652 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
657 GstSeekType start_type, stop_type;
659 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
660 g_return_val_if_fail (range != NULL, FALSE);
662 if (range->unit != GST_RTSP_RANGE_NPT)
665 /* depends on the current playing state of the pipeline. We might need to
666 * queue this until we get EOS. */
667 flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_KEY_UNIT;
669 start_type = stop_type = GST_SEEK_TYPE_NONE;
671 switch (range->min.type) {
672 case GST_RTSP_TIME_NOW:
675 case GST_RTSP_TIME_SECONDS:
676 /* only seek when something changed */
677 if (media->range.min.seconds == range->min.seconds) {
680 start = range->min.seconds * GST_SECOND;
681 start_type = GST_SEEK_TYPE_SET;
684 case GST_RTSP_TIME_END:
688 switch (range->max.type) {
689 case GST_RTSP_TIME_SECONDS:
690 /* only seek when something changed */
691 if (media->range.max.seconds == range->max.seconds) {
694 stop = range->max.seconds * GST_SECOND;
695 stop_type = GST_SEEK_TYPE_SET;
698 case GST_RTSP_TIME_END:
700 stop_type = GST_SEEK_TYPE_SET;
702 case GST_RTSP_TIME_NOW:
707 if (start != -1 || stop != -1) {
708 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
709 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
711 res = gst_element_seek (media->pipeline, 1.0, GST_FORMAT_TIME,
712 flags, start_type, start, stop_type, stop);
714 /* and block for the seek to complete */
715 GST_INFO ("done seeking %d", res);
716 gst_element_get_state (media->pipeline, NULL, NULL, -1);
717 GST_INFO ("prerolled again");
719 collect_media_stats (media);
721 GST_INFO ("no seek needed");
730 GST_WARNING ("seek unit %d not supported", range->unit);
735 GST_WARNING ("weird range type %d not supported", range->min.type);
741 * gst_rtsp_media_stream_rtp:
742 * @stream: a #GstRTSPMediaStream
743 * @buffer: a #GstBuffer
745 * Handle an RTP buffer for the stream. This method is usually called when a
746 * message has been received from a client using the TCP transport.
748 * This function takes ownership of @buffer.
750 * Returns: a GstFlowReturn.
753 gst_rtsp_media_stream_rtp (GstRTSPMediaStream * stream, GstBuffer * buffer)
757 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (stream->appsrc[0]), buffer);
763 * gst_rtsp_media_stream_rtcp:
764 * @stream: a #GstRTSPMediaStream
765 * @buffer: a #GstBuffer
767 * Handle an RTCP buffer for the stream. This method is usually called when a
768 * message has been received from a client using the TCP transport.
770 * This function takes ownership of @buffer.
772 * Returns: a GstFlowReturn.
775 gst_rtsp_media_stream_rtcp (GstRTSPMediaStream * stream, GstBuffer * buffer)
779 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (stream->appsrc[1]), buffer);
784 /* Allocate the udp ports and sockets */
786 alloc_udp_ports (GstRTSPMedia * media, GstRTSPMediaStream * stream)
788 GstStateChangeReturn ret;
789 GstElement *udpsrc0, *udpsrc1;
790 GstElement *udpsink0, *udpsink1;
791 gint tmp_rtp, tmp_rtcp;
793 gint rtpport, rtcpport, sockfd;
802 /* Start with random port */
806 host = "udp://[::0]";
808 host = "udp://0.0.0.0";
810 /* try to allocate 2 UDP ports, the RTP port should be an even
811 * number and the RTCP port should be the next (uneven) port */
813 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
815 goto no_udp_protocol;
816 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, NULL);
818 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
819 if (ret == GST_STATE_CHANGE_FAILURE) {
825 gst_element_set_state (udpsrc0, GST_STATE_NULL);
826 gst_object_unref (udpsrc0);
830 goto no_udp_protocol;
833 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
835 /* check if port is even */
836 if ((tmp_rtp & 1) != 0) {
837 /* port not even, close and allocate another */
841 gst_element_set_state (udpsrc0, GST_STATE_NULL);
842 gst_object_unref (udpsrc0);
848 /* allocate port+1 for RTCP now */
849 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
851 goto no_udp_rtcp_protocol;
854 tmp_rtcp = tmp_rtp + 1;
855 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, NULL);
857 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
858 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
859 if (ret == GST_STATE_CHANGE_FAILURE) {
864 gst_element_set_state (udpsrc0, GST_STATE_NULL);
865 gst_object_unref (udpsrc0);
867 gst_element_set_state (udpsrc1, GST_STATE_NULL);
868 gst_object_unref (udpsrc1);
874 /* all fine, do port check */
875 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
876 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
878 /* this should not happen... */
879 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
882 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
884 goto no_udp_protocol;
886 g_object_get (G_OBJECT (udpsrc0), "sock", &sockfd, NULL);
887 g_object_set (G_OBJECT (udpsink0), "sockfd", sockfd, NULL);
888 g_object_set (G_OBJECT (udpsink0), "closefd", FALSE, NULL);
890 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
892 goto no_udp_protocol;
894 if (g_object_class_find_property (G_OBJECT_GET_CLASS (udpsink0),
895 "send-duplicates")) {
896 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
897 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
898 stream->filter_duplicates = FALSE;
900 GST_WARNING ("multiudpsink version found without send-duplicates property");
901 stream->filter_duplicates = TRUE;
904 if (g_object_class_find_property (G_OBJECT_GET_CLASS (udpsink0),
906 g_object_set (G_OBJECT (udpsink0), "buffer-size", 0x80000, NULL);
908 GST_WARNING ("multiudpsink version found without buffer-size property");
911 g_object_get (G_OBJECT (udpsrc1), "sock", &sockfd, NULL);
912 g_object_set (G_OBJECT (udpsink1), "sockfd", sockfd, NULL);
913 g_object_set (G_OBJECT (udpsink1), "closefd", FALSE, NULL);
914 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
915 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
917 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
918 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
919 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
920 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
922 /* we keep these elements, we configure all in configure_transport when the
923 * server told us to really use the UDP ports. */
924 stream->udpsrc[0] = udpsrc0;
925 stream->udpsrc[1] = udpsrc1;
926 stream->udpsink[0] = udpsink0;
927 stream->udpsink[1] = udpsink1;
928 stream->server_port.min = rtpport;
929 stream->server_port.max = rtcpport;
942 no_udp_rtcp_protocol:
953 gst_element_set_state (udpsrc0, GST_STATE_NULL);
954 gst_object_unref (udpsrc0);
957 gst_element_set_state (udpsrc1, GST_STATE_NULL);
958 gst_object_unref (udpsrc1);
961 gst_element_set_state (udpsink0, GST_STATE_NULL);
962 gst_object_unref (udpsink0);
965 gst_element_set_state (udpsink1, GST_STATE_NULL);
966 gst_object_unref (udpsink1);
972 /* executed from streaming thread */
974 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPMediaStream * stream)
977 GstCaps *newcaps, *oldcaps;
979 if ((newcaps = GST_PAD_CAPS (pad)))
980 gst_caps_ref (newcaps);
982 oldcaps = stream->caps;
983 stream->caps = newcaps;
986 gst_caps_unref (oldcaps);
988 capsstr = gst_caps_to_string (newcaps);
989 GST_INFO ("stream %p received caps %p, %s", stream, newcaps, capsstr);
994 dump_structure (const GstStructure * s)
998 sstr = gst_structure_to_string (s);
999 GST_INFO ("structure: %s", sstr);
1003 static GstRTSPMediaTrans *
1004 find_transport (GstRTSPMediaStream * stream, const gchar * rtcp_from)
1007 GstRTSPMediaTrans *result = NULL;
1012 if (rtcp_from == NULL)
1015 tmp = g_strrstr (rtcp_from, ":");
1019 port = atoi (tmp + 1);
1020 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1022 GST_INFO ("finding %s:%d", dest, port);
1024 for (walk = stream->transports; walk; walk = g_list_next (walk)) {
1025 GstRTSPMediaTrans *trans = walk->data;
1028 min = trans->transport->client_port.min;
1029 max = trans->transport->client_port.max;
1031 if ((strcmp (trans->transport->destination, dest) == 0) && (min == port
1043 on_new_ssrc (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1045 GstStructure *stats;
1046 GstRTSPMediaTrans *trans;
1048 GST_INFO ("%p: new source %p", stream, source);
1050 /* see if we have a stream to match with the origin of the RTCP packet */
1051 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1052 if (trans == NULL) {
1053 g_object_get (source, "stats", &stats, NULL);
1055 const gchar *rtcp_from;
1057 dump_structure (stats);
1059 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1060 if ((trans = find_transport (stream, rtcp_from))) {
1061 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1064 /* keep ref to the source */
1065 trans->rtpsource = source;
1067 g_object_set_qdata (source, ssrc_stream_map_key, trans);
1069 gst_structure_free (stats);
1072 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1077 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1079 GST_INFO ("%p: new SDES %p", stream, source);
1083 on_ssrc_active (GObject * session, GObject * source,
1084 GstRTSPMediaStream * stream)
1086 GstRTSPMediaTrans *trans;
1088 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1090 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1092 if (trans && trans->keep_alive)
1093 trans->keep_alive (trans->ka_user_data);
1097 GstStructure *stats;
1098 g_object_get (source, "stats", &stats, NULL);
1100 dump_structure (stats);
1101 gst_structure_free (stats);
1108 on_bye_ssrc (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1110 GST_INFO ("%p: source %p bye", stream, source);
1114 on_bye_timeout (GObject * session, GObject * source,
1115 GstRTSPMediaStream * stream)
1117 GstRTSPMediaTrans *trans;
1119 GST_INFO ("%p: source %p bye timeout", stream, source);
1121 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1122 trans->rtpsource = NULL;
1123 trans->timeout = TRUE;
1128 on_timeout (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1130 GstRTSPMediaTrans *trans;
1132 GST_INFO ("%p: source %p timeout", stream, source);
1134 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1135 trans->rtpsource = NULL;
1136 trans->timeout = TRUE;
1140 static GstFlowReturn
1141 handle_new_buffer (GstAppSink * sink, gpointer user_data)
1145 GstRTSPMediaStream *stream;
1147 buffer = gst_app_sink_pull_buffer (sink);
1151 stream = (GstRTSPMediaStream *) user_data;
1153 for (walk = stream->transports; walk; walk = g_list_next (walk)) {
1154 GstRTSPMediaTrans *tr = (GstRTSPMediaTrans *) walk->data;
1156 if (GST_ELEMENT_CAST (sink) == stream->appsink[0]) {
1158 tr->send_rtp (buffer, tr->transport->interleaved.min, tr->user_data);
1161 tr->send_rtcp (buffer, tr->transport->interleaved.max, tr->user_data);
1164 gst_buffer_unref (buffer);
1169 static GstFlowReturn
1170 handle_new_buffer_list (GstAppSink * sink, gpointer user_data)
1173 GstBufferList *blist;
1174 GstRTSPMediaStream *stream;
1176 blist = gst_app_sink_pull_buffer_list (sink);
1180 stream = (GstRTSPMediaStream *) user_data;
1182 for (walk = stream->transports; walk; walk = g_list_next (walk)) {
1183 GstRTSPMediaTrans *tr = (GstRTSPMediaTrans *) walk->data;
1185 if (GST_ELEMENT_CAST (sink) == stream->appsink[0]) {
1186 if (tr->send_rtp_list)
1187 tr->send_rtp_list (blist, tr->transport->interleaved.min,
1190 if (tr->send_rtcp_list)
1191 tr->send_rtcp_list (blist, tr->transport->interleaved.max,
1195 gst_buffer_list_unref (blist);
1200 static GstAppSinkCallbacks sink_cb = {
1201 NULL, /* not interested in EOS */
1202 NULL, /* not interested in preroll buffers */
1204 handle_new_buffer_list
1207 /* prepare the pipeline objects to handle @stream in @media */
1209 setup_stream (GstRTSPMediaStream * stream, guint idx, GstRTSPMedia * media)
1212 GstPad *pad, *teepad, *selpad;
1213 GstPadLinkReturn ret;
1216 /* allocate udp ports, we will have 4 of them, 2 for receiving RTP/RTCP and 2
1217 * for sending RTP/RTCP. The sender and receiver ports are shared between the
1219 if (!alloc_udp_ports (media, stream))
1222 /* add the ports to the pipeline */
1223 for (i = 0; i < 2; i++) {
1224 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->udpsink[i]);
1225 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->udpsrc[i]);
1228 /* create elements for the TCP transfer */
1229 for (i = 0; i < 2; i++) {
1230 stream->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
1231 stream->appsink[i] = gst_element_factory_make ("appsink", NULL);
1232 g_object_set (stream->appsink[i], "async", FALSE, "sync", FALSE, NULL);
1233 g_object_set (stream->appsink[i], "emit-signals", FALSE, NULL);
1234 g_object_set (stream->appsink[i], "preroll-queue-len", 1, NULL);
1235 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->appsink[i]);
1236 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->appsrc[i]);
1237 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (stream->appsink[i]),
1238 &sink_cb, stream, NULL);
1241 /* hook up the stream to the RTP session elements. */
1242 name = g_strdup_printf ("send_rtp_sink_%d", idx);
1243 stream->send_rtp_sink = gst_element_get_request_pad (media->rtpbin, name);
1245 name = g_strdup_printf ("send_rtp_src_%d", idx);
1246 stream->send_rtp_src = gst_element_get_static_pad (media->rtpbin, name);
1248 name = g_strdup_printf ("send_rtcp_src_%d", idx);
1249 stream->send_rtcp_src = gst_element_get_request_pad (media->rtpbin, name);
1251 name = g_strdup_printf ("recv_rtcp_sink_%d", idx);
1252 stream->recv_rtcp_sink = gst_element_get_request_pad (media->rtpbin, name);
1254 name = g_strdup_printf ("recv_rtp_sink_%d", idx);
1255 stream->recv_rtp_sink = gst_element_get_request_pad (media->rtpbin, name);
1258 /* get the session */
1259 g_signal_emit_by_name (media->rtpbin, "get-internal-session", idx,
1262 g_signal_connect (stream->session, "on-new-ssrc", (GCallback) on_new_ssrc,
1264 g_signal_connect (stream->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
1266 g_signal_connect (stream->session, "on-ssrc-active",
1267 (GCallback) on_ssrc_active, stream);
1268 g_signal_connect (stream->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
1270 g_signal_connect (stream->session, "on-bye-timeout",
1271 (GCallback) on_bye_timeout, stream);
1272 g_signal_connect (stream->session, "on-timeout", (GCallback) on_timeout,
1275 /* link the RTP pad to the session manager */
1276 ret = gst_pad_link (stream->srcpad, stream->send_rtp_sink);
1277 if (ret != GST_PAD_LINK_OK)
1280 /* make tee for RTP and link to stream */
1281 stream->tee[0] = gst_element_factory_make ("tee", NULL);
1282 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->tee[0]);
1284 pad = gst_element_get_static_pad (stream->tee[0], "sink");
1285 gst_pad_link (stream->send_rtp_src, pad);
1286 gst_object_unref (pad);
1288 /* link RTP sink, we're pretty sure this will work. */
1289 teepad = gst_element_get_request_pad (stream->tee[0], "src%d");
1290 pad = gst_element_get_static_pad (stream->udpsink[0], "sink");
1291 gst_pad_link (teepad, pad);
1292 gst_object_unref (pad);
1293 gst_object_unref (teepad);
1295 teepad = gst_element_get_request_pad (stream->tee[0], "src%d");
1296 pad = gst_element_get_static_pad (stream->appsink[0], "sink");
1297 gst_pad_link (teepad, pad);
1298 gst_object_unref (pad);
1299 gst_object_unref (teepad);
1301 /* make tee for RTCP */
1302 stream->tee[1] = gst_element_factory_make ("tee", NULL);
1303 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->tee[1]);
1305 pad = gst_element_get_static_pad (stream->tee[1], "sink");
1306 gst_pad_link (stream->send_rtcp_src, pad);
1307 gst_object_unref (pad);
1309 /* link RTCP elements */
1310 teepad = gst_element_get_request_pad (stream->tee[1], "src%d");
1311 pad = gst_element_get_static_pad (stream->udpsink[1], "sink");
1312 gst_pad_link (teepad, pad);
1313 gst_object_unref (pad);
1314 gst_object_unref (teepad);
1316 teepad = gst_element_get_request_pad (stream->tee[1], "src%d");
1317 pad = gst_element_get_static_pad (stream->appsink[1], "sink");
1318 gst_pad_link (teepad, pad);
1319 gst_object_unref (pad);
1320 gst_object_unref (teepad);
1322 /* make selector for the RTP receivers */
1323 stream->selector[0] = gst_element_factory_make ("rtspfunnel", NULL);
1324 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->selector[0]);
1326 pad = gst_element_get_static_pad (stream->selector[0], "src");
1327 gst_pad_link (pad, stream->recv_rtp_sink);
1328 gst_object_unref (pad);
1330 selpad = gst_element_get_request_pad (stream->selector[0], "sink%d");
1331 pad = gst_element_get_static_pad (stream->udpsrc[0], "src");
1332 gst_pad_link (pad, selpad);
1333 gst_object_unref (pad);
1334 gst_object_unref (selpad);
1336 selpad = gst_element_get_request_pad (stream->selector[0], "sink%d");
1337 pad = gst_element_get_static_pad (stream->appsrc[0], "src");
1338 gst_pad_link (pad, selpad);
1339 gst_object_unref (pad);
1340 gst_object_unref (selpad);
1342 /* make selector for the RTCP receivers */
1343 stream->selector[1] = gst_element_factory_make ("rtspfunnel", NULL);
1344 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->selector[1]);
1346 pad = gst_element_get_static_pad (stream->selector[1], "src");
1347 gst_pad_link (pad, stream->recv_rtcp_sink);
1348 gst_object_unref (pad);
1350 selpad = gst_element_get_request_pad (stream->selector[1], "sink%d");
1351 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
1352 gst_pad_link (pad, selpad);
1353 gst_object_unref (pad);
1354 gst_object_unref (selpad);
1356 selpad = gst_element_get_request_pad (stream->selector[1], "sink%d");
1357 pad = gst_element_get_static_pad (stream->appsrc[1], "src");
1358 gst_pad_link (pad, selpad);
1359 gst_object_unref (pad);
1360 gst_object_unref (selpad);
1362 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1364 gst_element_set_state (stream->udpsrc[0], GST_STATE_PLAYING);
1365 gst_element_set_state (stream->udpsrc[1], GST_STATE_PLAYING);
1366 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
1367 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
1369 /* be notified of caps changes */
1370 stream->caps_sig = g_signal_connect (stream->send_rtp_sink, "notify::caps",
1371 (GCallback) caps_notify, stream);
1373 stream->prepared = TRUE;
1380 GST_WARNING ("failed to link stream %d", idx);
1386 unlock_streams (GstRTSPMedia * media)
1390 /* unlock the udp src elements */
1391 n_streams = gst_rtsp_media_n_streams (media);
1392 for (i = 0; i < n_streams; i++) {
1393 GstRTSPMediaStream *stream;
1395 stream = gst_rtsp_media_get_stream (media, i);
1397 gst_element_set_locked_state (stream->udpsrc[0], FALSE);
1398 gst_element_set_locked_state (stream->udpsrc[1], FALSE);
1403 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1405 g_mutex_lock (media->lock);
1406 /* never overwrite the error status */
1407 if (media->status != GST_RTSP_MEDIA_STATUS_ERROR)
1408 media->status = status;
1409 GST_DEBUG ("setting new status to %d", status);
1410 g_cond_broadcast (media->cond);
1411 g_mutex_unlock (media->lock);
1414 static GstRTSPMediaStatus
1415 gst_rtsp_media_get_status (GstRTSPMedia * media)
1417 GstRTSPMediaStatus result;
1420 g_mutex_lock (media->lock);
1421 g_get_current_time (&timeout);
1422 g_time_val_add (&timeout, 20 * G_USEC_PER_SEC);
1423 /* while we are preparing, wait */
1424 while (media->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1425 GST_DEBUG ("waiting for status change");
1426 if (!g_cond_timed_wait (media->cond, media->lock, &timeout)) {
1427 GST_DEBUG ("timeout, assuming error status");
1428 media->status = GST_RTSP_MEDIA_STATUS_ERROR;
1431 /* could be success or error */
1432 result = media->status;
1433 GST_DEBUG ("got status %d", result);
1434 g_mutex_unlock (media->lock);
1440 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1442 GstMessageType type;
1444 type = GST_MESSAGE_TYPE (message);
1447 case GST_MESSAGE_STATE_CHANGED:
1449 case GST_MESSAGE_BUFFERING:
1453 gst_message_parse_buffering (message, &percent);
1455 /* no state management needed for live pipelines */
1459 if (percent == 100) {
1460 /* a 100% message means buffering is done */
1461 media->buffering = FALSE;
1462 /* if the desired state is playing, go back */
1463 if (media->target_state == GST_STATE_PLAYING) {
1464 GST_INFO ("Buffering done, setting pipeline to PLAYING");
1465 gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1467 GST_INFO ("Buffering done");
1470 /* buffering busy */
1471 if (media->buffering == FALSE) {
1472 if (media->target_state == GST_STATE_PLAYING) {
1473 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1474 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1475 gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1477 GST_INFO ("Buffering ...");
1480 media->buffering = TRUE;
1484 case GST_MESSAGE_LATENCY:
1486 gst_bin_recalculate_latency (GST_BIN_CAST (media->pipeline));
1489 case GST_MESSAGE_ERROR:
1494 gst_message_parse_error (message, &gerror, &debug);
1495 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1496 g_error_free (gerror);
1499 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1502 case GST_MESSAGE_WARNING:
1507 gst_message_parse_warning (message, &gerror, &debug);
1508 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1509 g_error_free (gerror);
1513 case GST_MESSAGE_ELEMENT:
1515 case GST_MESSAGE_STREAM_STATUS:
1517 case GST_MESSAGE_ASYNC_DONE:
1518 if (!media->adding) {
1519 /* when we are dynamically adding pads, the addition of the udpsrc will
1520 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1521 * wait for the final ASYNC_DONE after everything prerolled */
1522 GST_INFO ("%p: got ASYNC_DONE", media);
1523 collect_media_stats (media);
1525 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1527 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1530 case GST_MESSAGE_EOS:
1531 GST_INFO ("%p: got EOS", media);
1532 if (media->eos_pending) {
1533 GST_DEBUG ("shutting down after EOS");
1534 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1535 media->eos_pending = FALSE;
1536 g_object_unref (media);
1540 GST_INFO ("%p: got message type %s", media,
1541 gst_message_type_get_name (type));
1548 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1550 GstRTSPMediaClass *klass;
1553 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1555 if (klass->handle_message)
1556 ret = klass->handle_message (media, message);
1563 /* called from streaming threads */
1565 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1567 GstRTSPMediaStream *stream;
1571 i = media->streams->len + 1;
1573 GST_INFO ("pad added %s:%s, stream %d", GST_DEBUG_PAD_NAME (pad), i);
1575 stream = g_new0 (GstRTSPMediaStream, 1);
1576 stream->payloader = element;
1578 name = g_strdup_printf ("dynpay%d", i);
1580 media->adding = TRUE;
1582 /* ghost the pad of the payloader to the element */
1583 stream->srcpad = gst_ghost_pad_new (name, pad);
1584 gst_pad_set_active (stream->srcpad, TRUE);
1585 gst_element_add_pad (media->element, stream->srcpad);
1588 /* add stream now */
1589 g_array_append_val (media->streams, stream);
1591 setup_stream (stream, i, media);
1593 for (i = 0; i < 2; i++) {
1594 gst_element_set_state (stream->udpsink[i], GST_STATE_PAUSED);
1595 gst_element_set_state (stream->appsink[i], GST_STATE_PAUSED);
1596 gst_element_set_state (stream->tee[i], GST_STATE_PAUSED);
1597 gst_element_set_state (stream->selector[i], GST_STATE_PAUSED);
1598 gst_element_set_state (stream->appsrc[i], GST_STATE_PAUSED);
1600 media->adding = FALSE;
1604 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1606 GST_INFO ("no more pads");
1607 if (media->fakesink) {
1608 gst_object_ref (media->fakesink);
1609 gst_bin_remove (GST_BIN (media->pipeline), media->fakesink);
1610 gst_element_set_state (media->fakesink, GST_STATE_NULL);
1611 gst_object_unref (media->fakesink);
1612 media->fakesink = NULL;
1613 GST_INFO ("removed fakesink");
1618 * gst_rtsp_media_prepare:
1619 * @media: a #GstRTSPMedia
1621 * Prepare @media for streaming. This function will create the pipeline and
1622 * other objects to manage the streaming.
1624 * It will preroll the pipeline and collect vital information about the streams
1625 * such as the duration.
1627 * Returns: %TRUE on success.
1630 gst_rtsp_media_prepare (GstRTSPMedia * media)
1632 GstStateChangeReturn ret;
1633 GstRTSPMediaStatus status;
1635 GstRTSPMediaClass *klass;
1639 if (media->status == GST_RTSP_MEDIA_STATUS_PREPARED)
1642 if (!media->reusable && media->reused)
1645 GST_INFO ("preparing media %p", media);
1647 /* reset some variables */
1648 media->is_live = FALSE;
1649 media->buffering = FALSE;
1650 /* we're preparing now */
1651 media->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1653 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (media->pipeline));
1655 /* add the pipeline bus to our custom mainloop */
1656 media->source = gst_bus_create_watch (bus);
1657 gst_object_unref (bus);
1659 g_source_set_callback (media->source, (GSourceFunc) bus_message, media, NULL);
1661 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1662 media->id = g_source_attach (media->source, klass->context);
1664 media->rtpbin = gst_element_factory_make ("gstrtpbin", NULL);
1666 /* add stuff to the bin */
1667 gst_bin_add (GST_BIN (media->pipeline), media->rtpbin);
1669 /* link streams we already have, other streams might appear when we have
1670 * dynamic elements */
1671 n_streams = gst_rtsp_media_n_streams (media);
1672 for (i = 0; i < n_streams; i++) {
1673 GstRTSPMediaStream *stream;
1675 stream = gst_rtsp_media_get_stream (media, i);
1677 setup_stream (stream, i, media);
1680 for (walk = media->dynamic; walk; walk = g_list_next (walk)) {
1681 GstElement *elem = walk->data;
1683 GST_INFO ("adding callbacks for dynamic element %p", elem);
1685 g_signal_connect (elem, "pad-added", (GCallback) pad_added_cb, media);
1686 g_signal_connect (elem, "no-more-pads", (GCallback) no_more_pads_cb, media);
1688 /* we add a fakesink here in order to make the state change async. We remove
1689 * the fakesink again in the no-more-pads callback. */
1690 media->fakesink = gst_element_factory_make ("fakesink", "fakesink");
1691 gst_bin_add (GST_BIN (media->pipeline), media->fakesink);
1694 GST_INFO ("setting pipeline to PAUSED for media %p", media);
1695 /* first go to PAUSED */
1696 ret = gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1697 media->target_state = GST_STATE_PAUSED;
1700 case GST_STATE_CHANGE_SUCCESS:
1701 GST_INFO ("SUCCESS state change for media %p", media);
1703 case GST_STATE_CHANGE_ASYNC:
1704 GST_INFO ("ASYNC state change for media %p", media);
1706 case GST_STATE_CHANGE_NO_PREROLL:
1707 /* we need to go to PLAYING */
1708 GST_INFO ("NO_PREROLL state change: live media %p", media);
1709 media->is_live = TRUE;
1710 ret = gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1711 if (ret == GST_STATE_CHANGE_FAILURE)
1714 case GST_STATE_CHANGE_FAILURE:
1718 /* now wait for all pads to be prerolled */
1719 status = gst_rtsp_media_get_status (media);
1720 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1723 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
1725 GST_INFO ("object %p is prerolled", media);
1737 GST_WARNING ("can not reuse media %p", media);
1742 GST_WARNING ("failed to preroll pipeline");
1743 unlock_streams (media);
1744 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1745 gst_rtsp_media_unprepare (media);
1751 * gst_rtsp_media_unprepare:
1752 * @media: a #GstRTSPMedia
1754 * Unprepare @media. After this call, the media should be prepared again before
1755 * it can be used again. If the media is set to be non-reusable, a new instance
1758 * Returns: %TRUE on success.
1761 gst_rtsp_media_unprepare (GstRTSPMedia * media)
1763 GstRTSPMediaClass *klass;
1766 if (media->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
1769 GST_INFO ("unprepare media %p", media);
1770 media->target_state = GST_STATE_NULL;
1772 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1773 if (klass->unprepare)
1774 success = klass->unprepare (media);
1778 media->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
1779 media->reused = TRUE;
1781 /* when the media is not reusable, this will effectively unref the media and
1783 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
1789 default_unprepare (GstRTSPMedia * media)
1791 if (media->eos_shutdown) {
1792 GST_DEBUG ("sending EOS for shutdown");
1793 /* ref so that we don't disappear */
1794 g_object_ref (media);
1795 media->eos_pending = TRUE;
1796 gst_element_send_event (media->pipeline, gst_event_new_eos ());
1797 /* we need to go to playing again for the EOS to propagate, normally in this
1798 * state, nothing is receiving data from us anymore so this is ok. */
1799 gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1801 GST_DEBUG ("shutting down");
1802 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1808 add_udp_destination (GstRTSPMedia * media, GstRTSPMediaStream * stream,
1809 gchar * dest, gint min, gint max)
1811 gboolean do_add = TRUE;
1812 RTSPDestination *ndest;
1814 if (stream->filter_duplicates) {
1815 RTSPDestination fdest;
1822 /* first see if we already added this destination */
1824 g_list_find_custom (stream->destinations, &fdest,
1825 (GCompareFunc) dest_compare);
1827 ndest = (RTSPDestination *) find->data;
1829 GST_INFO ("already streaming to %s:%d-%d with %d clients", dest, min, max,
1837 GST_INFO ("adding %s:%d-%d", dest, min, max);
1838 g_signal_emit_by_name (stream->udpsink[0], "add", dest, min, NULL);
1839 g_signal_emit_by_name (stream->udpsink[1], "add", dest, max, NULL);
1841 if (stream->filter_duplicates) {
1842 ndest = create_destination (dest, min, max);
1843 stream->destinations = g_list_prepend (stream->destinations, ndest);
1849 remove_udp_destination (GstRTSPMedia * media, GstRTSPMediaStream * stream,
1850 gchar * dest, gint min, gint max)
1852 gboolean do_remove = TRUE;
1853 RTSPDestination *ndest = NULL;
1856 if (stream->filter_duplicates) {
1857 RTSPDestination fdest;
1863 /* first see if we already added this destination */
1865 g_list_find_custom (stream->destinations, &fdest,
1866 (GCompareFunc) dest_compare);
1870 ndest = (RTSPDestination *) find->data;
1871 if (--ndest->count > 0) {
1873 GST_INFO ("still streaming to %s:%d-%d with %d clients", dest, min, max,
1879 GST_INFO ("removing %s:%d-%d", dest, min, max);
1880 g_signal_emit_by_name (stream->udpsink[0], "remove", dest, min, NULL);
1881 g_signal_emit_by_name (stream->udpsink[1], "remove", dest, max, NULL);
1883 if (stream->filter_duplicates) {
1884 stream->destinations = g_list_delete_link (stream->destinations, find);
1885 free_destination (ndest);
1891 * gst_rtsp_media_set_state:
1892 * @media: a #GstRTSPMedia
1893 * @state: the target state of the media
1894 * @transports: a #GArray of #GstRTSPMediaTrans pointers
1896 * Set the state of @media to @state and for the transports in @transports.
1898 * Returns: %TRUE on success.
1901 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
1902 GArray * transports)
1905 GstStateChangeReturn ret;
1906 gboolean add, remove, do_state;
1909 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1910 g_return_val_if_fail (transports != NULL, FALSE);
1912 /* NULL and READY are the same */
1913 if (state == GST_STATE_READY)
1914 state = GST_STATE_NULL;
1916 add = remove = FALSE;
1918 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
1922 case GST_STATE_NULL:
1923 /* unlock the streams so that they follow the state changes from now on */
1924 unlock_streams (media);
1926 case GST_STATE_PAUSED:
1927 /* we're going from PLAYING to PAUSED, READY or NULL, remove */
1928 if (media->target_state == GST_STATE_PLAYING)
1931 case GST_STATE_PLAYING:
1932 /* we're going to PLAYING, add */
1938 old_active = media->active;
1940 for (i = 0; i < transports->len; i++) {
1941 GstRTSPMediaTrans *tr;
1942 GstRTSPMediaStream *stream;
1943 GstRTSPTransport *trans;
1945 /* we need a non-NULL entry in the array */
1946 tr = g_array_index (transports, GstRTSPMediaTrans *, i);
1950 /* we need a transport */
1951 if (!(trans = tr->transport))
1954 /* get the stream and add the destinations */
1955 stream = gst_rtsp_media_get_stream (media, tr->idx);
1956 switch (trans->lower_transport) {
1957 case GST_RTSP_LOWER_TRANS_UDP:
1958 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1963 dest = trans->destination;
1964 if (trans->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1965 min = trans->port.min;
1966 max = trans->port.max;
1968 min = trans->client_port.min;
1969 max = trans->client_port.max;
1972 if (add && !tr->active) {
1973 add_udp_destination (media, stream, dest, min, max);
1974 stream->transports = g_list_prepend (stream->transports, tr);
1977 } else if (remove && tr->active) {
1978 remove_udp_destination (media, stream, dest, min, max);
1979 stream->transports = g_list_remove (stream->transports, tr);
1985 case GST_RTSP_LOWER_TRANS_TCP:
1986 if (add && !tr->active) {
1987 GST_INFO ("adding TCP %s", trans->destination);
1988 stream->transports = g_list_prepend (stream->transports, tr);
1991 } else if (remove && tr->active) {
1992 GST_INFO ("removing TCP %s", trans->destination);
1993 stream->transports = g_list_remove (stream->transports, tr);
1999 GST_INFO ("Unknown transport %d", trans->lower_transport);
2004 /* we just added the first media, do the playing state change */
2005 if (old_active == 0 && add)
2007 /* if we have no more active media, do the downward state changes */
2008 else if (media->active == 0)
2013 GST_INFO ("state %d active %d media %p do_state %d", state, media->active,
2016 if (media->target_state != state) {
2018 if (state == GST_STATE_NULL) {
2019 gst_rtsp_media_unprepare (media);
2021 GST_INFO ("state %s media %p", gst_element_state_get_name (state),
2023 media->target_state = state;
2024 ret = gst_element_set_state (media->pipeline, state);
2027 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
2031 /* remember where we are */
2032 if (state == GST_STATE_PAUSED || old_active != media->active)
2033 collect_media_stats (media);
2039 * gst_rtsp_media_remove_elements:
2040 * @media: a #GstRTSPMedia
2042 * Remove all elements and the pipeline controlled by @media.
2045 gst_rtsp_media_remove_elements (GstRTSPMedia * media)
2049 unlock_streams (media);
2051 for (i = 0; i < media->streams->len; i++) {
2052 GstRTSPMediaStream *stream;
2054 GST_INFO ("Removing elements of stream %d from pipeline", i);
2056 stream = g_array_index (media->streams, GstRTSPMediaStream *, i);
2058 gst_pad_unlink (stream->srcpad, stream->send_rtp_sink);
2060 g_signal_handler_disconnect (stream->send_rtp_sink, stream->caps_sig);
2062 for (j = 0; j < 2; j++) {
2063 gst_element_set_state (stream->udpsrc[j], GST_STATE_NULL);
2064 gst_element_set_state (stream->udpsink[j], GST_STATE_NULL);
2065 gst_element_set_state (stream->appsrc[j], GST_STATE_NULL);
2066 gst_element_set_state (stream->appsink[j], GST_STATE_NULL);
2067 gst_element_set_state (stream->tee[j], GST_STATE_NULL);
2068 gst_element_set_state (stream->selector[j], GST_STATE_NULL);
2070 gst_bin_remove (GST_BIN (media->pipeline), stream->udpsrc[j]);
2071 gst_bin_remove (GST_BIN (media->pipeline), stream->udpsink[j]);
2072 gst_bin_remove (GST_BIN (media->pipeline), stream->appsrc[j]);
2073 gst_bin_remove (GST_BIN (media->pipeline), stream->appsink[j]);
2074 gst_bin_remove (GST_BIN (media->pipeline), stream->tee[j]);
2075 gst_bin_remove (GST_BIN (media->pipeline), stream->selector[j]);
2078 gst_caps_unref (stream->caps);
2079 stream->caps = NULL;
2080 gst_rtsp_media_stream_free (stream);
2082 g_array_remove_range (media->streams, 0, media->streams->len);
2084 gst_element_set_state (media->rtpbin, GST_STATE_NULL);
2085 gst_bin_remove (GST_BIN (media->pipeline), media->rtpbin);
2087 gst_object_unref (media->pipeline);
2088 media->pipeline = NULL;