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_%u",
110 GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
112 /* templates used internally */
113 static GstStaticPadTemplate anysrctemplate =
114 GST_STATIC_PAD_TEMPLATE ("internalsrc_%u",
117 GST_STATIC_CAPS_ANY);
119 static GstStaticPadTemplate anysinktemplate =
120 GST_STATIC_PAD_TEMPLATE ("internalsink_%u",
123 GST_STATIC_CAPS_ANY);
131 enum _GstRtspSrcRtcpSyncMode
138 enum _GstRtspSrcBufferMode
146 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
148 gst_rtsp_src_buffer_mode_get_type (void)
150 static GType buffer_mode_type = 0;
151 static const GEnumValue buffer_modes[] = {
152 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
153 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
154 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
155 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
159 if (!buffer_mode_type) {
161 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
163 return buffer_mode_type;
166 #define DEFAULT_LOCATION NULL
167 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
168 #define DEFAULT_DEBUG FALSE
169 #define DEFAULT_RETRY 20
170 #define DEFAULT_TIMEOUT 5000000
171 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
172 #define DEFAULT_TCP_TIMEOUT 20000000
173 #define DEFAULT_LATENCY_MS 2000
174 #define DEFAULT_CONNECTION_SPEED 0
175 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
176 #define DEFAULT_DO_RTCP TRUE
177 #define DEFAULT_PROXY NULL
178 #define DEFAULT_RTP_BLOCKSIZE 0
179 #define DEFAULT_USER_ID NULL
180 #define DEFAULT_USER_PW NULL
181 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
182 #define DEFAULT_PORT_RANGE NULL
183 #define DEFAULT_SHORT_HEADER FALSE
195 PROP_CONNECTION_SPEED,
204 PROP_UDP_BUFFER_SIZE,
209 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
211 gst_rtsp_nat_method_get_type (void)
213 static GType rtsp_nat_method_type = 0;
214 static const GEnumValue rtsp_nat_method[] = {
215 {GST_RTSP_NAT_NONE, "None", "none"},
216 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
220 if (!rtsp_nat_method_type) {
221 rtsp_nat_method_type =
222 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
224 return rtsp_nat_method_type;
227 static void gst_rtspsrc_finalize (GObject * object);
229 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
230 const GValue * value, GParamSpec * pspec);
231 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
232 GValue * value, GParamSpec * pspec);
234 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
235 gpointer iface_data);
237 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
240 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
241 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
243 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
245 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
246 GstStateChange transition);
247 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
248 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
250 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
251 GstRTSPMessage * response);
253 static void gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
255 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
256 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
258 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
259 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
261 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean idle,
263 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
264 gboolean only_close);
266 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
269 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
270 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
271 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
272 GstRTSPStream * stream, GstEvent * event, gboolean source);
273 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event,
276 /* commands we send to out loop to notify it of events */
282 #define CMD_RECONNECT 5
285 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
287 gchar *__txt = _gst_element_error_printf text; \
288 gst_element_post_message (GST_ELEMENT_CAST (el), \
289 gst_message_new_progress (GST_OBJECT_CAST (el), \
290 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
294 /*static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 }; */
295 #define gst_rtspsrc_parent_class parent_class
296 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
297 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
300 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
302 GObjectClass *gobject_class;
303 GstElementClass *gstelement_class;
304 GstBinClass *gstbin_class;
306 gobject_class = (GObjectClass *) klass;
307 gstelement_class = (GstElementClass *) klass;
308 gstbin_class = (GstBinClass *) klass;
310 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
312 gobject_class->set_property = gst_rtspsrc_set_property;
313 gobject_class->get_property = gst_rtspsrc_get_property;
315 gobject_class->finalize = gst_rtspsrc_finalize;
317 g_object_class_install_property (gobject_class, PROP_LOCATION,
318 g_param_spec_string ("location", "RTSP Location",
319 "Location of the RTSP url to read",
320 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
322 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
323 g_param_spec_flags ("protocols", "Protocols",
324 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
325 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
327 g_object_class_install_property (gobject_class, PROP_DEBUG,
328 g_param_spec_boolean ("debug", "Debug",
329 "Dump request and response messages to stdout",
330 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
332 g_object_class_install_property (gobject_class, PROP_RETRY,
333 g_param_spec_uint ("retry", "Retry",
334 "Max number of retries when allocating RTP ports.",
335 0, G_MAXUINT16, DEFAULT_RETRY,
336 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
338 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
339 g_param_spec_uint64 ("timeout", "Timeout",
340 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
341 0, G_MAXUINT64, DEFAULT_TIMEOUT,
342 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
344 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
345 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
346 "Fail after timeout microseconds on TCP connections (0 = disabled)",
347 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
348 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
350 g_object_class_install_property (gobject_class, PROP_LATENCY,
351 g_param_spec_uint ("latency", "Buffer latency in ms",
352 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
353 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
355 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
356 g_param_spec_uint ("connection-speed", "Connection Speed",
357 "Network connection speed in kbps (0 = unknown)",
358 0, G_MAXINT / 1000, DEFAULT_CONNECTION_SPEED,
359 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
361 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
362 g_param_spec_enum ("nat-method", "NAT Method",
363 "Method to use for traversing firewalls and NAT",
364 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
365 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
368 * GstRTSPSrc::do-rtcp
370 * Enable RTCP support. Some old server don't like RTCP and then this property
371 * needs to be set to FALSE.
375 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
376 g_param_spec_boolean ("do-rtcp", "Do RTCP",
377 "Send RTCP packets, disable for old incompatible server.",
378 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
383 * Set the proxy parameters. This has to be a string of the format
384 * [http://][user:passwd@]host[:port].
388 g_object_class_install_property (gobject_class, PROP_PROXY,
389 g_param_spec_string ("proxy", "Proxy",
390 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
391 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
394 * GstRTSPSrc::rtp_blocksize
396 * RTP package size to suggest to server.
400 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
401 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
402 "RTP package size to suggest to server (0 = disabled)",
403 0, 65536, DEFAULT_RTP_BLOCKSIZE,
404 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
406 g_object_class_install_property (gobject_class,
408 g_param_spec_string ("user-id", "user-id",
409 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
410 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
411 g_object_class_install_property (gobject_class, PROP_USER_PW,
412 g_param_spec_string ("user-pw", "user-pw",
413 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
414 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
417 * GstRTSPSrc::buffer-mode:
419 * Control the buffering and timestamping mode used by the jitterbuffer.
423 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
424 g_param_spec_enum ("buffer-mode", "Buffer Mode",
425 "Control the buffering algorithm in use",
426 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
427 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
430 * GstRTSPSrc::port-range:
432 * Configure the client port numbers that can be used to recieve RTP and
437 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
438 g_param_spec_string ("port-range", "Port range",
439 "Client port range that can be used to receive RTP and RTCP data, "
440 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
441 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
444 * GstRTSPSrc::udp-buffer-size:
446 * Size of the kernel UDP receive buffer in bytes.
450 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
451 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
452 "Size of the kernel UDP receive buffer in bytes, 0=default",
453 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
454 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
457 * GstRTSPSrc::short-header:
459 * Only send the basic RTSP headers for broken encoders.
463 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
464 g_param_spec_boolean ("short-header", "Short Header",
465 "Only send the basic RTSP headers for broken encoders",
466 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
468 gstelement_class->send_event = gst_rtspsrc_send_event;
469 gstelement_class->change_state = gst_rtspsrc_change_state;
471 gst_element_class_add_pad_template (gstelement_class,
472 gst_static_pad_template_get (&rtptemplate));
474 gst_element_class_set_details_simple (gstelement_class,
475 "RTSP packet receiver", "Source/Network",
476 "Receive data over the network via RTSP (RFC 2326)",
477 "Wim Taymans <wim@fluendo.com>, "
478 "Thijs Vermeir <thijs.vermeir@barco.com>, "
479 "Lutz Mueller <lutz@topfrose.de>");
481 gstbin_class->handle_message = gst_rtspsrc_handle_message;
483 gst_rtsp_ext_list_init ();
488 gst_rtspsrc_init (GstRTSPSrc * src)
493 if (WSAStartup (MAKEWORD (2, 2), &wsa_data) != 0) {
494 GST_ERROR_OBJECT (src, "WSAStartup failed: 0x%08x", WSAGetLastError ());
498 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
499 src->protocols = DEFAULT_PROTOCOLS;
500 src->debug = DEFAULT_DEBUG;
501 src->retry = DEFAULT_RETRY;
502 src->udp_timeout = DEFAULT_TIMEOUT;
503 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
504 src->latency = DEFAULT_LATENCY_MS;
505 src->connection_speed = DEFAULT_CONNECTION_SPEED;
506 src->nat_method = DEFAULT_NAT_METHOD;
507 src->do_rtcp = DEFAULT_DO_RTCP;
508 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
509 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
510 src->user_id = g_strdup (DEFAULT_USER_ID);
511 src->user_pw = g_strdup (DEFAULT_USER_PW);
512 src->buffer_mode = DEFAULT_BUFFER_MODE;
513 src->client_port_range.min = 0;
514 src->client_port_range.max = 0;
515 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
516 src->short_header = DEFAULT_SHORT_HEADER;
518 /* get a list of all extensions */
519 src->extensions = gst_rtsp_ext_list_get ();
521 /* connect to send signal */
522 gst_rtsp_ext_list_connect (src->extensions, "send",
523 (GCallback) gst_rtspsrc_send_cb, src);
525 /* protects the streaming thread in interleaved mode or the polling
526 * thread in UDP mode. */
527 src->stream_rec_lock = g_new (GStaticRecMutex, 1);
528 g_static_rec_mutex_init (src->stream_rec_lock);
530 /* protects our state changes from multiple invocations */
531 src->state_rec_lock = g_new (GStaticRecMutex, 1);
532 g_static_rec_mutex_init (src->state_rec_lock);
534 src->state = GST_RTSP_STATE_INVALID;
536 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_IS_SOURCE);
540 gst_rtspsrc_finalize (GObject * object)
544 rtspsrc = GST_RTSPSRC (object);
546 gst_rtsp_ext_list_free (rtspsrc->extensions);
547 g_free (rtspsrc->conninfo.location);
548 gst_rtsp_url_free (rtspsrc->conninfo.url);
549 g_free (rtspsrc->conninfo.url_str);
550 g_free (rtspsrc->user_id);
551 g_free (rtspsrc->user_pw);
554 gst_sdp_message_free (rtspsrc->sdp);
559 g_static_rec_mutex_free (rtspsrc->stream_rec_lock);
560 g_free (rtspsrc->stream_rec_lock);
561 g_static_rec_mutex_free (rtspsrc->state_rec_lock);
562 g_free (rtspsrc->state_rec_lock);
568 G_OBJECT_CLASS (parent_class)->finalize (object);
571 /* a proxy string of the format [user:passwd@]host[:port] */
573 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
577 g_free (rtsp->proxy_user);
578 rtsp->proxy_user = NULL;
579 g_free (rtsp->proxy_passwd);
580 rtsp->proxy_passwd = NULL;
581 g_free (rtsp->proxy_host);
582 rtsp->proxy_host = NULL;
583 rtsp->proxy_port = 0;
590 /* we allow http:// in front but ignore it */
591 if (g_str_has_prefix (p, "http://"))
594 at = strchr (p, '@');
596 /* look for user:passwd */
597 col = strchr (proxy, ':');
598 if (col == NULL || col > at)
601 rtsp->proxy_user = g_strndup (p, col - p);
603 rtsp->proxy_passwd = g_strndup (col, at - col);
608 col = strchr (p, ':');
611 /* everything before the colon is the hostname */
612 rtsp->proxy_host = g_strndup (p, col - p);
614 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
616 rtsp->proxy_host = g_strdup (p);
617 rtsp->proxy_port = 8080;
623 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
625 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
626 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
629 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
631 rtspsrc->ptcp_timeout = NULL;
635 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
640 rtspsrc = GST_RTSPSRC (object);
644 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
645 g_value_get_string (value));
648 rtspsrc->protocols = g_value_get_flags (value);
651 rtspsrc->debug = g_value_get_boolean (value);
654 rtspsrc->retry = g_value_get_uint (value);
657 rtspsrc->udp_timeout = g_value_get_uint64 (value);
659 case PROP_TCP_TIMEOUT:
660 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
663 rtspsrc->latency = g_value_get_uint (value);
665 case PROP_CONNECTION_SPEED:
666 rtspsrc->connection_speed = g_value_get_uint (value);
668 case PROP_NAT_METHOD:
669 rtspsrc->nat_method = g_value_get_enum (value);
672 rtspsrc->do_rtcp = g_value_get_boolean (value);
675 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
677 case PROP_RTP_BLOCKSIZE:
678 rtspsrc->rtp_blocksize = g_value_get_uint (value);
681 if (rtspsrc->user_id)
682 g_free (rtspsrc->user_id);
683 rtspsrc->user_id = g_value_dup_string (value);
686 if (rtspsrc->user_pw)
687 g_free (rtspsrc->user_pw);
688 rtspsrc->user_pw = g_value_dup_string (value);
690 case PROP_BUFFER_MODE:
691 rtspsrc->buffer_mode = g_value_get_enum (value);
693 case PROP_PORT_RANGE:
697 str = g_value_get_string (value);
699 sscanf (str, "%u-%u",
700 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
702 rtspsrc->client_port_range.min = 0;
703 rtspsrc->client_port_range.max = 0;
707 case PROP_UDP_BUFFER_SIZE:
708 rtspsrc->udp_buffer_size = g_value_get_int (value);
710 case PROP_SHORT_HEADER:
711 rtspsrc->short_header = g_value_get_boolean (value);
714 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
720 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
725 rtspsrc = GST_RTSPSRC (object);
729 g_value_set_string (value, rtspsrc->conninfo.location);
732 g_value_set_flags (value, rtspsrc->protocols);
735 g_value_set_boolean (value, rtspsrc->debug);
738 g_value_set_uint (value, rtspsrc->retry);
741 g_value_set_uint64 (value, rtspsrc->udp_timeout);
743 case PROP_TCP_TIMEOUT:
747 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
748 rtspsrc->tcp_timeout.tv_usec;
749 g_value_set_uint64 (value, timeout);
753 g_value_set_uint (value, rtspsrc->latency);
755 case PROP_CONNECTION_SPEED:
756 g_value_set_uint (value, rtspsrc->connection_speed);
758 case PROP_NAT_METHOD:
759 g_value_set_enum (value, rtspsrc->nat_method);
762 g_value_set_boolean (value, rtspsrc->do_rtcp);
768 if (rtspsrc->proxy_host) {
770 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
774 g_value_take_string (value, str);
777 case PROP_RTP_BLOCKSIZE:
778 g_value_set_uint (value, rtspsrc->rtp_blocksize);
781 g_value_set_string (value, rtspsrc->user_id);
784 g_value_set_string (value, rtspsrc->user_pw);
786 case PROP_BUFFER_MODE:
787 g_value_set_enum (value, rtspsrc->buffer_mode);
789 case PROP_PORT_RANGE:
793 if (rtspsrc->client_port_range.min != 0) {
794 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
795 rtspsrc->client_port_range.max);
799 g_value_take_string (value, str);
802 case PROP_UDP_BUFFER_SIZE:
803 g_value_set_int (value, rtspsrc->udp_buffer_size);
805 case PROP_SHORT_HEADER:
806 g_value_set_boolean (value, rtspsrc->short_header);
809 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
815 find_stream_by_id (GstRTSPStream * stream, gint * id)
817 if (stream->id == *id)
824 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
826 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
833 find_stream_by_pt (GstRTSPStream * stream, gint * pt)
835 if (stream->pt == *pt)
842 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
844 GstElement *src = (GstElement *) a;
846 if (stream->udpsrc[0] == src)
848 if (stream->udpsrc[1] == src)
855 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
857 /* check qualified setup_url */
858 if (!strcmp (stream->conninfo.location, (gchar *) a))
860 /* check original control_url */
861 if (!strcmp (stream->control_url, (gchar *) a))
864 /* check if qualified setup_url ends with string */
865 if (g_str_has_suffix (stream->control_url, (gchar *) a))
871 static GstRTSPStream *
872 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
876 /* find and get stream */
877 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
878 return (GstRTSPStream *) lstream->data;
883 static const GstSDPBandwidth *
884 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
885 const GstSDPMedia * media, const gchar * type)
889 /* first look in the media specific section */
890 len = gst_sdp_media_bandwidths_len (media);
891 for (i = 0; i < len; i++) {
892 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
894 if (strcmp (bw->bwtype, type) == 0)
897 /* then look in the message specific section */
898 len = gst_sdp_message_bandwidths_len (sdp);
899 for (i = 0; i < len; i++) {
900 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
902 if (strcmp (bw->bwtype, type) == 0)
909 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
910 const GstSDPMedia * media, GstRTSPStream * stream)
912 const GstSDPBandwidth *bw;
914 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
915 stream->as_bandwidth = bw->bandwidth;
917 stream->as_bandwidth = -1;
919 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
920 stream->rr_bandwidth = bw->bandwidth;
922 stream->rr_bandwidth = -1;
924 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
925 stream->rs_bandwidth = bw->bandwidth;
927 stream->rs_bandwidth = -1;
931 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
932 const GstSDPConnection * conn)
934 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
937 if (conn->addrtype == NULL)
941 if (strcmp (conn->addrtype, "IP4") == 0)
942 stream->is_ipv6 = FALSE;
943 else if (strcmp (conn->addrtype, "IP6") == 0)
944 stream->is_ipv6 = TRUE;
949 g_free (stream->destination);
950 stream->destination = g_strdup (conn->address);
952 /* check for multicast */
953 stream->is_multicast =
954 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
956 stream->ttl = conn->ttl;
959 /* Go over the connections for a stream.
960 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
962 * - If we are dealing with a localhost address, we disable multicast
965 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
966 const GstSDPMedia * media, GstRTSPStream * stream)
968 const GstSDPConnection *conn;
971 /* first look in the media specific section */
972 len = gst_sdp_media_connections_len (media);
973 for (i = 0; i < len; i++) {
974 conn = gst_sdp_media_get_connection (media, i);
976 gst_rtspsrc_do_stream_connection (src, stream, conn);
978 /* then look in the message specific section */
979 if ((conn = gst_sdp_message_get_connection (sdp))) {
980 gst_rtspsrc_do_stream_connection (src, stream, conn);
984 static GstRTSPStream *
985 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
987 GstRTSPStream *stream;
988 const gchar *control_url;
989 const gchar *payload;
990 const GstSDPMedia *media;
992 /* get media, should not return NULL */
993 media = gst_sdp_message_get_media (sdp, idx);
997 stream = g_new0 (GstRTSPStream, 1);
998 stream->parent = src;
999 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1001 stream->last_ret = GST_FLOW_NOT_LINKED;
1002 stream->added = FALSE;
1003 stream->disabled = FALSE;
1004 stream->id = src->numstreams++;
1005 stream->eos = FALSE;
1006 stream->discont = TRUE;
1007 stream->seqbase = -1;
1008 stream->timebase = -1;
1010 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1011 * session manager to scale RTCP. */
1012 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1014 /* collect connection info */
1015 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1017 /* we must have a payload. No payload means we cannot create caps */
1018 /* FIXME, handle multiple formats. The problem here is that we just want to
1019 * take the first available format that we can handle but in order to do that
1020 * we need to scan for depayloader plugins. Scanning for payloader plugins is
1021 * also suboptimal because the user maybe just wants to save the raw stream
1022 * and then we don't care. */
1023 if ((payload = gst_sdp_media_get_format (media, 0))) {
1024 stream->pt = atoi (payload);
1026 stream->caps = gst_rtspsrc_media_to_caps (stream->pt, media);
1028 GST_DEBUG ("mapping sdp session level attributes to caps");
1029 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, stream->caps);
1030 GST_DEBUG ("mapping sdp media level attributes to caps");
1031 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, stream->caps);
1033 if (stream->pt >= 96) {
1034 /* If we have a dynamic payload type, see if we have a stream with the
1035 * same payload number. If there is one, they are part of the same
1036 * container and we only need to add one pad. */
1037 if (find_stream (src, &stream->pt, (gpointer) find_stream_by_pt)) {
1038 stream->container = TRUE;
1039 GST_DEBUG ("found another stream with pt %d, marking as container",
1044 /* collect port number */
1045 stream->port = gst_sdp_media_get_port (media);
1047 /* get control url to construct the setup url. The setup url is used to
1048 * configure the transport of the stream and is used to identity the stream in
1049 * the RTP-Info header field returned from PLAY. */
1050 control_url = gst_sdp_media_get_attribute_val (media, "control");
1051 if (control_url == NULL)
1052 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1054 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1055 GST_DEBUG_OBJECT (src, " pt: %d", stream->pt);
1056 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1057 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1058 GST_DEBUG_OBJECT (src, " caps: %" GST_PTR_FORMAT, stream->caps);
1059 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1061 if (control_url != NULL) {
1062 stream->control_url = g_strdup (control_url);
1063 /* Build a fully qualified url using the content_base if any or by prefixing
1064 * the original request.
1065 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1066 * likely build a URL that the server will fail to understand, this is ok,
1067 * we will fail then. */
1068 if (g_str_has_prefix (control_url, "rtsp://"))
1069 stream->conninfo.location = g_strdup (control_url);
1074 if (g_strcmp0 (control_url, "*") == 0)
1078 base = src->control;
1079 else if (src->content_base)
1080 base = src->content_base;
1081 else if (src->conninfo.url_str)
1082 base = src->conninfo.url_str;
1086 /* check if the base ends or control starts with / */
1087 has_slash = g_str_has_prefix (control_url, "/");
1088 has_slash = has_slash || g_str_has_suffix (base, "/");
1090 /* concatenate the two strings, insert / when not present */
1091 stream->conninfo.location =
1092 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1095 GST_DEBUG_OBJECT (src, " setup: %s",
1096 GST_STR_NULL (stream->conninfo.location));
1098 /* we keep track of all streams */
1099 src->streams = g_list_append (src->streams, stream);
1107 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1111 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1114 gst_caps_unref (stream->caps);
1116 g_free (stream->destination);
1117 g_free (stream->control_url);
1118 g_free (stream->conninfo.location);
1120 for (i = 0; i < 2; i++) {
1121 if (stream->udpsrc[i]) {
1122 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1123 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1124 gst_object_unref (stream->udpsrc[i]);
1125 stream->udpsrc[i] = NULL;
1127 if (stream->channelpad[i]) {
1128 gst_object_unref (stream->channelpad[i]);
1129 stream->channelpad[i] = NULL;
1131 if (stream->udpsink[i]) {
1132 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1133 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1134 gst_object_unref (stream->udpsink[i]);
1135 stream->udpsink[i] = NULL;
1138 if (stream->fakesrc) {
1139 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1140 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1141 gst_object_unref (stream->fakesrc);
1142 stream->fakesrc = NULL;
1144 if (stream->srcpad) {
1145 gst_pad_set_active (stream->srcpad, FALSE);
1146 if (stream->added) {
1147 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1148 stream->added = FALSE;
1150 stream->srcpad = NULL;
1152 if (stream->rtcppad) {
1153 gst_object_unref (stream->rtcppad);
1154 stream->rtcppad = NULL;
1156 if (stream->session) {
1157 g_object_unref (stream->session);
1158 stream->session = NULL;
1164 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1168 GST_DEBUG_OBJECT (src, "cleanup");
1170 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1171 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1173 gst_rtspsrc_stream_free (src, stream);
1175 g_list_free (src->streams);
1176 src->streams = NULL;
1178 if (src->manager_sig_id) {
1179 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1180 src->manager_sig_id = 0;
1182 gst_element_set_state (src->manager, GST_STATE_NULL);
1183 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1184 src->manager = NULL;
1186 src->numstreams = 0;
1188 gst_structure_free (src->props);
1191 g_free (src->content_base);
1192 src->content_base = NULL;
1194 g_free (src->control);
1195 src->control = NULL;
1198 gst_rtsp_range_free (src->range);
1201 /* don't clear the SDP when it was used in the url */
1202 if (src->sdp && !src->from_sdp) {
1203 gst_sdp_message_free (src->sdp);
1208 #define PARSE_INT(p, del, res) \
1211 p = strstr (p, del); \
1221 #define PARSE_STRING(p, del, res) \
1224 p = strstr (p, del); \
1236 #define SKIP_SPACES(p) \
1237 while (*p && g_ascii_isspace (*p)) \
1242 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1245 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1246 gint * rate, gchar ** params)
1250 p = (gchar *) rtpmap;
1252 PARSE_INT (p, " ", *payload);
1260 PARSE_STRING (p, "/", *name);
1261 if (*name == NULL) {
1262 GST_DEBUG ("no rate, name %s", p);
1263 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1264 * streams seem to omit the rate. */
1271 p = strstr (p, "/");
1289 * Mapping SDP attributes to caps
1291 * prepend 'a-' to IANA registered sdp attributes names
1292 * (ie: not prefixed with 'x-') in order to avoid
1293 * collision with gstreamer standard caps properties names
1296 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1298 if (attributes->len > 0) {
1302 s = gst_caps_get_structure (caps, 0);
1304 for (i = 0; i < attributes->len; i++) {
1305 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1306 gchar *tofree, *key;
1310 /* skip some of the attribute we already handle */
1311 if (!strcmp (key, "fmtp"))
1313 if (!strcmp (key, "rtpmap"))
1315 if (!strcmp (key, "control"))
1317 if (!strcmp (key, "range"))
1320 /* string must be valid UTF8 */
1321 if (!g_utf8_validate (attr->value, -1, NULL))
1324 if (!g_str_has_prefix (key, "x-"))
1325 tofree = key = g_strdup_printf ("a-%s", key);
1329 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1330 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1337 * Mapping of caps to and from SDP fields:
1339 * m=<media> <UDP port> RTP/AVP <payload>
1340 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1341 * a=fmtp:<payload> <param>[=<value>];...
1344 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1347 const gchar *rtpmap;
1351 gchar *params = NULL;
1357 /* get and parse rtpmap */
1358 if ((rtpmap = gst_sdp_media_get_attribute_val (media, "rtpmap"))) {
1359 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
1361 if (payload != pt) {
1362 /* we ignore the rtpmap if the payload type is different. */
1363 g_warning ("rtpmap of wrong payload type, ignoring");
1369 /* if we failed to parse the rtpmap for a dynamic payload type, we have an
1373 /* else we can ignore */
1374 g_warning ("error parsing rtpmap, ignoring");
1377 /* dynamic payloads need rtpmap or we fail */
1381 /* check if we have a rate, if not, we need to look up the rate from the
1382 * default rates based on the payload types. */
1384 const GstRTPPayloadInfo *info;
1386 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
1387 /* dynamic types, use media and encoding_name */
1388 tmp = g_ascii_strdown (media->media, -1);
1389 info = gst_rtp_payload_info_for_name (tmp, name);
1392 /* static types, use payload type */
1393 info = gst_rtp_payload_info_for_pt (pt);
1397 if ((rate = info->clock_rate) == 0)
1400 /* we fail if we cannot find one */
1405 tmp = g_ascii_strdown (media->media, -1);
1406 caps = gst_caps_new_simple ("application/x-unknown",
1407 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
1409 s = gst_caps_get_structure (caps, 0);
1411 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
1413 /* encoding name must be upper case */
1415 tmp = g_ascii_strup (name, -1);
1416 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
1420 /* params must be lower case */
1421 if (params != NULL) {
1422 tmp = g_ascii_strdown (params, -1);
1423 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
1427 /* parse optional fmtp: field */
1428 if ((fmtp = gst_sdp_media_get_attribute_val (media, "fmtp"))) {
1434 /* p is now of the format <payload> <param>[=<value>];... */
1435 PARSE_INT (p, " ", payload);
1436 if (payload != -1 && payload == pt) {
1440 /* <param>[=<value>] are separated with ';' */
1441 pairs = g_strsplit (p, ";", 0);
1442 for (i = 0; pairs[i]; i++) {
1444 const gchar *val, *key;
1446 /* the key may not have a '=', the value can have other '='s */
1447 valpos = strstr (pairs[i], "=");
1449 /* we have a '=' and thus a value, remove the '=' with \0 */
1451 /* value is everything between '=' and ';'. We split the pairs at ;
1452 * boundaries so we can take the remainder of the value. Some servers
1453 * put spaces around the value which we strip off here. Alternatively
1454 * we could strip those spaces in the depayloaders should these spaces
1455 * actually carry any meaning in the future. */
1456 val = g_strstrip (valpos + 1);
1458 /* simple <param>;.. is translated into <param>=1;... */
1461 /* strip the key of spaces, convert key to lowercase but not the value. */
1462 key = g_strstrip (pairs[i]);
1463 if (strlen (key) > 1) {
1464 tmp = g_ascii_strdown (key, -1);
1465 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
1477 g_warning ("rtpmap type not given for dynamic payload %d", pt);
1482 g_warning ("rate unknown for payload type %d", pt);
1488 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1489 gint * rtpport, gint * rtcpport)
1492 GstStateChangeReturn ret;
1493 GstElement *udpsrc0, *udpsrc1;
1494 gint tmp_rtp, tmp_rtcp;
1498 src = stream->parent;
1504 /* Start at next port */
1505 tmp_rtp = src->next_port_num;
1507 if (stream->is_ipv6)
1508 host = "udp://[::0]";
1510 host = "udp://0.0.0.0";
1512 /* try to allocate 2 UDP ports, the RTP port should be an even
1513 * number and the RTCP port should be the next (uneven) port */
1516 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1517 tmp_rtp >= src->client_port_range.max)
1520 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
1521 if (udpsrc0 == NULL)
1522 goto no_udp_protocol;
1523 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1525 if (src->udp_buffer_size != 0)
1526 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
1529 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
1530 if (ret == GST_STATE_CHANGE_FAILURE) {
1532 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
1535 if (++count > src->retry)
1538 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1539 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1540 gst_object_unref (udpsrc0);
1542 GST_DEBUG_OBJECT (src, "retry %d", count);
1545 goto no_udp_protocol;
1548 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
1549 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
1551 /* check if port is even */
1552 if ((tmp_rtp & 0x01) != 0) {
1553 /* port not even, close and allocate another */
1554 if (++count > src->retry)
1557 GST_DEBUG_OBJECT (src, "RTP port not even");
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, "retry %d", count);
1568 /* allocate port+1 for RTCP now */
1569 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL);
1570 if (udpsrc1 == NULL)
1571 goto no_udp_rtcp_protocol;
1574 tmp_rtcp = tmp_rtp + 1;
1575 if (src->client_port_range.max > 0 && tmp_rtcp >= src->client_port_range.max)
1578 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
1580 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
1581 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
1582 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
1583 if (ret == GST_STATE_CHANGE_FAILURE) {
1584 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
1586 if (++count > src->retry)
1589 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1590 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1591 gst_object_unref (udpsrc0);
1593 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
1594 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1595 gst_object_unref (udpsrc1);
1599 GST_DEBUG_OBJECT (src, "retry %d", count);
1603 /* all fine, do port check */
1604 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
1605 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
1607 /* this should not happen... */
1608 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
1611 /* we keep these elements, we configure all in configure_transport when the
1612 * server told us to really use the UDP ports. */
1613 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
1614 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
1616 /* keep track of next available port number when we have a range
1618 if (src->next_port_num != 0)
1619 src->next_port_num = tmp_rtcp + 1;
1626 GST_DEBUG_OBJECT (src, "could not get UDP source");
1631 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
1635 no_udp_rtcp_protocol:
1637 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
1642 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
1643 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
1649 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1650 gst_object_unref (udpsrc0);
1653 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1654 gst_object_unref (udpsrc1);
1661 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
1668 GstClockTime base_time = GST_CLOCK_TIME_NONE;
1671 event = gst_event_new_flush_start ();
1672 GST_DEBUG_OBJECT (src, "start flush");
1674 state = GST_STATE_PAUSED;
1676 event = gst_event_new_flush_stop (TRUE);
1677 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
1680 state = GST_STATE_PLAYING;
1682 state = GST_STATE_PAUSED;
1683 clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
1685 base_time = gst_clock_get_time (clock);
1686 gst_object_unref (clock);
1689 gst_rtspsrc_push_event (src, event, FALSE);
1690 gst_rtspsrc_loop_send_cmd (src, cmd, flush);
1692 /* set up manager before data-flow resumes */
1693 /* to manage jitterbuffer buffer mode */
1695 gst_element_set_base_time (GST_ELEMENT_CAST (src->manager), base_time);
1696 /* and to have base_time trickle further down,
1697 * e.g. to jitterbuffer for its timeout handling */
1698 if (base_time != -1)
1699 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
1702 /* make running time start start at 0 again */
1703 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1704 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1706 for (i = 0; i < 2; i++) {
1708 if (stream->udpsrc[i]) {
1709 if (base_time != -1)
1710 gst_element_set_base_time (stream->udpsrc[i], base_time);
1711 gst_element_set_state (stream->udpsrc[i], state);
1715 /* for tcp interleaved case */
1716 if (base_time != -1)
1717 gst_element_set_base_time (GST_ELEMENT_CAST (src), base_time);
1720 static GstRTSPResult
1721 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
1722 GstRTSPMessage * message, GTimeVal * timeout)
1727 ret = gst_rtsp_connection_send (conn, message, timeout);
1729 ret = GST_RTSP_ERROR;
1734 static GstRTSPResult
1735 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
1736 GstRTSPMessage * message, GTimeVal * timeout)
1741 ret = gst_rtsp_connection_receive (conn, message, timeout);
1743 ret = GST_RTSP_ERROR;
1749 gst_rtspsrc_get_position (GstRTSPSrc * src)
1754 query = gst_query_new_position (GST_FORMAT_TIME);
1755 /* should be known somewhere down the stream (e.g. jitterbuffer) */
1756 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1757 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1761 if (stream->srcpad) {
1762 if (gst_pad_query (stream->srcpad, query)) {
1763 gst_query_parse_position (query, &fmt, &pos);
1764 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
1765 GST_TIME_ARGS (pos));
1766 src->last_pos = pos;
1776 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
1778 src->state = GST_RTSP_STATE_SEEKING;
1779 /* PLAY will add the range header now. */
1780 src->need_range = TRUE;
1786 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
1791 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
1793 gboolean flush, skip;
1796 GstSegment seeksegment = { 0, };
1800 GST_DEBUG_OBJECT (src, "doing seek with event");
1802 gst_event_parse_seek (event, &rate, &format, &flags,
1803 &cur_type, &cur, &stop_type, &stop);
1805 /* no negative rates yet */
1809 /* we need TIME format */
1810 if (format != src->segment.format)
1813 GST_DEBUG_OBJECT (src, "doing seek without event");
1815 cur_type = GST_SEEK_TYPE_SET;
1816 stop_type = GST_SEEK_TYPE_SET;
1819 /* get flush flag */
1820 flush = flags & GST_SEEK_FLAG_FLUSH;
1821 skip = flags & GST_SEEK_FLAG_SKIP;
1823 /* now we need to make sure the streaming thread is stopped. We do this by
1824 * either sending a FLUSH_START event downstream which will cause the
1825 * streaming thread to stop with a WRONG_STATE.
1826 * For a non-flushing seek we simply pause the task, which will happen as soon
1827 * as it completes one iteration (and thus might block when the sink is
1828 * blocking in preroll). */
1830 GST_DEBUG_OBJECT (src, "starting flush");
1831 gst_rtspsrc_flush (src, TRUE, FALSE);
1834 gst_task_pause (src->task);
1838 /* we should now be able to grab the streaming thread because we stopped it
1839 * with the above flush/pause code */
1840 GST_RTSP_STREAM_LOCK (src);
1842 GST_DEBUG_OBJECT (src, "stopped streaming");
1844 /* copy segment, we need this because we still need the old
1845 * segment when we close the current segment. */
1846 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1848 /* configure the seek parameters in the seeksegment. We will then have the
1849 * right values in the segment to perform the seek */
1851 GST_DEBUG_OBJECT (src, "configuring seek");
1852 gst_segment_do_seek (&seeksegment, rate, format, flags,
1853 cur_type, cur, stop_type, stop, &update);
1856 /* figure out the last position we need to play. If it's configured (stop !=
1857 * -1), use that, else we play until the total duration of the file */
1858 if ((stop = seeksegment.stop) == -1)
1859 stop = seeksegment.duration;
1861 playing = (src->state == GST_RTSP_STATE_PLAYING);
1863 /* if we were playing, pause first */
1865 /* obtain current position in case seek fails */
1866 gst_rtspsrc_get_position (src);
1867 gst_rtspsrc_pause (src, FALSE, FALSE);
1870 gst_rtspsrc_do_seek (src, &seeksegment);
1872 /* and continue playing */
1874 gst_rtspsrc_play (src, &seeksegment, FALSE);
1876 /* prepare for streaming again */
1878 /* if we started flush, we stop now */
1879 GST_DEBUG_OBJECT (src, "stopping flush");
1880 gst_rtspsrc_flush (src, FALSE, playing);
1883 /* now we did the seek and can activate the new segment values */
1884 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1886 /* if we're doing a segment seek, post a SEGMENT_START message */
1887 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1888 gst_element_post_message (GST_ELEMENT_CAST (src),
1889 gst_message_new_segment_start (GST_OBJECT_CAST (src),
1890 src->segment.format, src->segment.position));
1893 /* now create the newsegment */
1894 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
1895 " to %" G_GINT64_FORMAT, src->segment.position, stop);
1897 /* store the newsegment event so it can be sent from the streaming thread. */
1898 if (src->start_segment)
1899 gst_event_unref (src->start_segment);
1900 src->start_segment = gst_event_new_segment (&src->segment);
1903 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
1904 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1905 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1906 stream->discont = TRUE;
1910 GST_RTSP_STREAM_UNLOCK (src);
1917 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
1922 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
1928 gst_rtspsrc_handle_src_event (GstPad * pad, GstEvent * event)
1931 gboolean res = TRUE;
1934 src = GST_RTSPSRC_CAST (gst_pad_get_parent (pad));
1936 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
1937 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
1939 switch (GST_EVENT_TYPE (event)) {
1940 case GST_EVENT_SEEK:
1941 res = gst_rtspsrc_perform_seek (src, event);
1945 case GST_EVENT_NAVIGATION:
1946 case GST_EVENT_LATENCY:
1954 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
1955 res = gst_pad_send_event (target, event);
1956 gst_object_unref (target);
1958 gst_event_unref (event);
1961 gst_event_unref (event);
1963 gst_object_unref (src);
1968 /* this is the final event function we receive on the internal source pad when
1969 * we deal with TCP connections */
1971 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstEvent * event)
1976 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
1978 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
1979 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
1981 switch (GST_EVENT_TYPE (event)) {
1982 case GST_EVENT_SEEK:
1984 case GST_EVENT_NAVIGATION:
1985 case GST_EVENT_LATENCY:
1987 gst_event_unref (event);
1994 /* this is the final query function we receive on the internal source pad when
1995 * we deal with TCP connections */
1997 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstQuery * query)
2000 gboolean res = TRUE;
2002 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2004 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2005 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2007 switch (GST_QUERY_TYPE (query)) {
2008 case GST_QUERY_POSITION:
2013 case GST_QUERY_DURATION:
2017 gst_query_parse_duration (query, &format, NULL);
2020 case GST_FORMAT_TIME:
2021 gst_query_set_duration (query, format, src->segment.duration);
2029 case GST_QUERY_LATENCY:
2031 /* we are live with a min latency of 0 and unlimited max latency, this
2032 * result will be updated by the session manager if there is any. */
2033 gst_query_set_latency (query, TRUE, 0, -1);
2043 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2045 gst_rtspsrc_handle_src_query (GstPad * pad, GstQuery * query)
2048 gboolean res = FALSE;
2050 src = GST_RTSPSRC_CAST (gst_pad_get_parent (pad));
2052 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2053 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2055 switch (GST_QUERY_TYPE (query)) {
2056 case GST_QUERY_DURATION:
2060 gst_query_parse_duration (query, &format, NULL);
2063 case GST_FORMAT_TIME:
2064 gst_query_set_duration (query, format, src->segment.duration);
2072 case GST_QUERY_SEEKING:
2076 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2077 if (format == GST_FORMAT_TIME) {
2079 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2081 /* seeking without duration is unlikely */
2082 seekable = seekable && src->seekable && src->segment.duration &&
2083 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2085 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2086 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2087 src->segment.start, src->segment.stop);
2094 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2096 /* forward the query to the proxy target pad */
2098 res = gst_pad_query (target, query);
2099 gst_object_unref (target);
2104 gst_object_unref (src);
2109 /* callback for RTCP messages to be sent to the server when operating in TCP
2111 static GstFlowReturn
2112 gst_rtspsrc_sink_chain (GstPad * pad, GstBuffer * buffer)
2115 GstRTSPStream *stream;
2116 GstFlowReturn res = GST_FLOW_OK;
2121 GstRTSPMessage message = { 0 };
2122 GstRTSPConnection *conn;
2124 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2125 src = stream->parent;
2127 data = gst_buffer_map (buffer, &bsize, NULL, GST_MAP_READ);
2130 gst_rtsp_message_init_data (&message, stream->channel[1]);
2132 /* lend the body data to the message */
2133 gst_rtsp_message_take_body (&message, data, size);
2135 if (stream->conninfo.connection)
2136 conn = stream->conninfo.connection;
2138 conn = src->conninfo.connection;
2140 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2141 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2142 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2144 /* and steal it away again because we will free it when unreffing the
2146 gst_rtsp_message_steal_body (&message, &data, &size);
2147 gst_rtsp_message_unset (&message);
2149 gst_buffer_unmap (buffer, data, size);
2150 gst_buffer_unref (buffer);
2155 static GstPadProbeReturn
2156 pad_blocked (GstPad * pad, GstPadProbeType type, gpointer type_data,
2159 GstRTSPSrc *src = user_data;
2161 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2162 GST_DEBUG_PAD_NAME (pad));
2164 /* activate the streams */
2165 GST_OBJECT_LOCK (src);
2166 if (!src->need_activate)
2169 src->need_activate = FALSE;
2170 GST_OBJECT_UNLOCK (src);
2172 gst_rtspsrc_activate_streams (src);
2174 return GST_PAD_PROBE_OK;
2178 GST_OBJECT_UNLOCK (src);
2179 return GST_PAD_PROBE_OK;
2183 /* this callback is called when the session manager generated a new src pad with
2184 * payloaded RTP packets. We simply ghost the pad here. */
2186 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2189 GstPadTemplate *template;
2192 GstRTSPStream *stream;
2195 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2197 GST_RTSP_STATE_LOCK (src);
2199 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2200 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2201 goto unknown_stream;
2203 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %d, PT %d", id, ssrc, pt);
2205 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2207 goto unknown_stream;
2209 /* create a new pad we will use to stream to */
2210 template = gst_static_pad_template_get (&rtptemplate);
2211 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2212 gst_object_unref (template);
2215 stream->added = TRUE;
2216 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2217 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2218 gst_pad_set_active (stream->srcpad, TRUE);
2219 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2221 /* check if we added all streams */
2223 for (lstream = src->streams; lstream; lstream = g_list_next (lstream)) {
2224 stream = (GstRTSPStream *) lstream->data;
2226 GST_DEBUG_OBJECT (src, "stream %p, container %d, disabled %d, added %d",
2227 stream, stream->container, stream->disabled, stream->added);
2229 /* a container stream only needs one pad added. Also disabled streams don't
2231 if (!stream->container && !stream->disabled && !stream->added) {
2236 GST_RTSP_STATE_UNLOCK (src);
2239 GST_DEBUG_OBJECT (src, "We added all streams");
2240 /* when we get here, all stream are added and we can fire the no-more-pads
2242 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2250 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2251 GST_RTSP_STATE_UNLOCK (src);
2258 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2260 GstRTSPStream *stream;
2263 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2265 GST_RTSP_STATE_LOCK (src);
2266 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2268 goto unknown_stream;
2270 caps = stream->caps;
2272 gst_caps_ref (caps);
2273 GST_RTSP_STATE_UNLOCK (src);
2279 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2280 GST_RTSP_STATE_UNLOCK (src);
2286 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2288 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2294 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos (), TRUE);
2300 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2306 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2308 GstRTSPSrc *src = stream->parent;
2310 GST_DEBUG_OBJECT (src, "source in session %u received BYE", stream->id);
2312 gst_rtspsrc_do_stream_eos (src, stream);
2316 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2318 GstRTSPSrc *src = stream->parent;
2320 GST_DEBUG_OBJECT (src, "source in session %u timed out", stream->id);
2322 gst_rtspsrc_do_stream_eos (src, stream);
2326 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2328 GstRTSPStream *stream;
2330 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2332 /* get stream for session */
2333 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2335 gst_rtspsrc_do_stream_eos (src, stream);
2340 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2342 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2346 /* try to get and configure a manager */
2348 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
2349 GstRTSPTransport * transport)
2351 const gchar *manager;
2353 GstStateChangeReturn ret;
2355 /* find a manager */
2356 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
2360 GST_DEBUG_OBJECT (src, "using manager %s", manager);
2362 /* configure the manager */
2363 if (src->manager == NULL) {
2364 GObjectClass *klass;
2367 if (!(src->manager = gst_element_factory_make (manager, NULL))) {
2369 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
2373 goto use_no_manager;
2375 if (!(src->manager = gst_element_factory_make (manager, NULL)))
2376 goto manager_failed;
2379 /* we manage this element */
2380 gst_bin_add (GST_BIN_CAST (src), src->manager);
2382 GST_OBJECT_LOCK (src);
2383 target = GST_STATE_TARGET (src);
2384 GST_OBJECT_UNLOCK (src);
2386 ret = gst_element_set_state (src->manager, target);
2387 if (ret == GST_STATE_CHANGE_FAILURE)
2388 goto start_manager_failure;
2390 g_object_set (src->manager, "latency", src->latency, NULL);
2392 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2393 if (g_object_class_find_property (klass, "buffer-mode")) {
2394 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2395 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2397 gboolean need_slave;
2399 const gchar *encoding;
2401 /* buffer mode pauses are handled by adding offsets to buffer times,
2402 * but some depayloaders may have a hard time syncing output times
2403 * with such input times, e.g. container ones, most notably ASF */
2404 /* TODO alternatives are having an event that indicates these shifts,
2405 * or having rtsp extensions provide suggestion on buffer mode */
2406 need_slave = stream->container;
2407 if (stream->caps && (s = gst_caps_get_structure (stream->caps, 0)) &&
2408 (encoding = gst_structure_get_string (s, "encoding-name")))
2409 need_slave = need_slave || (strcmp (encoding, "X-ASF-PF") == 0);
2410 GST_DEBUG_OBJECT (src, "auto buffering mode, need_slave %d",
2412 /* valid duration implies not likely live pipeline,
2413 * so slaving in jitterbuffer does not make much sense
2414 * (and might mess things up due to bursts) */
2415 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
2416 src->segment.duration && !need_slave) {
2417 GST_DEBUG_OBJECT (src, "selected buffer");
2418 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER,
2421 GST_DEBUG_OBJECT (src, "selected slave");
2422 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2427 /* connect to signals if we did not already do so */
2428 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
2430 src->manager_sig_id =
2431 g_signal_connect (src->manager, "pad-added",
2432 (GCallback) new_manager_pad, src);
2433 src->manager_ptmap_id =
2434 g_signal_connect (src->manager, "request-pt-map",
2435 (GCallback) request_pt_map, src);
2437 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
2441 /* we stream directly to the manager, get some pads. Each RTSP stream goes
2442 * into a separate RTP session. */
2443 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
2444 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
2446 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
2447 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
2450 /* now configure the bandwidth in the manager */
2451 if (g_signal_lookup ("get-internal-session",
2452 G_OBJECT_TYPE (src->manager)) != 0) {
2453 GObject *rtpsession;
2455 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
2458 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
2460 stream->session = rtpsession;
2462 if (stream->as_bandwidth != -1) {
2463 GST_INFO_OBJECT (src, "setting AS: %f",
2464 (gdouble) (stream->as_bandwidth * 1000));
2465 g_object_set (rtpsession, "bandwidth",
2466 (gdouble) (stream->as_bandwidth * 1000), NULL);
2468 if (stream->rr_bandwidth != -1) {
2469 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
2470 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
2473 if (stream->rs_bandwidth != -1) {
2474 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
2475 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
2478 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2480 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
2482 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
2484 g_signal_connect (rtpsession, "on-ssrc-active",
2485 (GCallback) on_ssrc_active, stream);
2496 GST_DEBUG_OBJECT (src, "cannot get a session manager");
2501 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
2504 start_manager_failure:
2506 GST_DEBUG_OBJECT (src, "could not start session manager");
2511 /* free the UDP sources allocated when negotiating a transport.
2512 * This function is called when the server negotiated to a transport where the
2513 * UDP sources are not needed anymore, such as TCP or multicast. */
2515 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
2519 for (i = 0; i < 2; i++) {
2520 if (stream->udpsrc[i]) {
2521 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
2522 gst_object_unref (stream->udpsrc[i]);
2523 stream->udpsrc[i] = NULL;
2528 /* for TCP, create pads to send and receive data to and from the manager and to
2529 * intercept various events and queries
2532 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
2533 GstRTSPTransport * transport, GstPad ** outpad)
2536 GstPadTemplate *template;
2537 GstPad *pad0, *pad1;
2539 /* configure for interleaved delivery, nothing needs to be done
2540 * here, the loop function will call the chain functions of the
2541 * session manager. */
2542 stream->channel[0] = transport->interleaved.min;
2543 stream->channel[1] = transport->interleaved.max;
2544 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
2545 stream->channel[0], stream->channel[1]);
2547 /* we can remove the allocated UDP ports now */
2548 gst_rtspsrc_stream_free_udp (stream);
2550 /* no session manager, send data to srcpad directly */
2551 if (!stream->channelpad[0]) {
2552 GST_DEBUG_OBJECT (src, "no manager, creating pad");
2554 /* create a new pad we will use to stream to */
2555 name = g_strdup_printf ("stream_%u", stream->id);
2556 template = gst_static_pad_template_get (&rtptemplate);
2557 stream->channelpad[0] = gst_pad_new_from_template (template, name);
2558 gst_object_unref (template);
2561 /* set caps and activate */
2562 gst_pad_use_fixed_caps (stream->channelpad[0]);
2563 gst_pad_set_active (stream->channelpad[0], TRUE);
2565 *outpad = gst_object_ref (stream->channelpad[0]);
2567 GST_DEBUG_OBJECT (src, "using manager source pad");
2569 template = gst_static_pad_template_get (&anysrctemplate);
2571 /* allocate pads for sending the channel data into the manager */
2572 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
2573 gst_pad_link (pad0, stream->channelpad[0]);
2574 gst_object_unref (stream->channelpad[0]);
2575 stream->channelpad[0] = pad0;
2576 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
2577 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
2578 gst_pad_set_element_private (pad0, src);
2579 gst_pad_set_active (pad0, TRUE);
2581 if (stream->channelpad[1]) {
2582 /* if we have a sinkpad for the other channel, create a pad and link to the
2584 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
2585 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
2586 gst_pad_link (pad1, stream->channelpad[1]);
2587 gst_object_unref (stream->channelpad[1]);
2588 stream->channelpad[1] = pad1;
2589 gst_pad_set_active (pad1, TRUE);
2591 gst_object_unref (template);
2593 /* setup RTCP transport back to the server if we have to. */
2594 if (src->manager && src->do_rtcp) {
2597 template = gst_static_pad_template_get (&anysinktemplate);
2599 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
2600 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
2601 gst_pad_set_element_private (stream->rtcppad, stream);
2602 gst_pad_set_active (stream->rtcppad, TRUE);
2604 /* get session RTCP pad */
2605 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
2606 pad = gst_element_get_request_pad (src->manager, name);
2611 gst_pad_link (pad, stream->rtcppad);
2612 gst_object_unref (pad);
2615 gst_object_unref (template);
2621 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
2622 GstRTSPTransport * transport, const gchar ** destination, gint * min,
2623 gint * max, guint * ttl)
2625 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2627 if (!(*destination = transport->destination))
2628 *destination = stream->destination;
2631 /* transport first */
2632 *min = transport->port.min;
2633 *max = transport->port.max;
2634 if (*min == -1 && *max == -1) {
2635 /* then try from SDP */
2636 if (stream->port != 0) {
2637 *min = stream->port;
2638 *max = stream->port + 1;
2644 if (!(*ttl = transport->ttl))
2649 /* first take the source, then the endpoint to figure out where to send
2651 if (!(*destination = transport->source)) {
2652 if (src->conninfo.connection)
2653 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
2654 else if (stream->conninfo.connection)
2656 gst_rtsp_connection_get_ip (stream->conninfo.connection);
2660 /* for unicast we only expect the ports here */
2661 *min = transport->server_port.min;
2662 *max = transport->server_port.max;
2667 /* For multicast create UDP sources and join the multicast group. */
2669 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
2670 GstRTSPTransport * transport, GstPad ** outpad)
2673 const gchar *destination;
2676 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
2678 /* we can remove the allocated UDP ports now */
2679 gst_rtspsrc_stream_free_udp (stream);
2681 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
2684 /* we need a destination now */
2685 if (destination == NULL)
2686 goto no_destination;
2688 /* we really need ports now or we won't be able to receive anything at all */
2689 if (min == -1 && max == -1)
2692 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
2693 destination, min, max);
2695 /* creating UDP source for RTP */
2697 uri = g_strdup_printf ("udp://%s:%d", destination, min);
2698 stream->udpsrc[0] = gst_element_make_from_uri (GST_URI_SRC, uri, NULL);
2700 if (stream->udpsrc[0] == NULL)
2703 /* take ownership */
2704 gst_object_ref_sink (stream->udpsrc[0]);
2707 gst_element_set_state (stream->udpsrc[0], GST_STATE_PAUSED);
2710 /* creating another UDP source for RTCP */
2712 uri = g_strdup_printf ("udp://%s:%d", destination, max);
2713 stream->udpsrc[1] = gst_element_make_from_uri (GST_URI_SRC, uri, NULL);
2715 if (stream->udpsrc[1] == NULL)
2718 /* take ownership */
2719 gst_object_ref_sink (stream->udpsrc[1]);
2721 gst_element_set_state (stream->udpsrc[1], GST_STATE_PAUSED);
2728 GST_DEBUG_OBJECT (src, "no UDP source element found");
2733 GST_DEBUG_OBJECT (src, "no destination found");
2738 GST_DEBUG_OBJECT (src, "no ports found");
2743 /* configure the remainder of the UDP ports */
2745 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
2746 GstRTSPTransport * transport, GstPad ** outpad)
2748 /* we manage the UDP elements now. For unicast, the UDP sources where
2749 * allocated in the stream when we suggested a transport. */
2750 if (stream->udpsrc[0]) {
2751 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
2753 GST_DEBUG_OBJECT (src, "setting up UDP source");
2755 /* configure a timeout on the UDP port. When the timeout message is
2756 * posted, we assume UDP transport is not possible. We reconnect using TCP
2758 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", src->udp_timeout,
2761 /* get output pad of the UDP source. */
2762 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
2764 /* save it so we can unblock */
2765 stream->blockedpad = *outpad;
2767 /* configure pad block on the pad. As soon as there is dataflow on the
2768 * UDP source, we know that UDP is not blocked by a firewall and we can
2769 * configure all the streams to let the application autoplug decoders. */
2771 gst_pad_add_probe (stream->blockedpad, GST_PAD_PROBE_TYPE_BLOCK,
2772 pad_blocked, src, NULL);
2774 if (stream->channelpad[0]) {
2775 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
2776 /* configure for UDP delivery, we need to connect the UDP pads to
2777 * the session plugin. */
2778 gst_pad_link (*outpad, stream->channelpad[0]);
2779 gst_object_unref (*outpad);
2781 /* we connected to pad-added signal to get pads from the manager */
2783 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
2788 if (stream->udpsrc[1]) {
2789 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
2791 if (stream->channelpad[1]) {
2794 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
2796 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
2797 gst_pad_link (pad, stream->channelpad[1]);
2798 gst_object_unref (pad);
2800 /* leave unlinked */
2806 /* configure the UDP sink back to the server for status reports */
2808 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
2809 GstRTSPStream * stream, GstRTSPTransport * transport)
2812 gint rtp_port, rtcp_port, sockfd = -1;
2813 gboolean do_rtp, do_rtcp;
2814 const gchar *destination;
2818 /* get transport info */
2819 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
2820 &rtp_port, &rtcp_port, &ttl);
2822 /* see what we need to do */
2823 do_rtp = (rtp_port != -1);
2824 /* it's possible that the server does not want us to send RTCP in which case
2826 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
2828 /* we need a destination when we have RTP or RTCP ports */
2829 if (destination == NULL && (do_rtp || do_rtcp))
2830 goto no_destination;
2832 /* try to construct the fakesrc to the RTP port of the server to open up any
2835 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
2838 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
2839 stream->udpsink[0] = gst_element_make_from_uri (GST_URI_SINK, uri, NULL);
2841 if (stream->udpsink[0] == NULL)
2842 goto no_sink_element;
2844 /* don't join multicast group, we will have the source socket do that */
2845 /* no sync or async state changes needed */
2846 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
2847 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
2849 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
2851 if (stream->udpsrc[0]) {
2852 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
2853 * so that NAT firewalls will open a hole for us */
2854 g_object_get (G_OBJECT (stream->udpsrc[0]), "sock", &sockfd, NULL);
2855 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %d", sockfd);
2856 /* configure socket and make sure udpsink does not close it when shutting
2857 * down, it belongs to udpsrc after all. */
2858 g_object_set (G_OBJECT (stream->udpsink[0]), "sockfd", sockfd,
2859 "closefd", FALSE, NULL);
2862 /* the source for the dummy packets to open up NAT */
2863 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
2864 if (stream->fakesrc == NULL)
2865 goto no_fakesrc_element;
2867 /* random data in 5 buffers, a size of 200 bytes should be fine */
2868 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
2869 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
2871 /* we don't want to consider this a sink */
2872 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_IS_SINK);
2874 /* keep everything locked */
2875 gst_element_set_locked_state (stream->udpsink[0], TRUE);
2876 gst_element_set_locked_state (stream->fakesrc, TRUE);
2878 gst_object_ref (stream->udpsink[0]);
2879 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
2880 gst_object_ref (stream->fakesrc);
2881 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
2883 gst_element_link (stream->fakesrc, stream->udpsink[0]);
2886 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
2889 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
2890 stream->udpsink[1] = gst_element_make_from_uri (GST_URI_SINK, uri, NULL);
2892 if (stream->udpsink[1] == NULL)
2893 goto no_sink_element;
2895 /* don't join multicast group, we will have the source socket do that */
2896 /* no sync or async state changes needed */
2897 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
2898 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
2900 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
2902 if (stream->udpsrc[1]) {
2903 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
2904 * because some servers check the port number of where it sends RTCP to identify
2905 * the RTCP packets it receives */
2906 g_object_get (G_OBJECT (stream->udpsrc[1]), "sock", &sockfd, NULL);
2907 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %d", sockfd);
2908 /* configure socket and make sure udpsink does not close it when shutting
2909 * down, it belongs to udpsrc after all. */
2910 g_object_set (G_OBJECT (stream->udpsink[1]), "sockfd", sockfd,
2911 "closefd", FALSE, NULL);
2914 /* we don't want to consider this a sink */
2915 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_IS_SINK);
2917 /* we keep this playing always */
2918 gst_element_set_locked_state (stream->udpsink[1], TRUE);
2919 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
2921 gst_object_ref (stream->udpsink[1]);
2922 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
2924 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
2926 /* get session RTCP pad */
2927 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
2928 pad = gst_element_get_request_pad (src->manager, name);
2933 gst_pad_link (pad, stream->rtcppad);
2934 gst_object_unref (pad);
2943 GST_DEBUG_OBJECT (src, "no destination address specified");
2948 GST_DEBUG_OBJECT (src, "no UDP sink element found");
2953 GST_DEBUG_OBJECT (src, "no fakesrc element found");
2958 /* sets up all elements needed for streaming over the specified transport.
2959 * Does not yet expose the element pads, this will be done when there is actuall
2960 * dataflow detected, which might never happen when UDP is blocked in a
2961 * firewall, for example.
2964 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
2965 GstRTSPTransport * transport)
2968 GstPad *outpad = NULL;
2969 GstPadTemplate *template;
2974 src = stream->parent;
2976 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
2978 s = gst_caps_get_structure (stream->caps, 0);
2980 /* get the proper mime type for this stream now */
2981 if (gst_rtsp_transport_get_mime (transport->trans, &mime) < 0)
2982 goto unknown_transport;
2984 goto unknown_transport;
2986 /* configure the final mime type */
2987 GST_DEBUG_OBJECT (src, "setting mime to %s", mime);
2988 gst_structure_set_name (s, mime);
2990 /* try to get and configure a manager, channelpad[0-1] will be configured with
2991 * the pads for the manager, or NULL when no manager is needed. */
2992 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
2995 switch (transport->lower_transport) {
2996 case GST_RTSP_LOWER_TRANS_TCP:
2997 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
2998 goto transport_failed;
3000 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3001 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3002 goto transport_failed;
3003 /* fallthrough, the rest is the same for UDP and MCAST */
3004 case GST_RTSP_LOWER_TRANS_UDP:
3005 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3006 goto transport_failed;
3007 /* configure udpsinks back to the server for RTCP messages and for the
3008 * dummy RTP messages to open NAT. */
3009 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3010 goto transport_failed;
3013 goto unknown_transport;
3017 GST_DEBUG_OBJECT (src, "creating ghostpad");
3019 gst_pad_use_fixed_caps (outpad);
3021 /* create ghostpad, don't add just yet, this will be done when we activate
3023 name = g_strdup_printf ("stream_%u", stream->id);
3024 template = gst_static_pad_template_get (&rtptemplate);
3025 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3026 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3027 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3028 gst_object_unref (template);
3031 gst_object_unref (outpad);
3033 /* mark pad as ok */
3034 stream->last_ret = GST_FLOW_OK;
3041 GST_DEBUG_OBJECT (src, "failed to configure transport");
3046 GST_DEBUG_OBJECT (src, "unknown transport");
3051 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3056 /* send a couple of dummy random packets on the receiver RTP port to the server,
3057 * this should make a firewall think we initiated the data transfer and
3058 * hopefully allow packets to go from the sender port to our RTP receiver port */
3060 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3064 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3067 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3068 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3070 if (stream->fakesrc && stream->udpsink[0]) {
3071 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3072 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3073 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3074 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3075 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3081 /* Adds the source pads of all configured streams to the element.
3082 * This code is performed when we detected dataflow.
3084 * We detect dataflow from either the _loop function or with pad probes on the
3088 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3092 GST_DEBUG_OBJECT (src, "activating streams");
3094 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3095 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3097 if (stream->udpsrc[0]) {
3098 /* remove timeout, we are streaming now and timeouts will be handled by
3099 * the session manager and jitter buffer */
3100 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3102 if (stream->srcpad) {
3103 /* if we don't have a session manager, set the caps now. If we have a
3104 * session, we will get a notification of the pad and the caps. */
3105 if (!src->manager) {
3106 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3107 gst_pad_set_caps (stream->srcpad, stream->caps);
3110 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3111 gst_pad_set_active (stream->srcpad, TRUE);
3113 if (!stream->added) {
3114 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3115 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3116 stream->added = TRUE;
3121 /* unblock all pads */
3122 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3123 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3125 if (stream->blockid) {
3126 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3127 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3128 stream->blockid = 0;
3136 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment)
3139 guint64 start, stop;
3140 gdouble play_speed, play_scale;
3142 GST_DEBUG_OBJECT (src, "configuring stream caps");
3144 start = segment->position;
3145 stop = segment->duration;
3146 play_speed = segment->rate;
3147 play_scale = segment->applied_rate;
3149 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3150 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3153 if ((caps = stream->caps)) {
3154 caps = gst_caps_make_writable (caps);
3156 if (stream->timebase != -1)
3157 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3158 (guint) stream->timebase, NULL);
3159 if (stream->seqbase != -1)
3160 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3161 (guint) stream->seqbase, NULL);
3162 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3164 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3165 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3166 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3168 stream->caps = caps;
3170 GST_DEBUG_OBJECT (src, "stream %p, caps %" GST_PTR_FORMAT, stream, caps);
3173 GST_DEBUG_OBJECT (src, "clear session");
3174 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3178 static GstFlowReturn
3179 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3184 /* store the value */
3185 stream->last_ret = ret;
3187 /* if it's success we can return the value right away */
3188 if (ret == GST_FLOW_OK)
3191 /* any other error that is not-linked can be returned right
3193 if (ret != GST_FLOW_NOT_LINKED)
3196 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3197 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3198 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3200 ret = ostream->last_ret;
3201 /* some other return value (must be SUCCESS but we can return
3202 * other values as well) */
3203 if (ret != GST_FLOW_NOT_LINKED)
3206 /* if we get here, all other pads were unlinked and we return
3207 * NOT_LINKED then */
3213 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
3214 GstEvent * event, gboolean source)
3216 gboolean res = TRUE;
3218 /* only streams that have a connection to the outside world */
3219 if (stream->srcpad == NULL)
3222 if (source && stream->udpsrc[0]) {
3223 gst_event_ref (event);
3224 res = gst_element_send_event (stream->udpsrc[0], event);
3225 } else if (stream->channelpad[0]) {
3226 gst_event_ref (event);
3227 if (GST_PAD_IS_SRC (stream->channelpad[0]))
3228 res = gst_pad_push_event (stream->channelpad[0], event);
3230 res = gst_pad_send_event (stream->channelpad[0], event);
3233 if (source && stream->udpsrc[1]) {
3234 gst_event_ref (event);
3235 res &= gst_element_send_event (stream->udpsrc[1], event);
3236 } else if (stream->channelpad[1]) {
3237 gst_event_ref (event);
3238 if (GST_PAD_IS_SRC (stream->channelpad[1]))
3239 res &= gst_pad_push_event (stream->channelpad[1], event);
3241 res &= gst_pad_send_event (stream->channelpad[1], event);
3245 gst_event_unref (event);
3251 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event, gboolean source)
3254 gboolean res = TRUE;
3256 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3257 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3259 gst_event_ref (event);
3260 res &= gst_rtspsrc_stream_push_event (src, ostream, event, source);
3262 gst_event_unref (event);
3267 static GstRTSPResult
3268 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3273 if (info->connection == NULL) {
3274 if (info->url == NULL) {
3275 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
3276 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
3280 /* create connection */
3281 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
3282 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
3283 goto could_not_create;
3286 g_free (info->url_str);
3287 info->url_str = gst_rtsp_url_get_request_uri (info->url);
3289 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
3291 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
3292 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
3294 if (src->proxy_host) {
3295 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
3297 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
3302 if (!info->connected) {
3305 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
3306 ("Connecting to %s", info->location));
3307 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
3309 gst_rtsp_connection_connect (info->connection,
3310 src->ptcp_timeout)) < 0)
3311 goto could_not_connect;
3313 info->connected = TRUE;
3320 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
3325 gchar *str = gst_rtsp_strresult (res);
3326 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
3332 gchar *str = gst_rtsp_strresult (res);
3333 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
3339 static GstRTSPResult
3340 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
3343 if (info->connected) {
3344 GST_DEBUG_OBJECT (src, "closing connection...");
3345 gst_rtsp_connection_close (info->connection);
3346 info->connected = FALSE;
3348 if (free && info->connection) {
3349 /* free connection */
3350 GST_DEBUG_OBJECT (src, "freeing connection...");
3351 gst_rtsp_connection_free (info->connection);
3352 info->connection = NULL;
3357 static GstRTSPResult
3358 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3363 GST_DEBUG_OBJECT (src, "reconnecting connection...");
3364 gst_rtsp_conninfo_close (src, info, FALSE);
3365 res = gst_rtsp_conninfo_connect (src, info, async);
3371 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
3375 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
3376 if (src->conninfo.connection) {
3377 GST_DEBUG_OBJECT (src, "connection flush");
3378 gst_rtsp_connection_flush (src->conninfo.connection, flush);
3380 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3381 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3382 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
3383 if (stream->conninfo.connection)
3384 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
3388 /* FIXME, handle server request, reply with OK, for now */
3389 static GstRTSPResult
3390 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
3391 GstRTSPMessage * request)
3393 GstRTSPMessage response = { 0 };
3396 GST_DEBUG_OBJECT (src, "got server request message");
3399 gst_rtsp_message_dump (request);
3401 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
3403 if (res == GST_RTSP_ENOTIMPL) {
3404 /* default implementation, send OK */
3406 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
3411 GST_DEBUG_OBJECT (src, "replying with OK");
3414 gst_rtsp_message_dump (&response);
3416 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
3420 gst_rtsp_message_unset (&response);
3421 } else if (res == GST_RTSP_EEOF)
3429 gst_rtsp_message_unset (&response);
3434 /* send server keep-alive */
3435 static GstRTSPResult
3436 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
3438 GstRTSPMessage request = { 0 };
3440 GstRTSPMethod method;
3443 GST_DEBUG_OBJECT (src, "creating server keep-alive");
3445 /* find a method to use for keep-alive */
3446 if (src->methods & GST_RTSP_GET_PARAMETER)
3447 method = GST_RTSP_GET_PARAMETER;
3449 method = GST_RTSP_OPTIONS;
3452 control = src->control;
3454 control = src->conninfo.url_str;
3456 if (control == NULL)
3459 res = gst_rtsp_message_init_request (&request, method, control);
3464 gst_rtsp_message_dump (&request);
3467 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
3472 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3473 gst_rtsp_message_unset (&request);
3480 GST_WARNING_OBJECT (src, "no control url to send keepalive");
3485 gchar *str = gst_rtsp_strresult (res);
3487 gst_rtsp_message_unset (&request);
3488 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
3489 ("Could not send keep-alive. (%s)", str));
3495 static GstFlowReturn
3496 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
3498 GstRTSPMessage message = { 0 };
3501 GstRTSPStream *stream;
3502 GstPad *outpad = NULL;
3505 GstFlowReturn ret = GST_FLOW_OK;
3507 gboolean is_rtcp, have_data;
3509 /* here we are only interested in data messages */
3512 GTimeVal tv_timeout;
3514 /* get the next timeout interval */
3515 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3517 /* see if the timeout period expired */
3518 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
3519 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
3520 /* send keep-alive, only act on interrupt, a warning will be posted for
3522 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3524 /* get new timeout */
3525 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3528 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
3529 tv_timeout.tv_sec, tv_timeout.tv_usec);
3531 /* protect the connection with the connection lock so that we can see when
3532 * we are finished doing server communication */
3534 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
3535 &message, src->ptcp_timeout);
3539 GST_DEBUG_OBJECT (src, "we received a server message");
3541 case GST_RTSP_EINTR:
3542 /* we got interrupted this means we need to stop */
3544 case GST_RTSP_ETIMEOUT:
3545 /* no reply, send keep alive */
3546 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
3547 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3551 /* go EOS when the server closed the connection */
3557 switch (message.type) {
3558 case GST_RTSP_MESSAGE_REQUEST:
3559 /* server sends us a request message, handle it */
3561 gst_rtspsrc_handle_request (src, src->conninfo.connection,
3563 if (res == GST_RTSP_EEOF)
3566 goto handle_request_failed;
3568 case GST_RTSP_MESSAGE_RESPONSE:
3569 /* we ignore response messages */
3570 GST_DEBUG_OBJECT (src, "ignoring response message");
3572 gst_rtsp_message_dump (&message);
3574 case GST_RTSP_MESSAGE_DATA:
3575 GST_DEBUG_OBJECT (src, "got data message");
3579 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
3586 channel = message.type_data.data.channel;
3588 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
3590 goto unknown_stream;
3592 if (channel == stream->channel[0]) {
3593 outpad = stream->channelpad[0];
3595 } else if (channel == stream->channel[1]) {
3596 outpad = stream->channelpad[1];
3602 /* take a look at the body to figure out what we have */
3603 gst_rtsp_message_get_body (&message, &data, &size);
3605 goto invalid_length;
3607 /* channels are not correct on some servers, do extra check */
3608 if (data[1] >= 200 && data[1] <= 204) {
3609 /* hmm RTCP message switch to the RTCP pad of the same stream. */
3610 outpad = stream->channelpad[1];
3614 /* we have no clue what this is, just ignore then. */
3616 goto unknown_stream;
3618 /* take the message body for further processing */
3619 gst_rtsp_message_steal_body (&message, &data, &size);
3621 /* strip the trailing \0 */
3624 buf = gst_buffer_new ();
3625 gst_buffer_take_memory (buf, -1,
3626 gst_memory_new_wrapped (0, data, g_free, size, 0, size));
3628 /* don't need message anymore */
3629 gst_rtsp_message_unset (&message);
3631 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
3634 if (src->need_activate) {
3635 gst_rtspsrc_activate_streams (src);
3636 src->need_activate = FALSE;
3639 if (src->base_time == -1) {
3640 /* Take current running_time. This timestamp will be put on
3641 * the first buffer of each stream because we are a live source and so we
3642 * timestamp with the running_time. When we are dealing with TCP, we also
3643 * only timestamp the first buffer (using the DISCONT flag) because a server
3644 * typically bursts data, for which we don't want to compensate by speeding
3645 * up the media. The other timestamps will be interpollated from this one
3646 * using the RTP timestamps. */
3647 GST_OBJECT_LOCK (src);
3648 if (GST_ELEMENT_CLOCK (src)) {
3650 GstClockTime base_time;
3652 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
3653 base_time = GST_ELEMENT_CAST (src)->base_time;
3655 src->base_time = now - base_time;
3657 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
3658 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
3660 GST_OBJECT_UNLOCK (src);
3663 if (stream->discont && !is_rtcp) {
3664 /* mark first RTP buffer as discont */
3665 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
3666 stream->discont = FALSE;
3667 /* first buffer gets the timestamp, other buffers are not timestamped and
3668 * their presentation time will be interpollated from the rtp timestamps. */
3669 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
3670 GST_TIME_ARGS (src->base_time));
3672 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
3675 /* chain to the peer pad */
3676 if (GST_PAD_IS_SINK (outpad))
3677 ret = gst_pad_chain (outpad, buf);
3679 ret = gst_pad_push (outpad, buf);
3682 /* combine all stream flows for the data transport */
3683 ret = gst_rtspsrc_combine_flows (src, stream, ret);
3690 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
3691 gst_rtsp_message_unset (&message);
3696 GST_DEBUG_OBJECT (src, "we got an eof from the server");
3697 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3698 ("The server closed the connection."));
3699 src->conninfo.connected = FALSE;
3700 gst_rtsp_message_unset (&message);
3701 return GST_FLOW_UNEXPECTED;
3705 gst_rtsp_message_unset (&message);
3706 GST_DEBUG_OBJECT (src, "got interrupted: stop connection flush");
3707 gst_rtspsrc_connection_flush (src, FALSE);
3708 return GST_FLOW_WRONG_STATE;
3712 gchar *str = gst_rtsp_strresult (res);
3714 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
3715 ("Could not receive message. (%s)", str));
3718 gst_rtsp_message_unset (&message);
3719 return GST_FLOW_ERROR;
3721 handle_request_failed:
3723 gchar *str = gst_rtsp_strresult (res);
3725 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
3726 ("Could not handle server message. (%s)", str));
3728 gst_rtsp_message_unset (&message);
3729 return GST_FLOW_ERROR;
3733 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3734 ("Short message received, ignoring."));
3735 gst_rtsp_message_unset (&message);
3740 static GstFlowReturn
3741 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
3744 GstRTSPMessage message = { 0 };
3748 GTimeVal tv_timeout;
3750 /* get the next timeout interval */
3751 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3753 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
3754 (gint) tv_timeout.tv_sec);
3756 gst_rtsp_message_unset (&message);
3758 /* we should continue reading the TCP socket because the server might
3759 * send us requests. When the session timeout expires, we need to send a
3760 * keep-alive request to keep the session open. */
3761 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
3762 &message, &tv_timeout);
3766 GST_DEBUG_OBJECT (src, "we received a server message");
3768 case GST_RTSP_EINTR:
3769 /* we got interrupted, see what we have to do */
3771 case GST_RTSP_ETIMEOUT:
3772 /* send keep-alive, ignore the result, a warning will be posted. */
3773 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
3774 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3778 /* server closed the connection. not very fatal for UDP, reconnect and
3779 * see what happens. */
3780 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3781 ("The server closed the connection."));
3783 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
3791 switch (message.type) {
3792 case GST_RTSP_MESSAGE_REQUEST:
3793 /* server sends us a request message, handle it */
3795 gst_rtspsrc_handle_request (src, src->conninfo.connection,
3797 if (res == GST_RTSP_EEOF)
3800 goto handle_request_failed;
3802 case GST_RTSP_MESSAGE_RESPONSE:
3803 /* we ignore response and data messages */
3804 GST_DEBUG_OBJECT (src, "ignoring response message");
3806 gst_rtsp_message_dump (&message);
3807 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
3808 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
3809 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
3810 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
3811 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3818 case GST_RTSP_MESSAGE_DATA:
3819 /* we ignore response and data messages */
3820 GST_DEBUG_OBJECT (src, "ignoring data message");
3823 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
3829 /* we get here when the connection got interrupted */
3832 gst_rtsp_message_unset (&message);
3833 GST_DEBUG_OBJECT (src, "got interrupted: stop connection flush");
3834 gst_rtspsrc_connection_flush (src, FALSE);
3835 return GST_FLOW_WRONG_STATE;
3839 gchar *str = gst_rtsp_strresult (res);
3842 src->conninfo.connected = FALSE;
3843 if (res != GST_RTSP_EINTR) {
3844 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
3845 ("Could not connect to server. (%s)", str));
3847 ret = GST_FLOW_ERROR;
3849 ret = GST_FLOW_WRONG_STATE;
3855 gchar *str = gst_rtsp_strresult (res);
3857 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
3858 ("Could not receive message. (%s)", str));
3860 return GST_FLOW_ERROR;
3862 handle_request_failed:
3864 gchar *str = gst_rtsp_strresult (res);
3867 gst_rtsp_message_unset (&message);
3868 if (res != GST_RTSP_EINTR) {
3869 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
3870 ("Could not handle server message. (%s)", str));
3872 ret = GST_FLOW_ERROR;
3874 ret = GST_FLOW_WRONG_STATE;
3880 GST_DEBUG_OBJECT (src, "we got an eof from the server");
3881 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3882 ("The server closed the connection."));
3883 src->conninfo.connected = FALSE;
3884 gst_rtsp_message_unset (&message);
3885 return GST_FLOW_UNEXPECTED;
3889 static GstRTSPResult
3890 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
3892 GstRTSPResult res = GST_RTSP_OK;
3895 GST_DEBUG_OBJECT (src, "doing reconnect");
3897 GST_OBJECT_LOCK (src);
3898 /* only restart when the pads were not yet activated, else we were
3899 * streaming over UDP */
3900 restart = src->need_activate;
3901 GST_OBJECT_UNLOCK (src);
3903 /* no need to restart, we're done */
3907 /* we can try only TCP now */
3908 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
3910 /* close and cleanup our state */
3911 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
3914 /* see if we have TCP left to try. Also don't try TCP when we were configured
3916 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
3919 /* We post a warning message now to inform the user
3920 * that nothing happened. It's most likely a firewall thing. */
3921 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3922 ("Could not receive any UDP packets for %.4f seconds, maybe your "
3923 "firewall is blocking it. Retrying using a TCP connection.",
3924 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
3926 /* open new connection using tcp */
3927 if (gst_rtspsrc_open (src, async) < 0)
3930 /* start playback */
3931 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
3940 src->cur_protocols = 0;
3941 /* no transport possible, post an error and stop */
3942 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
3943 ("Could not receive any UDP packets for %.4f seconds, maybe your "
3944 "firewall is blocking it. No other protocols to try.",
3945 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
3946 return GST_FLOW_ERROR;
3950 GST_DEBUG_OBJECT (src, "open failed");
3955 GST_DEBUG_OBJECT (src, "play failed");
3961 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
3965 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
3968 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
3971 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
3974 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
3982 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
3986 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
3989 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
3992 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
3995 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4003 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4007 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4010 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4013 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4016 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4024 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4028 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
4031 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4034 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4037 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4045 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4047 if (ret == GST_RTSP_OK)
4048 gst_rtspsrc_loop_complete_cmd (src, cmd);
4049 else if (ret == GST_RTSP_EINTR)
4050 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4052 gst_rtspsrc_loop_error_cmd (src, cmd);
4056 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gboolean flush)
4060 /* FIXME flush param mute; remove at discretion */
4062 /* start new request */
4063 gst_rtspsrc_loop_start_cmd (src, cmd);
4065 GST_DEBUG_OBJECT (src, "sending cmd %d", cmd);
4067 GST_OBJECT_LOCK (src);
4068 old = src->loop_cmd;
4069 if (old != CMD_WAIT) {
4070 src->loop_cmd = CMD_WAIT;
4071 GST_OBJECT_UNLOCK (src);
4072 /* cancel previous request */
4073 gst_rtspsrc_loop_cancel_cmd (src, old);
4074 GST_OBJECT_LOCK (src);
4076 src->loop_cmd = cmd;
4077 /* interrupt if allowed */
4079 GST_DEBUG_OBJECT (src, "start connection flush");
4080 gst_rtspsrc_connection_flush (src, TRUE);
4083 gst_task_start (src->task);
4084 GST_OBJECT_UNLOCK (src);
4088 gst_rtspsrc_loop (GstRTSPSrc * src)
4092 if (!src->conninfo.connection || !src->conninfo.connected)
4095 if (src->interleaved)
4096 ret = gst_rtspsrc_loop_interleaved (src);
4098 ret = gst_rtspsrc_loop_udp (src);
4100 if (ret != GST_FLOW_OK)
4108 GST_WARNING_OBJECT (src, "we are not connected");
4109 ret = GST_FLOW_WRONG_STATE;
4114 const gchar *reason = gst_flow_get_name (ret);
4116 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
4117 src->running = FALSE;
4118 if (ret == GST_FLOW_UNEXPECTED) {
4119 /* perform EOS logic */
4120 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4121 gst_element_post_message (GST_ELEMENT_CAST (src),
4122 gst_message_new_segment_done (GST_OBJECT_CAST (src),
4123 src->segment.format, src->segment.position));
4125 gst_rtspsrc_push_event (src, gst_event_new_eos (), FALSE);
4127 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
4128 /* for fatal errors we post an error message, post the error before the
4129 * EOS so the app knows about the error first. */
4130 GST_ELEMENT_ERROR (src, STREAM, FAILED,
4131 ("Internal data flow error."),
4132 ("streaming task paused, reason %s (%d)", reason, ret));
4133 gst_rtspsrc_push_event (src, gst_event_new_eos (), FALSE);
4139 #ifndef GST_DISABLE_GST_DEBUG
4140 static const gchar *
4141 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
4145 while (method != 0) {
4162 static const gchar *
4163 gst_rtspsrc_skip_lws (const gchar * s)
4165 while (g_ascii_isspace (*s))
4170 static const gchar *
4171 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
4173 while (s > start && g_ascii_isspace (*(s - 1)))
4178 static const gchar *
4179 gst_rtspsrc_skip_commas (const gchar * s)
4181 /* The grammar allows for multiple commas */
4182 while (g_ascii_isspace (*s) || *s == ',')
4187 static const gchar *
4188 gst_rtspsrc_skip_item (const gchar * s)
4190 gboolean quoted = FALSE;
4191 const gchar *start = s;
4193 /* A list item ends at the last non-whitespace character
4194 * before a comma which is not inside a quoted-string. Or at
4195 * the end of the string.
4201 if (*s == '\\' && *(s + 1))
4210 return gst_rtspsrc_unskip_lws (s, start);
4214 gst_rtsp_decode_quoted_string (gchar * quoted_string)
4218 src = quoted_string + 1;
4219 dst = quoted_string;
4220 while (*src && *src != '"') {
4221 if (*src == '\\' && *(src + 1))
4228 /* Extract the authentication tokens that the server provided for each method
4229 * into an array of structures and give those to the connection object.
4232 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
4233 const gchar * header, gboolean * stale)
4235 GSList *list = NULL, *iter;
4237 gchar *item, *eq, *name_end, *value;
4239 g_return_if_fail (stale != NULL);
4241 gst_rtsp_connection_clear_auth_params (conn);
4244 /* Parse a header whose content is described by RFC2616 as
4245 * "#something", where "something" does not itself contain commas,
4246 * except as part of quoted-strings, into a list of allocated strings.
4248 header = gst_rtspsrc_skip_commas (header);
4250 end = gst_rtspsrc_skip_item (header);
4251 list = g_slist_prepend (list, g_strndup (header, end - header));
4252 header = gst_rtspsrc_skip_commas (end);
4257 list = g_slist_reverse (list);
4258 for (iter = list; iter; iter = iter->next) {
4261 eq = strchr (item, '=');
4263 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
4264 if (name_end == item) {
4265 /* That's no good... */
4272 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
4274 gst_rtsp_decode_quoted_string (value);
4278 if ((strcmp (item, "stale") == 0) && (strcmp (value, "TRUE") == 0))
4280 gst_rtsp_connection_set_auth_param (conn, item, value);
4284 g_slist_free (list);
4287 /* Parse a WWW-Authenticate Response header and determine the
4288 * available authentication methods
4290 * This code should also cope with the fact that each WWW-Authenticate
4291 * header can contain multiple challenge methods + tokens
4293 * At the moment, for Basic auth, we just do a minimal check and don't
4294 * even parse out the realm */
4296 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
4297 GstRTSPConnection * conn, gboolean * stale)
4301 g_return_if_fail (hdr != NULL);
4302 g_return_if_fail (methods != NULL);
4303 g_return_if_fail (stale != NULL);
4305 /* Skip whitespace at the start of the string */
4306 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
4308 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
4309 *methods |= GST_RTSP_AUTH_BASIC;
4310 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
4311 *methods |= GST_RTSP_AUTH_DIGEST;
4312 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
4317 * gst_rtspsrc_setup_auth:
4318 * @src: the rtsp source
4320 * Configure a username and password and auth method on the
4321 * connection object based on a response we received from the
4324 * Currently, this requires that a username and password were supplied
4325 * in the uri. In the future, they may be requested on demand by sending
4326 * a message up the bus.
4328 * Returns: TRUE if authentication information could be set up correctly.
4331 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
4335 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
4336 GstRTSPAuthMethod method;
4337 GstRTSPResult auth_result;
4339 GstRTSPConnection *conn;
4341 gboolean stale = FALSE;
4343 conn = src->conninfo.connection;
4345 /* Identify the available auth methods and see if any are supported */
4346 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
4347 &hdr, 0) == GST_RTSP_OK) {
4348 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
4351 if (avail_methods == GST_RTSP_AUTH_NONE)
4352 goto no_auth_available;
4354 /* For digest auth, if the response indicates that the session
4355 * data are stale, we just update them in the connection object and
4356 * return TRUE to retry the request */
4358 src->tried_url_auth = FALSE;
4360 url = gst_rtsp_connection_get_url (conn);
4362 /* Do we have username and password available? */
4363 if (url != NULL && !src->tried_url_auth && url->user != NULL
4364 && url->passwd != NULL) {
4367 src->tried_url_auth = TRUE;
4368 GST_DEBUG_OBJECT (src,
4369 "Attempting authentication using credentials from the URL");
4371 user = src->user_id;
4372 pass = src->user_pw;
4373 GST_DEBUG_OBJECT (src,
4374 "Attempting authentication using credentials from the properties");
4377 /* FIXME: If the url didn't contain username and password or we tried them
4378 * already, request a username and passwd from the application via some kind
4379 * of credentials request message */
4381 /* If we don't have a username and passwd at this point, bail out. */
4382 if (user == NULL || pass == NULL)
4385 /* Try to configure for each available authentication method, strongest to
4387 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
4388 /* Check if this method is available on the server */
4389 if ((method & avail_methods) == 0)
4392 /* Pass the credentials to the connection to try on the next request */
4393 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
4394 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
4395 * ignore it and end up retrying later */
4396 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
4397 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
4398 gst_rtsp_auth_method_to_string (method));
4403 if (method == GST_RTSP_AUTH_NONE)
4404 goto no_auth_available;
4410 /* Output an error indicating that we couldn't connect because there were
4411 * no supported authentication protocols */
4412 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4413 ("No supported authentication protocol was found"));
4418 /* We don't fire an error message, we just return FALSE and let the
4419 * normal NOT_AUTHORIZED error be propagated */
4424 static GstRTSPResult
4425 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4426 GstRTSPMessage * request, GstRTSPMessage * response,
4427 GstRTSPStatusCode * code)
4430 GstRTSPStatusCode thecode;
4431 gchar *content_base = NULL;
4435 if (!src->short_header)
4436 gst_rtsp_ext_list_before_send (src->extensions, request);
4438 GST_DEBUG_OBJECT (src, "sending message");
4441 gst_rtsp_message_dump (request);
4443 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
4447 gst_rtsp_connection_reset_timeout (conn);
4450 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
4455 gst_rtsp_message_dump (response);
4457 switch (response->type) {
4458 case GST_RTSP_MESSAGE_REQUEST:
4459 res = gst_rtspsrc_handle_request (src, conn, response);
4460 if (res == GST_RTSP_EEOF)
4463 goto handle_request_failed;
4465 case GST_RTSP_MESSAGE_RESPONSE:
4466 /* ok, a response is good */
4467 GST_DEBUG_OBJECT (src, "received response message");
4469 case GST_RTSP_MESSAGE_DATA:
4470 /* get next response */
4471 GST_DEBUG_OBJECT (src, "ignoring data response message");
4474 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4479 thecode = response->type_data.response.code;
4481 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
4483 /* if the caller wanted the result code, we store it. */
4487 /* If the request didn't succeed, bail out before doing any more */
4488 if (thecode != GST_RTSP_STS_OK)
4491 /* store new content base if any */
4492 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
4495 g_free (src->content_base);
4496 src->content_base = g_strdup (content_base);
4498 gst_rtsp_ext_list_after_send (src->extensions, request, response);
4505 gchar *str = gst_rtsp_strresult (res);
4507 if (res != GST_RTSP_EINTR) {
4508 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4509 ("Could not send message. (%s)", str));
4511 GST_WARNING_OBJECT (src, "send interrupted");
4520 GST_WARNING_OBJECT (src, "server closed connection, doing reconnect");
4523 /* if reconnect succeeds, try again */
4525 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
4529 /* only try once after reconnect, then fallthrough and error out */
4532 gchar *str = gst_rtsp_strresult (res);
4534 if (res != GST_RTSP_EINTR) {
4535 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4536 ("Could not receive message. (%s)", str));
4538 GST_WARNING_OBJECT (src, "receive interrupted");
4546 handle_request_failed:
4548 /* ERROR was posted */
4549 gst_rtsp_message_unset (response);
4554 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4555 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4556 ("The server closed the connection."));
4557 gst_rtsp_message_unset (response);
4564 * @src: the rtsp source
4565 * @conn: the connection to send on
4566 * @request: must point to a valid request
4567 * @response: must point to an empty #GstRTSPMessage
4568 * @code: an optional code result
4570 * send @request and retrieve the response in @response. optionally @code can be
4571 * non-NULL in which case it will contain the status code of the response.
4573 * If This function returns #GST_RTSP_OK, @response will contain a valid response
4574 * message that should be cleaned with gst_rtsp_message_unset() after usage.
4576 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
4577 * @response message) if the response code was not 200 (OK).
4579 * If the attempt results in an authentication failure, then this will attempt
4580 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
4583 * Returns: #GST_RTSP_OK if the processing was successful.
4585 static GstRTSPResult
4586 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4587 GstRTSPMessage * request, GstRTSPMessage * response,
4588 GstRTSPStatusCode * code)
4590 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
4591 GstRTSPResult res = GST_RTSP_ERROR;
4594 GstRTSPMethod method = GST_RTSP_INVALID;
4600 /* make sure we don't loop forever */
4604 /* save method so we can disable it when the server complains */
4605 method = request->type_data.request.method;
4608 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
4612 case GST_RTSP_STS_UNAUTHORIZED:
4613 if (gst_rtspsrc_setup_auth (src, response)) {
4614 /* Try the request/response again after configuring the auth info
4622 } while (retry == TRUE);
4624 /* If the user requested the code, let them handle errors, otherwise
4625 * post an error below */
4628 else if (int_code != GST_RTSP_STS_OK)
4629 goto error_response;
4636 GST_DEBUG_OBJECT (src, "got error %d", res);
4641 res = GST_RTSP_ERROR;
4643 switch (response->type_data.response.code) {
4644 case GST_RTSP_STS_NOT_FOUND:
4645 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
4646 response->type_data.response.reason));
4648 case GST_RTSP_STS_MOVED_PERMANENTLY:
4649 case GST_RTSP_STS_MOVE_TEMPORARILY:
4651 gchar *new_location;
4652 GstRTSPLowerTrans transports;
4654 GST_DEBUG_OBJECT (src, "got redirection");
4655 /* if we don't have a Location Header, we must error */
4656 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
4657 &new_location, 0) < 0)
4660 /* When we receive a redirect result, we go back to the INIT state after
4661 * parsing the new URI. The caller should do the needed steps to issue
4662 * a new setup when it detects this state change. */
4663 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
4665 /* save current transports */
4666 if (src->conninfo.url)
4667 transports = src->conninfo.url->transports;
4669 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
4671 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location);
4673 /* set old transports */
4674 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
4675 src->conninfo.url->transports = transports;
4677 src->need_redirect = TRUE;
4678 src->state = GST_RTSP_STATE_INIT;
4682 case GST_RTSP_STS_NOT_ACCEPTABLE:
4683 case GST_RTSP_STS_NOT_IMPLEMENTED:
4684 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
4685 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
4686 gst_rtsp_method_as_text (method));
4687 src->methods &= ~method;
4691 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4692 ("Got error response: %d (%s).", response->type_data.response.code,
4693 response->type_data.response.reason));
4696 /* if we return ERROR we should unset the response ourselves */
4697 if (res == GST_RTSP_ERROR)
4698 gst_rtsp_message_unset (response);
4704 static GstRTSPResult
4705 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
4706 GstRTSPMessage * response, GstRTSPSrc * src)
4708 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
4713 /* parse the response and collect all the supported methods. We need this
4714 * information so that we don't try to send an unsupported request to the
4718 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
4720 GstRTSPHeaderField field;
4726 /* reset supported methods */
4729 /* Try Allow Header first */
4730 field = GST_RTSP_HDR_ALLOW;
4733 gst_rtsp_message_get_header (response, field, &respoptions, indx);
4734 if (indx == 0 && !respoptions) {
4735 /* if no Allow header was found then try the Public header... */
4736 field = GST_RTSP_HDR_PUBLIC;
4737 gst_rtsp_message_get_header (response, field, &respoptions, indx);
4742 /* If we get here, the server gave a list of supported methods, parse
4743 * them here. The string is like:
4745 * OPTIONS, DESCRIBE, ANNOUNCE, PLAY, SETUP, ...
4747 options = g_strsplit (respoptions, ",", 0);
4749 for (i = 0; options[i]; i++) {
4753 stripped = g_strstrip (options[i]);
4754 method = gst_rtsp_find_method (stripped);
4756 /* keep bitfield of supported methods */
4757 if (method != GST_RTSP_INVALID)
4758 src->methods |= method;
4760 g_strfreev (options);
4765 if (src->methods == 0) {
4766 /* neither Allow nor Public are required, assume the server supports
4767 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
4769 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
4770 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
4772 /* always assume PLAY, FIXME, extensions should be able to override
4774 src->methods |= GST_RTSP_PLAY;
4775 /* also assume it will support Range */
4776 src->seekable = TRUE;
4778 /* we need describe and setup */
4779 if (!(src->methods & GST_RTSP_DESCRIBE))
4781 if (!(src->methods & GST_RTSP_SETUP))
4789 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4790 ("Server does not support DESCRIBE."));
4795 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4796 ("Server does not support SETUP."));
4801 /* masks to be kept in sync with the hardcoded protocol order of preference
4803 static guint protocol_masks[] = {
4804 GST_RTSP_LOWER_TRANS_UDP,
4805 GST_RTSP_LOWER_TRANS_UDP_MCAST,
4806 GST_RTSP_LOWER_TRANS_TCP,
4810 static GstRTSPResult
4811 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
4812 GstRTSPLowerTrans protocols, gchar ** transports)
4816 gboolean add_udp_str;
4821 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
4826 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
4828 /* extension listed transports, use those */
4829 if (*transports != NULL)
4832 /* it's the default */
4833 add_udp_str = FALSE;
4835 /* the default RTSP transports */
4836 result = g_string_new ("");
4837 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
4838 GST_DEBUG_OBJECT (src, "adding UDP unicast");
4840 g_string_append (result, "RTP/AVP");
4842 g_string_append (result, "/UDP");
4843 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
4844 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
4845 GST_DEBUG_OBJECT (src, "adding UDP multicast");
4847 /* we don't have to allocate any UDP ports yet, if the selected transport
4848 * turns out to be multicast we can create them and join the multicast
4849 * group indicated in the transport reply */
4850 if (result->len > 0)
4851 g_string_append (result, ",");
4852 g_string_append (result, "RTP/AVP");
4854 g_string_append (result, "/UDP");
4855 g_string_append (result, ";multicast");
4856 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
4857 GST_DEBUG_OBJECT (src, "adding TCP");
4859 if (result->len > 0)
4860 g_string_append (result, ",");
4861 g_string_append (result, "RTP/AVP/TCP;unicast;interleaved=%%i1-%%i2");
4863 *transports = g_string_free (result, FALSE);
4865 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
4876 static GstRTSPResult
4877 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
4878 gint orig_rtpport, gint orig_rtcpport)
4881 gint nr_udp, nr_int;
4883 gint rtpport = 0, rtcpport = 0;
4886 src = stream->parent;
4888 /* find number of placeholders first */
4889 if (strstr (*transports, "%%i2"))
4891 else if (strstr (*transports, "%%i1"))
4896 if (strstr (*transports, "%%u2"))
4898 else if (strstr (*transports, "%%u1"))
4903 if (nr_udp == 0 && nr_int == 0)
4907 if (!orig_rtpport || !orig_rtcpport) {
4908 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
4911 rtpport = orig_rtpport;
4912 rtcpport = orig_rtcpport;
4916 str = g_string_new ("");
4918 while ((next = strstr (p, "%%"))) {
4919 g_string_append_len (str, p, next - p);
4920 if (next[2] == 'u') {
4922 g_string_append_printf (str, "%d", rtpport);
4923 else if (next[3] == '2')
4924 g_string_append_printf (str, "%d", rtcpport);
4926 if (next[2] == 'i') {
4928 g_string_append_printf (str, "%d", src->free_channel);
4929 else if (next[3] == '2')
4930 g_string_append_printf (str, "%d", src->free_channel + 1);
4935 /* append final part */
4936 g_string_append (str, p);
4938 g_free (*transports);
4939 *transports = g_string_free (str, FALSE);
4947 return GST_RTSP_ERROR;
4952 gst_rtspsrc_stream_is_real_media (GstRTSPStream * stream)
4954 gboolean res = FALSE;
4958 const gchar *enc = NULL;
4960 s = gst_caps_get_structure (stream->caps, 0);
4961 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
4962 res = (strstr (enc, "-REAL") != NULL);
4968 /* Perform the SETUP request for all the streams.
4970 * We ask the server for a specific transport, which initially includes all the
4971 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
4972 * two local UDP ports that we send to the server.
4974 * Once the server replied with a transport, we configure the other streams
4975 * with the same transport.
4977 * This function will also configure the stream for the selected transport,
4978 * which basically means creating the pipeline.
4980 static GstRTSPResult
4981 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
4984 GstRTSPResult res = GST_RTSP_ERROR;
4985 GstRTSPMessage request = { 0 };
4986 GstRTSPMessage response = { 0 };
4987 GstRTSPStream *stream = NULL;
4988 GstRTSPLowerTrans protocols;
4989 GstRTSPStatusCode code;
4990 gboolean unsupported_real = FALSE;
4991 gint rtpport, rtcpport;
4995 if (src->conninfo.connection) {
4996 url = gst_rtsp_connection_get_url (src->conninfo.connection);
4997 /* we initially allow all configured lower transports. based on the URL
4998 * transports and the replies from the server we narrow them down. */
4999 protocols = url->transports & src->cur_protocols;
5002 protocols = src->cur_protocols;
5008 /* reset some state */
5009 src->free_channel = 0;
5010 src->interleaved = FALSE;
5011 src->need_activate = FALSE;
5012 /* keep track of next port number, 0 is random */
5013 src->next_port_num = src->client_port_range.min;
5014 rtpport = rtcpport = 0;
5016 if (G_UNLIKELY (src->streams == NULL))
5019 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5020 GstRTSPConnection *conn;
5025 stream = (GstRTSPStream *) walk->data;
5027 /* see if we need to configure this stream */
5028 if (!gst_rtsp_ext_list_configure_stream (src->extensions, stream->caps)) {
5029 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
5031 stream->disabled = TRUE;
5035 /* merge/overwrite global caps */
5040 s = gst_caps_get_structure (stream->caps, 0);
5042 num = gst_structure_n_fields (src->props);
5043 for (j = 0; j < num; j++) {
5047 name = gst_structure_nth_field_name (src->props, j);
5048 val = gst_structure_get_value (src->props, name);
5049 gst_structure_set_value (s, name, val);
5051 GST_DEBUG_OBJECT (src, "copied %s", name);
5055 /* skip setup if we have no URL for it */
5056 if (stream->conninfo.location == NULL) {
5057 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
5061 if (src->conninfo.connection == NULL) {
5062 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
5063 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
5066 conn = stream->conninfo.connection;
5068 conn = src->conninfo.connection;
5070 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
5071 stream->conninfo.location);
5073 /* if we have a multicast connection, only suggest multicast from now on */
5074 if (stream->is_multicast)
5075 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
5078 /* first selectable protocol */
5079 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5081 if (!protocol_masks[mask])
5085 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
5086 protocol_masks[mask]);
5087 /* create a string with first transport in line */
5089 res = gst_rtspsrc_create_transports_string (src,
5090 protocols & protocol_masks[mask], &transports);
5091 if (res < 0 || transports == NULL)
5092 goto setup_transport_failed;
5094 if (strlen (transports) == 0) {
5095 g_free (transports);
5096 GST_DEBUG_OBJECT (src, "no transports found");
5101 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
5103 /* replace placeholders with real values, this function will optionally
5104 * allocate UDP ports and other info needed to execute the setup request */
5105 res = gst_rtspsrc_prepare_transports (stream, &transports,
5106 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
5108 g_free (transports);
5109 goto setup_transport_failed;
5112 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
5114 /* create SETUP request */
5116 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
5117 stream->conninfo.location);
5119 g_free (transports);
5120 goto create_request_failed;
5123 /* select transport, copy is made when adding to header so we can free it. */
5124 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
5125 g_free (transports);
5127 /* if the user wants a non default RTP packet size we add the blocksize
5129 if (src->rtp_blocksize > 0) {
5130 hval = g_strdup_printf ("%d", src->rtp_blocksize);
5131 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
5136 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
5139 /* handle the code ourselves */
5140 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
5144 case GST_RTSP_STS_OK:
5146 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
5147 gst_rtsp_message_unset (&request);
5148 gst_rtsp_message_unset (&response);
5149 /* cleanup of leftover transport */
5150 gst_rtspsrc_stream_free_udp (stream);
5151 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
5152 * we might be in this case */
5153 if (stream->container && rtpport && rtcpport && !retry) {
5154 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
5159 /* this transport did not go down well, but we may have others to try
5160 * that we did not send yet, try those and only give up then
5161 * but not without checking for lost cause/extension so we can
5162 * post a nicer/more useful error message later */
5163 if (!unsupported_real)
5164 unsupported_real = gst_rtspsrc_stream_is_real_media (stream);
5165 /* select next available protocol, give up on this stream if none */
5167 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5169 if (!protocol_masks[mask] || unsupported_real)
5174 /* cleanup of leftover transport and move to the next stream */
5175 gst_rtspsrc_stream_free_udp (stream);
5176 goto response_error;
5179 /* parse response transport */
5181 gchar *resptrans = NULL;
5182 GstRTSPTransport transport = { 0 };
5184 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
5187 gst_rtspsrc_stream_free_udp (stream);
5191 /* parse transport, go to next stream on parse error */
5192 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
5193 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
5197 /* update allowed transports for other streams. once the transport of
5198 * one stream has been determined, we make sure that all other streams
5199 * are configured in the same way */
5200 switch (transport.lower_transport) {
5201 case GST_RTSP_LOWER_TRANS_TCP:
5202 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
5203 protocols = GST_RTSP_LOWER_TRANS_TCP;
5204 src->interleaved = TRUE;
5205 /* update free channels */
5207 MAX (transport.interleaved.min, src->free_channel);
5209 MAX (transport.interleaved.max, src->free_channel);
5210 src->free_channel++;
5212 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
5213 /* only allow multicast for other streams */
5214 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
5215 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
5217 case GST_RTSP_LOWER_TRANS_UDP:
5218 /* only allow unicast for other streams */
5219 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
5220 protocols = GST_RTSP_LOWER_TRANS_UDP;
5223 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
5224 transport.lower_transport);
5228 if (!stream->container || (!src->interleaved && !retry)) {
5229 /* now configure the stream with the selected transport */
5230 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
5231 GST_DEBUG_OBJECT (src,
5232 "could not configure stream %p transport, skipping stream",
5235 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
5236 /* retain the first allocated UDP port pair */
5237 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
5238 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
5241 /* we need to activate at least one streams when we detect activity */
5242 src->need_activate = TRUE;
5244 /* clean up our transport struct */
5245 gst_rtsp_transport_init (&transport);
5246 /* clean up used RTSP messages */
5247 gst_rtsp_message_unset (&request);
5248 gst_rtsp_message_unset (&response);
5252 /* store the transport protocol that was configured */
5253 src->cur_protocols = protocols;
5255 gst_rtsp_ext_list_stream_select (src->extensions, url);
5257 /* if there is nothing to activate, error out */
5258 if (!src->need_activate)
5259 goto nothing_to_activate;
5266 /* no transport possible, post an error and stop */
5267 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5268 ("Could not connect to server, no protocols left"));
5269 return GST_RTSP_ERROR;
5273 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5274 ("SDP contains no streams"));
5275 return GST_RTSP_ERROR;
5277 create_request_failed:
5279 gchar *str = gst_rtsp_strresult (res);
5281 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5282 ("Could not create request. (%s)", str));
5286 setup_transport_failed:
5288 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5289 ("Could not setup transport."));
5290 res = GST_RTSP_ERROR;
5295 const gchar *str = gst_rtsp_status_as_text (code);
5297 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5298 ("Error (%d): %s", code, GST_STR_NULL (str)));
5299 res = GST_RTSP_ERROR;
5304 gchar *str = gst_rtsp_strresult (res);
5306 if (res != GST_RTSP_EINTR) {
5307 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5308 ("Could not send message. (%s)", str));
5310 GST_WARNING_OBJECT (src, "send interrupted");
5317 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5318 ("Server did not select transport."));
5319 res = GST_RTSP_ERROR;
5322 nothing_to_activate:
5324 /* none of the available error codes is really right .. */
5325 if (unsupported_real) {
5326 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5327 (_("No supported stream was found. You might need to install a "
5328 "GStreamer RTSP extension plugin for Real media streams.")),
5331 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5332 (_("No supported stream was found. You might need to allow "
5333 "more transport protocols or may otherwise be missing "
5334 "the right GStreamer RTSP extension plugin.")), (NULL));
5336 return GST_RTSP_ERROR;
5340 gst_rtsp_message_unset (&request);
5341 gst_rtsp_message_unset (&response);
5347 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
5348 GstSegment * segment)
5351 GstRTSPTimeRange *therange;
5354 gst_rtsp_range_free (src->range);
5356 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
5357 GST_DEBUG_OBJECT (src, "parsed range %s", range);
5358 src->range = therange;
5360 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
5362 gst_segment_init (segment, GST_FORMAT_TIME);
5366 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
5367 therange->min.type, therange->min.seconds, therange->max.type,
5368 therange->max.seconds);
5370 if (therange->min.type == GST_RTSP_TIME_NOW)
5372 else if (therange->min.type == GST_RTSP_TIME_END)
5375 seconds = therange->min.seconds * GST_SECOND;
5377 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
5378 GST_TIME_ARGS (seconds));
5380 /* we need to start playback without clipping from the position reported by
5382 segment->start = seconds;
5383 segment->position = seconds;
5385 if (therange->max.type == GST_RTSP_TIME_NOW)
5387 else if (therange->max.type == GST_RTSP_TIME_END)
5390 seconds = therange->max.seconds * GST_SECOND;
5392 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
5393 GST_TIME_ARGS (seconds));
5395 /* live (WMS) server might send overflowed large max as its idea of infinity,
5396 * compensate to prevent problems later on */
5397 if (seconds != -1 && seconds < 0) {
5399 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
5402 /* live (WMS) might send min == max, which is not worth recording */
5403 if (segment->duration == -1 && seconds == segment->start)
5406 /* don't change duration with unknown value, we might have a valid value
5407 * there that we want to keep. */
5409 segment->duration = seconds;
5414 /* must be called with the RTSP state lock */
5415 static GstRTSPResult
5416 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
5422 /* prepare global stream caps properties */
5424 gst_structure_remove_all_fields (src->props);
5426 src->props = gst_structure_new_empty ("RTSPProperties");
5429 gst_sdp_message_dump (sdp);
5431 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
5433 gst_segment_init (&src->segment, GST_FORMAT_TIME);
5435 /* parse range for duration reporting. */
5440 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
5444 /* keep track of the range and configure it in the segment */
5445 if (gst_rtspsrc_parse_range (src, range, &src->segment))
5449 /* try to find a global control attribute. Note that a '*' means that we should
5450 * do aggregate control with the current url (so we don't do anything and
5451 * leave the current connection as is) */
5453 const gchar *control;
5456 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
5457 if (control == NULL)
5460 /* only take fully qualified urls */
5461 if (g_str_has_prefix (control, "rtsp://"))
5465 g_free (src->conninfo.location);
5466 src->conninfo.location = g_strdup (control);
5467 /* make a connection for this, if there was a connection already, nothing
5469 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
5470 GST_ERROR_OBJECT (src, "could not connect");
5473 /* we need to keep the control url separate from the connection url because
5474 * the rules for constructing the media control url need it */
5475 g_free (src->control);
5476 src->control = g_strdup (control);
5479 /* create streams */
5480 n_streams = gst_sdp_message_medias_len (sdp);
5481 for (i = 0; i < n_streams; i++) {
5482 gst_rtspsrc_create_stream (src, sdp, i);
5485 src->state = GST_RTSP_STATE_INIT;
5488 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
5491 /* reset our state */
5492 src->need_range = TRUE;
5495 src->state = GST_RTSP_STATE_READY;
5502 GST_ERROR_OBJECT (src, "setup failed");
5507 static GstRTSPResult
5508 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
5512 GstRTSPMessage request = { 0 };
5513 GstRTSPMessage response = { 0 };
5516 gchar *respcont = NULL;
5519 src->need_redirect = FALSE;
5521 /* can't continue without a valid url */
5522 if (G_UNLIKELY (src->conninfo.url == NULL)) {
5523 res = GST_RTSP_EINVAL;
5526 src->tried_url_auth = FALSE;
5528 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
5529 goto connect_failed;
5531 /* create OPTIONS */
5532 GST_DEBUG_OBJECT (src, "create options...");
5534 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
5535 src->conninfo.url_str);
5537 goto create_request_failed;
5540 GST_DEBUG_OBJECT (src, "send options...");
5543 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
5546 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
5551 if (!gst_rtspsrc_parse_methods (src, &response))
5554 /* create DESCRIBE */
5555 GST_DEBUG_OBJECT (src, "create describe...");
5557 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
5558 src->conninfo.url_str);
5560 goto create_request_failed;
5562 /* we only accept SDP for now */
5563 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
5567 GST_DEBUG_OBJECT (src, "send describe...");
5570 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
5573 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
5577 /* we only perform redirect for the describe, currently */
5578 if (src->need_redirect) {
5579 /* close connection, we don't have to send a TEARDOWN yet, ignore the
5581 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5583 gst_rtsp_message_unset (&request);
5584 gst_rtsp_message_unset (&response);
5590 /* it could be that the DESCRIBE method was not implemented */
5591 if (!src->methods & GST_RTSP_DESCRIBE)
5594 /* check if reply is SDP */
5595 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
5597 /* could not be set but since the request returned OK, we assume it
5598 * was SDP, else check it. */
5600 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
5601 goto wrong_content_type;
5604 /* get message body and parse as SDP */
5605 gst_rtsp_message_get_body (&response, &data, &size);
5606 if (data == NULL || size == 0)
5609 GST_DEBUG_OBJECT (src, "parse SDP...");
5610 gst_sdp_message_new (sdp);
5611 gst_sdp_message_parse_buffer (data, size, *sdp);
5613 /* clean up any messages */
5614 gst_rtsp_message_unset (&request);
5615 gst_rtsp_message_unset (&response);
5622 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
5623 ("No valid RTSP URL was provided"));
5628 gchar *str = gst_rtsp_strresult (res);
5630 if (res != GST_RTSP_EINTR) {
5631 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
5632 ("Failed to connect. (%s)", str));
5634 GST_WARNING_OBJECT (src, "connect interrupted");
5639 create_request_failed:
5641 gchar *str = gst_rtsp_strresult (res);
5643 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5644 ("Could not create request. (%s)", str));
5650 /* Don't post a message - the rtsp_send method will have
5651 * taken care of it because we passed NULL for the response code */
5656 /* error was posted */
5657 res = GST_RTSP_ERROR;
5662 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5663 ("Server does not support SDP, got %s.", respcont));
5664 res = GST_RTSP_ERROR;
5669 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5670 ("Server can not provide an SDP."));
5671 res = GST_RTSP_ERROR;
5676 if (src->conninfo.connection) {
5677 GST_DEBUG_OBJECT (src, "free connection");
5678 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5680 gst_rtsp_message_unset (&request);
5681 gst_rtsp_message_unset (&response);
5686 static GstRTSPResult
5687 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
5692 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
5694 if (src->sdp == NULL) {
5695 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
5699 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
5704 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
5711 GST_WARNING_OBJECT (src, "can't get sdp");
5712 src->open_error = TRUE;
5717 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
5718 src->open_error = TRUE;
5723 static GstRTSPResult
5724 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
5726 GstRTSPMessage request = { 0 };
5727 GstRTSPMessage response = { 0 };
5728 GstRTSPResult res = GST_RTSP_OK;
5732 GST_DEBUG_OBJECT (src, "TEARDOWN...");
5734 if (src->state < GST_RTSP_STATE_READY) {
5735 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
5742 /* construct a control url */
5744 control = src->control;
5746 control = src->conninfo.url_str;
5748 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
5751 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5752 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5754 GstRTSPConnInfo *info;
5756 /* try aggregate control first but do non-aggregate control otherwise */
5758 setup_url = control;
5759 else if ((setup_url = stream->conninfo.location) == NULL)
5762 if (src->conninfo.connection) {
5763 info = &src->conninfo;
5764 } else if (stream->conninfo.connection) {
5765 info = &stream->conninfo;
5769 if (!info->connected)
5774 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
5776 goto create_request_failed;
5779 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
5782 gst_rtspsrc_send (src, info->connection, &request, &response,
5786 /* FIXME, parse result? */
5787 gst_rtsp_message_unset (&request);
5788 gst_rtsp_message_unset (&response);
5791 /* early exit when we did aggregate control */
5797 /* close connections */
5798 GST_DEBUG_OBJECT (src, "closing connection...");
5799 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5800 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5801 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5802 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
5806 gst_rtspsrc_cleanup (src);
5808 src->state = GST_RTSP_STATE_INVALID;
5811 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
5816 create_request_failed:
5818 gchar *str = gst_rtsp_strresult (res);
5820 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5821 ("Could not create request. (%s)", str));
5827 gchar *str = gst_rtsp_strresult (res);
5829 gst_rtsp_message_unset (&request);
5830 if (res != GST_RTSP_EINTR) {
5831 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5832 ("Could not send message. (%s)", str));
5834 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
5841 GST_DEBUG_OBJECT (src,
5842 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
5847 /* RTP-Info is of the format:
5849 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
5851 * rtptime corresponds to the timestamp for the NPT time given in the header
5852 * seqbase corresponds to the next sequence number we received. This number
5853 * indicates the first seqnum after the seek and should be used to discard
5854 * packets that are from before the seek.
5857 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
5862 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
5864 infos = g_strsplit (rtpinfo, ",", 0);
5865 for (i = 0; infos[i]; i++) {
5867 GstRTSPStream *stream;
5871 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
5873 /* init values, types of seqbase and timebase are bigger than needed so we
5874 * can store -1 as uninitialized values */
5879 /* parse url, find stream for url.
5880 * parse seq and rtptime. The seq number should be configured in the rtp
5881 * depayloader or session manager to detect gaps. Same for the rtptime, it
5882 * should be used to create an initial time newsegment. */
5883 fields = g_strsplit (infos[i], ";", 0);
5884 for (j = 0; fields[j]; j++) {
5885 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
5886 /* remove leading whitespace */
5887 fields[j] = g_strchug (fields[j]);
5888 if (g_str_has_prefix (fields[j], "url=")) {
5889 /* get the url and the stream */
5891 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
5892 } else if (g_str_has_prefix (fields[j], "seq=")) {
5893 seqbase = atoi (fields[j] + 4);
5894 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
5895 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
5898 g_strfreev (fields);
5899 /* now we need to store the values for the caps of the stream */
5900 if (stream != NULL) {
5901 GST_DEBUG_OBJECT (src,
5902 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
5903 stream, seqbase, timebase);
5905 /* we have a stream, configure detected params */
5906 stream->seqbase = seqbase;
5907 stream->timebase = timebase;
5916 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
5921 interval = strtoul (rtcp, NULL, 10);
5922 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
5927 interval *= GST_MSECOND;
5929 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5930 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5932 /* already (optionally) retrieved this when configuring manager */
5933 if (stream->session) {
5934 GObject *rtpsession = stream->session;
5936 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
5938 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
5942 /* now it happens that (Xenon) server sending this may also provide bogus
5943 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
5944 * and just use RTP-Info to sync */
5946 GObjectClass *klass;
5948 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
5949 if (g_object_class_find_property (klass, "rtcp-sync")) {
5950 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
5951 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
5957 gst_rtspsrc_get_float (const gchar * dstr)
5959 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
5961 /* canonicalise floating point string so we can handle float strings
5962 * in the form "24.930" or "24,930" irrespective of the current locale */
5963 g_strlcpy (s, dstr, sizeof (s));
5964 g_strdelimit (s, ",", '.');
5965 return g_ascii_strtod (s, NULL);
5969 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
5971 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
5973 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
5974 g_strlcpy (val_str, "now", sizeof (val_str));
5976 if (segment->position == 0) {
5977 g_strlcpy (val_str, "0", sizeof (val_str));
5979 g_ascii_dtostr (val_str, sizeof (val_str),
5980 ((gdouble) segment->position) / GST_SECOND);
5983 return g_strdup_printf ("npt=%s-", val_str);
5987 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
5989 stream->timebase = -1;
5990 stream->seqbase = -1;
5994 stream->caps = gst_caps_make_writable (stream->caps);
5995 s = gst_caps_get_structure (stream->caps, 0);
5996 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
6000 static GstRTSPResult
6001 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
6003 GstRTSPResult res = GST_RTSP_OK;
6005 if (src->state < GST_RTSP_STATE_READY) {
6006 res = GST_RTSP_ERROR;
6007 if (src->open_error) {
6008 GST_DEBUG_OBJECT (src, "the stream was in error");
6012 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
6014 if ((res = gst_rtspsrc_open (src, async)) < 0) {
6015 GST_DEBUG_OBJECT (src, "failed to open stream");
6024 static GstRTSPResult
6025 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
6027 GstRTSPMessage request = { 0 };
6028 GstRTSPMessage response = { 0 };
6029 GstRTSPResult res = GST_RTSP_OK;
6035 GST_DEBUG_OBJECT (src, "PLAY...");
6037 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6040 if (!(src->methods & GST_RTSP_PLAY))
6043 if (src->state == GST_RTSP_STATE_PLAYING)
6046 if (!src->conninfo.connection || !src->conninfo.connected)
6049 /* send some dummy packets before we activate the receive in the
6051 gst_rtspsrc_send_dummy_packets (src);
6053 /* activate receive elements;
6054 * only in async case, since receive elements may not have been affected
6055 * by overall state change (e.g. not around yet),
6056 * do not mess with state in sync case (e.g. seeking) */
6058 gst_element_set_state (GST_ELEMENT_CAST (src), GST_STATE_PLAYING);
6060 /* construct a control url */
6062 control = src->control;
6064 control = src->conninfo.url_str;
6066 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6067 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6069 GstRTSPConnection *conn;
6071 /* try aggregate control first but do non-aggregate control otherwise */
6073 setup_url = control;
6074 else if ((setup_url = stream->conninfo.location) == NULL)
6077 if (src->conninfo.connection) {
6078 conn = src->conninfo.connection;
6079 } else if (stream->conninfo.connection) {
6080 conn = stream->conninfo.connection;
6086 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
6088 goto create_request_failed;
6090 if (src->need_range) {
6091 hval = gen_range_header (src, segment);
6093 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_RANGE, hval);
6097 if (segment->rate != 1.0) {
6098 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
6100 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
6102 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
6104 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
6108 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
6110 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6113 /* seek may have silently failed as it is not supported */
6114 if (!(src->methods & GST_RTSP_PLAY)) {
6115 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
6116 /* obviously it is supported as we made it here */
6117 src->methods |= GST_RTSP_PLAY;
6118 src->seekable = FALSE;
6119 /* but there is nothing to parse in the response,
6120 * so convey we have no idea and not to expect anything particular */
6121 clear_rtp_base (src, stream);
6125 /* need to do for all streams */
6126 for (run = src->streams; run; run = g_list_next (run))
6127 clear_rtp_base (src, (GstRTSPStream *) run->data);
6129 /* NOTE the above also disables npt based eos detection */
6130 /* and below forces position to 0,
6131 * which is visible feedback we lost the plot */
6132 segment->start = segment->position = src->last_pos;
6135 gst_rtsp_message_unset (&request);
6137 /* parse RTP npt field. This is the current position in the stream (Normal
6138 * Play Time) and should be put in the NEWSEGMENT position field. */
6139 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
6141 gst_rtspsrc_parse_range (src, hval, segment);
6143 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
6144 segment->rate = 1.0;
6146 /* parse Speed header. This is the intended playback rate of the stream
6147 * and should be put in the NEWSEGMENT rate field. */
6148 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
6149 0) == GST_RTSP_OK) {
6150 segment->rate = gst_rtspsrc_get_float (hval);
6151 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
6152 &hval, 0) == GST_RTSP_OK) {
6153 segment->rate = gst_rtspsrc_get_float (hval);
6156 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
6157 * for the RTP packets. If this is not present, we assume all starts from 0...
6158 * This is info for the RTP session manager that we pass to it in caps. */
6160 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
6161 &hval, hval_idx++) == GST_RTSP_OK)
6162 gst_rtspsrc_parse_rtpinfo (src, hval);
6164 /* some servers indicate RTCP parameters in PLAY response,
6165 * rather than properly in SDP */
6166 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
6167 &hval, 0) == GST_RTSP_OK)
6168 gst_rtspsrc_handle_rtcp_interval (src, hval);
6170 gst_rtsp_message_unset (&response);
6172 /* early exit when we did aggregate control */
6176 /* set again when needed */
6177 src->need_range = FALSE;
6179 /* configure the caps of the streams after we parsed all headers. */
6180 gst_rtspsrc_configure_caps (src, segment);
6182 src->running = TRUE;
6183 src->base_time = -1;
6184 src->state = GST_RTSP_STATE_PLAYING;
6187 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
6188 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6189 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6190 stream->discont = TRUE;
6195 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
6202 GST_DEBUG_OBJECT (src, "failed to open stream");
6207 GST_DEBUG_OBJECT (src, "PLAY is not supported");
6212 GST_DEBUG_OBJECT (src, "we were already PLAYING");
6215 create_request_failed:
6217 gchar *str = gst_rtsp_strresult (res);
6219 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6220 ("Could not create request. (%s)", str));
6226 gchar *str = gst_rtsp_strresult (res);
6228 gst_rtsp_message_unset (&request);
6229 if (res != GST_RTSP_EINTR) {
6230 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6231 ("Could not send message. (%s)", str));
6233 GST_WARNING_OBJECT (src, "PLAY interrupted");
6240 static GstRTSPResult
6241 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean idle, gboolean async)
6243 GstRTSPResult res = GST_RTSP_OK;
6244 GstRTSPMessage request = { 0 };
6245 GstRTSPMessage response = { 0 };
6249 GST_DEBUG_OBJECT (src, "PAUSE...");
6251 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6254 if (!(src->methods & GST_RTSP_PAUSE))
6257 if (src->state == GST_RTSP_STATE_READY)
6260 if (!src->conninfo.connection || !src->conninfo.connected)
6263 /* construct a control url */
6265 control = src->control;
6267 control = src->conninfo.url_str;
6269 /* loop over the streams. We might exit the loop early when we could do an
6270 * aggregate control */
6271 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6272 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6273 GstRTSPConnection *conn;
6276 /* try aggregate control first but do non-aggregate control otherwise */
6278 setup_url = control;
6279 else if ((setup_url = stream->conninfo.location) == NULL)
6282 if (src->conninfo.connection) {
6283 conn = src->conninfo.connection;
6284 } else if (stream->conninfo.connection) {
6285 conn = stream->conninfo.connection;
6291 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
6292 ("Sending PAUSE request"));
6295 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
6297 goto create_request_failed;
6299 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6302 gst_rtsp_message_unset (&request);
6303 gst_rtsp_message_unset (&response);
6305 /* exit early when we did agregate control */
6311 src->state = GST_RTSP_STATE_READY;
6315 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
6322 GST_DEBUG_OBJECT (src, "failed to open stream");
6327 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
6332 GST_DEBUG_OBJECT (src, "we were already PAUSED");
6335 create_request_failed:
6337 gchar *str = gst_rtsp_strresult (res);
6339 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6340 ("Could not create request. (%s)", str));
6346 gchar *str = gst_rtsp_strresult (res);
6348 gst_rtsp_message_unset (&request);
6349 if (res != GST_RTSP_EINTR) {
6350 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6351 ("Could not send message. (%s)", str));
6353 GST_WARNING_OBJECT (src, "PAUSE interrupted");
6361 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
6363 GstRTSPSrc *rtspsrc;
6365 rtspsrc = GST_RTSPSRC (bin);
6367 switch (GST_MESSAGE_TYPE (message)) {
6368 case GST_MESSAGE_EOS:
6369 gst_message_unref (message);
6371 case GST_MESSAGE_ELEMENT:
6373 const GstStructure *s = gst_message_get_structure (message);
6375 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
6376 gboolean ignore_timeout;
6378 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
6380 GST_OBJECT_LOCK (rtspsrc);
6381 ignore_timeout = rtspsrc->ignore_timeout;
6382 rtspsrc->ignore_timeout = TRUE;
6383 GST_OBJECT_UNLOCK (rtspsrc);
6385 /* we only act on the first udp timeout message, others are irrelevant
6386 * and can be ignored. */
6387 if (!ignore_timeout)
6388 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, TRUE);
6390 gst_message_unref (message);
6393 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6396 case GST_MESSAGE_ERROR:
6399 GstRTSPStream *stream;
6402 udpsrc = GST_MESSAGE_SRC (message);
6404 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
6405 GST_ELEMENT_NAME (udpsrc));
6407 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
6411 /* we ignore the RTCP udpsrc */
6412 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
6415 /* if we get error messages from the udp sources, that's not a problem as
6416 * long as not all of them error out. We also don't really know what the
6417 * problem is, the message does not give enough detail... */
6418 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
6419 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
6420 if (ret != GST_FLOW_OK)
6424 gst_message_unref (message);
6428 /* fatal but not our message, forward */
6429 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6434 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6440 /* the thread where everything happens */
6442 gst_rtspsrc_thread (GstRTSPSrc * src)
6446 gboolean running = FALSE;
6448 GST_OBJECT_LOCK (src);
6449 cmd = src->loop_cmd;
6450 src->loop_cmd = CMD_WAIT;
6451 GST_DEBUG_OBJECT (src, "got command %d", cmd);
6453 /* we got the message command, so ensure communication is possible again */
6454 gst_rtspsrc_connection_flush (src, FALSE);
6456 /* we allow these to be interrupted */
6457 if (cmd == CMD_LOOP || cmd == CMD_CLOSE || cmd == CMD_PAUSE)
6458 src->waiting = TRUE;
6459 GST_OBJECT_UNLOCK (src);
6463 ret = gst_rtspsrc_open (src, TRUE);
6466 ret = gst_rtspsrc_play (src, &src->segment, TRUE);
6467 if (ret == GST_RTSP_OK)
6471 ret = gst_rtspsrc_pause (src, TRUE, TRUE);
6472 if (ret == GST_RTSP_OK)
6476 ret = gst_rtspsrc_close (src, TRUE, FALSE);
6479 running = gst_rtspsrc_loop (src);
6482 ret = gst_rtspsrc_reconnect (src, FALSE);
6483 if (ret == GST_RTSP_OK)
6490 GST_OBJECT_LOCK (src);
6491 /* and go back to sleep */
6492 if (src->loop_cmd == CMD_WAIT) {
6494 src->loop_cmd = CMD_LOOP;
6496 gst_task_pause (src->task);
6499 src->waiting = FALSE;
6500 GST_OBJECT_UNLOCK (src);
6504 gst_rtspsrc_start (GstRTSPSrc * src)
6506 GST_DEBUG_OBJECT (src, "starting");
6508 GST_OBJECT_LOCK (src);
6510 src->loop_cmd = CMD_WAIT;
6512 if (src->task == NULL) {
6513 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src);
6514 if (src->task == NULL)
6517 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
6519 GST_OBJECT_UNLOCK (src);
6526 GST_ERROR_OBJECT (src, "failed to create task");
6532 gst_rtspsrc_stop (GstRTSPSrc * src)
6536 GST_DEBUG_OBJECT (src, "stopping");
6538 /* also cancels pending task */
6539 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, TRUE);
6541 GST_OBJECT_LOCK (src);
6542 if ((task = src->task)) {
6544 GST_OBJECT_UNLOCK (src);
6546 gst_task_stop (task);
6548 /* make sure it is not running */
6549 GST_RTSP_STREAM_LOCK (src);
6550 GST_RTSP_STREAM_UNLOCK (src);
6552 /* now wait for the task to finish */
6553 gst_task_join (task);
6555 /* and free the task */
6556 gst_object_unref (GST_OBJECT (task));
6558 GST_OBJECT_LOCK (src);
6560 GST_OBJECT_UNLOCK (src);
6562 /* ensure synchronously all is closed and clean */
6563 gst_rtspsrc_close (src, FALSE, TRUE);
6568 static GstStateChangeReturn
6569 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
6571 GstRTSPSrc *rtspsrc;
6572 GstStateChangeReturn ret;
6574 rtspsrc = GST_RTSPSRC (element);
6576 switch (transition) {
6577 case GST_STATE_CHANGE_NULL_TO_READY:
6578 if (!gst_rtspsrc_start (rtspsrc))
6581 case GST_STATE_CHANGE_READY_TO_PAUSED:
6582 /* init some state */
6583 rtspsrc->cur_protocols = rtspsrc->protocols;
6584 /* first attempt, don't ignore timeouts */
6585 rtspsrc->ignore_timeout = FALSE;
6586 rtspsrc->open_error = FALSE;
6587 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, FALSE);
6589 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
6590 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
6591 /* unblock the tcp tasks and make the loop waiting */
6592 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, TRUE);
6594 case GST_STATE_CHANGE_PAUSED_TO_READY:
6600 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
6601 if (ret == GST_STATE_CHANGE_FAILURE)
6604 switch (transition) {
6605 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
6606 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, FALSE);
6608 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
6609 /* send pause request and keep the idle task around */
6610 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, FALSE);
6611 ret = GST_STATE_CHANGE_NO_PREROLL;
6613 case GST_STATE_CHANGE_READY_TO_PAUSED:
6614 ret = GST_STATE_CHANGE_NO_PREROLL;
6616 case GST_STATE_CHANGE_PAUSED_TO_READY:
6617 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, FALSE);
6619 case GST_STATE_CHANGE_READY_TO_NULL:
6620 gst_rtspsrc_stop (rtspsrc);
6631 GST_DEBUG_OBJECT (rtspsrc, "start failed");
6632 return GST_STATE_CHANGE_FAILURE;
6637 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
6640 GstRTSPSrc *rtspsrc;
6642 rtspsrc = GST_RTSPSRC (element);
6644 if (GST_EVENT_IS_DOWNSTREAM (event)) {
6645 res = gst_rtspsrc_push_event (rtspsrc, event, TRUE);
6647 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
6654 /*** GSTURIHANDLER INTERFACE *************************************************/
6657 gst_rtspsrc_uri_get_type (GType type)
6663 gst_rtspsrc_uri_get_protocols (GType type)
6665 static const gchar *protocols[] =
6666 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp", NULL };
6668 return (gchar **) protocols;
6671 static const gchar *
6672 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
6674 GstRTSPSrc *src = GST_RTSPSRC (handler);
6676 /* should not dup */
6677 return src->conninfo.location;
6681 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri)
6685 GstRTSPUrl *newurl = NULL;
6686 GstSDPMessage *sdp = NULL;
6688 src = GST_RTSPSRC (handler);
6690 /* same URI, we're fine */
6691 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
6694 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
6695 if ((res = gst_sdp_message_new (&sdp) < 0))
6698 GST_DEBUG_OBJECT (src, "parsing SDP message");
6699 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
6703 GST_DEBUG_OBJECT (src, "parsing URI");
6704 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
6708 /* if worked, free previous and store new url object along with the original
6710 GST_DEBUG_OBJECT (src, "configuring URI");
6711 g_free (src->conninfo.location);
6712 src->conninfo.location = g_strdup (uri);
6713 gst_rtsp_url_free (src->conninfo.url);
6714 src->conninfo.url = newurl;
6715 g_free (src->conninfo.url_str);
6717 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
6719 src->conninfo.url_str = NULL;
6722 gst_sdp_message_free (src->sdp);
6724 src->from_sdp = sdp != NULL;
6726 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
6727 GST_DEBUG_OBJECT (src, "request uri is: %s",
6728 GST_STR_NULL (src->conninfo.url_str));
6735 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
6740 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
6745 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
6746 GST_STR_NULL (uri));
6747 gst_sdp_message_free (sdp);
6752 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
6753 GST_STR_NULL (uri), res);
6759 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
6761 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
6763 iface->get_type = gst_rtspsrc_uri_get_type;
6764 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
6765 iface->get_uri = gst_rtspsrc_uri_get_uri;
6766 iface->set_uri = gst_rtspsrc_uri_set_uri;