2 * Copyright (C) <2005,2006> Wim Taymans <wim at fluendo dot com>
3 * <2006> Lutz Mueller <lutz at topfrose dot de>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 * Unless otherwise indicated, Source Code is licensed under MIT license.
22 * See further explanation attached in License Statement (distributed in the file
25 * Permission is hereby granted, free of charge, to any person obtaining a copy of
26 * this software and associated documentation files (the "Software"), to deal in
27 * the Software without restriction, including without limitation the rights to
28 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
29 * of the Software, and to permit persons to whom the Software is furnished to do
30 * so, subject to the following conditions:
32 * The above copyright notice and this permission notice shall be included in all
33 * copies or substantial portions of the Software.
35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44 * SECTION:element-rtspsrc
46 * Makes a connection to an RTSP server and read the data.
47 * rtspsrc strictly follows RFC 2326 and therefore does not (yet) support
48 * RealMedia/Quicktime/Microsoft extensions.
50 * RTSP supports transport over TCP or UDP in unicast or multicast mode. By
51 * default rtspsrc will negotiate a connection in the following order:
52 * UDP unicast/UDP multicast/TCP. The order cannot be changed but the allowed
53 * protocols can be controlled with the #GstRTSPSrc:protocols property.
55 * rtspsrc currently understands SDP as the format of the session description.
56 * For each stream listed in the SDP a new rtp_stream%d pad will be created
57 * with caps derived from the SDP media description. This is a caps of mime type
58 * "application/x-rtp" that can be connected to any available RTP depayloader
61 * rtspsrc will internally instantiate an RTP session manager element
62 * that will handle the RTCP messages to and from the server, jitter removal,
63 * packet reordering along with providing a clock for the pipeline.
64 * This feature is implemented using the gstrtpbin element.
66 * rtspsrc acts like a live source and will therefore only generate data in the
70 * <title>Example launch line</title>
72 * gst-launch rtspsrc location=rtsp://some.server/url ! fakesink
73 * ]| Establish a connection to an RTSP server and send the raw RTP packets to a
77 * Last reviewed on 2006-08-18 (0.10.5)
84 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
85 * with newer GLib versions (>= 2.31.0) */
86 #define GLIB_DISABLE_DEPRECATION_WARNINGS
90 #endif /* HAVE_UNISTD_H */
97 #include <gst/sdp/gstsdpmessage.h>
98 #include <gst/rtp/gstrtppayloads.h>
100 #include "gst/gst-i18n-plugin.h"
102 #include "gstrtspsrc.h"
104 GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
105 #define GST_CAT_DEFAULT (rtspsrc_debug)
107 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream_%u",
110 GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
112 /* templates used internally */
113 static GstStaticPadTemplate anysrctemplate =
114 GST_STATIC_PAD_TEMPLATE ("internalsrc_%u",
117 GST_STATIC_CAPS_ANY);
119 static GstStaticPadTemplate anysinktemplate =
120 GST_STATIC_PAD_TEMPLATE ("internalsink_%u",
123 GST_STATIC_CAPS_ANY);
131 enum _GstRtspSrcRtcpSyncMode
138 enum _GstRtspSrcBufferMode
146 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
148 gst_rtsp_src_buffer_mode_get_type (void)
150 static GType buffer_mode_type = 0;
151 static const GEnumValue buffer_modes[] = {
152 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
153 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
154 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
155 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
159 if (!buffer_mode_type) {
161 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
163 return buffer_mode_type;
166 #define DEFAULT_LOCATION NULL
167 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
168 #define DEFAULT_DEBUG FALSE
169 #define DEFAULT_RETRY 20
170 #define DEFAULT_TIMEOUT 5000000
171 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
172 #define DEFAULT_TCP_TIMEOUT 20000000
173 #define DEFAULT_LATENCY_MS 2000
174 #define DEFAULT_CONNECTION_SPEED 0
175 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
176 #define DEFAULT_DO_RTCP TRUE
177 #define DEFAULT_PROXY NULL
178 #define DEFAULT_RTP_BLOCKSIZE 0
179 #define DEFAULT_USER_ID NULL
180 #define DEFAULT_USER_PW NULL
181 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
182 #define DEFAULT_PORT_RANGE NULL
183 #define DEFAULT_SHORT_HEADER FALSE
195 PROP_CONNECTION_SPEED,
204 PROP_UDP_BUFFER_SIZE,
209 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
211 gst_rtsp_nat_method_get_type (void)
213 static GType rtsp_nat_method_type = 0;
214 static const GEnumValue rtsp_nat_method[] = {
215 {GST_RTSP_NAT_NONE, "None", "none"},
216 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
220 if (!rtsp_nat_method_type) {
221 rtsp_nat_method_type =
222 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
224 return rtsp_nat_method_type;
227 static void gst_rtspsrc_finalize (GObject * object);
229 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
230 const GValue * value, GParamSpec * pspec);
231 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
232 GValue * value, GParamSpec * pspec);
234 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
235 gpointer iface_data);
237 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
240 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
241 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
243 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
245 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
246 GstStateChange transition);
247 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
248 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
250 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
251 GstRTSPMessage * response);
253 static void gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd);
254 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
255 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
257 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
258 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
260 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean idle,
262 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
263 gboolean only_close);
265 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
266 const gchar * uri, GError ** error);
268 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
269 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
270 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
271 GstRTSPStream * stream, GstEvent * event, gboolean source);
272 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event,
275 /* commands we send to out loop to notify it of events */
281 #define CMD_RECONNECT 5
284 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
286 gchar *__txt = _gst_element_error_printf text; \
287 gst_element_post_message (GST_ELEMENT_CAST (el), \
288 gst_message_new_progress (GST_OBJECT_CAST (el), \
289 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
293 /*static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 }; */
294 #define gst_rtspsrc_parent_class parent_class
295 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
296 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
299 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
301 GObjectClass *gobject_class;
302 GstElementClass *gstelement_class;
303 GstBinClass *gstbin_class;
305 gobject_class = (GObjectClass *) klass;
306 gstelement_class = (GstElementClass *) klass;
307 gstbin_class = (GstBinClass *) klass;
309 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
311 gobject_class->set_property = gst_rtspsrc_set_property;
312 gobject_class->get_property = gst_rtspsrc_get_property;
314 gobject_class->finalize = gst_rtspsrc_finalize;
316 g_object_class_install_property (gobject_class, PROP_LOCATION,
317 g_param_spec_string ("location", "RTSP Location",
318 "Location of the RTSP url to read",
319 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
321 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
322 g_param_spec_flags ("protocols", "Protocols",
323 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
324 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326 g_object_class_install_property (gobject_class, PROP_DEBUG,
327 g_param_spec_boolean ("debug", "Debug",
328 "Dump request and response messages to stdout",
329 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
331 g_object_class_install_property (gobject_class, PROP_RETRY,
332 g_param_spec_uint ("retry", "Retry",
333 "Max number of retries when allocating RTP ports.",
334 0, G_MAXUINT16, DEFAULT_RETRY,
335 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
337 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
338 g_param_spec_uint64 ("timeout", "Timeout",
339 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
340 0, G_MAXUINT64, DEFAULT_TIMEOUT,
341 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
343 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
344 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
345 "Fail after timeout microseconds on TCP connections (0 = disabled)",
346 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
347 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
349 g_object_class_install_property (gobject_class, PROP_LATENCY,
350 g_param_spec_uint ("latency", "Buffer latency in ms",
351 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
352 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
354 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
355 g_param_spec_uint64 ("connection-speed", "Connection Speed",
356 "Network connection speed in kbps (0 = unknown)",
357 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
358 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
360 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
361 g_param_spec_enum ("nat-method", "NAT Method",
362 "Method to use for traversing firewalls and NAT",
363 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
364 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
367 * GstRTSPSrc::do-rtcp
369 * Enable RTCP support. Some old server don't like RTCP and then this property
370 * needs to be set to FALSE.
374 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
375 g_param_spec_boolean ("do-rtcp", "Do RTCP",
376 "Send RTCP packets, disable for old incompatible server.",
377 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
382 * Set the proxy parameters. This has to be a string of the format
383 * [http://][user:passwd@]host[:port].
387 g_object_class_install_property (gobject_class, PROP_PROXY,
388 g_param_spec_string ("proxy", "Proxy",
389 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
390 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
393 * GstRTSPSrc::rtp_blocksize
395 * RTP package size to suggest to server.
399 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
400 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
401 "RTP package size to suggest to server (0 = disabled)",
402 0, 65536, DEFAULT_RTP_BLOCKSIZE,
403 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
405 g_object_class_install_property (gobject_class,
407 g_param_spec_string ("user-id", "user-id",
408 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
409 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
410 g_object_class_install_property (gobject_class, PROP_USER_PW,
411 g_param_spec_string ("user-pw", "user-pw",
412 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
413 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
416 * GstRTSPSrc::buffer-mode:
418 * Control the buffering and timestamping mode used by the jitterbuffer.
422 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
423 g_param_spec_enum ("buffer-mode", "Buffer Mode",
424 "Control the buffering algorithm in use",
425 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
426 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
429 * GstRTSPSrc::port-range:
431 * Configure the client port numbers that can be used to recieve RTP and
436 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
437 g_param_spec_string ("port-range", "Port range",
438 "Client port range that can be used to receive RTP and RTCP data, "
439 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
440 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
443 * GstRTSPSrc::udp-buffer-size:
445 * Size of the kernel UDP receive buffer in bytes.
449 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
450 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
451 "Size of the kernel UDP receive buffer in bytes, 0=default",
452 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
453 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
456 * GstRTSPSrc::short-header:
458 * Only send the basic RTSP headers for broken encoders.
462 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
463 g_param_spec_boolean ("short-header", "Short Header",
464 "Only send the basic RTSP headers for broken encoders",
465 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
467 gstelement_class->send_event = gst_rtspsrc_send_event;
468 gstelement_class->change_state = gst_rtspsrc_change_state;
470 gst_element_class_add_pad_template (gstelement_class,
471 gst_static_pad_template_get (&rtptemplate));
473 gst_element_class_set_details_simple (gstelement_class,
474 "RTSP packet receiver", "Source/Network",
475 "Receive data over the network via RTSP (RFC 2326)",
476 "Wim Taymans <wim@fluendo.com>, "
477 "Thijs Vermeir <thijs.vermeir@barco.com>, "
478 "Lutz Mueller <lutz@topfrose.de>");
480 gstbin_class->handle_message = gst_rtspsrc_handle_message;
482 gst_rtsp_ext_list_init ();
487 gst_rtspsrc_init (GstRTSPSrc * src)
489 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
490 src->protocols = DEFAULT_PROTOCOLS;
491 src->debug = DEFAULT_DEBUG;
492 src->retry = DEFAULT_RETRY;
493 src->udp_timeout = DEFAULT_TIMEOUT;
494 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
495 src->latency = DEFAULT_LATENCY_MS;
496 src->connection_speed = DEFAULT_CONNECTION_SPEED;
497 src->nat_method = DEFAULT_NAT_METHOD;
498 src->do_rtcp = DEFAULT_DO_RTCP;
499 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
500 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
501 src->user_id = g_strdup (DEFAULT_USER_ID);
502 src->user_pw = g_strdup (DEFAULT_USER_PW);
503 src->buffer_mode = DEFAULT_BUFFER_MODE;
504 src->client_port_range.min = 0;
505 src->client_port_range.max = 0;
506 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
507 src->short_header = DEFAULT_SHORT_HEADER;
509 /* get a list of all extensions */
510 src->extensions = gst_rtsp_ext_list_get ();
512 /* connect to send signal */
513 gst_rtsp_ext_list_connect (src->extensions, "send",
514 (GCallback) gst_rtspsrc_send_cb, src);
516 /* protects the streaming thread in interleaved mode or the polling
517 * thread in UDP mode. */
518 src->stream_rec_lock = g_new (GStaticRecMutex, 1);
519 g_static_rec_mutex_init (src->stream_rec_lock);
521 /* protects our state changes from multiple invocations */
522 src->state_rec_lock = g_new (GStaticRecMutex, 1);
523 g_static_rec_mutex_init (src->state_rec_lock);
525 src->state = GST_RTSP_STATE_INVALID;
527 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
531 gst_rtspsrc_finalize (GObject * object)
535 rtspsrc = GST_RTSPSRC (object);
537 gst_rtsp_ext_list_free (rtspsrc->extensions);
538 g_free (rtspsrc->conninfo.location);
539 gst_rtsp_url_free (rtspsrc->conninfo.url);
540 g_free (rtspsrc->conninfo.url_str);
541 g_free (rtspsrc->user_id);
542 g_free (rtspsrc->user_pw);
545 gst_sdp_message_free (rtspsrc->sdp);
550 g_static_rec_mutex_free (rtspsrc->stream_rec_lock);
551 g_free (rtspsrc->stream_rec_lock);
552 g_static_rec_mutex_free (rtspsrc->state_rec_lock);
553 g_free (rtspsrc->state_rec_lock);
555 G_OBJECT_CLASS (parent_class)->finalize (object);
558 /* a proxy string of the format [user:passwd@]host[:port] */
560 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
564 g_free (rtsp->proxy_user);
565 rtsp->proxy_user = NULL;
566 g_free (rtsp->proxy_passwd);
567 rtsp->proxy_passwd = NULL;
568 g_free (rtsp->proxy_host);
569 rtsp->proxy_host = NULL;
570 rtsp->proxy_port = 0;
577 /* we allow http:// in front but ignore it */
578 if (g_str_has_prefix (p, "http://"))
581 at = strchr (p, '@');
583 /* look for user:passwd */
584 col = strchr (proxy, ':');
585 if (col == NULL || col > at)
588 rtsp->proxy_user = g_strndup (p, col - p);
590 rtsp->proxy_passwd = g_strndup (col, at - col);
595 col = strchr (p, ':');
598 /* everything before the colon is the hostname */
599 rtsp->proxy_host = g_strndup (p, col - p);
601 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
603 rtsp->proxy_host = g_strdup (p);
604 rtsp->proxy_port = 8080;
610 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
612 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
613 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
616 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
618 rtspsrc->ptcp_timeout = NULL;
622 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
627 rtspsrc = GST_RTSPSRC (object);
631 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
632 g_value_get_string (value), NULL);
635 rtspsrc->protocols = g_value_get_flags (value);
638 rtspsrc->debug = g_value_get_boolean (value);
641 rtspsrc->retry = g_value_get_uint (value);
644 rtspsrc->udp_timeout = g_value_get_uint64 (value);
646 case PROP_TCP_TIMEOUT:
647 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
650 rtspsrc->latency = g_value_get_uint (value);
652 case PROP_CONNECTION_SPEED:
653 rtspsrc->connection_speed = g_value_get_uint64 (value);
655 case PROP_NAT_METHOD:
656 rtspsrc->nat_method = g_value_get_enum (value);
659 rtspsrc->do_rtcp = g_value_get_boolean (value);
662 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
664 case PROP_RTP_BLOCKSIZE:
665 rtspsrc->rtp_blocksize = g_value_get_uint (value);
668 if (rtspsrc->user_id)
669 g_free (rtspsrc->user_id);
670 rtspsrc->user_id = g_value_dup_string (value);
673 if (rtspsrc->user_pw)
674 g_free (rtspsrc->user_pw);
675 rtspsrc->user_pw = g_value_dup_string (value);
677 case PROP_BUFFER_MODE:
678 rtspsrc->buffer_mode = g_value_get_enum (value);
680 case PROP_PORT_RANGE:
684 str = g_value_get_string (value);
686 sscanf (str, "%u-%u",
687 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
689 rtspsrc->client_port_range.min = 0;
690 rtspsrc->client_port_range.max = 0;
694 case PROP_UDP_BUFFER_SIZE:
695 rtspsrc->udp_buffer_size = g_value_get_int (value);
697 case PROP_SHORT_HEADER:
698 rtspsrc->short_header = g_value_get_boolean (value);
701 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
707 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
712 rtspsrc = GST_RTSPSRC (object);
716 g_value_set_string (value, rtspsrc->conninfo.location);
719 g_value_set_flags (value, rtspsrc->protocols);
722 g_value_set_boolean (value, rtspsrc->debug);
725 g_value_set_uint (value, rtspsrc->retry);
728 g_value_set_uint64 (value, rtspsrc->udp_timeout);
730 case PROP_TCP_TIMEOUT:
734 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
735 rtspsrc->tcp_timeout.tv_usec;
736 g_value_set_uint64 (value, timeout);
740 g_value_set_uint (value, rtspsrc->latency);
742 case PROP_CONNECTION_SPEED:
743 g_value_set_uint64 (value, rtspsrc->connection_speed);
745 case PROP_NAT_METHOD:
746 g_value_set_enum (value, rtspsrc->nat_method);
749 g_value_set_boolean (value, rtspsrc->do_rtcp);
755 if (rtspsrc->proxy_host) {
757 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
761 g_value_take_string (value, str);
764 case PROP_RTP_BLOCKSIZE:
765 g_value_set_uint (value, rtspsrc->rtp_blocksize);
768 g_value_set_string (value, rtspsrc->user_id);
771 g_value_set_string (value, rtspsrc->user_pw);
773 case PROP_BUFFER_MODE:
774 g_value_set_enum (value, rtspsrc->buffer_mode);
776 case PROP_PORT_RANGE:
780 if (rtspsrc->client_port_range.min != 0) {
781 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
782 rtspsrc->client_port_range.max);
786 g_value_take_string (value, str);
789 case PROP_UDP_BUFFER_SIZE:
790 g_value_set_int (value, rtspsrc->udp_buffer_size);
792 case PROP_SHORT_HEADER:
793 g_value_set_boolean (value, rtspsrc->short_header);
796 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
802 find_stream_by_id (GstRTSPStream * stream, gint * id)
804 if (stream->id == *id)
811 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
813 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
820 find_stream_by_pt (GstRTSPStream * stream, gint * pt)
822 if (stream->pt == *pt)
829 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
831 GstElement *src = (GstElement *) a;
833 if (stream->udpsrc[0] == src)
835 if (stream->udpsrc[1] == src)
842 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
844 /* check qualified setup_url */
845 if (!strcmp (stream->conninfo.location, (gchar *) a))
847 /* check original control_url */
848 if (!strcmp (stream->control_url, (gchar *) a))
851 /* check if qualified setup_url ends with string */
852 if (g_str_has_suffix (stream->control_url, (gchar *) a))
858 static GstRTSPStream *
859 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
863 /* find and get stream */
864 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
865 return (GstRTSPStream *) lstream->data;
870 static const GstSDPBandwidth *
871 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
872 const GstSDPMedia * media, const gchar * type)
876 /* first look in the media specific section */
877 len = gst_sdp_media_bandwidths_len (media);
878 for (i = 0; i < len; i++) {
879 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
881 if (strcmp (bw->bwtype, type) == 0)
884 /* then look in the message specific section */
885 len = gst_sdp_message_bandwidths_len (sdp);
886 for (i = 0; i < len; i++) {
887 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
889 if (strcmp (bw->bwtype, type) == 0)
896 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
897 const GstSDPMedia * media, GstRTSPStream * stream)
899 const GstSDPBandwidth *bw;
901 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
902 stream->as_bandwidth = bw->bandwidth;
904 stream->as_bandwidth = -1;
906 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
907 stream->rr_bandwidth = bw->bandwidth;
909 stream->rr_bandwidth = -1;
911 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
912 stream->rs_bandwidth = bw->bandwidth;
914 stream->rs_bandwidth = -1;
918 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
919 const GstSDPConnection * conn)
921 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
924 if (conn->addrtype == NULL)
928 if (strcmp (conn->addrtype, "IP4") == 0)
929 stream->is_ipv6 = FALSE;
930 else if (strcmp (conn->addrtype, "IP6") == 0)
931 stream->is_ipv6 = TRUE;
936 g_free (stream->destination);
937 stream->destination = g_strdup (conn->address);
939 /* check for multicast */
940 stream->is_multicast =
941 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
943 stream->ttl = conn->ttl;
946 /* Go over the connections for a stream.
947 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
949 * - If we are dealing with a localhost address, we disable multicast
952 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
953 const GstSDPMedia * media, GstRTSPStream * stream)
955 const GstSDPConnection *conn;
958 /* first look in the media specific section */
959 len = gst_sdp_media_connections_len (media);
960 for (i = 0; i < len; i++) {
961 conn = gst_sdp_media_get_connection (media, i);
963 gst_rtspsrc_do_stream_connection (src, stream, conn);
965 /* then look in the message specific section */
966 if ((conn = gst_sdp_message_get_connection (sdp))) {
967 gst_rtspsrc_do_stream_connection (src, stream, conn);
971 static GstRTSPStream *
972 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
974 GstRTSPStream *stream;
975 const gchar *control_url;
976 const gchar *payload;
977 const GstSDPMedia *media;
979 /* get media, should not return NULL */
980 media = gst_sdp_message_get_media (sdp, idx);
984 stream = g_new0 (GstRTSPStream, 1);
985 stream->parent = src;
986 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
988 stream->last_ret = GST_FLOW_NOT_LINKED;
989 stream->added = FALSE;
990 stream->disabled = FALSE;
991 stream->id = src->numstreams++;
993 stream->discont = TRUE;
994 stream->seqbase = -1;
995 stream->timebase = -1;
997 /* collect bandwidth information for this steam. FIXME, configure in the RTP
998 * session manager to scale RTCP. */
999 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1001 /* collect connection info */
1002 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1004 /* we must have a payload. No payload means we cannot create caps */
1005 /* FIXME, handle multiple formats. The problem here is that we just want to
1006 * take the first available format that we can handle but in order to do that
1007 * we need to scan for depayloader plugins. Scanning for payloader plugins is
1008 * also suboptimal because the user maybe just wants to save the raw stream
1009 * and then we don't care. */
1010 if ((payload = gst_sdp_media_get_format (media, 0))) {
1011 stream->pt = atoi (payload);
1013 stream->caps = gst_rtspsrc_media_to_caps (stream->pt, media);
1015 GST_DEBUG ("mapping sdp session level attributes to caps");
1016 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, stream->caps);
1017 GST_DEBUG ("mapping sdp media level attributes to caps");
1018 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, stream->caps);
1020 if (stream->pt >= 96) {
1021 /* If we have a dynamic payload type, see if we have a stream with the
1022 * same payload number. If there is one, they are part of the same
1023 * container and we only need to add one pad. */
1024 if (find_stream (src, &stream->pt, (gpointer) find_stream_by_pt)) {
1025 stream->container = TRUE;
1026 GST_DEBUG ("found another stream with pt %d, marking as container",
1031 /* collect port number */
1032 stream->port = gst_sdp_media_get_port (media);
1034 /* get control url to construct the setup url. The setup url is used to
1035 * configure the transport of the stream and is used to identity the stream in
1036 * the RTP-Info header field returned from PLAY. */
1037 control_url = gst_sdp_media_get_attribute_val (media, "control");
1038 if (control_url == NULL)
1039 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1041 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1042 GST_DEBUG_OBJECT (src, " pt: %d", stream->pt);
1043 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1044 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1045 GST_DEBUG_OBJECT (src, " caps: %" GST_PTR_FORMAT, stream->caps);
1046 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1048 if (control_url != NULL) {
1049 stream->control_url = g_strdup (control_url);
1050 /* Build a fully qualified url using the content_base if any or by prefixing
1051 * the original request.
1052 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1053 * likely build a URL that the server will fail to understand, this is ok,
1054 * we will fail then. */
1055 if (g_str_has_prefix (control_url, "rtsp://"))
1056 stream->conninfo.location = g_strdup (control_url);
1061 if (g_strcmp0 (control_url, "*") == 0)
1065 base = src->control;
1066 else if (src->content_base)
1067 base = src->content_base;
1068 else if (src->conninfo.url_str)
1069 base = src->conninfo.url_str;
1073 /* check if the base ends or control starts with / */
1074 has_slash = g_str_has_prefix (control_url, "/");
1075 has_slash = has_slash || g_str_has_suffix (base, "/");
1077 /* concatenate the two strings, insert / when not present */
1078 stream->conninfo.location =
1079 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1082 GST_DEBUG_OBJECT (src, " setup: %s",
1083 GST_STR_NULL (stream->conninfo.location));
1085 /* we keep track of all streams */
1086 src->streams = g_list_append (src->streams, stream);
1094 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1098 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1101 gst_caps_unref (stream->caps);
1103 g_free (stream->destination);
1104 g_free (stream->control_url);
1105 g_free (stream->conninfo.location);
1107 for (i = 0; i < 2; i++) {
1108 if (stream->udpsrc[i]) {
1109 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1110 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1111 gst_object_unref (stream->udpsrc[i]);
1112 stream->udpsrc[i] = NULL;
1114 if (stream->channelpad[i]) {
1115 gst_object_unref (stream->channelpad[i]);
1116 stream->channelpad[i] = NULL;
1118 if (stream->udpsink[i]) {
1119 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1120 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1121 gst_object_unref (stream->udpsink[i]);
1122 stream->udpsink[i] = NULL;
1125 if (stream->fakesrc) {
1126 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1127 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1128 gst_object_unref (stream->fakesrc);
1129 stream->fakesrc = NULL;
1131 if (stream->srcpad) {
1132 gst_pad_set_active (stream->srcpad, FALSE);
1133 if (stream->added) {
1134 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1135 stream->added = FALSE;
1137 stream->srcpad = NULL;
1139 if (stream->rtcppad) {
1140 gst_object_unref (stream->rtcppad);
1141 stream->rtcppad = NULL;
1143 if (stream->session) {
1144 g_object_unref (stream->session);
1145 stream->session = NULL;
1151 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1155 GST_DEBUG_OBJECT (src, "cleanup");
1157 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1158 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1160 gst_rtspsrc_stream_free (src, stream);
1162 g_list_free (src->streams);
1163 src->streams = NULL;
1165 if (src->manager_sig_id) {
1166 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1167 src->manager_sig_id = 0;
1169 gst_element_set_state (src->manager, GST_STATE_NULL);
1170 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1171 src->manager = NULL;
1173 src->numstreams = 0;
1175 gst_structure_free (src->props);
1178 g_free (src->content_base);
1179 src->content_base = NULL;
1181 g_free (src->control);
1182 src->control = NULL;
1185 gst_rtsp_range_free (src->range);
1188 /* don't clear the SDP when it was used in the url */
1189 if (src->sdp && !src->from_sdp) {
1190 gst_sdp_message_free (src->sdp);
1195 #define PARSE_INT(p, del, res) \
1198 p = strstr (p, del); \
1208 #define PARSE_STRING(p, del, res) \
1211 p = strstr (p, del); \
1223 #define SKIP_SPACES(p) \
1224 while (*p && g_ascii_isspace (*p)) \
1229 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1232 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1233 gint * rate, gchar ** params)
1237 p = (gchar *) rtpmap;
1239 PARSE_INT (p, " ", *payload);
1247 PARSE_STRING (p, "/", *name);
1248 if (*name == NULL) {
1249 GST_DEBUG ("no rate, name %s", p);
1250 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1251 * streams seem to omit the rate. */
1258 p = strstr (p, "/");
1276 * Mapping SDP attributes to caps
1278 * prepend 'a-' to IANA registered sdp attributes names
1279 * (ie: not prefixed with 'x-') in order to avoid
1280 * collision with gstreamer standard caps properties names
1283 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1285 if (attributes->len > 0) {
1289 s = gst_caps_get_structure (caps, 0);
1291 for (i = 0; i < attributes->len; i++) {
1292 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1293 gchar *tofree, *key;
1297 /* skip some of the attribute we already handle */
1298 if (!strcmp (key, "fmtp"))
1300 if (!strcmp (key, "rtpmap"))
1302 if (!strcmp (key, "control"))
1304 if (!strcmp (key, "range"))
1307 /* string must be valid UTF8 */
1308 if (!g_utf8_validate (attr->value, -1, NULL))
1311 if (!g_str_has_prefix (key, "x-"))
1312 tofree = key = g_strdup_printf ("a-%s", key);
1316 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1317 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1324 * Mapping of caps to and from SDP fields:
1326 * m=<media> <UDP port> RTP/AVP <payload>
1327 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1328 * a=fmtp:<payload> <param>[=<value>];...
1331 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1334 const gchar *rtpmap;
1338 gchar *params = NULL;
1344 /* get and parse rtpmap */
1345 if ((rtpmap = gst_sdp_media_get_attribute_val (media, "rtpmap"))) {
1346 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
1348 if (payload != pt) {
1349 /* we ignore the rtpmap if the payload type is different. */
1350 g_warning ("rtpmap of wrong payload type, ignoring");
1356 /* if we failed to parse the rtpmap for a dynamic payload type, we have an
1360 /* else we can ignore */
1361 g_warning ("error parsing rtpmap, ignoring");
1364 /* dynamic payloads need rtpmap or we fail */
1368 /* check if we have a rate, if not, we need to look up the rate from the
1369 * default rates based on the payload types. */
1371 const GstRTPPayloadInfo *info;
1373 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
1374 /* dynamic types, use media and encoding_name */
1375 tmp = g_ascii_strdown (media->media, -1);
1376 info = gst_rtp_payload_info_for_name (tmp, name);
1379 /* static types, use payload type */
1380 info = gst_rtp_payload_info_for_pt (pt);
1384 if ((rate = info->clock_rate) == 0)
1387 /* we fail if we cannot find one */
1392 tmp = g_ascii_strdown (media->media, -1);
1393 caps = gst_caps_new_simple ("application/x-unknown",
1394 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
1396 s = gst_caps_get_structure (caps, 0);
1398 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
1400 /* encoding name must be upper case */
1402 tmp = g_ascii_strup (name, -1);
1403 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
1407 /* params must be lower case */
1408 if (params != NULL) {
1409 tmp = g_ascii_strdown (params, -1);
1410 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
1414 /* parse optional fmtp: field */
1415 if ((fmtp = gst_sdp_media_get_attribute_val (media, "fmtp"))) {
1421 /* p is now of the format <payload> <param>[=<value>];... */
1422 PARSE_INT (p, " ", payload);
1423 if (payload != -1 && payload == pt) {
1427 /* <param>[=<value>] are separated with ';' */
1428 pairs = g_strsplit (p, ";", 0);
1429 for (i = 0; pairs[i]; i++) {
1431 const gchar *val, *key;
1433 /* the key may not have a '=', the value can have other '='s */
1434 valpos = strstr (pairs[i], "=");
1436 /* we have a '=' and thus a value, remove the '=' with \0 */
1438 /* value is everything between '=' and ';'. We split the pairs at ;
1439 * boundaries so we can take the remainder of the value. Some servers
1440 * put spaces around the value which we strip off here. Alternatively
1441 * we could strip those spaces in the depayloaders should these spaces
1442 * actually carry any meaning in the future. */
1443 val = g_strstrip (valpos + 1);
1445 /* simple <param>;.. is translated into <param>=1;... */
1448 /* strip the key of spaces, convert key to lowercase but not the value. */
1449 key = g_strstrip (pairs[i]);
1450 if (strlen (key) > 1) {
1451 tmp = g_ascii_strdown (key, -1);
1452 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
1464 g_warning ("rtpmap type not given for dynamic payload %d", pt);
1469 g_warning ("rate unknown for payload type %d", pt);
1475 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1476 gint * rtpport, gint * rtcpport)
1479 GstStateChangeReturn ret;
1480 GstElement *udpsrc0, *udpsrc1;
1481 gint tmp_rtp, tmp_rtcp;
1485 src = stream->parent;
1491 /* Start at next port */
1492 tmp_rtp = src->next_port_num;
1494 if (stream->is_ipv6)
1495 host = "udp://[::0]";
1497 host = "udp://0.0.0.0";
1499 /* try to allocate 2 UDP ports, the RTP port should be an even
1500 * number and the RTCP port should be the next (uneven) port */
1503 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1504 tmp_rtp >= src->client_port_range.max)
1507 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
1508 if (udpsrc0 == NULL)
1509 goto no_udp_protocol;
1510 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1512 if (src->udp_buffer_size != 0)
1513 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
1516 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
1517 if (ret == GST_STATE_CHANGE_FAILURE) {
1519 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
1522 if (++count > src->retry)
1525 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1526 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1527 gst_object_unref (udpsrc0);
1529 GST_DEBUG_OBJECT (src, "retry %d", count);
1532 goto no_udp_protocol;
1535 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
1536 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
1538 /* check if port is even */
1539 if ((tmp_rtp & 0x01) != 0) {
1540 /* port not even, close and allocate another */
1541 if (++count > src->retry)
1544 GST_DEBUG_OBJECT (src, "RTP port not even");
1546 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1547 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1548 gst_object_unref (udpsrc0);
1550 GST_DEBUG_OBJECT (src, "retry %d", count);
1555 /* allocate port+1 for RTCP now */
1556 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
1557 if (udpsrc1 == NULL)
1558 goto no_udp_rtcp_protocol;
1561 tmp_rtcp = tmp_rtp + 1;
1562 if (src->client_port_range.max > 0 && tmp_rtcp >= src->client_port_range.max)
1565 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
1567 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
1568 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
1569 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
1570 if (ret == GST_STATE_CHANGE_FAILURE) {
1571 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
1573 if (++count > src->retry)
1576 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1577 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1578 gst_object_unref (udpsrc0);
1580 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
1581 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1582 gst_object_unref (udpsrc1);
1586 GST_DEBUG_OBJECT (src, "retry %d", count);
1590 /* all fine, do port check */
1591 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
1592 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
1594 /* this should not happen... */
1595 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
1598 /* we keep these elements, we configure all in configure_transport when the
1599 * server told us to really use the UDP ports. */
1600 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
1601 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
1603 /* keep track of next available port number when we have a range
1605 if (src->next_port_num != 0)
1606 src->next_port_num = tmp_rtcp + 1;
1613 GST_DEBUG_OBJECT (src, "could not get UDP source");
1618 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
1622 no_udp_rtcp_protocol:
1624 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
1629 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
1630 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
1636 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1637 gst_object_unref (udpsrc0);
1640 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1641 gst_object_unref (udpsrc1);
1648 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
1655 GstClockTime base_time = GST_CLOCK_TIME_NONE;
1658 event = gst_event_new_flush_start ();
1659 GST_DEBUG_OBJECT (src, "start flush");
1661 state = GST_STATE_PAUSED;
1663 event = gst_event_new_flush_stop (TRUE);
1664 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
1667 state = GST_STATE_PLAYING;
1669 state = GST_STATE_PAUSED;
1670 clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
1672 base_time = gst_clock_get_time (clock);
1673 gst_object_unref (clock);
1676 gst_rtspsrc_push_event (src, event, FALSE);
1677 gst_rtspsrc_loop_send_cmd (src, cmd);
1679 /* set up manager before data-flow resumes */
1680 /* to manage jitterbuffer buffer mode */
1682 gst_element_set_base_time (GST_ELEMENT_CAST (src->manager), base_time);
1683 /* and to have base_time trickle further down,
1684 * e.g. to jitterbuffer for its timeout handling */
1685 if (base_time != -1)
1686 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
1689 /* make running time start start at 0 again */
1690 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1691 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1693 for (i = 0; i < 2; i++) {
1695 if (stream->udpsrc[i]) {
1696 if (base_time != -1)
1697 gst_element_set_base_time (stream->udpsrc[i], base_time);
1698 gst_element_set_state (stream->udpsrc[i], state);
1702 /* for tcp interleaved case */
1703 if (base_time != -1)
1704 gst_element_set_base_time (GST_ELEMENT_CAST (src), base_time);
1707 static GstRTSPResult
1708 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
1709 GstRTSPMessage * message, GTimeVal * timeout)
1714 ret = gst_rtsp_connection_send (conn, message, timeout);
1716 ret = GST_RTSP_ERROR;
1721 static GstRTSPResult
1722 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
1723 GstRTSPMessage * message, GTimeVal * timeout)
1728 ret = gst_rtsp_connection_receive (conn, message, timeout);
1730 ret = GST_RTSP_ERROR;
1736 gst_rtspsrc_get_position (GstRTSPSrc * src)
1741 query = gst_query_new_position (GST_FORMAT_TIME);
1742 /* should be known somewhere down the stream (e.g. jitterbuffer) */
1743 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1744 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1748 if (stream->srcpad) {
1749 if (gst_pad_query (stream->srcpad, query)) {
1750 gst_query_parse_position (query, &fmt, &pos);
1751 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
1752 GST_TIME_ARGS (pos));
1753 src->last_pos = pos;
1763 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
1765 src->state = GST_RTSP_STATE_SEEKING;
1766 /* PLAY will add the range header now. */
1767 src->need_range = TRUE;
1773 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
1778 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
1780 gboolean flush, skip;
1783 GstSegment seeksegment = { 0, };
1787 GST_DEBUG_OBJECT (src, "doing seek with event");
1789 gst_event_parse_seek (event, &rate, &format, &flags,
1790 &cur_type, &cur, &stop_type, &stop);
1792 /* no negative rates yet */
1796 /* we need TIME format */
1797 if (format != src->segment.format)
1800 GST_DEBUG_OBJECT (src, "doing seek without event");
1802 cur_type = GST_SEEK_TYPE_SET;
1803 stop_type = GST_SEEK_TYPE_SET;
1806 /* get flush flag */
1807 flush = flags & GST_SEEK_FLAG_FLUSH;
1808 skip = flags & GST_SEEK_FLAG_SKIP;
1810 /* now we need to make sure the streaming thread is stopped. We do this by
1811 * either sending a FLUSH_START event downstream which will cause the
1812 * streaming thread to stop with a WRONG_STATE.
1813 * For a non-flushing seek we simply pause the task, which will happen as soon
1814 * as it completes one iteration (and thus might block when the sink is
1815 * blocking in preroll). */
1817 GST_DEBUG_OBJECT (src, "starting flush");
1818 gst_rtspsrc_flush (src, TRUE, FALSE);
1821 gst_task_pause (src->task);
1825 /* we should now be able to grab the streaming thread because we stopped it
1826 * with the above flush/pause code */
1827 GST_RTSP_STREAM_LOCK (src);
1829 GST_DEBUG_OBJECT (src, "stopped streaming");
1831 /* copy segment, we need this because we still need the old
1832 * segment when we close the current segment. */
1833 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1835 /* configure the seek parameters in the seeksegment. We will then have the
1836 * right values in the segment to perform the seek */
1838 GST_DEBUG_OBJECT (src, "configuring seek");
1839 gst_segment_do_seek (&seeksegment, rate, format, flags,
1840 cur_type, cur, stop_type, stop, &update);
1843 /* figure out the last position we need to play. If it's configured (stop !=
1844 * -1), use that, else we play until the total duration of the file */
1845 if ((stop = seeksegment.stop) == -1)
1846 stop = seeksegment.duration;
1848 playing = (src->state == GST_RTSP_STATE_PLAYING);
1850 /* if we were playing, pause first */
1852 /* obtain current position in case seek fails */
1853 gst_rtspsrc_get_position (src);
1854 gst_rtspsrc_pause (src, FALSE, FALSE);
1857 gst_rtspsrc_do_seek (src, &seeksegment);
1859 /* and continue playing */
1861 gst_rtspsrc_play (src, &seeksegment, FALSE);
1863 /* prepare for streaming again */
1865 /* if we started flush, we stop now */
1866 GST_DEBUG_OBJECT (src, "stopping flush");
1867 gst_rtspsrc_flush (src, FALSE, playing);
1870 /* now we did the seek and can activate the new segment values */
1871 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1873 /* if we're doing a segment seek, post a SEGMENT_START message */
1874 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1875 gst_element_post_message (GST_ELEMENT_CAST (src),
1876 gst_message_new_segment_start (GST_OBJECT_CAST (src),
1877 src->segment.format, src->segment.position));
1880 /* now create the newsegment */
1881 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
1882 " to %" G_GINT64_FORMAT, src->segment.position, stop);
1884 /* store the newsegment event so it can be sent from the streaming thread. */
1885 if (src->start_segment)
1886 gst_event_unref (src->start_segment);
1887 src->start_segment = gst_event_new_segment (&src->segment);
1890 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
1891 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1892 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1893 stream->discont = TRUE;
1897 GST_RTSP_STREAM_UNLOCK (src);
1904 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
1909 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
1915 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
1919 gboolean res = TRUE;
1922 src = GST_RTSPSRC_CAST (parent);
1924 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
1925 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
1927 switch (GST_EVENT_TYPE (event)) {
1928 case GST_EVENT_SEEK:
1929 res = gst_rtspsrc_perform_seek (src, event);
1933 case GST_EVENT_NAVIGATION:
1934 case GST_EVENT_LATENCY:
1942 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
1943 res = gst_pad_send_event (target, event);
1944 gst_object_unref (target);
1946 gst_event_unref (event);
1949 gst_event_unref (event);
1955 /* this is the final event function we receive on the internal source pad when
1956 * we deal with TCP connections */
1958 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
1964 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
1966 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
1967 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
1969 switch (GST_EVENT_TYPE (event)) {
1970 case GST_EVENT_SEEK:
1972 case GST_EVENT_NAVIGATION:
1973 case GST_EVENT_LATENCY:
1975 gst_event_unref (event);
1982 /* this is the final query function we receive on the internal source pad when
1983 * we deal with TCP connections */
1985 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
1989 gboolean res = TRUE;
1991 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
1993 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
1994 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
1996 switch (GST_QUERY_TYPE (query)) {
1997 case GST_QUERY_POSITION:
2002 case GST_QUERY_DURATION:
2006 gst_query_parse_duration (query, &format, NULL);
2009 case GST_FORMAT_TIME:
2010 gst_query_set_duration (query, format, src->segment.duration);
2018 case GST_QUERY_LATENCY:
2020 /* we are live with a min latency of 0 and unlimited max latency, this
2021 * result will be updated by the session manager if there is any. */
2022 gst_query_set_latency (query, TRUE, 0, -1);
2032 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2034 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2038 gboolean res = FALSE;
2040 src = GST_RTSPSRC_CAST (parent);
2042 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2043 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2045 switch (GST_QUERY_TYPE (query)) {
2046 case GST_QUERY_DURATION:
2050 gst_query_parse_duration (query, &format, NULL);
2053 case GST_FORMAT_TIME:
2054 gst_query_set_duration (query, format, src->segment.duration);
2062 case GST_QUERY_SEEKING:
2066 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2067 if (format == GST_FORMAT_TIME) {
2069 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2071 /* seeking without duration is unlikely */
2072 seekable = seekable && src->seekable && src->segment.duration &&
2073 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2075 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2076 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2077 src->segment.start, src->segment.stop);
2084 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2086 /* forward the query to the proxy target pad */
2088 res = gst_pad_query (target, query);
2089 gst_object_unref (target);
2098 /* callback for RTCP messages to be sent to the server when operating in TCP
2100 static GstFlowReturn
2101 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2104 GstRTSPStream *stream;
2105 GstFlowReturn res = GST_FLOW_OK;
2110 GstRTSPMessage message = { 0 };
2111 GstRTSPConnection *conn;
2113 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2114 src = stream->parent;
2116 data = gst_buffer_map (buffer, &bsize, NULL, GST_MAP_READ);
2119 gst_rtsp_message_init_data (&message, stream->channel[1]);
2121 /* lend the body data to the message */
2122 gst_rtsp_message_take_body (&message, data, size);
2124 if (stream->conninfo.connection)
2125 conn = stream->conninfo.connection;
2127 conn = src->conninfo.connection;
2129 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2130 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2131 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2133 /* and steal it away again because we will free it when unreffing the
2135 gst_rtsp_message_steal_body (&message, &data, &size);
2136 gst_rtsp_message_unset (&message);
2138 gst_buffer_unmap (buffer, data, size);
2139 gst_buffer_unref (buffer);
2144 static GstPadProbeReturn
2145 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2147 GstRTSPSrc *src = user_data;
2149 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2150 GST_DEBUG_PAD_NAME (pad));
2152 /* activate the streams */
2153 GST_OBJECT_LOCK (src);
2154 if (!src->need_activate)
2157 src->need_activate = FALSE;
2158 GST_OBJECT_UNLOCK (src);
2160 gst_rtspsrc_activate_streams (src);
2162 return GST_PAD_PROBE_OK;
2166 GST_OBJECT_UNLOCK (src);
2167 return GST_PAD_PROBE_OK;
2171 /* this callback is called when the session manager generated a new src pad with
2172 * payloaded RTP packets. We simply ghost the pad here. */
2174 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2177 GstPadTemplate *template;
2180 GstRTSPStream *stream;
2183 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2185 GST_RTSP_STATE_LOCK (src);
2187 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2188 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2189 goto unknown_stream;
2191 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %d, PT %d", id, ssrc, pt);
2193 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2195 goto unknown_stream;
2197 /* create a new pad we will use to stream to */
2198 template = gst_static_pad_template_get (&rtptemplate);
2199 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2200 gst_object_unref (template);
2203 stream->added = TRUE;
2204 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2205 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2206 gst_pad_set_active (stream->srcpad, TRUE);
2207 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2209 /* check if we added all streams */
2211 for (lstream = src->streams; lstream; lstream = g_list_next (lstream)) {
2212 stream = (GstRTSPStream *) lstream->data;
2214 GST_DEBUG_OBJECT (src, "stream %p, container %d, disabled %d, added %d",
2215 stream, stream->container, stream->disabled, stream->added);
2217 /* a container stream only needs one pad added. Also disabled streams don't
2219 if (!stream->container && !stream->disabled && !stream->added) {
2224 GST_RTSP_STATE_UNLOCK (src);
2227 GST_DEBUG_OBJECT (src, "We added all streams");
2228 /* when we get here, all stream are added and we can fire the no-more-pads
2230 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2238 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2239 GST_RTSP_STATE_UNLOCK (src);
2246 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2248 GstRTSPStream *stream;
2251 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2253 GST_RTSP_STATE_LOCK (src);
2254 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2256 goto unknown_stream;
2258 caps = stream->caps;
2260 gst_caps_ref (caps);
2261 GST_RTSP_STATE_UNLOCK (src);
2267 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2268 GST_RTSP_STATE_UNLOCK (src);
2274 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2276 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2282 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos (), TRUE);
2288 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2294 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2296 GstRTSPSrc *src = stream->parent;
2298 GST_DEBUG_OBJECT (src, "source in session %u received BYE", stream->id);
2300 gst_rtspsrc_do_stream_eos (src, stream);
2304 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2306 GstRTSPSrc *src = stream->parent;
2308 GST_DEBUG_OBJECT (src, "source in session %u timed out", stream->id);
2310 gst_rtspsrc_do_stream_eos (src, stream);
2314 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2316 GstRTSPStream *stream;
2318 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2320 /* get stream for session */
2321 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2323 gst_rtspsrc_do_stream_eos (src, stream);
2328 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2330 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2334 /* try to get and configure a manager */
2336 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
2337 GstRTSPTransport * transport)
2339 const gchar *manager;
2341 GstStateChangeReturn ret;
2343 /* find a manager */
2344 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
2348 GST_DEBUG_OBJECT (src, "using manager %s", manager);
2350 /* configure the manager */
2351 if (src->manager == NULL) {
2352 GObjectClass *klass;
2355 if (!(src->manager = gst_element_factory_make (manager, NULL))) {
2357 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
2361 goto use_no_manager;
2363 if (!(src->manager = gst_element_factory_make (manager, NULL)))
2364 goto manager_failed;
2367 /* we manage this element */
2368 gst_bin_add (GST_BIN_CAST (src), src->manager);
2370 GST_OBJECT_LOCK (src);
2371 target = GST_STATE_TARGET (src);
2372 GST_OBJECT_UNLOCK (src);
2374 ret = gst_element_set_state (src->manager, target);
2375 if (ret == GST_STATE_CHANGE_FAILURE)
2376 goto start_manager_failure;
2378 g_object_set (src->manager, "latency", src->latency, NULL);
2380 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2381 if (g_object_class_find_property (klass, "buffer-mode")) {
2382 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2383 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2385 gboolean need_slave;
2387 const gchar *encoding;
2389 /* buffer mode pauses are handled by adding offsets to buffer times,
2390 * but some depayloaders may have a hard time syncing output times
2391 * with such input times, e.g. container ones, most notably ASF */
2392 /* TODO alternatives are having an event that indicates these shifts,
2393 * or having rtsp extensions provide suggestion on buffer mode */
2394 need_slave = stream->container;
2395 if (stream->caps && (s = gst_caps_get_structure (stream->caps, 0)) &&
2396 (encoding = gst_structure_get_string (s, "encoding-name")))
2397 need_slave = need_slave || (strcmp (encoding, "X-ASF-PF") == 0);
2398 GST_DEBUG_OBJECT (src, "auto buffering mode, need_slave %d",
2400 /* valid duration implies not likely live pipeline,
2401 * so slaving in jitterbuffer does not make much sense
2402 * (and might mess things up due to bursts) */
2403 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
2404 src->segment.duration && !need_slave) {
2405 GST_DEBUG_OBJECT (src, "selected buffer");
2406 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER,
2409 GST_DEBUG_OBJECT (src, "selected slave");
2410 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2415 /* connect to signals if we did not already do so */
2416 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
2418 src->manager_sig_id =
2419 g_signal_connect (src->manager, "pad-added",
2420 (GCallback) new_manager_pad, src);
2421 src->manager_ptmap_id =
2422 g_signal_connect (src->manager, "request-pt-map",
2423 (GCallback) request_pt_map, src);
2425 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
2429 /* we stream directly to the manager, get some pads. Each RTSP stream goes
2430 * into a separate RTP session. */
2431 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
2432 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
2434 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
2435 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
2438 /* now configure the bandwidth in the manager */
2439 if (g_signal_lookup ("get-internal-session",
2440 G_OBJECT_TYPE (src->manager)) != 0) {
2441 GObject *rtpsession;
2443 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
2446 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
2448 stream->session = rtpsession;
2450 if (stream->as_bandwidth != -1) {
2451 GST_INFO_OBJECT (src, "setting AS: %f",
2452 (gdouble) (stream->as_bandwidth * 1000));
2453 g_object_set (rtpsession, "bandwidth",
2454 (gdouble) (stream->as_bandwidth * 1000), NULL);
2456 if (stream->rr_bandwidth != -1) {
2457 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
2458 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
2461 if (stream->rs_bandwidth != -1) {
2462 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
2463 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
2466 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2468 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
2470 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
2472 g_signal_connect (rtpsession, "on-ssrc-active",
2473 (GCallback) on_ssrc_active, stream);
2484 GST_DEBUG_OBJECT (src, "cannot get a session manager");
2489 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
2492 start_manager_failure:
2494 GST_DEBUG_OBJECT (src, "could not start session manager");
2499 /* free the UDP sources allocated when negotiating a transport.
2500 * This function is called when the server negotiated to a transport where the
2501 * UDP sources are not needed anymore, such as TCP or multicast. */
2503 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
2507 for (i = 0; i < 2; i++) {
2508 if (stream->udpsrc[i]) {
2509 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
2510 gst_object_unref (stream->udpsrc[i]);
2511 stream->udpsrc[i] = NULL;
2516 /* for TCP, create pads to send and receive data to and from the manager and to
2517 * intercept various events and queries
2520 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
2521 GstRTSPTransport * transport, GstPad ** outpad)
2524 GstPadTemplate *template;
2525 GstPad *pad0, *pad1;
2527 /* configure for interleaved delivery, nothing needs to be done
2528 * here, the loop function will call the chain functions of the
2529 * session manager. */
2530 stream->channel[0] = transport->interleaved.min;
2531 stream->channel[1] = transport->interleaved.max;
2532 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
2533 stream->channel[0], stream->channel[1]);
2535 /* we can remove the allocated UDP ports now */
2536 gst_rtspsrc_stream_free_udp (stream);
2538 /* no session manager, send data to srcpad directly */
2539 if (!stream->channelpad[0]) {
2540 GST_DEBUG_OBJECT (src, "no manager, creating pad");
2542 /* create a new pad we will use to stream to */
2543 name = g_strdup_printf ("stream_%u", stream->id);
2544 template = gst_static_pad_template_get (&rtptemplate);
2545 stream->channelpad[0] = gst_pad_new_from_template (template, name);
2546 gst_object_unref (template);
2549 /* set caps and activate */
2550 gst_pad_use_fixed_caps (stream->channelpad[0]);
2551 gst_pad_set_active (stream->channelpad[0], TRUE);
2553 *outpad = gst_object_ref (stream->channelpad[0]);
2555 GST_DEBUG_OBJECT (src, "using manager source pad");
2557 template = gst_static_pad_template_get (&anysrctemplate);
2559 /* allocate pads for sending the channel data into the manager */
2560 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
2561 gst_pad_link (pad0, stream->channelpad[0]);
2562 gst_object_unref (stream->channelpad[0]);
2563 stream->channelpad[0] = pad0;
2564 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
2565 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
2566 gst_pad_set_element_private (pad0, src);
2567 gst_pad_set_active (pad0, TRUE);
2569 if (stream->channelpad[1]) {
2570 /* if we have a sinkpad for the other channel, create a pad and link to the
2572 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
2573 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
2574 gst_pad_link (pad1, stream->channelpad[1]);
2575 gst_object_unref (stream->channelpad[1]);
2576 stream->channelpad[1] = pad1;
2577 gst_pad_set_active (pad1, TRUE);
2579 gst_object_unref (template);
2581 /* setup RTCP transport back to the server if we have to. */
2582 if (src->manager && src->do_rtcp) {
2585 template = gst_static_pad_template_get (&anysinktemplate);
2587 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
2588 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
2589 gst_pad_set_element_private (stream->rtcppad, stream);
2590 gst_pad_set_active (stream->rtcppad, TRUE);
2592 /* get session RTCP pad */
2593 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
2594 pad = gst_element_get_request_pad (src->manager, name);
2599 gst_pad_link (pad, stream->rtcppad);
2600 gst_object_unref (pad);
2603 gst_object_unref (template);
2609 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
2610 GstRTSPTransport * transport, const gchar ** destination, gint * min,
2611 gint * max, guint * ttl)
2613 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2615 if (!(*destination = transport->destination))
2616 *destination = stream->destination;
2619 /* transport first */
2620 *min = transport->port.min;
2621 *max = transport->port.max;
2622 if (*min == -1 && *max == -1) {
2623 /* then try from SDP */
2624 if (stream->port != 0) {
2625 *min = stream->port;
2626 *max = stream->port + 1;
2632 if (!(*ttl = transport->ttl))
2637 /* first take the source, then the endpoint to figure out where to send
2639 if (!(*destination = transport->source)) {
2640 if (src->conninfo.connection)
2641 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
2642 else if (stream->conninfo.connection)
2644 gst_rtsp_connection_get_ip (stream->conninfo.connection);
2648 /* for unicast we only expect the ports here */
2649 *min = transport->server_port.min;
2650 *max = transport->server_port.max;
2655 /* For multicast create UDP sources and join the multicast group. */
2657 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
2658 GstRTSPTransport * transport, GstPad ** outpad)
2661 const gchar *destination;
2664 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
2666 /* we can remove the allocated UDP ports now */
2667 gst_rtspsrc_stream_free_udp (stream);
2669 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
2672 /* we need a destination now */
2673 if (destination == NULL)
2674 goto no_destination;
2676 /* we really need ports now or we won't be able to receive anything at all */
2677 if (min == -1 && max == -1)
2680 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
2681 destination, min, max);
2683 /* creating UDP source for RTP */
2685 uri = g_strdup_printf ("udp://%s:%d", destination, min);
2686 stream->udpsrc[0] = gst_element_make_from_uri (GST_URI_SRC, uri, NULL);
2688 if (stream->udpsrc[0] == NULL)
2691 /* take ownership */
2692 gst_object_ref_sink (stream->udpsrc[0]);
2695 gst_element_set_state (stream->udpsrc[0], GST_STATE_PAUSED);
2698 /* creating another UDP source for RTCP */
2700 uri = g_strdup_printf ("udp://%s:%d", destination, max);
2701 stream->udpsrc[1] = gst_element_make_from_uri (GST_URI_SRC, uri, NULL);
2703 if (stream->udpsrc[1] == NULL)
2706 /* take ownership */
2707 gst_object_ref_sink (stream->udpsrc[1]);
2709 gst_element_set_state (stream->udpsrc[1], GST_STATE_PAUSED);
2716 GST_DEBUG_OBJECT (src, "no UDP source element found");
2721 GST_DEBUG_OBJECT (src, "no destination found");
2726 GST_DEBUG_OBJECT (src, "no ports found");
2731 /* configure the remainder of the UDP ports */
2733 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
2734 GstRTSPTransport * transport, GstPad ** outpad)
2736 /* we manage the UDP elements now. For unicast, the UDP sources where
2737 * allocated in the stream when we suggested a transport. */
2738 if (stream->udpsrc[0]) {
2739 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
2741 GST_DEBUG_OBJECT (src, "setting up UDP source");
2743 /* configure a timeout on the UDP port. When the timeout message is
2744 * posted, we assume UDP transport is not possible. We reconnect using TCP
2746 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", src->udp_timeout,
2749 /* get output pad of the UDP source. */
2750 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
2752 /* save it so we can unblock */
2753 stream->blockedpad = *outpad;
2755 /* configure pad block on the pad. As soon as there is dataflow on the
2756 * UDP source, we know that UDP is not blocked by a firewall and we can
2757 * configure all the streams to let the application autoplug decoders. */
2759 gst_pad_add_probe (stream->blockedpad,
2760 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, pad_blocked, src, NULL);
2762 if (stream->channelpad[0]) {
2763 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
2764 /* configure for UDP delivery, we need to connect the UDP pads to
2765 * the session plugin. */
2766 gst_pad_link (*outpad, stream->channelpad[0]);
2767 gst_object_unref (*outpad);
2769 /* we connected to pad-added signal to get pads from the manager */
2771 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
2776 if (stream->udpsrc[1]) {
2777 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
2779 if (stream->channelpad[1]) {
2782 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
2784 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
2785 gst_pad_link (pad, stream->channelpad[1]);
2786 gst_object_unref (pad);
2788 /* leave unlinked */
2794 /* configure the UDP sink back to the server for status reports */
2796 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
2797 GstRTSPStream * stream, GstRTSPTransport * transport)
2800 gint rtp_port, rtcp_port;
2801 gboolean do_rtp, do_rtcp;
2802 const gchar *destination;
2807 /* get transport info */
2808 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
2809 &rtp_port, &rtcp_port, &ttl);
2811 /* see what we need to do */
2812 do_rtp = (rtp_port != -1);
2813 /* it's possible that the server does not want us to send RTCP in which case
2815 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
2817 /* we need a destination when we have RTP or RTCP ports */
2818 if (destination == NULL && (do_rtp || do_rtcp))
2819 goto no_destination;
2821 /* try to construct the fakesrc to the RTP port of the server to open up any
2824 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
2827 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
2828 stream->udpsink[0] = gst_element_make_from_uri (GST_URI_SINK, uri, NULL);
2830 if (stream->udpsink[0] == NULL)
2831 goto no_sink_element;
2833 /* don't join multicast group, we will have the source socket do that */
2834 /* no sync or async state changes needed */
2835 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
2836 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
2838 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
2840 if (stream->udpsrc[0]) {
2841 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
2842 * so that NAT firewalls will open a hole for us */
2843 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
2844 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
2845 /* configure socket and make sure udpsink does not close it when shutting
2846 * down, it belongs to udpsrc after all. */
2847 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
2848 "close-socket", FALSE, NULL);
2849 g_object_unref (socket);
2852 /* the source for the dummy packets to open up NAT */
2853 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
2854 if (stream->fakesrc == NULL)
2855 goto no_fakesrc_element;
2857 /* random data in 5 buffers, a size of 200 bytes should be fine */
2858 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
2859 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
2861 /* we don't want to consider this a sink */
2862 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
2864 /* keep everything locked */
2865 gst_element_set_locked_state (stream->udpsink[0], TRUE);
2866 gst_element_set_locked_state (stream->fakesrc, TRUE);
2868 gst_object_ref (stream->udpsink[0]);
2869 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
2870 gst_object_ref (stream->fakesrc);
2871 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
2873 gst_element_link (stream->fakesrc, stream->udpsink[0]);
2876 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
2879 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
2880 stream->udpsink[1] = gst_element_make_from_uri (GST_URI_SINK, uri, NULL);
2882 if (stream->udpsink[1] == NULL)
2883 goto no_sink_element;
2885 /* don't join multicast group, we will have the source socket do that */
2886 /* no sync or async state changes needed */
2887 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
2888 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
2890 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
2892 if (stream->udpsrc[1]) {
2893 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
2894 * because some servers check the port number of where it sends RTCP to identify
2895 * the RTCP packets it receives */
2896 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
2897 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
2898 /* configure socket and make sure udpsink does not close it when shutting
2899 * down, it belongs to udpsrc after all. */
2900 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
2901 "close-socket", FALSE, NULL);
2902 g_object_unref (socket);
2905 /* we don't want to consider this a sink */
2906 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
2908 /* we keep this playing always */
2909 gst_element_set_locked_state (stream->udpsink[1], TRUE);
2910 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
2912 gst_object_ref (stream->udpsink[1]);
2913 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
2915 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
2917 /* get session RTCP pad */
2918 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
2919 pad = gst_element_get_request_pad (src->manager, name);
2924 gst_pad_link (pad, stream->rtcppad);
2925 gst_object_unref (pad);
2934 GST_DEBUG_OBJECT (src, "no destination address specified");
2939 GST_DEBUG_OBJECT (src, "no UDP sink element found");
2944 GST_DEBUG_OBJECT (src, "no fakesrc element found");
2949 /* sets up all elements needed for streaming over the specified transport.
2950 * Does not yet expose the element pads, this will be done when there is actuall
2951 * dataflow detected, which might never happen when UDP is blocked in a
2952 * firewall, for example.
2955 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
2956 GstRTSPTransport * transport)
2959 GstPad *outpad = NULL;
2960 GstPadTemplate *template;
2965 src = stream->parent;
2967 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
2969 s = gst_caps_get_structure (stream->caps, 0);
2971 /* get the proper mime type for this stream now */
2972 if (gst_rtsp_transport_get_mime (transport->trans, &mime) < 0)
2973 goto unknown_transport;
2975 goto unknown_transport;
2977 /* configure the final mime type */
2978 GST_DEBUG_OBJECT (src, "setting mime to %s", mime);
2979 gst_structure_set_name (s, mime);
2981 /* try to get and configure a manager, channelpad[0-1] will be configured with
2982 * the pads for the manager, or NULL when no manager is needed. */
2983 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
2986 switch (transport->lower_transport) {
2987 case GST_RTSP_LOWER_TRANS_TCP:
2988 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
2989 goto transport_failed;
2991 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
2992 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
2993 goto transport_failed;
2994 /* fallthrough, the rest is the same for UDP and MCAST */
2995 case GST_RTSP_LOWER_TRANS_UDP:
2996 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
2997 goto transport_failed;
2998 /* configure udpsinks back to the server for RTCP messages and for the
2999 * dummy RTP messages to open NAT. */
3000 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3001 goto transport_failed;
3004 goto unknown_transport;
3008 GST_DEBUG_OBJECT (src, "creating ghostpad");
3010 gst_pad_use_fixed_caps (outpad);
3012 /* create ghostpad, don't add just yet, this will be done when we activate
3014 name = g_strdup_printf ("stream_%u", stream->id);
3015 template = gst_static_pad_template_get (&rtptemplate);
3016 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3017 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3018 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3019 gst_object_unref (template);
3022 gst_object_unref (outpad);
3024 /* mark pad as ok */
3025 stream->last_ret = GST_FLOW_OK;
3032 GST_DEBUG_OBJECT (src, "failed to configure transport");
3037 GST_DEBUG_OBJECT (src, "unknown transport");
3042 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3047 /* send a couple of dummy random packets on the receiver RTP port to the server,
3048 * this should make a firewall think we initiated the data transfer and
3049 * hopefully allow packets to go from the sender port to our RTP receiver port */
3051 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3055 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3058 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3059 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3061 if (stream->fakesrc && stream->udpsink[0]) {
3062 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3063 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3064 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3065 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3066 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3072 /* Adds the source pads of all configured streams to the element.
3073 * This code is performed when we detected dataflow.
3075 * We detect dataflow from either the _loop function or with pad probes on the
3079 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3083 GST_DEBUG_OBJECT (src, "activating streams");
3085 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3086 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3088 if (stream->udpsrc[0]) {
3089 /* remove timeout, we are streaming now and timeouts will be handled by
3090 * the session manager and jitter buffer */
3091 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3093 if (stream->srcpad) {
3094 /* if we don't have a session manager, set the caps now. If we have a
3095 * session, we will get a notification of the pad and the caps. */
3096 if (!src->manager) {
3097 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3098 gst_pad_set_caps (stream->srcpad, stream->caps);
3101 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3102 gst_pad_set_active (stream->srcpad, TRUE);
3104 if (!stream->added) {
3105 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3106 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3107 stream->added = TRUE;
3112 /* unblock all pads */
3113 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3114 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3116 if (stream->blockid) {
3117 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3118 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3119 stream->blockid = 0;
3127 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment)
3130 guint64 start, stop;
3131 gdouble play_speed, play_scale;
3133 GST_DEBUG_OBJECT (src, "configuring stream caps");
3135 start = segment->position;
3136 stop = segment->duration;
3137 play_speed = segment->rate;
3138 play_scale = segment->applied_rate;
3140 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3141 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3144 if ((caps = stream->caps)) {
3145 caps = gst_caps_make_writable (caps);
3147 if (stream->timebase != -1)
3148 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3149 (guint) stream->timebase, NULL);
3150 if (stream->seqbase != -1)
3151 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3152 (guint) stream->seqbase, NULL);
3153 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3155 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3156 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3157 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3159 stream->caps = caps;
3161 GST_DEBUG_OBJECT (src, "stream %p, caps %" GST_PTR_FORMAT, stream, caps);
3164 GST_DEBUG_OBJECT (src, "clear session");
3165 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3169 static GstFlowReturn
3170 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3175 /* store the value */
3176 stream->last_ret = ret;
3178 /* if it's success we can return the value right away */
3179 if (ret == GST_FLOW_OK)
3182 /* any other error that is not-linked can be returned right
3184 if (ret != GST_FLOW_NOT_LINKED)
3187 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3188 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3189 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3191 ret = ostream->last_ret;
3192 /* some other return value (must be SUCCESS but we can return
3193 * other values as well) */
3194 if (ret != GST_FLOW_NOT_LINKED)
3197 /* if we get here, all other pads were unlinked and we return
3198 * NOT_LINKED then */
3204 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
3205 GstEvent * event, gboolean source)
3207 gboolean res = TRUE;
3209 /* only streams that have a connection to the outside world */
3210 if (stream->srcpad == NULL)
3213 if (source && stream->udpsrc[0]) {
3214 gst_event_ref (event);
3215 res = gst_element_send_event (stream->udpsrc[0], event);
3216 } else if (stream->channelpad[0]) {
3217 gst_event_ref (event);
3218 if (GST_PAD_IS_SRC (stream->channelpad[0]))
3219 res = gst_pad_push_event (stream->channelpad[0], event);
3221 res = gst_pad_send_event (stream->channelpad[0], event);
3224 if (source && stream->udpsrc[1]) {
3225 gst_event_ref (event);
3226 res &= gst_element_send_event (stream->udpsrc[1], event);
3227 } else if (stream->channelpad[1]) {
3228 gst_event_ref (event);
3229 if (GST_PAD_IS_SRC (stream->channelpad[1]))
3230 res &= gst_pad_push_event (stream->channelpad[1], event);
3232 res &= gst_pad_send_event (stream->channelpad[1], event);
3236 gst_event_unref (event);
3242 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event, gboolean source)
3245 gboolean res = TRUE;
3247 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3248 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3250 gst_event_ref (event);
3251 res &= gst_rtspsrc_stream_push_event (src, ostream, event, source);
3253 gst_event_unref (event);
3258 static GstRTSPResult
3259 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3264 if (info->connection == NULL) {
3265 if (info->url == NULL) {
3266 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
3267 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
3271 /* create connection */
3272 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
3273 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
3274 goto could_not_create;
3277 g_free (info->url_str);
3278 info->url_str = gst_rtsp_url_get_request_uri (info->url);
3280 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
3282 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
3283 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
3285 if (src->proxy_host) {
3286 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
3288 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
3293 if (!info->connected) {
3296 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
3297 ("Connecting to %s", info->location));
3298 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
3300 gst_rtsp_connection_connect (info->connection,
3301 src->ptcp_timeout)) < 0)
3302 goto could_not_connect;
3304 info->connected = TRUE;
3311 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
3316 gchar *str = gst_rtsp_strresult (res);
3317 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
3323 gchar *str = gst_rtsp_strresult (res);
3324 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
3330 static GstRTSPResult
3331 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
3334 if (info->connected) {
3335 GST_DEBUG_OBJECT (src, "closing connection...");
3336 gst_rtsp_connection_close (info->connection);
3337 info->connected = FALSE;
3339 if (free && info->connection) {
3340 /* free connection */
3341 GST_DEBUG_OBJECT (src, "freeing connection...");
3342 gst_rtsp_connection_free (info->connection);
3343 info->connection = NULL;
3348 static GstRTSPResult
3349 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3354 GST_DEBUG_OBJECT (src, "reconnecting connection...");
3355 gst_rtsp_conninfo_close (src, info, FALSE);
3356 res = gst_rtsp_conninfo_connect (src, info, async);
3362 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
3366 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
3367 if (src->conninfo.connection) {
3368 GST_DEBUG_OBJECT (src, "connection flush");
3369 gst_rtsp_connection_flush (src->conninfo.connection, flush);
3371 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3372 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3373 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
3374 if (stream->conninfo.connection)
3375 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
3379 /* FIXME, handle server request, reply with OK, for now */
3380 static GstRTSPResult
3381 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
3382 GstRTSPMessage * request)
3384 GstRTSPMessage response = { 0 };
3387 GST_DEBUG_OBJECT (src, "got server request message");
3390 gst_rtsp_message_dump (request);
3392 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
3394 if (res == GST_RTSP_ENOTIMPL) {
3395 /* default implementation, send OK */
3397 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
3402 GST_DEBUG_OBJECT (src, "replying with OK");
3405 gst_rtsp_message_dump (&response);
3407 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
3411 gst_rtsp_message_unset (&response);
3412 } else if (res == GST_RTSP_EEOF)
3420 gst_rtsp_message_unset (&response);
3425 /* send server keep-alive */
3426 static GstRTSPResult
3427 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
3429 GstRTSPMessage request = { 0 };
3431 GstRTSPMethod method;
3434 GST_DEBUG_OBJECT (src, "creating server keep-alive");
3436 /* find a method to use for keep-alive */
3437 if (src->methods & GST_RTSP_GET_PARAMETER)
3438 method = GST_RTSP_GET_PARAMETER;
3440 method = GST_RTSP_OPTIONS;
3443 control = src->control;
3445 control = src->conninfo.url_str;
3447 if (control == NULL)
3450 res = gst_rtsp_message_init_request (&request, method, control);
3455 gst_rtsp_message_dump (&request);
3458 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
3463 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3464 gst_rtsp_message_unset (&request);
3471 GST_WARNING_OBJECT (src, "no control url to send keepalive");
3476 gchar *str = gst_rtsp_strresult (res);
3478 gst_rtsp_message_unset (&request);
3479 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
3480 ("Could not send keep-alive. (%s)", str));
3486 static GstFlowReturn
3487 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
3489 GstRTSPMessage message = { 0 };
3492 GstRTSPStream *stream;
3493 GstPad *outpad = NULL;
3496 GstFlowReturn ret = GST_FLOW_OK;
3498 gboolean is_rtcp, have_data;
3500 /* here we are only interested in data messages */
3503 GTimeVal tv_timeout;
3505 /* get the next timeout interval */
3506 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3508 /* see if the timeout period expired */
3509 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
3510 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
3511 /* send keep-alive, only act on interrupt, a warning will be posted for
3513 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3515 /* get new timeout */
3516 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3519 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
3520 tv_timeout.tv_sec, tv_timeout.tv_usec);
3522 /* protect the connection with the connection lock so that we can see when
3523 * we are finished doing server communication */
3525 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
3526 &message, src->ptcp_timeout);
3530 GST_DEBUG_OBJECT (src, "we received a server message");
3532 case GST_RTSP_EINTR:
3533 /* we got interrupted this means we need to stop */
3535 case GST_RTSP_ETIMEOUT:
3536 /* no reply, send keep alive */
3537 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
3538 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3542 /* go EOS when the server closed the connection */
3548 switch (message.type) {
3549 case GST_RTSP_MESSAGE_REQUEST:
3550 /* server sends us a request message, handle it */
3552 gst_rtspsrc_handle_request (src, src->conninfo.connection,
3554 if (res == GST_RTSP_EEOF)
3557 goto handle_request_failed;
3559 case GST_RTSP_MESSAGE_RESPONSE:
3560 /* we ignore response messages */
3561 GST_DEBUG_OBJECT (src, "ignoring response message");
3563 gst_rtsp_message_dump (&message);
3565 case GST_RTSP_MESSAGE_DATA:
3566 GST_DEBUG_OBJECT (src, "got data message");
3570 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
3577 channel = message.type_data.data.channel;
3579 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
3581 goto unknown_stream;
3583 if (channel == stream->channel[0]) {
3584 outpad = stream->channelpad[0];
3586 } else if (channel == stream->channel[1]) {
3587 outpad = stream->channelpad[1];
3593 /* take a look at the body to figure out what we have */
3594 gst_rtsp_message_get_body (&message, &data, &size);
3596 goto invalid_length;
3598 /* channels are not correct on some servers, do extra check */
3599 if (data[1] >= 200 && data[1] <= 204) {
3600 /* hmm RTCP message switch to the RTCP pad of the same stream. */
3601 outpad = stream->channelpad[1];
3605 /* we have no clue what this is, just ignore then. */
3607 goto unknown_stream;
3609 /* take the message body for further processing */
3610 gst_rtsp_message_steal_body (&message, &data, &size);
3612 /* strip the trailing \0 */
3615 buf = gst_buffer_new ();
3616 gst_buffer_take_memory (buf, -1,
3617 gst_memory_new_wrapped (0, data, g_free, size, 0, size));
3619 /* don't need message anymore */
3620 gst_rtsp_message_unset (&message);
3622 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
3625 if (src->need_activate) {
3626 gst_rtspsrc_activate_streams (src);
3627 src->need_activate = FALSE;
3630 if (src->base_time == -1) {
3631 /* Take current running_time. This timestamp will be put on
3632 * the first buffer of each stream because we are a live source and so we
3633 * timestamp with the running_time. When we are dealing with TCP, we also
3634 * only timestamp the first buffer (using the DISCONT flag) because a server
3635 * typically bursts data, for which we don't want to compensate by speeding
3636 * up the media. The other timestamps will be interpollated from this one
3637 * using the RTP timestamps. */
3638 GST_OBJECT_LOCK (src);
3639 if (GST_ELEMENT_CLOCK (src)) {
3641 GstClockTime base_time;
3643 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
3644 base_time = GST_ELEMENT_CAST (src)->base_time;
3646 src->base_time = now - base_time;
3648 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
3649 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
3651 GST_OBJECT_UNLOCK (src);
3654 if (stream->discont && !is_rtcp) {
3655 /* mark first RTP buffer as discont */
3656 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
3657 stream->discont = FALSE;
3658 /* first buffer gets the timestamp, other buffers are not timestamped and
3659 * their presentation time will be interpollated from the rtp timestamps. */
3660 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
3661 GST_TIME_ARGS (src->base_time));
3663 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
3666 /* chain to the peer pad */
3667 if (GST_PAD_IS_SINK (outpad))
3668 ret = gst_pad_chain (outpad, buf);
3670 ret = gst_pad_push (outpad, buf);
3673 /* combine all stream flows for the data transport */
3674 ret = gst_rtspsrc_combine_flows (src, stream, ret);
3681 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
3682 gst_rtsp_message_unset (&message);
3687 GST_DEBUG_OBJECT (src, "we got an eof from the server");
3688 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3689 ("The server closed the connection."));
3690 src->conninfo.connected = FALSE;
3691 gst_rtsp_message_unset (&message);
3692 return GST_FLOW_EOS;
3696 gst_rtsp_message_unset (&message);
3697 GST_DEBUG_OBJECT (src, "got interrupted: stop connection flush");
3698 gst_rtspsrc_connection_flush (src, FALSE);
3699 return GST_FLOW_WRONG_STATE;
3703 gchar *str = gst_rtsp_strresult (res);
3705 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
3706 ("Could not receive message. (%s)", str));
3709 gst_rtsp_message_unset (&message);
3710 return GST_FLOW_ERROR;
3712 handle_request_failed:
3714 gchar *str = gst_rtsp_strresult (res);
3716 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
3717 ("Could not handle server message. (%s)", str));
3719 gst_rtsp_message_unset (&message);
3720 return GST_FLOW_ERROR;
3724 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3725 ("Short message received, ignoring."));
3726 gst_rtsp_message_unset (&message);
3731 static GstFlowReturn
3732 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
3735 GstRTSPMessage message = { 0 };
3739 GTimeVal tv_timeout;
3741 /* get the next timeout interval */
3742 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3744 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
3745 (gint) tv_timeout.tv_sec);
3747 gst_rtsp_message_unset (&message);
3749 /* we should continue reading the TCP socket because the server might
3750 * send us requests. When the session timeout expires, we need to send a
3751 * keep-alive request to keep the session open. */
3752 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
3753 &message, &tv_timeout);
3757 GST_DEBUG_OBJECT (src, "we received a server message");
3759 case GST_RTSP_EINTR:
3760 /* we got interrupted, see what we have to do */
3762 case GST_RTSP_ETIMEOUT:
3763 /* send keep-alive, ignore the result, a warning will be posted. */
3764 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
3765 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3769 /* server closed the connection. not very fatal for UDP, reconnect and
3770 * see what happens. */
3771 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3772 ("The server closed the connection."));
3774 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
3782 switch (message.type) {
3783 case GST_RTSP_MESSAGE_REQUEST:
3784 /* server sends us a request message, handle it */
3786 gst_rtspsrc_handle_request (src, src->conninfo.connection,
3788 if (res == GST_RTSP_EEOF)
3791 goto handle_request_failed;
3793 case GST_RTSP_MESSAGE_RESPONSE:
3794 /* we ignore response and data messages */
3795 GST_DEBUG_OBJECT (src, "ignoring response message");
3797 gst_rtsp_message_dump (&message);
3798 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
3799 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
3800 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
3801 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
3802 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3809 case GST_RTSP_MESSAGE_DATA:
3810 /* we ignore response and data messages */
3811 GST_DEBUG_OBJECT (src, "ignoring data message");
3814 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
3820 /* we get here when the connection got interrupted */
3823 gst_rtsp_message_unset (&message);
3824 GST_DEBUG_OBJECT (src, "got interrupted: stop connection flush");
3825 gst_rtspsrc_connection_flush (src, FALSE);
3826 return GST_FLOW_WRONG_STATE;
3830 gchar *str = gst_rtsp_strresult (res);
3833 src->conninfo.connected = FALSE;
3834 if (res != GST_RTSP_EINTR) {
3835 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
3836 ("Could not connect to server. (%s)", str));
3838 ret = GST_FLOW_ERROR;
3840 ret = GST_FLOW_WRONG_STATE;
3846 gchar *str = gst_rtsp_strresult (res);
3848 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
3849 ("Could not receive message. (%s)", str));
3851 return GST_FLOW_ERROR;
3853 handle_request_failed:
3855 gchar *str = gst_rtsp_strresult (res);
3858 gst_rtsp_message_unset (&message);
3859 if (res != GST_RTSP_EINTR) {
3860 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
3861 ("Could not handle server message. (%s)", str));
3863 ret = GST_FLOW_ERROR;
3865 ret = GST_FLOW_WRONG_STATE;
3871 GST_DEBUG_OBJECT (src, "we got an eof from the server");
3872 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3873 ("The server closed the connection."));
3874 src->conninfo.connected = FALSE;
3875 gst_rtsp_message_unset (&message);
3876 return GST_FLOW_EOS;
3880 static GstRTSPResult
3881 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
3883 GstRTSPResult res = GST_RTSP_OK;
3886 GST_DEBUG_OBJECT (src, "doing reconnect");
3888 GST_OBJECT_LOCK (src);
3889 /* only restart when the pads were not yet activated, else we were
3890 * streaming over UDP */
3891 restart = src->need_activate;
3892 GST_OBJECT_UNLOCK (src);
3894 /* no need to restart, we're done */
3898 /* we can try only TCP now */
3899 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
3901 /* close and cleanup our state */
3902 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
3905 /* see if we have TCP left to try. Also don't try TCP when we were configured
3907 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
3910 /* We post a warning message now to inform the user
3911 * that nothing happened. It's most likely a firewall thing. */
3912 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3913 ("Could not receive any UDP packets for %.4f seconds, maybe your "
3914 "firewall is blocking it. Retrying using a TCP connection.",
3915 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
3917 /* open new connection using tcp */
3918 if (gst_rtspsrc_open (src, async) < 0)
3921 /* start playback */
3922 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
3931 src->cur_protocols = 0;
3932 /* no transport possible, post an error and stop */
3933 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
3934 ("Could not receive any UDP packets for %.4f seconds, maybe your "
3935 "firewall is blocking it. No other protocols to try.",
3936 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
3937 return GST_FLOW_ERROR;
3941 GST_DEBUG_OBJECT (src, "open failed");
3946 GST_DEBUG_OBJECT (src, "play failed");
3952 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
3956 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
3959 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
3962 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
3965 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
3973 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
3977 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
3980 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
3983 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
3986 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
3994 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
3998 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4001 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4004 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4007 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4015 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4019 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
4022 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4025 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4028 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4036 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4038 if (ret == GST_RTSP_OK)
4039 gst_rtspsrc_loop_complete_cmd (src, cmd);
4040 else if (ret == GST_RTSP_EINTR)
4041 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4043 gst_rtspsrc_loop_error_cmd (src, cmd);
4047 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd)
4051 /* start new request */
4052 gst_rtspsrc_loop_start_cmd (src, cmd);
4054 GST_DEBUG_OBJECT (src, "sending cmd %d", cmd);
4056 GST_OBJECT_LOCK (src);
4057 old = src->loop_cmd;
4058 if (old != CMD_WAIT) {
4059 src->loop_cmd = CMD_WAIT;
4060 GST_OBJECT_UNLOCK (src);
4061 /* cancel previous request */
4062 gst_rtspsrc_loop_cancel_cmd (src, old);
4063 GST_OBJECT_LOCK (src);
4065 src->loop_cmd = cmd;
4066 /* interrupt if allowed */
4068 GST_DEBUG_OBJECT (src, "start connection flush");
4069 gst_rtspsrc_connection_flush (src, TRUE);
4072 gst_task_start (src->task);
4073 GST_OBJECT_UNLOCK (src);
4077 gst_rtspsrc_loop (GstRTSPSrc * src)
4081 if (!src->conninfo.connection || !src->conninfo.connected)
4084 if (src->interleaved)
4085 ret = gst_rtspsrc_loop_interleaved (src);
4087 ret = gst_rtspsrc_loop_udp (src);
4089 if (ret != GST_FLOW_OK)
4097 GST_WARNING_OBJECT (src, "we are not connected");
4098 ret = GST_FLOW_WRONG_STATE;
4103 const gchar *reason = gst_flow_get_name (ret);
4105 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
4106 src->running = FALSE;
4107 if (ret == GST_FLOW_EOS) {
4108 /* perform EOS logic */
4109 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4110 gst_element_post_message (GST_ELEMENT_CAST (src),
4111 gst_message_new_segment_done (GST_OBJECT_CAST (src),
4112 src->segment.format, src->segment.position));
4114 gst_rtspsrc_push_event (src, gst_event_new_eos (), FALSE);
4116 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
4117 /* for fatal errors we post an error message, post the error before the
4118 * EOS so the app knows about the error first. */
4119 GST_ELEMENT_ERROR (src, STREAM, FAILED,
4120 ("Internal data flow error."),
4121 ("streaming task paused, reason %s (%d)", reason, ret));
4122 gst_rtspsrc_push_event (src, gst_event_new_eos (), FALSE);
4128 #ifndef GST_DISABLE_GST_DEBUG
4129 static const gchar *
4130 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
4134 while (method != 0) {
4151 static const gchar *
4152 gst_rtspsrc_skip_lws (const gchar * s)
4154 while (g_ascii_isspace (*s))
4159 static const gchar *
4160 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
4162 while (s > start && g_ascii_isspace (*(s - 1)))
4167 static const gchar *
4168 gst_rtspsrc_skip_commas (const gchar * s)
4170 /* The grammar allows for multiple commas */
4171 while (g_ascii_isspace (*s) || *s == ',')
4176 static const gchar *
4177 gst_rtspsrc_skip_item (const gchar * s)
4179 gboolean quoted = FALSE;
4180 const gchar *start = s;
4182 /* A list item ends at the last non-whitespace character
4183 * before a comma which is not inside a quoted-string. Or at
4184 * the end of the string.
4190 if (*s == '\\' && *(s + 1))
4199 return gst_rtspsrc_unskip_lws (s, start);
4203 gst_rtsp_decode_quoted_string (gchar * quoted_string)
4207 src = quoted_string + 1;
4208 dst = quoted_string;
4209 while (*src && *src != '"') {
4210 if (*src == '\\' && *(src + 1))
4217 /* Extract the authentication tokens that the server provided for each method
4218 * into an array of structures and give those to the connection object.
4221 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
4222 const gchar * header, gboolean * stale)
4224 GSList *list = NULL, *iter;
4226 gchar *item, *eq, *name_end, *value;
4228 g_return_if_fail (stale != NULL);
4230 gst_rtsp_connection_clear_auth_params (conn);
4233 /* Parse a header whose content is described by RFC2616 as
4234 * "#something", where "something" does not itself contain commas,
4235 * except as part of quoted-strings, into a list of allocated strings.
4237 header = gst_rtspsrc_skip_commas (header);
4239 end = gst_rtspsrc_skip_item (header);
4240 list = g_slist_prepend (list, g_strndup (header, end - header));
4241 header = gst_rtspsrc_skip_commas (end);
4246 list = g_slist_reverse (list);
4247 for (iter = list; iter; iter = iter->next) {
4250 eq = strchr (item, '=');
4252 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
4253 if (name_end == item) {
4254 /* That's no good... */
4261 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
4263 gst_rtsp_decode_quoted_string (value);
4267 if ((strcmp (item, "stale") == 0) && (strcmp (value, "TRUE") == 0))
4269 gst_rtsp_connection_set_auth_param (conn, item, value);
4273 g_slist_free (list);
4276 /* Parse a WWW-Authenticate Response header and determine the
4277 * available authentication methods
4279 * This code should also cope with the fact that each WWW-Authenticate
4280 * header can contain multiple challenge methods + tokens
4282 * At the moment, for Basic auth, we just do a minimal check and don't
4283 * even parse out the realm */
4285 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
4286 GstRTSPConnection * conn, gboolean * stale)
4290 g_return_if_fail (hdr != NULL);
4291 g_return_if_fail (methods != NULL);
4292 g_return_if_fail (stale != NULL);
4294 /* Skip whitespace at the start of the string */
4295 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
4297 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
4298 *methods |= GST_RTSP_AUTH_BASIC;
4299 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
4300 *methods |= GST_RTSP_AUTH_DIGEST;
4301 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
4306 * gst_rtspsrc_setup_auth:
4307 * @src: the rtsp source
4309 * Configure a username and password and auth method on the
4310 * connection object based on a response we received from the
4313 * Currently, this requires that a username and password were supplied
4314 * in the uri. In the future, they may be requested on demand by sending
4315 * a message up the bus.
4317 * Returns: TRUE if authentication information could be set up correctly.
4320 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
4324 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
4325 GstRTSPAuthMethod method;
4326 GstRTSPResult auth_result;
4328 GstRTSPConnection *conn;
4330 gboolean stale = FALSE;
4332 conn = src->conninfo.connection;
4334 /* Identify the available auth methods and see if any are supported */
4335 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
4336 &hdr, 0) == GST_RTSP_OK) {
4337 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
4340 if (avail_methods == GST_RTSP_AUTH_NONE)
4341 goto no_auth_available;
4343 /* For digest auth, if the response indicates that the session
4344 * data are stale, we just update them in the connection object and
4345 * return TRUE to retry the request */
4347 src->tried_url_auth = FALSE;
4349 url = gst_rtsp_connection_get_url (conn);
4351 /* Do we have username and password available? */
4352 if (url != NULL && !src->tried_url_auth && url->user != NULL
4353 && url->passwd != NULL) {
4356 src->tried_url_auth = TRUE;
4357 GST_DEBUG_OBJECT (src,
4358 "Attempting authentication using credentials from the URL");
4360 user = src->user_id;
4361 pass = src->user_pw;
4362 GST_DEBUG_OBJECT (src,
4363 "Attempting authentication using credentials from the properties");
4366 /* FIXME: If the url didn't contain username and password or we tried them
4367 * already, request a username and passwd from the application via some kind
4368 * of credentials request message */
4370 /* If we don't have a username and passwd at this point, bail out. */
4371 if (user == NULL || pass == NULL)
4374 /* Try to configure for each available authentication method, strongest to
4376 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
4377 /* Check if this method is available on the server */
4378 if ((method & avail_methods) == 0)
4381 /* Pass the credentials to the connection to try on the next request */
4382 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
4383 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
4384 * ignore it and end up retrying later */
4385 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
4386 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
4387 gst_rtsp_auth_method_to_string (method));
4392 if (method == GST_RTSP_AUTH_NONE)
4393 goto no_auth_available;
4399 /* Output an error indicating that we couldn't connect because there were
4400 * no supported authentication protocols */
4401 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4402 ("No supported authentication protocol was found"));
4407 /* We don't fire an error message, we just return FALSE and let the
4408 * normal NOT_AUTHORIZED error be propagated */
4413 static GstRTSPResult
4414 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4415 GstRTSPMessage * request, GstRTSPMessage * response,
4416 GstRTSPStatusCode * code)
4419 GstRTSPStatusCode thecode;
4420 gchar *content_base = NULL;
4424 if (!src->short_header)
4425 gst_rtsp_ext_list_before_send (src->extensions, request);
4427 GST_DEBUG_OBJECT (src, "sending message");
4430 gst_rtsp_message_dump (request);
4432 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
4436 gst_rtsp_connection_reset_timeout (conn);
4439 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
4444 gst_rtsp_message_dump (response);
4446 switch (response->type) {
4447 case GST_RTSP_MESSAGE_REQUEST:
4448 res = gst_rtspsrc_handle_request (src, conn, response);
4449 if (res == GST_RTSP_EEOF)
4452 goto handle_request_failed;
4454 case GST_RTSP_MESSAGE_RESPONSE:
4455 /* ok, a response is good */
4456 GST_DEBUG_OBJECT (src, "received response message");
4458 case GST_RTSP_MESSAGE_DATA:
4459 /* get next response */
4460 GST_DEBUG_OBJECT (src, "ignoring data response message");
4463 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4468 thecode = response->type_data.response.code;
4470 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
4472 /* if the caller wanted the result code, we store it. */
4476 /* If the request didn't succeed, bail out before doing any more */
4477 if (thecode != GST_RTSP_STS_OK)
4480 /* store new content base if any */
4481 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
4484 g_free (src->content_base);
4485 src->content_base = g_strdup (content_base);
4487 gst_rtsp_ext_list_after_send (src->extensions, request, response);
4494 gchar *str = gst_rtsp_strresult (res);
4496 if (res != GST_RTSP_EINTR) {
4497 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4498 ("Could not send message. (%s)", str));
4500 GST_WARNING_OBJECT (src, "send interrupted");
4509 GST_WARNING_OBJECT (src, "server closed connection, doing reconnect");
4512 /* if reconnect succeeds, try again */
4514 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
4518 /* only try once after reconnect, then fallthrough and error out */
4521 gchar *str = gst_rtsp_strresult (res);
4523 if (res != GST_RTSP_EINTR) {
4524 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4525 ("Could not receive message. (%s)", str));
4527 GST_WARNING_OBJECT (src, "receive interrupted");
4535 handle_request_failed:
4537 /* ERROR was posted */
4538 gst_rtsp_message_unset (response);
4543 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4544 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4545 ("The server closed the connection."));
4546 gst_rtsp_message_unset (response);
4553 * @src: the rtsp source
4554 * @conn: the connection to send on
4555 * @request: must point to a valid request
4556 * @response: must point to an empty #GstRTSPMessage
4557 * @code: an optional code result
4559 * send @request and retrieve the response in @response. optionally @code can be
4560 * non-NULL in which case it will contain the status code of the response.
4562 * If This function returns #GST_RTSP_OK, @response will contain a valid response
4563 * message that should be cleaned with gst_rtsp_message_unset() after usage.
4565 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
4566 * @response message) if the response code was not 200 (OK).
4568 * If the attempt results in an authentication failure, then this will attempt
4569 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
4572 * Returns: #GST_RTSP_OK if the processing was successful.
4574 static GstRTSPResult
4575 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4576 GstRTSPMessage * request, GstRTSPMessage * response,
4577 GstRTSPStatusCode * code)
4579 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
4580 GstRTSPResult res = GST_RTSP_ERROR;
4583 GstRTSPMethod method = GST_RTSP_INVALID;
4589 /* make sure we don't loop forever */
4593 /* save method so we can disable it when the server complains */
4594 method = request->type_data.request.method;
4597 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
4601 case GST_RTSP_STS_UNAUTHORIZED:
4602 if (gst_rtspsrc_setup_auth (src, response)) {
4603 /* Try the request/response again after configuring the auth info
4611 } while (retry == TRUE);
4613 /* If the user requested the code, let them handle errors, otherwise
4614 * post an error below */
4617 else if (int_code != GST_RTSP_STS_OK)
4618 goto error_response;
4625 GST_DEBUG_OBJECT (src, "got error %d", res);
4630 res = GST_RTSP_ERROR;
4632 switch (response->type_data.response.code) {
4633 case GST_RTSP_STS_NOT_FOUND:
4634 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
4635 response->type_data.response.reason));
4637 case GST_RTSP_STS_MOVED_PERMANENTLY:
4638 case GST_RTSP_STS_MOVE_TEMPORARILY:
4640 gchar *new_location;
4641 GstRTSPLowerTrans transports;
4643 GST_DEBUG_OBJECT (src, "got redirection");
4644 /* if we don't have a Location Header, we must error */
4645 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
4646 &new_location, 0) < 0)
4649 /* When we receive a redirect result, we go back to the INIT state after
4650 * parsing the new URI. The caller should do the needed steps to issue
4651 * a new setup when it detects this state change. */
4652 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
4654 /* save current transports */
4655 if (src->conninfo.url)
4656 transports = src->conninfo.url->transports;
4658 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
4660 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
4662 /* set old transports */
4663 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
4664 src->conninfo.url->transports = transports;
4666 src->need_redirect = TRUE;
4667 src->state = GST_RTSP_STATE_INIT;
4671 case GST_RTSP_STS_NOT_ACCEPTABLE:
4672 case GST_RTSP_STS_NOT_IMPLEMENTED:
4673 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
4674 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
4675 gst_rtsp_method_as_text (method));
4676 src->methods &= ~method;
4680 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4681 ("Got error response: %d (%s).", response->type_data.response.code,
4682 response->type_data.response.reason));
4685 /* if we return ERROR we should unset the response ourselves */
4686 if (res == GST_RTSP_ERROR)
4687 gst_rtsp_message_unset (response);
4693 static GstRTSPResult
4694 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
4695 GstRTSPMessage * response, GstRTSPSrc * src)
4697 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
4702 /* parse the response and collect all the supported methods. We need this
4703 * information so that we don't try to send an unsupported request to the
4707 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
4709 GstRTSPHeaderField field;
4715 /* reset supported methods */
4718 /* Try Allow Header first */
4719 field = GST_RTSP_HDR_ALLOW;
4722 gst_rtsp_message_get_header (response, field, &respoptions, indx);
4723 if (indx == 0 && !respoptions) {
4724 /* if no Allow header was found then try the Public header... */
4725 field = GST_RTSP_HDR_PUBLIC;
4726 gst_rtsp_message_get_header (response, field, &respoptions, indx);
4731 /* If we get here, the server gave a list of supported methods, parse
4732 * them here. The string is like:
4734 * OPTIONS, DESCRIBE, ANNOUNCE, PLAY, SETUP, ...
4736 options = g_strsplit (respoptions, ",", 0);
4738 for (i = 0; options[i]; i++) {
4742 stripped = g_strstrip (options[i]);
4743 method = gst_rtsp_find_method (stripped);
4745 /* keep bitfield of supported methods */
4746 if (method != GST_RTSP_INVALID)
4747 src->methods |= method;
4749 g_strfreev (options);
4754 if (src->methods == 0) {
4755 /* neither Allow nor Public are required, assume the server supports
4756 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
4758 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
4759 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
4761 /* always assume PLAY, FIXME, extensions should be able to override
4763 src->methods |= GST_RTSP_PLAY;
4764 /* also assume it will support Range */
4765 src->seekable = TRUE;
4767 /* we need describe and setup */
4768 if (!(src->methods & GST_RTSP_DESCRIBE))
4770 if (!(src->methods & GST_RTSP_SETUP))
4778 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4779 ("Server does not support DESCRIBE."));
4784 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4785 ("Server does not support SETUP."));
4790 /* masks to be kept in sync with the hardcoded protocol order of preference
4792 static guint protocol_masks[] = {
4793 GST_RTSP_LOWER_TRANS_UDP,
4794 GST_RTSP_LOWER_TRANS_UDP_MCAST,
4795 GST_RTSP_LOWER_TRANS_TCP,
4799 static GstRTSPResult
4800 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
4801 GstRTSPLowerTrans protocols, gchar ** transports)
4805 gboolean add_udp_str;
4810 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
4815 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
4817 /* extension listed transports, use those */
4818 if (*transports != NULL)
4821 /* it's the default */
4822 add_udp_str = FALSE;
4824 /* the default RTSP transports */
4825 result = g_string_new ("");
4826 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
4827 GST_DEBUG_OBJECT (src, "adding UDP unicast");
4829 g_string_append (result, "RTP/AVP");
4831 g_string_append (result, "/UDP");
4832 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
4833 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
4834 GST_DEBUG_OBJECT (src, "adding UDP multicast");
4836 /* we don't have to allocate any UDP ports yet, if the selected transport
4837 * turns out to be multicast we can create them and join the multicast
4838 * group indicated in the transport reply */
4839 if (result->len > 0)
4840 g_string_append (result, ",");
4841 g_string_append (result, "RTP/AVP");
4843 g_string_append (result, "/UDP");
4844 g_string_append (result, ";multicast");
4845 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
4846 GST_DEBUG_OBJECT (src, "adding TCP");
4848 if (result->len > 0)
4849 g_string_append (result, ",");
4850 g_string_append (result, "RTP/AVP/TCP;unicast;interleaved=%%i1-%%i2");
4852 *transports = g_string_free (result, FALSE);
4854 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
4865 static GstRTSPResult
4866 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
4867 gint orig_rtpport, gint orig_rtcpport)
4870 gint nr_udp, nr_int;
4872 gint rtpport = 0, rtcpport = 0;
4875 src = stream->parent;
4877 /* find number of placeholders first */
4878 if (strstr (*transports, "%%i2"))
4880 else if (strstr (*transports, "%%i1"))
4885 if (strstr (*transports, "%%u2"))
4887 else if (strstr (*transports, "%%u1"))
4892 if (nr_udp == 0 && nr_int == 0)
4896 if (!orig_rtpport || !orig_rtcpport) {
4897 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
4900 rtpport = orig_rtpport;
4901 rtcpport = orig_rtcpport;
4905 str = g_string_new ("");
4907 while ((next = strstr (p, "%%"))) {
4908 g_string_append_len (str, p, next - p);
4909 if (next[2] == 'u') {
4911 g_string_append_printf (str, "%d", rtpport);
4912 else if (next[3] == '2')
4913 g_string_append_printf (str, "%d", rtcpport);
4915 if (next[2] == 'i') {
4917 g_string_append_printf (str, "%d", src->free_channel);
4918 else if (next[3] == '2')
4919 g_string_append_printf (str, "%d", src->free_channel + 1);
4924 /* append final part */
4925 g_string_append (str, p);
4927 g_free (*transports);
4928 *transports = g_string_free (str, FALSE);
4936 return GST_RTSP_ERROR;
4941 gst_rtspsrc_stream_is_real_media (GstRTSPStream * stream)
4943 gboolean res = FALSE;
4947 const gchar *enc = NULL;
4949 s = gst_caps_get_structure (stream->caps, 0);
4950 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
4951 res = (strstr (enc, "-REAL") != NULL);
4957 /* Perform the SETUP request for all the streams.
4959 * We ask the server for a specific transport, which initially includes all the
4960 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
4961 * two local UDP ports that we send to the server.
4963 * Once the server replied with a transport, we configure the other streams
4964 * with the same transport.
4966 * This function will also configure the stream for the selected transport,
4967 * which basically means creating the pipeline.
4969 static GstRTSPResult
4970 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
4973 GstRTSPResult res = GST_RTSP_ERROR;
4974 GstRTSPMessage request = { 0 };
4975 GstRTSPMessage response = { 0 };
4976 GstRTSPStream *stream = NULL;
4977 GstRTSPLowerTrans protocols;
4978 GstRTSPStatusCode code;
4979 gboolean unsupported_real = FALSE;
4980 gint rtpport, rtcpport;
4984 if (src->conninfo.connection) {
4985 url = gst_rtsp_connection_get_url (src->conninfo.connection);
4986 /* we initially allow all configured lower transports. based on the URL
4987 * transports and the replies from the server we narrow them down. */
4988 protocols = url->transports & src->cur_protocols;
4991 protocols = src->cur_protocols;
4997 /* reset some state */
4998 src->free_channel = 0;
4999 src->interleaved = FALSE;
5000 src->need_activate = FALSE;
5001 /* keep track of next port number, 0 is random */
5002 src->next_port_num = src->client_port_range.min;
5003 rtpport = rtcpport = 0;
5005 if (G_UNLIKELY (src->streams == NULL))
5008 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5009 GstRTSPConnection *conn;
5014 stream = (GstRTSPStream *) walk->data;
5016 /* see if we need to configure this stream */
5017 if (!gst_rtsp_ext_list_configure_stream (src->extensions, stream->caps)) {
5018 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
5020 stream->disabled = TRUE;
5024 /* merge/overwrite global caps */
5029 s = gst_caps_get_structure (stream->caps, 0);
5031 num = gst_structure_n_fields (src->props);
5032 for (j = 0; j < num; j++) {
5036 name = gst_structure_nth_field_name (src->props, j);
5037 val = gst_structure_get_value (src->props, name);
5038 gst_structure_set_value (s, name, val);
5040 GST_DEBUG_OBJECT (src, "copied %s", name);
5044 /* skip setup if we have no URL for it */
5045 if (stream->conninfo.location == NULL) {
5046 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
5050 if (src->conninfo.connection == NULL) {
5051 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
5052 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
5055 conn = stream->conninfo.connection;
5057 conn = src->conninfo.connection;
5059 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
5060 stream->conninfo.location);
5062 /* if we have a multicast connection, only suggest multicast from now on */
5063 if (stream->is_multicast)
5064 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
5067 /* first selectable protocol */
5068 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5070 if (!protocol_masks[mask])
5074 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
5075 protocol_masks[mask]);
5076 /* create a string with first transport in line */
5078 res = gst_rtspsrc_create_transports_string (src,
5079 protocols & protocol_masks[mask], &transports);
5080 if (res < 0 || transports == NULL)
5081 goto setup_transport_failed;
5083 if (strlen (transports) == 0) {
5084 g_free (transports);
5085 GST_DEBUG_OBJECT (src, "no transports found");
5090 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
5092 /* replace placeholders with real values, this function will optionally
5093 * allocate UDP ports and other info needed to execute the setup request */
5094 res = gst_rtspsrc_prepare_transports (stream, &transports,
5095 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
5097 g_free (transports);
5098 goto setup_transport_failed;
5101 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
5103 /* create SETUP request */
5105 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
5106 stream->conninfo.location);
5108 g_free (transports);
5109 goto create_request_failed;
5112 /* select transport, copy is made when adding to header so we can free it. */
5113 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
5114 g_free (transports);
5116 /* if the user wants a non default RTP packet size we add the blocksize
5118 if (src->rtp_blocksize > 0) {
5119 hval = g_strdup_printf ("%d", src->rtp_blocksize);
5120 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
5125 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
5128 /* handle the code ourselves */
5129 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
5133 case GST_RTSP_STS_OK:
5135 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
5136 gst_rtsp_message_unset (&request);
5137 gst_rtsp_message_unset (&response);
5138 /* cleanup of leftover transport */
5139 gst_rtspsrc_stream_free_udp (stream);
5140 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
5141 * we might be in this case */
5142 if (stream->container && rtpport && rtcpport && !retry) {
5143 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
5148 /* this transport did not go down well, but we may have others to try
5149 * that we did not send yet, try those and only give up then
5150 * but not without checking for lost cause/extension so we can
5151 * post a nicer/more useful error message later */
5152 if (!unsupported_real)
5153 unsupported_real = gst_rtspsrc_stream_is_real_media (stream);
5154 /* select next available protocol, give up on this stream if none */
5156 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5158 if (!protocol_masks[mask] || unsupported_real)
5163 /* cleanup of leftover transport and move to the next stream */
5164 gst_rtspsrc_stream_free_udp (stream);
5165 goto response_error;
5168 /* parse response transport */
5170 gchar *resptrans = NULL;
5171 GstRTSPTransport transport = { 0 };
5173 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
5176 gst_rtspsrc_stream_free_udp (stream);
5180 /* parse transport, go to next stream on parse error */
5181 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
5182 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
5186 /* update allowed transports for other streams. once the transport of
5187 * one stream has been determined, we make sure that all other streams
5188 * are configured in the same way */
5189 switch (transport.lower_transport) {
5190 case GST_RTSP_LOWER_TRANS_TCP:
5191 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
5192 protocols = GST_RTSP_LOWER_TRANS_TCP;
5193 src->interleaved = TRUE;
5194 /* update free channels */
5196 MAX (transport.interleaved.min, src->free_channel);
5198 MAX (transport.interleaved.max, src->free_channel);
5199 src->free_channel++;
5201 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
5202 /* only allow multicast for other streams */
5203 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
5204 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
5206 case GST_RTSP_LOWER_TRANS_UDP:
5207 /* only allow unicast for other streams */
5208 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
5209 protocols = GST_RTSP_LOWER_TRANS_UDP;
5212 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
5213 transport.lower_transport);
5217 if (!stream->container || (!src->interleaved && !retry)) {
5218 /* now configure the stream with the selected transport */
5219 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
5220 GST_DEBUG_OBJECT (src,
5221 "could not configure stream %p transport, skipping stream",
5224 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
5225 /* retain the first allocated UDP port pair */
5226 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
5227 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
5230 /* we need to activate at least one streams when we detect activity */
5231 src->need_activate = TRUE;
5233 /* clean up our transport struct */
5234 gst_rtsp_transport_init (&transport);
5235 /* clean up used RTSP messages */
5236 gst_rtsp_message_unset (&request);
5237 gst_rtsp_message_unset (&response);
5241 /* store the transport protocol that was configured */
5242 src->cur_protocols = protocols;
5244 gst_rtsp_ext_list_stream_select (src->extensions, url);
5246 /* if there is nothing to activate, error out */
5247 if (!src->need_activate)
5248 goto nothing_to_activate;
5255 /* no transport possible, post an error and stop */
5256 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5257 ("Could not connect to server, no protocols left"));
5258 return GST_RTSP_ERROR;
5262 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5263 ("SDP contains no streams"));
5264 return GST_RTSP_ERROR;
5266 create_request_failed:
5268 gchar *str = gst_rtsp_strresult (res);
5270 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5271 ("Could not create request. (%s)", str));
5275 setup_transport_failed:
5277 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5278 ("Could not setup transport."));
5279 res = GST_RTSP_ERROR;
5284 const gchar *str = gst_rtsp_status_as_text (code);
5286 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5287 ("Error (%d): %s", code, GST_STR_NULL (str)));
5288 res = GST_RTSP_ERROR;
5293 gchar *str = gst_rtsp_strresult (res);
5295 if (res != GST_RTSP_EINTR) {
5296 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5297 ("Could not send message. (%s)", str));
5299 GST_WARNING_OBJECT (src, "send interrupted");
5306 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5307 ("Server did not select transport."));
5308 res = GST_RTSP_ERROR;
5311 nothing_to_activate:
5313 /* none of the available error codes is really right .. */
5314 if (unsupported_real) {
5315 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5316 (_("No supported stream was found. You might need to install a "
5317 "GStreamer RTSP extension plugin for Real media streams.")),
5320 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5321 (_("No supported stream was found. You might need to allow "
5322 "more transport protocols or may otherwise be missing "
5323 "the right GStreamer RTSP extension plugin.")), (NULL));
5325 return GST_RTSP_ERROR;
5329 gst_rtsp_message_unset (&request);
5330 gst_rtsp_message_unset (&response);
5336 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
5337 GstSegment * segment)
5340 GstRTSPTimeRange *therange;
5343 gst_rtsp_range_free (src->range);
5345 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
5346 GST_DEBUG_OBJECT (src, "parsed range %s", range);
5347 src->range = therange;
5349 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
5351 gst_segment_init (segment, GST_FORMAT_TIME);
5355 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
5356 therange->min.type, therange->min.seconds, therange->max.type,
5357 therange->max.seconds);
5359 if (therange->min.type == GST_RTSP_TIME_NOW)
5361 else if (therange->min.type == GST_RTSP_TIME_END)
5364 seconds = therange->min.seconds * GST_SECOND;
5366 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
5367 GST_TIME_ARGS (seconds));
5369 /* we need to start playback without clipping from the position reported by
5371 segment->start = seconds;
5372 segment->position = seconds;
5374 if (therange->max.type == GST_RTSP_TIME_NOW)
5376 else if (therange->max.type == GST_RTSP_TIME_END)
5379 seconds = therange->max.seconds * GST_SECOND;
5381 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
5382 GST_TIME_ARGS (seconds));
5384 /* live (WMS) server might send overflowed large max as its idea of infinity,
5385 * compensate to prevent problems later on */
5386 if (seconds != -1 && seconds < 0) {
5388 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
5391 /* live (WMS) might send min == max, which is not worth recording */
5392 if (segment->duration == -1 && seconds == segment->start)
5395 /* don't change duration with unknown value, we might have a valid value
5396 * there that we want to keep. */
5398 segment->duration = seconds;
5403 /* must be called with the RTSP state lock */
5404 static GstRTSPResult
5405 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
5411 /* prepare global stream caps properties */
5413 gst_structure_remove_all_fields (src->props);
5415 src->props = gst_structure_new_empty ("RTSPProperties");
5418 gst_sdp_message_dump (sdp);
5420 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
5422 gst_segment_init (&src->segment, GST_FORMAT_TIME);
5424 /* parse range for duration reporting. */
5429 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
5433 /* keep track of the range and configure it in the segment */
5434 if (gst_rtspsrc_parse_range (src, range, &src->segment))
5438 /* try to find a global control attribute. Note that a '*' means that we should
5439 * do aggregate control with the current url (so we don't do anything and
5440 * leave the current connection as is) */
5442 const gchar *control;
5445 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
5446 if (control == NULL)
5449 /* only take fully qualified urls */
5450 if (g_str_has_prefix (control, "rtsp://"))
5454 g_free (src->conninfo.location);
5455 src->conninfo.location = g_strdup (control);
5456 /* make a connection for this, if there was a connection already, nothing
5458 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
5459 GST_ERROR_OBJECT (src, "could not connect");
5462 /* we need to keep the control url separate from the connection url because
5463 * the rules for constructing the media control url need it */
5464 g_free (src->control);
5465 src->control = g_strdup (control);
5468 /* create streams */
5469 n_streams = gst_sdp_message_medias_len (sdp);
5470 for (i = 0; i < n_streams; i++) {
5471 gst_rtspsrc_create_stream (src, sdp, i);
5474 src->state = GST_RTSP_STATE_INIT;
5477 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
5480 /* reset our state */
5481 src->need_range = TRUE;
5484 src->state = GST_RTSP_STATE_READY;
5491 GST_ERROR_OBJECT (src, "setup failed");
5496 static GstRTSPResult
5497 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
5501 GstRTSPMessage request = { 0 };
5502 GstRTSPMessage response = { 0 };
5505 gchar *respcont = NULL;
5508 src->need_redirect = FALSE;
5510 /* can't continue without a valid url */
5511 if (G_UNLIKELY (src->conninfo.url == NULL)) {
5512 res = GST_RTSP_EINVAL;
5515 src->tried_url_auth = FALSE;
5517 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
5518 goto connect_failed;
5520 /* create OPTIONS */
5521 GST_DEBUG_OBJECT (src, "create options...");
5523 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
5524 src->conninfo.url_str);
5526 goto create_request_failed;
5529 GST_DEBUG_OBJECT (src, "send options...");
5532 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
5535 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
5540 if (!gst_rtspsrc_parse_methods (src, &response))
5543 /* create DESCRIBE */
5544 GST_DEBUG_OBJECT (src, "create describe...");
5546 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
5547 src->conninfo.url_str);
5549 goto create_request_failed;
5551 /* we only accept SDP for now */
5552 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
5556 GST_DEBUG_OBJECT (src, "send describe...");
5559 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
5562 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
5566 /* we only perform redirect for the describe, currently */
5567 if (src->need_redirect) {
5568 /* close connection, we don't have to send a TEARDOWN yet, ignore the
5570 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5572 gst_rtsp_message_unset (&request);
5573 gst_rtsp_message_unset (&response);
5579 /* it could be that the DESCRIBE method was not implemented */
5580 if (!src->methods & GST_RTSP_DESCRIBE)
5583 /* check if reply is SDP */
5584 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
5586 /* could not be set but since the request returned OK, we assume it
5587 * was SDP, else check it. */
5589 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
5590 goto wrong_content_type;
5593 /* get message body and parse as SDP */
5594 gst_rtsp_message_get_body (&response, &data, &size);
5595 if (data == NULL || size == 0)
5598 GST_DEBUG_OBJECT (src, "parse SDP...");
5599 gst_sdp_message_new (sdp);
5600 gst_sdp_message_parse_buffer (data, size, *sdp);
5602 /* clean up any messages */
5603 gst_rtsp_message_unset (&request);
5604 gst_rtsp_message_unset (&response);
5611 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
5612 ("No valid RTSP URL was provided"));
5617 gchar *str = gst_rtsp_strresult (res);
5619 if (res != GST_RTSP_EINTR) {
5620 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
5621 ("Failed to connect. (%s)", str));
5623 GST_WARNING_OBJECT (src, "connect interrupted");
5628 create_request_failed:
5630 gchar *str = gst_rtsp_strresult (res);
5632 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5633 ("Could not create request. (%s)", str));
5639 /* Don't post a message - the rtsp_send method will have
5640 * taken care of it because we passed NULL for the response code */
5645 /* error was posted */
5646 res = GST_RTSP_ERROR;
5651 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5652 ("Server does not support SDP, got %s.", respcont));
5653 res = GST_RTSP_ERROR;
5658 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5659 ("Server can not provide an SDP."));
5660 res = GST_RTSP_ERROR;
5665 if (src->conninfo.connection) {
5666 GST_DEBUG_OBJECT (src, "free connection");
5667 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5669 gst_rtsp_message_unset (&request);
5670 gst_rtsp_message_unset (&response);
5675 static GstRTSPResult
5676 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
5681 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
5683 if (src->sdp == NULL) {
5684 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
5688 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
5693 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
5700 GST_WARNING_OBJECT (src, "can't get sdp");
5701 src->open_error = TRUE;
5706 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
5707 src->open_error = TRUE;
5712 static GstRTSPResult
5713 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
5715 GstRTSPMessage request = { 0 };
5716 GstRTSPMessage response = { 0 };
5717 GstRTSPResult res = GST_RTSP_OK;
5721 GST_DEBUG_OBJECT (src, "TEARDOWN...");
5723 if (src->state < GST_RTSP_STATE_READY) {
5724 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
5731 /* construct a control url */
5733 control = src->control;
5735 control = src->conninfo.url_str;
5737 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
5740 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5741 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5743 GstRTSPConnInfo *info;
5745 /* try aggregate control first but do non-aggregate control otherwise */
5747 setup_url = control;
5748 else if ((setup_url = stream->conninfo.location) == NULL)
5751 if (src->conninfo.connection) {
5752 info = &src->conninfo;
5753 } else if (stream->conninfo.connection) {
5754 info = &stream->conninfo;
5758 if (!info->connected)
5763 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
5765 goto create_request_failed;
5768 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
5771 gst_rtspsrc_send (src, info->connection, &request, &response,
5775 /* FIXME, parse result? */
5776 gst_rtsp_message_unset (&request);
5777 gst_rtsp_message_unset (&response);
5780 /* early exit when we did aggregate control */
5786 /* close connections */
5787 GST_DEBUG_OBJECT (src, "closing connection...");
5788 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5789 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5790 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5791 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
5795 gst_rtspsrc_cleanup (src);
5797 src->state = GST_RTSP_STATE_INVALID;
5800 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
5805 create_request_failed:
5807 gchar *str = gst_rtsp_strresult (res);
5809 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5810 ("Could not create request. (%s)", str));
5816 gchar *str = gst_rtsp_strresult (res);
5818 gst_rtsp_message_unset (&request);
5819 if (res != GST_RTSP_EINTR) {
5820 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5821 ("Could not send message. (%s)", str));
5823 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
5830 GST_DEBUG_OBJECT (src,
5831 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
5836 /* RTP-Info is of the format:
5838 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
5840 * rtptime corresponds to the timestamp for the NPT time given in the header
5841 * seqbase corresponds to the next sequence number we received. This number
5842 * indicates the first seqnum after the seek and should be used to discard
5843 * packets that are from before the seek.
5846 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
5851 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
5853 infos = g_strsplit (rtpinfo, ",", 0);
5854 for (i = 0; infos[i]; i++) {
5856 GstRTSPStream *stream;
5860 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
5862 /* init values, types of seqbase and timebase are bigger than needed so we
5863 * can store -1 as uninitialized values */
5868 /* parse url, find stream for url.
5869 * parse seq and rtptime. The seq number should be configured in the rtp
5870 * depayloader or session manager to detect gaps. Same for the rtptime, it
5871 * should be used to create an initial time newsegment. */
5872 fields = g_strsplit (infos[i], ";", 0);
5873 for (j = 0; fields[j]; j++) {
5874 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
5875 /* remove leading whitespace */
5876 fields[j] = g_strchug (fields[j]);
5877 if (g_str_has_prefix (fields[j], "url=")) {
5878 /* get the url and the stream */
5880 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
5881 } else if (g_str_has_prefix (fields[j], "seq=")) {
5882 seqbase = atoi (fields[j] + 4);
5883 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
5884 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
5887 g_strfreev (fields);
5888 /* now we need to store the values for the caps of the stream */
5889 if (stream != NULL) {
5890 GST_DEBUG_OBJECT (src,
5891 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
5892 stream, seqbase, timebase);
5894 /* we have a stream, configure detected params */
5895 stream->seqbase = seqbase;
5896 stream->timebase = timebase;
5905 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
5910 interval = strtoul (rtcp, NULL, 10);
5911 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
5916 interval *= GST_MSECOND;
5918 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5919 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5921 /* already (optionally) retrieved this when configuring manager */
5922 if (stream->session) {
5923 GObject *rtpsession = stream->session;
5925 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
5927 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
5931 /* now it happens that (Xenon) server sending this may also provide bogus
5932 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
5933 * and just use RTP-Info to sync */
5935 GObjectClass *klass;
5937 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
5938 if (g_object_class_find_property (klass, "rtcp-sync")) {
5939 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
5940 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
5946 gst_rtspsrc_get_float (const gchar * dstr)
5948 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
5950 /* canonicalise floating point string so we can handle float strings
5951 * in the form "24.930" or "24,930" irrespective of the current locale */
5952 g_strlcpy (s, dstr, sizeof (s));
5953 g_strdelimit (s, ",", '.');
5954 return g_ascii_strtod (s, NULL);
5958 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
5960 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
5962 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
5963 g_strlcpy (val_str, "now", sizeof (val_str));
5965 if (segment->position == 0) {
5966 g_strlcpy (val_str, "0", sizeof (val_str));
5968 g_ascii_dtostr (val_str, sizeof (val_str),
5969 ((gdouble) segment->position) / GST_SECOND);
5972 return g_strdup_printf ("npt=%s-", val_str);
5976 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
5978 stream->timebase = -1;
5979 stream->seqbase = -1;
5983 stream->caps = gst_caps_make_writable (stream->caps);
5984 s = gst_caps_get_structure (stream->caps, 0);
5985 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
5989 static GstRTSPResult
5990 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
5992 GstRTSPResult res = GST_RTSP_OK;
5994 if (src->state < GST_RTSP_STATE_READY) {
5995 res = GST_RTSP_ERROR;
5996 if (src->open_error) {
5997 GST_DEBUG_OBJECT (src, "the stream was in error");
6001 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
6003 if ((res = gst_rtspsrc_open (src, async)) < 0) {
6004 GST_DEBUG_OBJECT (src, "failed to open stream");
6013 static GstRTSPResult
6014 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
6016 GstRTSPMessage request = { 0 };
6017 GstRTSPMessage response = { 0 };
6018 GstRTSPResult res = GST_RTSP_OK;
6024 GST_DEBUG_OBJECT (src, "PLAY...");
6026 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6029 if (!(src->methods & GST_RTSP_PLAY))
6032 if (src->state == GST_RTSP_STATE_PLAYING)
6035 if (!src->conninfo.connection || !src->conninfo.connected)
6038 /* send some dummy packets before we activate the receive in the
6040 gst_rtspsrc_send_dummy_packets (src);
6042 /* activate receive elements;
6043 * only in async case, since receive elements may not have been affected
6044 * by overall state change (e.g. not around yet),
6045 * do not mess with state in sync case (e.g. seeking) */
6047 gst_element_set_state (GST_ELEMENT_CAST (src), GST_STATE_PLAYING);
6049 /* construct a control url */
6051 control = src->control;
6053 control = src->conninfo.url_str;
6055 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6056 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6058 GstRTSPConnection *conn;
6060 /* try aggregate control first but do non-aggregate control otherwise */
6062 setup_url = control;
6063 else if ((setup_url = stream->conninfo.location) == NULL)
6066 if (src->conninfo.connection) {
6067 conn = src->conninfo.connection;
6068 } else if (stream->conninfo.connection) {
6069 conn = stream->conninfo.connection;
6075 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
6077 goto create_request_failed;
6079 if (src->need_range) {
6080 hval = gen_range_header (src, segment);
6082 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_RANGE, hval);
6086 if (segment->rate != 1.0) {
6087 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
6089 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
6091 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
6093 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
6097 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
6099 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6102 /* seek may have silently failed as it is not supported */
6103 if (!(src->methods & GST_RTSP_PLAY)) {
6104 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
6105 /* obviously it is supported as we made it here */
6106 src->methods |= GST_RTSP_PLAY;
6107 src->seekable = FALSE;
6108 /* but there is nothing to parse in the response,
6109 * so convey we have no idea and not to expect anything particular */
6110 clear_rtp_base (src, stream);
6114 /* need to do for all streams */
6115 for (run = src->streams; run; run = g_list_next (run))
6116 clear_rtp_base (src, (GstRTSPStream *) run->data);
6118 /* NOTE the above also disables npt based eos detection */
6119 /* and below forces position to 0,
6120 * which is visible feedback we lost the plot */
6121 segment->start = segment->position = src->last_pos;
6124 gst_rtsp_message_unset (&request);
6126 /* parse RTP npt field. This is the current position in the stream (Normal
6127 * Play Time) and should be put in the NEWSEGMENT position field. */
6128 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
6130 gst_rtspsrc_parse_range (src, hval, segment);
6132 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
6133 segment->rate = 1.0;
6135 /* parse Speed header. This is the intended playback rate of the stream
6136 * and should be put in the NEWSEGMENT rate field. */
6137 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
6138 0) == GST_RTSP_OK) {
6139 segment->rate = gst_rtspsrc_get_float (hval);
6140 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
6141 &hval, 0) == GST_RTSP_OK) {
6142 segment->rate = gst_rtspsrc_get_float (hval);
6145 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
6146 * for the RTP packets. If this is not present, we assume all starts from 0...
6147 * This is info for the RTP session manager that we pass to it in caps. */
6149 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
6150 &hval, hval_idx++) == GST_RTSP_OK)
6151 gst_rtspsrc_parse_rtpinfo (src, hval);
6153 /* some servers indicate RTCP parameters in PLAY response,
6154 * rather than properly in SDP */
6155 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
6156 &hval, 0) == GST_RTSP_OK)
6157 gst_rtspsrc_handle_rtcp_interval (src, hval);
6159 gst_rtsp_message_unset (&response);
6161 /* early exit when we did aggregate control */
6165 /* set again when needed */
6166 src->need_range = FALSE;
6168 /* configure the caps of the streams after we parsed all headers. */
6169 gst_rtspsrc_configure_caps (src, segment);
6171 src->running = TRUE;
6172 src->base_time = -1;
6173 src->state = GST_RTSP_STATE_PLAYING;
6176 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
6177 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6178 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6179 stream->discont = TRUE;
6184 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
6191 GST_DEBUG_OBJECT (src, "failed to open stream");
6196 GST_DEBUG_OBJECT (src, "PLAY is not supported");
6201 GST_DEBUG_OBJECT (src, "we were already PLAYING");
6204 create_request_failed:
6206 gchar *str = gst_rtsp_strresult (res);
6208 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6209 ("Could not create request. (%s)", str));
6215 gchar *str = gst_rtsp_strresult (res);
6217 gst_rtsp_message_unset (&request);
6218 if (res != GST_RTSP_EINTR) {
6219 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6220 ("Could not send message. (%s)", str));
6222 GST_WARNING_OBJECT (src, "PLAY interrupted");
6229 static GstRTSPResult
6230 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean idle, gboolean async)
6232 GstRTSPResult res = GST_RTSP_OK;
6233 GstRTSPMessage request = { 0 };
6234 GstRTSPMessage response = { 0 };
6238 GST_DEBUG_OBJECT (src, "PAUSE...");
6240 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6243 if (!(src->methods & GST_RTSP_PAUSE))
6246 if (src->state == GST_RTSP_STATE_READY)
6249 if (!src->conninfo.connection || !src->conninfo.connected)
6252 /* construct a control url */
6254 control = src->control;
6256 control = src->conninfo.url_str;
6258 /* loop over the streams. We might exit the loop early when we could do an
6259 * aggregate control */
6260 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6261 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6262 GstRTSPConnection *conn;
6265 /* try aggregate control first but do non-aggregate control otherwise */
6267 setup_url = control;
6268 else if ((setup_url = stream->conninfo.location) == NULL)
6271 if (src->conninfo.connection) {
6272 conn = src->conninfo.connection;
6273 } else if (stream->conninfo.connection) {
6274 conn = stream->conninfo.connection;
6280 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
6281 ("Sending PAUSE request"));
6284 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
6286 goto create_request_failed;
6288 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6291 gst_rtsp_message_unset (&request);
6292 gst_rtsp_message_unset (&response);
6294 /* exit early when we did agregate control */
6300 src->state = GST_RTSP_STATE_READY;
6304 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
6311 GST_DEBUG_OBJECT (src, "failed to open stream");
6316 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
6321 GST_DEBUG_OBJECT (src, "we were already PAUSED");
6324 create_request_failed:
6326 gchar *str = gst_rtsp_strresult (res);
6328 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6329 ("Could not create request. (%s)", str));
6335 gchar *str = gst_rtsp_strresult (res);
6337 gst_rtsp_message_unset (&request);
6338 if (res != GST_RTSP_EINTR) {
6339 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6340 ("Could not send message. (%s)", str));
6342 GST_WARNING_OBJECT (src, "PAUSE interrupted");
6350 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
6352 GstRTSPSrc *rtspsrc;
6354 rtspsrc = GST_RTSPSRC (bin);
6356 switch (GST_MESSAGE_TYPE (message)) {
6357 case GST_MESSAGE_EOS:
6358 gst_message_unref (message);
6360 case GST_MESSAGE_ELEMENT:
6362 const GstStructure *s = gst_message_get_structure (message);
6364 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
6365 gboolean ignore_timeout;
6367 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
6369 GST_OBJECT_LOCK (rtspsrc);
6370 ignore_timeout = rtspsrc->ignore_timeout;
6371 rtspsrc->ignore_timeout = TRUE;
6372 GST_OBJECT_UNLOCK (rtspsrc);
6374 /* we only act on the first udp timeout message, others are irrelevant
6375 * and can be ignored. */
6376 if (!ignore_timeout)
6377 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT);
6379 gst_message_unref (message);
6382 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6385 case GST_MESSAGE_ERROR:
6388 GstRTSPStream *stream;
6391 udpsrc = GST_MESSAGE_SRC (message);
6393 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
6394 GST_ELEMENT_NAME (udpsrc));
6396 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
6400 /* we ignore the RTCP udpsrc */
6401 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
6404 /* if we get error messages from the udp sources, that's not a problem as
6405 * long as not all of them error out. We also don't really know what the
6406 * problem is, the message does not give enough detail... */
6407 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
6408 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
6409 if (ret != GST_FLOW_OK)
6413 gst_message_unref (message);
6417 /* fatal but not our message, forward */
6418 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6423 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6429 /* the thread where everything happens */
6431 gst_rtspsrc_thread (GstRTSPSrc * src)
6435 gboolean running = FALSE;
6437 GST_OBJECT_LOCK (src);
6438 cmd = src->loop_cmd;
6439 src->loop_cmd = CMD_WAIT;
6440 GST_DEBUG_OBJECT (src, "got command %d", cmd);
6442 /* we got the message command, so ensure communication is possible again */
6443 gst_rtspsrc_connection_flush (src, FALSE);
6445 /* we allow these to be interrupted */
6446 if (cmd == CMD_LOOP || cmd == CMD_CLOSE || cmd == CMD_PAUSE)
6447 src->waiting = TRUE;
6448 GST_OBJECT_UNLOCK (src);
6452 ret = gst_rtspsrc_open (src, TRUE);
6455 ret = gst_rtspsrc_play (src, &src->segment, TRUE);
6456 if (ret == GST_RTSP_OK)
6460 ret = gst_rtspsrc_pause (src, TRUE, TRUE);
6461 if (ret == GST_RTSP_OK)
6465 ret = gst_rtspsrc_close (src, TRUE, FALSE);
6468 running = gst_rtspsrc_loop (src);
6471 ret = gst_rtspsrc_reconnect (src, FALSE);
6472 if (ret == GST_RTSP_OK)
6479 GST_OBJECT_LOCK (src);
6480 /* and go back to sleep */
6481 if (src->loop_cmd == CMD_WAIT) {
6483 src->loop_cmd = CMD_LOOP;
6485 gst_task_pause (src->task);
6488 src->waiting = FALSE;
6489 GST_OBJECT_UNLOCK (src);
6493 gst_rtspsrc_start (GstRTSPSrc * src)
6495 GST_DEBUG_OBJECT (src, "starting");
6497 GST_OBJECT_LOCK (src);
6499 src->loop_cmd = CMD_WAIT;
6501 if (src->task == NULL) {
6502 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src);
6503 if (src->task == NULL)
6506 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
6508 GST_OBJECT_UNLOCK (src);
6515 GST_ERROR_OBJECT (src, "failed to create task");
6521 gst_rtspsrc_stop (GstRTSPSrc * src)
6525 GST_DEBUG_OBJECT (src, "stopping");
6527 /* also cancels pending task */
6528 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT);
6530 GST_OBJECT_LOCK (src);
6531 if ((task = src->task)) {
6533 GST_OBJECT_UNLOCK (src);
6535 gst_task_stop (task);
6537 /* make sure it is not running */
6538 GST_RTSP_STREAM_LOCK (src);
6539 GST_RTSP_STREAM_UNLOCK (src);
6541 /* now wait for the task to finish */
6542 gst_task_join (task);
6544 /* and free the task */
6545 gst_object_unref (GST_OBJECT (task));
6547 GST_OBJECT_LOCK (src);
6549 GST_OBJECT_UNLOCK (src);
6551 /* ensure synchronously all is closed and clean */
6552 gst_rtspsrc_close (src, FALSE, TRUE);
6557 static GstStateChangeReturn
6558 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
6560 GstRTSPSrc *rtspsrc;
6561 GstStateChangeReturn ret;
6563 rtspsrc = GST_RTSPSRC (element);
6565 switch (transition) {
6566 case GST_STATE_CHANGE_NULL_TO_READY:
6567 if (!gst_rtspsrc_start (rtspsrc))
6570 case GST_STATE_CHANGE_READY_TO_PAUSED:
6571 /* init some state */
6572 rtspsrc->cur_protocols = rtspsrc->protocols;
6573 /* first attempt, don't ignore timeouts */
6574 rtspsrc->ignore_timeout = FALSE;
6575 rtspsrc->open_error = FALSE;
6576 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN);
6578 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
6579 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
6580 /* unblock the tcp tasks and make the loop waiting */
6581 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT);
6583 case GST_STATE_CHANGE_PAUSED_TO_READY:
6589 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
6590 if (ret == GST_STATE_CHANGE_FAILURE)
6593 switch (transition) {
6594 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
6595 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY);
6597 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
6598 /* send pause request and keep the idle task around */
6599 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE);
6600 ret = GST_STATE_CHANGE_NO_PREROLL;
6602 case GST_STATE_CHANGE_READY_TO_PAUSED:
6603 ret = GST_STATE_CHANGE_NO_PREROLL;
6605 case GST_STATE_CHANGE_PAUSED_TO_READY:
6606 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE);
6608 case GST_STATE_CHANGE_READY_TO_NULL:
6609 gst_rtspsrc_stop (rtspsrc);
6620 GST_DEBUG_OBJECT (rtspsrc, "start failed");
6621 return GST_STATE_CHANGE_FAILURE;
6626 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
6629 GstRTSPSrc *rtspsrc;
6631 rtspsrc = GST_RTSPSRC (element);
6633 if (GST_EVENT_IS_DOWNSTREAM (event)) {
6634 res = gst_rtspsrc_push_event (rtspsrc, event, TRUE);
6636 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
6643 /*** GSTURIHANDLER INTERFACE *************************************************/
6646 gst_rtspsrc_uri_get_type (GType type)
6651 static const gchar *const *
6652 gst_rtspsrc_uri_get_protocols (GType type)
6654 static const gchar *protocols[] =
6655 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp", NULL };
6661 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
6663 GstRTSPSrc *src = GST_RTSPSRC (handler);
6665 /* FIXME: make thread-safe */
6666 return g_strdup (src->conninfo.location);
6670 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
6675 GstRTSPUrl *newurl = NULL;
6676 GstSDPMessage *sdp = NULL;
6678 src = GST_RTSPSRC (handler);
6680 /* same URI, we're fine */
6681 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
6684 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
6685 if ((res = gst_sdp_message_new (&sdp) < 0))
6688 GST_DEBUG_OBJECT (src, "parsing SDP message");
6689 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
6693 GST_DEBUG_OBJECT (src, "parsing URI");
6694 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
6698 /* if worked, free previous and store new url object along with the original
6700 GST_DEBUG_OBJECT (src, "configuring URI");
6701 g_free (src->conninfo.location);
6702 src->conninfo.location = g_strdup (uri);
6703 gst_rtsp_url_free (src->conninfo.url);
6704 src->conninfo.url = newurl;
6705 g_free (src->conninfo.url_str);
6707 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
6709 src->conninfo.url_str = NULL;
6712 gst_sdp_message_free (src->sdp);
6714 src->from_sdp = sdp != NULL;
6716 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
6717 GST_DEBUG_OBJECT (src, "request uri is: %s",
6718 GST_STR_NULL (src->conninfo.url_str));
6725 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
6730 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
6731 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
6732 "Could not create SDP");
6737 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
6738 GST_STR_NULL (uri));
6739 gst_sdp_message_free (sdp);
6740 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
6746 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
6747 GST_STR_NULL (uri), res);
6748 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
6749 "Invalid RTSP URI");
6755 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
6757 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
6759 iface->get_type = gst_rtspsrc_uri_get_type;
6760 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
6761 iface->get_uri = gst_rtspsrc_uri_get_uri;
6762 iface->set_uri = gst_rtspsrc_uri_set_uri;