2 * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 #include <gst/app/gstappsrc.h>
24 #include <gst/app/gstappsink.h>
26 #include "rtsp-funnel.h"
27 #include "rtsp-media.h"
29 #define DEFAULT_SHARED FALSE
30 #define DEFAULT_REUSABLE FALSE
31 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_TCP
32 //#define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP_MCAST
33 #define DEFAULT_EOS_SHUTDOWN FALSE
34 #define DEFAULT_BUFFER_SIZE 0x80000
36 /* define to dump received RTCP packets */
58 GST_DEBUG_CATEGORY_STATIC (rtsp_media_debug);
59 #define GST_CAT_DEFAULT rtsp_media_debug
61 static GQuark ssrc_stream_map_key;
63 static void gst_rtsp_media_get_property (GObject * object, guint propid,
64 GValue * value, GParamSpec * pspec);
65 static void gst_rtsp_media_set_property (GObject * object, guint propid,
66 const GValue * value, GParamSpec * pspec);
67 static void gst_rtsp_media_finalize (GObject * obj);
69 static gpointer do_loop (GstRTSPMediaClass * klass);
70 static gboolean default_handle_message (GstRTSPMedia * media,
71 GstMessage * message);
72 static gboolean default_unprepare (GstRTSPMedia * media);
73 static void unlock_streams (GstRTSPMedia * media);
75 static guint gst_rtsp_media_signals[SIGNAL_LAST] = { 0 };
77 G_DEFINE_TYPE (GstRTSPMedia, gst_rtsp_media, G_TYPE_OBJECT);
80 gst_rtsp_media_class_init (GstRTSPMediaClass * klass)
82 GObjectClass *gobject_class;
85 gobject_class = G_OBJECT_CLASS (klass);
87 gobject_class->get_property = gst_rtsp_media_get_property;
88 gobject_class->set_property = gst_rtsp_media_set_property;
89 gobject_class->finalize = gst_rtsp_media_finalize;
91 g_object_class_install_property (gobject_class, PROP_SHARED,
92 g_param_spec_boolean ("shared", "Shared",
93 "If this media pipeline can be shared", DEFAULT_SHARED,
94 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
96 g_object_class_install_property (gobject_class, PROP_REUSABLE,
97 g_param_spec_boolean ("reusable", "Reusable",
98 "If this media pipeline can be reused after an unprepare",
99 DEFAULT_REUSABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
101 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
102 g_param_spec_flags ("protocols", "Protocols",
103 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
104 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
106 g_object_class_install_property (gobject_class, PROP_EOS_SHUTDOWN,
107 g_param_spec_boolean ("eos-shutdown", "EOS Shutdown",
108 "Send an EOS event to the pipeline before unpreparing",
109 DEFAULT_EOS_SHUTDOWN, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
111 g_object_class_install_property (gobject_class, PROP_BUFFER_SIZE,
112 g_param_spec_uint ("buffer-size", "Buffer Size",
113 "The kernel UDP buffer size to use", 0, G_MAXUINT,
114 DEFAULT_BUFFER_SIZE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
116 gst_rtsp_media_signals[SIGNAL_PREPARED] =
117 g_signal_new ("prepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
118 G_STRUCT_OFFSET (GstRTSPMediaClass, prepared), NULL, NULL,
119 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
121 gst_rtsp_media_signals[SIGNAL_UNPREPARED] =
122 g_signal_new ("unprepared", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
123 G_STRUCT_OFFSET (GstRTSPMediaClass, unprepared), NULL, NULL,
124 g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0, G_TYPE_NONE);
126 gst_rtsp_media_signals[SIGNAL_NEW_STATE] =
127 g_signal_new ("new-state", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
128 G_STRUCT_OFFSET (GstRTSPMediaClass, new_state), NULL, NULL,
129 g_cclosure_marshal_VOID__INT, G_TYPE_NONE, 0, G_TYPE_INT);
131 klass->context = g_main_context_new ();
132 klass->loop = g_main_loop_new (klass->context, TRUE);
134 GST_DEBUG_CATEGORY_INIT (rtsp_media_debug, "rtspmedia", 0, "GstRTSPMedia");
136 klass->thread = g_thread_create ((GThreadFunc) do_loop, klass, TRUE, &error);
138 g_critical ("could not start bus thread: %s", error->message);
140 klass->handle_message = default_handle_message;
141 klass->unprepare = default_unprepare;
143 ssrc_stream_map_key = g_quark_from_static_string ("GstRTSPServer.stream");
145 gst_element_register (NULL, "rtspfunnel", GST_RANK_NONE, RTSP_TYPE_FUNNEL);
150 gst_rtsp_media_init (GstRTSPMedia * media)
152 media->streams = g_array_new (FALSE, TRUE, sizeof (GstRTSPMediaStream *));
153 media->lock = g_mutex_new ();
154 media->cond = g_cond_new ();
156 media->shared = DEFAULT_SHARED;
157 media->reusable = DEFAULT_REUSABLE;
158 media->protocols = DEFAULT_PROTOCOLS;
159 media->eos_shutdown = DEFAULT_EOS_SHUTDOWN;
160 media->buffer_size = DEFAULT_BUFFER_SIZE;
164 gst_rtsp_media_trans_cleanup (GstRTSPMediaTrans * trans)
166 if (trans->transport) {
167 gst_rtsp_transport_free (trans->transport);
168 trans->transport = NULL;
170 if (trans->rtpsource) {
171 g_object_set_qdata (trans->rtpsource, ssrc_stream_map_key, NULL);
172 trans->rtpsource = NULL;
177 gst_rtsp_media_stream_free (GstRTSPMediaStream * stream)
180 g_object_unref (stream->session);
183 gst_caps_unref (stream->caps);
185 if (stream->send_rtp_sink)
186 gst_object_unref (stream->send_rtp_sink);
187 if (stream->send_rtp_src)
188 gst_object_unref (stream->send_rtp_src);
189 if (stream->send_rtcp_src)
190 gst_object_unref (stream->send_rtcp_src);
191 if (stream->recv_rtcp_sink)
192 gst_object_unref (stream->recv_rtcp_sink);
193 if (stream->recv_rtp_sink)
194 gst_object_unref (stream->recv_rtp_sink);
196 g_list_free (stream->transports);
202 gst_rtsp_media_finalize (GObject * obj)
207 media = GST_RTSP_MEDIA (obj);
209 GST_INFO ("finalize media %p", media);
211 if (media->pipeline) {
212 unlock_streams (media);
213 gst_element_set_state (media->pipeline, GST_STATE_NULL);
214 gst_object_unref (media->pipeline);
217 for (i = 0; i < media->streams->len; i++) {
218 GstRTSPMediaStream *stream;
220 stream = g_array_index (media->streams, GstRTSPMediaStream *, i);
222 gst_rtsp_media_stream_free (stream);
224 g_array_free (media->streams, TRUE);
226 g_list_foreach (media->dynamic, (GFunc) gst_object_unref, NULL);
227 g_list_free (media->dynamic);
230 g_source_destroy (media->source);
231 g_source_unref (media->source);
233 g_mutex_free (media->lock);
234 g_cond_free (media->cond);
236 G_OBJECT_CLASS (gst_rtsp_media_parent_class)->finalize (obj);
240 gst_rtsp_media_get_property (GObject * object, guint propid,
241 GValue * value, GParamSpec * pspec)
243 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
247 g_value_set_boolean (value, gst_rtsp_media_is_shared (media));
250 g_value_set_boolean (value, gst_rtsp_media_is_reusable (media));
253 g_value_set_flags (value, gst_rtsp_media_get_protocols (media));
255 case PROP_EOS_SHUTDOWN:
256 g_value_set_boolean (value, gst_rtsp_media_is_eos_shutdown (media));
258 case PROP_BUFFER_SIZE:
259 g_value_set_uint (value, gst_rtsp_media_get_buffer_size (media));
262 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
267 gst_rtsp_media_set_property (GObject * object, guint propid,
268 const GValue * value, GParamSpec * pspec)
270 GstRTSPMedia *media = GST_RTSP_MEDIA (object);
274 gst_rtsp_media_set_shared (media, g_value_get_boolean (value));
277 gst_rtsp_media_set_reusable (media, g_value_get_boolean (value));
280 gst_rtsp_media_set_protocols (media, g_value_get_flags (value));
282 case PROP_EOS_SHUTDOWN:
283 gst_rtsp_media_set_eos_shutdown (media, g_value_get_boolean (value));
285 case PROP_BUFFER_SIZE:
286 gst_rtsp_media_set_buffer_size (media, g_value_get_uint (value));
289 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, propid, pspec);
294 do_loop (GstRTSPMediaClass * klass)
296 GST_INFO ("enter mainloop");
297 g_main_loop_run (klass->loop);
298 GST_INFO ("exit mainloop");
304 collect_media_stats (GstRTSPMedia * media)
307 gint64 position, duration;
309 media->range.unit = GST_RTSP_RANGE_NPT;
311 if (media->is_live) {
312 media->range.min.type = GST_RTSP_TIME_NOW;
313 media->range.min.seconds = -1;
314 media->range.max.type = GST_RTSP_TIME_END;
315 media->range.max.seconds = -1;
317 /* get the position */
318 format = GST_FORMAT_TIME;
319 if (!gst_element_query_position (media->pipeline, &format, &position)) {
320 GST_INFO ("position query failed");
324 /* get the duration */
325 format = GST_FORMAT_TIME;
326 if (!gst_element_query_duration (media->pipeline, &format, &duration)) {
327 GST_INFO ("duration query failed");
331 GST_INFO ("stats: position %" GST_TIME_FORMAT ", duration %"
332 GST_TIME_FORMAT, GST_TIME_ARGS (position), GST_TIME_ARGS (duration));
334 if (position == -1) {
335 media->range.min.type = GST_RTSP_TIME_NOW;
336 media->range.min.seconds = -1;
338 media->range.min.type = GST_RTSP_TIME_SECONDS;
339 media->range.min.seconds = ((gdouble) position) / GST_SECOND;
341 if (duration == -1) {
342 media->range.max.type = GST_RTSP_TIME_END;
343 media->range.max.seconds = -1;
345 media->range.max.type = GST_RTSP_TIME_SECONDS;
346 media->range.max.seconds = ((gdouble) duration) / GST_SECOND;
352 * gst_rtsp_media_new:
354 * Create a new #GstRTSPMedia instance. The #GstRTSPMedia object contains the
355 * element to produde RTP data for one or more related (audio/video/..)
358 * Returns: a new #GstRTSPMedia object.
361 gst_rtsp_media_new (void)
363 GstRTSPMedia *result;
365 result = g_object_new (GST_TYPE_RTSP_MEDIA, NULL);
371 * gst_rtsp_media_set_shared:
372 * @media: a #GstRTSPMedia
373 * @shared: the new value
375 * Set or unset if the pipeline for @media can be shared will multiple clients.
376 * When @shared is %TRUE, client requests for this media will share the media
380 gst_rtsp_media_set_shared (GstRTSPMedia * media, gboolean shared)
382 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
384 media->shared = shared;
388 * gst_rtsp_media_is_shared:
389 * @media: a #GstRTSPMedia
391 * Check if the pipeline for @media can be shared between multiple clients.
393 * Returns: %TRUE if the media can be shared between clients.
396 gst_rtsp_media_is_shared (GstRTSPMedia * media)
398 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
400 return media->shared;
404 * gst_rtsp_media_set_reusable:
405 * @media: a #GstRTSPMedia
406 * @reusable: the new value
408 * Set or unset if the pipeline for @media can be reused after the pipeline has
412 gst_rtsp_media_set_reusable (GstRTSPMedia * media, gboolean reusable)
414 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
416 media->reusable = reusable;
420 * gst_rtsp_media_is_reusable:
421 * @media: a #GstRTSPMedia
423 * Check if the pipeline for @media can be reused after an unprepare.
425 * Returns: %TRUE if the media can be reused
428 gst_rtsp_media_is_reusable (GstRTSPMedia * media)
430 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
432 return media->reusable;
436 * gst_rtsp_media_set_protocols:
437 * @media: a #GstRTSPMedia
438 * @protocols: the new flags
440 * Configure the allowed lower transport for @media.
443 gst_rtsp_media_set_protocols (GstRTSPMedia * media, GstRTSPLowerTrans protocols)
445 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
447 media->protocols = protocols;
451 * gst_rtsp_media_get_protocols:
452 * @media: a #GstRTSPMedia
454 * Get the allowed protocols of @media.
456 * Returns: a #GstRTSPLowerTrans
459 gst_rtsp_media_get_protocols (GstRTSPMedia * media)
461 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media),
462 GST_RTSP_LOWER_TRANS_UNKNOWN);
464 return media->protocols;
468 * gst_rtsp_media_set_eos_shutdown:
469 * @media: a #GstRTSPMedia
470 * @eos_shutdown: the new value
472 * Set or unset if an EOS event will be sent to the pipeline for @media before
476 gst_rtsp_media_set_eos_shutdown (GstRTSPMedia * media, gboolean eos_shutdown)
478 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
480 media->eos_shutdown = eos_shutdown;
484 * gst_rtsp_media_is_eos_shutdown:
485 * @media: a #GstRTSPMedia
487 * Check if the pipeline for @media will send an EOS down the pipeline before
490 * Returns: %TRUE if the media will send EOS before unpreparing.
493 gst_rtsp_media_is_eos_shutdown (GstRTSPMedia * media)
495 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
497 return media->eos_shutdown;
501 * gst_rtsp_media_set_buffer_size:
502 * @media: a #GstRTSPMedia
503 * @size: the new value
505 * Set the kernel UDP buffer size.
508 gst_rtsp_media_set_buffer_size (GstRTSPMedia * media, guint size)
510 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
512 media->buffer_size = size;
516 * gst_rtsp_media_get_buffer_size:
517 * @media: a #GstRTSPMedia
519 * Get the kernel UDP buffer size.
521 * Returns: the kernel UDP buffer size.
524 gst_rtsp_media_get_buffer_size (GstRTSPMedia * media)
526 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
528 return media->buffer_size;
532 * gst_rtsp_media_set_auth:
533 * @media: a #GstRTSPMedia
534 * @auth: a #GstRTSPAuth
536 * configure @auth to be used as the authentication manager of @media.
539 gst_rtsp_media_set_auth (GstRTSPMedia * media, GstRTSPAuth * auth)
543 g_return_if_fail (GST_IS_RTSP_MEDIA (media));
552 g_object_unref (old);
557 * gst_rtsp_media_get_auth:
558 * @media: a #GstRTSPMedia
560 * Get the #GstRTSPAuth used as the authentication manager of @media.
562 * Returns: the #GstRTSPAuth of @media. g_object_unref() after
566 gst_rtsp_media_get_auth (GstRTSPMedia * media)
570 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
572 if ((result = media->auth))
573 g_object_ref (result);
580 * gst_rtsp_media_n_streams:
581 * @media: a #GstRTSPMedia
583 * Get the number of streams in this media.
585 * Returns: The number of streams.
588 gst_rtsp_media_n_streams (GstRTSPMedia * media)
590 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), 0);
592 return media->streams->len;
596 * gst_rtsp_media_get_stream:
597 * @media: a #GstRTSPMedia
598 * @idx: the stream index
600 * Retrieve the stream with index @idx from @media.
602 * Returns: the #GstRTSPMediaStream at index @idx or %NULL when a stream with
603 * that index did not exist.
606 gst_rtsp_media_get_stream (GstRTSPMedia * media, guint idx)
608 GstRTSPMediaStream *res;
610 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), NULL);
612 if (idx < media->streams->len)
613 res = g_array_index (media->streams, GstRTSPMediaStream *, idx);
621 * gst_rtsp_media_get_range_string:
622 * @media: a #GstRTSPMedia
623 * @play: for the PLAY request
625 * Get the current range as a string.
627 * Returns: The range as a string, g_free() after usage.
630 gst_rtsp_media_get_range_string (GstRTSPMedia * media, gboolean play)
633 GstRTSPTimeRange range;
636 range = media->range;
638 if (!play && media->active > 0) {
639 range.min.type = GST_RTSP_TIME_NOW;
640 range.min.seconds = -1;
643 result = gst_rtsp_range_to_string (&range);
649 * gst_rtsp_media_seek:
650 * @media: a #GstRTSPMedia
651 * @range: a #GstRTSPTimeRange
653 * Seek the pipeline to @range.
655 * Returns: %TRUE on success.
658 gst_rtsp_media_seek (GstRTSPMedia * media, GstRTSPTimeRange * range)
663 GstSeekType start_type, stop_type;
665 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
666 g_return_val_if_fail (range != NULL, FALSE);
668 if (range->unit != GST_RTSP_RANGE_NPT)
671 /* depends on the current playing state of the pipeline. We might need to
672 * queue this until we get EOS. */
673 flags = GST_SEEK_FLAG_FLUSH | GST_SEEK_FLAG_ACCURATE | GST_SEEK_FLAG_KEY_UNIT;
675 start_type = stop_type = GST_SEEK_TYPE_NONE;
677 switch (range->min.type) {
678 case GST_RTSP_TIME_NOW:
681 case GST_RTSP_TIME_SECONDS:
682 /* only seek when something changed */
683 if (media->range.min.seconds == range->min.seconds) {
686 start = range->min.seconds * GST_SECOND;
687 start_type = GST_SEEK_TYPE_SET;
690 case GST_RTSP_TIME_END:
694 switch (range->max.type) {
695 case GST_RTSP_TIME_SECONDS:
696 /* only seek when something changed */
697 if (media->range.max.seconds == range->max.seconds) {
700 stop = range->max.seconds * GST_SECOND;
701 stop_type = GST_SEEK_TYPE_SET;
704 case GST_RTSP_TIME_END:
706 stop_type = GST_SEEK_TYPE_SET;
708 case GST_RTSP_TIME_NOW:
713 if (start != -1 || stop != -1) {
714 GST_INFO ("seeking to %" GST_TIME_FORMAT " - %" GST_TIME_FORMAT,
715 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
717 res = gst_element_seek (media->pipeline, 1.0, GST_FORMAT_TIME,
718 flags, start_type, start, stop_type, stop);
720 /* and block for the seek to complete */
721 GST_INFO ("done seeking %d", res);
722 gst_element_get_state (media->pipeline, NULL, NULL, -1);
723 GST_INFO ("prerolled again");
725 collect_media_stats (media);
727 GST_INFO ("no seek needed");
736 GST_WARNING ("seek unit %d not supported", range->unit);
741 GST_WARNING ("weird range type %d not supported", range->min.type);
747 * gst_rtsp_media_stream_rtp:
748 * @stream: a #GstRTSPMediaStream
749 * @buffer: a #GstBuffer
751 * Handle an RTP buffer for the stream. This method is usually called when a
752 * message has been received from a client using the TCP transport.
754 * This function takes ownership of @buffer.
756 * Returns: a GstFlowReturn.
759 gst_rtsp_media_stream_rtp (GstRTSPMediaStream * stream, GstBuffer * buffer)
763 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (stream->appsrc[0]), buffer);
769 * gst_rtsp_media_stream_rtcp:
770 * @stream: a #GstRTSPMediaStream
771 * @buffer: a #GstBuffer
773 * Handle an RTCP buffer for the stream. This method is usually called when a
774 * message has been received from a client using the TCP transport.
776 * This function takes ownership of @buffer.
778 * Returns: a GstFlowReturn.
781 gst_rtsp_media_stream_rtcp (GstRTSPMediaStream * stream, GstBuffer * buffer)
785 ret = gst_app_src_push_buffer (GST_APP_SRC_CAST (stream->appsrc[1]), buffer);
790 /* Allocate the udp ports and sockets */
792 alloc_udp_ports (GstRTSPMedia * media, GstRTSPMediaStream * stream)
794 GstStateChangeReturn ret;
795 GstElement *udpsrc0, *udpsrc1;
796 GstElement *udpsink0, *udpsink1;
797 gint tmp_rtp, tmp_rtcp;
799 gint rtpport, rtcpport, sockfd;
808 /* Start with random port */
812 host = "udp://[::0]";
814 host = "udp://0.0.0.0";
816 /* try to allocate 2 UDP ports, the RTP port should be an even
817 * number and the RTCP port should be the next (uneven) port */
819 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
821 goto no_udp_protocol;
822 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, NULL);
824 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
825 if (ret == GST_STATE_CHANGE_FAILURE) {
831 gst_element_set_state (udpsrc0, GST_STATE_NULL);
832 gst_object_unref (udpsrc0);
836 goto no_udp_protocol;
839 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
841 /* check if port is even */
842 if ((tmp_rtp & 1) != 0) {
843 /* port not even, close and allocate another */
847 gst_element_set_state (udpsrc0, GST_STATE_NULL);
848 gst_object_unref (udpsrc0);
854 /* allocate port+1 for RTCP now */
855 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
857 goto no_udp_rtcp_protocol;
860 tmp_rtcp = tmp_rtp + 1;
861 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, NULL);
863 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
864 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
865 if (ret == GST_STATE_CHANGE_FAILURE) {
870 gst_element_set_state (udpsrc0, GST_STATE_NULL);
871 gst_object_unref (udpsrc0);
873 gst_element_set_state (udpsrc1, GST_STATE_NULL);
874 gst_object_unref (udpsrc1);
880 /* all fine, do port check */
881 g_object_get (G_OBJECT (udpsrc0), "port", &rtpport, NULL);
882 g_object_get (G_OBJECT (udpsrc1), "port", &rtcpport, NULL);
884 /* this should not happen... */
885 if (rtpport != tmp_rtp || rtcpport != tmp_rtcp)
888 udpsink0 = gst_element_factory_make ("multiudpsink", NULL);
890 goto no_udp_protocol;
892 g_object_get (G_OBJECT (udpsrc0), "sock", &sockfd, NULL);
893 g_object_set (G_OBJECT (udpsink0), "sockfd", sockfd, NULL);
894 g_object_set (G_OBJECT (udpsink0), "closefd", FALSE, NULL);
896 udpsink1 = gst_element_factory_make ("multiudpsink", NULL);
898 goto no_udp_protocol;
900 if (g_object_class_find_property (G_OBJECT_GET_CLASS (udpsink0),
901 "send-duplicates")) {
902 g_object_set (G_OBJECT (udpsink0), "send-duplicates", FALSE, NULL);
903 g_object_set (G_OBJECT (udpsink1), "send-duplicates", FALSE, NULL);
906 ("old multiudpsink version found without send-duplicates property");
909 if (g_object_class_find_property (G_OBJECT_GET_CLASS (udpsink0),
911 g_object_set (G_OBJECT (udpsink0), "buffer-size", media->buffer_size, NULL);
913 GST_WARNING ("multiudpsink version found without buffer-size property");
916 g_object_get (G_OBJECT (udpsrc1), "sock", &sockfd, NULL);
917 g_object_set (G_OBJECT (udpsink1), "sockfd", sockfd, NULL);
918 g_object_set (G_OBJECT (udpsink1), "closefd", FALSE, NULL);
919 g_object_set (G_OBJECT (udpsink1), "sync", FALSE, NULL);
920 g_object_set (G_OBJECT (udpsink1), "async", FALSE, NULL);
922 g_object_set (G_OBJECT (udpsink0), "auto-multicast", FALSE, NULL);
923 g_object_set (G_OBJECT (udpsink0), "loop", FALSE, NULL);
924 g_object_set (G_OBJECT (udpsink1), "auto-multicast", FALSE, NULL);
925 g_object_set (G_OBJECT (udpsink1), "loop", FALSE, NULL);
927 /* we keep these elements, we configure all in configure_transport when the
928 * server told us to really use the UDP ports. */
929 stream->udpsrc[0] = udpsrc0;
930 stream->udpsrc[1] = udpsrc1;
931 stream->udpsink[0] = udpsink0;
932 stream->udpsink[1] = udpsink1;
933 stream->server_port.min = rtpport;
934 stream->server_port.max = rtcpport;
947 no_udp_rtcp_protocol:
958 gst_element_set_state (udpsrc0, GST_STATE_NULL);
959 gst_object_unref (udpsrc0);
962 gst_element_set_state (udpsrc1, GST_STATE_NULL);
963 gst_object_unref (udpsrc1);
966 gst_element_set_state (udpsink0, GST_STATE_NULL);
967 gst_object_unref (udpsink0);
970 gst_element_set_state (udpsink1, GST_STATE_NULL);
971 gst_object_unref (udpsink1);
977 /* executed from streaming thread */
979 caps_notify (GstPad * pad, GParamSpec * unused, GstRTSPMediaStream * stream)
982 GstCaps *newcaps, *oldcaps;
984 if ((newcaps = GST_PAD_CAPS (pad)))
985 gst_caps_ref (newcaps);
987 oldcaps = stream->caps;
988 stream->caps = newcaps;
991 gst_caps_unref (oldcaps);
993 capsstr = gst_caps_to_string (newcaps);
994 GST_INFO ("stream %p received caps %p, %s", stream, newcaps, capsstr);
999 dump_structure (const GstStructure * s)
1003 sstr = gst_structure_to_string (s);
1004 GST_INFO ("structure: %s", sstr);
1008 static GstRTSPMediaTrans *
1009 find_transport (GstRTSPMediaStream * stream, const gchar * rtcp_from)
1012 GstRTSPMediaTrans *result = NULL;
1017 if (rtcp_from == NULL)
1020 tmp = g_strrstr (rtcp_from, ":");
1024 port = atoi (tmp + 1);
1025 dest = g_strndup (rtcp_from, tmp - rtcp_from);
1027 GST_INFO ("finding %s:%d", dest, port);
1029 for (walk = stream->transports; walk; walk = g_list_next (walk)) {
1030 GstRTSPMediaTrans *trans = walk->data;
1033 min = trans->transport->client_port.min;
1034 max = trans->transport->client_port.max;
1036 if ((strcmp (trans->transport->destination, dest) == 0) && (min == port
1048 on_new_ssrc (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1050 GstStructure *stats;
1051 GstRTSPMediaTrans *trans;
1053 GST_INFO ("%p: new source %p", stream, source);
1055 /* see if we have a stream to match with the origin of the RTCP packet */
1056 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1057 if (trans == NULL) {
1058 g_object_get (source, "stats", &stats, NULL);
1060 const gchar *rtcp_from;
1062 dump_structure (stats);
1064 rtcp_from = gst_structure_get_string (stats, "rtcp-from");
1065 if ((trans = find_transport (stream, rtcp_from))) {
1066 GST_INFO ("%p: found transport %p for source %p", stream, trans,
1069 /* keep ref to the source */
1070 trans->rtpsource = source;
1072 g_object_set_qdata (source, ssrc_stream_map_key, trans);
1074 gst_structure_free (stats);
1077 GST_INFO ("%p: source %p for transport %p", stream, source, trans);
1082 on_ssrc_sdes (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1084 GST_INFO ("%p: new SDES %p", stream, source);
1088 on_ssrc_active (GObject * session, GObject * source,
1089 GstRTSPMediaStream * stream)
1091 GstRTSPMediaTrans *trans;
1093 trans = g_object_get_qdata (source, ssrc_stream_map_key);
1095 GST_INFO ("%p: source %p in transport %p is active", stream, source, trans);
1097 if (trans && trans->keep_alive)
1098 trans->keep_alive (trans->ka_user_data);
1102 GstStructure *stats;
1103 g_object_get (source, "stats", &stats, NULL);
1105 dump_structure (stats);
1106 gst_structure_free (stats);
1113 on_bye_ssrc (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1115 GST_INFO ("%p: source %p bye", stream, source);
1119 on_bye_timeout (GObject * session, GObject * source,
1120 GstRTSPMediaStream * stream)
1122 GstRTSPMediaTrans *trans;
1124 GST_INFO ("%p: source %p bye timeout", stream, source);
1126 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1127 trans->rtpsource = NULL;
1128 trans->timeout = TRUE;
1133 on_timeout (GObject * session, GObject * source, GstRTSPMediaStream * stream)
1135 GstRTSPMediaTrans *trans;
1137 GST_INFO ("%p: source %p timeout", stream, source);
1139 if ((trans = g_object_get_qdata (source, ssrc_stream_map_key))) {
1140 trans->rtpsource = NULL;
1141 trans->timeout = TRUE;
1145 static GstFlowReturn
1146 handle_new_buffer (GstAppSink * sink, gpointer user_data)
1150 GstRTSPMediaStream *stream;
1152 buffer = gst_app_sink_pull_buffer (sink);
1156 stream = (GstRTSPMediaStream *) user_data;
1158 for (walk = stream->transports; walk; walk = g_list_next (walk)) {
1159 GstRTSPMediaTrans *tr = (GstRTSPMediaTrans *) walk->data;
1161 if (GST_ELEMENT_CAST (sink) == stream->appsink[0]) {
1163 tr->send_rtp (buffer, tr->transport->interleaved.min, tr->user_data);
1166 tr->send_rtcp (buffer, tr->transport->interleaved.max, tr->user_data);
1169 gst_buffer_unref (buffer);
1174 static GstFlowReturn
1175 handle_new_buffer_list (GstAppSink * sink, gpointer user_data)
1178 GstBufferList *blist;
1179 GstRTSPMediaStream *stream;
1181 blist = gst_app_sink_pull_buffer_list (sink);
1185 stream = (GstRTSPMediaStream *) user_data;
1187 for (walk = stream->transports; walk; walk = g_list_next (walk)) {
1188 GstRTSPMediaTrans *tr = (GstRTSPMediaTrans *) walk->data;
1190 if (GST_ELEMENT_CAST (sink) == stream->appsink[0]) {
1191 if (tr->send_rtp_list)
1192 tr->send_rtp_list (blist, tr->transport->interleaved.min,
1195 if (tr->send_rtcp_list)
1196 tr->send_rtcp_list (blist, tr->transport->interleaved.max,
1200 gst_buffer_list_unref (blist);
1205 static GstAppSinkCallbacks sink_cb = {
1206 NULL, /* not interested in EOS */
1207 NULL, /* not interested in preroll buffers */
1209 handle_new_buffer_list
1212 /* prepare the pipeline objects to handle @stream in @media */
1214 setup_stream (GstRTSPMediaStream * stream, guint idx, GstRTSPMedia * media)
1217 GstPad *pad, *teepad, *selpad;
1218 GstPadLinkReturn ret;
1221 /* allocate udp ports, we will have 4 of them, 2 for receiving RTP/RTCP and 2
1222 * for sending RTP/RTCP. The sender and receiver ports are shared between the
1224 if (!alloc_udp_ports (media, stream))
1227 /* add the ports to the pipeline */
1228 for (i = 0; i < 2; i++) {
1229 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->udpsink[i]);
1230 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->udpsrc[i]);
1233 /* create elements for the TCP transfer */
1234 for (i = 0; i < 2; i++) {
1235 stream->appsrc[i] = gst_element_factory_make ("appsrc", NULL);
1236 stream->appsink[i] = gst_element_factory_make ("appsink", NULL);
1237 g_object_set (stream->appsink[i], "async", FALSE, "sync", FALSE, NULL);
1238 g_object_set (stream->appsink[i], "emit-signals", FALSE, NULL);
1239 g_object_set (stream->appsink[i], "preroll-queue-len", 1, NULL);
1240 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->appsink[i]);
1241 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->appsrc[i]);
1242 gst_app_sink_set_callbacks (GST_APP_SINK_CAST (stream->appsink[i]),
1243 &sink_cb, stream, NULL);
1246 /* hook up the stream to the RTP session elements. */
1247 name = g_strdup_printf ("send_rtp_sink_%d", idx);
1248 stream->send_rtp_sink = gst_element_get_request_pad (media->rtpbin, name);
1250 name = g_strdup_printf ("send_rtp_src_%d", idx);
1251 stream->send_rtp_src = gst_element_get_static_pad (media->rtpbin, name);
1253 name = g_strdup_printf ("send_rtcp_src_%d", idx);
1254 stream->send_rtcp_src = gst_element_get_request_pad (media->rtpbin, name);
1256 name = g_strdup_printf ("recv_rtcp_sink_%d", idx);
1257 stream->recv_rtcp_sink = gst_element_get_request_pad (media->rtpbin, name);
1259 name = g_strdup_printf ("recv_rtp_sink_%d", idx);
1260 stream->recv_rtp_sink = gst_element_get_request_pad (media->rtpbin, name);
1263 /* get the session */
1264 g_signal_emit_by_name (media->rtpbin, "get-internal-session", idx,
1267 g_signal_connect (stream->session, "on-new-ssrc", (GCallback) on_new_ssrc,
1269 g_signal_connect (stream->session, "on-ssrc-sdes", (GCallback) on_ssrc_sdes,
1271 g_signal_connect (stream->session, "on-ssrc-active",
1272 (GCallback) on_ssrc_active, stream);
1273 g_signal_connect (stream->session, "on-bye-ssrc", (GCallback) on_bye_ssrc,
1275 g_signal_connect (stream->session, "on-bye-timeout",
1276 (GCallback) on_bye_timeout, stream);
1277 g_signal_connect (stream->session, "on-timeout", (GCallback) on_timeout,
1280 /* link the RTP pad to the session manager */
1281 ret = gst_pad_link (stream->srcpad, stream->send_rtp_sink);
1282 if (ret != GST_PAD_LINK_OK)
1285 /* make tee for RTP and link to stream */
1286 stream->tee[0] = gst_element_factory_make ("tee", NULL);
1287 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->tee[0]);
1289 pad = gst_element_get_static_pad (stream->tee[0], "sink");
1290 gst_pad_link (stream->send_rtp_src, pad);
1291 gst_object_unref (pad);
1293 /* link RTP sink, we're pretty sure this will work. */
1294 teepad = gst_element_get_request_pad (stream->tee[0], "src%d");
1295 pad = gst_element_get_static_pad (stream->udpsink[0], "sink");
1296 gst_pad_link (teepad, pad);
1297 gst_object_unref (pad);
1298 gst_object_unref (teepad);
1300 teepad = gst_element_get_request_pad (stream->tee[0], "src%d");
1301 pad = gst_element_get_static_pad (stream->appsink[0], "sink");
1302 gst_pad_link (teepad, pad);
1303 gst_object_unref (pad);
1304 gst_object_unref (teepad);
1306 /* make tee for RTCP */
1307 stream->tee[1] = gst_element_factory_make ("tee", NULL);
1308 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->tee[1]);
1310 pad = gst_element_get_static_pad (stream->tee[1], "sink");
1311 gst_pad_link (stream->send_rtcp_src, pad);
1312 gst_object_unref (pad);
1314 /* link RTCP elements */
1315 teepad = gst_element_get_request_pad (stream->tee[1], "src%d");
1316 pad = gst_element_get_static_pad (stream->udpsink[1], "sink");
1317 gst_pad_link (teepad, pad);
1318 gst_object_unref (pad);
1319 gst_object_unref (teepad);
1321 teepad = gst_element_get_request_pad (stream->tee[1], "src%d");
1322 pad = gst_element_get_static_pad (stream->appsink[1], "sink");
1323 gst_pad_link (teepad, pad);
1324 gst_object_unref (pad);
1325 gst_object_unref (teepad);
1327 /* make selector for the RTP receivers */
1328 stream->selector[0] = gst_element_factory_make ("rtspfunnel", NULL);
1329 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->selector[0]);
1331 pad = gst_element_get_static_pad (stream->selector[0], "src");
1332 gst_pad_link (pad, stream->recv_rtp_sink);
1333 gst_object_unref (pad);
1335 selpad = gst_element_get_request_pad (stream->selector[0], "sink%d");
1336 pad = gst_element_get_static_pad (stream->udpsrc[0], "src");
1337 gst_pad_link (pad, selpad);
1338 gst_object_unref (pad);
1339 gst_object_unref (selpad);
1341 selpad = gst_element_get_request_pad (stream->selector[0], "sink%d");
1342 pad = gst_element_get_static_pad (stream->appsrc[0], "src");
1343 gst_pad_link (pad, selpad);
1344 gst_object_unref (pad);
1345 gst_object_unref (selpad);
1347 /* make selector for the RTCP receivers */
1348 stream->selector[1] = gst_element_factory_make ("rtspfunnel", NULL);
1349 gst_bin_add (GST_BIN_CAST (media->pipeline), stream->selector[1]);
1351 pad = gst_element_get_static_pad (stream->selector[1], "src");
1352 gst_pad_link (pad, stream->recv_rtcp_sink);
1353 gst_object_unref (pad);
1355 selpad = gst_element_get_request_pad (stream->selector[1], "sink%d");
1356 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
1357 gst_pad_link (pad, selpad);
1358 gst_object_unref (pad);
1359 gst_object_unref (selpad);
1361 selpad = gst_element_get_request_pad (stream->selector[1], "sink%d");
1362 pad = gst_element_get_static_pad (stream->appsrc[1], "src");
1363 gst_pad_link (pad, selpad);
1364 gst_object_unref (pad);
1365 gst_object_unref (selpad);
1367 /* we set and keep these to playing so that they don't cause NO_PREROLL return
1369 gst_element_set_state (stream->udpsrc[0], GST_STATE_PLAYING);
1370 gst_element_set_state (stream->udpsrc[1], GST_STATE_PLAYING);
1371 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
1372 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
1374 /* be notified of caps changes */
1375 stream->caps_sig = g_signal_connect (stream->send_rtp_sink, "notify::caps",
1376 (GCallback) caps_notify, stream);
1378 stream->prepared = TRUE;
1385 GST_WARNING ("failed to link stream %d", idx);
1391 unlock_streams (GstRTSPMedia * media)
1395 /* unlock the udp src elements */
1396 n_streams = gst_rtsp_media_n_streams (media);
1397 for (i = 0; i < n_streams; i++) {
1398 GstRTSPMediaStream *stream;
1400 stream = gst_rtsp_media_get_stream (media, i);
1402 gst_element_set_locked_state (stream->udpsrc[0], FALSE);
1403 gst_element_set_locked_state (stream->udpsrc[1], FALSE);
1408 gst_rtsp_media_set_status (GstRTSPMedia * media, GstRTSPMediaStatus status)
1410 g_mutex_lock (media->lock);
1411 /* never overwrite the error status */
1412 if (media->status != GST_RTSP_MEDIA_STATUS_ERROR)
1413 media->status = status;
1414 GST_DEBUG ("setting new status to %d", status);
1415 g_cond_broadcast (media->cond);
1416 g_mutex_unlock (media->lock);
1419 static GstRTSPMediaStatus
1420 gst_rtsp_media_get_status (GstRTSPMedia * media)
1422 GstRTSPMediaStatus result;
1425 g_mutex_lock (media->lock);
1426 g_get_current_time (&timeout);
1427 g_time_val_add (&timeout, 20 * G_USEC_PER_SEC);
1428 /* while we are preparing, wait */
1429 while (media->status == GST_RTSP_MEDIA_STATUS_PREPARING) {
1430 GST_DEBUG ("waiting for status change");
1431 if (!g_cond_timed_wait (media->cond, media->lock, &timeout)) {
1432 GST_DEBUG ("timeout, assuming error status");
1433 media->status = GST_RTSP_MEDIA_STATUS_ERROR;
1436 /* could be success or error */
1437 result = media->status;
1438 GST_DEBUG ("got status %d", result);
1439 g_mutex_unlock (media->lock);
1445 default_handle_message (GstRTSPMedia * media, GstMessage * message)
1447 GstMessageType type;
1449 type = GST_MESSAGE_TYPE (message);
1452 case GST_MESSAGE_STATE_CHANGED:
1454 case GST_MESSAGE_BUFFERING:
1458 gst_message_parse_buffering (message, &percent);
1460 /* no state management needed for live pipelines */
1464 if (percent == 100) {
1465 /* a 100% message means buffering is done */
1466 media->buffering = FALSE;
1467 /* if the desired state is playing, go back */
1468 if (media->target_state == GST_STATE_PLAYING) {
1469 GST_INFO ("Buffering done, setting pipeline to PLAYING");
1470 gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1472 GST_INFO ("Buffering done");
1475 /* buffering busy */
1476 if (media->buffering == FALSE) {
1477 if (media->target_state == GST_STATE_PLAYING) {
1478 /* we were not buffering but PLAYING, PAUSE the pipeline. */
1479 GST_INFO ("Buffering, setting pipeline to PAUSED ...");
1480 gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1482 GST_INFO ("Buffering ...");
1485 media->buffering = TRUE;
1489 case GST_MESSAGE_LATENCY:
1491 gst_bin_recalculate_latency (GST_BIN_CAST (media->pipeline));
1494 case GST_MESSAGE_ERROR:
1499 gst_message_parse_error (message, &gerror, &debug);
1500 GST_WARNING ("%p: got error %s (%s)", media, gerror->message, debug);
1501 g_error_free (gerror);
1504 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_ERROR);
1507 case GST_MESSAGE_WARNING:
1512 gst_message_parse_warning (message, &gerror, &debug);
1513 GST_WARNING ("%p: got warning %s (%s)", media, gerror->message, debug);
1514 g_error_free (gerror);
1518 case GST_MESSAGE_ELEMENT:
1520 case GST_MESSAGE_STREAM_STATUS:
1522 case GST_MESSAGE_ASYNC_DONE:
1523 if (!media->adding) {
1524 /* when we are dynamically adding pads, the addition of the udpsrc will
1525 * temporarily produce ASYNC_DONE messages. We have to ignore them and
1526 * wait for the final ASYNC_DONE after everything prerolled */
1527 GST_INFO ("%p: got ASYNC_DONE", media);
1528 collect_media_stats (media);
1530 gst_rtsp_media_set_status (media, GST_RTSP_MEDIA_STATUS_PREPARED);
1532 GST_INFO ("%p: ignoring ASYNC_DONE", media);
1535 case GST_MESSAGE_EOS:
1536 GST_INFO ("%p: got EOS", media);
1537 if (media->eos_pending) {
1538 GST_DEBUG ("shutting down after EOS");
1539 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1540 media->eos_pending = FALSE;
1541 g_object_unref (media);
1545 GST_INFO ("%p: got message type %s", media,
1546 gst_message_type_get_name (type));
1553 bus_message (GstBus * bus, GstMessage * message, GstRTSPMedia * media)
1555 GstRTSPMediaClass *klass;
1558 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1560 if (klass->handle_message)
1561 ret = klass->handle_message (media, message);
1568 /* called from streaming threads */
1570 pad_added_cb (GstElement * element, GstPad * pad, GstRTSPMedia * media)
1572 GstRTSPMediaStream *stream;
1576 i = media->streams->len + 1;
1578 GST_INFO ("pad added %s:%s, stream %d", GST_DEBUG_PAD_NAME (pad), i);
1580 stream = g_new0 (GstRTSPMediaStream, 1);
1581 stream->payloader = element;
1583 name = g_strdup_printf ("dynpay%d", i);
1585 media->adding = TRUE;
1587 /* ghost the pad of the payloader to the element */
1588 stream->srcpad = gst_ghost_pad_new (name, pad);
1589 gst_pad_set_active (stream->srcpad, TRUE);
1590 gst_element_add_pad (media->element, stream->srcpad);
1593 /* add stream now */
1594 g_array_append_val (media->streams, stream);
1596 setup_stream (stream, i, media);
1598 for (i = 0; i < 2; i++) {
1599 gst_element_set_state (stream->udpsink[i], GST_STATE_PAUSED);
1600 gst_element_set_state (stream->appsink[i], GST_STATE_PAUSED);
1601 gst_element_set_state (stream->tee[i], GST_STATE_PAUSED);
1602 gst_element_set_state (stream->selector[i], GST_STATE_PAUSED);
1603 gst_element_set_state (stream->appsrc[i], GST_STATE_PAUSED);
1605 media->adding = FALSE;
1609 no_more_pads_cb (GstElement * element, GstRTSPMedia * media)
1611 GST_INFO ("no more pads");
1612 if (media->fakesink) {
1613 gst_object_ref (media->fakesink);
1614 gst_bin_remove (GST_BIN (media->pipeline), media->fakesink);
1615 gst_element_set_state (media->fakesink, GST_STATE_NULL);
1616 gst_object_unref (media->fakesink);
1617 media->fakesink = NULL;
1618 GST_INFO ("removed fakesink");
1623 * gst_rtsp_media_prepare:
1624 * @media: a #GstRTSPMedia
1626 * Prepare @media for streaming. This function will create the pipeline and
1627 * other objects to manage the streaming.
1629 * It will preroll the pipeline and collect vital information about the streams
1630 * such as the duration.
1632 * Returns: %TRUE on success.
1635 gst_rtsp_media_prepare (GstRTSPMedia * media)
1637 GstStateChangeReturn ret;
1638 GstRTSPMediaStatus status;
1640 GstRTSPMediaClass *klass;
1644 if (media->status == GST_RTSP_MEDIA_STATUS_PREPARED)
1647 if (!media->reusable && media->reused)
1650 GST_INFO ("preparing media %p", media);
1652 /* reset some variables */
1653 media->is_live = FALSE;
1654 media->buffering = FALSE;
1655 /* we're preparing now */
1656 media->status = GST_RTSP_MEDIA_STATUS_PREPARING;
1658 bus = gst_pipeline_get_bus (GST_PIPELINE_CAST (media->pipeline));
1660 /* add the pipeline bus to our custom mainloop */
1661 media->source = gst_bus_create_watch (bus);
1662 gst_object_unref (bus);
1664 g_source_set_callback (media->source, (GSourceFunc) bus_message, media, NULL);
1666 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1667 media->id = g_source_attach (media->source, klass->context);
1669 media->rtpbin = gst_element_factory_make ("gstrtpbin", NULL);
1671 /* add stuff to the bin */
1672 gst_bin_add (GST_BIN (media->pipeline), media->rtpbin);
1674 /* link streams we already have, other streams might appear when we have
1675 * dynamic elements */
1676 n_streams = gst_rtsp_media_n_streams (media);
1677 for (i = 0; i < n_streams; i++) {
1678 GstRTSPMediaStream *stream;
1680 stream = gst_rtsp_media_get_stream (media, i);
1682 setup_stream (stream, i, media);
1685 for (walk = media->dynamic; walk; walk = g_list_next (walk)) {
1686 GstElement *elem = walk->data;
1688 GST_INFO ("adding callbacks for dynamic element %p", elem);
1690 g_signal_connect (elem, "pad-added", (GCallback) pad_added_cb, media);
1691 g_signal_connect (elem, "no-more-pads", (GCallback) no_more_pads_cb, media);
1693 /* we add a fakesink here in order to make the state change async. We remove
1694 * the fakesink again in the no-more-pads callback. */
1695 media->fakesink = gst_element_factory_make ("fakesink", "fakesink");
1696 gst_bin_add (GST_BIN (media->pipeline), media->fakesink);
1699 GST_INFO ("setting pipeline to PAUSED for media %p", media);
1700 /* first go to PAUSED */
1701 ret = gst_element_set_state (media->pipeline, GST_STATE_PAUSED);
1702 media->target_state = GST_STATE_PAUSED;
1705 case GST_STATE_CHANGE_SUCCESS:
1706 GST_INFO ("SUCCESS state change for media %p", media);
1708 case GST_STATE_CHANGE_ASYNC:
1709 GST_INFO ("ASYNC state change for media %p", media);
1711 case GST_STATE_CHANGE_NO_PREROLL:
1712 /* we need to go to PLAYING */
1713 GST_INFO ("NO_PREROLL state change: live media %p", media);
1714 media->is_live = TRUE;
1715 ret = gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1716 if (ret == GST_STATE_CHANGE_FAILURE)
1719 case GST_STATE_CHANGE_FAILURE:
1723 /* now wait for all pads to be prerolled */
1724 status = gst_rtsp_media_get_status (media);
1725 if (status == GST_RTSP_MEDIA_STATUS_ERROR)
1728 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_PREPARED], 0, NULL);
1730 GST_INFO ("object %p is prerolled", media);
1742 GST_WARNING ("can not reuse media %p", media);
1747 GST_WARNING ("failed to preroll pipeline");
1748 unlock_streams (media);
1749 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1750 gst_rtsp_media_unprepare (media);
1756 * gst_rtsp_media_unprepare:
1757 * @media: a #GstRTSPMedia
1759 * Unprepare @media. After this call, the media should be prepared again before
1760 * it can be used again. If the media is set to be non-reusable, a new instance
1763 * Returns: %TRUE on success.
1766 gst_rtsp_media_unprepare (GstRTSPMedia * media)
1768 GstRTSPMediaClass *klass;
1771 if (media->status == GST_RTSP_MEDIA_STATUS_UNPREPARED)
1774 GST_INFO ("unprepare media %p", media);
1775 media->target_state = GST_STATE_NULL;
1777 klass = GST_RTSP_MEDIA_GET_CLASS (media);
1778 if (klass->unprepare)
1779 success = klass->unprepare (media);
1783 media->status = GST_RTSP_MEDIA_STATUS_UNPREPARED;
1784 media->reused = TRUE;
1786 /* when the media is not reusable, this will effectively unref the media and
1788 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_UNPREPARED], 0, NULL);
1794 default_unprepare (GstRTSPMedia * media)
1796 if (media->eos_shutdown) {
1797 GST_DEBUG ("sending EOS for shutdown");
1798 /* ref so that we don't disappear */
1799 g_object_ref (media);
1800 media->eos_pending = TRUE;
1801 gst_element_send_event (media->pipeline, gst_event_new_eos ());
1802 /* we need to go to playing again for the EOS to propagate, normally in this
1803 * state, nothing is receiving data from us anymore so this is ok. */
1804 gst_element_set_state (media->pipeline, GST_STATE_PLAYING);
1806 GST_DEBUG ("shutting down");
1807 gst_element_set_state (media->pipeline, GST_STATE_NULL);
1813 add_udp_destination (GstRTSPMedia * media, GstRTSPMediaStream * stream,
1814 gchar * dest, gint min, gint max)
1816 GST_INFO ("adding %s:%d-%d", dest, min, max);
1817 g_signal_emit_by_name (stream->udpsink[0], "add", dest, min, NULL);
1818 g_signal_emit_by_name (stream->udpsink[1], "add", dest, max, NULL);
1822 remove_udp_destination (GstRTSPMedia * media, GstRTSPMediaStream * stream,
1823 gchar * dest, gint min, gint max)
1825 GST_INFO ("removing %s:%d-%d", dest, min, max);
1826 g_signal_emit_by_name (stream->udpsink[0], "remove", dest, min, NULL);
1827 g_signal_emit_by_name (stream->udpsink[1], "remove", dest, max, NULL);
1831 * gst_rtsp_media_set_state:
1832 * @media: a #GstRTSPMedia
1833 * @state: the target state of the media
1834 * @transports: a #GArray of #GstRTSPMediaTrans pointers
1836 * Set the state of @media to @state and for the transports in @transports.
1838 * Returns: %TRUE on success.
1841 gst_rtsp_media_set_state (GstRTSPMedia * media, GstState state,
1842 GArray * transports)
1845 gboolean add, remove, do_state;
1848 g_return_val_if_fail (GST_IS_RTSP_MEDIA (media), FALSE);
1849 g_return_val_if_fail (transports != NULL, FALSE);
1851 /* NULL and READY are the same */
1852 if (state == GST_STATE_READY)
1853 state = GST_STATE_NULL;
1855 add = remove = FALSE;
1857 GST_INFO ("going to state %s media %p", gst_element_state_get_name (state),
1861 case GST_STATE_NULL:
1862 /* unlock the streams so that they follow the state changes from now on */
1863 unlock_streams (media);
1865 case GST_STATE_PAUSED:
1866 /* we're going from PLAYING to PAUSED, READY or NULL, remove */
1867 if (media->target_state == GST_STATE_PLAYING)
1870 case GST_STATE_PLAYING:
1871 /* we're going to PLAYING, add */
1877 old_active = media->active;
1879 for (i = 0; i < transports->len; i++) {
1880 GstRTSPMediaTrans *tr;
1881 GstRTSPMediaStream *stream;
1882 GstRTSPTransport *trans;
1884 /* we need a non-NULL entry in the array */
1885 tr = g_array_index (transports, GstRTSPMediaTrans *, i);
1889 /* we need a transport */
1890 if (!(trans = tr->transport))
1893 /* get the stream and add the destinations */
1894 stream = gst_rtsp_media_get_stream (media, tr->idx);
1895 switch (trans->lower_transport) {
1896 case GST_RTSP_LOWER_TRANS_UDP:
1897 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
1902 dest = trans->destination;
1903 if (trans->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
1904 min = trans->port.min;
1905 max = trans->port.max;
1907 min = trans->client_port.min;
1908 max = trans->client_port.max;
1911 if (add && !tr->active) {
1912 add_udp_destination (media, stream, dest, min, max);
1913 stream->transports = g_list_prepend (stream->transports, tr);
1916 } else if (remove && tr->active) {
1917 remove_udp_destination (media, stream, dest, min, max);
1918 stream->transports = g_list_remove (stream->transports, tr);
1924 case GST_RTSP_LOWER_TRANS_TCP:
1925 if (add && !tr->active) {
1926 GST_INFO ("adding TCP %s", trans->destination);
1927 stream->transports = g_list_prepend (stream->transports, tr);
1930 } else if (remove && tr->active) {
1931 GST_INFO ("removing TCP %s", trans->destination);
1932 stream->transports = g_list_remove (stream->transports, tr);
1938 GST_INFO ("Unknown transport %d", trans->lower_transport);
1943 /* we just added the first media, do the playing state change */
1944 if (old_active == 0 && add)
1946 /* if we have no more active media, do the downward state changes */
1947 else if (media->active == 0)
1952 GST_INFO ("state %d active %d media %p do_state %d", state, media->active,
1955 if (media->target_state != state) {
1957 if (state == GST_STATE_NULL) {
1958 gst_rtsp_media_unprepare (media);
1960 GST_INFO ("state %s media %p", gst_element_state_get_name (state),
1962 media->target_state = state;
1963 gst_element_set_state (media->pipeline, state);
1966 g_signal_emit (media, gst_rtsp_media_signals[SIGNAL_NEW_STATE], 0, state,
1970 /* remember where we are */
1971 if (state == GST_STATE_PAUSED || old_active != media->active)
1972 collect_media_stats (media);
1978 * gst_rtsp_media_remove_elements:
1979 * @media: a #GstRTSPMedia
1981 * Remove all elements and the pipeline controlled by @media.
1984 gst_rtsp_media_remove_elements (GstRTSPMedia * media)
1988 unlock_streams (media);
1990 for (i = 0; i < media->streams->len; i++) {
1991 GstRTSPMediaStream *stream;
1993 GST_INFO ("Removing elements of stream %d from pipeline", i);
1995 stream = g_array_index (media->streams, GstRTSPMediaStream *, i);
1997 gst_pad_unlink (stream->srcpad, stream->send_rtp_sink);
1999 g_signal_handler_disconnect (stream->send_rtp_sink, stream->caps_sig);
2001 for (j = 0; j < 2; j++) {
2002 gst_element_set_state (stream->udpsrc[j], GST_STATE_NULL);
2003 gst_element_set_state (stream->udpsink[j], GST_STATE_NULL);
2004 gst_element_set_state (stream->appsrc[j], GST_STATE_NULL);
2005 gst_element_set_state (stream->appsink[j], GST_STATE_NULL);
2006 gst_element_set_state (stream->tee[j], GST_STATE_NULL);
2007 gst_element_set_state (stream->selector[j], GST_STATE_NULL);
2009 gst_bin_remove (GST_BIN (media->pipeline), stream->udpsrc[j]);
2010 gst_bin_remove (GST_BIN (media->pipeline), stream->udpsink[j]);
2011 gst_bin_remove (GST_BIN (media->pipeline), stream->appsrc[j]);
2012 gst_bin_remove (GST_BIN (media->pipeline), stream->appsink[j]);
2013 gst_bin_remove (GST_BIN (media->pipeline), stream->tee[j]);
2014 gst_bin_remove (GST_BIN (media->pipeline), stream->selector[j]);
2017 gst_caps_unref (stream->caps);
2018 stream->caps = NULL;
2019 gst_rtsp_media_stream_free (stream);
2021 g_array_remove_range (media->streams, 0, media->streams->len);
2023 gst_element_set_state (media->rtpbin, GST_STATE_NULL);
2024 gst_bin_remove (GST_BIN (media->pipeline), media->rtpbin);
2026 gst_object_unref (media->pipeline);
2027 media->pipeline = NULL;