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)
86 #endif /* HAVE_UNISTD_H */
93 #include <gst/sdp/gstsdpmessage.h>
94 #include <gst/rtp/gstrtppayloads.h>
96 #include "gst/gst-i18n-plugin.h"
98 #include "gstrtspsrc.h"
101 #include <winsock2.h>
104 GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
105 #define GST_CAT_DEFAULT (rtspsrc_debug)
107 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream%d",
110 GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
112 /* templates used internally */
113 static GstStaticPadTemplate anysrctemplate =
114 GST_STATIC_PAD_TEMPLATE ("internalsrc%d",
117 GST_STATIC_CAPS_ANY);
119 static GstStaticPadTemplate anysinktemplate =
120 GST_STATIC_PAD_TEMPLATE ("internalsink%d",
123 GST_STATIC_CAPS_ANY);
131 enum _GstRtspSrcBufferMode
139 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
141 gst_rtsp_src_buffer_mode_get_type (void)
143 static GType buffer_mode_type = 0;
144 static const GEnumValue buffer_modes[] = {
145 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
146 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
147 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
148 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
152 if (!buffer_mode_type) {
154 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
156 return buffer_mode_type;
159 #define DEFAULT_LOCATION NULL
160 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
161 #define DEFAULT_DEBUG FALSE
162 #define DEFAULT_RETRY 20
163 #define DEFAULT_TIMEOUT 5000000
164 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
165 #define DEFAULT_TCP_TIMEOUT 20000000
166 #define DEFAULT_LATENCY_MS 2000
167 #define DEFAULT_CONNECTION_SPEED 0
168 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
169 #define DEFAULT_DO_RTCP TRUE
170 #define DEFAULT_PROXY NULL
171 #define DEFAULT_RTP_BLOCKSIZE 0
172 #define DEFAULT_USER_ID NULL
173 #define DEFAULT_USER_PW NULL
174 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
175 #define DEFAULT_PORT_RANGE NULL
187 PROP_CONNECTION_SPEED,
196 PROP_UDP_BUFFER_SIZE,
200 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
202 gst_rtsp_nat_method_get_type (void)
204 static GType rtsp_nat_method_type = 0;
205 static const GEnumValue rtsp_nat_method[] = {
206 {GST_RTSP_NAT_NONE, "None", "none"},
207 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
211 if (!rtsp_nat_method_type) {
212 rtsp_nat_method_type =
213 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
215 return rtsp_nat_method_type;
218 static void gst_rtspsrc_finalize (GObject * object);
220 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
221 const GValue * value, GParamSpec * pspec);
222 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
223 GValue * value, GParamSpec * pspec);
225 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
226 gpointer iface_data);
228 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
231 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
232 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
234 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
236 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
237 GstStateChange transition);
238 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
239 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
241 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
242 GstRTSPMessage * response);
244 static void gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
246 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
247 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
249 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
250 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
252 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean idle,
254 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
255 gboolean only_close);
257 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
260 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
261 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
262 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
263 GstRTSPStream * stream, GstEvent * event, gboolean source);
264 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event,
267 /* commands we send to out loop to notify it of events */
273 #define CMD_RECONNECT 5
276 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
278 gchar *__txt = _gst_element_error_printf text; \
279 gst_element_post_message (GST_ELEMENT_CAST (el), \
280 gst_message_new_progress (GST_OBJECT_CAST (el), \
281 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
285 /*static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 }; */
286 #define gst_rtspsrc_parent_class parent_class
287 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
288 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
291 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
293 GObjectClass *gobject_class;
294 GstElementClass *gstelement_class;
295 GstBinClass *gstbin_class;
297 gobject_class = (GObjectClass *) klass;
298 gstelement_class = (GstElementClass *) klass;
299 gstbin_class = (GstBinClass *) klass;
301 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
303 gobject_class->set_property = gst_rtspsrc_set_property;
304 gobject_class->get_property = gst_rtspsrc_get_property;
306 gobject_class->finalize = gst_rtspsrc_finalize;
308 g_object_class_install_property (gobject_class, PROP_LOCATION,
309 g_param_spec_string ("location", "RTSP Location",
310 "Location of the RTSP url to read",
311 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
313 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
314 g_param_spec_flags ("protocols", "Protocols",
315 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
316 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
318 g_object_class_install_property (gobject_class, PROP_DEBUG,
319 g_param_spec_boolean ("debug", "Debug",
320 "Dump request and response messages to stdout",
321 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
323 g_object_class_install_property (gobject_class, PROP_RETRY,
324 g_param_spec_uint ("retry", "Retry",
325 "Max number of retries when allocating RTP ports.",
326 0, G_MAXUINT16, DEFAULT_RETRY,
327 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
329 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
330 g_param_spec_uint64 ("timeout", "Timeout",
331 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
332 0, G_MAXUINT64, DEFAULT_TIMEOUT,
333 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
335 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
336 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
337 "Fail after timeout microseconds on TCP connections (0 = disabled)",
338 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
339 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
341 g_object_class_install_property (gobject_class, PROP_LATENCY,
342 g_param_spec_uint ("latency", "Buffer latency in ms",
343 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
344 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
346 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
347 g_param_spec_uint ("connection-speed", "Connection Speed",
348 "Network connection speed in kbps (0 = unknown)",
349 0, G_MAXINT / 1000, DEFAULT_CONNECTION_SPEED,
350 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
352 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
353 g_param_spec_enum ("nat-method", "NAT Method",
354 "Method to use for traversing firewalls and NAT",
355 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
356 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
359 * GstRTSPSrc::do-rtcp
361 * Enable RTCP support. Some old server don't like RTCP and then this property
362 * needs to be set to FALSE.
366 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
367 g_param_spec_boolean ("do-rtcp", "Do RTCP",
368 "Send RTCP packets, disable for old incompatible server.",
369 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
374 * Set the proxy parameters. This has to be a string of the format
375 * [http://][user:passwd@]host[:port].
379 g_object_class_install_property (gobject_class, PROP_PROXY,
380 g_param_spec_string ("proxy", "Proxy",
381 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
382 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
385 * GstRTSPSrc::rtp_blocksize
387 * RTP package size to suggest to server.
391 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
392 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
393 "RTP package size to suggest to server (0 = disabled)",
394 0, 65536, DEFAULT_RTP_BLOCKSIZE,
395 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
397 g_object_class_install_property (gobject_class,
399 g_param_spec_string ("user-id", "user-id",
400 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
401 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
402 g_object_class_install_property (gobject_class, PROP_USER_PW,
403 g_param_spec_string ("user-pw", "user-pw",
404 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
405 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
408 * GstRTSPSrc::buffer-mode:
410 * Control the buffering and timestamping mode used by the jitterbuffer.
414 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
415 g_param_spec_enum ("buffer-mode", "Buffer Mode",
416 "Control the buffering algorithm in use",
417 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
418 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
421 * GstRTSPSrc::port-range:
423 * Configure the client port numbers that can be used to recieve RTP and
428 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
429 g_param_spec_string ("port-range", "Port range",
430 "Client port range that can be used to receive RTP and RTCP data, "
431 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
432 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
435 * GstRTSPSrc::udp-buffer-size:
437 * Size of the kernel UDP receive buffer in bytes.
441 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
442 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
443 "Size of the kernel UDP receive buffer in bytes, 0=default",
444 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
445 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
447 gstelement_class->send_event = gst_rtspsrc_send_event;
448 gstelement_class->change_state = gst_rtspsrc_change_state;
450 gst_element_class_add_pad_template (gstelement_class,
451 gst_static_pad_template_get (&rtptemplate));
453 gst_element_class_set_details_simple (gstelement_class,
454 "RTSP packet receiver", "Source/Network",
455 "Receive data over the network via RTSP (RFC 2326)",
456 "Wim Taymans <wim@fluendo.com>, "
457 "Thijs Vermeir <thijs.vermeir@barco.com>, "
458 "Lutz Mueller <lutz@topfrose.de>");
460 gstbin_class->handle_message = gst_rtspsrc_handle_message;
462 gst_rtsp_ext_list_init ();
467 gst_rtspsrc_init (GstRTSPSrc * src)
472 if (WSAStartup (MAKEWORD (2, 2), &wsa_data) != 0) {
473 GST_ERROR_OBJECT (src, "WSAStartup failed: 0x%08x", WSAGetLastError ());
477 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
478 src->protocols = DEFAULT_PROTOCOLS;
479 src->debug = DEFAULT_DEBUG;
480 src->retry = DEFAULT_RETRY;
481 src->udp_timeout = DEFAULT_TIMEOUT;
482 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
483 src->latency = DEFAULT_LATENCY_MS;
484 src->connection_speed = DEFAULT_CONNECTION_SPEED;
485 src->nat_method = DEFAULT_NAT_METHOD;
486 src->do_rtcp = DEFAULT_DO_RTCP;
487 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
488 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
489 src->user_id = g_strdup (DEFAULT_USER_ID);
490 src->user_pw = g_strdup (DEFAULT_USER_PW);
491 src->buffer_mode = DEFAULT_BUFFER_MODE;
492 src->client_port_range.min = 0;
493 src->client_port_range.max = 0;
494 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
496 /* get a list of all extensions */
497 src->extensions = gst_rtsp_ext_list_get ();
499 /* connect to send signal */
500 gst_rtsp_ext_list_connect (src->extensions, "send",
501 (GCallback) gst_rtspsrc_send_cb, src);
503 /* protects the streaming thread in interleaved mode or the polling
504 * thread in UDP mode. */
505 src->stream_rec_lock = g_new (GStaticRecMutex, 1);
506 g_static_rec_mutex_init (src->stream_rec_lock);
508 /* protects our state changes from multiple invocations */
509 src->state_rec_lock = g_new (GStaticRecMutex, 1);
510 g_static_rec_mutex_init (src->state_rec_lock);
512 src->state = GST_RTSP_STATE_INVALID;
516 gst_rtspsrc_finalize (GObject * object)
520 rtspsrc = GST_RTSPSRC (object);
522 gst_rtsp_ext_list_free (rtspsrc->extensions);
523 g_free (rtspsrc->conninfo.location);
524 gst_rtsp_url_free (rtspsrc->conninfo.url);
525 g_free (rtspsrc->conninfo.url_str);
526 g_free (rtspsrc->user_id);
527 g_free (rtspsrc->user_pw);
530 gst_sdp_message_free (rtspsrc->sdp);
535 g_static_rec_mutex_free (rtspsrc->stream_rec_lock);
536 g_free (rtspsrc->stream_rec_lock);
537 g_static_rec_mutex_free (rtspsrc->state_rec_lock);
538 g_free (rtspsrc->state_rec_lock);
544 G_OBJECT_CLASS (parent_class)->finalize (object);
547 /* a proxy string of the format [user:passwd@]host[:port] */
549 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
553 g_free (rtsp->proxy_user);
554 rtsp->proxy_user = NULL;
555 g_free (rtsp->proxy_passwd);
556 rtsp->proxy_passwd = NULL;
557 g_free (rtsp->proxy_host);
558 rtsp->proxy_host = NULL;
559 rtsp->proxy_port = 0;
566 /* we allow http:// in front but ignore it */
567 if (g_str_has_prefix (p, "http://"))
570 at = strchr (p, '@');
572 /* look for user:passwd */
573 col = strchr (proxy, ':');
574 if (col == NULL || col > at)
577 rtsp->proxy_user = g_strndup (p, col - p);
579 rtsp->proxy_passwd = g_strndup (col, at - col);
584 col = strchr (p, ':');
587 /* everything before the colon is the hostname */
588 rtsp->proxy_host = g_strndup (p, col - p);
590 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
592 rtsp->proxy_host = g_strdup (p);
593 rtsp->proxy_port = 8080;
599 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
601 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
602 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
605 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
607 rtspsrc->ptcp_timeout = NULL;
611 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
616 rtspsrc = GST_RTSPSRC (object);
620 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
621 g_value_get_string (value));
624 rtspsrc->protocols = g_value_get_flags (value);
627 rtspsrc->debug = g_value_get_boolean (value);
630 rtspsrc->retry = g_value_get_uint (value);
633 rtspsrc->udp_timeout = g_value_get_uint64 (value);
635 case PROP_TCP_TIMEOUT:
636 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
639 rtspsrc->latency = g_value_get_uint (value);
641 case PROP_CONNECTION_SPEED:
642 rtspsrc->connection_speed = g_value_get_uint (value);
644 case PROP_NAT_METHOD:
645 rtspsrc->nat_method = g_value_get_enum (value);
648 rtspsrc->do_rtcp = g_value_get_boolean (value);
651 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
653 case PROP_RTP_BLOCKSIZE:
654 rtspsrc->rtp_blocksize = g_value_get_uint (value);
657 if (rtspsrc->user_id)
658 g_free (rtspsrc->user_id);
659 rtspsrc->user_id = g_value_dup_string (value);
662 if (rtspsrc->user_pw)
663 g_free (rtspsrc->user_pw);
664 rtspsrc->user_pw = g_value_dup_string (value);
666 case PROP_BUFFER_MODE:
667 rtspsrc->buffer_mode = g_value_get_enum (value);
669 case PROP_PORT_RANGE:
673 str = g_value_get_string (value);
675 sscanf (str, "%u-%u",
676 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
678 rtspsrc->client_port_range.min = 0;
679 rtspsrc->client_port_range.max = 0;
683 case PROP_UDP_BUFFER_SIZE:
684 rtspsrc->udp_buffer_size = g_value_get_int (value);
687 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
693 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
698 rtspsrc = GST_RTSPSRC (object);
702 g_value_set_string (value, rtspsrc->conninfo.location);
705 g_value_set_flags (value, rtspsrc->protocols);
708 g_value_set_boolean (value, rtspsrc->debug);
711 g_value_set_uint (value, rtspsrc->retry);
714 g_value_set_uint64 (value, rtspsrc->udp_timeout);
716 case PROP_TCP_TIMEOUT:
720 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
721 rtspsrc->tcp_timeout.tv_usec;
722 g_value_set_uint64 (value, timeout);
726 g_value_set_uint (value, rtspsrc->latency);
728 case PROP_CONNECTION_SPEED:
729 g_value_set_uint (value, rtspsrc->connection_speed);
731 case PROP_NAT_METHOD:
732 g_value_set_enum (value, rtspsrc->nat_method);
735 g_value_set_boolean (value, rtspsrc->do_rtcp);
741 if (rtspsrc->proxy_host) {
743 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
747 g_value_take_string (value, str);
750 case PROP_RTP_BLOCKSIZE:
751 g_value_set_uint (value, rtspsrc->rtp_blocksize);
754 g_value_set_string (value, rtspsrc->user_id);
757 g_value_set_string (value, rtspsrc->user_pw);
759 case PROP_BUFFER_MODE:
760 g_value_set_enum (value, rtspsrc->buffer_mode);
762 case PROP_PORT_RANGE:
766 if (rtspsrc->client_port_range.min != 0) {
767 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
768 rtspsrc->client_port_range.max);
772 g_value_take_string (value, str);
775 case PROP_UDP_BUFFER_SIZE:
776 g_value_set_int (value, rtspsrc->udp_buffer_size);
779 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
785 find_stream_by_id (GstRTSPStream * stream, gint * id)
787 if (stream->id == *id)
794 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
796 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
803 find_stream_by_pt (GstRTSPStream * stream, gint * pt)
805 if (stream->pt == *pt)
812 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
814 GstElement *src = (GstElement *) a;
816 if (stream->udpsrc[0] == src)
818 if (stream->udpsrc[1] == src)
825 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
827 /* check qualified setup_url */
828 if (!strcmp (stream->conninfo.location, (gchar *) a))
830 /* check original control_url */
831 if (!strcmp (stream->control_url, (gchar *) a))
834 /* check if qualified setup_url ends with string */
835 if (g_str_has_suffix (stream->control_url, (gchar *) a))
841 static GstRTSPStream *
842 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
846 /* find and get stream */
847 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
848 return (GstRTSPStream *) lstream->data;
853 static const GstSDPBandwidth *
854 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
855 const GstSDPMedia * media, const gchar * type)
859 /* first look in the media specific section */
860 len = gst_sdp_media_bandwidths_len (media);
861 for (i = 0; i < len; i++) {
862 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
864 if (strcmp (bw->bwtype, type) == 0)
867 /* then look in the message specific section */
868 len = gst_sdp_message_bandwidths_len (sdp);
869 for (i = 0; i < len; i++) {
870 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
872 if (strcmp (bw->bwtype, type) == 0)
879 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
880 const GstSDPMedia * media, GstRTSPStream * stream)
882 const GstSDPBandwidth *bw;
884 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
885 stream->as_bandwidth = bw->bandwidth;
887 stream->as_bandwidth = -1;
889 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
890 stream->rr_bandwidth = bw->bandwidth;
892 stream->rr_bandwidth = -1;
894 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
895 stream->rs_bandwidth = bw->bandwidth;
897 stream->rs_bandwidth = -1;
901 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
902 const GstSDPConnection * conn)
904 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
907 if (conn->addrtype == NULL)
911 if (strcmp (conn->addrtype, "IP4") == 0)
912 stream->is_ipv6 = FALSE;
913 else if (strcmp (conn->addrtype, "IP6") == 0)
914 stream->is_ipv6 = TRUE;
919 g_free (stream->destination);
920 stream->destination = g_strdup (conn->address);
922 /* check for multicast */
923 stream->is_multicast =
924 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
926 stream->ttl = conn->ttl;
929 /* Go over the connections for a stream.
930 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
932 * - If we are dealing with a localhost address, we disable multicast
935 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
936 const GstSDPMedia * media, GstRTSPStream * stream)
938 const GstSDPConnection *conn;
941 /* first look in the media specific section */
942 len = gst_sdp_media_connections_len (media);
943 for (i = 0; i < len; i++) {
944 conn = gst_sdp_media_get_connection (media, i);
946 gst_rtspsrc_do_stream_connection (src, stream, conn);
948 /* then look in the message specific section */
949 if ((conn = gst_sdp_message_get_connection (sdp))) {
950 gst_rtspsrc_do_stream_connection (src, stream, conn);
954 static GstRTSPStream *
955 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
957 GstRTSPStream *stream;
958 const gchar *control_url;
959 const gchar *payload;
960 const GstSDPMedia *media;
962 /* get media, should not return NULL */
963 media = gst_sdp_message_get_media (sdp, idx);
967 stream = g_new0 (GstRTSPStream, 1);
968 stream->parent = src;
969 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
971 stream->last_ret = GST_FLOW_NOT_LINKED;
972 stream->added = FALSE;
973 stream->disabled = FALSE;
974 stream->id = src->numstreams++;
976 stream->discont = TRUE;
977 stream->seqbase = -1;
978 stream->timebase = -1;
980 /* collect bandwidth information for this steam. FIXME, configure in the RTP
981 * session manager to scale RTCP. */
982 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
984 /* collect connection info */
985 gst_rtspsrc_collect_connections (src, sdp, media, stream);
987 /* we must have a payload. No payload means we cannot create caps */
988 /* FIXME, handle multiple formats. The problem here is that we just want to
989 * take the first available format that we can handle but in order to do that
990 * we need to scan for depayloader plugins. Scanning for payloader plugins is
991 * also suboptimal because the user maybe just wants to save the raw stream
992 * and then we don't care. */
993 if ((payload = gst_sdp_media_get_format (media, 0))) {
994 stream->pt = atoi (payload);
996 stream->caps = gst_rtspsrc_media_to_caps (stream->pt, media);
998 GST_DEBUG ("mapping sdp session level attributes to caps");
999 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, stream->caps);
1000 GST_DEBUG ("mapping sdp media level attributes to caps");
1001 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, stream->caps);
1003 if (stream->pt >= 96) {
1004 /* If we have a dynamic payload type, see if we have a stream with the
1005 * same payload number. If there is one, they are part of the same
1006 * container and we only need to add one pad. */
1007 if (find_stream (src, &stream->pt, (gpointer) find_stream_by_pt)) {
1008 stream->container = TRUE;
1009 GST_DEBUG ("found another stream with pt %d, marking as container",
1014 /* collect port number */
1015 stream->port = gst_sdp_media_get_port (media);
1017 /* get control url to construct the setup url. The setup url is used to
1018 * configure the transport of the stream and is used to identity the stream in
1019 * the RTP-Info header field returned from PLAY. */
1020 control_url = gst_sdp_media_get_attribute_val (media, "control");
1021 if (control_url == NULL)
1022 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1024 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1025 GST_DEBUG_OBJECT (src, " pt: %d", stream->pt);
1026 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1027 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1028 GST_DEBUG_OBJECT (src, " caps: %" GST_PTR_FORMAT, stream->caps);
1029 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1031 if (control_url != NULL) {
1032 stream->control_url = g_strdup (control_url);
1033 /* Build a fully qualified url using the content_base if any or by prefixing
1034 * the original request.
1035 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1036 * likely build a URL that the server will fail to understand, this is ok,
1037 * we will fail then. */
1038 if (g_str_has_prefix (control_url, "rtsp://"))
1039 stream->conninfo.location = g_strdup (control_url);
1044 if (g_strcmp0 (control_url, "*") == 0)
1048 base = src->control;
1049 else if (src->content_base)
1050 base = src->content_base;
1051 else if (src->conninfo.url_str)
1052 base = src->conninfo.url_str;
1056 /* check if the base ends or control starts with / */
1057 has_slash = g_str_has_prefix (control_url, "/");
1058 has_slash = has_slash || g_str_has_suffix (base, "/");
1060 /* concatenate the two strings, insert / when not present */
1061 stream->conninfo.location =
1062 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1065 GST_DEBUG_OBJECT (src, " setup: %s",
1066 GST_STR_NULL (stream->conninfo.location));
1068 /* we keep track of all streams */
1069 src->streams = g_list_append (src->streams, stream);
1077 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1081 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1084 gst_caps_unref (stream->caps);
1086 g_free (stream->destination);
1087 g_free (stream->control_url);
1088 g_free (stream->conninfo.location);
1090 for (i = 0; i < 2; i++) {
1091 if (stream->udpsrc[i]) {
1092 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1093 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1094 gst_object_unref (stream->udpsrc[i]);
1095 stream->udpsrc[i] = NULL;
1097 if (stream->channelpad[i]) {
1098 gst_object_unref (stream->channelpad[i]);
1099 stream->channelpad[i] = NULL;
1101 if (stream->udpsink[i]) {
1102 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1103 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1104 gst_object_unref (stream->udpsink[i]);
1105 stream->udpsink[i] = NULL;
1108 if (stream->fakesrc) {
1109 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1110 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1111 gst_object_unref (stream->fakesrc);
1112 stream->fakesrc = NULL;
1114 if (stream->srcpad) {
1115 gst_pad_set_active (stream->srcpad, FALSE);
1116 if (stream->added) {
1117 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1118 stream->added = FALSE;
1120 stream->srcpad = NULL;
1122 if (stream->rtcppad) {
1123 gst_object_unref (stream->rtcppad);
1124 stream->rtcppad = NULL;
1126 if (stream->session) {
1127 g_object_unref (stream->session);
1128 stream->session = NULL;
1134 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1138 GST_DEBUG_OBJECT (src, "cleanup");
1140 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1141 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1143 gst_rtspsrc_stream_free (src, stream);
1145 g_list_free (src->streams);
1146 src->streams = NULL;
1148 if (src->manager_sig_id) {
1149 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1150 src->manager_sig_id = 0;
1152 gst_element_set_state (src->manager, GST_STATE_NULL);
1153 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1154 src->manager = NULL;
1156 src->numstreams = 0;
1158 gst_structure_free (src->props);
1161 g_free (src->content_base);
1162 src->content_base = NULL;
1164 g_free (src->control);
1165 src->control = NULL;
1168 gst_rtsp_range_free (src->range);
1171 /* don't clear the SDP when it was used in the url */
1172 if (src->sdp && !src->from_sdp) {
1173 gst_sdp_message_free (src->sdp);
1178 #define PARSE_INT(p, del, res) \
1181 p = strstr (p, del); \
1191 #define PARSE_STRING(p, del, res) \
1194 p = strstr (p, del); \
1206 #define SKIP_SPACES(p) \
1207 while (*p && g_ascii_isspace (*p)) \
1212 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1215 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1216 gint * rate, gchar ** params)
1220 p = (gchar *) rtpmap;
1222 PARSE_INT (p, " ", *payload);
1230 PARSE_STRING (p, "/", *name);
1231 if (*name == NULL) {
1232 GST_DEBUG ("no rate, name %s", p);
1233 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1234 * streams seem to omit the rate. */
1241 p = strstr (p, "/");
1259 * Mapping SDP attributes to caps
1261 * prepend 'a-' to IANA registered sdp attributes names
1262 * (ie: not prefixed with 'x-') in order to avoid
1263 * collision with gstreamer standard caps properties names
1266 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1268 if (attributes->len > 0) {
1272 s = gst_caps_get_structure (caps, 0);
1274 for (i = 0; i < attributes->len; i++) {
1275 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1276 gchar *tofree, *key;
1280 /* skip some of the attribute we already handle */
1281 if (!strcmp (key, "fmtp"))
1283 if (!strcmp (key, "rtpmap"))
1285 if (!strcmp (key, "control"))
1287 if (!strcmp (key, "range"))
1290 /* string must be valid UTF8 */
1291 if (!g_utf8_validate (attr->value, -1, NULL))
1294 if (!g_str_has_prefix (key, "x-"))
1295 tofree = key = g_strdup_printf ("a-%s", key);
1299 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1300 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1307 * Mapping of caps to and from SDP fields:
1309 * m=<media> <UDP port> RTP/AVP <payload>
1310 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1311 * a=fmtp:<payload> <param>[=<value>];...
1314 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1317 const gchar *rtpmap;
1321 gchar *params = NULL;
1327 /* get and parse rtpmap */
1328 if ((rtpmap = gst_sdp_media_get_attribute_val (media, "rtpmap"))) {
1329 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
1331 if (payload != pt) {
1332 /* we ignore the rtpmap if the payload type is different. */
1333 g_warning ("rtpmap of wrong payload type, ignoring");
1339 /* if we failed to parse the rtpmap for a dynamic payload type, we have an
1343 /* else we can ignore */
1344 g_warning ("error parsing rtpmap, ignoring");
1347 /* dynamic payloads need rtpmap or we fail */
1351 /* check if we have a rate, if not, we need to look up the rate from the
1352 * default rates based on the payload types. */
1354 const GstRTPPayloadInfo *info;
1356 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
1357 /* dynamic types, use media and encoding_name */
1358 tmp = g_ascii_strdown (media->media, -1);
1359 info = gst_rtp_payload_info_for_name (tmp, name);
1362 /* static types, use payload type */
1363 info = gst_rtp_payload_info_for_pt (pt);
1367 if ((rate = info->clock_rate) == 0)
1370 /* we fail if we cannot find one */
1375 tmp = g_ascii_strdown (media->media, -1);
1376 caps = gst_caps_new_simple ("application/x-unknown",
1377 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
1379 s = gst_caps_get_structure (caps, 0);
1381 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
1383 /* encoding name must be upper case */
1385 tmp = g_ascii_strup (name, -1);
1386 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
1390 /* params must be lower case */
1391 if (params != NULL) {
1392 tmp = g_ascii_strdown (params, -1);
1393 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
1397 /* parse optional fmtp: field */
1398 if ((fmtp = gst_sdp_media_get_attribute_val (media, "fmtp"))) {
1404 /* p is now of the format <payload> <param>[=<value>];... */
1405 PARSE_INT (p, " ", payload);
1406 if (payload != -1 && payload == pt) {
1410 /* <param>[=<value>] are separated with ';' */
1411 pairs = g_strsplit (p, ";", 0);
1412 for (i = 0; pairs[i]; i++) {
1414 const gchar *val, *key;
1416 /* the key may not have a '=', the value can have other '='s */
1417 valpos = strstr (pairs[i], "=");
1419 /* we have a '=' and thus a value, remove the '=' with \0 */
1421 /* value is everything between '=' and ';'. We split the pairs at ;
1422 * boundaries so we can take the remainder of the value. Some servers
1423 * put spaces around the value which we strip off here. Alternatively
1424 * we could strip those spaces in the depayloaders should these spaces
1425 * actually carry any meaning in the future. */
1426 val = g_strstrip (valpos + 1);
1428 /* simple <param>;.. is translated into <param>=1;... */
1431 /* strip the key of spaces, convert key to lowercase but not the value. */
1432 key = g_strstrip (pairs[i]);
1433 if (strlen (key) > 1) {
1434 tmp = g_ascii_strdown (key, -1);
1435 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
1447 g_warning ("rtpmap type not given for dynamic payload %d", pt);
1452 g_warning ("rate unknown for payload type %d", pt);
1458 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1459 gint * rtpport, gint * rtcpport)
1462 GstStateChangeReturn ret;
1463 GstElement *udpsrc0, *udpsrc1;
1464 gint tmp_rtp, tmp_rtcp;
1468 src = stream->parent;
1474 /* Start at next port */
1475 tmp_rtp = src->next_port_num;
1477 if (stream->is_ipv6)
1478 host = "udp://[::0]";
1480 host = "udp://0.0.0.0";
1482 /* try to allocate 2 UDP ports, the RTP port should be an even
1483 * number and the RTCP port should be the next (uneven) port */
1486 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1487 tmp_rtp >= src->client_port_range.max)
1490 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
1491 if (udpsrc0 == NULL)
1492 goto no_udp_protocol;
1493 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1495 if (src->udp_buffer_size != 0)
1496 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
1499 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
1500 if (ret == GST_STATE_CHANGE_FAILURE) {
1502 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
1505 if (++count > src->retry)
1508 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1509 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1510 gst_object_unref (udpsrc0);
1512 GST_DEBUG_OBJECT (src, "retry %d", count);
1515 goto no_udp_protocol;
1518 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
1519 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
1521 /* check if port is even */
1522 if ((tmp_rtp & 0x01) != 0) {
1523 /* port not even, close and allocate another */
1524 if (++count > src->retry)
1527 GST_DEBUG_OBJECT (src, "RTP port not even");
1529 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1530 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1531 gst_object_unref (udpsrc0);
1533 GST_DEBUG_OBJECT (src, "retry %d", count);
1538 /* allocate port+1 for RTCP now */
1539 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
1540 if (udpsrc1 == NULL)
1541 goto no_udp_rtcp_protocol;
1544 tmp_rtcp = tmp_rtp + 1;
1545 if (src->client_port_range.max > 0 && tmp_rtcp >= src->client_port_range.max)
1548 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
1550 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
1551 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
1552 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
1553 if (ret == GST_STATE_CHANGE_FAILURE) {
1554 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
1556 if (++count > src->retry)
1559 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1560 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1561 gst_object_unref (udpsrc0);
1563 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
1564 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1565 gst_object_unref (udpsrc1);
1569 GST_DEBUG_OBJECT (src, "retry %d", count);
1573 /* all fine, do port check */
1574 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
1575 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
1577 /* this should not happen... */
1578 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
1581 /* we keep these elements, we configure all in configure_transport when the
1582 * server told us to really use the UDP ports. */
1583 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
1584 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
1586 /* keep track of next available port number when we have a range
1588 if (src->next_port_num != 0)
1589 src->next_port_num = tmp_rtcp + 1;
1596 GST_DEBUG_OBJECT (src, "could not get UDP source");
1601 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
1605 no_udp_rtcp_protocol:
1607 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
1612 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
1613 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
1619 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1620 gst_object_unref (udpsrc0);
1623 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1624 gst_object_unref (udpsrc1);
1631 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush)
1638 GstClockTime base_time = GST_CLOCK_TIME_NONE;
1641 event = gst_event_new_flush_start ();
1642 GST_DEBUG_OBJECT (src, "start flush");
1644 state = GST_STATE_PAUSED;
1646 event = gst_event_new_flush_stop ();
1647 GST_DEBUG_OBJECT (src, "stop flush");
1649 state = GST_STATE_PLAYING;
1650 clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
1652 base_time = gst_clock_get_time (clock);
1653 gst_object_unref (clock);
1656 gst_rtspsrc_push_event (src, event, FALSE);
1657 gst_rtspsrc_loop_send_cmd (src, cmd, flush);
1659 /* set up manager before data-flow resumes */
1660 /* to manage jitterbuffer buffer mode */
1662 gst_element_set_base_time (GST_ELEMENT_CAST (src->manager), base_time);
1663 /* and to have base_time trickle further down,
1664 * e.g. to jitterbuffer for its timeout handling */
1665 if (base_time != -1)
1666 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
1669 /* make running time start start at 0 again */
1670 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1671 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1673 for (i = 0; i < 2; i++) {
1675 if (stream->udpsrc[i]) {
1676 if (base_time != -1)
1677 gst_element_set_base_time (stream->udpsrc[i], base_time);
1678 gst_element_set_state (stream->udpsrc[i], state);
1682 /* for tcp interleaved case */
1683 if (base_time != -1)
1684 gst_element_set_base_time (GST_ELEMENT_CAST (src), base_time);
1687 static GstRTSPResult
1688 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
1689 GstRTSPMessage * message, GTimeVal * timeout)
1694 ret = gst_rtsp_connection_send (conn, message, timeout);
1696 ret = GST_RTSP_ERROR;
1701 static GstRTSPResult
1702 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
1703 GstRTSPMessage * message, GTimeVal * timeout)
1708 ret = gst_rtsp_connection_receive (conn, message, timeout);
1710 ret = GST_RTSP_ERROR;
1716 gst_rtspsrc_get_position (GstRTSPSrc * src)
1721 query = gst_query_new_position (GST_FORMAT_TIME);
1722 /* should be known somewhere down the stream (e.g. jitterbuffer) */
1723 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1724 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1728 if (stream->srcpad) {
1729 if (gst_pad_query (stream->srcpad, query)) {
1730 gst_query_parse_position (query, &fmt, &pos);
1731 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
1732 GST_TIME_ARGS (pos));
1733 src->last_pos = pos;
1743 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
1745 src->state = GST_RTSP_STATE_SEEKING;
1746 /* PLAY will add the range header now. */
1747 src->need_range = TRUE;
1753 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
1758 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
1760 gboolean flush, skip;
1763 GstSegment seeksegment = { 0, };
1767 GST_DEBUG_OBJECT (src, "doing seek with event");
1769 gst_event_parse_seek (event, &rate, &format, &flags,
1770 &cur_type, &cur, &stop_type, &stop);
1772 /* no negative rates yet */
1776 /* we need TIME format */
1777 if (format != src->segment.format)
1780 GST_DEBUG_OBJECT (src, "doing seek without event");
1782 cur_type = GST_SEEK_TYPE_SET;
1783 stop_type = GST_SEEK_TYPE_SET;
1786 /* get flush flag */
1787 flush = flags & GST_SEEK_FLAG_FLUSH;
1788 skip = flags & GST_SEEK_FLAG_SKIP;
1790 /* now we need to make sure the streaming thread is stopped. We do this by
1791 * either sending a FLUSH_START event downstream which will cause the
1792 * streaming thread to stop with a WRONG_STATE.
1793 * For a non-flushing seek we simply pause the task, which will happen as soon
1794 * as it completes one iteration (and thus might block when the sink is
1795 * blocking in preroll). */
1797 GST_DEBUG_OBJECT (src, "starting flush");
1798 gst_rtspsrc_flush (src, TRUE);
1801 gst_task_pause (src->task);
1805 /* we should now be able to grab the streaming thread because we stopped it
1806 * with the above flush/pause code */
1807 GST_RTSP_STREAM_LOCK (src);
1809 GST_DEBUG_OBJECT (src, "stopped streaming");
1811 /* copy segment, we need this because we still need the old
1812 * segment when we close the current segment. */
1813 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1815 /* configure the seek parameters in the seeksegment. We will then have the
1816 * right values in the segment to perform the seek */
1818 GST_DEBUG_OBJECT (src, "configuring seek");
1819 gst_segment_set_seek (&seeksegment, rate, format, flags,
1820 cur_type, cur, stop_type, stop, &update);
1823 /* figure out the last position we need to play. If it's configured (stop !=
1824 * -1), use that, else we play until the total duration of the file */
1825 if ((stop = seeksegment.stop) == -1)
1826 stop = seeksegment.duration;
1828 playing = (src->state == GST_RTSP_STATE_PLAYING);
1830 /* if we were playing, pause first */
1832 /* obtain current position in case seek fails */
1833 gst_rtspsrc_get_position (src);
1834 gst_rtspsrc_pause (src, FALSE, FALSE);
1837 gst_rtspsrc_do_seek (src, &seeksegment);
1839 /* and continue playing */
1841 gst_rtspsrc_play (src, &seeksegment, FALSE);
1843 /* prepare for streaming again */
1845 /* if we started flush, we stop now */
1846 GST_DEBUG_OBJECT (src, "stopping flush");
1847 gst_rtspsrc_flush (src, FALSE);
1848 } else if (src->running) {
1849 /* re-engage loop */
1850 gst_rtspsrc_loop_send_cmd (src, CMD_LOOP, FALSE);
1852 /* we are running the current segment and doing a non-flushing seek,
1853 * close the segment first based on the previous last_stop. */
1854 GST_DEBUG_OBJECT (src, "closing running segment %" G_GINT64_FORMAT
1855 " to %" G_GINT64_FORMAT, src->segment.accum, src->segment.last_stop);
1857 /* queue the segment for sending in the stream thread */
1858 if (src->close_segment)
1859 gst_event_unref (src->close_segment);
1860 src->close_segment = gst_event_new_new_segment (TRUE,
1861 src->segment.rate, src->segment.format,
1862 src->segment.accum, src->segment.last_stop, src->segment.accum);
1864 /* keep track of our last_stop */
1865 seeksegment.accum = src->segment.last_stop;
1868 /* now we did the seek and can activate the new segment values */
1869 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1871 /* if we're doing a segment seek, post a SEGMENT_START message */
1872 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1873 gst_element_post_message (GST_ELEMENT_CAST (src),
1874 gst_message_new_segment_start (GST_OBJECT_CAST (src),
1875 src->segment.format, src->segment.last_stop));
1878 /* now create the newsegment */
1879 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
1880 " to %" G_GINT64_FORMAT, src->segment.last_stop, stop);
1882 /* store the newsegment event so it can be sent from the streaming thread. */
1883 if (src->start_segment)
1884 gst_event_unref (src->start_segment);
1885 src->start_segment =
1886 gst_event_new_new_segment (FALSE, src->segment.rate,
1887 src->segment.format, src->segment.last_stop, stop,
1888 src->segment.last_stop);
1891 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
1892 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1893 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1894 stream->discont = TRUE;
1898 GST_RTSP_STREAM_UNLOCK (src);
1905 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
1910 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
1916 gst_rtspsrc_handle_src_event (GstPad * pad, GstEvent * event)
1919 gboolean res = TRUE;
1922 src = GST_RTSPSRC_CAST (gst_pad_get_parent (pad));
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);
1951 gst_object_unref (src);
1956 /* this is the final event function we receive on the internal source pad when
1957 * we deal with TCP connections */
1959 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstEvent * event)
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, GstQuery * query)
1988 gboolean res = TRUE;
1990 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
1992 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
1993 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
1995 switch (GST_QUERY_TYPE (query)) {
1996 case GST_QUERY_POSITION:
2001 case GST_QUERY_DURATION:
2005 gst_query_parse_duration (query, &format, NULL);
2008 case GST_FORMAT_TIME:
2009 gst_query_set_duration (query, format, src->segment.duration);
2017 case GST_QUERY_LATENCY:
2019 /* we are live with a min latency of 0 and unlimited max latency, this
2020 * result will be updated by the session manager if there is any. */
2021 gst_query_set_latency (query, TRUE, 0, -1);
2031 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2033 gst_rtspsrc_handle_src_query (GstPad * pad, GstQuery * query)
2036 gboolean res = FALSE;
2038 src = GST_RTSPSRC_CAST (gst_pad_get_parent (pad));
2040 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2041 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2043 switch (GST_QUERY_TYPE (query)) {
2044 case GST_QUERY_DURATION:
2048 gst_query_parse_duration (query, &format, NULL);
2051 case GST_FORMAT_TIME:
2052 gst_query_set_duration (query, format, src->segment.duration);
2060 case GST_QUERY_SEEKING:
2064 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2065 if (format == GST_FORMAT_TIME) {
2067 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2069 /* seeking without duration is unlikely */
2070 seekable = seekable && src->seekable && src->segment.duration &&
2071 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2073 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2074 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2075 src->segment.start, src->segment.stop);
2082 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2084 /* forward the query to the proxy target pad */
2086 res = gst_pad_query (target, query);
2087 gst_object_unref (target);
2092 gst_object_unref (src);
2097 /* callback for RTCP messages to be sent to the server when operating in TCP
2099 static GstFlowReturn
2100 gst_rtspsrc_sink_chain (GstPad * pad, GstBuffer * buffer)
2103 GstRTSPStream *stream;
2104 GstFlowReturn res = GST_FLOW_OK;
2109 GstRTSPMessage message = { 0 };
2110 GstRTSPConnection *conn;
2112 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2113 src = stream->parent;
2115 data = gst_buffer_map (buffer, &bsize, NULL, GST_MAP_READ);
2118 gst_rtsp_message_init_data (&message, stream->channel[1]);
2120 /* lend the body data to the message */
2121 gst_rtsp_message_take_body (&message, data, size);
2123 if (stream->conninfo.connection)
2124 conn = stream->conninfo.connection;
2126 conn = src->conninfo.connection;
2128 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2129 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2130 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2132 /* and steal it away again because we will free it when unreffing the
2134 gst_rtsp_message_steal_body (&message, &data, &size);
2135 gst_rtsp_message_unset (&message);
2137 gst_buffer_unmap (buffer, data, size);
2138 gst_buffer_unref (buffer);
2144 pad_unblocked (GstPad * pad, gboolean blocked, GstRTSPSrc * src)
2146 GST_DEBUG_OBJECT (src, "pad %s:%s unblocked", GST_DEBUG_PAD_NAME (pad));
2150 pad_blocked (GstPad * pad, gboolean blocked, GstRTSPSrc * src)
2152 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2153 GST_DEBUG_PAD_NAME (pad));
2155 /* activate the streams */
2156 GST_OBJECT_LOCK (src);
2157 if (!src->need_activate)
2160 src->need_activate = FALSE;
2161 GST_OBJECT_UNLOCK (src);
2163 gst_rtspsrc_activate_streams (src);
2169 GST_OBJECT_UNLOCK (src);
2174 /* this callback is called when the session manager generated a new src pad with
2175 * payloaded RTP packets. We simply ghost the pad here. */
2177 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2180 GstPadTemplate *template;
2183 GstRTSPStream *stream;
2186 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2188 GST_RTSP_STATE_LOCK (src);
2190 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2191 if (sscanf (name, "recv_rtp_src_%d_%d_%d", &id, &ssrc, &pt) != 3)
2192 goto unknown_stream;
2194 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %d, PT %d", id, ssrc, pt);
2196 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2198 goto unknown_stream;
2200 /* create a new pad we will use to stream to */
2201 template = gst_static_pad_template_get (&rtptemplate);
2202 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2203 gst_object_unref (template);
2206 stream->added = TRUE;
2207 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2208 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2209 gst_pad_set_active (stream->srcpad, TRUE);
2210 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2212 /* check if we added all streams */
2214 for (lstream = src->streams; lstream; lstream = g_list_next (lstream)) {
2215 stream = (GstRTSPStream *) lstream->data;
2217 GST_DEBUG_OBJECT (src, "stream %p, container %d, disabled %d, added %d",
2218 stream, stream->container, stream->disabled, stream->added);
2220 /* a container stream only needs one pad added. Also disabled streams don't
2222 if (!stream->container && !stream->disabled && !stream->added) {
2227 GST_RTSP_STATE_UNLOCK (src);
2230 GST_DEBUG_OBJECT (src, "We added all streams");
2231 /* when we get here, all stream are added and we can fire the no-more-pads
2233 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2241 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2242 GST_RTSP_STATE_UNLOCK (src);
2249 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2251 GstRTSPStream *stream;
2254 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2256 GST_RTSP_STATE_LOCK (src);
2257 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2259 goto unknown_stream;
2261 caps = stream->caps;
2263 gst_caps_ref (caps);
2264 GST_RTSP_STATE_UNLOCK (src);
2270 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2271 GST_RTSP_STATE_UNLOCK (src);
2277 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2279 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2285 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos (), TRUE);
2291 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2297 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2299 GstRTSPSrc *src = stream->parent;
2301 GST_DEBUG_OBJECT (src, "source in session %u received BYE", stream->id);
2303 gst_rtspsrc_do_stream_eos (src, stream);
2307 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2309 GstRTSPSrc *src = stream->parent;
2311 GST_DEBUG_OBJECT (src, "source in session %u timed out", stream->id);
2313 gst_rtspsrc_do_stream_eos (src, stream);
2317 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2319 GstRTSPStream *stream;
2321 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2323 /* get stream for session */
2324 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2326 gst_rtspsrc_do_stream_eos (src, stream);
2331 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2333 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2337 /* try to get and configure a manager */
2339 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
2340 GstRTSPTransport * transport)
2342 const gchar *manager;
2344 GstStateChangeReturn ret;
2346 /* find a manager */
2347 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
2351 GST_DEBUG_OBJECT (src, "using manager %s", manager);
2353 /* configure the manager */
2354 if (src->manager == NULL) {
2355 GObjectClass *klass;
2358 if (!(src->manager = gst_element_factory_make (manager, NULL))) {
2360 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
2364 goto use_no_manager;
2366 if (!(src->manager = gst_element_factory_make (manager, NULL)))
2367 goto manager_failed;
2370 /* we manage this element */
2371 gst_bin_add (GST_BIN_CAST (src), src->manager);
2373 GST_OBJECT_LOCK (src);
2374 target = GST_STATE_TARGET (src);
2375 GST_OBJECT_UNLOCK (src);
2377 ret = gst_element_set_state (src->manager, target);
2378 if (ret == GST_STATE_CHANGE_FAILURE)
2379 goto start_manager_failure;
2381 g_object_set (src->manager, "latency", src->latency, NULL);
2383 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2384 if (g_object_class_find_property (klass, "buffer-mode")) {
2385 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2386 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2388 gboolean need_slave;
2390 const gchar *encoding;
2392 /* buffer mode pauses are handled by adding offsets to buffer times,
2393 * but some depayloaders may have a hard time syncing output times
2394 * with such input times, e.g. container ones, most notably ASF */
2395 /* TODO alternatives are having an event that indicates these shifts,
2396 * or having rtsp extensions provide suggestion on buffer mode */
2397 need_slave = stream->container;
2398 if (stream->caps && (s = gst_caps_get_structure (stream->caps, 0)) &&
2399 (encoding = gst_structure_get_string (s, "encoding-name")))
2400 need_slave = need_slave || (strcmp (encoding, "X-ASF-PF") == 0);
2401 GST_DEBUG_OBJECT (src, "auto buffering mode, need_slave %d",
2403 /* valid duration implies not likely live pipeline,
2404 * so slaving in jitterbuffer does not make much sense
2405 * (and might mess things up due to bursts) */
2406 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
2407 src->segment.duration && !need_slave) {
2408 GST_DEBUG_OBJECT (src, "selected buffer");
2409 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER,
2412 GST_DEBUG_OBJECT (src, "selected slave");
2413 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2418 /* connect to signals if we did not already do so */
2419 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
2421 src->manager_sig_id =
2422 g_signal_connect (src->manager, "pad-added",
2423 (GCallback) new_manager_pad, src);
2424 src->manager_ptmap_id =
2425 g_signal_connect (src->manager, "request-pt-map",
2426 (GCallback) request_pt_map, src);
2428 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
2432 /* we stream directly to the manager, get some pads. Each RTSP stream goes
2433 * into a separate RTP session. */
2434 name = g_strdup_printf ("recv_rtp_sink_%d", stream->id);
2435 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
2437 name = g_strdup_printf ("recv_rtcp_sink_%d", stream->id);
2438 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
2441 /* now configure the bandwidth in the manager */
2442 if (g_signal_lookup ("get-internal-session",
2443 G_OBJECT_TYPE (src->manager)) != 0) {
2444 GObject *rtpsession;
2446 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
2449 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
2451 stream->session = rtpsession;
2453 if (stream->as_bandwidth != -1) {
2454 GST_INFO_OBJECT (src, "setting AS: %f",
2455 (gdouble) (stream->as_bandwidth * 1000));
2456 g_object_set (rtpsession, "bandwidth",
2457 (gdouble) (stream->as_bandwidth * 1000), NULL);
2459 if (stream->rr_bandwidth != -1) {
2460 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
2461 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
2464 if (stream->rs_bandwidth != -1) {
2465 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
2466 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
2469 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2471 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
2473 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
2475 g_signal_connect (rtpsession, "on-ssrc-active",
2476 (GCallback) on_ssrc_active, stream);
2487 GST_DEBUG_OBJECT (src, "cannot get a session manager");
2492 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
2495 start_manager_failure:
2497 GST_DEBUG_OBJECT (src, "could not start session manager");
2502 /* free the UDP sources allocated when negotiating a transport.
2503 * This function is called when the server negotiated to a transport where the
2504 * UDP sources are not needed anymore, such as TCP or multicast. */
2506 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
2510 for (i = 0; i < 2; i++) {
2511 if (stream->udpsrc[i]) {
2512 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
2513 gst_object_unref (stream->udpsrc[i]);
2514 stream->udpsrc[i] = NULL;
2519 /* for TCP, create pads to send and receive data to and from the manager and to
2520 * intercept various events and queries
2523 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
2524 GstRTSPTransport * transport, GstPad ** outpad)
2527 GstPadTemplate *template;
2528 GstPad *pad0, *pad1;
2530 /* configure for interleaved delivery, nothing needs to be done
2531 * here, the loop function will call the chain functions of the
2532 * session manager. */
2533 stream->channel[0] = transport->interleaved.min;
2534 stream->channel[1] = transport->interleaved.max;
2535 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
2536 stream->channel[0], stream->channel[1]);
2538 /* we can remove the allocated UDP ports now */
2539 gst_rtspsrc_stream_free_udp (stream);
2541 /* no session manager, send data to srcpad directly */
2542 if (!stream->channelpad[0]) {
2543 GST_DEBUG_OBJECT (src, "no manager, creating pad");
2545 /* create a new pad we will use to stream to */
2546 name = g_strdup_printf ("stream%d", stream->id);
2547 template = gst_static_pad_template_get (&rtptemplate);
2548 stream->channelpad[0] = gst_pad_new_from_template (template, name);
2549 gst_object_unref (template);
2552 /* set caps and activate */
2553 gst_pad_use_fixed_caps (stream->channelpad[0]);
2554 gst_pad_set_active (stream->channelpad[0], TRUE);
2556 *outpad = gst_object_ref (stream->channelpad[0]);
2558 GST_DEBUG_OBJECT (src, "using manager source pad");
2560 template = gst_static_pad_template_get (&anysrctemplate);
2562 /* allocate pads for sending the channel data into the manager */
2563 pad0 = gst_pad_new_from_template (template, "internalsrc0");
2564 gst_pad_link (pad0, stream->channelpad[0]);
2565 gst_object_unref (stream->channelpad[0]);
2566 stream->channelpad[0] = pad0;
2567 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
2568 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
2569 gst_pad_set_element_private (pad0, src);
2570 gst_pad_set_active (pad0, TRUE);
2572 if (stream->channelpad[1]) {
2573 /* if we have a sinkpad for the other channel, create a pad and link to the
2575 pad1 = gst_pad_new_from_template (template, "internalsrc1");
2576 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
2577 gst_pad_link (pad1, stream->channelpad[1]);
2578 gst_object_unref (stream->channelpad[1]);
2579 stream->channelpad[1] = pad1;
2580 gst_pad_set_active (pad1, TRUE);
2582 gst_object_unref (template);
2584 /* setup RTCP transport back to the server if we have to. */
2585 if (src->manager && src->do_rtcp) {
2588 template = gst_static_pad_template_get (&anysinktemplate);
2590 stream->rtcppad = gst_pad_new_from_template (template, "internalsink0");
2591 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
2592 gst_pad_set_element_private (stream->rtcppad, stream);
2593 gst_pad_set_active (stream->rtcppad, TRUE);
2595 /* get session RTCP pad */
2596 name = g_strdup_printf ("send_rtcp_src_%d", stream->id);
2597 pad = gst_element_get_request_pad (src->manager, name);
2602 gst_pad_link (pad, stream->rtcppad);
2603 gst_object_unref (pad);
2606 gst_object_unref (template);
2612 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
2613 GstRTSPTransport * transport, const gchar ** destination, gint * min,
2614 gint * max, guint * ttl)
2616 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2618 if (!(*destination = transport->destination))
2619 *destination = stream->destination;
2622 /* transport first */
2623 *min = transport->port.min;
2624 *max = transport->port.max;
2625 if (*min == -1 && *max == -1) {
2626 /* then try from SDP */
2627 if (stream->port != 0) {
2628 *min = stream->port;
2629 *max = stream->port + 1;
2635 if (!(*ttl = transport->ttl))
2640 /* first take the source, then the endpoint to figure out where to send
2642 if (!(*destination = transport->source)) {
2643 if (src->conninfo.connection)
2644 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
2645 else if (stream->conninfo.connection)
2647 gst_rtsp_connection_get_ip (stream->conninfo.connection);
2651 /* for unicast we only expect the ports here */
2652 *min = transport->server_port.min;
2653 *max = transport->server_port.max;
2658 /* For multicast create UDP sources and join the multicast group. */
2660 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
2661 GstRTSPTransport * transport, GstPad ** outpad)
2664 const gchar *destination;
2667 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
2669 /* we can remove the allocated UDP ports now */
2670 gst_rtspsrc_stream_free_udp (stream);
2672 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
2675 /* we need a destination now */
2676 if (destination == NULL)
2677 goto no_destination;
2679 /* we really need ports now or we won't be able to receive anything at all */
2680 if (min == -1 && max == -1)
2683 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
2684 destination, min, max);
2686 /* creating UDP source for RTP */
2688 uri = g_strdup_printf ("udp://%s:%d", destination, min);
2689 stream->udpsrc[0] = gst_element_make_from_uri (GST_URI_SRC, uri, NULL);
2691 if (stream->udpsrc[0] == NULL)
2694 /* take ownership */
2695 gst_object_ref_sink (stream->udpsrc[0]);
2698 gst_element_set_state (stream->udpsrc[0], GST_STATE_PAUSED);
2701 /* creating another UDP source for RTCP */
2703 uri = g_strdup_printf ("udp://%s:%d", destination, max);
2704 stream->udpsrc[1] = gst_element_make_from_uri (GST_URI_SRC, uri, NULL);
2706 if (stream->udpsrc[1] == NULL)
2709 /* take ownership */
2710 gst_object_ref_sink (stream->udpsrc[1]);
2712 gst_element_set_state (stream->udpsrc[1], GST_STATE_PAUSED);
2719 GST_DEBUG_OBJECT (src, "no UDP source element found");
2724 GST_DEBUG_OBJECT (src, "no destination found");
2729 GST_DEBUG_OBJECT (src, "no ports found");
2734 /* configure the remainder of the UDP ports */
2736 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
2737 GstRTSPTransport * transport, GstPad ** outpad)
2739 /* we manage the UDP elements now. For unicast, the UDP sources where
2740 * allocated in the stream when we suggested a transport. */
2741 if (stream->udpsrc[0]) {
2742 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
2744 GST_DEBUG_OBJECT (src, "setting up UDP source");
2746 /* configure a timeout on the UDP port. When the timeout message is
2747 * posted, we assume UDP transport is not possible. We reconnect using TCP
2749 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", src->udp_timeout,
2752 /* get output pad of the UDP source. */
2753 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
2755 /* save it so we can unblock */
2756 stream->blockedpad = *outpad;
2758 /* configure pad block on the pad. As soon as there is dataflow on the
2759 * UDP source, we know that UDP is not blocked by a firewall and we can
2760 * configure all the streams to let the application autoplug decoders. */
2761 gst_pad_set_blocked_async (stream->blockedpad, TRUE,
2762 (GstPadBlockCallback) pad_blocked, src);
2764 if (stream->channelpad[0]) {
2765 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
2766 /* configure for UDP delivery, we need to connect the UDP pads to
2767 * the session plugin. */
2768 gst_pad_link (*outpad, stream->channelpad[0]);
2769 gst_object_unref (*outpad);
2771 /* we connected to pad-added signal to get pads from the manager */
2773 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
2778 if (stream->udpsrc[1]) {
2779 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
2781 if (stream->channelpad[1]) {
2784 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
2786 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
2787 gst_pad_link (pad, stream->channelpad[1]);
2788 gst_object_unref (pad);
2790 /* leave unlinked */
2796 /* configure the UDP sink back to the server for status reports */
2798 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
2799 GstRTSPStream * stream, GstRTSPTransport * transport)
2802 gint rtp_port, rtcp_port, sockfd = -1;
2803 gboolean do_rtp, do_rtcp;
2804 const gchar *destination;
2808 /* get transport info */
2809 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
2810 &rtp_port, &rtcp_port, &ttl);
2812 /* see what we need to do */
2813 do_rtp = (rtp_port != -1);
2814 /* it's possible that the server does not want us to send RTCP in which case
2816 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
2818 /* we need a destination when we have RTP or RTCP ports */
2819 if (destination == NULL && (do_rtp || do_rtcp))
2820 goto no_destination;
2822 /* try to construct the fakesrc to the RTP port of the server to open up any
2825 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
2828 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
2829 stream->udpsink[0] = gst_element_make_from_uri (GST_URI_SINK, uri, NULL);
2831 if (stream->udpsink[0] == NULL)
2832 goto no_sink_element;
2834 /* don't join multicast group, we will have the source socket do that */
2835 /* no sync or async state changes needed */
2836 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
2837 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
2839 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
2841 if (stream->udpsrc[0]) {
2842 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
2843 * so that NAT firewalls will open a hole for us */
2844 g_object_get (G_OBJECT (stream->udpsrc[0]), "sock", &sockfd, NULL);
2845 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %d", sockfd);
2846 /* configure socket and make sure udpsink does not close it when shutting
2847 * down, it belongs to udpsrc after all. */
2848 g_object_set (G_OBJECT (stream->udpsink[0]), "sockfd", sockfd,
2849 "closefd", FALSE, NULL);
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_IS_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]), "sock", &sockfd, NULL);
2897 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %d", sockfd);
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]), "sockfd", sockfd,
2901 "closefd", FALSE, NULL);
2904 /* we don't want to consider this a sink */
2905 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_IS_SINK);
2907 /* we keep this playing always */
2908 gst_element_set_locked_state (stream->udpsink[1], TRUE);
2909 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
2911 gst_object_ref (stream->udpsink[1]);
2912 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
2914 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
2916 /* get session RTCP pad */
2917 name = g_strdup_printf ("send_rtcp_src_%d", stream->id);
2918 pad = gst_element_get_request_pad (src->manager, name);
2923 gst_pad_link (pad, stream->rtcppad);
2924 gst_object_unref (pad);
2933 GST_DEBUG_OBJECT (src, "no destination address specified");
2938 GST_DEBUG_OBJECT (src, "no UDP sink element found");
2943 GST_DEBUG_OBJECT (src, "no fakesrc element found");
2948 /* sets up all elements needed for streaming over the specified transport.
2949 * Does not yet expose the element pads, this will be done when there is actuall
2950 * dataflow detected, which might never happen when UDP is blocked in a
2951 * firewall, for example.
2954 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
2955 GstRTSPTransport * transport)
2958 GstPad *outpad = NULL;
2959 GstPadTemplate *template;
2964 src = stream->parent;
2966 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
2968 s = gst_caps_get_structure (stream->caps, 0);
2970 /* get the proper mime type for this stream now */
2971 if (gst_rtsp_transport_get_mime (transport->trans, &mime) < 0)
2972 goto unknown_transport;
2974 goto unknown_transport;
2976 /* configure the final mime type */
2977 GST_DEBUG_OBJECT (src, "setting mime to %s", mime);
2978 gst_structure_set_name (s, mime);
2980 /* try to get and configure a manager, channelpad[0-1] will be configured with
2981 * the pads for the manager, or NULL when no manager is needed. */
2982 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
2985 switch (transport->lower_transport) {
2986 case GST_RTSP_LOWER_TRANS_TCP:
2987 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
2988 goto transport_failed;
2990 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
2991 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
2992 goto transport_failed;
2993 /* fallthrough, the rest is the same for UDP and MCAST */
2994 case GST_RTSP_LOWER_TRANS_UDP:
2995 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
2996 goto transport_failed;
2997 /* configure udpsinks back to the server for RTCP messages and for the
2998 * dummy RTP messages to open NAT. */
2999 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3000 goto transport_failed;
3003 goto unknown_transport;
3007 GST_DEBUG_OBJECT (src, "creating ghostpad");
3009 gst_pad_use_fixed_caps (outpad);
3011 /* create ghostpad, don't add just yet, this will be done when we activate
3013 name = g_strdup_printf ("stream%d", stream->id);
3014 template = gst_static_pad_template_get (&rtptemplate);
3015 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3016 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3017 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3018 gst_object_unref (template);
3021 gst_object_unref (outpad);
3023 /* mark pad as ok */
3024 stream->last_ret = GST_FLOW_OK;
3031 GST_DEBUG_OBJECT (src, "failed to configure transport");
3036 GST_DEBUG_OBJECT (src, "unknown transport");
3041 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3046 /* send a couple of dummy random packets on the receiver RTP port to the server,
3047 * this should make a firewall think we initiated the data transfer and
3048 * hopefully allow packets to go from the sender port to our RTP receiver port */
3050 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3054 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3057 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3058 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3060 if (stream->fakesrc && stream->udpsink[0]) {
3061 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3062 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3063 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3064 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3065 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3071 /* Adds the source pads of all configured streams to the element.
3072 * This code is performed when we detected dataflow.
3074 * We detect dataflow from either the _loop function or with pad probes on the
3078 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3082 GST_DEBUG_OBJECT (src, "activating streams");
3084 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3085 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3087 if (stream->udpsrc[0]) {
3088 /* remove timeout, we are streaming now and timeouts will be handled by
3089 * the session manager and jitter buffer */
3090 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3092 if (stream->srcpad) {
3093 /* if we don't have a session manager, set the caps now. If we have a
3094 * session, we will get a notification of the pad and the caps. */
3095 if (!src->manager) {
3096 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3097 gst_pad_set_caps (stream->srcpad, stream->caps);
3100 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3101 gst_pad_set_active (stream->srcpad, TRUE);
3103 if (!stream->added) {
3104 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3105 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3106 stream->added = TRUE;
3111 /* unblock all pads */
3112 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3113 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3115 if (stream->blockedpad) {
3116 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3117 gst_pad_set_blocked_async (stream->blockedpad, FALSE,
3118 (GstPadBlockCallback) pad_unblocked, src);
3119 stream->blockedpad = NULL;
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->last_stop;
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,
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->manager) {
3631 /* set stream caps on buffer when we don't have a session manager to do it
3633 gst_buffer_set_caps (buf, stream->caps);
3636 if (src->base_time == -1) {
3637 /* Take current running_time. This timestamp will be put on
3638 * the first buffer of each stream because we are a live source and so we
3639 * timestamp with the running_time. When we are dealing with TCP, we also
3640 * only timestamp the first buffer (using the DISCONT flag) because a server
3641 * typically bursts data, for which we don't want to compensate by speeding
3642 * up the media. The other timestamps will be interpollated from this one
3643 * using the RTP timestamps. */
3644 GST_OBJECT_LOCK (src);
3645 if (GST_ELEMENT_CLOCK (src)) {
3647 GstClockTime base_time;
3649 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
3650 base_time = GST_ELEMENT_CAST (src)->base_time;
3652 src->base_time = now - base_time;
3654 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
3655 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
3657 GST_OBJECT_UNLOCK (src);
3660 if (stream->discont && !is_rtcp) {
3661 /* mark first RTP buffer as discont */
3662 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
3663 stream->discont = FALSE;
3664 /* first buffer gets the timestamp, other buffers are not timestamped and
3665 * their presentation time will be interpollated from the rtp timestamps. */
3666 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
3667 GST_TIME_ARGS (src->base_time));
3669 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
3672 /* chain to the peer pad */
3673 if (GST_PAD_IS_SINK (outpad))
3674 ret = gst_pad_chain (outpad, buf);
3676 ret = gst_pad_push (outpad, buf);
3679 /* combine all stream flows for the data transport */
3680 ret = gst_rtspsrc_combine_flows (src, stream, ret);
3687 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
3688 gst_rtsp_message_unset (&message);
3693 GST_DEBUG_OBJECT (src, "we got an eof from the server");
3694 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3695 ("The server closed the connection."));
3696 src->conninfo.connected = FALSE;
3697 gst_rtsp_message_unset (&message);
3698 return GST_FLOW_UNEXPECTED;
3702 gst_rtsp_message_unset (&message);
3703 GST_DEBUG_OBJECT (src, "got interrupted: stop connection flush");
3704 gst_rtspsrc_connection_flush (src, FALSE);
3705 return GST_FLOW_WRONG_STATE;
3709 gchar *str = gst_rtsp_strresult (res);
3711 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
3712 ("Could not receive message. (%s)", str));
3715 gst_rtsp_message_unset (&message);
3716 return GST_FLOW_ERROR;
3718 handle_request_failed:
3720 gchar *str = gst_rtsp_strresult (res);
3722 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
3723 ("Could not handle server message. (%s)", str));
3725 gst_rtsp_message_unset (&message);
3726 return GST_FLOW_ERROR;
3730 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3731 ("Short message received, ignoring."));
3732 gst_rtsp_message_unset (&message);
3737 static GstFlowReturn
3738 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
3741 GstRTSPMessage message = { 0 };
3745 GTimeVal tv_timeout;
3747 /* get the next timeout interval */
3748 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3750 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
3751 (gint) tv_timeout.tv_sec);
3753 gst_rtsp_message_unset (&message);
3755 /* we should continue reading the TCP socket because the server might
3756 * send us requests. When the session timeout expires, we need to send a
3757 * keep-alive request to keep the session open. */
3758 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
3759 &message, &tv_timeout);
3763 GST_DEBUG_OBJECT (src, "we received a server message");
3765 case GST_RTSP_EINTR:
3766 /* we got interrupted, see what we have to do */
3768 case GST_RTSP_ETIMEOUT:
3769 /* send keep-alive, ignore the result, a warning will be posted. */
3770 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
3771 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3775 /* server closed the connection. not very fatal for UDP, reconnect and
3776 * see what happens. */
3777 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3778 ("The server closed the connection."));
3780 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
3788 switch (message.type) {
3789 case GST_RTSP_MESSAGE_REQUEST:
3790 /* server sends us a request message, handle it */
3792 gst_rtspsrc_handle_request (src, src->conninfo.connection,
3794 if (res == GST_RTSP_EEOF)
3797 goto handle_request_failed;
3799 case GST_RTSP_MESSAGE_RESPONSE:
3800 /* we ignore response and data messages */
3801 GST_DEBUG_OBJECT (src, "ignoring response message");
3803 gst_rtsp_message_dump (&message);
3804 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
3805 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
3806 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
3807 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
3808 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3815 case GST_RTSP_MESSAGE_DATA:
3816 /* we ignore response and data messages */
3817 GST_DEBUG_OBJECT (src, "ignoring data message");
3820 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
3826 /* we get here when the connection got interrupted */
3829 gst_rtsp_message_unset (&message);
3830 GST_DEBUG_OBJECT (src, "got interrupted: stop connection flush");
3831 gst_rtspsrc_connection_flush (src, FALSE);
3832 return GST_FLOW_WRONG_STATE;
3836 gchar *str = gst_rtsp_strresult (res);
3839 src->conninfo.connected = FALSE;
3840 if (res != GST_RTSP_EINTR) {
3841 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
3842 ("Could not connect to server. (%s)", str));
3844 ret = GST_FLOW_ERROR;
3846 ret = GST_FLOW_WRONG_STATE;
3852 gchar *str = gst_rtsp_strresult (res);
3854 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
3855 ("Could not receive message. (%s)", str));
3857 return GST_FLOW_ERROR;
3859 handle_request_failed:
3861 gchar *str = gst_rtsp_strresult (res);
3864 gst_rtsp_message_unset (&message);
3865 if (res != GST_RTSP_EINTR) {
3866 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
3867 ("Could not handle server message. (%s)", str));
3869 ret = GST_FLOW_ERROR;
3871 ret = GST_FLOW_WRONG_STATE;
3877 GST_DEBUG_OBJECT (src, "we got an eof from the server");
3878 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3879 ("The server closed the connection."));
3880 src->conninfo.connected = FALSE;
3881 gst_rtsp_message_unset (&message);
3882 return GST_FLOW_UNEXPECTED;
3886 static GstRTSPResult
3887 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
3889 GstRTSPResult res = GST_RTSP_OK;
3892 GST_DEBUG_OBJECT (src, "doing reconnect");
3894 GST_OBJECT_LOCK (src);
3895 /* only restart when the pads were not yet activated, else we were
3896 * streaming over UDP */
3897 restart = src->need_activate;
3898 GST_OBJECT_UNLOCK (src);
3900 /* no need to restart, we're done */
3904 /* we can try only TCP now */
3905 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
3907 /* close and cleanup our state */
3908 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
3911 /* see if we have TCP left to try. Also don't try TCP when we were configured
3913 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
3916 /* We post a warning message now to inform the user
3917 * that nothing happened. It's most likely a firewall thing. */
3918 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3919 ("Could not receive any UDP packets for %.4f seconds, maybe your "
3920 "firewall is blocking it. Retrying using a TCP connection.",
3921 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
3923 /* open new connection using tcp */
3924 if (gst_rtspsrc_open (src, async) < 0)
3927 /* start playback */
3928 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
3937 src->cur_protocols = 0;
3938 /* no transport possible, post an error and stop */
3939 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
3940 ("Could not receive any UDP packets for %.4f seconds, maybe your "
3941 "firewall is blocking it. No other protocols to try.",
3942 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
3943 return GST_FLOW_ERROR;
3947 GST_DEBUG_OBJECT (src, "open failed");
3952 GST_DEBUG_OBJECT (src, "play failed");
3958 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
3962 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
3965 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
3968 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
3971 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
3979 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
3983 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
3986 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
3989 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
3992 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4000 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4004 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4007 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4010 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4013 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4021 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4025 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
4028 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4031 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4034 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4042 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4044 if (ret == GST_RTSP_OK)
4045 gst_rtspsrc_loop_complete_cmd (src, cmd);
4046 else if (ret == GST_RTSP_EINTR)
4047 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4049 gst_rtspsrc_loop_error_cmd (src, cmd);
4053 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gboolean flush)
4057 /* FIXME flush param mute; remove at discretion */
4059 /* start new request */
4060 gst_rtspsrc_loop_start_cmd (src, cmd);
4062 GST_OBJECT_LOCK (src);
4063 old = src->loop_cmd;
4064 if (old != CMD_WAIT) {
4065 src->loop_cmd = CMD_WAIT;
4066 GST_OBJECT_UNLOCK (src);
4067 /* cancel previous request */
4068 gst_rtspsrc_loop_cancel_cmd (src, old);
4069 GST_OBJECT_LOCK (src);
4071 src->loop_cmd = cmd;
4072 /* interrupt if allowed */
4074 GST_DEBUG_OBJECT (src, "start connection flush");
4075 gst_rtspsrc_connection_flush (src, TRUE);
4078 gst_task_start (src->task);
4079 GST_OBJECT_UNLOCK (src);
4083 gst_rtspsrc_loop (GstRTSPSrc * src)
4087 if (!src->conninfo.connection || !src->conninfo.connected)
4090 if (src->interleaved)
4091 ret = gst_rtspsrc_loop_interleaved (src);
4093 ret = gst_rtspsrc_loop_udp (src);
4095 if (ret != GST_FLOW_OK)
4103 GST_WARNING_OBJECT (src, "we are not connected");
4104 ret = GST_FLOW_WRONG_STATE;
4109 const gchar *reason = gst_flow_get_name (ret);
4111 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
4112 src->running = FALSE;
4113 if (ret == GST_FLOW_UNEXPECTED) {
4114 /* perform EOS logic */
4115 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4116 gst_element_post_message (GST_ELEMENT_CAST (src),
4117 gst_message_new_segment_done (GST_OBJECT_CAST (src),
4118 src->segment.format, src->segment.last_stop));
4120 gst_rtspsrc_push_event (src, gst_event_new_eos (), FALSE);
4122 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
4123 /* for fatal errors we post an error message, post the error before the
4124 * EOS so the app knows about the error first. */
4125 GST_ELEMENT_ERROR (src, STREAM, FAILED,
4126 ("Internal data flow error."),
4127 ("streaming task paused, reason %s (%d)", reason, ret));
4128 gst_rtspsrc_push_event (src, gst_event_new_eos (), FALSE);
4134 #ifndef GST_DISABLE_GST_DEBUG
4135 static const gchar *
4136 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
4140 while (method != 0) {
4157 static const gchar *
4158 gst_rtspsrc_skip_lws (const gchar * s)
4160 while (g_ascii_isspace (*s))
4165 static const gchar *
4166 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
4168 while (s > start && g_ascii_isspace (*(s - 1)))
4173 static const gchar *
4174 gst_rtspsrc_skip_commas (const gchar * s)
4176 /* The grammar allows for multiple commas */
4177 while (g_ascii_isspace (*s) || *s == ',')
4182 static const gchar *
4183 gst_rtspsrc_skip_item (const gchar * s)
4185 gboolean quoted = FALSE;
4186 const gchar *start = s;
4188 /* A list item ends at the last non-whitespace character
4189 * before a comma which is not inside a quoted-string. Or at
4190 * the end of the string.
4196 if (*s == '\\' && *(s + 1))
4205 return gst_rtspsrc_unskip_lws (s, start);
4209 gst_rtsp_decode_quoted_string (gchar * quoted_string)
4213 src = quoted_string + 1;
4214 dst = quoted_string;
4215 while (*src && *src != '"') {
4216 if (*src == '\\' && *(src + 1))
4223 /* Extract the authentication tokens that the server provided for each method
4224 * into an array of structures and give those to the connection object.
4227 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
4228 const gchar * header, gboolean * stale)
4230 GSList *list = NULL, *iter;
4232 gchar *item, *eq, *name_end, *value;
4234 g_return_if_fail (stale != NULL);
4236 gst_rtsp_connection_clear_auth_params (conn);
4239 /* Parse a header whose content is described by RFC2616 as
4240 * "#something", where "something" does not itself contain commas,
4241 * except as part of quoted-strings, into a list of allocated strings.
4243 header = gst_rtspsrc_skip_commas (header);
4245 end = gst_rtspsrc_skip_item (header);
4246 list = g_slist_prepend (list, g_strndup (header, end - header));
4247 header = gst_rtspsrc_skip_commas (end);
4252 list = g_slist_reverse (list);
4253 for (iter = list; iter; iter = iter->next) {
4256 eq = strchr (item, '=');
4258 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
4259 if (name_end == item) {
4260 /* That's no good... */
4267 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
4269 gst_rtsp_decode_quoted_string (value);
4273 if ((strcmp (item, "stale") == 0) && (strcmp (value, "TRUE") == 0))
4275 gst_rtsp_connection_set_auth_param (conn, item, value);
4279 g_slist_free (list);
4282 /* Parse a WWW-Authenticate Response header and determine the
4283 * available authentication methods
4285 * This code should also cope with the fact that each WWW-Authenticate
4286 * header can contain multiple challenge methods + tokens
4288 * At the moment, for Basic auth, we just do a minimal check and don't
4289 * even parse out the realm */
4291 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
4292 GstRTSPConnection * conn, gboolean * stale)
4296 g_return_if_fail (hdr != NULL);
4297 g_return_if_fail (methods != NULL);
4298 g_return_if_fail (stale != NULL);
4300 /* Skip whitespace at the start of the string */
4301 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
4303 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
4304 *methods |= GST_RTSP_AUTH_BASIC;
4305 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
4306 *methods |= GST_RTSP_AUTH_DIGEST;
4307 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
4312 * gst_rtspsrc_setup_auth:
4313 * @src: the rtsp source
4315 * Configure a username and password and auth method on the
4316 * connection object based on a response we received from the
4319 * Currently, this requires that a username and password were supplied
4320 * in the uri. In the future, they may be requested on demand by sending
4321 * a message up the bus.
4323 * Returns: TRUE if authentication information could be set up correctly.
4326 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
4330 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
4331 GstRTSPAuthMethod method;
4332 GstRTSPResult auth_result;
4334 GstRTSPConnection *conn;
4336 gboolean stale = FALSE;
4338 conn = src->conninfo.connection;
4340 /* Identify the available auth methods and see if any are supported */
4341 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
4342 &hdr, 0) == GST_RTSP_OK) {
4343 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
4346 if (avail_methods == GST_RTSP_AUTH_NONE)
4347 goto no_auth_available;
4349 /* For digest auth, if the response indicates that the session
4350 * data are stale, we just update them in the connection object and
4351 * return TRUE to retry the request */
4353 src->tried_url_auth = FALSE;
4355 url = gst_rtsp_connection_get_url (conn);
4357 /* Do we have username and password available? */
4358 if (url != NULL && !src->tried_url_auth && url->user != NULL
4359 && url->passwd != NULL) {
4362 src->tried_url_auth = TRUE;
4363 GST_DEBUG_OBJECT (src,
4364 "Attempting authentication using credentials from the URL");
4366 user = src->user_id;
4367 pass = src->user_pw;
4368 GST_DEBUG_OBJECT (src,
4369 "Attempting authentication using credentials from the properties");
4372 /* FIXME: If the url didn't contain username and password or we tried them
4373 * already, request a username and passwd from the application via some kind
4374 * of credentials request message */
4376 /* If we don't have a username and passwd at this point, bail out. */
4377 if (user == NULL || pass == NULL)
4380 /* Try to configure for each available authentication method, strongest to
4382 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
4383 /* Check if this method is available on the server */
4384 if ((method & avail_methods) == 0)
4387 /* Pass the credentials to the connection to try on the next request */
4388 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
4389 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
4390 * ignore it and end up retrying later */
4391 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
4392 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
4393 gst_rtsp_auth_method_to_string (method));
4398 if (method == GST_RTSP_AUTH_NONE)
4399 goto no_auth_available;
4405 /* Output an error indicating that we couldn't connect because there were
4406 * no supported authentication protocols */
4407 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4408 ("No supported authentication protocol was found"));
4413 /* We don't fire an error message, we just return FALSE and let the
4414 * normal NOT_AUTHORIZED error be propagated */
4419 static GstRTSPResult
4420 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4421 GstRTSPMessage * request, GstRTSPMessage * response,
4422 GstRTSPStatusCode * code)
4425 GstRTSPStatusCode thecode;
4426 gchar *content_base = NULL;
4430 gst_rtsp_ext_list_before_send (src->extensions, request);
4432 GST_DEBUG_OBJECT (src, "sending message");
4435 gst_rtsp_message_dump (request);
4437 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
4441 gst_rtsp_connection_reset_timeout (conn);
4444 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
4449 gst_rtsp_message_dump (response);
4451 switch (response->type) {
4452 case GST_RTSP_MESSAGE_REQUEST:
4453 res = gst_rtspsrc_handle_request (src, conn, response);
4454 if (res == GST_RTSP_EEOF)
4457 goto handle_request_failed;
4459 case GST_RTSP_MESSAGE_RESPONSE:
4460 /* ok, a response is good */
4461 GST_DEBUG_OBJECT (src, "received response message");
4464 case GST_RTSP_MESSAGE_DATA:
4465 /* get next response */
4466 GST_DEBUG_OBJECT (src, "ignoring data response message");
4470 thecode = response->type_data.response.code;
4472 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
4474 /* if the caller wanted the result code, we store it. */
4478 /* If the request didn't succeed, bail out before doing any more */
4479 if (thecode != GST_RTSP_STS_OK)
4482 /* store new content base if any */
4483 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
4486 g_free (src->content_base);
4487 src->content_base = g_strdup (content_base);
4489 gst_rtsp_ext_list_after_send (src->extensions, request, response);
4496 gchar *str = gst_rtsp_strresult (res);
4498 if (res != GST_RTSP_EINTR) {
4499 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4500 ("Could not send message. (%s)", str));
4502 GST_WARNING_OBJECT (src, "send interrupted");
4511 GST_WARNING_OBJECT (src, "server closed connection, doing reconnect");
4514 /* if reconnect succeeds, try again */
4516 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
4520 /* only try once after reconnect, then fallthrough and error out */
4523 gchar *str = gst_rtsp_strresult (res);
4525 if (res != GST_RTSP_EINTR) {
4526 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4527 ("Could not receive message. (%s)", str));
4529 GST_WARNING_OBJECT (src, "receive interrupted");
4537 handle_request_failed:
4539 /* ERROR was posted */
4540 gst_rtsp_message_unset (response);
4545 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4546 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4547 ("The server closed the connection."));
4548 gst_rtsp_message_unset (response);
4555 * @src: the rtsp source
4556 * @conn: the connection to send on
4557 * @request: must point to a valid request
4558 * @response: must point to an empty #GstRTSPMessage
4559 * @code: an optional code result
4561 * send @request and retrieve the response in @response. optionally @code can be
4562 * non-NULL in which case it will contain the status code of the response.
4564 * If This function returns #GST_RTSP_OK, @response will contain a valid response
4565 * message that should be cleaned with gst_rtsp_message_unset() after usage.
4567 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
4568 * @response message) if the response code was not 200 (OK).
4570 * If the attempt results in an authentication failure, then this will attempt
4571 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
4574 * Returns: #GST_RTSP_OK if the processing was successful.
4576 static GstRTSPResult
4577 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4578 GstRTSPMessage * request, GstRTSPMessage * response,
4579 GstRTSPStatusCode * code)
4581 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
4582 GstRTSPResult res = GST_RTSP_ERROR;
4585 GstRTSPMethod method = GST_RTSP_INVALID;
4591 /* make sure we don't loop forever */
4595 /* save method so we can disable it when the server complains */
4596 method = request->type_data.request.method;
4599 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
4603 case GST_RTSP_STS_UNAUTHORIZED:
4604 if (gst_rtspsrc_setup_auth (src, response)) {
4605 /* Try the request/response again after configuring the auth info
4613 } while (retry == TRUE);
4615 /* If the user requested the code, let them handle errors, otherwise
4616 * post an error below */
4619 else if (int_code != GST_RTSP_STS_OK)
4620 goto error_response;
4627 GST_DEBUG_OBJECT (src, "got error %d", res);
4632 res = GST_RTSP_ERROR;
4634 switch (response->type_data.response.code) {
4635 case GST_RTSP_STS_NOT_FOUND:
4636 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
4637 response->type_data.response.reason));
4639 case GST_RTSP_STS_MOVED_PERMANENTLY:
4640 case GST_RTSP_STS_MOVE_TEMPORARILY:
4642 gchar *new_location;
4643 GstRTSPLowerTrans transports;
4645 GST_DEBUG_OBJECT (src, "got redirection");
4646 /* if we don't have a Location Header, we must error */
4647 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
4648 &new_location, 0) < 0)
4651 /* When we receive a redirect result, we go back to the INIT state after
4652 * parsing the new URI. The caller should do the needed steps to issue
4653 * a new setup when it detects this state change. */
4654 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
4656 /* save current transports */
4657 if (src->conninfo.url)
4658 transports = src->conninfo.url->transports;
4660 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
4662 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location);
4664 /* set old transports */
4665 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
4666 src->conninfo.url->transports = transports;
4668 src->need_redirect = TRUE;
4669 src->state = GST_RTSP_STATE_INIT;
4673 case GST_RTSP_STS_NOT_ACCEPTABLE:
4674 case GST_RTSP_STS_NOT_IMPLEMENTED:
4675 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
4676 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
4677 gst_rtsp_method_as_text (method));
4678 src->methods &= ~method;
4682 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4683 ("Got error response: %d (%s).", response->type_data.response.code,
4684 response->type_data.response.reason));
4687 /* if we return ERROR we should unset the response ourselves */
4688 if (res == GST_RTSP_ERROR)
4689 gst_rtsp_message_unset (response);
4695 static GstRTSPResult
4696 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
4697 GstRTSPMessage * response, GstRTSPSrc * src)
4699 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
4704 /* parse the response and collect all the supported methods. We need this
4705 * information so that we don't try to send an unsupported request to the
4709 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
4711 GstRTSPHeaderField field;
4717 /* reset supported methods */
4720 /* Try Allow Header first */
4721 field = GST_RTSP_HDR_ALLOW;
4724 gst_rtsp_message_get_header (response, field, &respoptions, indx);
4725 if (indx == 0 && !respoptions) {
4726 /* if no Allow header was found then try the Public header... */
4727 field = GST_RTSP_HDR_PUBLIC;
4728 gst_rtsp_message_get_header (response, field, &respoptions, indx);
4733 /* If we get here, the server gave a list of supported methods, parse
4734 * them here. The string is like:
4736 * OPTIONS, DESCRIBE, ANNOUNCE, PLAY, SETUP, ...
4738 options = g_strsplit (respoptions, ",", 0);
4740 for (i = 0; options[i]; i++) {
4744 stripped = g_strstrip (options[i]);
4745 method = gst_rtsp_find_method (stripped);
4747 /* keep bitfield of supported methods */
4748 if (method != GST_RTSP_INVALID)
4749 src->methods |= method;
4751 g_strfreev (options);
4756 if (src->methods == 0) {
4757 /* neither Allow nor Public are required, assume the server supports
4758 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
4760 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
4761 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
4763 /* always assume PLAY, FIXME, extensions should be able to override
4765 src->methods |= GST_RTSP_PLAY;
4766 /* also assume it will support Range */
4767 src->seekable = TRUE;
4769 /* we need describe and setup */
4770 if (!(src->methods & GST_RTSP_DESCRIBE))
4772 if (!(src->methods & GST_RTSP_SETUP))
4780 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4781 ("Server does not support DESCRIBE."));
4786 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4787 ("Server does not support SETUP."));
4792 /* masks to be kept in sync with the hardcoded protocol order of preference
4794 static guint protocol_masks[] = {
4795 GST_RTSP_LOWER_TRANS_UDP,
4796 GST_RTSP_LOWER_TRANS_UDP_MCAST,
4797 GST_RTSP_LOWER_TRANS_TCP,
4801 static GstRTSPResult
4802 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
4803 GstRTSPLowerTrans protocols, gchar ** transports)
4807 gboolean add_udp_str;
4812 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
4817 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
4819 /* extension listed transports, use those */
4820 if (*transports != NULL)
4823 /* it's the default */
4824 add_udp_str = FALSE;
4826 /* the default RTSP transports */
4827 result = g_string_new ("");
4828 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
4829 GST_DEBUG_OBJECT (src, "adding UDP unicast");
4831 g_string_append (result, "RTP/AVP");
4833 g_string_append (result, "/UDP");
4834 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
4835 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
4836 GST_DEBUG_OBJECT (src, "adding UDP multicast");
4838 /* we don't have to allocate any UDP ports yet, if the selected transport
4839 * turns out to be multicast we can create them and join the multicast
4840 * group indicated in the transport reply */
4841 if (result->len > 0)
4842 g_string_append (result, ",");
4843 g_string_append (result, "RTP/AVP");
4845 g_string_append (result, "/UDP");
4846 g_string_append (result, ";multicast");
4847 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
4848 GST_DEBUG_OBJECT (src, "adding TCP");
4850 if (result->len > 0)
4851 g_string_append (result, ",");
4852 g_string_append (result, "RTP/AVP/TCP;unicast;interleaved=%%i1-%%i2");
4854 *transports = g_string_free (result, FALSE);
4856 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
4867 static GstRTSPResult
4868 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
4869 gint orig_rtpport, gint orig_rtcpport)
4872 gint nr_udp, nr_int;
4874 gint rtpport = 0, rtcpport = 0;
4877 src = stream->parent;
4879 /* find number of placeholders first */
4880 if (strstr (*transports, "%%i2"))
4882 else if (strstr (*transports, "%%i1"))
4887 if (strstr (*transports, "%%u2"))
4889 else if (strstr (*transports, "%%u1"))
4894 if (nr_udp == 0 && nr_int == 0)
4898 if (!orig_rtpport || !orig_rtcpport) {
4899 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
4902 rtpport = orig_rtpport;
4903 rtcpport = orig_rtcpport;
4907 str = g_string_new ("");
4909 while ((next = strstr (p, "%%"))) {
4910 g_string_append_len (str, p, next - p);
4911 if (next[2] == 'u') {
4913 g_string_append_printf (str, "%d", rtpport);
4914 else if (next[3] == '2')
4915 g_string_append_printf (str, "%d", rtcpport);
4917 if (next[2] == 'i') {
4919 g_string_append_printf (str, "%d", src->free_channel);
4920 else if (next[3] == '2')
4921 g_string_append_printf (str, "%d", src->free_channel + 1);
4926 /* append final part */
4927 g_string_append (str, p);
4929 g_free (*transports);
4930 *transports = g_string_free (str, FALSE);
4938 return GST_RTSP_ERROR;
4943 gst_rtspsrc_stream_is_real_media (GstRTSPStream * stream)
4945 gboolean res = FALSE;
4949 const gchar *enc = NULL;
4951 s = gst_caps_get_structure (stream->caps, 0);
4952 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
4953 res = (strstr (enc, "-REAL") != NULL);
4959 /* Perform the SETUP request for all the streams.
4961 * We ask the server for a specific transport, which initially includes all the
4962 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
4963 * two local UDP ports that we send to the server.
4965 * Once the server replied with a transport, we configure the other streams
4966 * with the same transport.
4968 * This function will also configure the stream for the selected transport,
4969 * which basically means creating the pipeline.
4971 static GstRTSPResult
4972 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
4976 GstRTSPMessage request = { 0 };
4977 GstRTSPMessage response = { 0 };
4978 GstRTSPStream *stream = NULL;
4979 GstRTSPLowerTrans protocols;
4980 GstRTSPStatusCode code;
4981 gboolean unsupported_real = FALSE;
4982 gint rtpport, rtcpport;
4986 if (src->conninfo.connection) {
4987 url = gst_rtsp_connection_get_url (src->conninfo.connection);
4988 /* we initially allow all configured lower transports. based on the URL
4989 * transports and the replies from the server we narrow them down. */
4990 protocols = url->transports & src->cur_protocols;
4993 protocols = src->cur_protocols;
4999 /* reset some state */
5000 src->free_channel = 0;
5001 src->interleaved = FALSE;
5002 src->need_activate = FALSE;
5003 /* keep track of next port number, 0 is random */
5004 src->next_port_num = src->client_port_range.min;
5005 rtpport = rtcpport = 0;
5007 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5008 GstRTSPConnection *conn;
5013 stream = (GstRTSPStream *) walk->data;
5015 /* see if we need to configure this stream */
5016 if (!gst_rtsp_ext_list_configure_stream (src->extensions, stream->caps)) {
5017 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
5019 stream->disabled = TRUE;
5023 /* merge/overwrite global caps */
5028 s = gst_caps_get_structure (stream->caps, 0);
5030 num = gst_structure_n_fields (src->props);
5031 for (j = 0; j < num; j++) {
5035 name = gst_structure_nth_field_name (src->props, j);
5036 val = gst_structure_get_value (src->props, name);
5037 gst_structure_set_value (s, name, val);
5039 GST_DEBUG_OBJECT (src, "copied %s", name);
5043 /* skip setup if we have no URL for it */
5044 if (stream->conninfo.location == NULL) {
5045 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
5049 if (src->conninfo.connection == NULL) {
5050 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
5051 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
5054 conn = stream->conninfo.connection;
5056 conn = src->conninfo.connection;
5058 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
5059 stream->conninfo.location);
5061 /* if we have a multicast connection, only suggest multicast from now on */
5062 if (stream->is_multicast)
5063 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
5066 /* first selectable protocol */
5067 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5069 if (!protocol_masks[mask])
5073 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
5074 protocol_masks[mask]);
5075 /* create a string with first transport in line */
5077 res = gst_rtspsrc_create_transports_string (src,
5078 protocols & protocol_masks[mask], &transports);
5079 if (res < 0 || transports == NULL)
5080 goto setup_transport_failed;
5082 if (strlen (transports) == 0) {
5083 g_free (transports);
5084 GST_DEBUG_OBJECT (src, "no transports found");
5089 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
5091 /* replace placeholders with real values, this function will optionally
5092 * allocate UDP ports and other info needed to execute the setup request */
5093 res = gst_rtspsrc_prepare_transports (stream, &transports,
5094 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
5096 g_free (transports);
5097 goto setup_transport_failed;
5100 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
5102 /* create SETUP request */
5104 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
5105 stream->conninfo.location);
5107 g_free (transports);
5108 goto create_request_failed;
5111 /* select transport, copy is made when adding to header so we can free it. */
5112 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
5113 g_free (transports);
5115 /* if the user wants a non default RTP packet size we add the blocksize
5117 if (src->rtp_blocksize > 0) {
5118 hval = g_strdup_printf ("%d", src->rtp_blocksize);
5119 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
5124 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
5127 /* handle the code ourselves */
5128 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
5132 case GST_RTSP_STS_OK:
5134 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
5135 gst_rtsp_message_unset (&request);
5136 gst_rtsp_message_unset (&response);
5137 /* cleanup of leftover transport */
5138 gst_rtspsrc_stream_free_udp (stream);
5139 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
5140 * we might be in this case */
5141 if (stream->container && rtpport && rtcpport && !retry) {
5142 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
5147 /* this transport did not go down well, but we may have others to try
5148 * that we did not send yet, try those and only give up then
5149 * but not without checking for lost cause/extension so we can
5150 * post a nicer/more useful error message later */
5151 if (!unsupported_real)
5152 unsupported_real = gst_rtspsrc_stream_is_real_media (stream);
5153 /* select next available protocol, give up on this stream if none */
5155 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5157 if (!protocol_masks[mask] || unsupported_real)
5162 /* cleanup of leftover transport and move to the next stream */
5163 gst_rtspsrc_stream_free_udp (stream);
5164 goto response_error;
5167 /* parse response transport */
5169 gchar *resptrans = NULL;
5170 GstRTSPTransport transport = { 0 };
5172 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
5175 gst_rtspsrc_stream_free_udp (stream);
5179 /* parse transport, go to next stream on parse error */
5180 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
5181 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
5185 /* update allowed transports for other streams. once the transport of
5186 * one stream has been determined, we make sure that all other streams
5187 * are configured in the same way */
5188 switch (transport.lower_transport) {
5189 case GST_RTSP_LOWER_TRANS_TCP:
5190 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
5191 protocols = GST_RTSP_LOWER_TRANS_TCP;
5192 src->interleaved = TRUE;
5193 /* update free channels */
5195 MAX (transport.interleaved.min, src->free_channel);
5197 MAX (transport.interleaved.max, src->free_channel);
5198 src->free_channel++;
5200 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
5201 /* only allow multicast for other streams */
5202 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
5203 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
5205 case GST_RTSP_LOWER_TRANS_UDP:
5206 /* only allow unicast for other streams */
5207 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
5208 protocols = GST_RTSP_LOWER_TRANS_UDP;
5211 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
5212 transport.lower_transport);
5216 if (!stream->container || (!src->interleaved && !retry)) {
5217 /* now configure the stream with the selected transport */
5218 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
5219 GST_DEBUG_OBJECT (src,
5220 "could not configure stream %p transport, skipping stream",
5223 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
5224 /* retain the first allocated UDP port pair */
5225 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
5226 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
5229 /* we need to activate at least one streams when we detect activity */
5230 src->need_activate = TRUE;
5232 /* clean up our transport struct */
5233 gst_rtsp_transport_init (&transport);
5234 /* clean up used RTSP messages */
5235 gst_rtsp_message_unset (&request);
5236 gst_rtsp_message_unset (&response);
5240 /* store the transport protocol that was configured */
5241 src->cur_protocols = protocols;
5243 gst_rtsp_ext_list_stream_select (src->extensions, url);
5245 /* if there is nothing to activate, error out */
5246 if (!src->need_activate)
5247 goto nothing_to_activate;
5254 /* no transport possible, post an error and stop */
5255 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5256 ("Could not connect to server, no protocols left"));
5257 return GST_RTSP_ERROR;
5259 create_request_failed:
5261 gchar *str = gst_rtsp_strresult (res);
5263 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5264 ("Could not create request. (%s)", str));
5268 setup_transport_failed:
5270 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5271 ("Could not setup transport."));
5272 res = GST_RTSP_ERROR;
5277 const gchar *str = gst_rtsp_status_as_text (code);
5279 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5280 ("Error (%d): %s", code, GST_STR_NULL (str)));
5281 res = GST_RTSP_ERROR;
5286 gchar *str = gst_rtsp_strresult (res);
5288 if (res != GST_RTSP_EINTR) {
5289 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5290 ("Could not send message. (%s)", str));
5292 GST_WARNING_OBJECT (src, "send interrupted");
5299 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5300 ("Server did not select transport."));
5301 res = GST_RTSP_ERROR;
5304 nothing_to_activate:
5306 /* none of the available error codes is really right .. */
5307 if (unsupported_real) {
5308 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5309 (_("No supported stream was found. You might need to install a "
5310 "GStreamer RTSP extension plugin for Real media streams.")),
5313 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5314 (_("No supported stream was found. You might need to allow "
5315 "more transport protocols or may otherwise be missing "
5316 "the right GStreamer RTSP extension plugin.")), (NULL));
5318 return GST_RTSP_ERROR;
5322 gst_rtsp_message_unset (&request);
5323 gst_rtsp_message_unset (&response);
5329 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
5330 GstSegment * segment)
5333 GstRTSPTimeRange *therange;
5336 gst_rtsp_range_free (src->range);
5338 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
5339 GST_DEBUG_OBJECT (src, "parsed range %s", range);
5340 src->range = therange;
5342 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
5344 gst_segment_init (segment, GST_FORMAT_TIME);
5348 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
5349 therange->min.type, therange->min.seconds, therange->max.type,
5350 therange->max.seconds);
5352 if (therange->min.type == GST_RTSP_TIME_NOW)
5354 else if (therange->min.type == GST_RTSP_TIME_END)
5357 seconds = therange->min.seconds * GST_SECOND;
5359 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
5360 GST_TIME_ARGS (seconds));
5362 /* we need to start playback without clipping from the position reported by
5364 segment->start = seconds;
5365 segment->last_stop = seconds;
5367 if (therange->max.type == GST_RTSP_TIME_NOW)
5369 else if (therange->max.type == GST_RTSP_TIME_END)
5372 seconds = therange->max.seconds * GST_SECOND;
5374 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
5375 GST_TIME_ARGS (seconds));
5377 /* live (WMS) server might send overflowed large max as its idea of infinity,
5378 * compensate to prevent problems later on */
5379 if (seconds != -1 && seconds < 0) {
5381 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
5384 /* live (WMS) might send min == max, which is not worth recording */
5385 if (segment->duration == -1 && seconds == segment->start)
5388 /* don't change duration with unknown value, we might have a valid value
5389 * there that we want to keep. */
5391 gst_segment_set_duration (segment, GST_FORMAT_TIME, seconds);
5396 /* must be called with the RTSP state lock */
5397 static GstRTSPResult
5398 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
5404 /* prepare global stream caps properties */
5406 gst_structure_remove_all_fields (src->props);
5408 src->props = gst_structure_empty_new ("RTSPProperties");
5411 gst_sdp_message_dump (sdp);
5413 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
5415 gst_segment_init (&src->segment, GST_FORMAT_TIME);
5417 /* parse range for duration reporting. */
5422 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
5426 /* keep track of the range and configure it in the segment */
5427 if (gst_rtspsrc_parse_range (src, range, &src->segment))
5431 /* try to find a global control attribute. Note that a '*' means that we should
5432 * do aggregate control with the current url (so we don't do anything and
5433 * leave the current connection as is) */
5435 const gchar *control;
5438 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
5439 if (control == NULL)
5442 /* only take fully qualified urls */
5443 if (g_str_has_prefix (control, "rtsp://"))
5447 g_free (src->conninfo.location);
5448 src->conninfo.location = g_strdup (control);
5449 /* make a connection for this, if there was a connection already, nothing
5451 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
5452 GST_ERROR_OBJECT (src, "could not connect");
5455 /* we need to keep the control url separate from the connection url because
5456 * the rules for constructing the media control url need it */
5457 g_free (src->control);
5458 src->control = g_strdup (control);
5461 /* create streams */
5462 n_streams = gst_sdp_message_medias_len (sdp);
5463 for (i = 0; i < n_streams; i++) {
5464 gst_rtspsrc_create_stream (src, sdp, i);
5467 src->state = GST_RTSP_STATE_INIT;
5468 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_IS_SOURCE);
5471 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
5474 /* reset our state */
5475 src->need_range = TRUE;
5478 src->state = GST_RTSP_STATE_READY;
5485 GST_ERROR_OBJECT (src, "setup failed");
5490 static GstRTSPResult
5491 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
5495 GstRTSPMessage request = { 0 };
5496 GstRTSPMessage response = { 0 };
5499 gchar *respcont = NULL;
5502 src->need_redirect = FALSE;
5504 /* can't continue without a valid url */
5505 if (G_UNLIKELY (src->conninfo.url == NULL)) {
5506 res = GST_RTSP_EINVAL;
5509 src->tried_url_auth = FALSE;
5511 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
5512 goto connect_failed;
5514 /* create OPTIONS */
5515 GST_DEBUG_OBJECT (src, "create options...");
5517 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
5518 src->conninfo.url_str);
5520 goto create_request_failed;
5523 GST_DEBUG_OBJECT (src, "send options...");
5526 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
5529 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
5534 if (!gst_rtspsrc_parse_methods (src, &response))
5537 /* create DESCRIBE */
5538 GST_DEBUG_OBJECT (src, "create describe...");
5540 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
5541 src->conninfo.url_str);
5543 goto create_request_failed;
5545 /* we only accept SDP for now */
5546 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
5550 GST_DEBUG_OBJECT (src, "send describe...");
5553 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
5556 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
5560 /* we only perform redirect for the describe, currently */
5561 if (src->need_redirect) {
5562 /* close connection, we don't have to send a TEARDOWN yet, ignore the
5564 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5566 gst_rtsp_message_unset (&request);
5567 gst_rtsp_message_unset (&response);
5573 /* it could be that the DESCRIBE method was not implemented */
5574 if (!src->methods & GST_RTSP_DESCRIBE)
5577 /* check if reply is SDP */
5578 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
5580 /* could not be set but since the request returned OK, we assume it
5581 * was SDP, else check it. */
5583 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
5584 goto wrong_content_type;
5587 /* get message body and parse as SDP */
5588 gst_rtsp_message_get_body (&response, &data, &size);
5589 if (data == NULL || size == 0)
5592 GST_DEBUG_OBJECT (src, "parse SDP...");
5593 gst_sdp_message_new (sdp);
5594 gst_sdp_message_parse_buffer (data, size, *sdp);
5596 /* clean up any messages */
5597 gst_rtsp_message_unset (&request);
5598 gst_rtsp_message_unset (&response);
5605 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
5606 ("No valid RTSP URL was provided"));
5611 gchar *str = gst_rtsp_strresult (res);
5613 if (res != GST_RTSP_EINTR) {
5614 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
5615 ("Failed to connect. (%s)", str));
5617 GST_WARNING_OBJECT (src, "connect interrupted");
5622 create_request_failed:
5624 gchar *str = gst_rtsp_strresult (res);
5626 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5627 ("Could not create request. (%s)", str));
5633 /* Don't post a message - the rtsp_send method will have
5634 * taken care of it because we passed NULL for the response code */
5639 /* error was posted */
5640 res = GST_RTSP_ERROR;
5645 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5646 ("Server does not support SDP, got %s.", respcont));
5647 res = GST_RTSP_ERROR;
5652 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5653 ("Server can not provide an SDP."));
5654 res = GST_RTSP_ERROR;
5659 if (src->conninfo.connection) {
5660 GST_DEBUG_OBJECT (src, "free connection");
5661 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5663 gst_rtsp_message_unset (&request);
5664 gst_rtsp_message_unset (&response);
5669 static GstRTSPResult
5670 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
5675 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
5677 if (src->sdp == NULL) {
5678 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
5682 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
5687 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
5694 GST_WARNING_OBJECT (src, "can't get sdp");
5695 src->open_error = TRUE;
5700 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
5701 src->open_error = TRUE;
5706 static GstRTSPResult
5707 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
5709 GstRTSPMessage request = { 0 };
5710 GstRTSPMessage response = { 0 };
5711 GstRTSPResult res = GST_RTSP_OK;
5715 GST_DEBUG_OBJECT (src, "TEARDOWN...");
5717 if (src->state < GST_RTSP_STATE_READY) {
5718 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
5725 /* construct a control url */
5727 control = src->control;
5729 control = src->conninfo.url_str;
5731 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
5734 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5735 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5737 GstRTSPConnInfo *info;
5739 /* try aggregate control first but do non-aggregate control otherwise */
5741 setup_url = control;
5742 else if ((setup_url = stream->conninfo.location) == NULL)
5745 if (src->conninfo.connection) {
5746 info = &src->conninfo;
5747 } else if (stream->conninfo.connection) {
5748 info = &stream->conninfo;
5752 if (!info->connected)
5757 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
5759 goto create_request_failed;
5762 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
5765 gst_rtspsrc_send (src, info->connection, &request, &response,
5769 /* FIXME, parse result? */
5770 gst_rtsp_message_unset (&request);
5771 gst_rtsp_message_unset (&response);
5774 /* early exit when we did aggregate control */
5780 /* close connections */
5781 GST_DEBUG_OBJECT (src, "closing connection...");
5782 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5783 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5784 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5785 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
5789 gst_rtspsrc_cleanup (src);
5791 src->state = GST_RTSP_STATE_INVALID;
5794 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
5799 create_request_failed:
5801 gchar *str = gst_rtsp_strresult (res);
5803 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5804 ("Could not create request. (%s)", str));
5810 gchar *str = gst_rtsp_strresult (res);
5812 gst_rtsp_message_unset (&request);
5813 if (res != GST_RTSP_EINTR) {
5814 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5815 ("Could not send message. (%s)", str));
5817 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
5824 GST_DEBUG_OBJECT (src,
5825 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
5830 /* RTP-Info is of the format:
5832 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
5834 * rtptime corresponds to the timestamp for the NPT time given in the header
5835 * seqbase corresponds to the next sequence number we received. This number
5836 * indicates the first seqnum after the seek and should be used to discard
5837 * packets that are from before the seek.
5840 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
5845 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
5847 infos = g_strsplit (rtpinfo, ",", 0);
5848 for (i = 0; infos[i]; i++) {
5850 GstRTSPStream *stream;
5854 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
5856 /* init values, types of seqbase and timebase are bigger than needed so we
5857 * can store -1 as uninitialized values */
5862 /* parse url, find stream for url.
5863 * parse seq and rtptime. The seq number should be configured in the rtp
5864 * depayloader or session manager to detect gaps. Same for the rtptime, it
5865 * should be used to create an initial time newsegment. */
5866 fields = g_strsplit (infos[i], ";", 0);
5867 for (j = 0; fields[j]; j++) {
5868 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
5869 /* remove leading whitespace */
5870 fields[j] = g_strchug (fields[j]);
5871 if (g_str_has_prefix (fields[j], "url=")) {
5872 /* get the url and the stream */
5874 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
5875 } else if (g_str_has_prefix (fields[j], "seq=")) {
5876 seqbase = atoi (fields[j] + 4);
5877 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
5878 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
5881 g_strfreev (fields);
5882 /* now we need to store the values for the caps of the stream */
5883 if (stream != NULL) {
5884 GST_DEBUG_OBJECT (src,
5885 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
5886 stream, seqbase, timebase);
5888 /* we have a stream, configure detected params */
5889 stream->seqbase = seqbase;
5890 stream->timebase = timebase;
5899 gst_rtspsrc_get_float (const gchar * dstr)
5901 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
5903 /* canonicalise floating point string so we can handle float strings
5904 * in the form "24.930" or "24,930" irrespective of the current locale */
5905 g_strlcpy (s, dstr, sizeof (s));
5906 g_strdelimit (s, ",", '.');
5907 return g_ascii_strtod (s, NULL);
5911 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
5913 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
5915 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
5916 g_strlcpy (val_str, "now", sizeof (val_str));
5918 if (segment->last_stop == 0) {
5919 g_strlcpy (val_str, "0", sizeof (val_str));
5921 g_ascii_dtostr (val_str, sizeof (val_str),
5922 ((gdouble) segment->last_stop) / GST_SECOND);
5925 return g_strdup_printf ("npt=%s-", val_str);
5929 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
5931 stream->timebase = -1;
5932 stream->seqbase = -1;
5936 stream->caps = gst_caps_make_writable (stream->caps);
5937 s = gst_caps_get_structure (stream->caps, 0);
5938 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
5942 static GstRTSPResult
5943 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
5945 GstRTSPResult res = GST_RTSP_OK;
5947 if (src->state < GST_RTSP_STATE_READY) {
5948 res = GST_RTSP_ERROR;
5949 if (src->open_error) {
5950 GST_DEBUG_OBJECT (src, "the stream was in error");
5954 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
5956 if ((res = gst_rtspsrc_open (src, async)) < 0) {
5957 GST_DEBUG_OBJECT (src, "failed to open stream");
5966 static GstRTSPResult
5967 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
5969 GstRTSPMessage request = { 0 };
5970 GstRTSPMessage response = { 0 };
5971 GstRTSPResult res = GST_RTSP_OK;
5977 GST_DEBUG_OBJECT (src, "PLAY...");
5979 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
5982 if (!(src->methods & GST_RTSP_PLAY))
5985 if (src->state == GST_RTSP_STATE_PLAYING)
5988 if (!src->conninfo.connection || !src->conninfo.connected)
5991 /* send some dummy packets before we activate the receive in the
5993 gst_rtspsrc_send_dummy_packets (src);
5995 /* activate receive elements */
5996 gst_element_set_state (GST_ELEMENT_CAST (src), GST_STATE_PLAYING);
5998 /* construct a control url */
6000 control = src->control;
6002 control = src->conninfo.url_str;
6004 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6005 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6007 GstRTSPConnection *conn;
6009 /* try aggregate control first but do non-aggregate control otherwise */
6011 setup_url = control;
6012 else if ((setup_url = stream->conninfo.location) == NULL)
6015 if (src->conninfo.connection) {
6016 conn = src->conninfo.connection;
6017 } else if (stream->conninfo.connection) {
6018 conn = stream->conninfo.connection;
6024 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
6026 goto create_request_failed;
6028 if (src->need_range) {
6029 hval = gen_range_header (src, segment);
6031 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_RANGE, hval);
6035 if (segment->rate != 1.0) {
6036 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
6038 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
6040 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
6042 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
6046 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
6048 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6051 /* seek may have silently failed as it is not supported */
6052 if (!(src->methods & GST_RTSP_PLAY)) {
6053 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
6054 /* obviously it is supported as we made it here */
6055 src->methods |= GST_RTSP_PLAY;
6056 src->seekable = FALSE;
6057 /* but there is nothing to parse in the response,
6058 * so convey we have no idea and not to expect anything particular */
6059 clear_rtp_base (src, stream);
6063 /* need to do for all streams */
6064 for (run = src->streams; run; run = g_list_next (run))
6065 clear_rtp_base (src, (GstRTSPStream *) run->data);
6067 /* NOTE the above also disables npt based eos detection */
6068 /* and below forces position to 0,
6069 * which is visible feedback we lost the plot */
6070 segment->start = segment->last_stop = src->last_pos;
6073 gst_rtsp_message_unset (&request);
6075 /* parse RTP npt field. This is the current position in the stream (Normal
6076 * Play Time) and should be put in the NEWSEGMENT position field. */
6077 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
6079 gst_rtspsrc_parse_range (src, hval, segment);
6081 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
6082 segment->rate = 1.0;
6084 /* parse Speed header. This is the intended playback rate of the stream
6085 * and should be put in the NEWSEGMENT rate field. */
6086 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
6087 0) == GST_RTSP_OK) {
6088 segment->rate = gst_rtspsrc_get_float (hval);
6089 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
6090 &hval, 0) == GST_RTSP_OK) {
6091 segment->rate = gst_rtspsrc_get_float (hval);
6094 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
6095 * for the RTP packets. If this is not present, we assume all starts from 0...
6096 * This is info for the RTP session manager that we pass to it in caps. */
6098 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
6099 &hval, hval_idx++) == GST_RTSP_OK)
6100 gst_rtspsrc_parse_rtpinfo (src, hval);
6102 gst_rtsp_message_unset (&response);
6104 /* early exit when we did aggregate control */
6108 /* set again when needed */
6109 src->need_range = FALSE;
6111 /* configure the caps of the streams after we parsed all headers. */
6112 gst_rtspsrc_configure_caps (src, segment);
6114 src->running = TRUE;
6115 src->base_time = -1;
6116 src->state = GST_RTSP_STATE_PLAYING;
6119 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
6120 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6121 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6122 stream->discont = TRUE;
6127 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
6134 GST_DEBUG_OBJECT (src, "failed to open stream");
6139 GST_DEBUG_OBJECT (src, "PLAY is not supported");
6144 GST_DEBUG_OBJECT (src, "we were already PLAYING");
6147 create_request_failed:
6149 gchar *str = gst_rtsp_strresult (res);
6151 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6152 ("Could not create request. (%s)", str));
6158 gchar *str = gst_rtsp_strresult (res);
6160 gst_rtsp_message_unset (&request);
6161 if (res != GST_RTSP_EINTR) {
6162 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6163 ("Could not send message. (%s)", str));
6165 GST_WARNING_OBJECT (src, "PLAY interrupted");
6172 static GstRTSPResult
6173 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean idle, gboolean async)
6175 GstRTSPResult res = GST_RTSP_OK;
6176 GstRTSPMessage request = { 0 };
6177 GstRTSPMessage response = { 0 };
6181 GST_DEBUG_OBJECT (src, "PAUSE...");
6183 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6186 if (!(src->methods & GST_RTSP_PAUSE))
6189 if (src->state == GST_RTSP_STATE_READY)
6192 if (!src->conninfo.connection || !src->conninfo.connected)
6195 /* construct a control url */
6197 control = src->control;
6199 control = src->conninfo.url_str;
6201 /* loop over the streams. We might exit the loop early when we could do an
6202 * aggregate control */
6203 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6204 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6205 GstRTSPConnection *conn;
6208 /* try aggregate control first but do non-aggregate control otherwise */
6210 setup_url = control;
6211 else if ((setup_url = stream->conninfo.location) == NULL)
6214 if (src->conninfo.connection) {
6215 conn = src->conninfo.connection;
6216 } else if (stream->conninfo.connection) {
6217 conn = stream->conninfo.connection;
6223 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
6224 ("Sending PAUSE request"));
6227 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
6229 goto create_request_failed;
6231 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6234 gst_rtsp_message_unset (&request);
6235 gst_rtsp_message_unset (&response);
6237 /* exit early when we did agregate control */
6243 src->state = GST_RTSP_STATE_READY;
6247 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
6254 GST_DEBUG_OBJECT (src, "failed to open stream");
6259 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
6264 GST_DEBUG_OBJECT (src, "we were already PAUSED");
6267 create_request_failed:
6269 gchar *str = gst_rtsp_strresult (res);
6271 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6272 ("Could not create request. (%s)", str));
6278 gchar *str = gst_rtsp_strresult (res);
6280 gst_rtsp_message_unset (&request);
6281 if (res != GST_RTSP_EINTR) {
6282 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6283 ("Could not send message. (%s)", str));
6285 GST_WARNING_OBJECT (src, "PAUSE interrupted");
6293 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
6295 GstRTSPSrc *rtspsrc;
6297 rtspsrc = GST_RTSPSRC (bin);
6299 switch (GST_MESSAGE_TYPE (message)) {
6300 case GST_MESSAGE_EOS:
6301 gst_message_unref (message);
6303 case GST_MESSAGE_ELEMENT:
6305 const GstStructure *s = gst_message_get_structure (message);
6307 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
6308 gboolean ignore_timeout;
6310 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
6312 GST_OBJECT_LOCK (rtspsrc);
6313 ignore_timeout = rtspsrc->ignore_timeout;
6314 rtspsrc->ignore_timeout = TRUE;
6315 GST_OBJECT_UNLOCK (rtspsrc);
6317 /* we only act on the first udp timeout message, others are irrelevant
6318 * and can be ignored. */
6319 if (!ignore_timeout)
6320 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, TRUE);
6322 gst_message_unref (message);
6325 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6328 case GST_MESSAGE_ERROR:
6331 GstRTSPStream *stream;
6334 udpsrc = GST_MESSAGE_SRC (message);
6336 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
6337 GST_ELEMENT_NAME (udpsrc));
6339 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
6343 /* we ignore the RTCP udpsrc */
6344 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
6347 /* if we get error messages from the udp sources, that's not a problem as
6348 * long as not all of them error out. We also don't really know what the
6349 * problem is, the message does not give enough detail... */
6350 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
6351 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
6352 if (ret != GST_FLOW_OK)
6356 gst_message_unref (message);
6360 /* fatal but not our message, forward */
6361 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6366 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6372 /* the thread where everything happens */
6374 gst_rtspsrc_thread (GstRTSPSrc * src)
6378 gboolean running = FALSE;
6380 GST_OBJECT_LOCK (src);
6381 cmd = src->loop_cmd;
6382 src->loop_cmd = CMD_WAIT;
6383 GST_DEBUG_OBJECT (src, "got command %d", cmd);
6385 /* we got the message command, so ensure communication is possible again */
6386 gst_rtspsrc_connection_flush (src, FALSE);
6388 /* we allow these to be interrupted */
6389 if (cmd == CMD_LOOP || cmd == CMD_CLOSE || cmd == CMD_PAUSE)
6390 src->waiting = TRUE;
6391 GST_OBJECT_UNLOCK (src);
6395 src->cur_protocols = src->protocols;
6396 /* first attempt, don't ignore timeouts */
6397 src->ignore_timeout = FALSE;
6398 src->open_error = FALSE;
6399 ret = gst_rtspsrc_open (src, TRUE);
6402 ret = gst_rtspsrc_play (src, &src->segment, TRUE);
6403 if (ret == GST_RTSP_OK)
6407 ret = gst_rtspsrc_pause (src, TRUE, TRUE);
6408 if (ret == GST_RTSP_OK)
6412 ret = gst_rtspsrc_close (src, TRUE, FALSE);
6415 running = gst_rtspsrc_loop (src);
6418 ret = gst_rtspsrc_reconnect (src, FALSE);
6419 if (ret == GST_RTSP_OK)
6426 GST_OBJECT_LOCK (src);
6427 /* and go back to sleep */
6428 if (src->loop_cmd == CMD_WAIT) {
6430 src->loop_cmd = CMD_LOOP;
6432 gst_task_pause (src->task);
6434 GST_OBJECT_UNLOCK (src);
6438 gst_rtspsrc_start (GstRTSPSrc * src)
6440 GST_DEBUG_OBJECT (src, "starting");
6442 GST_OBJECT_LOCK (src);
6444 src->loop_cmd = CMD_WAIT;
6446 if (src->task == NULL) {
6447 src->task = gst_task_create ((GstTaskFunction) gst_rtspsrc_thread, src);
6448 if (src->task == NULL)
6451 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
6453 GST_OBJECT_UNLOCK (src);
6460 GST_ERROR_OBJECT (src, "failed to create task");
6466 gst_rtspsrc_stop (GstRTSPSrc * src)
6470 GST_DEBUG_OBJECT (src, "stopping");
6472 /* also cancels pending task */
6473 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, TRUE);
6475 GST_OBJECT_LOCK (src);
6476 if ((task = src->task)) {
6478 GST_OBJECT_UNLOCK (src);
6480 gst_task_stop (task);
6482 /* make sure it is not running */
6483 GST_RTSP_STREAM_LOCK (src);
6484 GST_RTSP_STREAM_UNLOCK (src);
6486 /* now wait for the task to finish */
6487 gst_task_join (task);
6489 /* and free the task */
6490 gst_object_unref (GST_OBJECT (task));
6492 GST_OBJECT_LOCK (src);
6494 GST_OBJECT_UNLOCK (src);
6496 /* ensure synchronously all is closed and clean */
6497 gst_rtspsrc_close (src, FALSE, TRUE);
6502 static GstStateChangeReturn
6503 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
6505 GstRTSPSrc *rtspsrc;
6506 GstStateChangeReturn ret;
6508 rtspsrc = GST_RTSPSRC (element);
6510 switch (transition) {
6511 case GST_STATE_CHANGE_NULL_TO_READY:
6512 if (!gst_rtspsrc_start (rtspsrc))
6515 case GST_STATE_CHANGE_READY_TO_PAUSED:
6516 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, FALSE);
6518 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
6519 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
6520 /* unblock the tcp tasks and make the loop waiting */
6521 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, TRUE);
6523 case GST_STATE_CHANGE_PAUSED_TO_READY:
6529 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
6530 if (ret == GST_STATE_CHANGE_FAILURE)
6533 switch (transition) {
6534 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
6535 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, FALSE);
6537 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
6538 /* send pause request and keep the idle task around */
6539 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, FALSE);
6540 ret = GST_STATE_CHANGE_NO_PREROLL;
6542 case GST_STATE_CHANGE_READY_TO_PAUSED:
6543 ret = GST_STATE_CHANGE_NO_PREROLL;
6545 case GST_STATE_CHANGE_PAUSED_TO_READY:
6546 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, FALSE);
6548 case GST_STATE_CHANGE_READY_TO_NULL:
6549 gst_rtspsrc_stop (rtspsrc);
6560 GST_DEBUG_OBJECT (rtspsrc, "start failed");
6561 return GST_STATE_CHANGE_FAILURE;
6566 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
6569 GstRTSPSrc *rtspsrc;
6571 rtspsrc = GST_RTSPSRC (element);
6573 if (GST_EVENT_IS_DOWNSTREAM (event)) {
6574 res = gst_rtspsrc_push_event (rtspsrc, event, TRUE);
6576 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
6583 /*** GSTURIHANDLER INTERFACE *************************************************/
6586 gst_rtspsrc_uri_get_type (void)
6592 gst_rtspsrc_uri_get_protocols (void)
6594 static const gchar *protocols[] =
6595 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp", NULL };
6597 return (gchar **) protocols;
6600 static const gchar *
6601 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
6603 GstRTSPSrc *src = GST_RTSPSRC (handler);
6605 /* should not dup */
6606 return src->conninfo.location;
6610 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri)
6614 GstRTSPUrl *newurl = NULL;
6615 GstSDPMessage *sdp = NULL;
6617 src = GST_RTSPSRC (handler);
6619 /* same URI, we're fine */
6620 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
6623 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
6624 if ((res = gst_sdp_message_new (&sdp) < 0))
6627 GST_DEBUG_OBJECT (src, "parsing SDP message");
6628 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
6632 GST_DEBUG_OBJECT (src, "parsing URI");
6633 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
6637 /* if worked, free previous and store new url object along with the original
6639 GST_DEBUG_OBJECT (src, "configuring URI");
6640 g_free (src->conninfo.location);
6641 src->conninfo.location = g_strdup (uri);
6642 gst_rtsp_url_free (src->conninfo.url);
6643 src->conninfo.url = newurl;
6644 g_free (src->conninfo.url_str);
6646 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
6648 src->conninfo.url_str = NULL;
6651 gst_sdp_message_free (src->sdp);
6653 src->from_sdp = sdp != NULL;
6655 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
6656 GST_DEBUG_OBJECT (src, "request uri is: %s",
6657 GST_STR_NULL (src->conninfo.url_str));
6664 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
6669 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
6674 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
6675 GST_STR_NULL (uri));
6676 gst_sdp_message_free (sdp);
6681 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
6682 GST_STR_NULL (uri), res);
6688 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
6690 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
6692 iface->get_type = gst_rtspsrc_uri_get_type;
6693 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
6694 iface->get_uri = gst_rtspsrc_uri_get_uri;
6695 iface->set_uri = gst_rtspsrc_uri_set_uri;