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 (TRUE);
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_do_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);
1850 /* now we did the seek and can activate the new segment values */
1851 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1853 /* if we're doing a segment seek, post a SEGMENT_START message */
1854 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1855 gst_element_post_message (GST_ELEMENT_CAST (src),
1856 gst_message_new_segment_start (GST_OBJECT_CAST (src),
1857 src->segment.format, src->segment.position));
1860 /* now create the newsegment */
1861 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
1862 " to %" G_GINT64_FORMAT, src->segment.position, stop);
1864 /* store the newsegment event so it can be sent from the streaming thread. */
1865 if (src->start_segment)
1866 gst_event_unref (src->start_segment);
1867 src->start_segment = gst_event_new_segment (&src->segment);
1870 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
1871 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1872 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1873 stream->discont = TRUE;
1877 GST_RTSP_STREAM_UNLOCK (src);
1884 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
1889 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
1895 gst_rtspsrc_handle_src_event (GstPad * pad, GstEvent * event)
1898 gboolean res = TRUE;
1901 src = GST_RTSPSRC_CAST (gst_pad_get_parent (pad));
1903 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
1904 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
1906 switch (GST_EVENT_TYPE (event)) {
1907 case GST_EVENT_SEEK:
1908 res = gst_rtspsrc_perform_seek (src, event);
1912 case GST_EVENT_NAVIGATION:
1913 case GST_EVENT_LATENCY:
1921 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
1922 res = gst_pad_send_event (target, event);
1923 gst_object_unref (target);
1925 gst_event_unref (event);
1928 gst_event_unref (event);
1930 gst_object_unref (src);
1935 /* this is the final event function we receive on the internal source pad when
1936 * we deal with TCP connections */
1938 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstEvent * event)
1943 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
1945 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
1946 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
1948 switch (GST_EVENT_TYPE (event)) {
1949 case GST_EVENT_SEEK:
1951 case GST_EVENT_NAVIGATION:
1952 case GST_EVENT_LATENCY:
1954 gst_event_unref (event);
1961 /* this is the final query function we receive on the internal source pad when
1962 * we deal with TCP connections */
1964 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstQuery * query)
1967 gboolean res = TRUE;
1969 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
1971 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
1972 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
1974 switch (GST_QUERY_TYPE (query)) {
1975 case GST_QUERY_POSITION:
1980 case GST_QUERY_DURATION:
1984 gst_query_parse_duration (query, &format, NULL);
1987 case GST_FORMAT_TIME:
1988 gst_query_set_duration (query, format, src->segment.duration);
1996 case GST_QUERY_LATENCY:
1998 /* we are live with a min latency of 0 and unlimited max latency, this
1999 * result will be updated by the session manager if there is any. */
2000 gst_query_set_latency (query, TRUE, 0, -1);
2010 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2012 gst_rtspsrc_handle_src_query (GstPad * pad, GstQuery * query)
2015 gboolean res = FALSE;
2017 src = GST_RTSPSRC_CAST (gst_pad_get_parent (pad));
2019 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2020 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2022 switch (GST_QUERY_TYPE (query)) {
2023 case GST_QUERY_DURATION:
2027 gst_query_parse_duration (query, &format, NULL);
2030 case GST_FORMAT_TIME:
2031 gst_query_set_duration (query, format, src->segment.duration);
2039 case GST_QUERY_SEEKING:
2043 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2044 if (format == GST_FORMAT_TIME) {
2046 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2048 /* seeking without duration is unlikely */
2049 seekable = seekable && src->seekable && src->segment.duration &&
2050 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2052 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2053 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2054 src->segment.start, src->segment.stop);
2061 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2063 /* forward the query to the proxy target pad */
2065 res = gst_pad_query (target, query);
2066 gst_object_unref (target);
2071 gst_object_unref (src);
2076 /* callback for RTCP messages to be sent to the server when operating in TCP
2078 static GstFlowReturn
2079 gst_rtspsrc_sink_chain (GstPad * pad, GstBuffer * buffer)
2082 GstRTSPStream *stream;
2083 GstFlowReturn res = GST_FLOW_OK;
2088 GstRTSPMessage message = { 0 };
2089 GstRTSPConnection *conn;
2091 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2092 src = stream->parent;
2094 data = gst_buffer_map (buffer, &bsize, NULL, GST_MAP_READ);
2097 gst_rtsp_message_init_data (&message, stream->channel[1]);
2099 /* lend the body data to the message */
2100 gst_rtsp_message_take_body (&message, data, size);
2102 if (stream->conninfo.connection)
2103 conn = stream->conninfo.connection;
2105 conn = src->conninfo.connection;
2107 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2108 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2109 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2111 /* and steal it away again because we will free it when unreffing the
2113 gst_rtsp_message_steal_body (&message, &data, &size);
2114 gst_rtsp_message_unset (&message);
2116 gst_buffer_unmap (buffer, data, size);
2117 gst_buffer_unref (buffer);
2122 static GstProbeReturn
2123 pad_blocked (GstPad * pad, GstProbeType type, gpointer type_data,
2126 GstRTSPSrc *src = user_data;
2128 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2129 GST_DEBUG_PAD_NAME (pad));
2131 /* activate the streams */
2132 GST_OBJECT_LOCK (src);
2133 if (!src->need_activate)
2136 src->need_activate = FALSE;
2137 GST_OBJECT_UNLOCK (src);
2139 gst_rtspsrc_activate_streams (src);
2141 return GST_PROBE_OK;
2145 GST_OBJECT_UNLOCK (src);
2146 return GST_PROBE_OK;
2150 /* this callback is called when the session manager generated a new src pad with
2151 * payloaded RTP packets. We simply ghost the pad here. */
2153 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2156 GstPadTemplate *template;
2159 GstRTSPStream *stream;
2162 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2164 GST_RTSP_STATE_LOCK (src);
2166 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2167 if (sscanf (name, "recv_rtp_src_%d_%d_%d", &id, &ssrc, &pt) != 3)
2168 goto unknown_stream;
2170 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %d, PT %d", id, ssrc, pt);
2172 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2174 goto unknown_stream;
2176 /* create a new pad we will use to stream to */
2177 template = gst_static_pad_template_get (&rtptemplate);
2178 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2179 gst_object_unref (template);
2182 stream->added = TRUE;
2183 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2184 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2185 gst_pad_set_active (stream->srcpad, TRUE);
2186 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2188 /* check if we added all streams */
2190 for (lstream = src->streams; lstream; lstream = g_list_next (lstream)) {
2191 stream = (GstRTSPStream *) lstream->data;
2193 GST_DEBUG_OBJECT (src, "stream %p, container %d, disabled %d, added %d",
2194 stream, stream->container, stream->disabled, stream->added);
2196 /* a container stream only needs one pad added. Also disabled streams don't
2198 if (!stream->container && !stream->disabled && !stream->added) {
2203 GST_RTSP_STATE_UNLOCK (src);
2206 GST_DEBUG_OBJECT (src, "We added all streams");
2207 /* when we get here, all stream are added and we can fire the no-more-pads
2209 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2217 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2218 GST_RTSP_STATE_UNLOCK (src);
2225 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2227 GstRTSPStream *stream;
2230 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2232 GST_RTSP_STATE_LOCK (src);
2233 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2235 goto unknown_stream;
2237 caps = stream->caps;
2239 gst_caps_ref (caps);
2240 GST_RTSP_STATE_UNLOCK (src);
2246 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2247 GST_RTSP_STATE_UNLOCK (src);
2253 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2255 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2261 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos (), TRUE);
2267 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2273 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2275 GstRTSPSrc *src = stream->parent;
2277 GST_DEBUG_OBJECT (src, "source in session %u received BYE", stream->id);
2279 gst_rtspsrc_do_stream_eos (src, stream);
2283 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2285 GstRTSPSrc *src = stream->parent;
2287 GST_DEBUG_OBJECT (src, "source in session %u timed out", stream->id);
2289 gst_rtspsrc_do_stream_eos (src, stream);
2293 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2295 GstRTSPStream *stream;
2297 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2299 /* get stream for session */
2300 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2302 gst_rtspsrc_do_stream_eos (src, stream);
2307 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2309 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2313 /* try to get and configure a manager */
2315 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
2316 GstRTSPTransport * transport)
2318 const gchar *manager;
2320 GstStateChangeReturn ret;
2322 /* find a manager */
2323 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
2327 GST_DEBUG_OBJECT (src, "using manager %s", manager);
2329 /* configure the manager */
2330 if (src->manager == NULL) {
2331 GObjectClass *klass;
2334 if (!(src->manager = gst_element_factory_make (manager, NULL))) {
2336 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
2340 goto use_no_manager;
2342 if (!(src->manager = gst_element_factory_make (manager, NULL)))
2343 goto manager_failed;
2346 /* we manage this element */
2347 gst_bin_add (GST_BIN_CAST (src), src->manager);
2349 GST_OBJECT_LOCK (src);
2350 target = GST_STATE_TARGET (src);
2351 GST_OBJECT_UNLOCK (src);
2353 ret = gst_element_set_state (src->manager, target);
2354 if (ret == GST_STATE_CHANGE_FAILURE)
2355 goto start_manager_failure;
2357 g_object_set (src->manager, "latency", src->latency, NULL);
2359 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2360 if (g_object_class_find_property (klass, "buffer-mode")) {
2361 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2362 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2364 gboolean need_slave;
2366 const gchar *encoding;
2368 /* buffer mode pauses are handled by adding offsets to buffer times,
2369 * but some depayloaders may have a hard time syncing output times
2370 * with such input times, e.g. container ones, most notably ASF */
2371 /* TODO alternatives are having an event that indicates these shifts,
2372 * or having rtsp extensions provide suggestion on buffer mode */
2373 need_slave = stream->container;
2374 if (stream->caps && (s = gst_caps_get_structure (stream->caps, 0)) &&
2375 (encoding = gst_structure_get_string (s, "encoding-name")))
2376 need_slave = need_slave || (strcmp (encoding, "X-ASF-PF") == 0);
2377 GST_DEBUG_OBJECT (src, "auto buffering mode, need_slave %d",
2379 /* valid duration implies not likely live pipeline,
2380 * so slaving in jitterbuffer does not make much sense
2381 * (and might mess things up due to bursts) */
2382 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
2383 src->segment.duration && !need_slave) {
2384 GST_DEBUG_OBJECT (src, "selected buffer");
2385 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER,
2388 GST_DEBUG_OBJECT (src, "selected slave");
2389 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2394 /* connect to signals if we did not already do so */
2395 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
2397 src->manager_sig_id =
2398 g_signal_connect (src->manager, "pad-added",
2399 (GCallback) new_manager_pad, src);
2400 src->manager_ptmap_id =
2401 g_signal_connect (src->manager, "request-pt-map",
2402 (GCallback) request_pt_map, src);
2404 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
2408 /* we stream directly to the manager, get some pads. Each RTSP stream goes
2409 * into a separate RTP session. */
2410 name = g_strdup_printf ("recv_rtp_sink_%d", stream->id);
2411 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
2413 name = g_strdup_printf ("recv_rtcp_sink_%d", stream->id);
2414 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
2417 /* now configure the bandwidth in the manager */
2418 if (g_signal_lookup ("get-internal-session",
2419 G_OBJECT_TYPE (src->manager)) != 0) {
2420 GObject *rtpsession;
2422 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
2425 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
2427 stream->session = rtpsession;
2429 if (stream->as_bandwidth != -1) {
2430 GST_INFO_OBJECT (src, "setting AS: %f",
2431 (gdouble) (stream->as_bandwidth * 1000));
2432 g_object_set (rtpsession, "bandwidth",
2433 (gdouble) (stream->as_bandwidth * 1000), NULL);
2435 if (stream->rr_bandwidth != -1) {
2436 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
2437 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
2440 if (stream->rs_bandwidth != -1) {
2441 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
2442 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
2445 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2447 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
2449 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
2451 g_signal_connect (rtpsession, "on-ssrc-active",
2452 (GCallback) on_ssrc_active, stream);
2463 GST_DEBUG_OBJECT (src, "cannot get a session manager");
2468 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
2471 start_manager_failure:
2473 GST_DEBUG_OBJECT (src, "could not start session manager");
2478 /* free the UDP sources allocated when negotiating a transport.
2479 * This function is called when the server negotiated to a transport where the
2480 * UDP sources are not needed anymore, such as TCP or multicast. */
2482 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
2486 for (i = 0; i < 2; i++) {
2487 if (stream->udpsrc[i]) {
2488 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
2489 gst_object_unref (stream->udpsrc[i]);
2490 stream->udpsrc[i] = NULL;
2495 /* for TCP, create pads to send and receive data to and from the manager and to
2496 * intercept various events and queries
2499 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
2500 GstRTSPTransport * transport, GstPad ** outpad)
2503 GstPadTemplate *template;
2504 GstPad *pad0, *pad1;
2506 /* configure for interleaved delivery, nothing needs to be done
2507 * here, the loop function will call the chain functions of the
2508 * session manager. */
2509 stream->channel[0] = transport->interleaved.min;
2510 stream->channel[1] = transport->interleaved.max;
2511 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
2512 stream->channel[0], stream->channel[1]);
2514 /* we can remove the allocated UDP ports now */
2515 gst_rtspsrc_stream_free_udp (stream);
2517 /* no session manager, send data to srcpad directly */
2518 if (!stream->channelpad[0]) {
2519 GST_DEBUG_OBJECT (src, "no manager, creating pad");
2521 /* create a new pad we will use to stream to */
2522 name = g_strdup_printf ("stream%d", stream->id);
2523 template = gst_static_pad_template_get (&rtptemplate);
2524 stream->channelpad[0] = gst_pad_new_from_template (template, name);
2525 gst_object_unref (template);
2528 /* set caps and activate */
2529 gst_pad_use_fixed_caps (stream->channelpad[0]);
2530 gst_pad_set_active (stream->channelpad[0], TRUE);
2532 *outpad = gst_object_ref (stream->channelpad[0]);
2534 GST_DEBUG_OBJECT (src, "using manager source pad");
2536 template = gst_static_pad_template_get (&anysrctemplate);
2538 /* allocate pads for sending the channel data into the manager */
2539 pad0 = gst_pad_new_from_template (template, "internalsrc0");
2540 gst_pad_link (pad0, stream->channelpad[0]);
2541 gst_object_unref (stream->channelpad[0]);
2542 stream->channelpad[0] = pad0;
2543 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
2544 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
2545 gst_pad_set_element_private (pad0, src);
2546 gst_pad_set_active (pad0, TRUE);
2548 if (stream->channelpad[1]) {
2549 /* if we have a sinkpad for the other channel, create a pad and link to the
2551 pad1 = gst_pad_new_from_template (template, "internalsrc1");
2552 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
2553 gst_pad_link (pad1, stream->channelpad[1]);
2554 gst_object_unref (stream->channelpad[1]);
2555 stream->channelpad[1] = pad1;
2556 gst_pad_set_active (pad1, TRUE);
2558 gst_object_unref (template);
2560 /* setup RTCP transport back to the server if we have to. */
2561 if (src->manager && src->do_rtcp) {
2564 template = gst_static_pad_template_get (&anysinktemplate);
2566 stream->rtcppad = gst_pad_new_from_template (template, "internalsink0");
2567 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
2568 gst_pad_set_element_private (stream->rtcppad, stream);
2569 gst_pad_set_active (stream->rtcppad, TRUE);
2571 /* get session RTCP pad */
2572 name = g_strdup_printf ("send_rtcp_src_%d", stream->id);
2573 pad = gst_element_get_request_pad (src->manager, name);
2578 gst_pad_link (pad, stream->rtcppad);
2579 gst_object_unref (pad);
2582 gst_object_unref (template);
2588 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
2589 GstRTSPTransport * transport, const gchar ** destination, gint * min,
2590 gint * max, guint * ttl)
2592 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2594 if (!(*destination = transport->destination))
2595 *destination = stream->destination;
2598 /* transport first */
2599 *min = transport->port.min;
2600 *max = transport->port.max;
2601 if (*min == -1 && *max == -1) {
2602 /* then try from SDP */
2603 if (stream->port != 0) {
2604 *min = stream->port;
2605 *max = stream->port + 1;
2611 if (!(*ttl = transport->ttl))
2616 /* first take the source, then the endpoint to figure out where to send
2618 if (!(*destination = transport->source)) {
2619 if (src->conninfo.connection)
2620 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
2621 else if (stream->conninfo.connection)
2623 gst_rtsp_connection_get_ip (stream->conninfo.connection);
2627 /* for unicast we only expect the ports here */
2628 *min = transport->server_port.min;
2629 *max = transport->server_port.max;
2634 /* For multicast create UDP sources and join the multicast group. */
2636 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
2637 GstRTSPTransport * transport, GstPad ** outpad)
2640 const gchar *destination;
2643 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
2645 /* we can remove the allocated UDP ports now */
2646 gst_rtspsrc_stream_free_udp (stream);
2648 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
2651 /* we need a destination now */
2652 if (destination == NULL)
2653 goto no_destination;
2655 /* we really need ports now or we won't be able to receive anything at all */
2656 if (min == -1 && max == -1)
2659 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
2660 destination, min, max);
2662 /* creating UDP source for RTP */
2664 uri = g_strdup_printf ("udp://%s:%d", destination, min);
2665 stream->udpsrc[0] = gst_element_make_from_uri (GST_URI_SRC, uri, NULL);
2667 if (stream->udpsrc[0] == NULL)
2670 /* take ownership */
2671 gst_object_ref_sink (stream->udpsrc[0]);
2674 gst_element_set_state (stream->udpsrc[0], GST_STATE_PAUSED);
2677 /* creating another UDP source for RTCP */
2679 uri = g_strdup_printf ("udp://%s:%d", destination, max);
2680 stream->udpsrc[1] = gst_element_make_from_uri (GST_URI_SRC, uri, NULL);
2682 if (stream->udpsrc[1] == NULL)
2685 /* take ownership */
2686 gst_object_ref_sink (stream->udpsrc[1]);
2688 gst_element_set_state (stream->udpsrc[1], GST_STATE_PAUSED);
2695 GST_DEBUG_OBJECT (src, "no UDP source element found");
2700 GST_DEBUG_OBJECT (src, "no destination found");
2705 GST_DEBUG_OBJECT (src, "no ports found");
2710 /* configure the remainder of the UDP ports */
2712 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
2713 GstRTSPTransport * transport, GstPad ** outpad)
2715 /* we manage the UDP elements now. For unicast, the UDP sources where
2716 * allocated in the stream when we suggested a transport. */
2717 if (stream->udpsrc[0]) {
2718 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
2720 GST_DEBUG_OBJECT (src, "setting up UDP source");
2722 /* configure a timeout on the UDP port. When the timeout message is
2723 * posted, we assume UDP transport is not possible. We reconnect using TCP
2725 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", src->udp_timeout,
2728 /* get output pad of the UDP source. */
2729 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
2731 /* save it so we can unblock */
2732 stream->blockedpad = *outpad;
2734 /* configure pad block on the pad. As soon as there is dataflow on the
2735 * UDP source, we know that UDP is not blocked by a firewall and we can
2736 * configure all the streams to let the application autoplug decoders. */
2738 gst_pad_add_probe (stream->blockedpad, GST_PROBE_TYPE_BLOCK,
2739 pad_blocked, src, NULL);
2741 if (stream->channelpad[0]) {
2742 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
2743 /* configure for UDP delivery, we need to connect the UDP pads to
2744 * the session plugin. */
2745 gst_pad_link (*outpad, stream->channelpad[0]);
2746 gst_object_unref (*outpad);
2748 /* we connected to pad-added signal to get pads from the manager */
2750 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
2755 if (stream->udpsrc[1]) {
2756 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
2758 if (stream->channelpad[1]) {
2761 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
2763 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
2764 gst_pad_link (pad, stream->channelpad[1]);
2765 gst_object_unref (pad);
2767 /* leave unlinked */
2773 /* configure the UDP sink back to the server for status reports */
2775 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
2776 GstRTSPStream * stream, GstRTSPTransport * transport)
2779 gint rtp_port, rtcp_port, sockfd = -1;
2780 gboolean do_rtp, do_rtcp;
2781 const gchar *destination;
2785 /* get transport info */
2786 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
2787 &rtp_port, &rtcp_port, &ttl);
2789 /* see what we need to do */
2790 do_rtp = (rtp_port != -1);
2791 /* it's possible that the server does not want us to send RTCP in which case
2793 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
2795 /* we need a destination when we have RTP or RTCP ports */
2796 if (destination == NULL && (do_rtp || do_rtcp))
2797 goto no_destination;
2799 /* try to construct the fakesrc to the RTP port of the server to open up any
2802 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
2805 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
2806 stream->udpsink[0] = gst_element_make_from_uri (GST_URI_SINK, uri, NULL);
2808 if (stream->udpsink[0] == NULL)
2809 goto no_sink_element;
2811 /* don't join multicast group, we will have the source socket do that */
2812 /* no sync or async state changes needed */
2813 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
2814 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
2816 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
2818 if (stream->udpsrc[0]) {
2819 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
2820 * so that NAT firewalls will open a hole for us */
2821 g_object_get (G_OBJECT (stream->udpsrc[0]), "sock", &sockfd, NULL);
2822 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %d", sockfd);
2823 /* configure socket and make sure udpsink does not close it when shutting
2824 * down, it belongs to udpsrc after all. */
2825 g_object_set (G_OBJECT (stream->udpsink[0]), "sockfd", sockfd,
2826 "closefd", FALSE, NULL);
2829 /* the source for the dummy packets to open up NAT */
2830 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
2831 if (stream->fakesrc == NULL)
2832 goto no_fakesrc_element;
2834 /* random data in 5 buffers, a size of 200 bytes should be fine */
2835 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
2836 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
2838 /* we don't want to consider this a sink */
2839 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_IS_SINK);
2841 /* keep everything locked */
2842 gst_element_set_locked_state (stream->udpsink[0], TRUE);
2843 gst_element_set_locked_state (stream->fakesrc, TRUE);
2845 gst_object_ref (stream->udpsink[0]);
2846 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
2847 gst_object_ref (stream->fakesrc);
2848 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
2850 gst_element_link (stream->fakesrc, stream->udpsink[0]);
2853 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
2856 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
2857 stream->udpsink[1] = gst_element_make_from_uri (GST_URI_SINK, uri, NULL);
2859 if (stream->udpsink[1] == NULL)
2860 goto no_sink_element;
2862 /* don't join multicast group, we will have the source socket do that */
2863 /* no sync or async state changes needed */
2864 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
2865 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
2867 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
2869 if (stream->udpsrc[1]) {
2870 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
2871 * because some servers check the port number of where it sends RTCP to identify
2872 * the RTCP packets it receives */
2873 g_object_get (G_OBJECT (stream->udpsrc[1]), "sock", &sockfd, NULL);
2874 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %d", sockfd);
2875 /* configure socket and make sure udpsink does not close it when shutting
2876 * down, it belongs to udpsrc after all. */
2877 g_object_set (G_OBJECT (stream->udpsink[1]), "sockfd", sockfd,
2878 "closefd", FALSE, NULL);
2881 /* we don't want to consider this a sink */
2882 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_IS_SINK);
2884 /* we keep this playing always */
2885 gst_element_set_locked_state (stream->udpsink[1], TRUE);
2886 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
2888 gst_object_ref (stream->udpsink[1]);
2889 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
2891 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
2893 /* get session RTCP pad */
2894 name = g_strdup_printf ("send_rtcp_src_%d", stream->id);
2895 pad = gst_element_get_request_pad (src->manager, name);
2900 gst_pad_link (pad, stream->rtcppad);
2901 gst_object_unref (pad);
2910 GST_DEBUG_OBJECT (src, "no destination address specified");
2915 GST_DEBUG_OBJECT (src, "no UDP sink element found");
2920 GST_DEBUG_OBJECT (src, "no fakesrc element found");
2925 /* sets up all elements needed for streaming over the specified transport.
2926 * Does not yet expose the element pads, this will be done when there is actuall
2927 * dataflow detected, which might never happen when UDP is blocked in a
2928 * firewall, for example.
2931 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
2932 GstRTSPTransport * transport)
2935 GstPad *outpad = NULL;
2936 GstPadTemplate *template;
2941 src = stream->parent;
2943 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
2945 s = gst_caps_get_structure (stream->caps, 0);
2947 /* get the proper mime type for this stream now */
2948 if (gst_rtsp_transport_get_mime (transport->trans, &mime) < 0)
2949 goto unknown_transport;
2951 goto unknown_transport;
2953 /* configure the final mime type */
2954 GST_DEBUG_OBJECT (src, "setting mime to %s", mime);
2955 gst_structure_set_name (s, mime);
2957 /* try to get and configure a manager, channelpad[0-1] will be configured with
2958 * the pads for the manager, or NULL when no manager is needed. */
2959 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
2962 switch (transport->lower_transport) {
2963 case GST_RTSP_LOWER_TRANS_TCP:
2964 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
2965 goto transport_failed;
2967 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
2968 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
2969 goto transport_failed;
2970 /* fallthrough, the rest is the same for UDP and MCAST */
2971 case GST_RTSP_LOWER_TRANS_UDP:
2972 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
2973 goto transport_failed;
2974 /* configure udpsinks back to the server for RTCP messages and for the
2975 * dummy RTP messages to open NAT. */
2976 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
2977 goto transport_failed;
2980 goto unknown_transport;
2984 GST_DEBUG_OBJECT (src, "creating ghostpad");
2986 gst_pad_use_fixed_caps (outpad);
2988 /* create ghostpad, don't add just yet, this will be done when we activate
2990 name = g_strdup_printf ("stream%d", stream->id);
2991 template = gst_static_pad_template_get (&rtptemplate);
2992 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
2993 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2994 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2995 gst_object_unref (template);
2998 gst_object_unref (outpad);
3000 /* mark pad as ok */
3001 stream->last_ret = GST_FLOW_OK;
3008 GST_DEBUG_OBJECT (src, "failed to configure transport");
3013 GST_DEBUG_OBJECT (src, "unknown transport");
3018 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3023 /* send a couple of dummy random packets on the receiver RTP port to the server,
3024 * this should make a firewall think we initiated the data transfer and
3025 * hopefully allow packets to go from the sender port to our RTP receiver port */
3027 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3031 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3034 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3035 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3037 if (stream->fakesrc && stream->udpsink[0]) {
3038 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3039 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3040 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3041 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3042 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3048 /* Adds the source pads of all configured streams to the element.
3049 * This code is performed when we detected dataflow.
3051 * We detect dataflow from either the _loop function or with pad probes on the
3055 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3059 GST_DEBUG_OBJECT (src, "activating streams");
3061 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3062 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3064 if (stream->udpsrc[0]) {
3065 /* remove timeout, we are streaming now and timeouts will be handled by
3066 * the session manager and jitter buffer */
3067 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3069 if (stream->srcpad) {
3070 /* if we don't have a session manager, set the caps now. If we have a
3071 * session, we will get a notification of the pad and the caps. */
3072 if (!src->manager) {
3073 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3074 gst_pad_set_caps (stream->srcpad, stream->caps);
3077 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3078 gst_pad_set_active (stream->srcpad, TRUE);
3080 if (!stream->added) {
3081 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3082 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3083 stream->added = TRUE;
3088 /* unblock all pads */
3089 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3090 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3092 if (stream->blockid) {
3093 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3094 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3095 stream->blockid = 0;
3103 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment)
3106 guint64 start, stop;
3107 gdouble play_speed, play_scale;
3109 GST_DEBUG_OBJECT (src, "configuring stream caps");
3111 start = segment->position;
3112 stop = segment->duration;
3113 play_speed = segment->rate;
3114 play_scale = segment->applied_rate;
3116 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3117 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3120 if ((caps = stream->caps)) {
3121 caps = gst_caps_make_writable (caps);
3123 if (stream->timebase != -1)
3124 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3125 (guint) stream->timebase, NULL);
3126 if (stream->seqbase != -1)
3127 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3128 (guint) stream->seqbase, NULL);
3129 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3131 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3132 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3133 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3135 stream->caps = caps;
3137 GST_DEBUG_OBJECT (src, "stream %p, caps %" GST_PTR_FORMAT, stream, caps);
3140 GST_DEBUG_OBJECT (src, "clear session");
3141 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3145 static GstFlowReturn
3146 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3151 /* store the value */
3152 stream->last_ret = ret;
3154 /* if it's success we can return the value right away */
3155 if (ret == GST_FLOW_OK)
3158 /* any other error that is not-linked can be returned right
3160 if (ret != GST_FLOW_NOT_LINKED)
3163 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3164 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3165 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3167 ret = ostream->last_ret;
3168 /* some other return value (must be SUCCESS but we can return
3169 * other values as well) */
3170 if (ret != GST_FLOW_NOT_LINKED)
3173 /* if we get here, all other pads were unlinked and we return
3174 * NOT_LINKED then */
3180 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
3181 GstEvent * event, gboolean source)
3183 gboolean res = TRUE;
3185 /* only streams that have a connection to the outside world */
3186 if (stream->srcpad == NULL)
3189 if (source && stream->udpsrc[0]) {
3190 gst_event_ref (event);
3191 res = gst_element_send_event (stream->udpsrc[0], event);
3192 } else if (stream->channelpad[0]) {
3193 gst_event_ref (event);
3194 if (GST_PAD_IS_SRC (stream->channelpad[0]))
3195 res = gst_pad_push_event (stream->channelpad[0], event);
3197 res = gst_pad_send_event (stream->channelpad[0], event);
3200 if (source && stream->udpsrc[1]) {
3201 gst_event_ref (event);
3202 res &= gst_element_send_event (stream->udpsrc[1], event);
3203 } else if (stream->channelpad[1]) {
3204 gst_event_ref (event);
3205 if (GST_PAD_IS_SRC (stream->channelpad[1]))
3206 res &= gst_pad_push_event (stream->channelpad[1], event);
3208 res &= gst_pad_send_event (stream->channelpad[1], event);
3212 gst_event_unref (event);
3218 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event, gboolean source)
3221 gboolean res = TRUE;
3223 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3224 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3226 gst_event_ref (event);
3227 res &= gst_rtspsrc_stream_push_event (src, ostream, event, source);
3229 gst_event_unref (event);
3234 static GstRTSPResult
3235 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3240 if (info->connection == NULL) {
3241 if (info->url == NULL) {
3242 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
3243 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
3247 /* create connection */
3248 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
3249 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
3250 goto could_not_create;
3253 g_free (info->url_str);
3254 info->url_str = gst_rtsp_url_get_request_uri (info->url);
3256 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
3258 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
3259 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
3261 if (src->proxy_host) {
3262 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
3264 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
3269 if (!info->connected) {
3272 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
3273 ("Connecting to %s", info->location));
3274 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
3276 gst_rtsp_connection_connect (info->connection,
3277 src->ptcp_timeout)) < 0)
3278 goto could_not_connect;
3280 info->connected = TRUE;
3287 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
3292 gchar *str = gst_rtsp_strresult (res);
3293 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
3299 gchar *str = gst_rtsp_strresult (res);
3300 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
3306 static GstRTSPResult
3307 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
3310 if (info->connected) {
3311 GST_DEBUG_OBJECT (src, "closing connection...");
3312 gst_rtsp_connection_close (info->connection);
3313 info->connected = FALSE;
3315 if (free && info->connection) {
3316 /* free connection */
3317 GST_DEBUG_OBJECT (src, "freeing connection...");
3318 gst_rtsp_connection_free (info->connection);
3319 info->connection = NULL;
3324 static GstRTSPResult
3325 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3330 GST_DEBUG_OBJECT (src, "reconnecting connection...");
3331 gst_rtsp_conninfo_close (src, info, FALSE);
3332 res = gst_rtsp_conninfo_connect (src, info, async);
3338 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
3342 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
3343 if (src->conninfo.connection) {
3344 GST_DEBUG_OBJECT (src, "connection flush");
3345 gst_rtsp_connection_flush (src->conninfo.connection, flush);
3347 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3348 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3349 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
3350 if (stream->conninfo.connection)
3351 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
3355 /* FIXME, handle server request, reply with OK, for now */
3356 static GstRTSPResult
3357 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
3358 GstRTSPMessage * request)
3360 GstRTSPMessage response = { 0 };
3363 GST_DEBUG_OBJECT (src, "got server request message");
3366 gst_rtsp_message_dump (request);
3368 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
3370 if (res == GST_RTSP_ENOTIMPL) {
3371 /* default implementation, send OK */
3373 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
3378 GST_DEBUG_OBJECT (src, "replying with OK");
3381 gst_rtsp_message_dump (&response);
3383 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
3387 gst_rtsp_message_unset (&response);
3388 } else if (res == GST_RTSP_EEOF)
3396 gst_rtsp_message_unset (&response);
3401 /* send server keep-alive */
3402 static GstRTSPResult
3403 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
3405 GstRTSPMessage request = { 0 };
3407 GstRTSPMethod method;
3410 GST_DEBUG_OBJECT (src, "creating server keep-alive");
3412 /* find a method to use for keep-alive */
3413 if (src->methods & GST_RTSP_GET_PARAMETER)
3414 method = GST_RTSP_GET_PARAMETER;
3416 method = GST_RTSP_OPTIONS;
3419 control = src->control;
3421 control = src->conninfo.url_str;
3423 if (control == NULL)
3426 res = gst_rtsp_message_init_request (&request, method, control);
3431 gst_rtsp_message_dump (&request);
3434 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
3439 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3440 gst_rtsp_message_unset (&request);
3447 GST_WARNING_OBJECT (src, "no control url to send keepalive");
3452 gchar *str = gst_rtsp_strresult (res);
3454 gst_rtsp_message_unset (&request);
3455 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
3456 ("Could not send keep-alive. (%s)", str));
3462 static GstFlowReturn
3463 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
3465 GstRTSPMessage message = { 0 };
3468 GstRTSPStream *stream;
3469 GstPad *outpad = NULL;
3472 GstFlowReturn ret = GST_FLOW_OK;
3474 gboolean is_rtcp, have_data;
3476 /* here we are only interested in data messages */
3479 GTimeVal tv_timeout;
3481 /* get the next timeout interval */
3482 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3484 /* see if the timeout period expired */
3485 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
3486 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
3487 /* send keep-alive, only act on interrupt, a warning will be posted for
3489 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3491 /* get new timeout */
3492 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3495 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
3496 tv_timeout.tv_sec, tv_timeout.tv_usec);
3498 /* protect the connection with the connection lock so that we can see when
3499 * we are finished doing server communication */
3501 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
3502 &message, src->ptcp_timeout);
3506 GST_DEBUG_OBJECT (src, "we received a server message");
3508 case GST_RTSP_EINTR:
3509 /* we got interrupted this means we need to stop */
3511 case GST_RTSP_ETIMEOUT:
3512 /* no reply, send keep alive */
3513 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
3514 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3518 /* go EOS when the server closed the connection */
3524 switch (message.type) {
3525 case GST_RTSP_MESSAGE_REQUEST:
3526 /* server sends us a request message, handle it */
3528 gst_rtspsrc_handle_request (src, src->conninfo.connection,
3530 if (res == GST_RTSP_EEOF)
3533 goto handle_request_failed;
3535 case GST_RTSP_MESSAGE_RESPONSE:
3536 /* we ignore response messages */
3537 GST_DEBUG_OBJECT (src, "ignoring response message");
3539 gst_rtsp_message_dump (&message);
3541 case GST_RTSP_MESSAGE_DATA:
3542 GST_DEBUG_OBJECT (src, "got data message");
3546 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
3553 channel = message.type_data.data.channel;
3555 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
3557 goto unknown_stream;
3559 if (channel == stream->channel[0]) {
3560 outpad = stream->channelpad[0];
3562 } else if (channel == stream->channel[1]) {
3563 outpad = stream->channelpad[1];
3569 /* take a look at the body to figure out what we have */
3570 gst_rtsp_message_get_body (&message, &data, &size);
3572 goto invalid_length;
3574 /* channels are not correct on some servers, do extra check */
3575 if (data[1] >= 200 && data[1] <= 204) {
3576 /* hmm RTCP message switch to the RTCP pad of the same stream. */
3577 outpad = stream->channelpad[1];
3581 /* we have no clue what this is, just ignore then. */
3583 goto unknown_stream;
3585 /* take the message body for further processing */
3586 gst_rtsp_message_steal_body (&message, &data, &size);
3588 /* strip the trailing \0 */
3591 buf = gst_buffer_new ();
3592 gst_buffer_take_memory (buf, -1,
3593 gst_memory_new_wrapped (0, data, g_free, size, 0, size));
3595 /* don't need message anymore */
3596 gst_rtsp_message_unset (&message);
3598 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
3601 if (src->need_activate) {
3602 gst_rtspsrc_activate_streams (src);
3603 src->need_activate = FALSE;
3606 if (src->base_time == -1) {
3607 /* Take current running_time. This timestamp will be put on
3608 * the first buffer of each stream because we are a live source and so we
3609 * timestamp with the running_time. When we are dealing with TCP, we also
3610 * only timestamp the first buffer (using the DISCONT flag) because a server
3611 * typically bursts data, for which we don't want to compensate by speeding
3612 * up the media. The other timestamps will be interpollated from this one
3613 * using the RTP timestamps. */
3614 GST_OBJECT_LOCK (src);
3615 if (GST_ELEMENT_CLOCK (src)) {
3617 GstClockTime base_time;
3619 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
3620 base_time = GST_ELEMENT_CAST (src)->base_time;
3622 src->base_time = now - base_time;
3624 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
3625 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
3627 GST_OBJECT_UNLOCK (src);
3630 if (stream->discont && !is_rtcp) {
3631 /* mark first RTP buffer as discont */
3632 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
3633 stream->discont = FALSE;
3634 /* first buffer gets the timestamp, other buffers are not timestamped and
3635 * their presentation time will be interpollated from the rtp timestamps. */
3636 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
3637 GST_TIME_ARGS (src->base_time));
3639 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
3642 /* chain to the peer pad */
3643 if (GST_PAD_IS_SINK (outpad))
3644 ret = gst_pad_chain (outpad, buf);
3646 ret = gst_pad_push (outpad, buf);
3649 /* combine all stream flows for the data transport */
3650 ret = gst_rtspsrc_combine_flows (src, stream, ret);
3657 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
3658 gst_rtsp_message_unset (&message);
3663 GST_DEBUG_OBJECT (src, "we got an eof from the server");
3664 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3665 ("The server closed the connection."));
3666 src->conninfo.connected = FALSE;
3667 gst_rtsp_message_unset (&message);
3668 return GST_FLOW_UNEXPECTED;
3672 gst_rtsp_message_unset (&message);
3673 GST_DEBUG_OBJECT (src, "got interrupted: stop connection flush");
3674 gst_rtspsrc_connection_flush (src, FALSE);
3675 return GST_FLOW_WRONG_STATE;
3679 gchar *str = gst_rtsp_strresult (res);
3681 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
3682 ("Could not receive message. (%s)", str));
3685 gst_rtsp_message_unset (&message);
3686 return GST_FLOW_ERROR;
3688 handle_request_failed:
3690 gchar *str = gst_rtsp_strresult (res);
3692 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
3693 ("Could not handle server message. (%s)", str));
3695 gst_rtsp_message_unset (&message);
3696 return GST_FLOW_ERROR;
3700 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3701 ("Short message received, ignoring."));
3702 gst_rtsp_message_unset (&message);
3707 static GstFlowReturn
3708 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
3711 GstRTSPMessage message = { 0 };
3715 GTimeVal tv_timeout;
3717 /* get the next timeout interval */
3718 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3720 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
3721 (gint) tv_timeout.tv_sec);
3723 gst_rtsp_message_unset (&message);
3725 /* we should continue reading the TCP socket because the server might
3726 * send us requests. When the session timeout expires, we need to send a
3727 * keep-alive request to keep the session open. */
3728 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
3729 &message, &tv_timeout);
3733 GST_DEBUG_OBJECT (src, "we received a server message");
3735 case GST_RTSP_EINTR:
3736 /* we got interrupted, see what we have to do */
3738 case GST_RTSP_ETIMEOUT:
3739 /* send keep-alive, ignore the result, a warning will be posted. */
3740 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
3741 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3745 /* server closed the connection. not very fatal for UDP, reconnect and
3746 * see what happens. */
3747 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3748 ("The server closed the connection."));
3750 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
3758 switch (message.type) {
3759 case GST_RTSP_MESSAGE_REQUEST:
3760 /* server sends us a request message, handle it */
3762 gst_rtspsrc_handle_request (src, src->conninfo.connection,
3764 if (res == GST_RTSP_EEOF)
3767 goto handle_request_failed;
3769 case GST_RTSP_MESSAGE_RESPONSE:
3770 /* we ignore response and data messages */
3771 GST_DEBUG_OBJECT (src, "ignoring response message");
3773 gst_rtsp_message_dump (&message);
3774 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
3775 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
3776 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
3777 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
3778 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3785 case GST_RTSP_MESSAGE_DATA:
3786 /* we ignore response and data messages */
3787 GST_DEBUG_OBJECT (src, "ignoring data message");
3790 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
3796 /* we get here when the connection got interrupted */
3799 gst_rtsp_message_unset (&message);
3800 GST_DEBUG_OBJECT (src, "got interrupted: stop connection flush");
3801 gst_rtspsrc_connection_flush (src, FALSE);
3802 return GST_FLOW_WRONG_STATE;
3806 gchar *str = gst_rtsp_strresult (res);
3809 src->conninfo.connected = FALSE;
3810 if (res != GST_RTSP_EINTR) {
3811 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
3812 ("Could not connect to server. (%s)", str));
3814 ret = GST_FLOW_ERROR;
3816 ret = GST_FLOW_WRONG_STATE;
3822 gchar *str = gst_rtsp_strresult (res);
3824 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
3825 ("Could not receive message. (%s)", str));
3827 return GST_FLOW_ERROR;
3829 handle_request_failed:
3831 gchar *str = gst_rtsp_strresult (res);
3834 gst_rtsp_message_unset (&message);
3835 if (res != GST_RTSP_EINTR) {
3836 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
3837 ("Could not handle server message. (%s)", str));
3839 ret = GST_FLOW_ERROR;
3841 ret = GST_FLOW_WRONG_STATE;
3847 GST_DEBUG_OBJECT (src, "we got an eof from the server");
3848 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3849 ("The server closed the connection."));
3850 src->conninfo.connected = FALSE;
3851 gst_rtsp_message_unset (&message);
3852 return GST_FLOW_UNEXPECTED;
3856 static GstRTSPResult
3857 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
3859 GstRTSPResult res = GST_RTSP_OK;
3862 GST_DEBUG_OBJECT (src, "doing reconnect");
3864 GST_OBJECT_LOCK (src);
3865 /* only restart when the pads were not yet activated, else we were
3866 * streaming over UDP */
3867 restart = src->need_activate;
3868 GST_OBJECT_UNLOCK (src);
3870 /* no need to restart, we're done */
3874 /* we can try only TCP now */
3875 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
3877 /* close and cleanup our state */
3878 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
3881 /* see if we have TCP left to try. Also don't try TCP when we were configured
3883 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
3886 /* We post a warning message now to inform the user
3887 * that nothing happened. It's most likely a firewall thing. */
3888 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3889 ("Could not receive any UDP packets for %.4f seconds, maybe your "
3890 "firewall is blocking it. Retrying using a TCP connection.",
3891 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
3893 /* open new connection using tcp */
3894 if (gst_rtspsrc_open (src, async) < 0)
3897 /* start playback */
3898 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
3907 src->cur_protocols = 0;
3908 /* no transport possible, post an error and stop */
3909 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
3910 ("Could not receive any UDP packets for %.4f seconds, maybe your "
3911 "firewall is blocking it. No other protocols to try.",
3912 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
3913 return GST_FLOW_ERROR;
3917 GST_DEBUG_OBJECT (src, "open failed");
3922 GST_DEBUG_OBJECT (src, "play failed");
3928 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
3932 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
3935 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
3938 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
3941 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
3949 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
3953 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
3956 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
3959 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
3962 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
3970 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
3974 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
3977 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
3980 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
3983 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
3991 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
3995 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
3998 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4001 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4004 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4012 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4014 if (ret == GST_RTSP_OK)
4015 gst_rtspsrc_loop_complete_cmd (src, cmd);
4016 else if (ret == GST_RTSP_EINTR)
4017 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4019 gst_rtspsrc_loop_error_cmd (src, cmd);
4023 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gboolean flush)
4027 /* FIXME flush param mute; remove at discretion */
4029 /* start new request */
4030 gst_rtspsrc_loop_start_cmd (src, cmd);
4032 GST_OBJECT_LOCK (src);
4033 old = src->loop_cmd;
4034 if (old != CMD_WAIT) {
4035 src->loop_cmd = CMD_WAIT;
4036 GST_OBJECT_UNLOCK (src);
4037 /* cancel previous request */
4038 gst_rtspsrc_loop_cancel_cmd (src, old);
4039 GST_OBJECT_LOCK (src);
4041 src->loop_cmd = cmd;
4042 /* interrupt if allowed */
4044 GST_DEBUG_OBJECT (src, "start connection flush");
4045 gst_rtspsrc_connection_flush (src, TRUE);
4048 gst_task_start (src->task);
4049 GST_OBJECT_UNLOCK (src);
4053 gst_rtspsrc_loop (GstRTSPSrc * src)
4057 if (!src->conninfo.connection || !src->conninfo.connected)
4060 if (src->interleaved)
4061 ret = gst_rtspsrc_loop_interleaved (src);
4063 ret = gst_rtspsrc_loop_udp (src);
4065 if (ret != GST_FLOW_OK)
4073 GST_WARNING_OBJECT (src, "we are not connected");
4074 ret = GST_FLOW_WRONG_STATE;
4079 const gchar *reason = gst_flow_get_name (ret);
4081 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
4082 src->running = FALSE;
4083 if (ret == GST_FLOW_UNEXPECTED) {
4084 /* perform EOS logic */
4085 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4086 gst_element_post_message (GST_ELEMENT_CAST (src),
4087 gst_message_new_segment_done (GST_OBJECT_CAST (src),
4088 src->segment.format, src->segment.position));
4090 gst_rtspsrc_push_event (src, gst_event_new_eos (), FALSE);
4092 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
4093 /* for fatal errors we post an error message, post the error before the
4094 * EOS so the app knows about the error first. */
4095 GST_ELEMENT_ERROR (src, STREAM, FAILED,
4096 ("Internal data flow error."),
4097 ("streaming task paused, reason %s (%d)", reason, ret));
4098 gst_rtspsrc_push_event (src, gst_event_new_eos (), FALSE);
4104 #ifndef GST_DISABLE_GST_DEBUG
4105 static const gchar *
4106 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
4110 while (method != 0) {
4127 static const gchar *
4128 gst_rtspsrc_skip_lws (const gchar * s)
4130 while (g_ascii_isspace (*s))
4135 static const gchar *
4136 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
4138 while (s > start && g_ascii_isspace (*(s - 1)))
4143 static const gchar *
4144 gst_rtspsrc_skip_commas (const gchar * s)
4146 /* The grammar allows for multiple commas */
4147 while (g_ascii_isspace (*s) || *s == ',')
4152 static const gchar *
4153 gst_rtspsrc_skip_item (const gchar * s)
4155 gboolean quoted = FALSE;
4156 const gchar *start = s;
4158 /* A list item ends at the last non-whitespace character
4159 * before a comma which is not inside a quoted-string. Or at
4160 * the end of the string.
4166 if (*s == '\\' && *(s + 1))
4175 return gst_rtspsrc_unskip_lws (s, start);
4179 gst_rtsp_decode_quoted_string (gchar * quoted_string)
4183 src = quoted_string + 1;
4184 dst = quoted_string;
4185 while (*src && *src != '"') {
4186 if (*src == '\\' && *(src + 1))
4193 /* Extract the authentication tokens that the server provided for each method
4194 * into an array of structures and give those to the connection object.
4197 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
4198 const gchar * header, gboolean * stale)
4200 GSList *list = NULL, *iter;
4202 gchar *item, *eq, *name_end, *value;
4204 g_return_if_fail (stale != NULL);
4206 gst_rtsp_connection_clear_auth_params (conn);
4209 /* Parse a header whose content is described by RFC2616 as
4210 * "#something", where "something" does not itself contain commas,
4211 * except as part of quoted-strings, into a list of allocated strings.
4213 header = gst_rtspsrc_skip_commas (header);
4215 end = gst_rtspsrc_skip_item (header);
4216 list = g_slist_prepend (list, g_strndup (header, end - header));
4217 header = gst_rtspsrc_skip_commas (end);
4222 list = g_slist_reverse (list);
4223 for (iter = list; iter; iter = iter->next) {
4226 eq = strchr (item, '=');
4228 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
4229 if (name_end == item) {
4230 /* That's no good... */
4237 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
4239 gst_rtsp_decode_quoted_string (value);
4243 if ((strcmp (item, "stale") == 0) && (strcmp (value, "TRUE") == 0))
4245 gst_rtsp_connection_set_auth_param (conn, item, value);
4249 g_slist_free (list);
4252 /* Parse a WWW-Authenticate Response header and determine the
4253 * available authentication methods
4255 * This code should also cope with the fact that each WWW-Authenticate
4256 * header can contain multiple challenge methods + tokens
4258 * At the moment, for Basic auth, we just do a minimal check and don't
4259 * even parse out the realm */
4261 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
4262 GstRTSPConnection * conn, gboolean * stale)
4266 g_return_if_fail (hdr != NULL);
4267 g_return_if_fail (methods != NULL);
4268 g_return_if_fail (stale != NULL);
4270 /* Skip whitespace at the start of the string */
4271 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
4273 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
4274 *methods |= GST_RTSP_AUTH_BASIC;
4275 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
4276 *methods |= GST_RTSP_AUTH_DIGEST;
4277 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
4282 * gst_rtspsrc_setup_auth:
4283 * @src: the rtsp source
4285 * Configure a username and password and auth method on the
4286 * connection object based on a response we received from the
4289 * Currently, this requires that a username and password were supplied
4290 * in the uri. In the future, they may be requested on demand by sending
4291 * a message up the bus.
4293 * Returns: TRUE if authentication information could be set up correctly.
4296 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
4300 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
4301 GstRTSPAuthMethod method;
4302 GstRTSPResult auth_result;
4304 GstRTSPConnection *conn;
4306 gboolean stale = FALSE;
4308 conn = src->conninfo.connection;
4310 /* Identify the available auth methods and see if any are supported */
4311 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
4312 &hdr, 0) == GST_RTSP_OK) {
4313 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
4316 if (avail_methods == GST_RTSP_AUTH_NONE)
4317 goto no_auth_available;
4319 /* For digest auth, if the response indicates that the session
4320 * data are stale, we just update them in the connection object and
4321 * return TRUE to retry the request */
4323 src->tried_url_auth = FALSE;
4325 url = gst_rtsp_connection_get_url (conn);
4327 /* Do we have username and password available? */
4328 if (url != NULL && !src->tried_url_auth && url->user != NULL
4329 && url->passwd != NULL) {
4332 src->tried_url_auth = TRUE;
4333 GST_DEBUG_OBJECT (src,
4334 "Attempting authentication using credentials from the URL");
4336 user = src->user_id;
4337 pass = src->user_pw;
4338 GST_DEBUG_OBJECT (src,
4339 "Attempting authentication using credentials from the properties");
4342 /* FIXME: If the url didn't contain username and password or we tried them
4343 * already, request a username and passwd from the application via some kind
4344 * of credentials request message */
4346 /* If we don't have a username and passwd at this point, bail out. */
4347 if (user == NULL || pass == NULL)
4350 /* Try to configure for each available authentication method, strongest to
4352 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
4353 /* Check if this method is available on the server */
4354 if ((method & avail_methods) == 0)
4357 /* Pass the credentials to the connection to try on the next request */
4358 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
4359 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
4360 * ignore it and end up retrying later */
4361 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
4362 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
4363 gst_rtsp_auth_method_to_string (method));
4368 if (method == GST_RTSP_AUTH_NONE)
4369 goto no_auth_available;
4375 /* Output an error indicating that we couldn't connect because there were
4376 * no supported authentication protocols */
4377 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4378 ("No supported authentication protocol was found"));
4383 /* We don't fire an error message, we just return FALSE and let the
4384 * normal NOT_AUTHORIZED error be propagated */
4389 static GstRTSPResult
4390 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4391 GstRTSPMessage * request, GstRTSPMessage * response,
4392 GstRTSPStatusCode * code)
4395 GstRTSPStatusCode thecode;
4396 gchar *content_base = NULL;
4400 gst_rtsp_ext_list_before_send (src->extensions, request);
4402 GST_DEBUG_OBJECT (src, "sending message");
4405 gst_rtsp_message_dump (request);
4407 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
4411 gst_rtsp_connection_reset_timeout (conn);
4414 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
4419 gst_rtsp_message_dump (response);
4421 switch (response->type) {
4422 case GST_RTSP_MESSAGE_REQUEST:
4423 res = gst_rtspsrc_handle_request (src, conn, response);
4424 if (res == GST_RTSP_EEOF)
4427 goto handle_request_failed;
4429 case GST_RTSP_MESSAGE_RESPONSE:
4430 /* ok, a response is good */
4431 GST_DEBUG_OBJECT (src, "received response message");
4433 case GST_RTSP_MESSAGE_DATA:
4434 /* get next response */
4435 GST_DEBUG_OBJECT (src, "ignoring data response message");
4438 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4443 thecode = response->type_data.response.code;
4445 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
4447 /* if the caller wanted the result code, we store it. */
4451 /* If the request didn't succeed, bail out before doing any more */
4452 if (thecode != GST_RTSP_STS_OK)
4455 /* store new content base if any */
4456 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
4459 g_free (src->content_base);
4460 src->content_base = g_strdup (content_base);
4462 gst_rtsp_ext_list_after_send (src->extensions, request, response);
4469 gchar *str = gst_rtsp_strresult (res);
4471 if (res != GST_RTSP_EINTR) {
4472 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4473 ("Could not send message. (%s)", str));
4475 GST_WARNING_OBJECT (src, "send interrupted");
4484 GST_WARNING_OBJECT (src, "server closed connection, doing reconnect");
4487 /* if reconnect succeeds, try again */
4489 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
4493 /* only try once after reconnect, then fallthrough and error out */
4496 gchar *str = gst_rtsp_strresult (res);
4498 if (res != GST_RTSP_EINTR) {
4499 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4500 ("Could not receive message. (%s)", str));
4502 GST_WARNING_OBJECT (src, "receive interrupted");
4510 handle_request_failed:
4512 /* ERROR was posted */
4513 gst_rtsp_message_unset (response);
4518 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4519 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4520 ("The server closed the connection."));
4521 gst_rtsp_message_unset (response);
4528 * @src: the rtsp source
4529 * @conn: the connection to send on
4530 * @request: must point to a valid request
4531 * @response: must point to an empty #GstRTSPMessage
4532 * @code: an optional code result
4534 * send @request and retrieve the response in @response. optionally @code can be
4535 * non-NULL in which case it will contain the status code of the response.
4537 * If This function returns #GST_RTSP_OK, @response will contain a valid response
4538 * message that should be cleaned with gst_rtsp_message_unset() after usage.
4540 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
4541 * @response message) if the response code was not 200 (OK).
4543 * If the attempt results in an authentication failure, then this will attempt
4544 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
4547 * Returns: #GST_RTSP_OK if the processing was successful.
4549 static GstRTSPResult
4550 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4551 GstRTSPMessage * request, GstRTSPMessage * response,
4552 GstRTSPStatusCode * code)
4554 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
4555 GstRTSPResult res = GST_RTSP_ERROR;
4558 GstRTSPMethod method = GST_RTSP_INVALID;
4564 /* make sure we don't loop forever */
4568 /* save method so we can disable it when the server complains */
4569 method = request->type_data.request.method;
4572 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
4576 case GST_RTSP_STS_UNAUTHORIZED:
4577 if (gst_rtspsrc_setup_auth (src, response)) {
4578 /* Try the request/response again after configuring the auth info
4586 } while (retry == TRUE);
4588 /* If the user requested the code, let them handle errors, otherwise
4589 * post an error below */
4592 else if (int_code != GST_RTSP_STS_OK)
4593 goto error_response;
4600 GST_DEBUG_OBJECT (src, "got error %d", res);
4605 res = GST_RTSP_ERROR;
4607 switch (response->type_data.response.code) {
4608 case GST_RTSP_STS_NOT_FOUND:
4609 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
4610 response->type_data.response.reason));
4612 case GST_RTSP_STS_MOVED_PERMANENTLY:
4613 case GST_RTSP_STS_MOVE_TEMPORARILY:
4615 gchar *new_location;
4616 GstRTSPLowerTrans transports;
4618 GST_DEBUG_OBJECT (src, "got redirection");
4619 /* if we don't have a Location Header, we must error */
4620 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
4621 &new_location, 0) < 0)
4624 /* When we receive a redirect result, we go back to the INIT state after
4625 * parsing the new URI. The caller should do the needed steps to issue
4626 * a new setup when it detects this state change. */
4627 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
4629 /* save current transports */
4630 if (src->conninfo.url)
4631 transports = src->conninfo.url->transports;
4633 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
4635 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location);
4637 /* set old transports */
4638 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
4639 src->conninfo.url->transports = transports;
4641 src->need_redirect = TRUE;
4642 src->state = GST_RTSP_STATE_INIT;
4646 case GST_RTSP_STS_NOT_ACCEPTABLE:
4647 case GST_RTSP_STS_NOT_IMPLEMENTED:
4648 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
4649 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
4650 gst_rtsp_method_as_text (method));
4651 src->methods &= ~method;
4655 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4656 ("Got error response: %d (%s).", response->type_data.response.code,
4657 response->type_data.response.reason));
4660 /* if we return ERROR we should unset the response ourselves */
4661 if (res == GST_RTSP_ERROR)
4662 gst_rtsp_message_unset (response);
4668 static GstRTSPResult
4669 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
4670 GstRTSPMessage * response, GstRTSPSrc * src)
4672 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
4677 /* parse the response and collect all the supported methods. We need this
4678 * information so that we don't try to send an unsupported request to the
4682 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
4684 GstRTSPHeaderField field;
4690 /* reset supported methods */
4693 /* Try Allow Header first */
4694 field = GST_RTSP_HDR_ALLOW;
4697 gst_rtsp_message_get_header (response, field, &respoptions, indx);
4698 if (indx == 0 && !respoptions) {
4699 /* if no Allow header was found then try the Public header... */
4700 field = GST_RTSP_HDR_PUBLIC;
4701 gst_rtsp_message_get_header (response, field, &respoptions, indx);
4706 /* If we get here, the server gave a list of supported methods, parse
4707 * them here. The string is like:
4709 * OPTIONS, DESCRIBE, ANNOUNCE, PLAY, SETUP, ...
4711 options = g_strsplit (respoptions, ",", 0);
4713 for (i = 0; options[i]; i++) {
4717 stripped = g_strstrip (options[i]);
4718 method = gst_rtsp_find_method (stripped);
4720 /* keep bitfield of supported methods */
4721 if (method != GST_RTSP_INVALID)
4722 src->methods |= method;
4724 g_strfreev (options);
4729 if (src->methods == 0) {
4730 /* neither Allow nor Public are required, assume the server supports
4731 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
4733 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
4734 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
4736 /* always assume PLAY, FIXME, extensions should be able to override
4738 src->methods |= GST_RTSP_PLAY;
4739 /* also assume it will support Range */
4740 src->seekable = TRUE;
4742 /* we need describe and setup */
4743 if (!(src->methods & GST_RTSP_DESCRIBE))
4745 if (!(src->methods & GST_RTSP_SETUP))
4753 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4754 ("Server does not support DESCRIBE."));
4759 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4760 ("Server does not support SETUP."));
4765 /* masks to be kept in sync with the hardcoded protocol order of preference
4767 static guint protocol_masks[] = {
4768 GST_RTSP_LOWER_TRANS_UDP,
4769 GST_RTSP_LOWER_TRANS_UDP_MCAST,
4770 GST_RTSP_LOWER_TRANS_TCP,
4774 static GstRTSPResult
4775 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
4776 GstRTSPLowerTrans protocols, gchar ** transports)
4780 gboolean add_udp_str;
4785 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
4790 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
4792 /* extension listed transports, use those */
4793 if (*transports != NULL)
4796 /* it's the default */
4797 add_udp_str = FALSE;
4799 /* the default RTSP transports */
4800 result = g_string_new ("");
4801 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
4802 GST_DEBUG_OBJECT (src, "adding UDP unicast");
4804 g_string_append (result, "RTP/AVP");
4806 g_string_append (result, "/UDP");
4807 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
4808 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
4809 GST_DEBUG_OBJECT (src, "adding UDP multicast");
4811 /* we don't have to allocate any UDP ports yet, if the selected transport
4812 * turns out to be multicast we can create them and join the multicast
4813 * group indicated in the transport reply */
4814 if (result->len > 0)
4815 g_string_append (result, ",");
4816 g_string_append (result, "RTP/AVP");
4818 g_string_append (result, "/UDP");
4819 g_string_append (result, ";multicast");
4820 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
4821 GST_DEBUG_OBJECT (src, "adding TCP");
4823 if (result->len > 0)
4824 g_string_append (result, ",");
4825 g_string_append (result, "RTP/AVP/TCP;unicast;interleaved=%%i1-%%i2");
4827 *transports = g_string_free (result, FALSE);
4829 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
4840 static GstRTSPResult
4841 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
4842 gint orig_rtpport, gint orig_rtcpport)
4845 gint nr_udp, nr_int;
4847 gint rtpport = 0, rtcpport = 0;
4850 src = stream->parent;
4852 /* find number of placeholders first */
4853 if (strstr (*transports, "%%i2"))
4855 else if (strstr (*transports, "%%i1"))
4860 if (strstr (*transports, "%%u2"))
4862 else if (strstr (*transports, "%%u1"))
4867 if (nr_udp == 0 && nr_int == 0)
4871 if (!orig_rtpport || !orig_rtcpport) {
4872 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
4875 rtpport = orig_rtpport;
4876 rtcpport = orig_rtcpport;
4880 str = g_string_new ("");
4882 while ((next = strstr (p, "%%"))) {
4883 g_string_append_len (str, p, next - p);
4884 if (next[2] == 'u') {
4886 g_string_append_printf (str, "%d", rtpport);
4887 else if (next[3] == '2')
4888 g_string_append_printf (str, "%d", rtcpport);
4890 if (next[2] == 'i') {
4892 g_string_append_printf (str, "%d", src->free_channel);
4893 else if (next[3] == '2')
4894 g_string_append_printf (str, "%d", src->free_channel + 1);
4899 /* append final part */
4900 g_string_append (str, p);
4902 g_free (*transports);
4903 *transports = g_string_free (str, FALSE);
4911 return GST_RTSP_ERROR;
4916 gst_rtspsrc_stream_is_real_media (GstRTSPStream * stream)
4918 gboolean res = FALSE;
4922 const gchar *enc = NULL;
4924 s = gst_caps_get_structure (stream->caps, 0);
4925 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
4926 res = (strstr (enc, "-REAL") != NULL);
4932 /* Perform the SETUP request for all the streams.
4934 * We ask the server for a specific transport, which initially includes all the
4935 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
4936 * two local UDP ports that we send to the server.
4938 * Once the server replied with a transport, we configure the other streams
4939 * with the same transport.
4941 * This function will also configure the stream for the selected transport,
4942 * which basically means creating the pipeline.
4944 static GstRTSPResult
4945 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
4949 GstRTSPMessage request = { 0 };
4950 GstRTSPMessage response = { 0 };
4951 GstRTSPStream *stream = NULL;
4952 GstRTSPLowerTrans protocols;
4953 GstRTSPStatusCode code;
4954 gboolean unsupported_real = FALSE;
4955 gint rtpport, rtcpport;
4959 if (src->conninfo.connection) {
4960 url = gst_rtsp_connection_get_url (src->conninfo.connection);
4961 /* we initially allow all configured lower transports. based on the URL
4962 * transports and the replies from the server we narrow them down. */
4963 protocols = url->transports & src->cur_protocols;
4966 protocols = src->cur_protocols;
4972 /* reset some state */
4973 src->free_channel = 0;
4974 src->interleaved = FALSE;
4975 src->need_activate = FALSE;
4976 /* keep track of next port number, 0 is random */
4977 src->next_port_num = src->client_port_range.min;
4978 rtpport = rtcpport = 0;
4980 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4981 GstRTSPConnection *conn;
4986 stream = (GstRTSPStream *) walk->data;
4988 /* see if we need to configure this stream */
4989 if (!gst_rtsp_ext_list_configure_stream (src->extensions, stream->caps)) {
4990 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
4992 stream->disabled = TRUE;
4996 /* merge/overwrite global caps */
5001 s = gst_caps_get_structure (stream->caps, 0);
5003 num = gst_structure_n_fields (src->props);
5004 for (j = 0; j < num; j++) {
5008 name = gst_structure_nth_field_name (src->props, j);
5009 val = gst_structure_get_value (src->props, name);
5010 gst_structure_set_value (s, name, val);
5012 GST_DEBUG_OBJECT (src, "copied %s", name);
5016 /* skip setup if we have no URL for it */
5017 if (stream->conninfo.location == NULL) {
5018 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
5022 if (src->conninfo.connection == NULL) {
5023 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
5024 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
5027 conn = stream->conninfo.connection;
5029 conn = src->conninfo.connection;
5031 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
5032 stream->conninfo.location);
5034 /* if we have a multicast connection, only suggest multicast from now on */
5035 if (stream->is_multicast)
5036 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
5039 /* first selectable protocol */
5040 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5042 if (!protocol_masks[mask])
5046 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
5047 protocol_masks[mask]);
5048 /* create a string with first transport in line */
5050 res = gst_rtspsrc_create_transports_string (src,
5051 protocols & protocol_masks[mask], &transports);
5052 if (res < 0 || transports == NULL)
5053 goto setup_transport_failed;
5055 if (strlen (transports) == 0) {
5056 g_free (transports);
5057 GST_DEBUG_OBJECT (src, "no transports found");
5062 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
5064 /* replace placeholders with real values, this function will optionally
5065 * allocate UDP ports and other info needed to execute the setup request */
5066 res = gst_rtspsrc_prepare_transports (stream, &transports,
5067 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
5069 g_free (transports);
5070 goto setup_transport_failed;
5073 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
5075 /* create SETUP request */
5077 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
5078 stream->conninfo.location);
5080 g_free (transports);
5081 goto create_request_failed;
5084 /* select transport, copy is made when adding to header so we can free it. */
5085 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
5086 g_free (transports);
5088 /* if the user wants a non default RTP packet size we add the blocksize
5090 if (src->rtp_blocksize > 0) {
5091 hval = g_strdup_printf ("%d", src->rtp_blocksize);
5092 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
5097 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
5100 /* handle the code ourselves */
5101 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
5105 case GST_RTSP_STS_OK:
5107 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
5108 gst_rtsp_message_unset (&request);
5109 gst_rtsp_message_unset (&response);
5110 /* cleanup of leftover transport */
5111 gst_rtspsrc_stream_free_udp (stream);
5112 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
5113 * we might be in this case */
5114 if (stream->container && rtpport && rtcpport && !retry) {
5115 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
5120 /* this transport did not go down well, but we may have others to try
5121 * that we did not send yet, try those and only give up then
5122 * but not without checking for lost cause/extension so we can
5123 * post a nicer/more useful error message later */
5124 if (!unsupported_real)
5125 unsupported_real = gst_rtspsrc_stream_is_real_media (stream);
5126 /* select next available protocol, give up on this stream if none */
5128 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5130 if (!protocol_masks[mask] || unsupported_real)
5135 /* cleanup of leftover transport and move to the next stream */
5136 gst_rtspsrc_stream_free_udp (stream);
5137 goto response_error;
5140 /* parse response transport */
5142 gchar *resptrans = NULL;
5143 GstRTSPTransport transport = { 0 };
5145 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
5148 gst_rtspsrc_stream_free_udp (stream);
5152 /* parse transport, go to next stream on parse error */
5153 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
5154 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
5158 /* update allowed transports for other streams. once the transport of
5159 * one stream has been determined, we make sure that all other streams
5160 * are configured in the same way */
5161 switch (transport.lower_transport) {
5162 case GST_RTSP_LOWER_TRANS_TCP:
5163 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
5164 protocols = GST_RTSP_LOWER_TRANS_TCP;
5165 src->interleaved = TRUE;
5166 /* update free channels */
5168 MAX (transport.interleaved.min, src->free_channel);
5170 MAX (transport.interleaved.max, src->free_channel);
5171 src->free_channel++;
5173 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
5174 /* only allow multicast for other streams */
5175 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
5176 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
5178 case GST_RTSP_LOWER_TRANS_UDP:
5179 /* only allow unicast for other streams */
5180 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
5181 protocols = GST_RTSP_LOWER_TRANS_UDP;
5184 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
5185 transport.lower_transport);
5189 if (!stream->container || (!src->interleaved && !retry)) {
5190 /* now configure the stream with the selected transport */
5191 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
5192 GST_DEBUG_OBJECT (src,
5193 "could not configure stream %p transport, skipping stream",
5196 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
5197 /* retain the first allocated UDP port pair */
5198 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
5199 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
5202 /* we need to activate at least one streams when we detect activity */
5203 src->need_activate = TRUE;
5205 /* clean up our transport struct */
5206 gst_rtsp_transport_init (&transport);
5207 /* clean up used RTSP messages */
5208 gst_rtsp_message_unset (&request);
5209 gst_rtsp_message_unset (&response);
5213 /* store the transport protocol that was configured */
5214 src->cur_protocols = protocols;
5216 gst_rtsp_ext_list_stream_select (src->extensions, url);
5218 /* if there is nothing to activate, error out */
5219 if (!src->need_activate)
5220 goto nothing_to_activate;
5227 /* no transport possible, post an error and stop */
5228 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5229 ("Could not connect to server, no protocols left"));
5230 return GST_RTSP_ERROR;
5232 create_request_failed:
5234 gchar *str = gst_rtsp_strresult (res);
5236 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5237 ("Could not create request. (%s)", str));
5241 setup_transport_failed:
5243 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5244 ("Could not setup transport."));
5245 res = GST_RTSP_ERROR;
5250 const gchar *str = gst_rtsp_status_as_text (code);
5252 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5253 ("Error (%d): %s", code, GST_STR_NULL (str)));
5254 res = GST_RTSP_ERROR;
5259 gchar *str = gst_rtsp_strresult (res);
5261 if (res != GST_RTSP_EINTR) {
5262 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5263 ("Could not send message. (%s)", str));
5265 GST_WARNING_OBJECT (src, "send interrupted");
5272 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5273 ("Server did not select transport."));
5274 res = GST_RTSP_ERROR;
5277 nothing_to_activate:
5279 /* none of the available error codes is really right .. */
5280 if (unsupported_real) {
5281 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5282 (_("No supported stream was found. You might need to install a "
5283 "GStreamer RTSP extension plugin for Real media streams.")),
5286 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5287 (_("No supported stream was found. You might need to allow "
5288 "more transport protocols or may otherwise be missing "
5289 "the right GStreamer RTSP extension plugin.")), (NULL));
5291 return GST_RTSP_ERROR;
5295 gst_rtsp_message_unset (&request);
5296 gst_rtsp_message_unset (&response);
5302 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
5303 GstSegment * segment)
5306 GstRTSPTimeRange *therange;
5309 gst_rtsp_range_free (src->range);
5311 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
5312 GST_DEBUG_OBJECT (src, "parsed range %s", range);
5313 src->range = therange;
5315 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
5317 gst_segment_init (segment, GST_FORMAT_TIME);
5321 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
5322 therange->min.type, therange->min.seconds, therange->max.type,
5323 therange->max.seconds);
5325 if (therange->min.type == GST_RTSP_TIME_NOW)
5327 else if (therange->min.type == GST_RTSP_TIME_END)
5330 seconds = therange->min.seconds * GST_SECOND;
5332 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
5333 GST_TIME_ARGS (seconds));
5335 /* we need to start playback without clipping from the position reported by
5337 segment->start = seconds;
5338 segment->position = seconds;
5340 if (therange->max.type == GST_RTSP_TIME_NOW)
5342 else if (therange->max.type == GST_RTSP_TIME_END)
5345 seconds = therange->max.seconds * GST_SECOND;
5347 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
5348 GST_TIME_ARGS (seconds));
5350 /* live (WMS) server might send overflowed large max as its idea of infinity,
5351 * compensate to prevent problems later on */
5352 if (seconds != -1 && seconds < 0) {
5354 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
5357 /* live (WMS) might send min == max, which is not worth recording */
5358 if (segment->duration == -1 && seconds == segment->start)
5361 /* don't change duration with unknown value, we might have a valid value
5362 * there that we want to keep. */
5364 segment->duration = seconds;
5369 /* must be called with the RTSP state lock */
5370 static GstRTSPResult
5371 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
5377 /* prepare global stream caps properties */
5379 gst_structure_remove_all_fields (src->props);
5381 src->props = gst_structure_empty_new ("RTSPProperties");
5384 gst_sdp_message_dump (sdp);
5386 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
5388 gst_segment_init (&src->segment, GST_FORMAT_TIME);
5390 /* parse range for duration reporting. */
5395 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
5399 /* keep track of the range and configure it in the segment */
5400 if (gst_rtspsrc_parse_range (src, range, &src->segment))
5404 /* try to find a global control attribute. Note that a '*' means that we should
5405 * do aggregate control with the current url (so we don't do anything and
5406 * leave the current connection as is) */
5408 const gchar *control;
5411 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
5412 if (control == NULL)
5415 /* only take fully qualified urls */
5416 if (g_str_has_prefix (control, "rtsp://"))
5420 g_free (src->conninfo.location);
5421 src->conninfo.location = g_strdup (control);
5422 /* make a connection for this, if there was a connection already, nothing
5424 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
5425 GST_ERROR_OBJECT (src, "could not connect");
5428 /* we need to keep the control url separate from the connection url because
5429 * the rules for constructing the media control url need it */
5430 g_free (src->control);
5431 src->control = g_strdup (control);
5434 /* create streams */
5435 n_streams = gst_sdp_message_medias_len (sdp);
5436 for (i = 0; i < n_streams; i++) {
5437 gst_rtspsrc_create_stream (src, sdp, i);
5440 src->state = GST_RTSP_STATE_INIT;
5441 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_IS_SOURCE);
5444 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
5447 /* reset our state */
5448 src->need_range = TRUE;
5451 src->state = GST_RTSP_STATE_READY;
5458 GST_ERROR_OBJECT (src, "setup failed");
5463 static GstRTSPResult
5464 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
5468 GstRTSPMessage request = { 0 };
5469 GstRTSPMessage response = { 0 };
5472 gchar *respcont = NULL;
5475 src->need_redirect = FALSE;
5477 /* can't continue without a valid url */
5478 if (G_UNLIKELY (src->conninfo.url == NULL)) {
5479 res = GST_RTSP_EINVAL;
5482 src->tried_url_auth = FALSE;
5484 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
5485 goto connect_failed;
5487 /* create OPTIONS */
5488 GST_DEBUG_OBJECT (src, "create options...");
5490 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
5491 src->conninfo.url_str);
5493 goto create_request_failed;
5496 GST_DEBUG_OBJECT (src, "send options...");
5499 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
5502 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
5507 if (!gst_rtspsrc_parse_methods (src, &response))
5510 /* create DESCRIBE */
5511 GST_DEBUG_OBJECT (src, "create describe...");
5513 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
5514 src->conninfo.url_str);
5516 goto create_request_failed;
5518 /* we only accept SDP for now */
5519 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
5523 GST_DEBUG_OBJECT (src, "send describe...");
5526 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
5529 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
5533 /* we only perform redirect for the describe, currently */
5534 if (src->need_redirect) {
5535 /* close connection, we don't have to send a TEARDOWN yet, ignore the
5537 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5539 gst_rtsp_message_unset (&request);
5540 gst_rtsp_message_unset (&response);
5546 /* it could be that the DESCRIBE method was not implemented */
5547 if (!src->methods & GST_RTSP_DESCRIBE)
5550 /* check if reply is SDP */
5551 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
5553 /* could not be set but since the request returned OK, we assume it
5554 * was SDP, else check it. */
5556 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
5557 goto wrong_content_type;
5560 /* get message body and parse as SDP */
5561 gst_rtsp_message_get_body (&response, &data, &size);
5562 if (data == NULL || size == 0)
5565 GST_DEBUG_OBJECT (src, "parse SDP...");
5566 gst_sdp_message_new (sdp);
5567 gst_sdp_message_parse_buffer (data, size, *sdp);
5569 /* clean up any messages */
5570 gst_rtsp_message_unset (&request);
5571 gst_rtsp_message_unset (&response);
5578 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
5579 ("No valid RTSP URL was provided"));
5584 gchar *str = gst_rtsp_strresult (res);
5586 if (res != GST_RTSP_EINTR) {
5587 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
5588 ("Failed to connect. (%s)", str));
5590 GST_WARNING_OBJECT (src, "connect interrupted");
5595 create_request_failed:
5597 gchar *str = gst_rtsp_strresult (res);
5599 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5600 ("Could not create request. (%s)", str));
5606 /* Don't post a message - the rtsp_send method will have
5607 * taken care of it because we passed NULL for the response code */
5612 /* error was posted */
5613 res = GST_RTSP_ERROR;
5618 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5619 ("Server does not support SDP, got %s.", respcont));
5620 res = GST_RTSP_ERROR;
5625 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5626 ("Server can not provide an SDP."));
5627 res = GST_RTSP_ERROR;
5632 if (src->conninfo.connection) {
5633 GST_DEBUG_OBJECT (src, "free connection");
5634 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5636 gst_rtsp_message_unset (&request);
5637 gst_rtsp_message_unset (&response);
5642 static GstRTSPResult
5643 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
5648 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
5650 if (src->sdp == NULL) {
5651 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
5655 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
5660 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
5667 GST_WARNING_OBJECT (src, "can't get sdp");
5668 src->open_error = TRUE;
5673 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
5674 src->open_error = TRUE;
5679 static GstRTSPResult
5680 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
5682 GstRTSPMessage request = { 0 };
5683 GstRTSPMessage response = { 0 };
5684 GstRTSPResult res = GST_RTSP_OK;
5688 GST_DEBUG_OBJECT (src, "TEARDOWN...");
5690 if (src->state < GST_RTSP_STATE_READY) {
5691 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
5698 /* construct a control url */
5700 control = src->control;
5702 control = src->conninfo.url_str;
5704 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
5707 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5708 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5710 GstRTSPConnInfo *info;
5712 /* try aggregate control first but do non-aggregate control otherwise */
5714 setup_url = control;
5715 else if ((setup_url = stream->conninfo.location) == NULL)
5718 if (src->conninfo.connection) {
5719 info = &src->conninfo;
5720 } else if (stream->conninfo.connection) {
5721 info = &stream->conninfo;
5725 if (!info->connected)
5730 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
5732 goto create_request_failed;
5735 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
5738 gst_rtspsrc_send (src, info->connection, &request, &response,
5742 /* FIXME, parse result? */
5743 gst_rtsp_message_unset (&request);
5744 gst_rtsp_message_unset (&response);
5747 /* early exit when we did aggregate control */
5753 /* close connections */
5754 GST_DEBUG_OBJECT (src, "closing connection...");
5755 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5756 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5757 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5758 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
5762 gst_rtspsrc_cleanup (src);
5764 src->state = GST_RTSP_STATE_INVALID;
5767 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
5772 create_request_failed:
5774 gchar *str = gst_rtsp_strresult (res);
5776 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5777 ("Could not create request. (%s)", str));
5783 gchar *str = gst_rtsp_strresult (res);
5785 gst_rtsp_message_unset (&request);
5786 if (res != GST_RTSP_EINTR) {
5787 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5788 ("Could not send message. (%s)", str));
5790 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
5797 GST_DEBUG_OBJECT (src,
5798 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
5803 /* RTP-Info is of the format:
5805 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
5807 * rtptime corresponds to the timestamp for the NPT time given in the header
5808 * seqbase corresponds to the next sequence number we received. This number
5809 * indicates the first seqnum after the seek and should be used to discard
5810 * packets that are from before the seek.
5813 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
5818 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
5820 infos = g_strsplit (rtpinfo, ",", 0);
5821 for (i = 0; infos[i]; i++) {
5823 GstRTSPStream *stream;
5827 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
5829 /* init values, types of seqbase and timebase are bigger than needed so we
5830 * can store -1 as uninitialized values */
5835 /* parse url, find stream for url.
5836 * parse seq and rtptime. The seq number should be configured in the rtp
5837 * depayloader or session manager to detect gaps. Same for the rtptime, it
5838 * should be used to create an initial time newsegment. */
5839 fields = g_strsplit (infos[i], ";", 0);
5840 for (j = 0; fields[j]; j++) {
5841 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
5842 /* remove leading whitespace */
5843 fields[j] = g_strchug (fields[j]);
5844 if (g_str_has_prefix (fields[j], "url=")) {
5845 /* get the url and the stream */
5847 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
5848 } else if (g_str_has_prefix (fields[j], "seq=")) {
5849 seqbase = atoi (fields[j] + 4);
5850 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
5851 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
5854 g_strfreev (fields);
5855 /* now we need to store the values for the caps of the stream */
5856 if (stream != NULL) {
5857 GST_DEBUG_OBJECT (src,
5858 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
5859 stream, seqbase, timebase);
5861 /* we have a stream, configure detected params */
5862 stream->seqbase = seqbase;
5863 stream->timebase = timebase;
5872 gst_rtspsrc_get_float (const gchar * dstr)
5874 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
5876 /* canonicalise floating point string so we can handle float strings
5877 * in the form "24.930" or "24,930" irrespective of the current locale */
5878 g_strlcpy (s, dstr, sizeof (s));
5879 g_strdelimit (s, ",", '.');
5880 return g_ascii_strtod (s, NULL);
5884 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
5886 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
5888 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
5889 g_strlcpy (val_str, "now", sizeof (val_str));
5891 if (segment->position == 0) {
5892 g_strlcpy (val_str, "0", sizeof (val_str));
5894 g_ascii_dtostr (val_str, sizeof (val_str),
5895 ((gdouble) segment->position) / GST_SECOND);
5898 return g_strdup_printf ("npt=%s-", val_str);
5902 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
5904 stream->timebase = -1;
5905 stream->seqbase = -1;
5909 stream->caps = gst_caps_make_writable (stream->caps);
5910 s = gst_caps_get_structure (stream->caps, 0);
5911 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
5915 static GstRTSPResult
5916 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
5918 GstRTSPResult res = GST_RTSP_OK;
5920 if (src->state < GST_RTSP_STATE_READY) {
5921 res = GST_RTSP_ERROR;
5922 if (src->open_error) {
5923 GST_DEBUG_OBJECT (src, "the stream was in error");
5927 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
5929 if ((res = gst_rtspsrc_open (src, async)) < 0) {
5930 GST_DEBUG_OBJECT (src, "failed to open stream");
5939 static GstRTSPResult
5940 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
5942 GstRTSPMessage request = { 0 };
5943 GstRTSPMessage response = { 0 };
5944 GstRTSPResult res = GST_RTSP_OK;
5950 GST_DEBUG_OBJECT (src, "PLAY...");
5952 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
5955 if (!(src->methods & GST_RTSP_PLAY))
5958 if (src->state == GST_RTSP_STATE_PLAYING)
5961 if (!src->conninfo.connection || !src->conninfo.connected)
5964 /* send some dummy packets before we activate the receive in the
5966 gst_rtspsrc_send_dummy_packets (src);
5968 /* activate receive elements;
5969 * only in async case, since receive elements may not have been affected
5970 * by overall state change (e.g. not around yet),
5971 * do not mess with state in sync case (e.g. seeking) */
5973 gst_element_set_state (GST_ELEMENT_CAST (src), GST_STATE_PLAYING);
5975 /* construct a control url */
5977 control = src->control;
5979 control = src->conninfo.url_str;
5981 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5982 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5984 GstRTSPConnection *conn;
5986 /* try aggregate control first but do non-aggregate control otherwise */
5988 setup_url = control;
5989 else if ((setup_url = stream->conninfo.location) == NULL)
5992 if (src->conninfo.connection) {
5993 conn = src->conninfo.connection;
5994 } else if (stream->conninfo.connection) {
5995 conn = stream->conninfo.connection;
6001 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
6003 goto create_request_failed;
6005 if (src->need_range) {
6006 hval = gen_range_header (src, segment);
6008 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_RANGE, hval);
6012 if (segment->rate != 1.0) {
6013 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
6015 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
6017 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
6019 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
6023 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
6025 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6028 /* seek may have silently failed as it is not supported */
6029 if (!(src->methods & GST_RTSP_PLAY)) {
6030 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
6031 /* obviously it is supported as we made it here */
6032 src->methods |= GST_RTSP_PLAY;
6033 src->seekable = FALSE;
6034 /* but there is nothing to parse in the response,
6035 * so convey we have no idea and not to expect anything particular */
6036 clear_rtp_base (src, stream);
6040 /* need to do for all streams */
6041 for (run = src->streams; run; run = g_list_next (run))
6042 clear_rtp_base (src, (GstRTSPStream *) run->data);
6044 /* NOTE the above also disables npt based eos detection */
6045 /* and below forces position to 0,
6046 * which is visible feedback we lost the plot */
6047 segment->start = segment->position = src->last_pos;
6050 gst_rtsp_message_unset (&request);
6052 /* parse RTP npt field. This is the current position in the stream (Normal
6053 * Play Time) and should be put in the NEWSEGMENT position field. */
6054 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
6056 gst_rtspsrc_parse_range (src, hval, segment);
6058 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
6059 segment->rate = 1.0;
6061 /* parse Speed header. This is the intended playback rate of the stream
6062 * and should be put in the NEWSEGMENT rate field. */
6063 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
6064 0) == GST_RTSP_OK) {
6065 segment->rate = gst_rtspsrc_get_float (hval);
6066 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
6067 &hval, 0) == GST_RTSP_OK) {
6068 segment->rate = gst_rtspsrc_get_float (hval);
6071 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
6072 * for the RTP packets. If this is not present, we assume all starts from 0...
6073 * This is info for the RTP session manager that we pass to it in caps. */
6075 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
6076 &hval, hval_idx++) == GST_RTSP_OK)
6077 gst_rtspsrc_parse_rtpinfo (src, hval);
6079 gst_rtsp_message_unset (&response);
6081 /* early exit when we did aggregate control */
6085 /* set again when needed */
6086 src->need_range = FALSE;
6088 /* configure the caps of the streams after we parsed all headers. */
6089 gst_rtspsrc_configure_caps (src, segment);
6091 src->running = TRUE;
6092 src->base_time = -1;
6093 src->state = GST_RTSP_STATE_PLAYING;
6096 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
6097 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6098 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6099 stream->discont = TRUE;
6104 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
6111 GST_DEBUG_OBJECT (src, "failed to open stream");
6116 GST_DEBUG_OBJECT (src, "PLAY is not supported");
6121 GST_DEBUG_OBJECT (src, "we were already PLAYING");
6124 create_request_failed:
6126 gchar *str = gst_rtsp_strresult (res);
6128 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6129 ("Could not create request. (%s)", str));
6135 gchar *str = gst_rtsp_strresult (res);
6137 gst_rtsp_message_unset (&request);
6138 if (res != GST_RTSP_EINTR) {
6139 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6140 ("Could not send message. (%s)", str));
6142 GST_WARNING_OBJECT (src, "PLAY interrupted");
6149 static GstRTSPResult
6150 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean idle, gboolean async)
6152 GstRTSPResult res = GST_RTSP_OK;
6153 GstRTSPMessage request = { 0 };
6154 GstRTSPMessage response = { 0 };
6158 GST_DEBUG_OBJECT (src, "PAUSE...");
6160 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6163 if (!(src->methods & GST_RTSP_PAUSE))
6166 if (src->state == GST_RTSP_STATE_READY)
6169 if (!src->conninfo.connection || !src->conninfo.connected)
6172 /* construct a control url */
6174 control = src->control;
6176 control = src->conninfo.url_str;
6178 /* loop over the streams. We might exit the loop early when we could do an
6179 * aggregate control */
6180 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6181 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6182 GstRTSPConnection *conn;
6185 /* try aggregate control first but do non-aggregate control otherwise */
6187 setup_url = control;
6188 else if ((setup_url = stream->conninfo.location) == NULL)
6191 if (src->conninfo.connection) {
6192 conn = src->conninfo.connection;
6193 } else if (stream->conninfo.connection) {
6194 conn = stream->conninfo.connection;
6200 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
6201 ("Sending PAUSE request"));
6204 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
6206 goto create_request_failed;
6208 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6211 gst_rtsp_message_unset (&request);
6212 gst_rtsp_message_unset (&response);
6214 /* exit early when we did agregate control */
6220 src->state = GST_RTSP_STATE_READY;
6224 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
6231 GST_DEBUG_OBJECT (src, "failed to open stream");
6236 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
6241 GST_DEBUG_OBJECT (src, "we were already PAUSED");
6244 create_request_failed:
6246 gchar *str = gst_rtsp_strresult (res);
6248 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6249 ("Could not create request. (%s)", str));
6255 gchar *str = gst_rtsp_strresult (res);
6257 gst_rtsp_message_unset (&request);
6258 if (res != GST_RTSP_EINTR) {
6259 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6260 ("Could not send message. (%s)", str));
6262 GST_WARNING_OBJECT (src, "PAUSE interrupted");
6270 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
6272 GstRTSPSrc *rtspsrc;
6274 rtspsrc = GST_RTSPSRC (bin);
6276 switch (GST_MESSAGE_TYPE (message)) {
6277 case GST_MESSAGE_EOS:
6278 gst_message_unref (message);
6280 case GST_MESSAGE_ELEMENT:
6282 const GstStructure *s = gst_message_get_structure (message);
6284 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
6285 gboolean ignore_timeout;
6287 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
6289 GST_OBJECT_LOCK (rtspsrc);
6290 ignore_timeout = rtspsrc->ignore_timeout;
6291 rtspsrc->ignore_timeout = TRUE;
6292 GST_OBJECT_UNLOCK (rtspsrc);
6294 /* we only act on the first udp timeout message, others are irrelevant
6295 * and can be ignored. */
6296 if (!ignore_timeout)
6297 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, TRUE);
6299 gst_message_unref (message);
6302 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6305 case GST_MESSAGE_ERROR:
6308 GstRTSPStream *stream;
6311 udpsrc = GST_MESSAGE_SRC (message);
6313 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
6314 GST_ELEMENT_NAME (udpsrc));
6316 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
6320 /* we ignore the RTCP udpsrc */
6321 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
6324 /* if we get error messages from the udp sources, that's not a problem as
6325 * long as not all of them error out. We also don't really know what the
6326 * problem is, the message does not give enough detail... */
6327 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
6328 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
6329 if (ret != GST_FLOW_OK)
6333 gst_message_unref (message);
6337 /* fatal but not our message, forward */
6338 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6343 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6349 /* the thread where everything happens */
6351 gst_rtspsrc_thread (GstRTSPSrc * src)
6355 gboolean running = FALSE;
6357 GST_OBJECT_LOCK (src);
6358 cmd = src->loop_cmd;
6359 src->loop_cmd = CMD_WAIT;
6360 GST_DEBUG_OBJECT (src, "got command %d", cmd);
6362 /* we got the message command, so ensure communication is possible again */
6363 gst_rtspsrc_connection_flush (src, FALSE);
6365 /* we allow these to be interrupted */
6366 if (cmd == CMD_LOOP || cmd == CMD_CLOSE || cmd == CMD_PAUSE)
6367 src->waiting = TRUE;
6368 GST_OBJECT_UNLOCK (src);
6372 src->cur_protocols = src->protocols;
6373 /* first attempt, don't ignore timeouts */
6374 src->ignore_timeout = FALSE;
6375 src->open_error = FALSE;
6376 ret = gst_rtspsrc_open (src, TRUE);
6379 ret = gst_rtspsrc_play (src, &src->segment, TRUE);
6380 if (ret == GST_RTSP_OK)
6384 ret = gst_rtspsrc_pause (src, TRUE, TRUE);
6385 if (ret == GST_RTSP_OK)
6389 ret = gst_rtspsrc_close (src, TRUE, FALSE);
6392 running = gst_rtspsrc_loop (src);
6395 ret = gst_rtspsrc_reconnect (src, FALSE);
6396 if (ret == GST_RTSP_OK)
6403 GST_OBJECT_LOCK (src);
6404 /* and go back to sleep */
6405 if (src->loop_cmd == CMD_WAIT) {
6407 src->loop_cmd = CMD_LOOP;
6409 gst_task_pause (src->task);
6412 src->waiting = FALSE;
6413 GST_OBJECT_UNLOCK (src);
6417 gst_rtspsrc_start (GstRTSPSrc * src)
6419 GST_DEBUG_OBJECT (src, "starting");
6421 GST_OBJECT_LOCK (src);
6423 src->loop_cmd = CMD_WAIT;
6425 if (src->task == NULL) {
6426 src->task = gst_task_create ((GstTaskFunction) gst_rtspsrc_thread, src);
6427 if (src->task == NULL)
6430 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
6432 GST_OBJECT_UNLOCK (src);
6439 GST_ERROR_OBJECT (src, "failed to create task");
6445 gst_rtspsrc_stop (GstRTSPSrc * src)
6449 GST_DEBUG_OBJECT (src, "stopping");
6451 /* also cancels pending task */
6452 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, TRUE);
6454 GST_OBJECT_LOCK (src);
6455 if ((task = src->task)) {
6457 GST_OBJECT_UNLOCK (src);
6459 gst_task_stop (task);
6461 /* make sure it is not running */
6462 GST_RTSP_STREAM_LOCK (src);
6463 GST_RTSP_STREAM_UNLOCK (src);
6465 /* now wait for the task to finish */
6466 gst_task_join (task);
6468 /* and free the task */
6469 gst_object_unref (GST_OBJECT (task));
6471 GST_OBJECT_LOCK (src);
6473 GST_OBJECT_UNLOCK (src);
6475 /* ensure synchronously all is closed and clean */
6476 gst_rtspsrc_close (src, FALSE, TRUE);
6481 static GstStateChangeReturn
6482 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
6484 GstRTSPSrc *rtspsrc;
6485 GstStateChangeReturn ret;
6487 rtspsrc = GST_RTSPSRC (element);
6489 switch (transition) {
6490 case GST_STATE_CHANGE_NULL_TO_READY:
6491 if (!gst_rtspsrc_start (rtspsrc))
6494 case GST_STATE_CHANGE_READY_TO_PAUSED:
6495 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, FALSE);
6497 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
6498 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
6499 /* unblock the tcp tasks and make the loop waiting */
6500 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, TRUE);
6502 case GST_STATE_CHANGE_PAUSED_TO_READY:
6508 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
6509 if (ret == GST_STATE_CHANGE_FAILURE)
6512 switch (transition) {
6513 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
6514 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, FALSE);
6516 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
6517 /* send pause request and keep the idle task around */
6518 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, FALSE);
6519 ret = GST_STATE_CHANGE_NO_PREROLL;
6521 case GST_STATE_CHANGE_READY_TO_PAUSED:
6522 ret = GST_STATE_CHANGE_NO_PREROLL;
6524 case GST_STATE_CHANGE_PAUSED_TO_READY:
6525 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, FALSE);
6527 case GST_STATE_CHANGE_READY_TO_NULL:
6528 gst_rtspsrc_stop (rtspsrc);
6539 GST_DEBUG_OBJECT (rtspsrc, "start failed");
6540 return GST_STATE_CHANGE_FAILURE;
6545 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
6548 GstRTSPSrc *rtspsrc;
6550 rtspsrc = GST_RTSPSRC (element);
6552 if (GST_EVENT_IS_DOWNSTREAM (event)) {
6553 res = gst_rtspsrc_push_event (rtspsrc, event, TRUE);
6555 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
6562 /*** GSTURIHANDLER INTERFACE *************************************************/
6565 gst_rtspsrc_uri_get_type (GType type)
6571 gst_rtspsrc_uri_get_protocols (GType type)
6573 static const gchar *protocols[] =
6574 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp", NULL };
6576 return (gchar **) protocols;
6579 static const gchar *
6580 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
6582 GstRTSPSrc *src = GST_RTSPSRC (handler);
6584 /* should not dup */
6585 return src->conninfo.location;
6589 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri)
6593 GstRTSPUrl *newurl = NULL;
6594 GstSDPMessage *sdp = NULL;
6596 src = GST_RTSPSRC (handler);
6598 /* same URI, we're fine */
6599 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
6602 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
6603 if ((res = gst_sdp_message_new (&sdp) < 0))
6606 GST_DEBUG_OBJECT (src, "parsing SDP message");
6607 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
6611 GST_DEBUG_OBJECT (src, "parsing URI");
6612 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
6616 /* if worked, free previous and store new url object along with the original
6618 GST_DEBUG_OBJECT (src, "configuring URI");
6619 g_free (src->conninfo.location);
6620 src->conninfo.location = g_strdup (uri);
6621 gst_rtsp_url_free (src->conninfo.url);
6622 src->conninfo.url = newurl;
6623 g_free (src->conninfo.url_str);
6625 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
6627 src->conninfo.url_str = NULL;
6630 gst_sdp_message_free (src->sdp);
6632 src->from_sdp = sdp != NULL;
6634 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
6635 GST_DEBUG_OBJECT (src, "request uri is: %s",
6636 GST_STR_NULL (src->conninfo.url_str));
6643 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
6648 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
6653 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
6654 GST_STR_NULL (uri));
6655 gst_sdp_message_free (sdp);
6660 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
6661 GST_STR_NULL (uri), res);
6667 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
6669 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
6671 iface->get_type = gst_rtspsrc_uri_get_type;
6672 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
6673 iface->get_uri = gst_rtspsrc_uri_get_uri;
6674 iface->set_uri = gst_rtspsrc_uri_set_uri;