2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 #include <gst/app/gstappsrc.h>
24 #include <gst/app/gstappsink.h>
26 #include "rtsp-media.h"
28 #define DEFAULT_SHARED FALSE
29 #define DEFAULT_REUSABLE FALSE
30 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_TCP
31 //#define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP_MCAST
32 #define DEFAULT_EOS_SHUTDOWN FALSE
33 #define DEFAULT_BUFFER_SIZE 0x80000
35 /* define to dump received RTCP packets */
57 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
58 #define GST_CAT_DEFAULT rtsp_media_debug
60 static GQuark ssrc_stream_map_key;
62 static void gst_rtsp_media_get_property (GObject * object, guint propid,
63 GValue * value, GParamSpec * pspec);
64 static void gst_rtsp_media_set_property (GObject * object, guint propid,
65 const GValue * value, GParamSpec * pspec);
66 static void gst_rtsp_media_finalize (GObject * obj);
68 static gpointer do_loop (GstRTSPMediaClass * klass);
69 static gboolean default_handle_message (GstRTSPMedia * media,
70 GstMessage * message);
71 static gboolean default_unprepare (GstRTSPMedia * media);
72 static void unlock_streams (GstRTSPMedia * media);
74 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
76 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
79 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
81 GObjectClass *gobject_class;
84 gobject_class = G_OBJECT_CLASS (klass);
86 gobject_class->get_property = gst_rtsp_media_get_property;
87 gobject_class->set_property = gst_rtsp_media_set_property;
88 gobject_class->finalize = gst_rtsp_media_finalize;
90 g_object_class_install_property (gobject_class, PROP_SHARED,
91 g_param_spec_boolean ("shared", "Shared",
92 "If this media pipeline can be shared", DEFAULT_SHARED,
93 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
95 g_object_class_install_property (gobject_class, PROP_REUSABLE,
96 g_param_spec_boolean ("reusable", "Reusable",
97 "If this media pipeline can be reused after an unprepare",
98 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
100 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
101 g_param_spec_flags ("protocols", "Protocols",
102 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
103 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
105 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
106 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
107 "Send an EOS event to the pipeline before unpreparing",
108 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
110 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
111 g_param_spec_uint ("buffer-size", "Buffer Size",
112 "The kernel UDP buffer size to use", 0, G_MAXUINT,
113 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
115 gst_rtsp_media_signals[SIGNAL_PREPARED] =
116 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
117 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
118 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
120 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
121 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
122 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
123 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
125 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
126 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
127 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
128 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 0, G_TYPE_INT);
130 klass->context = g_main_context_new ();
131 klass->loop = g_main_loop_new (klass->context, TRUE);
133 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
135 klass->thread = g_thread_create ((GThreadFunc) do_loop, klass, TRUE, &error);
137 g_critical ("could not start bus thread: %s", error->message);
139 klass->handle_message = default_handle_message;
140 klass->unprepare = default_unprepare;
142 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
146 gst_rtsp_media_init (GstRTSPMedia * media)
148 media->streams = g_array_new (FALSE, TRUE, sizeof (GstRTSPMediaStream *));
149 media->lock = g_mutex_new ();
150 media->cond = g_cond_new ();
152 media->shared = DEFAULT_SHARED;
153 media->reusable = DEFAULT_REUSABLE;
154 media->protocols = DEFAULT_PROTOCOLS;
155 media->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
156 media->buffer_size = DEFAULT_BUFFER_SIZE;
160 gst_rtsp_media_trans_cleanup (GstRTSPMediaTrans * trans)
162 if (trans->transport) {
163 gst_rtsp_transport_free (trans->transport);
164 trans->transport = NULL;
166 if (trans->rtpsource) {
167 g_object_set_qdata (trans->rtpsource, ssrc_stream_map_key, NULL);
168 trans->rtpsource = NULL;
173 gst_rtsp_media_stream_free (GstRTSPMediaStream * stream)
176 g_object_unref (stream->session);
179 gst_caps_unref (stream->caps);
181 if (stream->send_rtp_sink)
182 gst_object_unref (stream->send_rtp_sink);
183 if (stream->send_rtp_src)
184 gst_object_unref (stream->send_rtp_src);
185 if (stream->send_rtcp_src)
186 gst_object_unref (stream->send_rtcp_src);
187 if (stream->recv_rtcp_sink)
188 gst_object_unref (stream->recv_rtcp_sink);
189 if (stream->recv_rtp_sink)
190 gst_object_unref (stream->recv_rtp_sink);
192 g_list_free (stream->transports);
198 gst_rtsp_media_finalize (GObject * obj)
203 media = GST_RTSP_MEDIA (obj);
205 GST_INFO ("finalize media %p", media);
207 if (media->pipeline) {
208 unlock_streams (media);
209 gst_element_set_state (media->pipeline, GST_STATE_NULL);
210 gst_object_unref (media->pipeline);
213 for (i = 0; i < media->streams->len; i++) {
214 GstRTSPMediaStream *stream;
216 stream = g_array_index (media->streams, GstRTSPMediaStream *, i);
218 gst_rtsp_media_stream_free (stream);
220 g_array_free (media->streams, TRUE);
222 g_list_foreach (media->dynamic, (GFunc) gst_object_unref, NULL);
223 g_list_free (media->dynamic);
226 g_source_destroy (media->source);
227 g_source_unref (media->source);
229 g_mutex_free (media->lock);
230 g_cond_free (media->cond);
232 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
236 gst_rtsp_media_get_property (GObject * object, guint propid,
237 GValue * value, GParamSpec * pspec)
239 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
243 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
246 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
249 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
251 case PROP_EOS_SHUTDOWN:
252 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
254 case PROP_BUFFER_SIZE:
255 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
258 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
263 gst_rtsp_media_set_property (GObject * object, guint propid,
264 const GValue * value, GParamSpec * pspec)
266 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
270 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
273 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
276 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
278 case PROP_EOS_SHUTDOWN:
279 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
281 case PROP_BUFFER_SIZE:
282 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
285 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
290 do_loop (GstRTSPMediaClass * klass)
292 GST_INFO ("enter mainloop");
293 g_main_loop_run (klass->loop);
294 GST_INFO ("exit mainloop");
300 collect_media_stats (GstRTSPMedia * media)
303 gint64 position, duration;
305 media->range.unit = GST_RTSP_RANGE_NPT;
307 if (media->is_live) {
308 media->range.min.type = GST_RTSP_TIME_NOW;
309 media->range.min.seconds = -1;
310 media->range.max.type = GST_RTSP_TIME_END;
311 media->range.max.seconds = -1;
313 /* get the position */
314 format = GST_FORMAT_TIME;
315 if (!gst_element_query_position (media->pipeline, &format, &position)) {
316 GST_INFO ("position query failed");
320 /* get the duration */
321 format = GST_FORMAT_TIME;
322 if (!gst_element_query_duration (media->pipeline, &format, &duration)) {
323 GST_INFO ("duration query failed");
327 GST_INFO ("stats: position %" GST_TIME_FORMAT ", duration %"
328 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (duration));
330 if (position == -1) {
331 media->range.min.type = GST_RTSP_TIME_NOW;
332 media->range.min.seconds = -1;
334 media->range.min.type = GST_RTSP_TIME_SECONDS;
335 media->range.min.seconds = ((gdouble) position) / GST_SECOND;
337 if (duration == -1) {
338 media->range.max.type = GST_RTSP_TIME_END;
339 media->range.max.seconds = -1;
341 media->range.max.type = GST_RTSP_TIME_SECONDS;
342 media->range.max.seconds = ((gdouble) duration) / GST_SECOND;
348 * gst_rtsp_media_new:
350 * Create a new #GstRTSPMedia instance. The #GstRTSPMedia object contains the
351 * element to produde RTP data for one or more related (audio/video/..)
354 * Returns: a new #GstRTSPMedia object.
357 gst_rtsp_media_new (void)
359 GstRTSPMedia *result;
361 result = g_object_new (GST_TYPE_RTSP_MEDIA, NULL);
367 * gst_rtsp_media_set_shared:
368 * @media: a #GstRTSPMedia
369 * @shared: the new value
371 * Set or unset if the pipeline for @media can be shared will multiple clients.
372 * When @shared is %TRUE, client requests for this media will share the media
376 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
378 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
380 media->shared = shared;
384 * gst_rtsp_media_is_shared:
385 * @media: a #GstRTSPMedia
387 * Check if the pipeline for @media can be shared between multiple clients.
389 * Returns: %TRUE if the media can be shared between clients.
392 gst_rtsp_media_is_shared (GstRTSPMedia * media)
394 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
396 return media->shared;
400 * gst_rtsp_media_set_reusable:
401 * @media: a #GstRTSPMedia
402 * @reusable: the new value
404 * Set or unset if the pipeline for @media can be reused after the pipeline has
408 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
410 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
412 media->reusable = reusable;
416 * gst_rtsp_media_is_reusable:
417 * @media: a #GstRTSPMedia
419 * Check if the pipeline for @media can be reused after an unprepare.
421 * Returns: %TRUE if the media can be reused
424 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
426 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
428 return media->reusable;
432 * gst_rtsp_media_set_protocols:
433 * @media: a #GstRTSPMedia
434 * @protocols: the new flags
436 * Configure the allowed lower transport for @media.
439 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
441 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
443 media->protocols = protocols;
447 * gst_rtsp_media_get_protocols:
448 * @media: a #GstRTSPMedia
450 * Get the allowed protocols of @media.
452 * Returns: a #GstRTSPLowerTrans
455 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
457 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
458 GST_RTSP_LOWER_TRANS_UNKNOWN);
460 return media->protocols;
464 * gst_rtsp_media_set_eos_shutdown:
465 * @media: a #GstRTSPMedia
466 * @eos_shutdown: the new value
468 * Set or unset if an EOS event will be sent to the pipeline for @media before
472 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
474 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
476 media->eos_shutdown = eos_shutdown;
480 * gst_rtsp_media_is_eos_shutdown:
481 * @media: a #GstRTSPMedia
483 * Check if the pipeline for @media will send an EOS down the pipeline before
486 * Returns: %TRUE if the media will send EOS before unpreparing.
489 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
491 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
493 return media->eos_shutdown;
497 * gst_rtsp_media_set_buffer_size:
498 * @media: a #GstRTSPMedia
499 * @size: the new value
501 * Set the kernel UDP buffer size.
504 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
506 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
508 media->buffer_size = size;
512 * gst_rtsp_media_get_buffer_size:
513 * @media: a #GstRTSPMedia
515 * Get the kernel UDP buffer size.
517 * Returns: the kernel UDP buffer size.
520 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
522 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
524 return media->buffer_size;
528 * gst_rtsp_media_set_auth:
529 * @media: a #GstRTSPMedia
530 * @auth: a #GstRTSPAuth
532 * configure @auth to be used as the authentication manager of @media.
535 gst_rtsp_media_set_auth (GstRTSPMedia * media, GstRTSPAuth * auth)
539 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
548 g_object_unref (old);
553 * gst_rtsp_media_get_auth:
554 * @media: a #GstRTSPMedia
556 * Get the #GstRTSPAuth used as the authentication manager of @media.
558 * Returns: the #GstRTSPAuth of @media. g_object_unref() after
562 gst_rtsp_media_get_auth (GstRTSPMedia * media)
566 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
568 if ((result = media->auth))
569 g_object_ref (result);
576 * gst_rtsp_media_n_streams:
577 * @media: a #GstRTSPMedia
579 * Get the number of streams in this media.
581 * Returns: The number of streams.
584 gst_rtsp_media_n_streams (GstRTSPMedia * media)
586 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
588 return media->streams->len;
592 * gst_rtsp_media_get_stream:
593 * @media: a #GstRTSPMedia
594 * @idx: the stream index
596 * Retrieve the stream with index @idx from @media.
598 * Returns: the #GstRTSPMediaStream at index @idx or %NULL when a stream with
599 * that index did not exist.
602 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
604 GstRTSPMediaStream *res;
606 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
608 if (idx < media->streams->len)
609 res = g_array_index (media->streams, GstRTSPMediaStream *, idx);
617 * gst_rtsp_media_get_range_string:
618 * @media: a #GstRTSPMedia
619 * @play: for the PLAY request
621 * Get the current range as a string.
623 * Returns: The range as a string, g_free() after usage.
626 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play)
629 GstRTSPTimeRange range;
632 range = media->range;
634 if (!play && media->active > 0) {
635 range.min.type = GST_RTSP_TIME_NOW;
636 range.min.seconds = -1;
639 result = gst_rtsp_range_to_string (&range);
645 * gst_rtsp_media_seek:
646 * @media: a #GstRTSPMedia
647 * @range: a #GstRTSPTimeRange
649 * Seek the pipeline to @range.
651 * Returns: %TRUE on success.
654 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
659 GstSeekType start_type, stop_type;
661 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
662 g_return_val_if_fail (range != NULL, FALSE);
664 if (range->unit != GST_RTSP_RANGE_NPT)
667 /* depends on the current playing state of the pipeline. We might need to
668 * queue this until we get EOS. */
669 flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_KEY_UNIT;
671 start_type = stop_type = GST_SEEK_TYPE_NONE;
673 switch (range->min.type) {
674 case GST_RTSP_TIME_NOW:
677 case GST_RTSP_TIME_SECONDS:
678 /* only seek when something changed */
679 if (media->range.min.seconds == range->min.seconds) {
682 start = range->min.seconds * GST_SECOND;
683 start_type = GST_SEEK_TYPE_SET;
686 case GST_RTSP_TIME_END:
690 switch (range->max.type) {
691 case GST_RTSP_TIME_SECONDS:
692 /* only seek when something changed */
693 if (media->range.max.seconds == range->max.seconds) {
696 stop = range->max.seconds * GST_SECOND;
697 stop_type = GST_SEEK_TYPE_SET;
700 case GST_RTSP_TIME_END:
702 stop_type = GST_SEEK_TYPE_SET;
704 case GST_RTSP_TIME_NOW:
709 if (start != -1 || stop != -1) {
710 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
711 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
713 res = gst_element_seek (media->pipeline, 1.0, GST_FORMAT_TIME,
714 flags, start_type, start, stop_type, stop);
716 /* and block for the seek to complete */
717 GST_INFO ("done seeking %d", res);
718 gst_element_get_state (media->pipeline, NULL, NULL, -1);
719 GST_INFO ("prerolled again");
721 collect_media_stats (media);
723 GST_INFO ("no seek needed");
732 GST_WARNING ("seek unit %d not supported", range->unit);
737 GST_WARNING ("weird range type %d not supported", range->min.type);
743 * gst_rtsp_media_stream_rtp:
744 * @stream: a #GstRTSPMediaStream
745 * @buffer: a #GstBuffer
747 * Handle an RTP buffer for the stream. This method is usually called when a
748 * message has been received from a client using the TCP transport.
750 * This function takes ownership of @buffer.
752 * Returns: a GstFlowReturn.
755 gst_rtsp_media_stream_rtp (GstRTSPMediaStream * stream, GstBuffer * buffer)
759 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (stream->appsrc[0]), buffer);
765 * gst_rtsp_media_stream_rtcp:
766 * @stream: a #GstRTSPMediaStream
767 * @buffer: a #GstBuffer
769 * Handle an RTCP buffer for the stream. This method is usually called when a
770 * message has been received from a client using the TCP transport.
772 * This function takes ownership of @buffer.
774 * Returns: a GstFlowReturn.
777 gst_rtsp_media_stream_rtcp (GstRTSPMediaStream * stream, GstBuffer * buffer)
781 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (stream->appsrc[1]), buffer);
786 /* Allocate the udp ports and sockets */
788 alloc_udp_ports (GstRTSPMedia * media, GstRTSPMediaStream * stream)
790 GstStateChangeReturn ret;
791 GstElement *udpsrc0, *udpsrc1;
792 GstElement *udpsink0, *udpsink1;
793 gint tmp_rtp, tmp_rtcp;
795 gint rtpport, rtcpport, sockfd;
804 /* Start with random port */
808 host = "udp://[::0]";
810 host = "udp://0.0.0.0";
812 /* try to allocate 2 UDP ports, the RTP port should be an even
813 * number and the RTCP port should be the next (uneven) port */
815 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
817 goto no_udp_protocol;
818 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, NULL);
820 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
821 if (ret == GST_STATE_CHANGE_FAILURE) {
827 gst_element_set_state (udpsrc0, GST_STATE_NULL);
828 gst_object_unref (udpsrc0);
832 goto no_udp_protocol;
835 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
837 /* check if port is even */
838 if ((tmp_rtp & 1) != 0) {
839 /* port not even, close and allocate another */
843 gst_element_set_state (udpsrc0, GST_STATE_NULL);
844 gst_object_unref (udpsrc0);
850 /* allocate port+1 for RTCP now */
851 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
853 goto no_udp_rtcp_protocol;
856 tmp_rtcp = tmp_rtp + 1;
857 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, NULL);
859 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
860 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
861 if (ret == GST_STATE_CHANGE_FAILURE) {
866 gst_element_set_state (udpsrc0, GST_STATE_NULL);
867 gst_object_unref (udpsrc0);
869 gst_element_set_state (udpsrc1, GST_STATE_NULL);
870 gst_object_unref (udpsrc1);
876 /* all fine, do port check */
877 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
878 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
880 /* this should not happen... */
881 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
884 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
886 goto no_udp_protocol;
888 g_object_get (G_OBJECT (udpsrc0), "sock", &sockfd, NULL);
889 g_object_set (G_OBJECT (udpsink0), "sockfd", sockfd, NULL);
890 g_object_set (G_OBJECT (udpsink0), "closefd", FALSE, NULL);
892 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
894 goto no_udp_protocol;
896 if (g_object_class_find_property (G_OBJECT_GET_CLASS (udpsink0),
897 "send-duplicates")) {
898 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
899 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
902 ("old multiudpsink version found without send-duplicates property");
905 if (g_object_class_find_property (G_OBJECT_GET_CLASS (udpsink0),
907 g_object_set (G_OBJECT (udpsink0), "buffer-size", media->buffer_size, NULL);
909 GST_WARNING ("multiudpsink version found without buffer-size property");
912 g_object_get (G_OBJECT (udpsrc1), "sock", &sockfd, NULL);
913 g_object_set (G_OBJECT (udpsink1), "sockfd", sockfd, NULL);
914 g_object_set (G_OBJECT (udpsink1), "closefd", FALSE, NULL);
915 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
916 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
918 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
919 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
920 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
921 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
923 /* we keep these elements, we configure all in configure_transport when the
924 * server told us to really use the UDP ports. */
925 stream->udpsrc[0] = udpsrc0;
926 stream->udpsrc[1] = udpsrc1;
927 stream->udpsink[0] = udpsink0;
928 stream->udpsink[1] = udpsink1;
929 stream->server_port.min = rtpport;
930 stream->server_port.max = rtcpport;
943 no_udp_rtcp_protocol:
954 gst_element_set_state (udpsrc0, GST_STATE_NULL);
955 gst_object_unref (udpsrc0);
958 gst_element_set_state (udpsrc1, GST_STATE_NULL);
959 gst_object_unref (udpsrc1);
962 gst_element_set_state (udpsink0, GST_STATE_NULL);
963 gst_object_unref (udpsink0);
966 gst_element_set_state (udpsink1, GST_STATE_NULL);
967 gst_object_unref (udpsink1);
973 /* executed from streaming thread */
975 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPMediaStream * stream)
978 GstCaps *newcaps, *oldcaps;
980 newcaps = gst_pad_get_current_caps (pad);
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 ("funnel", 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 ("funnel", 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 media->rtpbin = gst_element_factory_make ("gstrtpbin", NULL);
1646 if (media->rtpbin == NULL)
1649 GST_INFO ("preparing media %p", media);
1651 /* reset some variables */
1652 media->is_live = FALSE;
1653 media->buffering = FALSE;
1654 /* we're preparing now */
1655 media->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1657 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (media->pipeline));
1659 /* add the pipeline bus to our custom mainloop */
1660 media->source = gst_bus_create_watch (bus);
1661 gst_object_unref (bus);
1663 g_source_set_callback (media->source, (GSourceFunc) bus_message, media, NULL);
1665 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1666 media->id = g_source_attach (media->source, klass->context);
1668 /* add stuff to the bin */
1669 gst_bin_add (GST_BIN (media->pipeline), media->rtpbin);
1671 /* link streams we already have, other streams might appear when we have
1672 * dynamic elements */
1673 n_streams = gst_rtsp_media_n_streams (media);
1674 for (i = 0; i < n_streams; i++) {
1675 GstRTSPMediaStream *stream;
1677 stream = gst_rtsp_media_get_stream (media, i);
1679 setup_stream (stream, i, media);
1682 for (walk = media->dynamic; walk; walk = g_list_next (walk)) {
1683 GstElement *elem = walk->data;
1685 GST_INFO ("adding callbacks for dynamic element %p", elem);
1687 g_signal_connect (elem, "pad-added", (GCallback) pad_added_cb, media);
1688 g_signal_connect (elem, "no-more-pads", (GCallback) no_more_pads_cb, media);
1690 /* we add a fakesink here in order to make the state change async. We remove
1691 * the fakesink again in the no-more-pads callback. */
1692 media->fakesink = gst_element_factory_make ("fakesink", "fakesink");
1693 gst_bin_add (GST_BIN (media->pipeline), media->fakesink);
1696 GST_INFO ("setting pipeline to PAUSED for media %p", media);
1697 /* first go to PAUSED */
1698 ret = gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1699 media->target_state = GST_STATE_PAUSED;
1702 case GST_STATE_CHANGE_SUCCESS:
1703 GST_INFO ("SUCCESS state change for media %p", media);
1705 case GST_STATE_CHANGE_ASYNC:
1706 GST_INFO ("ASYNC state change for media %p", media);
1708 case GST_STATE_CHANGE_NO_PREROLL:
1709 /* we need to go to PLAYING */
1710 GST_INFO ("NO_PREROLL state change: live media %p", media);
1711 media->is_live = TRUE;
1712 ret = gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1713 if (ret == GST_STATE_CHANGE_FAILURE)
1716 case GST_STATE_CHANGE_FAILURE:
1720 /* now wait for all pads to be prerolled */
1721 status = gst_rtsp_media_get_status (media);
1722 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1725 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
1727 GST_INFO ("object %p is prerolled", media);
1739 GST_WARNING ("can not reuse media %p", media);
1744 GST_WARNING ("no gstrtpbin element");
1745 g_warning ("failed to create element 'gstrtpbin', check your installation");
1750 GST_WARNING ("failed to preroll pipeline");
1751 unlock_streams (media);
1752 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1753 gst_rtsp_media_unprepare (media);
1759 * gst_rtsp_media_unprepare:
1760 * @media: a #GstRTSPMedia
1762 * Unprepare @media. After this call, the media should be prepared again before
1763 * it can be used again. If the media is set to be non-reusable, a new instance
1766 * Returns: %TRUE on success.
1769 gst_rtsp_media_unprepare (GstRTSPMedia * media)
1771 GstRTSPMediaClass *klass;
1774 if (media->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
1777 GST_INFO ("unprepare media %p", media);
1778 media->target_state = GST_STATE_NULL;
1780 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1781 if (klass->unprepare)
1782 success = klass->unprepare (media);
1786 media->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
1787 media->reused = TRUE;
1789 /* when the media is not reusable, this will effectively unref the media and
1791 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
1797 default_unprepare (GstRTSPMedia * media)
1799 if (media->eos_shutdown) {
1800 GST_DEBUG ("sending EOS for shutdown");
1801 /* ref so that we don't disappear */
1802 g_object_ref (media);
1803 media->eos_pending = TRUE;
1804 gst_element_send_event (media->pipeline, gst_event_new_eos ());
1805 /* we need to go to playing again for the EOS to propagate, normally in this
1806 * state, nothing is receiving data from us anymore so this is ok. */
1807 gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1809 GST_DEBUG ("shutting down");
1810 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1816 add_udp_destination (GstRTSPMedia * media, GstRTSPMediaStream * stream,
1817 gchar * dest, gint min, gint max)
1819 GST_INFO ("adding %s:%d-%d", dest, min, max);
1820 g_signal_emit_by_name (stream->udpsink[0], "add", dest, min, NULL);
1821 g_signal_emit_by_name (stream->udpsink[1], "add", dest, max, NULL);
1825 remove_udp_destination (GstRTSPMedia * media, GstRTSPMediaStream * stream,
1826 gchar * dest, gint min, gint max)
1828 GST_INFO ("removing %s:%d-%d", dest, min, max);
1829 g_signal_emit_by_name (stream->udpsink[0], "remove", dest, min, NULL);
1830 g_signal_emit_by_name (stream->udpsink[1], "remove", dest, max, NULL);
1834 * gst_rtsp_media_set_state:
1835 * @media: a #GstRTSPMedia
1836 * @state: the target state of the media
1837 * @transports: a #GArray of #GstRTSPMediaTrans pointers
1839 * Set the state of @media to @state and for the transports in @transports.
1841 * Returns: %TRUE on success.
1844 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
1845 GArray * transports)
1848 GstStateChangeReturn ret;
1849 gboolean add, remove, do_state;
1852 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1853 g_return_val_if_fail (transports != NULL, FALSE);
1855 /* NULL and READY are the same */
1856 if (state == GST_STATE_READY)
1857 state = GST_STATE_NULL;
1859 add = remove = FALSE;
1861 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
1865 case GST_STATE_NULL:
1866 /* unlock the streams so that they follow the state changes from now on */
1867 unlock_streams (media);
1869 case GST_STATE_PAUSED:
1870 /* we're going from PLAYING to PAUSED, READY or NULL, remove */
1871 if (media->target_state == GST_STATE_PLAYING)
1874 case GST_STATE_PLAYING:
1875 /* we're going to PLAYING, add */
1881 old_active = media->active;
1883 for (i = 0; i < transports->len; i++) {
1884 GstRTSPMediaTrans *tr;
1885 GstRTSPMediaStream *stream;
1886 GstRTSPTransport *trans;
1888 /* we need a non-NULL entry in the array */
1889 tr = g_array_index (transports, GstRTSPMediaTrans *, i);
1893 /* we need a transport */
1894 if (!(trans = tr->transport))
1897 /* get the stream and add the destinations */
1898 stream = gst_rtsp_media_get_stream (media, tr->idx);
1899 switch (trans->lower_transport) {
1900 case GST_RTSP_LOWER_TRANS_UDP:
1901 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1906 dest = trans->destination;
1907 if (trans->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1908 min = trans->port.min;
1909 max = trans->port.max;
1911 min = trans->client_port.min;
1912 max = trans->client_port.max;
1915 if (add && !tr->active) {
1916 add_udp_destination (media, stream, dest, min, max);
1917 stream->transports = g_list_prepend (stream->transports, tr);
1920 } else if (remove && tr->active) {
1921 remove_udp_destination (media, stream, dest, min, max);
1922 stream->transports = g_list_remove (stream->transports, tr);
1928 case GST_RTSP_LOWER_TRANS_TCP:
1929 if (add && !tr->active) {
1930 GST_INFO ("adding TCP %s", trans->destination);
1931 stream->transports = g_list_prepend (stream->transports, tr);
1934 } else if (remove && tr->active) {
1935 GST_INFO ("removing TCP %s", trans->destination);
1936 stream->transports = g_list_remove (stream->transports, tr);
1942 GST_INFO ("Unknown transport %d", trans->lower_transport);
1947 /* we just added the first media, do the playing state change */
1948 if (old_active == 0 && add)
1950 /* if we have no more active media, do the downward state changes */
1951 else if (media->active == 0)
1956 GST_INFO ("state %d active %d media %p do_state %d", state, media->active,
1959 if (media->target_state != state) {
1961 if (state == GST_STATE_NULL) {
1962 gst_rtsp_media_unprepare (media);
1964 GST_INFO ("state %s media %p", gst_element_state_get_name (state),
1966 media->target_state = state;
1967 ret = gst_element_set_state (media->pipeline, state);
1970 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
1974 /* remember where we are */
1975 if (state == GST_STATE_PAUSED || old_active != media->active)
1976 collect_media_stats (media);
1982 * gst_rtsp_media_remove_elements:
1983 * @media: a #GstRTSPMedia
1985 * Remove all elements and the pipeline controlled by @media.
1988 gst_rtsp_media_remove_elements (GstRTSPMedia * media)
1992 unlock_streams (media);
1994 for (i = 0; i < media->streams->len; i++) {
1995 GstRTSPMediaStream *stream;
1997 GST_INFO ("Removing elements of stream %d from pipeline", i);
1999 stream = g_array_index (media->streams, GstRTSPMediaStream *, i);
2001 gst_pad_unlink (stream->srcpad, stream->send_rtp_sink);
2003 g_signal_handler_disconnect (stream->send_rtp_sink, stream->caps_sig);
2005 for (j = 0; j < 2; j++) {
2006 gst_element_set_state (stream->udpsrc[j], GST_STATE_NULL);
2007 gst_element_set_state (stream->udpsink[j], GST_STATE_NULL);
2008 gst_element_set_state (stream->appsrc[j], GST_STATE_NULL);
2009 gst_element_set_state (stream->appsink[j], GST_STATE_NULL);
2010 gst_element_set_state (stream->tee[j], GST_STATE_NULL);
2011 gst_element_set_state (stream->selector[j], GST_STATE_NULL);
2013 gst_bin_remove (GST_BIN (media->pipeline), stream->udpsrc[j]);
2014 gst_bin_remove (GST_BIN (media->pipeline), stream->udpsink[j]);
2015 gst_bin_remove (GST_BIN (media->pipeline), stream->appsrc[j]);
2016 gst_bin_remove (GST_BIN (media->pipeline), stream->appsink[j]);
2017 gst_bin_remove (GST_BIN (media->pipeline), stream->tee[j]);
2018 gst_bin_remove (GST_BIN (media->pipeline), stream->selector[j]);
2021 gst_caps_unref (stream->caps);
2022 stream->caps = NULL;
2023 gst_rtsp_media_stream_free (stream);
2025 g_array_remove_range (media->streams, 0, media->streams->len);
2027 gst_element_set_state (media->rtpbin, GST_STATE_NULL);
2028 gst_bin_remove (GST_BIN (media->pipeline), media->rtpbin);
2030 gst_object_unref (media->pipeline);
2031 media->pipeline = NULL;