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"
100 GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
101 #define GST_CAT_DEFAULT (rtspsrc_debug)
103 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream_%u",
106 GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
108 /* templates used internally */
109 static GstStaticPadTemplate anysrctemplate =
110 GST_STATIC_PAD_TEMPLATE ("internalsrc_%u",
113 GST_STATIC_CAPS_ANY);
115 static GstStaticPadTemplate anysinktemplate =
116 GST_STATIC_PAD_TEMPLATE ("internalsink_%u",
119 GST_STATIC_CAPS_ANY);
127 enum _GstRtspSrcRtcpSyncMode
134 enum _GstRtspSrcBufferMode
142 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
144 gst_rtsp_src_buffer_mode_get_type (void)
146 static GType buffer_mode_type = 0;
147 static const GEnumValue buffer_modes[] = {
148 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
149 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
150 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
151 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
155 if (!buffer_mode_type) {
157 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
159 return buffer_mode_type;
162 #define DEFAULT_LOCATION NULL
163 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
164 #define DEFAULT_DEBUG FALSE
165 #define DEFAULT_RETRY 20
166 #define DEFAULT_TIMEOUT 5000000
167 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
168 #define DEFAULT_TCP_TIMEOUT 20000000
169 #define DEFAULT_LATENCY_MS 2000
170 #define DEFAULT_CONNECTION_SPEED 0
171 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
172 #define DEFAULT_DO_RTCP TRUE
173 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
174 #define DEFAULT_PROXY NULL
175 #define DEFAULT_RTP_BLOCKSIZE 0
176 #define DEFAULT_USER_ID NULL
177 #define DEFAULT_USER_PW NULL
178 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
179 #define DEFAULT_PORT_RANGE NULL
180 #define DEFAULT_SHORT_HEADER FALSE
192 PROP_CONNECTION_SPEED,
195 PROP_DO_RTSP_KEEP_ALIVE,
202 PROP_UDP_BUFFER_SIZE,
207 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
209 gst_rtsp_nat_method_get_type (void)
211 static GType rtsp_nat_method_type = 0;
212 static const GEnumValue rtsp_nat_method[] = {
213 {GST_RTSP_NAT_NONE, "None", "none"},
214 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
218 if (!rtsp_nat_method_type) {
219 rtsp_nat_method_type =
220 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
222 return rtsp_nat_method_type;
225 static void gst_rtspsrc_finalize (GObject * object);
227 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
228 const GValue * value, GParamSpec * pspec);
229 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
230 GValue * value, GParamSpec * pspec);
232 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
233 gpointer iface_data);
235 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
238 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
239 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
241 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
243 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
244 GstStateChange transition);
245 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
246 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
248 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
249 GstRTSPMessage * response);
251 static void gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask);
252 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
253 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
255 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
256 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
258 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
259 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
260 gboolean only_close);
262 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
263 const gchar * uri, GError ** error);
265 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
266 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
267 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
268 GstRTSPStream * stream, GstEvent * event);
269 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
271 /* commands we send to out loop to notify it of events */
272 #define CMD_OPEN (1 << 0)
273 #define CMD_PLAY (1 << 1)
274 #define CMD_PAUSE (1 << 2)
275 #define CMD_CLOSE (1 << 3)
276 #define CMD_WAIT (1 << 4)
277 #define CMD_RECONNECT (1 << 5)
278 #define CMD_LOOP (1 << 6)
280 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
282 gchar *__txt = _gst_element_error_printf text; \
283 gst_element_post_message (GST_ELEMENT_CAST (el), \
284 gst_message_new_progress (GST_OBJECT_CAST (el), \
285 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
289 /*static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 }; */
290 #define gst_rtspsrc_parent_class parent_class
291 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
292 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
295 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
297 GObjectClass *gobject_class;
298 GstElementClass *gstelement_class;
299 GstBinClass *gstbin_class;
301 gobject_class = (GObjectClass *) klass;
302 gstelement_class = (GstElementClass *) klass;
303 gstbin_class = (GstBinClass *) klass;
305 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
307 gobject_class->set_property = gst_rtspsrc_set_property;
308 gobject_class->get_property = gst_rtspsrc_get_property;
310 gobject_class->finalize = gst_rtspsrc_finalize;
312 g_object_class_install_property (gobject_class, PROP_LOCATION,
313 g_param_spec_string ("location", "RTSP Location",
314 "Location of the RTSP url to read",
315 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
317 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
318 g_param_spec_flags ("protocols", "Protocols",
319 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
320 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
322 g_object_class_install_property (gobject_class, PROP_DEBUG,
323 g_param_spec_boolean ("debug", "Debug",
324 "Dump request and response messages to stdout",
325 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
327 g_object_class_install_property (gobject_class, PROP_RETRY,
328 g_param_spec_uint ("retry", "Retry",
329 "Max number of retries when allocating RTP ports.",
330 0, G_MAXUINT16, DEFAULT_RETRY,
331 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
333 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
334 g_param_spec_uint64 ("timeout", "Timeout",
335 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
336 0, G_MAXUINT64, DEFAULT_TIMEOUT,
337 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
339 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
340 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
341 "Fail after timeout microseconds on TCP connections (0 = disabled)",
342 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
343 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
345 g_object_class_install_property (gobject_class, PROP_LATENCY,
346 g_param_spec_uint ("latency", "Buffer latency in ms",
347 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
348 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
350 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
351 g_param_spec_uint64 ("connection-speed", "Connection Speed",
352 "Network connection speed in kbps (0 = unknown)",
353 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
354 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
356 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
357 g_param_spec_enum ("nat-method", "NAT Method",
358 "Method to use for traversing firewalls and NAT",
359 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
360 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
363 * GstRTSPSrc::do-rtcp
365 * Enable RTCP support. Some old server don't like RTCP and then this property
366 * needs to be set to FALSE.
370 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
371 g_param_spec_boolean ("do-rtcp", "Do RTCP",
372 "Send RTCP packets, disable for old incompatible server.",
373 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
376 * GstRTSPSrc::do-rtsp-keep-alive
378 * Enable RTSP keep laive support. Some old server don't like RTSP
379 * keep alive and then this property needs to be set to FALSE.
383 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
384 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
385 "Send RTSP keep alive packets, disable for old incompatible server.",
386 DEFAULT_DO_RTSP_KEEP_ALIVE,
387 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
392 * Set the proxy parameters. This has to be a string of the format
393 * [http://][user:passwd@]host[:port].
397 g_object_class_install_property (gobject_class, PROP_PROXY,
398 g_param_spec_string ("proxy", "Proxy",
399 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
400 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
403 * GstRTSPSrc::rtp_blocksize
405 * RTP package size to suggest to server.
409 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
410 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
411 "RTP package size to suggest to server (0 = disabled)",
412 0, 65536, DEFAULT_RTP_BLOCKSIZE,
413 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
415 g_object_class_install_property (gobject_class,
417 g_param_spec_string ("user-id", "user-id",
418 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
419 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
420 g_object_class_install_property (gobject_class, PROP_USER_PW,
421 g_param_spec_string ("user-pw", "user-pw",
422 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
423 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
426 * GstRTSPSrc::buffer-mode:
428 * Control the buffering and timestamping mode used by the jitterbuffer.
432 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
433 g_param_spec_enum ("buffer-mode", "Buffer Mode",
434 "Control the buffering algorithm in use",
435 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
436 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
439 * GstRTSPSrc::port-range:
441 * Configure the client port numbers that can be used to recieve RTP and
446 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
447 g_param_spec_string ("port-range", "Port range",
448 "Client port range that can be used to receive RTP and RTCP data, "
449 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
450 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
453 * GstRTSPSrc::udp-buffer-size:
455 * Size of the kernel UDP receive buffer in bytes.
459 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
460 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
461 "Size of the kernel UDP receive buffer in bytes, 0=default",
462 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
463 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
466 * GstRTSPSrc::short-header:
468 * Only send the basic RTSP headers for broken encoders.
472 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
473 g_param_spec_boolean ("short-header", "Short Header",
474 "Only send the basic RTSP headers for broken encoders",
475 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
477 gstelement_class->send_event = gst_rtspsrc_send_event;
478 gstelement_class->change_state = gst_rtspsrc_change_state;
480 gst_element_class_add_pad_template (gstelement_class,
481 gst_static_pad_template_get (&rtptemplate));
483 gst_element_class_set_static_metadata (gstelement_class,
484 "RTSP packet receiver", "Source/Network",
485 "Receive data over the network via RTSP (RFC 2326)",
486 "Wim Taymans <wim@fluendo.com>, "
487 "Thijs Vermeir <thijs.vermeir@barco.com>, "
488 "Lutz Mueller <lutz@topfrose.de>");
490 gstbin_class->handle_message = gst_rtspsrc_handle_message;
492 gst_rtsp_ext_list_init ();
497 gst_rtspsrc_init (GstRTSPSrc * src)
499 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
500 src->protocols = DEFAULT_PROTOCOLS;
501 src->debug = DEFAULT_DEBUG;
502 src->retry = DEFAULT_RETRY;
503 src->udp_timeout = DEFAULT_TIMEOUT;
504 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
505 src->latency = DEFAULT_LATENCY_MS;
506 src->connection_speed = DEFAULT_CONNECTION_SPEED;
507 src->nat_method = DEFAULT_NAT_METHOD;
508 src->do_rtcp = DEFAULT_DO_RTCP;
509 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
510 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
511 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
512 src->user_id = g_strdup (DEFAULT_USER_ID);
513 src->user_pw = g_strdup (DEFAULT_USER_PW);
514 src->buffer_mode = DEFAULT_BUFFER_MODE;
515 src->client_port_range.min = 0;
516 src->client_port_range.max = 0;
517 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
518 src->short_header = DEFAULT_SHORT_HEADER;
520 /* get a list of all extensions */
521 src->extensions = gst_rtsp_ext_list_get ();
523 /* connect to send signal */
524 gst_rtsp_ext_list_connect (src->extensions, "send",
525 (GCallback) gst_rtspsrc_send_cb, src);
527 /* protects the streaming thread in interleaved mode or the polling
528 * thread in UDP mode. */
529 g_rec_mutex_init (&src->stream_rec_lock);
531 /* protects our state changes from multiple invocations */
532 g_rec_mutex_init (&src->state_rec_lock);
534 src->state = GST_RTSP_STATE_INVALID;
536 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_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_rec_mutex_clear (&rtspsrc->stream_rec_lock);
560 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
562 G_OBJECT_CLASS (parent_class)->finalize (object);
565 /* a proxy string of the format [user:passwd@]host[:port] */
567 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
571 g_free (rtsp->proxy_user);
572 rtsp->proxy_user = NULL;
573 g_free (rtsp->proxy_passwd);
574 rtsp->proxy_passwd = NULL;
575 g_free (rtsp->proxy_host);
576 rtsp->proxy_host = NULL;
577 rtsp->proxy_port = 0;
584 /* we allow http:// in front but ignore it */
585 if (g_str_has_prefix (p, "http://"))
588 at = strchr (p, '@');
590 /* look for user:passwd */
591 col = strchr (proxy, ':');
592 if (col == NULL || col > at)
595 rtsp->proxy_user = g_strndup (p, col - p);
597 rtsp->proxy_passwd = g_strndup (col, at - col);
602 col = strchr (p, ':');
605 /* everything before the colon is the hostname */
606 rtsp->proxy_host = g_strndup (p, col - p);
608 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
610 rtsp->proxy_host = g_strdup (p);
611 rtsp->proxy_port = 8080;
617 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
619 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
620 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
623 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
625 rtspsrc->ptcp_timeout = NULL;
629 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
634 rtspsrc = GST_RTSPSRC (object);
638 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
639 g_value_get_string (value), NULL);
642 rtspsrc->protocols = g_value_get_flags (value);
645 rtspsrc->debug = g_value_get_boolean (value);
648 rtspsrc->retry = g_value_get_uint (value);
651 rtspsrc->udp_timeout = g_value_get_uint64 (value);
653 case PROP_TCP_TIMEOUT:
654 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
657 rtspsrc->latency = g_value_get_uint (value);
659 case PROP_CONNECTION_SPEED:
660 rtspsrc->connection_speed = g_value_get_uint64 (value);
662 case PROP_NAT_METHOD:
663 rtspsrc->nat_method = g_value_get_enum (value);
666 rtspsrc->do_rtcp = g_value_get_boolean (value);
668 case PROP_DO_RTSP_KEEP_ALIVE:
669 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
672 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
674 case PROP_RTP_BLOCKSIZE:
675 rtspsrc->rtp_blocksize = g_value_get_uint (value);
678 if (rtspsrc->user_id)
679 g_free (rtspsrc->user_id);
680 rtspsrc->user_id = g_value_dup_string (value);
683 if (rtspsrc->user_pw)
684 g_free (rtspsrc->user_pw);
685 rtspsrc->user_pw = g_value_dup_string (value);
687 case PROP_BUFFER_MODE:
688 rtspsrc->buffer_mode = g_value_get_enum (value);
690 case PROP_PORT_RANGE:
694 str = g_value_get_string (value);
696 sscanf (str, "%u-%u",
697 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
699 rtspsrc->client_port_range.min = 0;
700 rtspsrc->client_port_range.max = 0;
704 case PROP_UDP_BUFFER_SIZE:
705 rtspsrc->udp_buffer_size = g_value_get_int (value);
707 case PROP_SHORT_HEADER:
708 rtspsrc->short_header = g_value_get_boolean (value);
711 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
717 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
722 rtspsrc = GST_RTSPSRC (object);
726 g_value_set_string (value, rtspsrc->conninfo.location);
729 g_value_set_flags (value, rtspsrc->protocols);
732 g_value_set_boolean (value, rtspsrc->debug);
735 g_value_set_uint (value, rtspsrc->retry);
738 g_value_set_uint64 (value, rtspsrc->udp_timeout);
740 case PROP_TCP_TIMEOUT:
744 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
745 rtspsrc->tcp_timeout.tv_usec;
746 g_value_set_uint64 (value, timeout);
750 g_value_set_uint (value, rtspsrc->latency);
752 case PROP_CONNECTION_SPEED:
753 g_value_set_uint64 (value, rtspsrc->connection_speed);
755 case PROP_NAT_METHOD:
756 g_value_set_enum (value, rtspsrc->nat_method);
759 g_value_set_boolean (value, rtspsrc->do_rtcp);
761 case PROP_DO_RTSP_KEEP_ALIVE:
762 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
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, 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, 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)
1669 event = gst_event_new_flush_start ();
1670 GST_DEBUG_OBJECT (src, "start flush");
1672 state = GST_STATE_PAUSED;
1674 event = gst_event_new_flush_stop (FALSE);
1675 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
1678 state = GST_STATE_PLAYING;
1680 state = GST_STATE_PAUSED;
1682 gst_rtspsrc_push_event (src, event);
1683 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
1685 /* to manage jitterbuffer buffer mode */
1687 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
1689 /* make running time start start at 0 again */
1690 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1691 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1693 for (i = 0; i < 2; i++) {
1695 if (stream->udpsrc[i]) {
1696 gst_element_set_state (stream->udpsrc[i], state);
1702 static GstRTSPResult
1703 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
1704 GstRTSPMessage * message, GTimeVal * timeout)
1709 ret = gst_rtsp_connection_send (conn, message, timeout);
1711 ret = GST_RTSP_ERROR;
1716 static GstRTSPResult
1717 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
1718 GstRTSPMessage * message, GTimeVal * timeout)
1723 ret = gst_rtsp_connection_receive (conn, message, timeout);
1725 ret = GST_RTSP_ERROR;
1731 gst_rtspsrc_get_position (GstRTSPSrc * src)
1736 query = gst_query_new_position (GST_FORMAT_TIME);
1737 /* should be known somewhere down the stream (e.g. jitterbuffer) */
1738 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1739 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1743 if (stream->srcpad) {
1744 if (gst_pad_query (stream->srcpad, query)) {
1745 gst_query_parse_position (query, &fmt, &pos);
1746 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
1747 GST_TIME_ARGS (pos));
1748 src->last_pos = pos;
1758 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
1760 src->state = GST_RTSP_STATE_SEEKING;
1761 /* PLAY will add the range header now. */
1762 src->need_range = TRUE;
1768 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
1773 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
1775 gboolean flush, skip;
1778 GstSegment seeksegment = { 0, };
1782 GST_DEBUG_OBJECT (src, "doing seek with event");
1784 gst_event_parse_seek (event, &rate, &format, &flags,
1785 &cur_type, &cur, &stop_type, &stop);
1787 /* no negative rates yet */
1791 /* we need TIME format */
1792 if (format != src->segment.format)
1795 GST_DEBUG_OBJECT (src, "doing seek without event");
1797 cur_type = GST_SEEK_TYPE_SET;
1798 stop_type = GST_SEEK_TYPE_SET;
1801 /* get flush flag */
1802 flush = flags & GST_SEEK_FLAG_FLUSH;
1803 skip = flags & GST_SEEK_FLAG_SKIP;
1805 /* now we need to make sure the streaming thread is stopped. We do this by
1806 * either sending a FLUSH_START event downstream which will cause the
1807 * streaming thread to stop with a WRONG_STATE.
1808 * For a non-flushing seek we simply pause the task, which will happen as soon
1809 * as it completes one iteration (and thus might block when the sink is
1810 * blocking in preroll). */
1812 GST_DEBUG_OBJECT (src, "starting flush");
1813 gst_rtspsrc_flush (src, TRUE, FALSE);
1816 gst_task_pause (src->task);
1820 /* we should now be able to grab the streaming thread because we stopped it
1821 * with the above flush/pause code */
1822 GST_RTSP_STREAM_LOCK (src);
1824 GST_DEBUG_OBJECT (src, "stopped streaming");
1826 /* copy segment, we need this because we still need the old
1827 * segment when we close the current segment. */
1828 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1830 /* configure the seek parameters in the seeksegment. We will then have the
1831 * right values in the segment to perform the seek */
1833 GST_DEBUG_OBJECT (src, "configuring seek");
1834 gst_segment_do_seek (&seeksegment, rate, format, flags,
1835 cur_type, cur, stop_type, stop, &update);
1838 /* figure out the last position we need to play. If it's configured (stop !=
1839 * -1), use that, else we play until the total duration of the file */
1840 if ((stop = seeksegment.stop) == -1)
1841 stop = seeksegment.duration;
1843 playing = (src->state == GST_RTSP_STATE_PLAYING);
1845 /* if we were playing, pause first */
1847 /* obtain current position in case seek fails */
1848 gst_rtspsrc_get_position (src);
1849 gst_rtspsrc_pause (src, FALSE);
1853 gst_rtspsrc_do_seek (src, &seeksegment);
1855 /* and continue playing */
1857 gst_rtspsrc_play (src, &seeksegment, FALSE);
1859 /* prepare for streaming again */
1861 /* if we started flush, we stop now */
1862 GST_DEBUG_OBJECT (src, "stopping flush");
1863 gst_rtspsrc_flush (src, FALSE, playing);
1866 /* now we did the seek and can activate the new segment values */
1867 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1869 /* if we're doing a segment seek, post a SEGMENT_START message */
1870 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1871 gst_element_post_message (GST_ELEMENT_CAST (src),
1872 gst_message_new_segment_start (GST_OBJECT_CAST (src),
1873 src->segment.format, src->segment.position));
1876 /* now create the newsegment */
1877 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
1878 " to %" G_GINT64_FORMAT, src->segment.position, stop);
1880 /* store the newsegment event so it can be sent from the streaming thread. */
1881 if (src->start_segment)
1882 gst_event_unref (src->start_segment);
1883 src->start_segment = gst_event_new_segment (&src->segment);
1886 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
1887 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1888 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1889 stream->discont = TRUE;
1892 GST_RTSP_STREAM_UNLOCK (src);
1899 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
1904 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
1910 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
1914 gboolean res = TRUE;
1917 src = GST_RTSPSRC_CAST (parent);
1919 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
1920 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
1922 switch (GST_EVENT_TYPE (event)) {
1923 case GST_EVENT_SEEK:
1924 res = gst_rtspsrc_perform_seek (src, event);
1928 case GST_EVENT_NAVIGATION:
1929 case GST_EVENT_LATENCY:
1937 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
1938 res = gst_pad_send_event (target, event);
1939 gst_object_unref (target);
1941 gst_event_unref (event);
1944 gst_event_unref (event);
1950 /* this is the final event function we receive on the internal source pad when
1951 * we deal with TCP connections */
1953 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
1958 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
1960 switch (GST_EVENT_TYPE (event)) {
1961 case GST_EVENT_SEEK:
1963 case GST_EVENT_NAVIGATION:
1964 case GST_EVENT_LATENCY:
1966 gst_event_unref (event);
1973 /* this is the final query function we receive on the internal source pad when
1974 * we deal with TCP connections */
1976 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
1980 gboolean res = TRUE;
1982 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
1984 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
1985 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
1987 switch (GST_QUERY_TYPE (query)) {
1988 case GST_QUERY_POSITION:
1993 case GST_QUERY_DURATION:
1997 gst_query_parse_duration (query, &format, NULL);
2000 case GST_FORMAT_TIME:
2001 gst_query_set_duration (query, format, src->segment.duration);
2009 case GST_QUERY_LATENCY:
2011 /* we are live with a min latency of 0 and unlimited max latency, this
2012 * result will be updated by the session manager if there is any. */
2013 gst_query_set_latency (query, TRUE, 0, -1);
2023 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2025 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2029 gboolean res = FALSE;
2031 src = GST_RTSPSRC_CAST (parent);
2033 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2034 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2036 switch (GST_QUERY_TYPE (query)) {
2037 case GST_QUERY_DURATION:
2041 gst_query_parse_duration (query, &format, NULL);
2044 case GST_FORMAT_TIME:
2045 gst_query_set_duration (query, format, src->segment.duration);
2053 case GST_QUERY_SEEKING:
2057 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2058 if (format == GST_FORMAT_TIME) {
2060 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2062 /* seeking without duration is unlikely */
2063 seekable = seekable && src->seekable && src->segment.duration &&
2064 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2066 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2067 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2068 src->segment.start, src->segment.stop);
2075 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2077 /* forward the query to the proxy target pad */
2079 res = gst_pad_query (target, query);
2080 gst_object_unref (target);
2089 /* callback for RTCP messages to be sent to the server when operating in TCP
2091 static GstFlowReturn
2092 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2095 GstRTSPStream *stream;
2096 GstFlowReturn res = GST_FLOW_OK;
2101 GstRTSPMessage message = { 0 };
2102 GstRTSPConnection *conn;
2104 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2105 src = stream->parent;
2107 gst_buffer_map (buffer, &map, GST_MAP_READ);
2111 gst_rtsp_message_init_data (&message, stream->channel[1]);
2113 /* lend the body data to the message */
2114 gst_rtsp_message_take_body (&message, data, size);
2116 if (stream->conninfo.connection)
2117 conn = stream->conninfo.connection;
2119 conn = src->conninfo.connection;
2121 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2122 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2123 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2125 /* and steal it away again because we will free it when unreffing the
2127 gst_rtsp_message_steal_body (&message, &data, &size);
2128 gst_rtsp_message_unset (&message);
2130 gst_buffer_unmap (buffer, &map);
2131 gst_buffer_unref (buffer);
2136 static GstPadProbeReturn
2137 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2139 GstRTSPSrc *src = user_data;
2141 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2142 GST_DEBUG_PAD_NAME (pad));
2144 /* activate the streams */
2145 GST_OBJECT_LOCK (src);
2146 if (!src->need_activate)
2149 src->need_activate = FALSE;
2150 GST_OBJECT_UNLOCK (src);
2152 gst_rtspsrc_activate_streams (src);
2154 return GST_PAD_PROBE_OK;
2158 GST_OBJECT_UNLOCK (src);
2159 return GST_PAD_PROBE_OK;
2163 /* this callback is called when the session manager generated a new src pad with
2164 * payloaded RTP packets. We simply ghost the pad here. */
2166 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2169 GstPadTemplate *template;
2172 GstRTSPStream *stream;
2175 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2177 GST_RTSP_STATE_LOCK (src);
2179 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2180 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2181 goto unknown_stream;
2183 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %d, PT %d", id, ssrc, pt);
2185 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2187 goto unknown_stream;
2189 /* create a new pad we will use to stream to */
2190 template = gst_static_pad_template_get (&rtptemplate);
2191 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2192 gst_object_unref (template);
2195 stream->added = TRUE;
2196 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2197 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2198 gst_pad_set_active (stream->srcpad, TRUE);
2199 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2201 /* check if we added all streams */
2203 for (lstream = src->streams; lstream; lstream = g_list_next (lstream)) {
2204 stream = (GstRTSPStream *) lstream->data;
2206 GST_DEBUG_OBJECT (src, "stream %p, container %d, disabled %d, added %d",
2207 stream, stream->container, stream->disabled, stream->added);
2209 /* a container stream only needs one pad added. Also disabled streams don't
2211 if (!stream->container && !stream->disabled && !stream->added) {
2216 GST_RTSP_STATE_UNLOCK (src);
2219 GST_DEBUG_OBJECT (src, "We added all streams");
2220 /* when we get here, all stream are added and we can fire the no-more-pads
2222 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2230 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2231 GST_RTSP_STATE_UNLOCK (src);
2238 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2240 GstRTSPStream *stream;
2243 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2245 GST_RTSP_STATE_LOCK (src);
2246 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2248 goto unknown_stream;
2250 caps = stream->caps;
2252 gst_caps_ref (caps);
2253 GST_RTSP_STATE_UNLOCK (src);
2259 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2260 GST_RTSP_STATE_UNLOCK (src);
2266 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2268 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2274 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2280 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2286 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2288 GstRTSPSrc *src = stream->parent;
2290 GST_DEBUG_OBJECT (src, "source in session %u received BYE", stream->id);
2292 gst_rtspsrc_do_stream_eos (src, stream);
2296 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2298 GstRTSPSrc *src = stream->parent;
2300 GST_DEBUG_OBJECT (src, "source in session %u timed out", stream->id);
2302 gst_rtspsrc_do_stream_eos (src, stream);
2306 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2308 GstRTSPStream *stream;
2310 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2312 /* get stream for session */
2313 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2315 gst_rtspsrc_do_stream_eos (src, stream);
2320 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2322 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2326 /* try to get and configure a manager */
2328 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
2329 GstRTSPTransport * transport)
2331 const gchar *manager;
2333 GstStateChangeReturn ret;
2335 /* find a manager */
2336 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
2340 GST_DEBUG_OBJECT (src, "using manager %s", manager);
2342 /* configure the manager */
2343 if (src->manager == NULL) {
2344 GObjectClass *klass;
2347 if (!(src->manager = gst_element_factory_make (manager, NULL))) {
2349 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
2353 goto use_no_manager;
2355 if (!(src->manager = gst_element_factory_make (manager, NULL)))
2356 goto manager_failed;
2359 /* we manage this element */
2360 gst_bin_add (GST_BIN_CAST (src), src->manager);
2362 GST_OBJECT_LOCK (src);
2363 target = GST_STATE_TARGET (src);
2364 GST_OBJECT_UNLOCK (src);
2366 ret = gst_element_set_state (src->manager, target);
2367 if (ret == GST_STATE_CHANGE_FAILURE)
2368 goto start_manager_failure;
2370 g_object_set (src->manager, "latency", src->latency, NULL);
2372 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2373 if (g_object_class_find_property (klass, "buffer-mode")) {
2374 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2375 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2377 gboolean need_slave;
2379 const gchar *encoding;
2381 /* buffer mode pauses are handled by adding offsets to buffer times,
2382 * but some depayloaders may have a hard time syncing output times
2383 * with such input times, e.g. container ones, most notably ASF */
2384 /* TODO alternatives are having an event that indicates these shifts,
2385 * or having rtsp extensions provide suggestion on buffer mode */
2386 need_slave = stream->container;
2387 if (stream->caps && (s = gst_caps_get_structure (stream->caps, 0)) &&
2388 (encoding = gst_structure_get_string (s, "encoding-name")))
2389 need_slave = need_slave || (strcmp (encoding, "X-ASF-PF") == 0);
2390 GST_DEBUG_OBJECT (src, "auto buffering mode, need_slave %d",
2392 /* valid duration implies not likely live pipeline,
2393 * so slaving in jitterbuffer does not make much sense
2394 * (and might mess things up due to bursts) */
2395 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
2396 src->segment.duration && !need_slave) {
2397 GST_DEBUG_OBJECT (src, "selected buffer");
2398 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER,
2401 GST_DEBUG_OBJECT (src, "selected slave");
2402 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2407 /* connect to signals if we did not already do so */
2408 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
2410 src->manager_sig_id =
2411 g_signal_connect (src->manager, "pad-added",
2412 (GCallback) new_manager_pad, src);
2413 src->manager_ptmap_id =
2414 g_signal_connect (src->manager, "request-pt-map",
2415 (GCallback) request_pt_map, src);
2417 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
2421 /* we stream directly to the manager, get some pads. Each RTSP stream goes
2422 * into a separate RTP session. */
2423 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
2424 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
2426 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
2427 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
2430 /* now configure the bandwidth in the manager */
2431 if (g_signal_lookup ("get-internal-session",
2432 G_OBJECT_TYPE (src->manager)) != 0) {
2433 GObject *rtpsession;
2435 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
2438 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
2440 stream->session = rtpsession;
2442 if (stream->as_bandwidth != -1) {
2443 GST_INFO_OBJECT (src, "setting AS: %f",
2444 (gdouble) (stream->as_bandwidth * 1000));
2445 g_object_set (rtpsession, "bandwidth",
2446 (gdouble) (stream->as_bandwidth * 1000), NULL);
2448 if (stream->rr_bandwidth != -1) {
2449 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
2450 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
2453 if (stream->rs_bandwidth != -1) {
2454 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
2455 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
2458 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2460 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
2462 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
2464 g_signal_connect (rtpsession, "on-ssrc-active",
2465 (GCallback) on_ssrc_active, stream);
2476 GST_DEBUG_OBJECT (src, "cannot get a session manager");
2481 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
2484 start_manager_failure:
2486 GST_DEBUG_OBJECT (src, "could not start session manager");
2491 /* free the UDP sources allocated when negotiating a transport.
2492 * This function is called when the server negotiated to a transport where the
2493 * UDP sources are not needed anymore, such as TCP or multicast. */
2495 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
2499 for (i = 0; i < 2; i++) {
2500 if (stream->udpsrc[i]) {
2501 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
2502 gst_object_unref (stream->udpsrc[i]);
2503 stream->udpsrc[i] = NULL;
2508 /* for TCP, create pads to send and receive data to and from the manager and to
2509 * intercept various events and queries
2512 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
2513 GstRTSPTransport * transport, GstPad ** outpad)
2516 GstPadTemplate *template;
2517 GstPad *pad0, *pad1;
2519 /* configure for interleaved delivery, nothing needs to be done
2520 * here, the loop function will call the chain functions of the
2521 * session manager. */
2522 stream->channel[0] = transport->interleaved.min;
2523 stream->channel[1] = transport->interleaved.max;
2524 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
2525 stream->channel[0], stream->channel[1]);
2527 /* we can remove the allocated UDP ports now */
2528 gst_rtspsrc_stream_free_udp (stream);
2530 /* no session manager, send data to srcpad directly */
2531 if (!stream->channelpad[0]) {
2532 GST_DEBUG_OBJECT (src, "no manager, creating pad");
2534 /* create a new pad we will use to stream to */
2535 name = g_strdup_printf ("stream_%u", stream->id);
2536 template = gst_static_pad_template_get (&rtptemplate);
2537 stream->channelpad[0] = gst_pad_new_from_template (template, name);
2538 gst_object_unref (template);
2541 /* set caps and activate */
2542 gst_pad_use_fixed_caps (stream->channelpad[0]);
2543 gst_pad_set_active (stream->channelpad[0], TRUE);
2545 *outpad = gst_object_ref (stream->channelpad[0]);
2547 GST_DEBUG_OBJECT (src, "using manager source pad");
2549 template = gst_static_pad_template_get (&anysrctemplate);
2551 /* allocate pads for sending the channel data into the manager */
2552 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
2553 gst_pad_link (pad0, stream->channelpad[0]);
2554 gst_object_unref (stream->channelpad[0]);
2555 stream->channelpad[0] = pad0;
2556 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
2557 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
2558 gst_pad_set_element_private (pad0, src);
2559 gst_pad_set_active (pad0, TRUE);
2561 if (stream->channelpad[1]) {
2562 /* if we have a sinkpad for the other channel, create a pad and link to the
2564 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
2565 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
2566 gst_pad_link (pad1, stream->channelpad[1]);
2567 gst_object_unref (stream->channelpad[1]);
2568 stream->channelpad[1] = pad1;
2569 gst_pad_set_active (pad1, TRUE);
2571 gst_object_unref (template);
2573 /* setup RTCP transport back to the server if we have to. */
2574 if (src->manager && src->do_rtcp) {
2577 template = gst_static_pad_template_get (&anysinktemplate);
2579 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
2580 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
2581 gst_pad_set_element_private (stream->rtcppad, stream);
2582 gst_pad_set_active (stream->rtcppad, TRUE);
2584 /* get session RTCP pad */
2585 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
2586 pad = gst_element_get_request_pad (src->manager, name);
2591 gst_pad_link (pad, stream->rtcppad);
2592 gst_object_unref (pad);
2595 gst_object_unref (template);
2601 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
2602 GstRTSPTransport * transport, const gchar ** destination, gint * min,
2603 gint * max, guint * ttl)
2605 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2607 if (!(*destination = transport->destination))
2608 *destination = stream->destination;
2611 /* transport first */
2612 *min = transport->port.min;
2613 *max = transport->port.max;
2614 if (*min == -1 && *max == -1) {
2615 /* then try from SDP */
2616 if (stream->port != 0) {
2617 *min = stream->port;
2618 *max = stream->port + 1;
2624 if (!(*ttl = transport->ttl))
2629 /* first take the source, then the endpoint to figure out where to send
2631 if (!(*destination = transport->source)) {
2632 if (src->conninfo.connection)
2633 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
2634 else if (stream->conninfo.connection)
2636 gst_rtsp_connection_get_ip (stream->conninfo.connection);
2640 /* for unicast we only expect the ports here */
2641 *min = transport->server_port.min;
2642 *max = transport->server_port.max;
2647 /* For multicast create UDP sources and join the multicast group. */
2649 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
2650 GstRTSPTransport * transport, GstPad ** outpad)
2653 const gchar *destination;
2656 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
2658 /* we can remove the allocated UDP ports now */
2659 gst_rtspsrc_stream_free_udp (stream);
2661 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
2664 /* we need a destination now */
2665 if (destination == NULL)
2666 goto no_destination;
2668 /* we really need ports now or we won't be able to receive anything at all */
2669 if (min == -1 && max == -1)
2672 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
2673 destination, min, max);
2675 /* creating UDP source for RTP */
2677 uri = g_strdup_printf ("udp://%s:%d", destination, min);
2679 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
2681 if (stream->udpsrc[0] == NULL)
2684 /* take ownership */
2685 gst_object_ref_sink (stream->udpsrc[0]);
2687 if (src->udp_buffer_size != 0)
2688 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
2689 src->udp_buffer_size, NULL);
2692 gst_element_set_state (stream->udpsrc[0], GST_STATE_PAUSED);
2695 /* creating another UDP source for RTCP */
2697 uri = g_strdup_printf ("udp://%s:%d", destination, max);
2699 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
2701 if (stream->udpsrc[1] == NULL)
2704 /* take ownership */
2705 gst_object_ref_sink (stream->udpsrc[1]);
2707 gst_element_set_state (stream->udpsrc[1], GST_STATE_PAUSED);
2714 GST_DEBUG_OBJECT (src, "no UDP source element found");
2719 GST_DEBUG_OBJECT (src, "no destination found");
2724 GST_DEBUG_OBJECT (src, "no ports found");
2729 /* configure the remainder of the UDP ports */
2731 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
2732 GstRTSPTransport * transport, GstPad ** outpad)
2734 /* we manage the UDP elements now. For unicast, the UDP sources where
2735 * allocated in the stream when we suggested a transport. */
2736 if (stream->udpsrc[0]) {
2737 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
2739 GST_DEBUG_OBJECT (src, "setting up UDP source");
2741 /* configure a timeout on the UDP port. When the timeout message is
2742 * posted, we assume UDP transport is not possible. We reconnect using TCP
2744 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", src->udp_timeout,
2747 /* get output pad of the UDP source. */
2748 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
2750 /* save it so we can unblock */
2751 stream->blockedpad = *outpad;
2753 /* configure pad block on the pad. As soon as there is dataflow on the
2754 * UDP source, we know that UDP is not blocked by a firewall and we can
2755 * configure all the streams to let the application autoplug decoders. */
2757 gst_pad_add_probe (stream->blockedpad,
2758 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, pad_blocked, src, NULL);
2760 if (stream->channelpad[0]) {
2761 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
2762 /* configure for UDP delivery, we need to connect the UDP pads to
2763 * the session plugin. */
2764 gst_pad_link (*outpad, stream->channelpad[0]);
2765 gst_object_unref (*outpad);
2767 /* we connected to pad-added signal to get pads from the manager */
2769 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
2774 if (stream->udpsrc[1]) {
2775 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
2777 if (stream->channelpad[1]) {
2780 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
2782 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
2783 gst_pad_link (pad, stream->channelpad[1]);
2784 gst_object_unref (pad);
2786 /* leave unlinked */
2792 /* configure the UDP sink back to the server for status reports */
2794 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
2795 GstRTSPStream * stream, GstRTSPTransport * transport)
2798 gint rtp_port, rtcp_port;
2799 gboolean do_rtp, do_rtcp;
2800 const gchar *destination;
2805 /* get transport info */
2806 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
2807 &rtp_port, &rtcp_port, &ttl);
2809 /* see what we need to do */
2810 do_rtp = (rtp_port != -1);
2811 /* it's possible that the server does not want us to send RTCP in which case
2813 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
2815 /* we need a destination when we have RTP or RTCP ports */
2816 if (destination == NULL && (do_rtp || do_rtcp))
2817 goto no_destination;
2819 /* try to construct the fakesrc to the RTP port of the server to open up any
2822 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
2825 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
2826 stream->udpsink[0] =
2827 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
2829 if (stream->udpsink[0] == NULL)
2830 goto no_sink_element;
2832 /* don't join multicast group, we will have the source socket do that */
2833 /* no sync or async state changes needed */
2834 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
2835 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
2837 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
2839 if (stream->udpsrc[0]) {
2840 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
2841 * so that NAT firewalls will open a hole for us */
2842 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
2843 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
2844 /* configure socket and make sure udpsink does not close it when shutting
2845 * down, it belongs to udpsrc after all. */
2846 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
2847 "close-socket", FALSE, NULL);
2848 g_object_unref (socket);
2851 /* the source for the dummy packets to open up NAT */
2852 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
2853 if (stream->fakesrc == NULL)
2854 goto no_fakesrc_element;
2856 /* random data in 5 buffers, a size of 200 bytes should be fine */
2857 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
2858 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
2860 /* we don't want to consider this a sink */
2861 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
2863 /* keep everything locked */
2864 gst_element_set_locked_state (stream->udpsink[0], TRUE);
2865 gst_element_set_locked_state (stream->fakesrc, TRUE);
2867 gst_object_ref (stream->udpsink[0]);
2868 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
2869 gst_object_ref (stream->fakesrc);
2870 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
2872 gst_element_link (stream->fakesrc, stream->udpsink[0]);
2875 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
2878 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
2879 stream->udpsink[1] =
2880 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
2882 if (stream->udpsink[1] == NULL)
2883 goto no_sink_element;
2885 /* don't join multicast group, we will have the source socket do that */
2886 /* no sync or async state changes needed */
2887 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
2888 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
2890 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
2892 if (stream->udpsrc[1]) {
2893 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
2894 * because some servers check the port number of where it sends RTCP to identify
2895 * the RTCP packets it receives */
2896 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
2897 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
2898 /* configure socket and make sure udpsink does not close it when shutting
2899 * down, it belongs to udpsrc after all. */
2900 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
2901 "close-socket", FALSE, NULL);
2902 g_object_unref (socket);
2905 /* we don't want to consider this a sink */
2906 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
2908 /* we keep this playing always */
2909 gst_element_set_locked_state (stream->udpsink[1], TRUE);
2910 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
2912 gst_object_ref (stream->udpsink[1]);
2913 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
2915 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
2917 /* get session RTCP pad */
2918 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
2919 pad = gst_element_get_request_pad (src->manager, name);
2924 gst_pad_link (pad, stream->rtcppad);
2925 gst_object_unref (pad);
2934 GST_DEBUG_OBJECT (src, "no destination address specified");
2939 GST_DEBUG_OBJECT (src, "no UDP sink element found");
2944 GST_DEBUG_OBJECT (src, "no fakesrc element found");
2949 /* sets up all elements needed for streaming over the specified transport.
2950 * Does not yet expose the element pads, this will be done when there is actuall
2951 * dataflow detected, which might never happen when UDP is blocked in a
2952 * firewall, for example.
2955 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
2956 GstRTSPTransport * transport)
2959 GstPad *outpad = NULL;
2960 GstPadTemplate *template;
2965 src = stream->parent;
2967 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
2969 s = gst_caps_get_structure (stream->caps, 0);
2971 /* get the proper mime type for this stream now */
2972 if (gst_rtsp_transport_get_mime (transport->trans, &mime) < 0)
2973 goto unknown_transport;
2975 goto unknown_transport;
2977 /* configure the final mime type */
2978 GST_DEBUG_OBJECT (src, "setting mime to %s", mime);
2979 gst_structure_set_name (s, mime);
2981 /* try to get and configure a manager, channelpad[0-1] will be configured with
2982 * the pads for the manager, or NULL when no manager is needed. */
2983 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
2986 switch (transport->lower_transport) {
2987 case GST_RTSP_LOWER_TRANS_TCP:
2988 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
2989 goto transport_failed;
2991 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
2992 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
2993 goto transport_failed;
2994 /* fallthrough, the rest is the same for UDP and MCAST */
2995 case GST_RTSP_LOWER_TRANS_UDP:
2996 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
2997 goto transport_failed;
2998 /* configure udpsinks back to the server for RTCP messages and for the
2999 * dummy RTP messages to open NAT. */
3000 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3001 goto transport_failed;
3004 goto unknown_transport;
3008 GST_DEBUG_OBJECT (src, "creating ghostpad");
3010 gst_pad_use_fixed_caps (outpad);
3012 /* create ghostpad, don't add just yet, this will be done when we activate
3014 name = g_strdup_printf ("stream_%u", stream->id);
3015 template = gst_static_pad_template_get (&rtptemplate);
3016 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3017 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3018 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3019 gst_object_unref (template);
3022 gst_object_unref (outpad);
3024 /* mark pad as ok */
3025 stream->last_ret = GST_FLOW_OK;
3032 GST_DEBUG_OBJECT (src, "failed to configure transport");
3037 GST_DEBUG_OBJECT (src, "unknown transport");
3042 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3047 /* send a couple of dummy random packets on the receiver RTP port to the server,
3048 * this should make a firewall think we initiated the data transfer and
3049 * hopefully allow packets to go from the sender port to our RTP receiver port */
3051 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3055 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3058 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3059 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3061 if (stream->fakesrc && stream->udpsink[0]) {
3062 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3063 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3064 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3065 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3066 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3072 /* Adds the source pads of all configured streams to the element.
3073 * This code is performed when we detected dataflow.
3075 * We detect dataflow from either the _loop function or with pad probes on the
3079 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3083 GST_DEBUG_OBJECT (src, "activating streams");
3085 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3086 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3088 if (stream->udpsrc[0]) {
3089 /* remove timeout, we are streaming now and timeouts will be handled by
3090 * the session manager and jitter buffer */
3091 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3093 if (stream->srcpad) {
3094 /* if we don't have a session manager, set the caps now. If we have a
3095 * session, we will get a notification of the pad and the caps. */
3096 if (!src->manager) {
3097 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3098 gst_pad_set_caps (stream->srcpad, stream->caps);
3101 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3102 gst_pad_set_active (stream->srcpad, TRUE);
3104 if (!stream->added) {
3105 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3106 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3107 stream->added = TRUE;
3112 /* unblock all pads */
3113 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3114 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3116 if (stream->blockid) {
3117 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3118 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3119 stream->blockid = 0;
3127 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
3128 gboolean reset_manager)
3131 guint64 start, stop;
3132 gdouble play_speed, play_scale;
3134 GST_DEBUG_OBJECT (src, "configuring stream caps");
3136 start = segment->position;
3137 stop = segment->duration;
3138 play_speed = segment->rate;
3139 play_scale = segment->applied_rate;
3141 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3142 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3145 if ((caps = stream->caps)) {
3146 caps = gst_caps_make_writable (caps);
3148 if (stream->timebase != -1)
3149 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3150 (guint) stream->timebase, NULL);
3151 if (stream->seqbase != -1)
3152 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3153 (guint) stream->seqbase, NULL);
3154 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3156 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3157 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3158 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3160 stream->caps = caps;
3162 GST_DEBUG_OBJECT (src, "stream %p, caps %" GST_PTR_FORMAT, stream, caps);
3164 if (reset_manager && src->manager) {
3165 GST_DEBUG_OBJECT (src, "clear session");
3166 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3170 static GstFlowReturn
3171 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3176 /* store the value */
3177 stream->last_ret = ret;
3179 /* if it's success we can return the value right away */
3180 if (ret == GST_FLOW_OK)
3183 /* any other error that is not-linked can be returned right
3185 if (ret != GST_FLOW_NOT_LINKED)
3188 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3189 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3190 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3192 ret = ostream->last_ret;
3193 /* some other return value (must be SUCCESS but we can return
3194 * other values as well) */
3195 if (ret != GST_FLOW_NOT_LINKED)
3198 /* if we get here, all other pads were unlinked and we return
3199 * NOT_LINKED then */
3205 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
3208 gboolean res = TRUE;
3210 /* only streams that have a connection to the outside world */
3211 if (stream->srcpad == NULL)
3214 if (stream->udpsrc[0]) {
3215 gst_event_ref (event);
3216 res = gst_element_send_event (stream->udpsrc[0], event);
3217 } else if (stream->channelpad[0]) {
3218 gst_event_ref (event);
3219 if (GST_PAD_IS_SRC (stream->channelpad[0]))
3220 res = gst_pad_push_event (stream->channelpad[0], event);
3222 res = gst_pad_send_event (stream->channelpad[0], event);
3225 if (stream->udpsrc[1]) {
3226 gst_event_ref (event);
3227 res &= gst_element_send_event (stream->udpsrc[1], event);
3228 } else if (stream->channelpad[1]) {
3229 gst_event_ref (event);
3230 if (GST_PAD_IS_SRC (stream->channelpad[1]))
3231 res &= gst_pad_push_event (stream->channelpad[1], event);
3233 res &= gst_pad_send_event (stream->channelpad[1], event);
3237 gst_event_unref (event);
3243 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
3246 gboolean res = TRUE;
3248 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3249 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3251 gst_event_ref (event);
3252 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
3254 gst_event_unref (event);
3259 static GstRTSPResult
3260 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3265 if (info->connection == NULL) {
3266 if (info->url == NULL) {
3267 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
3268 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
3272 /* create connection */
3273 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
3274 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
3275 goto could_not_create;
3278 g_free (info->url_str);
3279 info->url_str = gst_rtsp_url_get_request_uri (info->url);
3281 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
3283 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
3284 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
3286 if (src->proxy_host) {
3287 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
3289 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
3294 if (!info->connected) {
3297 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
3298 ("Connecting to %s", info->location));
3299 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
3301 gst_rtsp_connection_connect (info->connection,
3302 src->ptcp_timeout)) < 0)
3303 goto could_not_connect;
3305 info->connected = TRUE;
3312 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
3317 gchar *str = gst_rtsp_strresult (res);
3318 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
3324 gchar *str = gst_rtsp_strresult (res);
3325 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
3331 static GstRTSPResult
3332 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
3335 if (info->connected) {
3336 GST_DEBUG_OBJECT (src, "closing connection...");
3337 gst_rtsp_connection_close (info->connection);
3338 info->connected = FALSE;
3340 if (free && info->connection) {
3341 /* free connection */
3342 GST_DEBUG_OBJECT (src, "freeing connection...");
3343 gst_rtsp_connection_free (info->connection);
3344 info->connection = NULL;
3349 static GstRTSPResult
3350 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3355 GST_DEBUG_OBJECT (src, "reconnecting connection...");
3356 gst_rtsp_conninfo_close (src, info, FALSE);
3357 res = gst_rtsp_conninfo_connect (src, info, async);
3363 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
3367 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
3368 if (src->conninfo.connection) {
3369 GST_DEBUG_OBJECT (src, "connection flush");
3370 gst_rtsp_connection_flush (src->conninfo.connection, flush);
3372 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3373 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3374 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
3375 if (stream->conninfo.connection)
3376 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
3380 /* FIXME, handle server request, reply with OK, for now */
3381 static GstRTSPResult
3382 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
3383 GstRTSPMessage * request)
3385 GstRTSPMessage response = { 0 };
3388 GST_DEBUG_OBJECT (src, "got server request message");
3391 gst_rtsp_message_dump (request);
3393 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
3395 if (res == GST_RTSP_ENOTIMPL) {
3396 /* default implementation, send OK */
3398 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
3403 GST_DEBUG_OBJECT (src, "replying with OK");
3406 gst_rtsp_message_dump (&response);
3408 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
3412 gst_rtsp_message_unset (&response);
3413 } else if (res == GST_RTSP_EEOF)
3421 gst_rtsp_message_unset (&response);
3426 /* send server keep-alive */
3427 static GstRTSPResult
3428 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
3430 GstRTSPMessage request = { 0 };
3432 GstRTSPMethod method;
3435 if (src->do_rtsp_keep_alive == FALSE) {
3436 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
3437 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3441 GST_DEBUG_OBJECT (src, "creating server keep-alive");
3443 /* find a method to use for keep-alive */
3444 if (src->methods & GST_RTSP_GET_PARAMETER)
3445 method = GST_RTSP_GET_PARAMETER;
3447 method = GST_RTSP_OPTIONS;
3450 control = src->control;
3452 control = src->conninfo.url_str;
3454 if (control == NULL)
3457 res = gst_rtsp_message_init_request (&request, method, control);
3462 gst_rtsp_message_dump (&request);
3465 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
3470 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3471 gst_rtsp_message_unset (&request);
3478 GST_WARNING_OBJECT (src, "no control url to send keepalive");
3483 gchar *str = gst_rtsp_strresult (res);
3485 gst_rtsp_message_unset (&request);
3486 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
3487 ("Could not send keep-alive. (%s)", str));
3493 static GstFlowReturn
3494 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
3496 GstRTSPMessage message = { 0 };
3499 GstRTSPStream *stream;
3500 GstPad *outpad = NULL;
3503 GstFlowReturn ret = GST_FLOW_OK;
3505 gboolean is_rtcp, have_data;
3507 /* here we are only interested in data messages */
3510 GTimeVal tv_timeout;
3512 /* get the next timeout interval */
3513 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3515 /* see if the timeout period expired */
3516 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
3517 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
3518 /* send keep-alive, only act on interrupt, a warning will be posted for
3520 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3522 /* get new timeout */
3523 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3526 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
3527 tv_timeout.tv_sec, tv_timeout.tv_usec);
3529 /* protect the connection with the connection lock so that we can see when
3530 * we are finished doing server communication */
3532 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
3533 &message, src->ptcp_timeout);
3537 GST_DEBUG_OBJECT (src, "we received a server message");
3539 case GST_RTSP_EINTR:
3540 /* we got interrupted this means we need to stop */
3542 case GST_RTSP_ETIMEOUT:
3543 /* no reply, send keep alive */
3544 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
3545 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3549 /* go EOS when the server closed the connection */
3555 switch (message.type) {
3556 case GST_RTSP_MESSAGE_REQUEST:
3557 /* server sends us a request message, handle it */
3559 gst_rtspsrc_handle_request (src, src->conninfo.connection,
3561 if (res == GST_RTSP_EEOF)
3564 goto handle_request_failed;
3566 case GST_RTSP_MESSAGE_RESPONSE:
3567 /* we ignore response messages */
3568 GST_DEBUG_OBJECT (src, "ignoring response message");
3570 gst_rtsp_message_dump (&message);
3572 case GST_RTSP_MESSAGE_DATA:
3573 GST_DEBUG_OBJECT (src, "got data message");
3577 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
3584 channel = message.type_data.data.channel;
3586 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
3588 goto unknown_stream;
3590 if (channel == stream->channel[0]) {
3591 outpad = stream->channelpad[0];
3593 } else if (channel == stream->channel[1]) {
3594 outpad = stream->channelpad[1];
3600 /* take a look at the body to figure out what we have */
3601 gst_rtsp_message_get_body (&message, &data, &size);
3603 goto invalid_length;
3605 /* channels are not correct on some servers, do extra check */
3606 if (data[1] >= 200 && data[1] <= 204) {
3607 /* hmm RTCP message switch to the RTCP pad of the same stream. */
3608 outpad = stream->channelpad[1];
3612 /* we have no clue what this is, just ignore then. */
3614 goto unknown_stream;
3616 /* take the message body for further processing */
3617 gst_rtsp_message_steal_body (&message, &data, &size);
3619 /* strip the trailing \0 */
3622 buf = gst_buffer_new ();
3623 gst_buffer_append_memory (buf,
3624 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
3626 /* don't need message anymore */
3627 gst_rtsp_message_unset (&message);
3629 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
3632 if (src->need_activate) {
3633 gst_rtspsrc_activate_streams (src);
3634 src->need_activate = FALSE;
3637 if (src->base_time == -1) {
3638 /* Take current running_time. This timestamp will be put on
3639 * the first buffer of each stream because we are a live source and so we
3640 * timestamp with the running_time. When we are dealing with TCP, we also
3641 * only timestamp the first buffer (using the DISCONT flag) because a server
3642 * typically bursts data, for which we don't want to compensate by speeding
3643 * up the media. The other timestamps will be interpollated from this one
3644 * using the RTP timestamps. */
3645 GST_OBJECT_LOCK (src);
3646 if (GST_ELEMENT_CLOCK (src)) {
3648 GstClockTime base_time;
3650 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
3651 base_time = GST_ELEMENT_CAST (src)->base_time;
3653 src->base_time = now - base_time;
3655 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
3656 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
3658 GST_OBJECT_UNLOCK (src);
3661 if (stream->discont && !is_rtcp) {
3662 /* mark first RTP buffer as discont */
3663 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
3664 stream->discont = FALSE;
3665 /* first buffer gets the timestamp, other buffers are not timestamped and
3666 * their presentation time will be interpollated from the rtp timestamps. */
3667 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
3668 GST_TIME_ARGS (src->base_time));
3670 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
3673 /* chain to the peer pad */
3674 if (GST_PAD_IS_SINK (outpad))
3675 ret = gst_pad_chain (outpad, buf);
3677 ret = gst_pad_push (outpad, buf);
3680 /* combine all stream flows for the data transport */
3681 ret = gst_rtspsrc_combine_flows (src, stream, ret);
3688 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
3689 gst_rtsp_message_unset (&message);
3694 GST_DEBUG_OBJECT (src, "we got an eof from the server");
3695 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3696 ("The server closed the connection."));
3697 src->conninfo.connected = FALSE;
3698 gst_rtsp_message_unset (&message);
3699 return GST_FLOW_EOS;
3703 gst_rtsp_message_unset (&message);
3704 GST_DEBUG_OBJECT (src, "got interrupted: stop connection flush");
3705 gst_rtspsrc_connection_flush (src, FALSE);
3706 return GST_FLOW_FLUSHING;
3710 gchar *str = gst_rtsp_strresult (res);
3712 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
3713 ("Could not receive message. (%s)", str));
3716 gst_rtsp_message_unset (&message);
3717 return GST_FLOW_ERROR;
3719 handle_request_failed:
3721 gchar *str = gst_rtsp_strresult (res);
3723 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
3724 ("Could not handle server message. (%s)", str));
3726 gst_rtsp_message_unset (&message);
3727 return GST_FLOW_ERROR;
3731 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3732 ("Short message received, ignoring."));
3733 gst_rtsp_message_unset (&message);
3738 static GstFlowReturn
3739 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
3742 GstRTSPMessage message = { 0 };
3746 GTimeVal tv_timeout;
3748 /* get the next timeout interval */
3749 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3751 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
3752 (gint) tv_timeout.tv_sec);
3754 gst_rtsp_message_unset (&message);
3756 /* we should continue reading the TCP socket because the server might
3757 * send us requests. When the session timeout expires, we need to send a
3758 * keep-alive request to keep the session open. */
3759 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
3760 &message, &tv_timeout);
3764 GST_DEBUG_OBJECT (src, "we received a server message");
3766 case GST_RTSP_EINTR:
3767 /* we got interrupted, see what we have to do */
3769 case GST_RTSP_ETIMEOUT:
3770 /* send keep-alive, ignore the result, a warning will be posted. */
3771 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
3772 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3776 /* server closed the connection. not very fatal for UDP, reconnect and
3777 * see what happens. */
3778 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3779 ("The server closed the connection."));
3781 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
3786 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
3788 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3789 ("Unhandled return value %d.", res));
3793 switch (message.type) {
3794 case GST_RTSP_MESSAGE_REQUEST:
3795 /* server sends us a request message, handle it */
3797 gst_rtspsrc_handle_request (src, src->conninfo.connection,
3799 if (res == GST_RTSP_EEOF)
3802 goto handle_request_failed;
3804 case GST_RTSP_MESSAGE_RESPONSE:
3805 /* we ignore response and data messages */
3806 GST_DEBUG_OBJECT (src, "ignoring response message");
3808 gst_rtsp_message_dump (&message);
3809 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
3810 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
3811 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
3812 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
3813 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3820 case GST_RTSP_MESSAGE_DATA:
3821 /* we ignore response and data messages */
3822 GST_DEBUG_OBJECT (src, "ignoring data message");
3825 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
3831 /* we get here when the connection got interrupted */
3834 gst_rtsp_message_unset (&message);
3835 GST_DEBUG_OBJECT (src, "got interrupted: stop connection flush");
3836 gst_rtspsrc_connection_flush (src, FALSE);
3837 return GST_FLOW_FLUSHING;
3841 gchar *str = gst_rtsp_strresult (res);
3844 src->conninfo.connected = FALSE;
3845 if (res != GST_RTSP_EINTR) {
3846 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
3847 ("Could not connect to server. (%s)", str));
3849 ret = GST_FLOW_ERROR;
3851 ret = GST_FLOW_FLUSHING;
3857 gchar *str = gst_rtsp_strresult (res);
3859 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
3860 ("Could not receive message. (%s)", str));
3862 return GST_FLOW_ERROR;
3864 handle_request_failed:
3866 gchar *str = gst_rtsp_strresult (res);
3869 gst_rtsp_message_unset (&message);
3870 if (res != GST_RTSP_EINTR) {
3871 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
3872 ("Could not handle server message. (%s)", str));
3874 ret = GST_FLOW_ERROR;
3876 ret = GST_FLOW_FLUSHING;
3882 GST_DEBUG_OBJECT (src, "we got an eof from the server");
3883 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3884 ("The server closed the connection."));
3885 src->conninfo.connected = FALSE;
3886 gst_rtsp_message_unset (&message);
3887 return GST_FLOW_EOS;
3891 static GstRTSPResult
3892 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
3894 GstRTSPResult res = GST_RTSP_OK;
3897 GST_DEBUG_OBJECT (src, "doing reconnect");
3899 GST_OBJECT_LOCK (src);
3900 /* only restart when the pads were not yet activated, else we were
3901 * streaming over UDP */
3902 restart = src->need_activate;
3903 GST_OBJECT_UNLOCK (src);
3905 /* no need to restart, we're done */
3909 /* we can try only TCP now */
3910 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
3912 /* close and cleanup our state */
3913 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
3916 /* see if we have TCP left to try. Also don't try TCP when we were configured
3918 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
3921 /* We post a warning message now to inform the user
3922 * that nothing happened. It's most likely a firewall thing. */
3923 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3924 ("Could not receive any UDP packets for %.4f seconds, maybe your "
3925 "firewall is blocking it. Retrying using a TCP connection.",
3926 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
3928 /* open new connection using tcp */
3929 if (gst_rtspsrc_open (src, async) < 0)
3932 /* start playback */
3933 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
3942 src->cur_protocols = 0;
3943 /* no transport possible, post an error and stop */
3944 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
3945 ("Could not receive any UDP packets for %.4f seconds, maybe your "
3946 "firewall is blocking it. No other protocols to try.",
3947 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
3948 return GST_RTSP_ERROR;
3952 GST_DEBUG_OBJECT (src, "open failed");
3957 GST_DEBUG_OBJECT (src, "play failed");
3963 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
3967 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
3970 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
3973 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
3976 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
3984 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
3988 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
3991 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
3994 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
3997 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4005 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4009 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4012 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4015 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4018 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4026 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4030 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
4033 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4036 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4039 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4047 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4049 if (ret == GST_RTSP_OK)
4050 gst_rtspsrc_loop_complete_cmd (src, cmd);
4051 else if (ret == GST_RTSP_EINTR)
4052 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4054 gst_rtspsrc_loop_error_cmd (src, cmd);
4058 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
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->pending_cmd;
4069 if (old != CMD_WAIT) {
4070 src->pending_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->pending_cmd = cmd;
4077 /* interrupt if allowed */
4078 if (src->busy_cmd & mask) {
4079 GST_DEBUG_OBJECT (src, "connection flush busy %d", src->busy_cmd);
4080 gst_rtspsrc_connection_flush (src, TRUE);
4082 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %d", src->busy_cmd);
4085 gst_task_start (src->task);
4086 GST_OBJECT_UNLOCK (src);
4090 gst_rtspsrc_loop (GstRTSPSrc * src)
4094 if (!src->conninfo.connection || !src->conninfo.connected)
4097 if (src->interleaved)
4098 ret = gst_rtspsrc_loop_interleaved (src);
4100 ret = gst_rtspsrc_loop_udp (src);
4102 if (ret != GST_FLOW_OK)
4110 GST_WARNING_OBJECT (src, "we are not connected");
4111 ret = GST_FLOW_FLUSHING;
4116 const gchar *reason = gst_flow_get_name (ret);
4118 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
4119 src->running = FALSE;
4120 if (ret == GST_FLOW_EOS) {
4121 /* perform EOS logic */
4122 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4123 gst_element_post_message (GST_ELEMENT_CAST (src),
4124 gst_message_new_segment_done (GST_OBJECT_CAST (src),
4125 src->segment.format, src->segment.position));
4126 gst_rtspsrc_push_event (src,
4127 gst_event_new_segment_done (src->segment.format,
4128 src->segment.position));
4130 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4132 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
4133 /* for fatal errors we post an error message, post the error before the
4134 * EOS so the app knows about the error first. */
4135 GST_ELEMENT_ERROR (src, STREAM, FAILED,
4136 ("Internal data flow error."),
4137 ("streaming task paused, reason %s (%d)", reason, ret));
4138 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4144 #ifndef GST_DISABLE_GST_DEBUG
4145 static const gchar *
4146 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
4150 while (method != 0) {
4167 static const gchar *
4168 gst_rtspsrc_skip_lws (const gchar * s)
4170 while (g_ascii_isspace (*s))
4175 static const gchar *
4176 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
4178 while (s > start && g_ascii_isspace (*(s - 1)))
4183 static const gchar *
4184 gst_rtspsrc_skip_commas (const gchar * s)
4186 /* The grammar allows for multiple commas */
4187 while (g_ascii_isspace (*s) || *s == ',')
4192 static const gchar *
4193 gst_rtspsrc_skip_item (const gchar * s)
4195 gboolean quoted = FALSE;
4196 const gchar *start = s;
4198 /* A list item ends at the last non-whitespace character
4199 * before a comma which is not inside a quoted-string. Or at
4200 * the end of the string.
4206 if (*s == '\\' && *(s + 1))
4215 return gst_rtspsrc_unskip_lws (s, start);
4219 gst_rtsp_decode_quoted_string (gchar * quoted_string)
4223 src = quoted_string + 1;
4224 dst = quoted_string;
4225 while (*src && *src != '"') {
4226 if (*src == '\\' && *(src + 1))
4233 /* Extract the authentication tokens that the server provided for each method
4234 * into an array of structures and give those to the connection object.
4237 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
4238 const gchar * header, gboolean * stale)
4240 GSList *list = NULL, *iter;
4242 gchar *item, *eq, *name_end, *value;
4244 g_return_if_fail (stale != NULL);
4246 gst_rtsp_connection_clear_auth_params (conn);
4249 /* Parse a header whose content is described by RFC2616 as
4250 * "#something", where "something" does not itself contain commas,
4251 * except as part of quoted-strings, into a list of allocated strings.
4253 header = gst_rtspsrc_skip_commas (header);
4255 end = gst_rtspsrc_skip_item (header);
4256 list = g_slist_prepend (list, g_strndup (header, end - header));
4257 header = gst_rtspsrc_skip_commas (end);
4262 list = g_slist_reverse (list);
4263 for (iter = list; iter; iter = iter->next) {
4266 eq = strchr (item, '=');
4268 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
4269 if (name_end == item) {
4270 /* That's no good... */
4277 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
4279 gst_rtsp_decode_quoted_string (value);
4283 if (item && (strcmp (item, "stale") == 0) &&
4284 value && (strcmp (value, "TRUE") == 0))
4286 gst_rtsp_connection_set_auth_param (conn, item, value);
4290 g_slist_free (list);
4293 /* Parse a WWW-Authenticate Response header and determine the
4294 * available authentication methods
4296 * This code should also cope with the fact that each WWW-Authenticate
4297 * header can contain multiple challenge methods + tokens
4299 * At the moment, for Basic auth, we just do a minimal check and don't
4300 * even parse out the realm */
4302 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
4303 GstRTSPConnection * conn, gboolean * stale)
4307 g_return_if_fail (hdr != NULL);
4308 g_return_if_fail (methods != NULL);
4309 g_return_if_fail (stale != NULL);
4311 /* Skip whitespace at the start of the string */
4312 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
4314 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
4315 *methods |= GST_RTSP_AUTH_BASIC;
4316 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
4317 *methods |= GST_RTSP_AUTH_DIGEST;
4318 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
4323 * gst_rtspsrc_setup_auth:
4324 * @src: the rtsp source
4326 * Configure a username and password and auth method on the
4327 * connection object based on a response we received from the
4330 * Currently, this requires that a username and password were supplied
4331 * in the uri. In the future, they may be requested on demand by sending
4332 * a message up the bus.
4334 * Returns: TRUE if authentication information could be set up correctly.
4337 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
4341 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
4342 GstRTSPAuthMethod method;
4343 GstRTSPResult auth_result;
4345 GstRTSPConnection *conn;
4347 gboolean stale = FALSE;
4349 conn = src->conninfo.connection;
4351 /* Identify the available auth methods and see if any are supported */
4352 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
4353 &hdr, 0) == GST_RTSP_OK) {
4354 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
4357 if (avail_methods == GST_RTSP_AUTH_NONE)
4358 goto no_auth_available;
4360 /* For digest auth, if the response indicates that the session
4361 * data are stale, we just update them in the connection object and
4362 * return TRUE to retry the request */
4364 src->tried_url_auth = FALSE;
4366 url = gst_rtsp_connection_get_url (conn);
4368 /* Do we have username and password available? */
4369 if (url != NULL && !src->tried_url_auth && url->user != NULL
4370 && url->passwd != NULL) {
4373 src->tried_url_auth = TRUE;
4374 GST_DEBUG_OBJECT (src,
4375 "Attempting authentication using credentials from the URL");
4377 user = src->user_id;
4378 pass = src->user_pw;
4379 GST_DEBUG_OBJECT (src,
4380 "Attempting authentication using credentials from the properties");
4383 /* FIXME: If the url didn't contain username and password or we tried them
4384 * already, request a username and passwd from the application via some kind
4385 * of credentials request message */
4387 /* If we don't have a username and passwd at this point, bail out. */
4388 if (user == NULL || pass == NULL)
4391 /* Try to configure for each available authentication method, strongest to
4393 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
4394 /* Check if this method is available on the server */
4395 if ((method & avail_methods) == 0)
4398 /* Pass the credentials to the connection to try on the next request */
4399 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
4400 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
4401 * ignore it and end up retrying later */
4402 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
4403 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
4404 gst_rtsp_auth_method_to_string (method));
4409 if (method == GST_RTSP_AUTH_NONE)
4410 goto no_auth_available;
4416 /* Output an error indicating that we couldn't connect because there were
4417 * no supported authentication protocols */
4418 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4419 ("No supported authentication protocol was found"));
4424 /* We don't fire an error message, we just return FALSE and let the
4425 * normal NOT_AUTHORIZED error be propagated */
4430 static GstRTSPResult
4431 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4432 GstRTSPMessage * request, GstRTSPMessage * response,
4433 GstRTSPStatusCode * code)
4436 GstRTSPStatusCode thecode;
4437 gchar *content_base = NULL;
4441 if (!src->short_header)
4442 gst_rtsp_ext_list_before_send (src->extensions, request);
4444 GST_DEBUG_OBJECT (src, "sending message");
4447 gst_rtsp_message_dump (request);
4449 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
4453 gst_rtsp_connection_reset_timeout (conn);
4456 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
4461 gst_rtsp_message_dump (response);
4463 switch (response->type) {
4464 case GST_RTSP_MESSAGE_REQUEST:
4465 res = gst_rtspsrc_handle_request (src, conn, response);
4466 if (res == GST_RTSP_EEOF)
4469 goto handle_request_failed;
4471 case GST_RTSP_MESSAGE_RESPONSE:
4472 /* ok, a response is good */
4473 GST_DEBUG_OBJECT (src, "received response message");
4475 case GST_RTSP_MESSAGE_DATA:
4476 /* get next response */
4477 GST_DEBUG_OBJECT (src, "ignoring data response message");
4480 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4485 thecode = response->type_data.response.code;
4487 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
4489 /* if the caller wanted the result code, we store it. */
4493 /* If the request didn't succeed, bail out before doing any more */
4494 if (thecode != GST_RTSP_STS_OK)
4497 /* store new content base if any */
4498 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
4501 g_free (src->content_base);
4502 src->content_base = g_strdup (content_base);
4504 gst_rtsp_ext_list_after_send (src->extensions, request, response);
4511 gchar *str = gst_rtsp_strresult (res);
4513 if (res != GST_RTSP_EINTR) {
4514 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4515 ("Could not send message. (%s)", str));
4517 GST_WARNING_OBJECT (src, "send interrupted");
4526 GST_WARNING_OBJECT (src, "server closed connection, doing reconnect");
4529 /* if reconnect succeeds, try again */
4531 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
4535 /* only try once after reconnect, then fallthrough and error out */
4538 gchar *str = gst_rtsp_strresult (res);
4540 if (res != GST_RTSP_EINTR) {
4541 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4542 ("Could not receive message. (%s)", str));
4544 GST_WARNING_OBJECT (src, "receive interrupted");
4552 handle_request_failed:
4554 /* ERROR was posted */
4555 gst_rtsp_message_unset (response);
4560 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4561 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4562 ("The server closed the connection."));
4563 gst_rtsp_message_unset (response);
4570 * @src: the rtsp source
4571 * @conn: the connection to send on
4572 * @request: must point to a valid request
4573 * @response: must point to an empty #GstRTSPMessage
4574 * @code: an optional code result
4576 * send @request and retrieve the response in @response. optionally @code can be
4577 * non-NULL in which case it will contain the status code of the response.
4579 * If This function returns #GST_RTSP_OK, @response will contain a valid response
4580 * message that should be cleaned with gst_rtsp_message_unset() after usage.
4582 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
4583 * @response message) if the response code was not 200 (OK).
4585 * If the attempt results in an authentication failure, then this will attempt
4586 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
4589 * Returns: #GST_RTSP_OK if the processing was successful.
4591 static GstRTSPResult
4592 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4593 GstRTSPMessage * request, GstRTSPMessage * response,
4594 GstRTSPStatusCode * code)
4596 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
4597 GstRTSPResult res = GST_RTSP_ERROR;
4600 GstRTSPMethod method = GST_RTSP_INVALID;
4606 /* make sure we don't loop forever */
4610 /* save method so we can disable it when the server complains */
4611 method = request->type_data.request.method;
4614 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
4618 case GST_RTSP_STS_UNAUTHORIZED:
4619 if (gst_rtspsrc_setup_auth (src, response)) {
4620 /* Try the request/response again after configuring the auth info
4628 } while (retry == TRUE);
4630 /* If the user requested the code, let them handle errors, otherwise
4631 * post an error below */
4634 else if (int_code != GST_RTSP_STS_OK)
4635 goto error_response;
4642 GST_DEBUG_OBJECT (src, "got error %d", res);
4647 res = GST_RTSP_ERROR;
4649 switch (response->type_data.response.code) {
4650 case GST_RTSP_STS_NOT_FOUND:
4651 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
4652 response->type_data.response.reason));
4654 case GST_RTSP_STS_MOVED_PERMANENTLY:
4655 case GST_RTSP_STS_MOVE_TEMPORARILY:
4657 gchar *new_location;
4658 GstRTSPLowerTrans transports;
4660 GST_DEBUG_OBJECT (src, "got redirection");
4661 /* if we don't have a Location Header, we must error */
4662 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
4663 &new_location, 0) < 0)
4666 /* When we receive a redirect result, we go back to the INIT state after
4667 * parsing the new URI. The caller should do the needed steps to issue
4668 * a new setup when it detects this state change. */
4669 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
4671 /* save current transports */
4672 if (src->conninfo.url)
4673 transports = src->conninfo.url->transports;
4675 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
4677 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
4679 /* set old transports */
4680 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
4681 src->conninfo.url->transports = transports;
4683 src->need_redirect = TRUE;
4684 src->state = GST_RTSP_STATE_INIT;
4688 case GST_RTSP_STS_NOT_ACCEPTABLE:
4689 case GST_RTSP_STS_NOT_IMPLEMENTED:
4690 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
4691 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
4692 gst_rtsp_method_as_text (method));
4693 src->methods &= ~method;
4697 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4698 ("Got error response: %d (%s).", response->type_data.response.code,
4699 response->type_data.response.reason));
4702 /* if we return ERROR we should unset the response ourselves */
4703 if (res == GST_RTSP_ERROR)
4704 gst_rtsp_message_unset (response);
4710 static GstRTSPResult
4711 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
4712 GstRTSPMessage * response, GstRTSPSrc * src)
4714 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
4719 /* parse the response and collect all the supported methods. We need this
4720 * information so that we don't try to send an unsupported request to the
4724 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
4726 GstRTSPHeaderField field;
4732 /* reset supported methods */
4735 /* Try Allow Header first */
4736 field = GST_RTSP_HDR_ALLOW;
4739 gst_rtsp_message_get_header (response, field, &respoptions, indx);
4740 if (indx == 0 && !respoptions) {
4741 /* if no Allow header was found then try the Public header... */
4742 field = GST_RTSP_HDR_PUBLIC;
4743 gst_rtsp_message_get_header (response, field, &respoptions, indx);
4748 /* If we get here, the server gave a list of supported methods, parse
4749 * them here. The string is like:
4751 * OPTIONS, DESCRIBE, ANNOUNCE, PLAY, SETUP, ...
4753 options = g_strsplit (respoptions, ",", 0);
4755 for (i = 0; options[i]; i++) {
4759 stripped = g_strstrip (options[i]);
4760 method = gst_rtsp_find_method (stripped);
4762 /* keep bitfield of supported methods */
4763 if (method != GST_RTSP_INVALID)
4764 src->methods |= method;
4766 g_strfreev (options);
4771 if (src->methods == 0) {
4772 /* neither Allow nor Public are required, assume the server supports
4773 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
4775 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
4776 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
4778 /* always assume PLAY, FIXME, extensions should be able to override
4780 src->methods |= GST_RTSP_PLAY;
4781 /* also assume it will support Range */
4782 src->seekable = TRUE;
4784 /* we need describe and setup */
4785 if (!(src->methods & GST_RTSP_DESCRIBE))
4787 if (!(src->methods & GST_RTSP_SETUP))
4795 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4796 ("Server does not support DESCRIBE."));
4801 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4802 ("Server does not support SETUP."));
4807 /* masks to be kept in sync with the hardcoded protocol order of preference
4809 static guint protocol_masks[] = {
4810 GST_RTSP_LOWER_TRANS_UDP,
4811 GST_RTSP_LOWER_TRANS_UDP_MCAST,
4812 GST_RTSP_LOWER_TRANS_TCP,
4816 static GstRTSPResult
4817 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
4818 GstRTSPLowerTrans protocols, gchar ** transports)
4822 gboolean add_udp_str;
4827 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
4832 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
4834 /* extension listed transports, use those */
4835 if (*transports != NULL)
4838 /* it's the default */
4839 add_udp_str = FALSE;
4841 /* the default RTSP transports */
4842 result = g_string_new ("");
4843 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
4844 GST_DEBUG_OBJECT (src, "adding UDP unicast");
4846 g_string_append (result, "RTP/AVP");
4848 g_string_append (result, "/UDP");
4849 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
4850 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
4851 GST_DEBUG_OBJECT (src, "adding UDP multicast");
4853 /* we don't have to allocate any UDP ports yet, if the selected transport
4854 * turns out to be multicast we can create them and join the multicast
4855 * group indicated in the transport reply */
4856 if (result->len > 0)
4857 g_string_append (result, ",");
4858 g_string_append (result, "RTP/AVP");
4860 g_string_append (result, "/UDP");
4861 g_string_append (result, ";multicast");
4862 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
4863 GST_DEBUG_OBJECT (src, "adding TCP");
4865 if (result->len > 0)
4866 g_string_append (result, ",");
4867 g_string_append (result, "RTP/AVP/TCP;unicast;interleaved=%%i1-%%i2");
4869 *transports = g_string_free (result, FALSE);
4871 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
4882 static GstRTSPResult
4883 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
4884 gint orig_rtpport, gint orig_rtcpport)
4887 gint nr_udp, nr_int;
4889 gint rtpport = 0, rtcpport = 0;
4892 src = stream->parent;
4894 /* find number of placeholders first */
4895 if (strstr (*transports, "%%i2"))
4897 else if (strstr (*transports, "%%i1"))
4902 if (strstr (*transports, "%%u2"))
4904 else if (strstr (*transports, "%%u1"))
4909 if (nr_udp == 0 && nr_int == 0)
4913 if (!orig_rtpport || !orig_rtcpport) {
4914 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
4917 rtpport = orig_rtpport;
4918 rtcpport = orig_rtcpport;
4922 str = g_string_new ("");
4924 while ((next = strstr (p, "%%"))) {
4925 g_string_append_len (str, p, next - p);
4926 if (next[2] == 'u') {
4928 g_string_append_printf (str, "%d", rtpport);
4929 else if (next[3] == '2')
4930 g_string_append_printf (str, "%d", rtcpport);
4932 if (next[2] == 'i') {
4934 g_string_append_printf (str, "%d", src->free_channel);
4935 else if (next[3] == '2')
4936 g_string_append_printf (str, "%d", src->free_channel + 1);
4941 /* append final part */
4942 g_string_append (str, p);
4944 g_free (*transports);
4945 *transports = g_string_free (str, FALSE);
4953 return GST_RTSP_ERROR;
4958 gst_rtspsrc_stream_is_real_media (GstRTSPStream * stream)
4960 gboolean res = FALSE;
4964 const gchar *enc = NULL;
4966 s = gst_caps_get_structure (stream->caps, 0);
4967 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
4968 res = (strstr (enc, "-REAL") != NULL);
4974 /* Perform the SETUP request for all the streams.
4976 * We ask the server for a specific transport, which initially includes all the
4977 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
4978 * two local UDP ports that we send to the server.
4980 * Once the server replied with a transport, we configure the other streams
4981 * with the same transport.
4983 * This function will also configure the stream for the selected transport,
4984 * which basically means creating the pipeline.
4986 static GstRTSPResult
4987 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
4990 GstRTSPResult res = GST_RTSP_ERROR;
4991 GstRTSPMessage request = { 0 };
4992 GstRTSPMessage response = { 0 };
4993 GstRTSPStream *stream = NULL;
4994 GstRTSPLowerTrans protocols;
4995 GstRTSPStatusCode code;
4996 gboolean unsupported_real = FALSE;
4997 gint rtpport, rtcpport;
5001 if (src->conninfo.connection) {
5002 url = gst_rtsp_connection_get_url (src->conninfo.connection);
5003 /* we initially allow all configured lower transports. based on the URL
5004 * transports and the replies from the server we narrow them down. */
5005 protocols = url->transports & src->cur_protocols;
5008 protocols = src->cur_protocols;
5014 /* reset some state */
5015 src->free_channel = 0;
5016 src->interleaved = FALSE;
5017 src->need_activate = FALSE;
5018 /* keep track of next port number, 0 is random */
5019 src->next_port_num = src->client_port_range.min;
5020 rtpport = rtcpport = 0;
5022 if (G_UNLIKELY (src->streams == NULL))
5025 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5026 GstRTSPConnection *conn;
5031 stream = (GstRTSPStream *) walk->data;
5033 /* see if we need to configure this stream */
5034 if (!gst_rtsp_ext_list_configure_stream (src->extensions, stream->caps)) {
5035 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
5037 stream->disabled = TRUE;
5041 /* merge/overwrite global caps */
5046 s = gst_caps_get_structure (stream->caps, 0);
5048 num = gst_structure_n_fields (src->props);
5049 for (j = 0; j < num; j++) {
5053 name = gst_structure_nth_field_name (src->props, j);
5054 val = gst_structure_get_value (src->props, name);
5055 gst_structure_set_value (s, name, val);
5057 GST_DEBUG_OBJECT (src, "copied %s", name);
5061 /* skip setup if we have no URL for it */
5062 if (stream->conninfo.location == NULL) {
5063 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
5067 if (src->conninfo.connection == NULL) {
5068 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
5069 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
5072 conn = stream->conninfo.connection;
5074 conn = src->conninfo.connection;
5076 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
5077 stream->conninfo.location);
5079 /* if we have a multicast connection, only suggest multicast from now on */
5080 if (stream->is_multicast)
5081 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
5084 /* first selectable protocol */
5085 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5087 if (!protocol_masks[mask])
5091 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
5092 protocol_masks[mask]);
5093 /* create a string with first transport in line */
5095 res = gst_rtspsrc_create_transports_string (src,
5096 protocols & protocol_masks[mask], &transports);
5097 if (res < 0 || transports == NULL)
5098 goto setup_transport_failed;
5100 if (strlen (transports) == 0) {
5101 g_free (transports);
5102 GST_DEBUG_OBJECT (src, "no transports found");
5107 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
5109 /* replace placeholders with real values, this function will optionally
5110 * allocate UDP ports and other info needed to execute the setup request */
5111 res = gst_rtspsrc_prepare_transports (stream, &transports,
5112 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
5114 g_free (transports);
5115 goto setup_transport_failed;
5118 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
5120 /* create SETUP request */
5122 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
5123 stream->conninfo.location);
5125 g_free (transports);
5126 goto create_request_failed;
5129 /* select transport, copy is made when adding to header so we can free it. */
5130 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
5131 g_free (transports);
5133 /* if the user wants a non default RTP packet size we add the blocksize
5135 if (src->rtp_blocksize > 0) {
5136 hval = g_strdup_printf ("%d", src->rtp_blocksize);
5137 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
5142 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
5145 /* handle the code ourselves */
5146 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
5150 case GST_RTSP_STS_OK:
5152 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
5153 gst_rtsp_message_unset (&request);
5154 gst_rtsp_message_unset (&response);
5155 /* cleanup of leftover transport */
5156 gst_rtspsrc_stream_free_udp (stream);
5157 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
5158 * we might be in this case */
5159 if (stream->container && rtpport && rtcpport && !retry) {
5160 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
5165 /* this transport did not go down well, but we may have others to try
5166 * that we did not send yet, try those and only give up then
5167 * but not without checking for lost cause/extension so we can
5168 * post a nicer/more useful error message later */
5169 if (!unsupported_real)
5170 unsupported_real = gst_rtspsrc_stream_is_real_media (stream);
5171 /* select next available protocol, give up on this stream if none */
5173 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5175 if (!protocol_masks[mask] || unsupported_real)
5180 /* cleanup of leftover transport and move to the next stream */
5181 gst_rtspsrc_stream_free_udp (stream);
5182 goto response_error;
5185 /* parse response transport */
5187 gchar *resptrans = NULL;
5188 GstRTSPTransport transport = { 0 };
5190 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
5193 gst_rtspsrc_stream_free_udp (stream);
5197 /* parse transport, go to next stream on parse error */
5198 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
5199 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
5203 /* update allowed transports for other streams. once the transport of
5204 * one stream has been determined, we make sure that all other streams
5205 * are configured in the same way */
5206 switch (transport.lower_transport) {
5207 case GST_RTSP_LOWER_TRANS_TCP:
5208 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
5209 protocols = GST_RTSP_LOWER_TRANS_TCP;
5210 src->interleaved = TRUE;
5211 /* update free channels */
5213 MAX (transport.interleaved.min, src->free_channel);
5215 MAX (transport.interleaved.max, src->free_channel);
5216 src->free_channel++;
5218 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
5219 /* only allow multicast for other streams */
5220 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
5221 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
5223 case GST_RTSP_LOWER_TRANS_UDP:
5224 /* only allow unicast for other streams */
5225 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
5226 protocols = GST_RTSP_LOWER_TRANS_UDP;
5229 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
5230 transport.lower_transport);
5234 if (!stream->container || (!src->interleaved && !retry)) {
5235 /* now configure the stream with the selected transport */
5236 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
5237 GST_DEBUG_OBJECT (src,
5238 "could not configure stream %p transport, skipping stream",
5241 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
5242 /* retain the first allocated UDP port pair */
5243 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
5244 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
5247 /* we need to activate at least one streams when we detect activity */
5248 src->need_activate = TRUE;
5250 /* clean up our transport struct */
5251 gst_rtsp_transport_init (&transport);
5252 /* clean up used RTSP messages */
5253 gst_rtsp_message_unset (&request);
5254 gst_rtsp_message_unset (&response);
5258 /* store the transport protocol that was configured */
5259 src->cur_protocols = protocols;
5261 gst_rtsp_ext_list_stream_select (src->extensions, url);
5263 /* if there is nothing to activate, error out */
5264 if (!src->need_activate)
5265 goto nothing_to_activate;
5272 /* no transport possible, post an error and stop */
5273 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5274 ("Could not connect to server, no protocols left"));
5275 return GST_RTSP_ERROR;
5279 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5280 ("SDP contains no streams"));
5281 return GST_RTSP_ERROR;
5283 create_request_failed:
5285 gchar *str = gst_rtsp_strresult (res);
5287 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5288 ("Could not create request. (%s)", str));
5292 setup_transport_failed:
5294 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5295 ("Could not setup transport."));
5296 res = GST_RTSP_ERROR;
5301 const gchar *str = gst_rtsp_status_as_text (code);
5303 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5304 ("Error (%d): %s", code, GST_STR_NULL (str)));
5305 res = GST_RTSP_ERROR;
5310 gchar *str = gst_rtsp_strresult (res);
5312 if (res != GST_RTSP_EINTR) {
5313 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5314 ("Could not send message. (%s)", str));
5316 GST_WARNING_OBJECT (src, "send interrupted");
5323 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5324 ("Server did not select transport."));
5325 res = GST_RTSP_ERROR;
5328 nothing_to_activate:
5330 /* none of the available error codes is really right .. */
5331 if (unsupported_real) {
5332 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5333 (_("No supported stream was found. You might need to install a "
5334 "GStreamer RTSP extension plugin for Real media streams.")),
5337 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5338 (_("No supported stream was found. You might need to allow "
5339 "more transport protocols or may otherwise be missing "
5340 "the right GStreamer RTSP extension plugin.")), (NULL));
5342 return GST_RTSP_ERROR;
5346 gst_rtsp_message_unset (&request);
5347 gst_rtsp_message_unset (&response);
5353 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
5354 GstSegment * segment)
5357 GstRTSPTimeRange *therange;
5360 gst_rtsp_range_free (src->range);
5362 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
5363 GST_DEBUG_OBJECT (src, "parsed range %s", range);
5364 src->range = therange;
5366 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
5368 gst_segment_init (segment, GST_FORMAT_TIME);
5372 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
5373 therange->min.type, therange->min.seconds, therange->max.type,
5374 therange->max.seconds);
5376 if (therange->min.type == GST_RTSP_TIME_NOW)
5378 else if (therange->min.type == GST_RTSP_TIME_END)
5381 seconds = therange->min.seconds * GST_SECOND;
5383 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
5384 GST_TIME_ARGS (seconds));
5386 /* we need to start playback without clipping from the position reported by
5388 segment->start = seconds;
5389 segment->position = seconds;
5391 if (therange->max.type == GST_RTSP_TIME_NOW)
5393 else if (therange->max.type == GST_RTSP_TIME_END)
5396 seconds = therange->max.seconds * GST_SECOND;
5398 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
5399 GST_TIME_ARGS (seconds));
5401 /* live (WMS) server might send overflowed large max as its idea of infinity,
5402 * compensate to prevent problems later on */
5403 if (seconds != -1 && seconds < 0) {
5405 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
5408 /* live (WMS) might send min == max, which is not worth recording */
5409 if (segment->duration == -1 && seconds == segment->start)
5412 /* don't change duration with unknown value, we might have a valid value
5413 * there that we want to keep. */
5415 segment->duration = seconds;
5420 /* must be called with the RTSP state lock */
5421 static GstRTSPResult
5422 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
5428 /* prepare global stream caps properties */
5430 gst_structure_remove_all_fields (src->props);
5432 src->props = gst_structure_new_empty ("RTSPProperties");
5435 gst_sdp_message_dump (sdp);
5437 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
5439 gst_segment_init (&src->segment, GST_FORMAT_TIME);
5441 /* parse range for duration reporting. */
5446 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
5450 /* keep track of the range and configure it in the segment */
5451 if (gst_rtspsrc_parse_range (src, range, &src->segment))
5455 /* try to find a global control attribute. Note that a '*' means that we should
5456 * do aggregate control with the current url (so we don't do anything and
5457 * leave the current connection as is) */
5459 const gchar *control;
5462 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
5463 if (control == NULL)
5466 /* only take fully qualified urls */
5467 if (g_str_has_prefix (control, "rtsp://"))
5471 g_free (src->conninfo.location);
5472 src->conninfo.location = g_strdup (control);
5473 /* make a connection for this, if there was a connection already, nothing
5475 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
5476 GST_ERROR_OBJECT (src, "could not connect");
5479 /* we need to keep the control url separate from the connection url because
5480 * the rules for constructing the media control url need it */
5481 g_free (src->control);
5482 src->control = g_strdup (control);
5485 /* create streams */
5486 n_streams = gst_sdp_message_medias_len (sdp);
5487 for (i = 0; i < n_streams; i++) {
5488 gst_rtspsrc_create_stream (src, sdp, i);
5491 src->state = GST_RTSP_STATE_INIT;
5494 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
5497 /* reset our state */
5498 src->need_range = TRUE;
5501 src->state = GST_RTSP_STATE_READY;
5508 GST_ERROR_OBJECT (src, "setup failed");
5513 static GstRTSPResult
5514 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
5518 GstRTSPMessage request = { 0 };
5519 GstRTSPMessage response = { 0 };
5522 gchar *respcont = NULL;
5525 src->need_redirect = FALSE;
5527 /* can't continue without a valid url */
5528 if (G_UNLIKELY (src->conninfo.url == NULL)) {
5529 res = GST_RTSP_EINVAL;
5532 src->tried_url_auth = FALSE;
5534 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
5535 goto connect_failed;
5537 /* create OPTIONS */
5538 GST_DEBUG_OBJECT (src, "create options...");
5540 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
5541 src->conninfo.url_str);
5543 goto create_request_failed;
5546 GST_DEBUG_OBJECT (src, "send options...");
5549 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
5552 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
5557 if (!gst_rtspsrc_parse_methods (src, &response))
5560 /* create DESCRIBE */
5561 GST_DEBUG_OBJECT (src, "create describe...");
5563 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
5564 src->conninfo.url_str);
5566 goto create_request_failed;
5568 /* we only accept SDP for now */
5569 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
5573 GST_DEBUG_OBJECT (src, "send describe...");
5576 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
5579 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
5583 /* we only perform redirect for the describe, currently */
5584 if (src->need_redirect) {
5585 /* close connection, we don't have to send a TEARDOWN yet, ignore the
5587 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5589 gst_rtsp_message_unset (&request);
5590 gst_rtsp_message_unset (&response);
5596 /* it could be that the DESCRIBE method was not implemented */
5597 if (!src->methods & GST_RTSP_DESCRIBE)
5600 /* check if reply is SDP */
5601 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
5603 /* could not be set but since the request returned OK, we assume it
5604 * was SDP, else check it. */
5606 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
5607 goto wrong_content_type;
5610 /* get message body and parse as SDP */
5611 gst_rtsp_message_get_body (&response, &data, &size);
5612 if (data == NULL || size == 0)
5615 GST_DEBUG_OBJECT (src, "parse SDP...");
5616 gst_sdp_message_new (sdp);
5617 gst_sdp_message_parse_buffer (data, size, *sdp);
5619 /* clean up any messages */
5620 gst_rtsp_message_unset (&request);
5621 gst_rtsp_message_unset (&response);
5628 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
5629 ("No valid RTSP URL was provided"));
5634 gchar *str = gst_rtsp_strresult (res);
5636 if (res != GST_RTSP_EINTR) {
5637 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
5638 ("Failed to connect. (%s)", str));
5640 GST_WARNING_OBJECT (src, "connect interrupted");
5645 create_request_failed:
5647 gchar *str = gst_rtsp_strresult (res);
5649 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5650 ("Could not create request. (%s)", str));
5656 /* Don't post a message - the rtsp_send method will have
5657 * taken care of it because we passed NULL for the response code */
5662 /* error was posted */
5663 res = GST_RTSP_ERROR;
5668 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5669 ("Server does not support SDP, got %s.", respcont));
5670 res = GST_RTSP_ERROR;
5675 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5676 ("Server can not provide an SDP."));
5677 res = GST_RTSP_ERROR;
5682 if (src->conninfo.connection) {
5683 GST_DEBUG_OBJECT (src, "free connection");
5684 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5686 gst_rtsp_message_unset (&request);
5687 gst_rtsp_message_unset (&response);
5692 static GstRTSPResult
5693 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
5698 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
5700 if (src->sdp == NULL) {
5701 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
5705 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
5710 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
5717 GST_WARNING_OBJECT (src, "can't get sdp");
5718 src->open_error = TRUE;
5723 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
5724 src->open_error = TRUE;
5729 static GstRTSPResult
5730 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
5732 GstRTSPMessage request = { 0 };
5733 GstRTSPMessage response = { 0 };
5734 GstRTSPResult res = GST_RTSP_OK;
5738 GST_DEBUG_OBJECT (src, "TEARDOWN...");
5740 if (src->state < GST_RTSP_STATE_READY) {
5741 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
5748 /* construct a control url */
5750 control = src->control;
5752 control = src->conninfo.url_str;
5754 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
5757 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5758 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5760 GstRTSPConnInfo *info;
5762 /* try aggregate control first but do non-aggregate control otherwise */
5764 setup_url = control;
5765 else if ((setup_url = stream->conninfo.location) == NULL)
5768 if (src->conninfo.connection) {
5769 info = &src->conninfo;
5770 } else if (stream->conninfo.connection) {
5771 info = &stream->conninfo;
5775 if (!info->connected)
5780 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
5782 goto create_request_failed;
5785 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
5788 gst_rtspsrc_send (src, info->connection, &request, &response,
5792 /* FIXME, parse result? */
5793 gst_rtsp_message_unset (&request);
5794 gst_rtsp_message_unset (&response);
5797 /* early exit when we did aggregate control */
5803 /* close connections */
5804 GST_DEBUG_OBJECT (src, "closing connection...");
5805 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5806 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5807 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5808 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
5812 gst_rtspsrc_cleanup (src);
5814 src->state = GST_RTSP_STATE_INVALID;
5817 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
5822 create_request_failed:
5824 gchar *str = gst_rtsp_strresult (res);
5826 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5827 ("Could not create request. (%s)", str));
5833 gchar *str = gst_rtsp_strresult (res);
5835 gst_rtsp_message_unset (&request);
5836 if (res != GST_RTSP_EINTR) {
5837 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5838 ("Could not send message. (%s)", str));
5840 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
5847 GST_DEBUG_OBJECT (src,
5848 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
5853 /* RTP-Info is of the format:
5855 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
5857 * rtptime corresponds to the timestamp for the NPT time given in the header
5858 * seqbase corresponds to the next sequence number we received. This number
5859 * indicates the first seqnum after the seek and should be used to discard
5860 * packets that are from before the seek.
5863 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
5868 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
5870 infos = g_strsplit (rtpinfo, ",", 0);
5871 for (i = 0; infos[i]; i++) {
5873 GstRTSPStream *stream;
5877 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
5879 /* init values, types of seqbase and timebase are bigger than needed so we
5880 * can store -1 as uninitialized values */
5885 /* parse url, find stream for url.
5886 * parse seq and rtptime. The seq number should be configured in the rtp
5887 * depayloader or session manager to detect gaps. Same for the rtptime, it
5888 * should be used to create an initial time newsegment. */
5889 fields = g_strsplit (infos[i], ";", 0);
5890 for (j = 0; fields[j]; j++) {
5891 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
5892 /* remove leading whitespace */
5893 fields[j] = g_strchug (fields[j]);
5894 if (g_str_has_prefix (fields[j], "url=")) {
5895 /* get the url and the stream */
5897 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
5898 } else if (g_str_has_prefix (fields[j], "seq=")) {
5899 seqbase = atoi (fields[j] + 4);
5900 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
5901 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
5904 g_strfreev (fields);
5905 /* now we need to store the values for the caps of the stream */
5906 if (stream != NULL) {
5907 GST_DEBUG_OBJECT (src,
5908 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
5909 stream, seqbase, timebase);
5911 /* we have a stream, configure detected params */
5912 stream->seqbase = seqbase;
5913 stream->timebase = timebase;
5922 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
5927 interval = strtoul (rtcp, NULL, 10);
5928 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
5933 interval *= GST_MSECOND;
5935 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5936 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5938 /* already (optionally) retrieved this when configuring manager */
5939 if (stream->session) {
5940 GObject *rtpsession = stream->session;
5942 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
5944 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
5948 /* now it happens that (Xenon) server sending this may also provide bogus
5949 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
5950 * and just use RTP-Info to sync */
5952 GObjectClass *klass;
5954 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
5955 if (g_object_class_find_property (klass, "rtcp-sync")) {
5956 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
5957 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
5963 gst_rtspsrc_get_float (const gchar * dstr)
5965 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
5967 /* canonicalise floating point string so we can handle float strings
5968 * in the form "24.930" or "24,930" irrespective of the current locale */
5969 g_strlcpy (s, dstr, sizeof (s));
5970 g_strdelimit (s, ",", '.');
5971 return g_ascii_strtod (s, NULL);
5975 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
5977 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
5979 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
5980 g_strlcpy (val_str, "now", sizeof (val_str));
5982 if (segment->position == 0) {
5983 g_strlcpy (val_str, "0", sizeof (val_str));
5985 g_ascii_dtostr (val_str, sizeof (val_str),
5986 ((gdouble) segment->position) / GST_SECOND);
5989 return g_strdup_printf ("npt=%s-", val_str);
5993 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
5995 stream->timebase = -1;
5996 stream->seqbase = -1;
6000 stream->caps = gst_caps_make_writable (stream->caps);
6001 s = gst_caps_get_structure (stream->caps, 0);
6002 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
6006 static GstRTSPResult
6007 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
6009 GstRTSPResult res = GST_RTSP_OK;
6011 if (src->state < GST_RTSP_STATE_READY) {
6012 res = GST_RTSP_ERROR;
6013 if (src->open_error) {
6014 GST_DEBUG_OBJECT (src, "the stream was in error");
6018 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
6020 if ((res = gst_rtspsrc_open (src, async)) < 0) {
6021 GST_DEBUG_OBJECT (src, "failed to open stream");
6030 static GstRTSPResult
6031 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
6033 GstRTSPMessage request = { 0 };
6034 GstRTSPMessage response = { 0 };
6035 GstRTSPResult res = GST_RTSP_OK;
6041 GST_DEBUG_OBJECT (src, "PLAY...");
6043 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6046 if (!(src->methods & GST_RTSP_PLAY))
6049 if (src->state == GST_RTSP_STATE_PLAYING)
6052 if (!src->conninfo.connection || !src->conninfo.connected)
6055 /* send some dummy packets before we activate the receive in the
6057 gst_rtspsrc_send_dummy_packets (src);
6059 /* activate receive elements;
6060 * only in async case, since receive elements may not have been affected
6061 * by overall state change (e.g. not around yet),
6062 * do not mess with state in sync case (e.g. seeking) */
6064 gst_element_set_state (GST_ELEMENT_CAST (src), GST_STATE_PLAYING);
6066 /* construct a control url */
6068 control = src->control;
6070 control = src->conninfo.url_str;
6072 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6073 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6075 GstRTSPConnection *conn;
6077 /* try aggregate control first but do non-aggregate control otherwise */
6079 setup_url = control;
6080 else if ((setup_url = stream->conninfo.location) == NULL)
6083 if (src->conninfo.connection) {
6084 conn = src->conninfo.connection;
6085 } else if (stream->conninfo.connection) {
6086 conn = stream->conninfo.connection;
6092 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
6094 goto create_request_failed;
6096 if (src->need_range) {
6097 hval = gen_range_header (src, segment);
6099 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_RANGE, hval);
6103 if (segment->rate != 1.0) {
6104 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
6106 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
6108 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
6110 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
6114 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
6116 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6119 /* seek may have silently failed as it is not supported */
6120 if (!(src->methods & GST_RTSP_PLAY)) {
6121 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
6122 /* obviously it is supported as we made it here */
6123 src->methods |= GST_RTSP_PLAY;
6124 src->seekable = FALSE;
6125 /* but there is nothing to parse in the response,
6126 * so convey we have no idea and not to expect anything particular */
6127 clear_rtp_base (src, stream);
6131 /* need to do for all streams */
6132 for (run = src->streams; run; run = g_list_next (run))
6133 clear_rtp_base (src, (GstRTSPStream *) run->data);
6135 /* NOTE the above also disables npt based eos detection */
6136 /* and below forces position to 0,
6137 * which is visible feedback we lost the plot */
6138 segment->start = segment->position = src->last_pos;
6141 gst_rtsp_message_unset (&request);
6143 /* parse RTP npt field. This is the current position in the stream (Normal
6144 * Play Time) and should be put in the NEWSEGMENT position field. */
6145 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
6147 gst_rtspsrc_parse_range (src, hval, segment);
6149 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
6150 segment->rate = 1.0;
6152 /* parse Speed header. This is the intended playback rate of the stream
6153 * and should be put in the NEWSEGMENT rate field. */
6154 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
6155 0) == GST_RTSP_OK) {
6156 segment->rate = gst_rtspsrc_get_float (hval);
6157 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
6158 &hval, 0) == GST_RTSP_OK) {
6159 segment->rate = gst_rtspsrc_get_float (hval);
6162 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
6163 * for the RTP packets. If this is not present, we assume all starts from 0...
6164 * This is info for the RTP session manager that we pass to it in caps. */
6166 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
6167 &hval, hval_idx++) == GST_RTSP_OK)
6168 gst_rtspsrc_parse_rtpinfo (src, hval);
6170 /* some servers indicate RTCP parameters in PLAY response,
6171 * rather than properly in SDP */
6172 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
6173 &hval, 0) == GST_RTSP_OK)
6174 gst_rtspsrc_handle_rtcp_interval (src, hval);
6176 gst_rtsp_message_unset (&response);
6178 /* early exit when we did aggregate control */
6182 /* configure the caps of the streams after we parsed all headers. Only reset
6183 * the manager object when we set a new Range header (we did a seek) */
6184 gst_rtspsrc_configure_caps (src, segment, src->need_range);
6186 /* set again when needed */
6187 src->need_range = FALSE;
6189 src->running = TRUE;
6190 src->base_time = -1;
6191 src->state = GST_RTSP_STATE_PLAYING;
6194 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
6195 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6196 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6197 stream->discont = TRUE;
6202 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
6209 GST_DEBUG_OBJECT (src, "failed to open stream");
6214 GST_DEBUG_OBJECT (src, "PLAY is not supported");
6219 GST_DEBUG_OBJECT (src, "we were already PLAYING");
6222 create_request_failed:
6224 gchar *str = gst_rtsp_strresult (res);
6226 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6227 ("Could not create request. (%s)", str));
6233 gchar *str = gst_rtsp_strresult (res);
6235 gst_rtsp_message_unset (&request);
6236 if (res != GST_RTSP_EINTR) {
6237 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6238 ("Could not send message. (%s)", str));
6240 GST_WARNING_OBJECT (src, "PLAY interrupted");
6247 static GstRTSPResult
6248 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
6250 GstRTSPResult res = GST_RTSP_OK;
6251 GstRTSPMessage request = { 0 };
6252 GstRTSPMessage response = { 0 };
6256 GST_DEBUG_OBJECT (src, "PAUSE...");
6258 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6261 if (!(src->methods & GST_RTSP_PAUSE))
6264 if (src->state == GST_RTSP_STATE_READY)
6267 if (!src->conninfo.connection || !src->conninfo.connected)
6270 /* construct a control url */
6272 control = src->control;
6274 control = src->conninfo.url_str;
6276 /* loop over the streams. We might exit the loop early when we could do an
6277 * aggregate control */
6278 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6279 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6280 GstRTSPConnection *conn;
6283 /* try aggregate control first but do non-aggregate control otherwise */
6285 setup_url = control;
6286 else if ((setup_url = stream->conninfo.location) == NULL)
6289 if (src->conninfo.connection) {
6290 conn = src->conninfo.connection;
6291 } else if (stream->conninfo.connection) {
6292 conn = stream->conninfo.connection;
6298 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
6299 ("Sending PAUSE request"));
6302 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
6304 goto create_request_failed;
6306 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6309 gst_rtsp_message_unset (&request);
6310 gst_rtsp_message_unset (&response);
6312 /* exit early when we did agregate control */
6318 src->state = GST_RTSP_STATE_READY;
6322 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
6329 GST_DEBUG_OBJECT (src, "failed to open stream");
6334 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
6339 GST_DEBUG_OBJECT (src, "we were already PAUSED");
6342 create_request_failed:
6344 gchar *str = gst_rtsp_strresult (res);
6346 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6347 ("Could not create request. (%s)", str));
6353 gchar *str = gst_rtsp_strresult (res);
6355 gst_rtsp_message_unset (&request);
6356 if (res != GST_RTSP_EINTR) {
6357 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6358 ("Could not send message. (%s)", str));
6360 GST_WARNING_OBJECT (src, "PAUSE interrupted");
6368 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
6370 GstRTSPSrc *rtspsrc;
6372 rtspsrc = GST_RTSPSRC (bin);
6374 switch (GST_MESSAGE_TYPE (message)) {
6375 case GST_MESSAGE_EOS:
6376 gst_message_unref (message);
6378 case GST_MESSAGE_ELEMENT:
6380 const GstStructure *s = gst_message_get_structure (message);
6382 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
6383 gboolean ignore_timeout;
6385 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
6387 GST_OBJECT_LOCK (rtspsrc);
6388 ignore_timeout = rtspsrc->ignore_timeout;
6389 rtspsrc->ignore_timeout = TRUE;
6390 GST_OBJECT_UNLOCK (rtspsrc);
6392 /* we only act on the first udp timeout message, others are irrelevant
6393 * and can be ignored. */
6394 if (!ignore_timeout)
6395 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
6397 gst_message_unref (message);
6400 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6403 case GST_MESSAGE_ERROR:
6406 GstRTSPStream *stream;
6409 udpsrc = GST_MESSAGE_SRC (message);
6411 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
6412 GST_ELEMENT_NAME (udpsrc));
6414 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
6418 /* we ignore the RTCP udpsrc */
6419 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
6422 /* if we get error messages from the udp sources, that's not a problem as
6423 * long as not all of them error out. We also don't really know what the
6424 * problem is, the message does not give enough detail... */
6425 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
6426 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
6427 if (ret != GST_FLOW_OK)
6431 gst_message_unref (message);
6435 /* fatal but not our message, forward */
6436 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6441 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6447 /* the thread where everything happens */
6449 gst_rtspsrc_thread (GstRTSPSrc * src)
6453 GST_OBJECT_LOCK (src);
6454 cmd = src->pending_cmd;
6455 src->pending_cmd = CMD_WAIT;
6456 GST_DEBUG_OBJECT (src, "got command %d", cmd);
6458 /* we got the message command, so ensure communication is possible again */
6459 gst_rtspsrc_connection_flush (src, FALSE);
6461 src->busy_cmd = cmd;
6462 GST_OBJECT_UNLOCK (src);
6466 gst_rtspsrc_open (src, TRUE);
6469 gst_rtspsrc_play (src, &src->segment, TRUE);
6472 gst_rtspsrc_pause (src, TRUE);
6475 gst_rtspsrc_close (src, TRUE, FALSE);
6478 gst_rtspsrc_loop (src);
6481 gst_rtspsrc_reconnect (src, FALSE);
6487 GST_OBJECT_LOCK (src);
6488 /* and go back to sleep */
6489 if (src->pending_cmd == CMD_WAIT) {
6491 gst_task_pause (src->task);
6494 src->busy_cmd = CMD_WAIT;
6495 GST_OBJECT_UNLOCK (src);
6499 gst_rtspsrc_start (GstRTSPSrc * src)
6501 GST_DEBUG_OBJECT (src, "starting");
6503 GST_OBJECT_LOCK (src);
6505 src->pending_cmd = CMD_WAIT;
6507 if (src->task == NULL) {
6508 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
6509 if (src->task == NULL)
6512 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
6514 GST_OBJECT_UNLOCK (src);
6521 GST_ERROR_OBJECT (src, "failed to create task");
6527 gst_rtspsrc_stop (GstRTSPSrc * src)
6531 GST_DEBUG_OBJECT (src, "stopping");
6533 /* also cancels pending task */
6534 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_CLOSE);
6536 GST_OBJECT_LOCK (src);
6537 if ((task = src->task)) {
6539 GST_OBJECT_UNLOCK (src);
6541 gst_task_stop (task);
6543 /* make sure it is not running */
6544 GST_RTSP_STREAM_LOCK (src);
6545 GST_RTSP_STREAM_UNLOCK (src);
6547 /* now wait for the task to finish */
6548 gst_task_join (task);
6550 /* and free the task */
6551 gst_object_unref (GST_OBJECT (task));
6553 GST_OBJECT_LOCK (src);
6555 GST_OBJECT_UNLOCK (src);
6557 /* ensure synchronously all is closed and clean */
6558 gst_rtspsrc_close (src, FALSE, TRUE);
6563 static GstStateChangeReturn
6564 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
6566 GstRTSPSrc *rtspsrc;
6567 GstStateChangeReturn ret;
6569 rtspsrc = GST_RTSPSRC (element);
6571 switch (transition) {
6572 case GST_STATE_CHANGE_NULL_TO_READY:
6573 if (!gst_rtspsrc_start (rtspsrc))
6576 case GST_STATE_CHANGE_READY_TO_PAUSED:
6577 /* init some state */
6578 rtspsrc->cur_protocols = rtspsrc->protocols;
6579 /* first attempt, don't ignore timeouts */
6580 rtspsrc->ignore_timeout = FALSE;
6581 rtspsrc->open_error = FALSE;
6582 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
6584 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
6585 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
6586 /* unblock the tcp tasks and make the loop waiting */
6587 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP);
6589 case GST_STATE_CHANGE_PAUSED_TO_READY:
6595 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
6596 if (ret == GST_STATE_CHANGE_FAILURE)
6599 switch (transition) {
6600 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
6601 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
6603 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
6604 /* send pause request and keep the idle task around */
6605 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
6606 ret = GST_STATE_CHANGE_NO_PREROLL;
6608 case GST_STATE_CHANGE_READY_TO_PAUSED:
6609 ret = GST_STATE_CHANGE_NO_PREROLL;
6611 case GST_STATE_CHANGE_PAUSED_TO_READY:
6612 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
6614 case GST_STATE_CHANGE_READY_TO_NULL:
6615 gst_rtspsrc_stop (rtspsrc);
6626 GST_DEBUG_OBJECT (rtspsrc, "start failed");
6627 return GST_STATE_CHANGE_FAILURE;
6632 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
6635 GstRTSPSrc *rtspsrc;
6637 rtspsrc = GST_RTSPSRC (element);
6639 if (GST_EVENT_IS_DOWNSTREAM (event)) {
6640 res = gst_rtspsrc_push_event (rtspsrc, event);
6642 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
6649 /*** GSTURIHANDLER INTERFACE *************************************************/
6652 gst_rtspsrc_uri_get_type (GType type)
6657 static const gchar *const *
6658 gst_rtspsrc_uri_get_protocols (GType type)
6660 static const gchar *protocols[] =
6661 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp", NULL };
6667 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
6669 GstRTSPSrc *src = GST_RTSPSRC (handler);
6671 /* FIXME: make thread-safe */
6672 return g_strdup (src->conninfo.location);
6676 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
6681 GstRTSPUrl *newurl = NULL;
6682 GstSDPMessage *sdp = NULL;
6684 src = GST_RTSPSRC (handler);
6686 /* same URI, we're fine */
6687 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
6690 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
6691 if ((res = gst_sdp_message_new (&sdp) < 0))
6694 GST_DEBUG_OBJECT (src, "parsing SDP message");
6695 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
6699 GST_DEBUG_OBJECT (src, "parsing URI");
6700 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
6704 /* if worked, free previous and store new url object along with the original
6706 GST_DEBUG_OBJECT (src, "configuring URI");
6707 g_free (src->conninfo.location);
6708 src->conninfo.location = g_strdup (uri);
6709 gst_rtsp_url_free (src->conninfo.url);
6710 src->conninfo.url = newurl;
6711 g_free (src->conninfo.url_str);
6713 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
6715 src->conninfo.url_str = NULL;
6718 gst_sdp_message_free (src->sdp);
6720 src->from_sdp = sdp != NULL;
6722 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
6723 GST_DEBUG_OBJECT (src, "request uri is: %s",
6724 GST_STR_NULL (src->conninfo.url_str));
6731 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
6736 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
6737 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
6738 "Could not create SDP");
6743 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
6744 GST_STR_NULL (uri));
6745 gst_sdp_message_free (sdp);
6746 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
6752 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
6753 GST_STR_NULL (uri), res);
6754 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
6755 "Invalid RTSP URI");
6761 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
6763 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
6765 iface->get_type = gst_rtspsrc_uri_get_type;
6766 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
6767 iface->get_uri = gst_rtspsrc_uri_get_uri;
6768 iface->set_uri = gst_rtspsrc_uri_set_uri;