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., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, 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-1.0 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 */
92 #include <gst/sdp/gstsdpmessage.h>
93 #include <gst/rtp/gstrtppayloads.h>
95 #include "gst/gst-i18n-plugin.h"
97 #include "gstrtspsrc.h"
99 GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
100 #define GST_CAT_DEFAULT (rtspsrc_debug)
102 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream_%u",
105 GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
107 /* templates used internally */
108 static GstStaticPadTemplate anysrctemplate =
109 GST_STATIC_PAD_TEMPLATE ("internalsrc_%u",
112 GST_STATIC_CAPS_ANY);
114 static GstStaticPadTemplate anysinktemplate =
115 GST_STATIC_PAD_TEMPLATE ("internalsink_%u",
118 GST_STATIC_CAPS_ANY);
126 enum _GstRtspSrcRtcpSyncMode
133 enum _GstRtspSrcBufferMode
141 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
143 gst_rtsp_src_buffer_mode_get_type (void)
145 static GType buffer_mode_type = 0;
146 static const GEnumValue buffer_modes[] = {
147 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
148 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
149 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
150 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
154 if (!buffer_mode_type) {
156 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
158 return buffer_mode_type;
161 #define DEFAULT_LOCATION NULL
162 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
163 #define DEFAULT_DEBUG FALSE
164 #define DEFAULT_RETRY 20
165 #define DEFAULT_TIMEOUT 5000000
166 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
167 #define DEFAULT_TCP_TIMEOUT 20000000
168 #define DEFAULT_LATENCY_MS 2000
169 #define DEFAULT_DROP_ON_LATENCY FALSE
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
181 #define DEFAULT_PROBATION 2
193 PROP_DROP_ON_LATENCY,
194 PROP_CONNECTION_SPEED,
197 PROP_DO_RTSP_KEEP_ALIVE,
204 PROP_UDP_BUFFER_SIZE,
210 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
212 gst_rtsp_nat_method_get_type (void)
214 static GType rtsp_nat_method_type = 0;
215 static const GEnumValue rtsp_nat_method[] = {
216 {GST_RTSP_NAT_NONE, "None", "none"},
217 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
221 if (!rtsp_nat_method_type) {
222 rtsp_nat_method_type =
223 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
225 return rtsp_nat_method_type;
228 static void gst_rtspsrc_finalize (GObject * object);
230 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
231 const GValue * value, GParamSpec * pspec);
232 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
233 GValue * value, GParamSpec * pspec);
235 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
236 gpointer iface_data);
238 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
241 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
242 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
244 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
246 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
247 GstStateChange transition);
248 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
249 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
251 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
252 GstRTSPMessage * response);
254 static void gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask);
255 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
256 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
258 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
259 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
261 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
262 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
263 gboolean only_close);
265 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
266 const gchar * uri, GError ** error);
267 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
269 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
270 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
271 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
272 GstRTSPStream * stream, GstEvent * event);
273 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
275 /* commands we send to out loop to notify it of events */
276 #define CMD_OPEN (1 << 0)
277 #define CMD_PLAY (1 << 1)
278 #define CMD_PAUSE (1 << 2)
279 #define CMD_CLOSE (1 << 3)
280 #define CMD_WAIT (1 << 4)
281 #define CMD_RECONNECT (1 << 5)
282 #define CMD_LOOP (1 << 6)
284 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
286 gchar *__txt = _gst_element_error_printf text; \
287 gst_element_post_message (GST_ELEMENT_CAST (el), \
288 gst_message_new_progress (GST_OBJECT_CAST (el), \
289 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
293 /*static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 }; */
294 #define gst_rtspsrc_parent_class parent_class
295 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
296 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
299 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
301 GObjectClass *gobject_class;
302 GstElementClass *gstelement_class;
303 GstBinClass *gstbin_class;
305 gobject_class = (GObjectClass *) klass;
306 gstelement_class = (GstElementClass *) klass;
307 gstbin_class = (GstBinClass *) klass;
309 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
311 gobject_class->set_property = gst_rtspsrc_set_property;
312 gobject_class->get_property = gst_rtspsrc_get_property;
314 gobject_class->finalize = gst_rtspsrc_finalize;
316 g_object_class_install_property (gobject_class, PROP_LOCATION,
317 g_param_spec_string ("location", "RTSP Location",
318 "Location of the RTSP url to read",
319 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
321 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
322 g_param_spec_flags ("protocols", "Protocols",
323 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
324 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
326 g_object_class_install_property (gobject_class, PROP_DEBUG,
327 g_param_spec_boolean ("debug", "Debug",
328 "Dump request and response messages to stdout",
329 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
331 g_object_class_install_property (gobject_class, PROP_RETRY,
332 g_param_spec_uint ("retry", "Retry",
333 "Max number of retries when allocating RTP ports.",
334 0, G_MAXUINT16, DEFAULT_RETRY,
335 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
337 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
338 g_param_spec_uint64 ("timeout", "Timeout",
339 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
340 0, G_MAXUINT64, DEFAULT_TIMEOUT,
341 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
343 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
344 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
345 "Fail after timeout microseconds on TCP connections (0 = disabled)",
346 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
347 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
349 g_object_class_install_property (gobject_class, PROP_LATENCY,
350 g_param_spec_uint ("latency", "Buffer latency in ms",
351 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
352 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
354 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
355 g_param_spec_boolean ("drop-on-latency",
356 "Drop buffers when maximum latency is reached",
357 "Tells the jitterbuffer to never exceed the given latency in size",
358 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
360 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
361 g_param_spec_uint64 ("connection-speed", "Connection Speed",
362 "Network connection speed in kbps (0 = unknown)",
363 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
364 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
366 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
367 g_param_spec_enum ("nat-method", "NAT Method",
368 "Method to use for traversing firewalls and NAT",
369 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
370 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
373 * GstRTSPSrc::do-rtcp
375 * Enable RTCP support. Some old server don't like RTCP and then this property
376 * needs to be set to FALSE.
380 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
381 g_param_spec_boolean ("do-rtcp", "Do RTCP",
382 "Send RTCP packets, disable for old incompatible server.",
383 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
386 * GstRTSPSrc::do-rtsp-keep-alive
388 * Enable RTSP keep laive support. Some old server don't like RTSP
389 * keep alive and then this property needs to be set to FALSE.
393 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
394 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
395 "Send RTSP keep alive packets, disable for old incompatible server.",
396 DEFAULT_DO_RTSP_KEEP_ALIVE,
397 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
402 * Set the proxy parameters. This has to be a string of the format
403 * [http://][user:passwd@]host[:port].
407 g_object_class_install_property (gobject_class, PROP_PROXY,
408 g_param_spec_string ("proxy", "Proxy",
409 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
410 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
413 * GstRTSPSrc::rtp_blocksize
415 * RTP package size to suggest to server.
419 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
420 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
421 "RTP package size to suggest to server (0 = disabled)",
422 0, 65536, DEFAULT_RTP_BLOCKSIZE,
423 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
425 g_object_class_install_property (gobject_class,
427 g_param_spec_string ("user-id", "user-id",
428 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
429 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
430 g_object_class_install_property (gobject_class, PROP_USER_PW,
431 g_param_spec_string ("user-pw", "user-pw",
432 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
433 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
436 * GstRTSPSrc::buffer-mode:
438 * Control the buffering and timestamping mode used by the jitterbuffer.
442 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
443 g_param_spec_enum ("buffer-mode", "Buffer Mode",
444 "Control the buffering algorithm in use",
445 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
446 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
449 * GstRTSPSrc::port-range:
451 * Configure the client port numbers that can be used to recieve RTP and
456 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
457 g_param_spec_string ("port-range", "Port range",
458 "Client port range that can be used to receive RTP and RTCP data, "
459 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
460 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
463 * GstRTSPSrc::udp-buffer-size:
465 * Size of the kernel UDP receive buffer in bytes.
469 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
470 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
471 "Size of the kernel UDP receive buffer in bytes, 0=default",
472 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
473 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
476 * GstRTSPSrc::short-header:
478 * Only send the basic RTSP headers for broken encoders.
482 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
483 g_param_spec_boolean ("short-header", "Short Header",
484 "Only send the basic RTSP headers for broken encoders",
485 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
487 g_object_class_install_property (gobject_class, PROP_PROBATION,
488 g_param_spec_uint ("probation", "Number of probations",
489 "Consecutive packet sequence numbers to accept the source",
490 0, G_MAXUINT, DEFAULT_PROBATION,
491 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
493 gstelement_class->send_event = gst_rtspsrc_send_event;
494 gstelement_class->change_state = gst_rtspsrc_change_state;
496 gst_element_class_add_pad_template (gstelement_class,
497 gst_static_pad_template_get (&rtptemplate));
499 gst_element_class_set_static_metadata (gstelement_class,
500 "RTSP packet receiver", "Source/Network",
501 "Receive data over the network via RTSP (RFC 2326)",
502 "Wim Taymans <wim@fluendo.com>, "
503 "Thijs Vermeir <thijs.vermeir@barco.com>, "
504 "Lutz Mueller <lutz@topfrose.de>");
506 gstbin_class->handle_message = gst_rtspsrc_handle_message;
508 gst_rtsp_ext_list_init ();
513 gst_rtspsrc_init (GstRTSPSrc * src)
515 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
516 src->protocols = DEFAULT_PROTOCOLS;
517 src->debug = DEFAULT_DEBUG;
518 src->retry = DEFAULT_RETRY;
519 src->udp_timeout = DEFAULT_TIMEOUT;
520 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
521 src->latency = DEFAULT_LATENCY_MS;
522 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
523 src->connection_speed = DEFAULT_CONNECTION_SPEED;
524 src->nat_method = DEFAULT_NAT_METHOD;
525 src->do_rtcp = DEFAULT_DO_RTCP;
526 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
527 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
528 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
529 src->user_id = g_strdup (DEFAULT_USER_ID);
530 src->user_pw = g_strdup (DEFAULT_USER_PW);
531 src->buffer_mode = DEFAULT_BUFFER_MODE;
532 src->client_port_range.min = 0;
533 src->client_port_range.max = 0;
534 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
535 src->short_header = DEFAULT_SHORT_HEADER;
536 src->probation = DEFAULT_PROBATION;
538 /* get a list of all extensions */
539 src->extensions = gst_rtsp_ext_list_get ();
541 /* connect to send signal */
542 gst_rtsp_ext_list_connect (src->extensions, "send",
543 (GCallback) gst_rtspsrc_send_cb, src);
545 /* protects the streaming thread in interleaved mode or the polling
546 * thread in UDP mode. */
547 g_rec_mutex_init (&src->stream_rec_lock);
549 /* protects our state changes from multiple invocations */
550 g_rec_mutex_init (&src->state_rec_lock);
552 src->state = GST_RTSP_STATE_INVALID;
554 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
558 gst_rtspsrc_finalize (GObject * object)
562 rtspsrc = GST_RTSPSRC (object);
564 gst_rtsp_ext_list_free (rtspsrc->extensions);
565 g_free (rtspsrc->conninfo.location);
566 gst_rtsp_url_free (rtspsrc->conninfo.url);
567 g_free (rtspsrc->conninfo.url_str);
568 g_free (rtspsrc->user_id);
569 g_free (rtspsrc->user_pw);
572 gst_sdp_message_free (rtspsrc->sdp);
577 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
578 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
580 G_OBJECT_CLASS (parent_class)->finalize (object);
583 /* a proxy string of the format [user:passwd@]host[:port] */
585 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
589 g_free (rtsp->proxy_user);
590 rtsp->proxy_user = NULL;
591 g_free (rtsp->proxy_passwd);
592 rtsp->proxy_passwd = NULL;
593 g_free (rtsp->proxy_host);
594 rtsp->proxy_host = NULL;
595 rtsp->proxy_port = 0;
602 /* we allow http:// in front but ignore it */
603 if (g_str_has_prefix (p, "http://"))
606 at = strchr (p, '@');
608 /* look for user:passwd */
609 col = strchr (proxy, ':');
610 if (col == NULL || col > at)
613 rtsp->proxy_user = g_strndup (p, col - p);
615 rtsp->proxy_passwd = g_strndup (col, at - col);
620 col = strchr (p, ':');
623 /* everything before the colon is the hostname */
624 rtsp->proxy_host = g_strndup (p, col - p);
626 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
628 rtsp->proxy_host = g_strdup (p);
629 rtsp->proxy_port = 8080;
635 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
637 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
638 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
641 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
643 rtspsrc->ptcp_timeout = NULL;
647 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
652 rtspsrc = GST_RTSPSRC (object);
656 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
657 g_value_get_string (value), NULL);
660 rtspsrc->protocols = g_value_get_flags (value);
663 rtspsrc->debug = g_value_get_boolean (value);
666 rtspsrc->retry = g_value_get_uint (value);
669 rtspsrc->udp_timeout = g_value_get_uint64 (value);
671 case PROP_TCP_TIMEOUT:
672 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
675 rtspsrc->latency = g_value_get_uint (value);
677 case PROP_DROP_ON_LATENCY:
678 rtspsrc->drop_on_latency = g_value_get_boolean (value);
680 case PROP_CONNECTION_SPEED:
681 rtspsrc->connection_speed = g_value_get_uint64 (value);
683 case PROP_NAT_METHOD:
684 rtspsrc->nat_method = g_value_get_enum (value);
687 rtspsrc->do_rtcp = g_value_get_boolean (value);
689 case PROP_DO_RTSP_KEEP_ALIVE:
690 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
693 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
695 case PROP_RTP_BLOCKSIZE:
696 rtspsrc->rtp_blocksize = g_value_get_uint (value);
699 if (rtspsrc->user_id)
700 g_free (rtspsrc->user_id);
701 rtspsrc->user_id = g_value_dup_string (value);
704 if (rtspsrc->user_pw)
705 g_free (rtspsrc->user_pw);
706 rtspsrc->user_pw = g_value_dup_string (value);
708 case PROP_BUFFER_MODE:
709 rtspsrc->buffer_mode = g_value_get_enum (value);
711 case PROP_PORT_RANGE:
715 str = g_value_get_string (value);
717 sscanf (str, "%u-%u",
718 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
720 rtspsrc->client_port_range.min = 0;
721 rtspsrc->client_port_range.max = 0;
725 case PROP_UDP_BUFFER_SIZE:
726 rtspsrc->udp_buffer_size = g_value_get_int (value);
728 case PROP_SHORT_HEADER:
729 rtspsrc->short_header = g_value_get_boolean (value);
732 rtspsrc->probation = g_value_get_uint (value);
735 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
741 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
746 rtspsrc = GST_RTSPSRC (object);
750 g_value_set_string (value, rtspsrc->conninfo.location);
753 g_value_set_flags (value, rtspsrc->protocols);
756 g_value_set_boolean (value, rtspsrc->debug);
759 g_value_set_uint (value, rtspsrc->retry);
762 g_value_set_uint64 (value, rtspsrc->udp_timeout);
764 case PROP_TCP_TIMEOUT:
768 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
769 rtspsrc->tcp_timeout.tv_usec;
770 g_value_set_uint64 (value, timeout);
774 g_value_set_uint (value, rtspsrc->latency);
776 case PROP_DROP_ON_LATENCY:
777 g_value_set_boolean (value, rtspsrc->drop_on_latency);
779 case PROP_CONNECTION_SPEED:
780 g_value_set_uint64 (value, rtspsrc->connection_speed);
782 case PROP_NAT_METHOD:
783 g_value_set_enum (value, rtspsrc->nat_method);
786 g_value_set_boolean (value, rtspsrc->do_rtcp);
788 case PROP_DO_RTSP_KEEP_ALIVE:
789 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
795 if (rtspsrc->proxy_host) {
797 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
801 g_value_take_string (value, str);
804 case PROP_RTP_BLOCKSIZE:
805 g_value_set_uint (value, rtspsrc->rtp_blocksize);
808 g_value_set_string (value, rtspsrc->user_id);
811 g_value_set_string (value, rtspsrc->user_pw);
813 case PROP_BUFFER_MODE:
814 g_value_set_enum (value, rtspsrc->buffer_mode);
816 case PROP_PORT_RANGE:
820 if (rtspsrc->client_port_range.min != 0) {
821 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
822 rtspsrc->client_port_range.max);
826 g_value_take_string (value, str);
829 case PROP_UDP_BUFFER_SIZE:
830 g_value_set_int (value, rtspsrc->udp_buffer_size);
832 case PROP_SHORT_HEADER:
833 g_value_set_boolean (value, rtspsrc->short_header);
836 g_value_set_uint (value, rtspsrc->probation);
839 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
845 find_stream_by_id (GstRTSPStream * stream, gint * id)
847 if (stream->id == *id)
854 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
856 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
863 find_stream_by_pt (GstRTSPStream * stream, gint * pt)
865 if (stream->pt == *pt)
872 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
874 GstElement *src = (GstElement *) a;
876 if (stream->udpsrc[0] == src)
878 if (stream->udpsrc[1] == src)
885 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
887 /* check qualified setup_url */
888 if (!strcmp (stream->conninfo.location, (gchar *) a))
890 /* check original control_url */
891 if (!strcmp (stream->control_url, (gchar *) a))
894 /* check if qualified setup_url ends with string */
895 if (g_str_has_suffix (stream->control_url, (gchar *) a))
901 static GstRTSPStream *
902 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
906 /* find and get stream */
907 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
908 return (GstRTSPStream *) lstream->data;
913 static const GstSDPBandwidth *
914 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
915 const GstSDPMedia * media, const gchar * type)
919 /* first look in the media specific section */
920 len = gst_sdp_media_bandwidths_len (media);
921 for (i = 0; i < len; i++) {
922 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
924 if (strcmp (bw->bwtype, type) == 0)
927 /* then look in the message specific section */
928 len = gst_sdp_message_bandwidths_len (sdp);
929 for (i = 0; i < len; i++) {
930 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
932 if (strcmp (bw->bwtype, type) == 0)
939 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
940 const GstSDPMedia * media, GstRTSPStream * stream)
942 const GstSDPBandwidth *bw;
944 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
945 stream->as_bandwidth = bw->bandwidth;
947 stream->as_bandwidth = -1;
949 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
950 stream->rr_bandwidth = bw->bandwidth;
952 stream->rr_bandwidth = -1;
954 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
955 stream->rs_bandwidth = bw->bandwidth;
957 stream->rs_bandwidth = -1;
961 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
962 const GstSDPConnection * conn)
964 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
967 if (conn->addrtype == NULL)
971 if (strcmp (conn->addrtype, "IP4") == 0)
972 stream->is_ipv6 = FALSE;
973 else if (strcmp (conn->addrtype, "IP6") == 0)
974 stream->is_ipv6 = TRUE;
979 g_free (stream->destination);
980 stream->destination = g_strdup (conn->address);
982 /* check for multicast */
983 stream->is_multicast =
984 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
986 stream->ttl = conn->ttl;
989 /* Go over the connections for a stream.
990 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
992 * - If we are dealing with a localhost address, we disable multicast
995 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
996 const GstSDPMedia * media, GstRTSPStream * stream)
998 const GstSDPConnection *conn;
1001 /* first look in the media specific section */
1002 len = gst_sdp_media_connections_len (media);
1003 for (i = 0; i < len; i++) {
1004 conn = gst_sdp_media_get_connection (media, i);
1006 gst_rtspsrc_do_stream_connection (src, stream, conn);
1008 /* then look in the message specific section */
1009 if ((conn = gst_sdp_message_get_connection (sdp))) {
1010 gst_rtspsrc_do_stream_connection (src, stream, conn);
1014 static GstRTSPStream *
1015 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1017 GstRTSPStream *stream;
1018 const gchar *control_url;
1019 const gchar *payload;
1020 const GstSDPMedia *media;
1022 /* get media, should not return NULL */
1023 media = gst_sdp_message_get_media (sdp, idx);
1027 stream = g_new0 (GstRTSPStream, 1);
1028 stream->parent = src;
1029 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1031 stream->last_ret = GST_FLOW_NOT_LINKED;
1032 stream->added = FALSE;
1033 stream->disabled = FALSE;
1034 stream->id = src->numstreams++;
1035 stream->eos = FALSE;
1036 stream->discont = TRUE;
1037 stream->seqbase = -1;
1038 stream->timebase = -1;
1040 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1041 * session manager to scale RTCP. */
1042 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1044 /* collect connection info */
1045 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1047 /* we must have a payload. No payload means we cannot create caps */
1048 /* FIXME, handle multiple formats. The problem here is that we just want to
1049 * take the first available format that we can handle but in order to do that
1050 * we need to scan for depayloader plugins. Scanning for payloader plugins is
1051 * also suboptimal because the user maybe just wants to save the raw stream
1052 * and then we don't care. */
1053 if ((payload = gst_sdp_media_get_format (media, 0))) {
1054 stream->pt = atoi (payload);
1056 stream->caps = gst_rtspsrc_media_to_caps (stream->pt, media);
1058 GST_DEBUG ("mapping sdp session level attributes to caps");
1059 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, stream->caps);
1060 GST_DEBUG ("mapping sdp media level attributes to caps");
1061 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, stream->caps);
1063 if (stream->pt >= 96) {
1064 /* If we have a dynamic payload type, see if we have a stream with the
1065 * same payload number. If there is one, they are part of the same
1066 * container and we only need to add one pad. */
1067 if (find_stream (src, &stream->pt, (gpointer) find_stream_by_pt)) {
1068 stream->container = TRUE;
1069 GST_DEBUG ("found another stream with pt %d, marking as container",
1074 /* collect port number */
1075 stream->port = gst_sdp_media_get_port (media);
1077 /* get control url to construct the setup url. The setup url is used to
1078 * configure the transport of the stream and is used to identity the stream in
1079 * the RTP-Info header field returned from PLAY. */
1080 control_url = gst_sdp_media_get_attribute_val (media, "control");
1081 if (control_url == NULL)
1082 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1084 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1085 GST_DEBUG_OBJECT (src, " pt: %d", stream->pt);
1086 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1087 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1088 GST_DEBUG_OBJECT (src, " caps: %" GST_PTR_FORMAT, stream->caps);
1089 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1091 if (control_url != NULL) {
1092 stream->control_url = g_strdup (control_url);
1093 /* Build a fully qualified url using the content_base if any or by prefixing
1094 * the original request.
1095 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1096 * likely build a URL that the server will fail to understand, this is ok,
1097 * we will fail then. */
1098 if (g_str_has_prefix (control_url, "rtsp://"))
1099 stream->conninfo.location = g_strdup (control_url);
1104 if (g_strcmp0 (control_url, "*") == 0)
1108 base = src->control;
1109 else if (src->content_base)
1110 base = src->content_base;
1111 else if (src->conninfo.url_str)
1112 base = src->conninfo.url_str;
1116 /* check if the base ends or control starts with / */
1117 has_slash = g_str_has_prefix (control_url, "/");
1118 has_slash = has_slash || g_str_has_suffix (base, "/");
1120 /* concatenate the two strings, insert / when not present */
1121 stream->conninfo.location =
1122 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1125 GST_DEBUG_OBJECT (src, " setup: %s",
1126 GST_STR_NULL (stream->conninfo.location));
1128 /* we keep track of all streams */
1129 src->streams = g_list_append (src->streams, stream);
1137 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1141 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1144 gst_caps_unref (stream->caps);
1146 g_free (stream->destination);
1147 g_free (stream->control_url);
1148 g_free (stream->conninfo.location);
1150 for (i = 0; i < 2; i++) {
1151 if (stream->udpsrc[i]) {
1152 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1153 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1154 gst_object_unref (stream->udpsrc[i]);
1155 stream->udpsrc[i] = NULL;
1157 if (stream->channelpad[i]) {
1158 gst_object_unref (stream->channelpad[i]);
1159 stream->channelpad[i] = NULL;
1161 if (stream->udpsink[i]) {
1162 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1163 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1164 gst_object_unref (stream->udpsink[i]);
1165 stream->udpsink[i] = NULL;
1168 if (stream->fakesrc) {
1169 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1170 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1171 gst_object_unref (stream->fakesrc);
1172 stream->fakesrc = NULL;
1174 if (stream->srcpad) {
1175 gst_pad_set_active (stream->srcpad, FALSE);
1176 if (stream->added) {
1177 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1178 stream->added = FALSE;
1180 stream->srcpad = NULL;
1182 if (stream->rtcppad) {
1183 gst_object_unref (stream->rtcppad);
1184 stream->rtcppad = NULL;
1186 if (stream->session) {
1187 g_object_unref (stream->session);
1188 stream->session = NULL;
1194 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1198 GST_DEBUG_OBJECT (src, "cleanup");
1200 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1201 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1203 gst_rtspsrc_stream_free (src, stream);
1205 g_list_free (src->streams);
1206 src->streams = NULL;
1208 if (src->manager_sig_id) {
1209 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1210 src->manager_sig_id = 0;
1212 gst_element_set_state (src->manager, GST_STATE_NULL);
1213 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1214 src->manager = NULL;
1216 src->numstreams = 0;
1218 gst_structure_free (src->props);
1221 g_free (src->content_base);
1222 src->content_base = NULL;
1224 g_free (src->control);
1225 src->control = NULL;
1228 gst_rtsp_range_free (src->range);
1231 /* don't clear the SDP when it was used in the url */
1232 if (src->sdp && !src->from_sdp) {
1233 gst_sdp_message_free (src->sdp);
1238 #define PARSE_INT(p, del, res) \
1241 p = strstr (p, del); \
1251 #define PARSE_STRING(p, del, res) \
1254 p = strstr (p, del); \
1266 #define SKIP_SPACES(p) \
1267 while (*p && g_ascii_isspace (*p)) \
1272 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1275 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1276 gint * rate, gchar ** params)
1280 p = (gchar *) rtpmap;
1282 PARSE_INT (p, " ", *payload);
1290 PARSE_STRING (p, "/", *name);
1291 if (*name == NULL) {
1292 GST_DEBUG ("no rate, name %s", p);
1293 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1294 * streams seem to omit the rate. */
1301 p = strstr (p, "/");
1319 * Mapping SDP attributes to caps
1321 * prepend 'a-' to IANA registered sdp attributes names
1322 * (ie: not prefixed with 'x-') in order to avoid
1323 * collision with gstreamer standard caps properties names
1326 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1328 if (attributes->len > 0) {
1332 s = gst_caps_get_structure (caps, 0);
1334 for (i = 0; i < attributes->len; i++) {
1335 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1336 gchar *tofree, *key;
1340 /* skip some of the attribute we already handle */
1341 if (!strcmp (key, "fmtp"))
1343 if (!strcmp (key, "rtpmap"))
1345 if (!strcmp (key, "control"))
1347 if (!strcmp (key, "range"))
1350 /* string must be valid UTF8 */
1351 if (!g_utf8_validate (attr->value, -1, NULL))
1354 if (!g_str_has_prefix (key, "x-"))
1355 tofree = key = g_strdup_printf ("a-%s", key);
1359 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1360 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1367 * Mapping of caps to and from SDP fields:
1369 * m=<media> <UDP port> RTP/AVP <payload>
1370 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1371 * a=fmtp:<payload> <param>[=<value>];...
1374 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1377 const gchar *rtpmap;
1381 gchar *params = NULL;
1387 /* get and parse rtpmap */
1388 if ((rtpmap = gst_sdp_media_get_attribute_val (media, "rtpmap"))) {
1389 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
1391 if (payload != pt) {
1392 /* we ignore the rtpmap if the payload type is different. */
1393 g_warning ("rtpmap of wrong payload type, ignoring");
1399 /* if we failed to parse the rtpmap for a dynamic payload type, we have an
1403 /* else we can ignore */
1404 g_warning ("error parsing rtpmap, ignoring");
1407 /* dynamic payloads need rtpmap or we fail */
1411 /* check if we have a rate, if not, we need to look up the rate from the
1412 * default rates based on the payload types. */
1414 const GstRTPPayloadInfo *info;
1416 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
1417 /* dynamic types, use media and encoding_name */
1418 tmp = g_ascii_strdown (media->media, -1);
1419 info = gst_rtp_payload_info_for_name (tmp, name);
1422 /* static types, use payload type */
1423 info = gst_rtp_payload_info_for_pt (pt);
1427 if ((rate = info->clock_rate) == 0)
1430 /* we fail if we cannot find one */
1435 tmp = g_ascii_strdown (media->media, -1);
1436 caps = gst_caps_new_simple ("application/x-unknown",
1437 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
1439 s = gst_caps_get_structure (caps, 0);
1441 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
1443 /* encoding name must be upper case */
1445 tmp = g_ascii_strup (name, -1);
1446 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
1450 /* params must be lower case */
1451 if (params != NULL) {
1452 tmp = g_ascii_strdown (params, -1);
1453 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
1457 /* parse optional fmtp: field */
1458 if ((fmtp = gst_sdp_media_get_attribute_val (media, "fmtp"))) {
1464 /* p is now of the format <payload> <param>[=<value>];... */
1465 PARSE_INT (p, " ", payload);
1466 if (payload != -1 && payload == pt) {
1470 /* <param>[=<value>] are separated with ';' */
1471 pairs = g_strsplit (p, ";", 0);
1472 for (i = 0; pairs[i]; i++) {
1474 const gchar *val, *key;
1476 /* the key may not have a '=', the value can have other '='s */
1477 valpos = strstr (pairs[i], "=");
1479 /* we have a '=' and thus a value, remove the '=' with \0 */
1481 /* value is everything between '=' and ';'. We split the pairs at ;
1482 * boundaries so we can take the remainder of the value. Some servers
1483 * put spaces around the value which we strip off here. Alternatively
1484 * we could strip those spaces in the depayloaders should these spaces
1485 * actually carry any meaning in the future. */
1486 val = g_strstrip (valpos + 1);
1488 /* simple <param>;.. is translated into <param>=1;... */
1491 /* strip the key of spaces, convert key to lowercase but not the value. */
1492 key = g_strstrip (pairs[i]);
1493 if (strlen (key) > 1) {
1494 tmp = g_ascii_strdown (key, -1);
1495 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
1507 g_warning ("rtpmap type not given for dynamic payload %d", pt);
1512 g_warning ("rate unknown for payload type %d", pt);
1518 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1519 gint * rtpport, gint * rtcpport)
1522 GstStateChangeReturn ret;
1523 GstElement *udpsrc0, *udpsrc1;
1524 gint tmp_rtp, tmp_rtcp;
1528 src = stream->parent;
1534 /* Start at next port */
1535 tmp_rtp = src->next_port_num;
1537 if (stream->is_ipv6)
1538 host = "udp://[::0]";
1540 host = "udp://0.0.0.0";
1542 /* try to allocate 2 UDP ports, the RTP port should be an even
1543 * number and the RTCP port should be the next (uneven) port */
1546 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1547 tmp_rtp >= src->client_port_range.max)
1550 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1551 if (udpsrc0 == NULL)
1552 goto no_udp_protocol;
1553 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1555 if (src->udp_buffer_size != 0)
1556 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
1559 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
1560 if (ret == GST_STATE_CHANGE_FAILURE) {
1562 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
1565 if (++count > src->retry)
1568 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1569 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1570 gst_object_unref (udpsrc0);
1572 GST_DEBUG_OBJECT (src, "retry %d", count);
1575 goto no_udp_protocol;
1578 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
1579 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
1581 /* check if port is even */
1582 if ((tmp_rtp & 0x01) != 0) {
1583 /* port not even, close and allocate another */
1584 if (++count > src->retry)
1587 GST_DEBUG_OBJECT (src, "RTP port not even");
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, "retry %d", count);
1598 /* allocate port+1 for RTCP now */
1599 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1600 if (udpsrc1 == NULL)
1601 goto no_udp_rtcp_protocol;
1604 tmp_rtcp = tmp_rtp + 1;
1605 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
1608 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
1610 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
1611 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
1612 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
1613 if (ret == GST_STATE_CHANGE_FAILURE) {
1614 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
1616 if (++count > src->retry)
1619 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1620 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1621 gst_object_unref (udpsrc0);
1623 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
1624 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1625 gst_object_unref (udpsrc1);
1629 GST_DEBUG_OBJECT (src, "retry %d", count);
1633 /* all fine, do port check */
1634 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
1635 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
1637 /* this should not happen... */
1638 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
1641 /* we keep these elements, we configure all in configure_transport when the
1642 * server told us to really use the UDP ports. */
1643 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
1644 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
1646 /* keep track of next available port number when we have a range
1648 if (src->next_port_num != 0)
1649 src->next_port_num = tmp_rtcp + 1;
1656 GST_DEBUG_OBJECT (src, "could not get UDP source");
1661 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
1665 no_udp_rtcp_protocol:
1667 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
1672 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
1673 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
1679 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1680 gst_object_unref (udpsrc0);
1683 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1684 gst_object_unref (udpsrc1);
1691 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
1699 event = gst_event_new_flush_start ();
1700 GST_DEBUG_OBJECT (src, "start flush");
1702 state = GST_STATE_PAUSED;
1704 event = gst_event_new_flush_stop (FALSE);
1705 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
1708 state = GST_STATE_PLAYING;
1710 state = GST_STATE_PAUSED;
1712 gst_rtspsrc_push_event (src, event);
1713 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
1715 /* to manage jitterbuffer buffer mode */
1717 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
1719 /* make running time start start at 0 again */
1720 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1721 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1723 for (i = 0; i < 2; i++) {
1725 if (stream->udpsrc[i]) {
1726 gst_element_set_state (stream->udpsrc[i], state);
1732 static GstRTSPResult
1733 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
1734 GstRTSPMessage * message, GTimeVal * timeout)
1739 ret = gst_rtsp_connection_send (conn, message, timeout);
1741 ret = GST_RTSP_ERROR;
1746 static GstRTSPResult
1747 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
1748 GstRTSPMessage * message, GTimeVal * timeout)
1753 ret = gst_rtsp_connection_receive (conn, message, timeout);
1755 ret = GST_RTSP_ERROR;
1761 gst_rtspsrc_get_position (GstRTSPSrc * src)
1766 query = gst_query_new_position (GST_FORMAT_TIME);
1767 /* should be known somewhere down the stream (e.g. jitterbuffer) */
1768 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1769 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1773 if (stream->srcpad) {
1774 if (gst_pad_query (stream->srcpad, query)) {
1775 gst_query_parse_position (query, &fmt, &pos);
1776 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
1777 GST_TIME_ARGS (pos));
1778 src->last_pos = pos;
1788 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
1790 src->state = GST_RTSP_STATE_SEEKING;
1791 /* PLAY will add the range header now. */
1792 src->need_range = TRUE;
1798 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
1803 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
1805 gboolean flush, skip;
1808 GstSegment seeksegment = { 0, };
1812 GST_DEBUG_OBJECT (src, "doing seek with event");
1814 gst_event_parse_seek (event, &rate, &format, &flags,
1815 &cur_type, &cur, &stop_type, &stop);
1817 /* no negative rates yet */
1821 /* we need TIME format */
1822 if (format != src->segment.format)
1825 GST_DEBUG_OBJECT (src, "doing seek without event");
1827 cur_type = GST_SEEK_TYPE_SET;
1828 stop_type = GST_SEEK_TYPE_SET;
1831 /* get flush flag */
1832 flush = flags & GST_SEEK_FLAG_FLUSH;
1833 skip = flags & GST_SEEK_FLAG_SKIP;
1835 /* now we need to make sure the streaming thread is stopped. We do this by
1836 * either sending a FLUSH_START event downstream which will cause the
1837 * streaming thread to stop with a WRONG_STATE.
1838 * For a non-flushing seek we simply pause the task, which will happen as soon
1839 * as it completes one iteration (and thus might block when the sink is
1840 * blocking in preroll). */
1842 GST_DEBUG_OBJECT (src, "starting flush");
1843 gst_rtspsrc_flush (src, TRUE, FALSE);
1846 gst_task_pause (src->task);
1850 /* we should now be able to grab the streaming thread because we stopped it
1851 * with the above flush/pause code */
1852 GST_RTSP_STREAM_LOCK (src);
1854 GST_DEBUG_OBJECT (src, "stopped streaming");
1856 /* copy segment, we need this because we still need the old
1857 * segment when we close the current segment. */
1858 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1860 /* configure the seek parameters in the seeksegment. We will then have the
1861 * right values in the segment to perform the seek */
1863 GST_DEBUG_OBJECT (src, "configuring seek");
1864 gst_segment_do_seek (&seeksegment, rate, format, flags,
1865 cur_type, cur, stop_type, stop, &update);
1868 /* figure out the last position we need to play. If it's configured (stop !=
1869 * -1), use that, else we play until the total duration of the file */
1870 if ((stop = seeksegment.stop) == -1)
1871 stop = seeksegment.duration;
1873 playing = (src->state == GST_RTSP_STATE_PLAYING);
1875 /* if we were playing, pause first */
1877 /* obtain current position in case seek fails */
1878 gst_rtspsrc_get_position (src);
1879 gst_rtspsrc_pause (src, FALSE);
1883 gst_rtspsrc_do_seek (src, &seeksegment);
1885 /* and continue playing */
1887 gst_rtspsrc_play (src, &seeksegment, FALSE);
1889 /* prepare for streaming again */
1891 /* if we started flush, we stop now */
1892 GST_DEBUG_OBJECT (src, "stopping flush");
1893 gst_rtspsrc_flush (src, FALSE, playing);
1896 /* now we did the seek and can activate the new segment values */
1897 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1899 /* if we're doing a segment seek, post a SEGMENT_START message */
1900 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1901 gst_element_post_message (GST_ELEMENT_CAST (src),
1902 gst_message_new_segment_start (GST_OBJECT_CAST (src),
1903 src->segment.format, src->segment.position));
1906 /* now create the newsegment */
1907 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
1908 " to %" G_GINT64_FORMAT, src->segment.position, stop);
1910 /* store the newsegment event so it can be sent from the streaming thread. */
1911 if (src->start_segment)
1912 gst_event_unref (src->start_segment);
1913 src->start_segment = gst_event_new_segment (&src->segment);
1916 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
1917 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1918 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1919 stream->discont = TRUE;
1922 GST_RTSP_STREAM_UNLOCK (src);
1929 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
1934 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
1940 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
1944 gboolean res = TRUE;
1947 src = GST_RTSPSRC_CAST (parent);
1949 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
1950 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
1952 switch (GST_EVENT_TYPE (event)) {
1953 case GST_EVENT_SEEK:
1954 res = gst_rtspsrc_perform_seek (src, event);
1958 case GST_EVENT_NAVIGATION:
1959 case GST_EVENT_LATENCY:
1967 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
1968 res = gst_pad_send_event (target, event);
1969 gst_object_unref (target);
1971 gst_event_unref (event);
1974 gst_event_unref (event);
1980 /* this is the final event function we receive on the internal source pad when
1981 * we deal with TCP connections */
1983 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
1988 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
1990 switch (GST_EVENT_TYPE (event)) {
1991 case GST_EVENT_SEEK:
1993 case GST_EVENT_NAVIGATION:
1994 case GST_EVENT_LATENCY:
1996 gst_event_unref (event);
2003 /* this is the final query function we receive on the internal source pad when
2004 * we deal with TCP connections */
2006 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2010 gboolean res = TRUE;
2012 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2014 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2015 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2017 switch (GST_QUERY_TYPE (query)) {
2018 case GST_QUERY_POSITION:
2023 case GST_QUERY_DURATION:
2027 gst_query_parse_duration (query, &format, NULL);
2030 case GST_FORMAT_TIME:
2031 gst_query_set_duration (query, format, src->segment.duration);
2039 case GST_QUERY_LATENCY:
2041 /* we are live with a min latency of 0 and unlimited max latency, this
2042 * result will be updated by the session manager if there is any. */
2043 gst_query_set_latency (query, TRUE, 0, -1);
2053 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2055 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2059 gboolean res = FALSE;
2061 src = GST_RTSPSRC_CAST (parent);
2063 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2064 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2066 switch (GST_QUERY_TYPE (query)) {
2067 case GST_QUERY_DURATION:
2071 gst_query_parse_duration (query, &format, NULL);
2074 case GST_FORMAT_TIME:
2075 gst_query_set_duration (query, format, src->segment.duration);
2083 case GST_QUERY_SEEKING:
2087 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2088 if (format == GST_FORMAT_TIME) {
2090 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2092 /* seeking without duration is unlikely */
2093 seekable = seekable && src->seekable && src->segment.duration &&
2094 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2096 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2097 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2098 src->segment.start, src->segment.stop);
2107 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2109 gst_query_set_uri (query, uri);
2117 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2119 /* forward the query to the proxy target pad */
2121 res = gst_pad_query (target, query);
2122 gst_object_unref (target);
2131 /* callback for RTCP messages to be sent to the server when operating in TCP
2133 static GstFlowReturn
2134 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2137 GstRTSPStream *stream;
2138 GstFlowReturn res = GST_FLOW_OK;
2143 GstRTSPMessage message = { 0 };
2144 GstRTSPConnection *conn;
2146 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2147 src = stream->parent;
2149 gst_buffer_map (buffer, &map, GST_MAP_READ);
2153 gst_rtsp_message_init_data (&message, stream->channel[1]);
2155 /* lend the body data to the message */
2156 gst_rtsp_message_take_body (&message, data, size);
2158 if (stream->conninfo.connection)
2159 conn = stream->conninfo.connection;
2161 conn = src->conninfo.connection;
2163 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2164 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2165 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2167 /* and steal it away again because we will free it when unreffing the
2169 gst_rtsp_message_steal_body (&message, &data, &size);
2170 gst_rtsp_message_unset (&message);
2172 gst_buffer_unmap (buffer, &map);
2173 gst_buffer_unref (buffer);
2178 static GstPadProbeReturn
2179 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2181 GstRTSPSrc *src = user_data;
2183 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2184 GST_DEBUG_PAD_NAME (pad));
2186 /* activate the streams */
2187 GST_OBJECT_LOCK (src);
2188 if (!src->need_activate)
2191 src->need_activate = FALSE;
2192 GST_OBJECT_UNLOCK (src);
2194 gst_rtspsrc_activate_streams (src);
2196 return GST_PAD_PROBE_OK;
2200 GST_OBJECT_UNLOCK (src);
2201 return GST_PAD_PROBE_OK;
2205 /* this callback is called when the session manager generated a new src pad with
2206 * payloaded RTP packets. We simply ghost the pad here. */
2208 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2211 GstPadTemplate *template;
2214 GstRTSPStream *stream;
2217 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2219 GST_RTSP_STATE_LOCK (src);
2221 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2222 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2223 goto unknown_stream;
2225 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %d, PT %d", id, ssrc, pt);
2227 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2229 goto unknown_stream;
2231 /* create a new pad we will use to stream to */
2232 template = gst_static_pad_template_get (&rtptemplate);
2233 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2234 gst_object_unref (template);
2237 stream->added = TRUE;
2238 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2239 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2240 gst_pad_set_active (stream->srcpad, TRUE);
2241 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2243 /* check if we added all streams */
2245 for (lstream = src->streams; lstream; lstream = g_list_next (lstream)) {
2246 stream = (GstRTSPStream *) lstream->data;
2248 GST_DEBUG_OBJECT (src, "stream %p, container %d, disabled %d, added %d",
2249 stream, stream->container, stream->disabled, stream->added);
2251 /* a container stream only needs one pad added. Also disabled streams don't
2253 if (!stream->container && !stream->disabled && !stream->added) {
2258 GST_RTSP_STATE_UNLOCK (src);
2261 GST_DEBUG_OBJECT (src, "We added all streams");
2262 /* when we get here, all stream are added and we can fire the no-more-pads
2264 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2272 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2273 GST_RTSP_STATE_UNLOCK (src);
2280 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2282 GstRTSPStream *stream;
2285 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2287 GST_RTSP_STATE_LOCK (src);
2288 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2290 goto unknown_stream;
2292 caps = stream->caps;
2294 gst_caps_ref (caps);
2295 GST_RTSP_STATE_UNLOCK (src);
2301 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2302 GST_RTSP_STATE_UNLOCK (src);
2308 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2310 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2316 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2322 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2328 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2330 GstRTSPSrc *src = stream->parent;
2332 GST_DEBUG_OBJECT (src, "source in session %u received BYE", stream->id);
2334 gst_rtspsrc_do_stream_eos (src, stream);
2338 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2340 GstRTSPSrc *src = stream->parent;
2342 GST_DEBUG_OBJECT (src, "source in session %u timed out", stream->id);
2344 gst_rtspsrc_do_stream_eos (src, stream);
2348 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2350 GstRTSPStream *stream;
2352 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2354 /* get stream for session */
2355 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2357 gst_rtspsrc_do_stream_eos (src, stream);
2362 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2364 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2368 /* try to get and configure a manager */
2370 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
2371 GstRTSPTransport * transport)
2373 const gchar *manager;
2375 GstStateChangeReturn ret;
2377 /* find a manager */
2378 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
2382 GST_DEBUG_OBJECT (src, "using manager %s", manager);
2384 /* configure the manager */
2385 if (src->manager == NULL) {
2386 GObjectClass *klass;
2389 if (!(src->manager = gst_element_factory_make (manager, NULL))) {
2391 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
2395 goto use_no_manager;
2397 if (!(src->manager = gst_element_factory_make (manager, NULL)))
2398 goto manager_failed;
2401 /* we manage this element */
2402 gst_bin_add (GST_BIN_CAST (src), src->manager);
2404 GST_OBJECT_LOCK (src);
2405 target = GST_STATE_TARGET (src);
2406 GST_OBJECT_UNLOCK (src);
2408 ret = gst_element_set_state (src->manager, target);
2409 if (ret == GST_STATE_CHANGE_FAILURE)
2410 goto start_manager_failure;
2412 g_object_set (src->manager, "latency", src->latency, NULL);
2414 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2415 if (g_object_class_find_property (klass, "drop-on-latency")) {
2416 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
2420 if (g_object_class_find_property (klass, "buffer-mode")) {
2421 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2422 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2424 gboolean need_slave;
2426 const gchar *encoding;
2428 /* buffer mode pauses are handled by adding offsets to buffer times,
2429 * but some depayloaders may have a hard time syncing output times
2430 * with such input times, e.g. container ones, most notably ASF */
2431 /* TODO alternatives are having an event that indicates these shifts,
2432 * or having rtsp extensions provide suggestion on buffer mode */
2433 need_slave = stream->container;
2434 if (stream->caps && (s = gst_caps_get_structure (stream->caps, 0)) &&
2435 (encoding = gst_structure_get_string (s, "encoding-name")))
2436 need_slave = need_slave || (strcmp (encoding, "X-ASF-PF") == 0);
2437 GST_DEBUG_OBJECT (src, "auto buffering mode, need_slave %d",
2439 /* valid duration implies not likely live pipeline,
2440 * so slaving in jitterbuffer does not make much sense
2441 * (and might mess things up due to bursts) */
2442 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
2443 src->segment.duration && !need_slave) {
2444 GST_DEBUG_OBJECT (src, "selected buffer");
2445 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER,
2448 GST_DEBUG_OBJECT (src, "selected slave");
2449 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2454 /* connect to signals if we did not already do so */
2455 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
2457 src->manager_sig_id =
2458 g_signal_connect (src->manager, "pad-added",
2459 (GCallback) new_manager_pad, src);
2460 src->manager_ptmap_id =
2461 g_signal_connect (src->manager, "request-pt-map",
2462 (GCallback) request_pt_map, src);
2464 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
2468 /* we stream directly to the manager, get some pads. Each RTSP stream goes
2469 * into a separate RTP session. */
2470 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
2471 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
2473 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
2474 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
2477 /* now configure the bandwidth in the manager */
2478 if (g_signal_lookup ("get-internal-session",
2479 G_OBJECT_TYPE (src->manager)) != 0) {
2480 GObject *rtpsession;
2482 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
2485 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
2487 stream->session = rtpsession;
2489 if (stream->as_bandwidth != -1) {
2490 GST_INFO_OBJECT (src, "setting AS: %f",
2491 (gdouble) (stream->as_bandwidth * 1000));
2492 g_object_set (rtpsession, "bandwidth",
2493 (gdouble) (stream->as_bandwidth * 1000), NULL);
2495 if (stream->rr_bandwidth != -1) {
2496 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
2497 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
2500 if (stream->rs_bandwidth != -1) {
2501 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
2502 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
2506 g_object_set (rtpsession, "probation", src->probation, NULL);
2508 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2510 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
2512 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
2514 g_signal_connect (rtpsession, "on-ssrc-active",
2515 (GCallback) on_ssrc_active, stream);
2526 GST_DEBUG_OBJECT (src, "cannot get a session manager");
2531 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
2534 start_manager_failure:
2536 GST_DEBUG_OBJECT (src, "could not start session manager");
2541 /* free the UDP sources allocated when negotiating a transport.
2542 * This function is called when the server negotiated to a transport where the
2543 * UDP sources are not needed anymore, such as TCP or multicast. */
2545 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
2549 for (i = 0; i < 2; i++) {
2550 if (stream->udpsrc[i]) {
2551 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
2552 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
2553 gst_object_unref (stream->udpsrc[i]);
2554 stream->udpsrc[i] = NULL;
2559 /* for TCP, create pads to send and receive data to and from the manager and to
2560 * intercept various events and queries
2563 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
2564 GstRTSPTransport * transport, GstPad ** outpad)
2567 GstPadTemplate *template;
2568 GstPad *pad0, *pad1;
2570 /* configure for interleaved delivery, nothing needs to be done
2571 * here, the loop function will call the chain functions of the
2572 * session manager. */
2573 stream->channel[0] = transport->interleaved.min;
2574 stream->channel[1] = transport->interleaved.max;
2575 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
2576 stream->channel[0], stream->channel[1]);
2578 /* we can remove the allocated UDP ports now */
2579 gst_rtspsrc_stream_free_udp (stream);
2581 /* no session manager, send data to srcpad directly */
2582 if (!stream->channelpad[0]) {
2583 GST_DEBUG_OBJECT (src, "no manager, creating pad");
2585 /* create a new pad we will use to stream to */
2586 name = g_strdup_printf ("stream_%u", stream->id);
2587 template = gst_static_pad_template_get (&rtptemplate);
2588 stream->channelpad[0] = gst_pad_new_from_template (template, name);
2589 gst_object_unref (template);
2592 /* set caps and activate */
2593 gst_pad_use_fixed_caps (stream->channelpad[0]);
2594 gst_pad_set_active (stream->channelpad[0], TRUE);
2596 *outpad = gst_object_ref (stream->channelpad[0]);
2598 GST_DEBUG_OBJECT (src, "using manager source pad");
2600 template = gst_static_pad_template_get (&anysrctemplate);
2602 /* allocate pads for sending the channel data into the manager */
2603 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
2604 gst_pad_link (pad0, stream->channelpad[0]);
2605 gst_object_unref (stream->channelpad[0]);
2606 stream->channelpad[0] = pad0;
2607 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
2608 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
2609 gst_pad_set_element_private (pad0, src);
2610 gst_pad_set_active (pad0, TRUE);
2612 if (stream->channelpad[1]) {
2613 /* if we have a sinkpad for the other channel, create a pad and link to the
2615 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
2616 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
2617 gst_pad_link (pad1, stream->channelpad[1]);
2618 gst_object_unref (stream->channelpad[1]);
2619 stream->channelpad[1] = pad1;
2620 gst_pad_set_active (pad1, TRUE);
2622 gst_object_unref (template);
2624 /* setup RTCP transport back to the server if we have to. */
2625 if (src->manager && src->do_rtcp) {
2628 template = gst_static_pad_template_get (&anysinktemplate);
2630 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
2631 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
2632 gst_pad_set_element_private (stream->rtcppad, stream);
2633 gst_pad_set_active (stream->rtcppad, TRUE);
2635 /* get session RTCP pad */
2636 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
2637 pad = gst_element_get_request_pad (src->manager, name);
2642 gst_pad_link (pad, stream->rtcppad);
2643 gst_object_unref (pad);
2646 gst_object_unref (template);
2652 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
2653 GstRTSPTransport * transport, const gchar ** destination, gint * min,
2654 gint * max, guint * ttl)
2656 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2658 if (!(*destination = transport->destination))
2659 *destination = stream->destination;
2662 /* transport first */
2663 *min = transport->port.min;
2664 *max = transport->port.max;
2665 if (*min == -1 && *max == -1) {
2666 /* then try from SDP */
2667 if (stream->port != 0) {
2668 *min = stream->port;
2669 *max = stream->port + 1;
2675 if (!(*ttl = transport->ttl))
2680 /* first take the source, then the endpoint to figure out where to send
2682 if (!(*destination = transport->source)) {
2683 if (src->conninfo.connection)
2684 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
2685 else if (stream->conninfo.connection)
2687 gst_rtsp_connection_get_ip (stream->conninfo.connection);
2691 /* for unicast we only expect the ports here */
2692 *min = transport->server_port.min;
2693 *max = transport->server_port.max;
2698 /* For multicast create UDP sources and join the multicast group. */
2700 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
2701 GstRTSPTransport * transport, GstPad ** outpad)
2704 const gchar *destination;
2707 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
2709 /* we can remove the allocated UDP ports now */
2710 gst_rtspsrc_stream_free_udp (stream);
2712 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
2715 /* we need a destination now */
2716 if (destination == NULL)
2717 goto no_destination;
2719 /* we really need ports now or we won't be able to receive anything at all */
2720 if (min == -1 && max == -1)
2723 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
2724 destination, min, max);
2726 /* creating UDP source for RTP */
2728 uri = g_strdup_printf ("udp://%s:%d", destination, min);
2730 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
2732 if (stream->udpsrc[0] == NULL)
2735 /* take ownership */
2736 gst_object_ref_sink (stream->udpsrc[0]);
2738 if (src->udp_buffer_size != 0)
2739 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
2740 src->udp_buffer_size, NULL);
2743 gst_element_set_state (stream->udpsrc[0], GST_STATE_PAUSED);
2746 /* creating another UDP source for RTCP */
2748 uri = g_strdup_printf ("udp://%s:%d", destination, max);
2750 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
2752 if (stream->udpsrc[1] == NULL)
2755 /* take ownership */
2756 gst_object_ref_sink (stream->udpsrc[1]);
2758 gst_element_set_state (stream->udpsrc[1], GST_STATE_PAUSED);
2765 GST_DEBUG_OBJECT (src, "no UDP source element found");
2770 GST_DEBUG_OBJECT (src, "no destination found");
2775 GST_DEBUG_OBJECT (src, "no ports found");
2780 /* configure the remainder of the UDP ports */
2782 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
2783 GstRTSPTransport * transport, GstPad ** outpad)
2785 /* we manage the UDP elements now. For unicast, the UDP sources where
2786 * allocated in the stream when we suggested a transport. */
2787 if (stream->udpsrc[0]) {
2788 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
2790 GST_DEBUG_OBJECT (src, "setting up UDP source");
2792 /* configure a timeout on the UDP port. When the timeout message is
2793 * posted, we assume UDP transport is not possible. We reconnect using TCP
2795 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", src->udp_timeout,
2798 /* get output pad of the UDP source. */
2799 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
2801 /* save it so we can unblock */
2802 stream->blockedpad = *outpad;
2804 /* configure pad block on the pad. As soon as there is dataflow on the
2805 * UDP source, we know that UDP is not blocked by a firewall and we can
2806 * configure all the streams to let the application autoplug decoders. */
2808 gst_pad_add_probe (stream->blockedpad,
2809 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, pad_blocked, src, NULL);
2811 if (stream->channelpad[0]) {
2812 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
2813 /* configure for UDP delivery, we need to connect the UDP pads to
2814 * the session plugin. */
2815 gst_pad_link (*outpad, stream->channelpad[0]);
2816 gst_object_unref (*outpad);
2818 /* we connected to pad-added signal to get pads from the manager */
2820 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
2825 if (stream->udpsrc[1]) {
2826 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
2828 if (stream->channelpad[1]) {
2831 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
2833 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
2834 gst_pad_link (pad, stream->channelpad[1]);
2835 gst_object_unref (pad);
2837 /* leave unlinked */
2843 /* configure the UDP sink back to the server for status reports */
2845 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
2846 GstRTSPStream * stream, GstRTSPTransport * transport)
2849 gint rtp_port, rtcp_port;
2850 gboolean do_rtp, do_rtcp;
2851 const gchar *destination;
2856 /* get transport info */
2857 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
2858 &rtp_port, &rtcp_port, &ttl);
2860 /* see what we need to do */
2861 do_rtp = (rtp_port != -1);
2862 /* it's possible that the server does not want us to send RTCP in which case
2864 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
2866 /* we need a destination when we have RTP or RTCP ports */
2867 if (destination == NULL && (do_rtp || do_rtcp))
2868 goto no_destination;
2870 /* try to construct the fakesrc to the RTP port of the server to open up any
2873 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
2876 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
2877 stream->udpsink[0] =
2878 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
2880 if (stream->udpsink[0] == NULL)
2881 goto no_sink_element;
2883 /* don't join multicast group, we will have the source socket do that */
2884 /* no sync or async state changes needed */
2885 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
2886 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
2888 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
2890 if (stream->udpsrc[0]) {
2891 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
2892 * so that NAT firewalls will open a hole for us */
2893 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
2894 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
2895 /* configure socket and make sure udpsink does not close it when shutting
2896 * down, it belongs to udpsrc after all. */
2897 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
2898 "close-socket", FALSE, NULL);
2899 g_object_unref (socket);
2902 /* the source for the dummy packets to open up NAT */
2903 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
2904 if (stream->fakesrc == NULL)
2905 goto no_fakesrc_element;
2907 /* random data in 5 buffers, a size of 200 bytes should be fine */
2908 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
2909 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
2911 /* we don't want to consider this a sink */
2912 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
2914 /* keep everything locked */
2915 gst_element_set_locked_state (stream->udpsink[0], TRUE);
2916 gst_element_set_locked_state (stream->fakesrc, TRUE);
2918 gst_object_ref (stream->udpsink[0]);
2919 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
2920 gst_object_ref (stream->fakesrc);
2921 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
2923 gst_element_link (stream->fakesrc, stream->udpsink[0]);
2926 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
2929 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
2930 stream->udpsink[1] =
2931 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
2933 if (stream->udpsink[1] == NULL)
2934 goto no_sink_element;
2936 /* don't join multicast group, we will have the source socket do that */
2937 /* no sync or async state changes needed */
2938 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
2939 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
2941 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
2943 if (stream->udpsrc[1]) {
2944 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
2945 * because some servers check the port number of where it sends RTCP to identify
2946 * the RTCP packets it receives */
2947 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
2948 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
2949 /* configure socket and make sure udpsink does not close it when shutting
2950 * down, it belongs to udpsrc after all. */
2951 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
2952 "close-socket", FALSE, NULL);
2953 g_object_unref (socket);
2956 /* we don't want to consider this a sink */
2957 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
2959 /* we keep this playing always */
2960 gst_element_set_locked_state (stream->udpsink[1], TRUE);
2961 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
2963 gst_object_ref (stream->udpsink[1]);
2964 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
2966 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
2968 /* get session RTCP pad */
2969 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
2970 pad = gst_element_get_request_pad (src->manager, name);
2975 gst_pad_link (pad, stream->rtcppad);
2976 gst_object_unref (pad);
2985 GST_DEBUG_OBJECT (src, "no destination address specified");
2990 GST_DEBUG_OBJECT (src, "no UDP sink element found");
2995 GST_DEBUG_OBJECT (src, "no fakesrc element found");
3000 /* sets up all elements needed for streaming over the specified transport.
3001 * Does not yet expose the element pads, this will be done when there is actuall
3002 * dataflow detected, which might never happen when UDP is blocked in a
3003 * firewall, for example.
3006 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3007 GstRTSPTransport * transport)
3010 GstPad *outpad = NULL;
3011 GstPadTemplate *template;
3016 src = stream->parent;
3018 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3020 s = gst_caps_get_structure (stream->caps, 0);
3022 /* get the proper mime type for this stream now */
3023 if (gst_rtsp_transport_get_mime (transport->trans, &mime) < 0)
3024 goto unknown_transport;
3026 goto unknown_transport;
3028 /* configure the final mime type */
3029 GST_DEBUG_OBJECT (src, "setting mime to %s", mime);
3030 gst_structure_set_name (s, mime);
3032 /* try to get and configure a manager, channelpad[0-1] will be configured with
3033 * the pads for the manager, or NULL when no manager is needed. */
3034 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3037 switch (transport->lower_transport) {
3038 case GST_RTSP_LOWER_TRANS_TCP:
3039 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3040 goto transport_failed;
3042 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3043 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3044 goto transport_failed;
3045 /* fallthrough, the rest is the same for UDP and MCAST */
3046 case GST_RTSP_LOWER_TRANS_UDP:
3047 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3048 goto transport_failed;
3049 /* configure udpsinks back to the server for RTCP messages and for the
3050 * dummy RTP messages to open NAT. */
3051 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3052 goto transport_failed;
3055 goto unknown_transport;
3059 GST_DEBUG_OBJECT (src, "creating ghostpad");
3061 gst_pad_use_fixed_caps (outpad);
3063 /* create ghostpad, don't add just yet, this will be done when we activate
3065 name = g_strdup_printf ("stream_%u", stream->id);
3066 template = gst_static_pad_template_get (&rtptemplate);
3067 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3068 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3069 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3070 gst_object_unref (template);
3073 gst_object_unref (outpad);
3075 /* mark pad as ok */
3076 stream->last_ret = GST_FLOW_OK;
3083 GST_DEBUG_OBJECT (src, "failed to configure transport");
3088 GST_DEBUG_OBJECT (src, "unknown transport");
3093 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3098 /* send a couple of dummy random packets on the receiver RTP port to the server,
3099 * this should make a firewall think we initiated the data transfer and
3100 * hopefully allow packets to go from the sender port to our RTP receiver port */
3102 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3106 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3109 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3110 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3112 if (stream->fakesrc && stream->udpsink[0]) {
3113 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3114 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3115 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3116 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3117 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3123 /* Adds the source pads of all configured streams to the element.
3124 * This code is performed when we detected dataflow.
3126 * We detect dataflow from either the _loop function or with pad probes on the
3130 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3134 GST_DEBUG_OBJECT (src, "activating streams");
3136 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3137 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3139 if (stream->udpsrc[0]) {
3140 /* remove timeout, we are streaming now and timeouts will be handled by
3141 * the session manager and jitter buffer */
3142 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3144 if (stream->srcpad) {
3145 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3146 gst_pad_set_active (stream->srcpad, TRUE);
3148 /* if we don't have a session manager, set the caps now. If we have a
3149 * session, we will get a notification of the pad and the caps. */
3150 if (!src->manager) {
3151 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3152 gst_pad_set_caps (stream->srcpad, stream->caps);
3155 if (!stream->added) {
3156 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3157 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3158 stream->added = TRUE;
3163 /* unblock all pads */
3164 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3165 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3167 if (stream->blockid) {
3168 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3169 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3170 stream->blockid = 0;
3178 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
3179 gboolean reset_manager)
3182 guint64 start, stop;
3183 gdouble play_speed, play_scale;
3185 GST_DEBUG_OBJECT (src, "configuring stream caps");
3187 start = segment->position;
3188 stop = segment->duration;
3189 play_speed = segment->rate;
3190 play_scale = segment->applied_rate;
3192 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3193 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3196 if ((caps = stream->caps)) {
3197 caps = gst_caps_make_writable (caps);
3199 if (stream->timebase != -1)
3200 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3201 (guint) stream->timebase, NULL);
3202 if (stream->seqbase != -1)
3203 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3204 (guint) stream->seqbase, NULL);
3205 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3207 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3208 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3209 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3211 stream->caps = caps;
3213 GST_DEBUG_OBJECT (src, "stream %p, caps %" GST_PTR_FORMAT, stream, caps);
3215 if (reset_manager && src->manager) {
3216 GST_DEBUG_OBJECT (src, "clear session");
3217 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3221 static GstFlowReturn
3222 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3227 /* store the value */
3228 stream->last_ret = ret;
3230 /* if it's success we can return the value right away */
3231 if (ret == GST_FLOW_OK)
3234 /* any other error that is not-linked can be returned right
3236 if (ret != GST_FLOW_NOT_LINKED)
3239 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3240 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3241 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3243 ret = ostream->last_ret;
3244 /* some other return value (must be SUCCESS but we can return
3245 * other values as well) */
3246 if (ret != GST_FLOW_NOT_LINKED)
3249 /* if we get here, all other pads were unlinked and we return
3250 * NOT_LINKED then */
3256 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
3259 gboolean res = TRUE;
3261 /* only streams that have a connection to the outside world */
3262 if (stream->srcpad == NULL)
3265 if (stream->udpsrc[0]) {
3266 gst_event_ref (event);
3267 res = gst_element_send_event (stream->udpsrc[0], event);
3268 } else if (stream->channelpad[0]) {
3269 gst_event_ref (event);
3270 if (GST_PAD_IS_SRC (stream->channelpad[0]))
3271 res = gst_pad_push_event (stream->channelpad[0], event);
3273 res = gst_pad_send_event (stream->channelpad[0], event);
3276 if (stream->udpsrc[1]) {
3277 gst_event_ref (event);
3278 res &= gst_element_send_event (stream->udpsrc[1], event);
3279 } else if (stream->channelpad[1]) {
3280 gst_event_ref (event);
3281 if (GST_PAD_IS_SRC (stream->channelpad[1]))
3282 res &= gst_pad_push_event (stream->channelpad[1], event);
3284 res &= gst_pad_send_event (stream->channelpad[1], event);
3288 gst_event_unref (event);
3294 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
3297 gboolean res = TRUE;
3299 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3300 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3302 gst_event_ref (event);
3303 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
3305 gst_event_unref (event);
3310 static GstRTSPResult
3311 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3316 if (info->connection == NULL) {
3317 if (info->url == NULL) {
3318 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
3319 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
3323 /* create connection */
3324 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
3325 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
3326 goto could_not_create;
3329 g_free (info->url_str);
3330 info->url_str = gst_rtsp_url_get_request_uri (info->url);
3332 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
3334 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
3335 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
3337 if (src->proxy_host) {
3338 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
3340 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
3345 if (!info->connected) {
3348 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
3349 ("Connecting to %s", info->location));
3350 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
3352 gst_rtsp_connection_connect (info->connection,
3353 src->ptcp_timeout)) < 0)
3354 goto could_not_connect;
3356 info->connected = TRUE;
3363 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
3368 gchar *str = gst_rtsp_strresult (res);
3369 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
3375 gchar *str = gst_rtsp_strresult (res);
3376 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
3382 static GstRTSPResult
3383 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
3386 GST_RTSP_STATE_LOCK (src);
3387 if (info->connected) {
3388 GST_DEBUG_OBJECT (src, "closing connection...");
3389 gst_rtsp_connection_close (info->connection);
3390 info->connected = FALSE;
3392 if (free && info->connection) {
3393 /* free connection */
3394 GST_DEBUG_OBJECT (src, "freeing connection...");
3395 gst_rtsp_connection_free (info->connection);
3396 info->connection = NULL;
3398 GST_RTSP_STATE_UNLOCK (src);
3402 static GstRTSPResult
3403 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3408 GST_DEBUG_OBJECT (src, "reconnecting connection...");
3409 gst_rtsp_conninfo_close (src, info, FALSE);
3410 res = gst_rtsp_conninfo_connect (src, info, async);
3416 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
3420 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
3421 GST_RTSP_STATE_LOCK (src);
3422 if (src->conninfo.connection) {
3423 GST_DEBUG_OBJECT (src, "connection flush");
3424 gst_rtsp_connection_flush (src->conninfo.connection, flush);
3426 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3427 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3428 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
3429 if (stream->conninfo.connection)
3430 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
3432 GST_RTSP_STATE_UNLOCK (src);
3435 /* FIXME, handle server request, reply with OK, for now */
3436 static GstRTSPResult
3437 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
3438 GstRTSPMessage * request)
3440 GstRTSPMessage response = { 0 };
3443 GST_DEBUG_OBJECT (src, "got server request message");
3446 gst_rtsp_message_dump (request);
3448 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
3450 if (res == GST_RTSP_ENOTIMPL) {
3451 /* default implementation, send OK */
3453 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
3458 GST_DEBUG_OBJECT (src, "replying with OK");
3461 gst_rtsp_message_dump (&response);
3463 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
3467 gst_rtsp_message_unset (&response);
3468 } else if (res == GST_RTSP_EEOF)
3476 gst_rtsp_message_unset (&response);
3481 /* send server keep-alive */
3482 static GstRTSPResult
3483 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
3485 GstRTSPMessage request = { 0 };
3487 GstRTSPMethod method;
3490 if (src->do_rtsp_keep_alive == FALSE) {
3491 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
3492 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3496 GST_DEBUG_OBJECT (src, "creating server keep-alive");
3498 /* find a method to use for keep-alive */
3499 if (src->methods & GST_RTSP_GET_PARAMETER)
3500 method = GST_RTSP_GET_PARAMETER;
3502 method = GST_RTSP_OPTIONS;
3505 control = src->control;
3507 control = src->conninfo.url_str;
3509 if (control == NULL)
3512 res = gst_rtsp_message_init_request (&request, method, control);
3517 gst_rtsp_message_dump (&request);
3520 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
3525 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3526 gst_rtsp_message_unset (&request);
3533 GST_WARNING_OBJECT (src, "no control url to send keepalive");
3538 gchar *str = gst_rtsp_strresult (res);
3540 gst_rtsp_message_unset (&request);
3541 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
3542 ("Could not send keep-alive. (%s)", str));
3548 static GstFlowReturn
3549 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
3551 GstRTSPMessage message = { 0 };
3554 GstRTSPStream *stream;
3555 GstPad *outpad = NULL;
3558 GstFlowReturn ret = GST_FLOW_OK;
3560 gboolean is_rtcp, have_data;
3562 /* here we are only interested in data messages */
3565 GTimeVal tv_timeout;
3567 /* get the next timeout interval */
3568 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3570 /* see if the timeout period expired */
3571 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
3572 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
3573 /* send keep-alive, only act on interrupt, a warning will be posted for
3575 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3577 /* get new timeout */
3578 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3581 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
3582 tv_timeout.tv_sec, tv_timeout.tv_usec);
3584 /* protect the connection with the connection lock so that we can see when
3585 * we are finished doing server communication */
3587 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
3588 &message, src->ptcp_timeout);
3592 GST_DEBUG_OBJECT (src, "we received a server message");
3594 case GST_RTSP_EINTR:
3595 /* we got interrupted this means we need to stop */
3597 case GST_RTSP_ETIMEOUT:
3598 /* no reply, send keep alive */
3599 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
3600 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3604 /* go EOS when the server closed the connection */
3610 switch (message.type) {
3611 case GST_RTSP_MESSAGE_REQUEST:
3612 /* server sends us a request message, handle it */
3614 gst_rtspsrc_handle_request (src, src->conninfo.connection,
3616 if (res == GST_RTSP_EEOF)
3619 goto handle_request_failed;
3621 case GST_RTSP_MESSAGE_RESPONSE:
3622 /* we ignore response messages */
3623 GST_DEBUG_OBJECT (src, "ignoring response message");
3625 gst_rtsp_message_dump (&message);
3627 case GST_RTSP_MESSAGE_DATA:
3628 GST_DEBUG_OBJECT (src, "got data message");
3632 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
3639 channel = message.type_data.data.channel;
3641 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
3643 goto unknown_stream;
3645 if (channel == stream->channel[0]) {
3646 outpad = stream->channelpad[0];
3648 } else if (channel == stream->channel[1]) {
3649 outpad = stream->channelpad[1];
3655 /* take a look at the body to figure out what we have */
3656 gst_rtsp_message_get_body (&message, &data, &size);
3658 goto invalid_length;
3660 /* channels are not correct on some servers, do extra check */
3661 if (data[1] >= 200 && data[1] <= 204) {
3662 /* hmm RTCP message switch to the RTCP pad of the same stream. */
3663 outpad = stream->channelpad[1];
3667 /* we have no clue what this is, just ignore then. */
3669 goto unknown_stream;
3671 /* take the message body for further processing */
3672 gst_rtsp_message_steal_body (&message, &data, &size);
3674 /* strip the trailing \0 */
3677 buf = gst_buffer_new ();
3678 gst_buffer_append_memory (buf,
3679 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
3681 /* don't need message anymore */
3682 gst_rtsp_message_unset (&message);
3684 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
3687 if (src->need_activate) {
3688 gst_rtspsrc_activate_streams (src);
3689 src->need_activate = FALSE;
3692 if (src->base_time == -1) {
3693 /* Take current running_time. This timestamp will be put on
3694 * the first buffer of each stream because we are a live source and so we
3695 * timestamp with the running_time. When we are dealing with TCP, we also
3696 * only timestamp the first buffer (using the DISCONT flag) because a server
3697 * typically bursts data, for which we don't want to compensate by speeding
3698 * up the media. The other timestamps will be interpollated from this one
3699 * using the RTP timestamps. */
3700 GST_OBJECT_LOCK (src);
3701 if (GST_ELEMENT_CLOCK (src)) {
3703 GstClockTime base_time;
3705 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
3706 base_time = GST_ELEMENT_CAST (src)->base_time;
3708 src->base_time = now - base_time;
3710 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
3711 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
3713 GST_OBJECT_UNLOCK (src);
3716 if (stream->discont && !is_rtcp) {
3717 /* mark first RTP buffer as discont */
3718 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
3719 stream->discont = FALSE;
3720 /* first buffer gets the timestamp, other buffers are not timestamped and
3721 * their presentation time will be interpollated from the rtp timestamps. */
3722 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
3723 GST_TIME_ARGS (src->base_time));
3725 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
3728 /* chain to the peer pad */
3729 if (GST_PAD_IS_SINK (outpad))
3730 ret = gst_pad_chain (outpad, buf);
3732 ret = gst_pad_push (outpad, buf);
3735 /* combine all stream flows for the data transport */
3736 ret = gst_rtspsrc_combine_flows (src, stream, ret);
3743 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
3744 gst_rtsp_message_unset (&message);
3749 GST_DEBUG_OBJECT (src, "we got an eof from the server");
3750 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3751 ("The server closed the connection."));
3752 src->conninfo.connected = FALSE;
3753 gst_rtsp_message_unset (&message);
3754 return GST_FLOW_EOS;
3758 gst_rtsp_message_unset (&message);
3759 GST_DEBUG_OBJECT (src, "got interrupted: stop connection flush");
3760 gst_rtspsrc_connection_flush (src, FALSE);
3761 return GST_FLOW_FLUSHING;
3765 gchar *str = gst_rtsp_strresult (res);
3767 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
3768 ("Could not receive message. (%s)", str));
3771 gst_rtsp_message_unset (&message);
3772 return GST_FLOW_ERROR;
3774 handle_request_failed:
3776 gchar *str = gst_rtsp_strresult (res);
3778 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
3779 ("Could not handle server message. (%s)", str));
3781 gst_rtsp_message_unset (&message);
3782 return GST_FLOW_ERROR;
3786 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3787 ("Short message received, ignoring."));
3788 gst_rtsp_message_unset (&message);
3793 static GstFlowReturn
3794 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
3797 GstRTSPMessage message = { 0 };
3801 GTimeVal tv_timeout;
3803 /* get the next timeout interval */
3804 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3806 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
3807 (gint) tv_timeout.tv_sec);
3809 gst_rtsp_message_unset (&message);
3811 /* we should continue reading the TCP socket because the server might
3812 * send us requests. When the session timeout expires, we need to send a
3813 * keep-alive request to keep the session open. */
3814 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
3815 &message, &tv_timeout);
3819 GST_DEBUG_OBJECT (src, "we received a server message");
3821 case GST_RTSP_EINTR:
3822 /* we got interrupted, see what we have to do */
3824 case GST_RTSP_ETIMEOUT:
3825 /* send keep-alive, ignore the result, a warning will be posted. */
3826 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
3827 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3831 /* server closed the connection. not very fatal for UDP, reconnect and
3832 * see what happens. */
3833 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3834 ("The server closed the connection."));
3836 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
3841 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
3843 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3844 ("Unhandled return value %d.", res));
3848 switch (message.type) {
3849 case GST_RTSP_MESSAGE_REQUEST:
3850 /* server sends us a request message, handle it */
3852 gst_rtspsrc_handle_request (src, src->conninfo.connection,
3854 if (res == GST_RTSP_EEOF)
3857 goto handle_request_failed;
3859 case GST_RTSP_MESSAGE_RESPONSE:
3860 /* we ignore response and data messages */
3861 GST_DEBUG_OBJECT (src, "ignoring response message");
3863 gst_rtsp_message_dump (&message);
3864 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
3865 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
3866 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
3867 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
3868 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3875 case GST_RTSP_MESSAGE_DATA:
3876 /* we ignore response and data messages */
3877 GST_DEBUG_OBJECT (src, "ignoring data message");
3880 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
3886 /* we get here when the connection got interrupted */
3889 gst_rtsp_message_unset (&message);
3890 GST_DEBUG_OBJECT (src, "got interrupted: stop connection flush");
3891 gst_rtspsrc_connection_flush (src, FALSE);
3892 return GST_FLOW_FLUSHING;
3896 gchar *str = gst_rtsp_strresult (res);
3899 src->conninfo.connected = FALSE;
3900 if (res != GST_RTSP_EINTR) {
3901 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
3902 ("Could not connect to server. (%s)", str));
3904 ret = GST_FLOW_ERROR;
3906 ret = GST_FLOW_FLUSHING;
3912 gchar *str = gst_rtsp_strresult (res);
3914 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
3915 ("Could not receive message. (%s)", str));
3917 return GST_FLOW_ERROR;
3919 handle_request_failed:
3921 gchar *str = gst_rtsp_strresult (res);
3924 gst_rtsp_message_unset (&message);
3925 if (res != GST_RTSP_EINTR) {
3926 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
3927 ("Could not handle server message. (%s)", str));
3929 ret = GST_FLOW_ERROR;
3931 ret = GST_FLOW_FLUSHING;
3937 GST_DEBUG_OBJECT (src, "we got an eof from the server");
3938 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3939 ("The server closed the connection."));
3940 src->conninfo.connected = FALSE;
3941 gst_rtsp_message_unset (&message);
3942 return GST_FLOW_EOS;
3946 static GstRTSPResult
3947 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
3949 GstRTSPResult res = GST_RTSP_OK;
3952 GST_DEBUG_OBJECT (src, "doing reconnect");
3954 GST_OBJECT_LOCK (src);
3955 /* only restart when the pads were not yet activated, else we were
3956 * streaming over UDP */
3957 restart = src->need_activate;
3958 GST_OBJECT_UNLOCK (src);
3960 /* no need to restart, we're done */
3964 /* we can try only TCP now */
3965 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
3967 /* close and cleanup our state */
3968 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
3971 /* see if we have TCP left to try. Also don't try TCP when we were configured
3973 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
3976 /* We post a warning message now to inform the user
3977 * that nothing happened. It's most likely a firewall thing. */
3978 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3979 ("Could not receive any UDP packets for %.4f seconds, maybe your "
3980 "firewall is blocking it. Retrying using a TCP connection.",
3981 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
3983 /* open new connection using tcp */
3984 if (gst_rtspsrc_open (src, async) < 0)
3987 /* start playback */
3988 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
3997 src->cur_protocols = 0;
3998 /* no transport possible, post an error and stop */
3999 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4000 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4001 "firewall is blocking it. No other protocols to try.",
4002 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4003 return GST_RTSP_ERROR;
4007 GST_DEBUG_OBJECT (src, "open failed");
4012 GST_DEBUG_OBJECT (src, "play failed");
4018 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
4022 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
4025 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
4028 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
4031 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
4039 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
4043 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
4046 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
4049 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
4052 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4060 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4064 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4067 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4070 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4073 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4081 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4085 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
4088 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4091 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4094 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4102 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4104 if (ret == GST_RTSP_OK)
4105 gst_rtspsrc_loop_complete_cmd (src, cmd);
4106 else if (ret == GST_RTSP_EINTR)
4107 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4109 gst_rtspsrc_loop_error_cmd (src, cmd);
4113 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
4117 /* start new request */
4118 gst_rtspsrc_loop_start_cmd (src, cmd);
4120 GST_DEBUG_OBJECT (src, "sending cmd %d", cmd);
4122 GST_OBJECT_LOCK (src);
4123 old = src->pending_cmd;
4124 if (old != CMD_WAIT) {
4125 src->pending_cmd = CMD_WAIT;
4126 GST_OBJECT_UNLOCK (src);
4127 /* cancel previous request */
4128 gst_rtspsrc_loop_cancel_cmd (src, old);
4129 GST_OBJECT_LOCK (src);
4131 src->pending_cmd = cmd;
4132 /* interrupt if allowed */
4133 if (src->busy_cmd & mask) {
4134 GST_DEBUG_OBJECT (src, "connection flush busy %d", src->busy_cmd);
4135 gst_rtspsrc_connection_flush (src, TRUE);
4137 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %d", src->busy_cmd);
4140 gst_task_start (src->task);
4141 GST_OBJECT_UNLOCK (src);
4145 gst_rtspsrc_loop (GstRTSPSrc * src)
4149 if (!src->conninfo.connection || !src->conninfo.connected)
4152 if (src->interleaved)
4153 ret = gst_rtspsrc_loop_interleaved (src);
4155 ret = gst_rtspsrc_loop_udp (src);
4157 if (ret != GST_FLOW_OK)
4165 GST_WARNING_OBJECT (src, "we are not connected");
4166 ret = GST_FLOW_FLUSHING;
4171 const gchar *reason = gst_flow_get_name (ret);
4173 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
4174 src->running = FALSE;
4175 if (ret == GST_FLOW_EOS) {
4176 /* perform EOS logic */
4177 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4178 gst_element_post_message (GST_ELEMENT_CAST (src),
4179 gst_message_new_segment_done (GST_OBJECT_CAST (src),
4180 src->segment.format, src->segment.position));
4181 gst_rtspsrc_push_event (src,
4182 gst_event_new_segment_done (src->segment.format,
4183 src->segment.position));
4185 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4187 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
4188 /* for fatal errors we post an error message, post the error before the
4189 * EOS so the app knows about the error first. */
4190 GST_ELEMENT_ERROR (src, STREAM, FAILED,
4191 ("Internal data flow error."),
4192 ("streaming task paused, reason %s (%d)", reason, ret));
4193 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4199 #ifndef GST_DISABLE_GST_DEBUG
4200 static const gchar *
4201 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
4205 while (method != 0) {
4222 static const gchar *
4223 gst_rtspsrc_skip_lws (const gchar * s)
4225 while (g_ascii_isspace (*s))
4230 static const gchar *
4231 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
4233 while (s > start && g_ascii_isspace (*(s - 1)))
4238 static const gchar *
4239 gst_rtspsrc_skip_commas (const gchar * s)
4241 /* The grammar allows for multiple commas */
4242 while (g_ascii_isspace (*s) || *s == ',')
4247 static const gchar *
4248 gst_rtspsrc_skip_item (const gchar * s)
4250 gboolean quoted = FALSE;
4251 const gchar *start = s;
4253 /* A list item ends at the last non-whitespace character
4254 * before a comma which is not inside a quoted-string. Or at
4255 * the end of the string.
4261 if (*s == '\\' && *(s + 1))
4270 return gst_rtspsrc_unskip_lws (s, start);
4274 gst_rtsp_decode_quoted_string (gchar * quoted_string)
4278 src = quoted_string + 1;
4279 dst = quoted_string;
4280 while (*src && *src != '"') {
4281 if (*src == '\\' && *(src + 1))
4288 /* Extract the authentication tokens that the server provided for each method
4289 * into an array of structures and give those to the connection object.
4292 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
4293 const gchar * header, gboolean * stale)
4295 GSList *list = NULL, *iter;
4297 gchar *item, *eq, *name_end, *value;
4299 g_return_if_fail (stale != NULL);
4301 gst_rtsp_connection_clear_auth_params (conn);
4304 /* Parse a header whose content is described by RFC2616 as
4305 * "#something", where "something" does not itself contain commas,
4306 * except as part of quoted-strings, into a list of allocated strings.
4308 header = gst_rtspsrc_skip_commas (header);
4310 end = gst_rtspsrc_skip_item (header);
4311 list = g_slist_prepend (list, g_strndup (header, end - header));
4312 header = gst_rtspsrc_skip_commas (end);
4317 list = g_slist_reverse (list);
4318 for (iter = list; iter; iter = iter->next) {
4321 eq = strchr (item, '=');
4323 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
4324 if (name_end == item) {
4325 /* That's no good... */
4332 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
4334 gst_rtsp_decode_quoted_string (value);
4338 if (item && (strcmp (item, "stale") == 0) &&
4339 value && (strcmp (value, "TRUE") == 0))
4341 gst_rtsp_connection_set_auth_param (conn, item, value);
4345 g_slist_free (list);
4348 /* Parse a WWW-Authenticate Response header and determine the
4349 * available authentication methods
4351 * This code should also cope with the fact that each WWW-Authenticate
4352 * header can contain multiple challenge methods + tokens
4354 * At the moment, for Basic auth, we just do a minimal check and don't
4355 * even parse out the realm */
4357 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
4358 GstRTSPConnection * conn, gboolean * stale)
4362 g_return_if_fail (hdr != NULL);
4363 g_return_if_fail (methods != NULL);
4364 g_return_if_fail (stale != NULL);
4366 /* Skip whitespace at the start of the string */
4367 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
4369 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
4370 *methods |= GST_RTSP_AUTH_BASIC;
4371 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
4372 *methods |= GST_RTSP_AUTH_DIGEST;
4373 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
4378 * gst_rtspsrc_setup_auth:
4379 * @src: the rtsp source
4381 * Configure a username and password and auth method on the
4382 * connection object based on a response we received from the
4385 * Currently, this requires that a username and password were supplied
4386 * in the uri. In the future, they may be requested on demand by sending
4387 * a message up the bus.
4389 * Returns: TRUE if authentication information could be set up correctly.
4392 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
4396 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
4397 GstRTSPAuthMethod method;
4398 GstRTSPResult auth_result;
4400 GstRTSPConnection *conn;
4402 gboolean stale = FALSE;
4404 conn = src->conninfo.connection;
4406 /* Identify the available auth methods and see if any are supported */
4407 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
4408 &hdr, 0) == GST_RTSP_OK) {
4409 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
4412 if (avail_methods == GST_RTSP_AUTH_NONE)
4413 goto no_auth_available;
4415 /* For digest auth, if the response indicates that the session
4416 * data are stale, we just update them in the connection object and
4417 * return TRUE to retry the request */
4419 src->tried_url_auth = FALSE;
4421 url = gst_rtsp_connection_get_url (conn);
4423 /* Do we have username and password available? */
4424 if (url != NULL && !src->tried_url_auth && url->user != NULL
4425 && url->passwd != NULL) {
4428 src->tried_url_auth = TRUE;
4429 GST_DEBUG_OBJECT (src,
4430 "Attempting authentication using credentials from the URL");
4432 user = src->user_id;
4433 pass = src->user_pw;
4434 GST_DEBUG_OBJECT (src,
4435 "Attempting authentication using credentials from the properties");
4438 /* FIXME: If the url didn't contain username and password or we tried them
4439 * already, request a username and passwd from the application via some kind
4440 * of credentials request message */
4442 /* If we don't have a username and passwd at this point, bail out. */
4443 if (user == NULL || pass == NULL)
4446 /* Try to configure for each available authentication method, strongest to
4448 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
4449 /* Check if this method is available on the server */
4450 if ((method & avail_methods) == 0)
4453 /* Pass the credentials to the connection to try on the next request */
4454 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
4455 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
4456 * ignore it and end up retrying later */
4457 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
4458 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
4459 gst_rtsp_auth_method_to_string (method));
4464 if (method == GST_RTSP_AUTH_NONE)
4465 goto no_auth_available;
4471 /* Output an error indicating that we couldn't connect because there were
4472 * no supported authentication protocols */
4473 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4474 ("No supported authentication protocol was found"));
4479 /* We don't fire an error message, we just return FALSE and let the
4480 * normal NOT_AUTHORIZED error be propagated */
4485 static GstRTSPResult
4486 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4487 GstRTSPMessage * request, GstRTSPMessage * response,
4488 GstRTSPStatusCode * code)
4491 GstRTSPStatusCode thecode;
4492 gchar *content_base = NULL;
4496 if (!src->short_header)
4497 gst_rtsp_ext_list_before_send (src->extensions, request);
4499 GST_DEBUG_OBJECT (src, "sending message");
4502 gst_rtsp_message_dump (request);
4504 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
4508 gst_rtsp_connection_reset_timeout (conn);
4511 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
4516 gst_rtsp_message_dump (response);
4518 switch (response->type) {
4519 case GST_RTSP_MESSAGE_REQUEST:
4520 res = gst_rtspsrc_handle_request (src, conn, response);
4521 if (res == GST_RTSP_EEOF)
4524 goto handle_request_failed;
4526 case GST_RTSP_MESSAGE_RESPONSE:
4527 /* ok, a response is good */
4528 GST_DEBUG_OBJECT (src, "received response message");
4530 case GST_RTSP_MESSAGE_DATA:
4531 /* get next response */
4532 GST_DEBUG_OBJECT (src, "ignoring data response message");
4535 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4540 thecode = response->type_data.response.code;
4542 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
4544 /* if the caller wanted the result code, we store it. */
4548 /* If the request didn't succeed, bail out before doing any more */
4549 if (thecode != GST_RTSP_STS_OK)
4552 /* store new content base if any */
4553 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
4556 g_free (src->content_base);
4557 src->content_base = g_strdup (content_base);
4559 gst_rtsp_ext_list_after_send (src->extensions, request, response);
4566 gchar *str = gst_rtsp_strresult (res);
4568 if (res != GST_RTSP_EINTR) {
4569 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4570 ("Could not send message. (%s)", str));
4572 GST_WARNING_OBJECT (src, "send interrupted");
4581 GST_WARNING_OBJECT (src, "server closed connection, doing reconnect");
4584 /* if reconnect succeeds, try again */
4586 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
4590 /* only try once after reconnect, then fallthrough and error out */
4593 gchar *str = gst_rtsp_strresult (res);
4595 if (res != GST_RTSP_EINTR) {
4596 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4597 ("Could not receive message. (%s)", str));
4599 GST_WARNING_OBJECT (src, "receive interrupted");
4607 handle_request_failed:
4609 /* ERROR was posted */
4610 gst_rtsp_message_unset (response);
4615 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4616 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4617 ("The server closed the connection."));
4618 gst_rtsp_message_unset (response);
4625 * @src: the rtsp source
4626 * @conn: the connection to send on
4627 * @request: must point to a valid request
4628 * @response: must point to an empty #GstRTSPMessage
4629 * @code: an optional code result
4631 * send @request and retrieve the response in @response. optionally @code can be
4632 * non-NULL in which case it will contain the status code of the response.
4634 * If This function returns #GST_RTSP_OK, @response will contain a valid response
4635 * message that should be cleaned with gst_rtsp_message_unset() after usage.
4637 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
4638 * @response message) if the response code was not 200 (OK).
4640 * If the attempt results in an authentication failure, then this will attempt
4641 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
4644 * Returns: #GST_RTSP_OK if the processing was successful.
4646 static GstRTSPResult
4647 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4648 GstRTSPMessage * request, GstRTSPMessage * response,
4649 GstRTSPStatusCode * code)
4651 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
4652 GstRTSPResult res = GST_RTSP_ERROR;
4655 GstRTSPMethod method = GST_RTSP_INVALID;
4661 /* make sure we don't loop forever */
4665 /* save method so we can disable it when the server complains */
4666 method = request->type_data.request.method;
4669 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
4673 case GST_RTSP_STS_UNAUTHORIZED:
4674 if (gst_rtspsrc_setup_auth (src, response)) {
4675 /* Try the request/response again after configuring the auth info
4683 } while (retry == TRUE);
4685 /* If the user requested the code, let them handle errors, otherwise
4686 * post an error below */
4689 else if (int_code != GST_RTSP_STS_OK)
4690 goto error_response;
4697 GST_DEBUG_OBJECT (src, "got error %d", res);
4702 res = GST_RTSP_ERROR;
4704 switch (response->type_data.response.code) {
4705 case GST_RTSP_STS_NOT_FOUND:
4706 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
4707 response->type_data.response.reason));
4709 case GST_RTSP_STS_MOVED_PERMANENTLY:
4710 case GST_RTSP_STS_MOVE_TEMPORARILY:
4712 gchar *new_location;
4713 GstRTSPLowerTrans transports;
4715 GST_DEBUG_OBJECT (src, "got redirection");
4716 /* if we don't have a Location Header, we must error */
4717 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
4718 &new_location, 0) < 0)
4721 /* When we receive a redirect result, we go back to the INIT state after
4722 * parsing the new URI. The caller should do the needed steps to issue
4723 * a new setup when it detects this state change. */
4724 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
4726 /* save current transports */
4727 if (src->conninfo.url)
4728 transports = src->conninfo.url->transports;
4730 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
4732 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
4734 /* set old transports */
4735 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
4736 src->conninfo.url->transports = transports;
4738 src->need_redirect = TRUE;
4739 src->state = GST_RTSP_STATE_INIT;
4743 case GST_RTSP_STS_NOT_ACCEPTABLE:
4744 case GST_RTSP_STS_NOT_IMPLEMENTED:
4745 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
4746 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
4747 gst_rtsp_method_as_text (method));
4748 src->methods &= ~method;
4752 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4753 ("Got error response: %d (%s).", response->type_data.response.code,
4754 response->type_data.response.reason));
4757 /* if we return ERROR we should unset the response ourselves */
4758 if (res == GST_RTSP_ERROR)
4759 gst_rtsp_message_unset (response);
4765 static GstRTSPResult
4766 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
4767 GstRTSPMessage * response, GstRTSPSrc * src)
4769 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
4774 /* parse the response and collect all the supported methods. We need this
4775 * information so that we don't try to send an unsupported request to the
4779 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
4781 GstRTSPHeaderField field;
4787 /* reset supported methods */
4790 /* Try Allow Header first */
4791 field = GST_RTSP_HDR_ALLOW;
4794 gst_rtsp_message_get_header (response, field, &respoptions, indx);
4795 if (indx == 0 && !respoptions) {
4796 /* if no Allow header was found then try the Public header... */
4797 field = GST_RTSP_HDR_PUBLIC;
4798 gst_rtsp_message_get_header (response, field, &respoptions, indx);
4803 /* If we get here, the server gave a list of supported methods, parse
4804 * them here. The string is like:
4806 * OPTIONS, DESCRIBE, ANNOUNCE, PLAY, SETUP, ...
4808 options = g_strsplit (respoptions, ",", 0);
4810 for (i = 0; options[i]; i++) {
4814 stripped = g_strstrip (options[i]);
4815 method = gst_rtsp_find_method (stripped);
4817 /* keep bitfield of supported methods */
4818 if (method != GST_RTSP_INVALID)
4819 src->methods |= method;
4821 g_strfreev (options);
4826 if (src->methods == 0) {
4827 /* neither Allow nor Public are required, assume the server supports
4828 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
4830 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
4831 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
4833 /* always assume PLAY, FIXME, extensions should be able to override
4835 src->methods |= GST_RTSP_PLAY;
4836 /* also assume it will support Range */
4837 src->seekable = TRUE;
4839 /* we need describe and setup */
4840 if (!(src->methods & GST_RTSP_DESCRIBE))
4842 if (!(src->methods & GST_RTSP_SETUP))
4850 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4851 ("Server does not support DESCRIBE."));
4856 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4857 ("Server does not support SETUP."));
4862 /* masks to be kept in sync with the hardcoded protocol order of preference
4864 static guint protocol_masks[] = {
4865 GST_RTSP_LOWER_TRANS_UDP,
4866 GST_RTSP_LOWER_TRANS_UDP_MCAST,
4867 GST_RTSP_LOWER_TRANS_TCP,
4871 static GstRTSPResult
4872 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
4873 GstRTSPLowerTrans protocols, gchar ** transports)
4877 gboolean add_udp_str;
4882 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
4887 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
4889 /* extension listed transports, use those */
4890 if (*transports != NULL)
4893 /* it's the default */
4894 add_udp_str = FALSE;
4896 /* the default RTSP transports */
4897 result = g_string_new ("");
4898 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
4899 GST_DEBUG_OBJECT (src, "adding UDP unicast");
4901 g_string_append (result, "RTP/AVP");
4903 g_string_append (result, "/UDP");
4904 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
4905 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
4906 GST_DEBUG_OBJECT (src, "adding UDP multicast");
4908 /* we don't have to allocate any UDP ports yet, if the selected transport
4909 * turns out to be multicast we can create them and join the multicast
4910 * group indicated in the transport reply */
4911 if (result->len > 0)
4912 g_string_append (result, ",");
4913 g_string_append (result, "RTP/AVP");
4915 g_string_append (result, "/UDP");
4916 g_string_append (result, ";multicast");
4917 if (src->next_port_num != 0) {
4918 if (src->client_port_range.max > 0 &&
4919 src->next_port_num >= src->client_port_range.max)
4922 g_string_append_printf (result, ";client_port=%d-%d",
4923 src->next_port_num, src->next_port_num + 1);
4925 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
4926 GST_DEBUG_OBJECT (src, "adding TCP");
4928 if (result->len > 0)
4929 g_string_append (result, ",");
4930 g_string_append (result, "RTP/AVP/TCP;unicast;interleaved=%%i1-%%i2");
4932 *transports = g_string_free (result, FALSE);
4934 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
4941 GST_ERROR ("extension gave error %d", res);
4946 GST_ERROR ("no more ports available");
4947 return GST_RTSP_ERROR;
4951 static GstRTSPResult
4952 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
4953 gint orig_rtpport, gint orig_rtcpport)
4956 gint nr_udp, nr_int;
4958 gint rtpport = 0, rtcpport = 0;
4961 src = stream->parent;
4963 /* find number of placeholders first */
4964 if (strstr (*transports, "%%i2"))
4966 else if (strstr (*transports, "%%i1"))
4971 if (strstr (*transports, "%%u2"))
4973 else if (strstr (*transports, "%%u1"))
4978 if (nr_udp == 0 && nr_int == 0)
4982 if (!orig_rtpport || !orig_rtcpport) {
4983 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
4986 rtpport = orig_rtpport;
4987 rtcpport = orig_rtcpport;
4991 str = g_string_new ("");
4993 while ((next = strstr (p, "%%"))) {
4994 g_string_append_len (str, p, next - p);
4995 if (next[2] == 'u') {
4997 g_string_append_printf (str, "%d", rtpport);
4998 else if (next[3] == '2')
4999 g_string_append_printf (str, "%d", rtcpport);
5001 if (next[2] == 'i') {
5003 g_string_append_printf (str, "%d", src->free_channel);
5004 else if (next[3] == '2')
5005 g_string_append_printf (str, "%d", src->free_channel + 1);
5010 /* append final part */
5011 g_string_append (str, p);
5013 g_free (*transports);
5014 *transports = g_string_free (str, FALSE);
5022 GST_ERROR ("failed to allocate udp ports");
5023 return GST_RTSP_ERROR;
5028 gst_rtspsrc_stream_is_real_media (GstRTSPStream * stream)
5030 gboolean res = FALSE;
5034 const gchar *enc = NULL;
5036 s = gst_caps_get_structure (stream->caps, 0);
5037 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
5038 res = (strstr (enc, "-REAL") != NULL);
5044 /* Perform the SETUP request for all the streams.
5046 * We ask the server for a specific transport, which initially includes all the
5047 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
5048 * two local UDP ports that we send to the server.
5050 * Once the server replied with a transport, we configure the other streams
5051 * with the same transport.
5053 * This function will also configure the stream for the selected transport,
5054 * which basically means creating the pipeline.
5056 static GstRTSPResult
5057 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
5060 GstRTSPResult res = GST_RTSP_ERROR;
5061 GstRTSPMessage request = { 0 };
5062 GstRTSPMessage response = { 0 };
5063 GstRTSPStream *stream = NULL;
5064 GstRTSPLowerTrans protocols;
5065 GstRTSPStatusCode code;
5066 gboolean unsupported_real = FALSE;
5067 gint rtpport, rtcpport;
5071 if (src->conninfo.connection) {
5072 url = gst_rtsp_connection_get_url (src->conninfo.connection);
5073 /* we initially allow all configured lower transports. based on the URL
5074 * transports and the replies from the server we narrow them down. */
5075 protocols = url->transports & src->cur_protocols;
5078 protocols = src->cur_protocols;
5084 /* reset some state */
5085 src->free_channel = 0;
5086 src->interleaved = FALSE;
5087 src->need_activate = FALSE;
5088 /* keep track of next port number, 0 is random */
5089 src->next_port_num = src->client_port_range.min;
5090 rtpport = rtcpport = 0;
5092 if (G_UNLIKELY (src->streams == NULL))
5095 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5096 GstRTSPConnection *conn;
5101 stream = (GstRTSPStream *) walk->data;
5103 /* see if we need to configure this stream */
5104 if (!gst_rtsp_ext_list_configure_stream (src->extensions, stream->caps)) {
5105 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
5107 stream->disabled = TRUE;
5111 /* merge/overwrite global caps */
5116 s = gst_caps_get_structure (stream->caps, 0);
5118 num = gst_structure_n_fields (src->props);
5119 for (j = 0; j < num; j++) {
5123 name = gst_structure_nth_field_name (src->props, j);
5124 val = gst_structure_get_value (src->props, name);
5125 gst_structure_set_value (s, name, val);
5127 GST_DEBUG_OBJECT (src, "copied %s", name);
5131 /* skip setup if we have no URL for it */
5132 if (stream->conninfo.location == NULL) {
5133 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
5137 if (src->conninfo.connection == NULL) {
5138 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
5139 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
5142 conn = stream->conninfo.connection;
5144 conn = src->conninfo.connection;
5146 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
5147 stream->conninfo.location);
5149 /* if we have a multicast connection, only suggest multicast from now on */
5150 if (stream->is_multicast)
5151 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
5154 /* first selectable protocol */
5155 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5157 if (!protocol_masks[mask])
5161 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
5162 protocol_masks[mask]);
5163 /* create a string with first transport in line */
5165 res = gst_rtspsrc_create_transports_string (src,
5166 protocols & protocol_masks[mask], &transports);
5167 if (res < 0 || transports == NULL)
5168 goto setup_transport_failed;
5170 if (strlen (transports) == 0) {
5171 g_free (transports);
5172 GST_DEBUG_OBJECT (src, "no transports found");
5177 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
5179 /* replace placeholders with real values, this function will optionally
5180 * allocate UDP ports and other info needed to execute the setup request */
5181 res = gst_rtspsrc_prepare_transports (stream, &transports,
5182 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
5184 g_free (transports);
5185 goto setup_transport_failed;
5188 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
5190 /* create SETUP request */
5192 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
5193 stream->conninfo.location);
5195 g_free (transports);
5196 goto create_request_failed;
5199 /* select transport, copy is made when adding to header so we can free it. */
5200 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
5201 g_free (transports);
5203 /* if the user wants a non default RTP packet size we add the blocksize
5205 if (src->rtp_blocksize > 0) {
5206 hval = g_strdup_printf ("%d", src->rtp_blocksize);
5207 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
5212 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
5215 /* handle the code ourselves */
5216 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
5220 case GST_RTSP_STS_OK:
5222 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
5223 gst_rtsp_message_unset (&request);
5224 gst_rtsp_message_unset (&response);
5225 /* cleanup of leftover transport */
5226 gst_rtspsrc_stream_free_udp (stream);
5227 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
5228 * we might be in this case */
5229 if (stream->container && rtpport && rtcpport && !retry) {
5230 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
5235 /* this transport did not go down well, but we may have others to try
5236 * that we did not send yet, try those and only give up then
5237 * but not without checking for lost cause/extension so we can
5238 * post a nicer/more useful error message later */
5239 if (!unsupported_real)
5240 unsupported_real = gst_rtspsrc_stream_is_real_media (stream);
5241 /* select next available protocol, give up on this stream if none */
5243 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5245 if (!protocol_masks[mask] || unsupported_real)
5250 /* cleanup of leftover transport and move to the next stream */
5251 gst_rtspsrc_stream_free_udp (stream);
5252 goto response_error;
5255 /* parse response transport */
5257 gchar *resptrans = NULL;
5258 GstRTSPTransport transport = { 0 };
5260 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
5263 gst_rtspsrc_stream_free_udp (stream);
5267 /* parse transport, go to next stream on parse error */
5268 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
5269 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
5273 /* update allowed transports for other streams. once the transport of
5274 * one stream has been determined, we make sure that all other streams
5275 * are configured in the same way */
5276 switch (transport.lower_transport) {
5277 case GST_RTSP_LOWER_TRANS_TCP:
5278 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
5279 protocols = GST_RTSP_LOWER_TRANS_TCP;
5280 src->interleaved = TRUE;
5281 /* update free channels */
5283 MAX (transport.interleaved.min, src->free_channel);
5285 MAX (transport.interleaved.max, src->free_channel);
5286 src->free_channel++;
5288 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
5289 /* only allow multicast for other streams */
5290 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
5291 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
5292 /* if the server selected our ports, increment our counters so that
5293 * we select a new port later */
5294 if (src->next_port_num == transport.port.min &&
5295 src->next_port_num + 1 == transport.port.max) {
5296 src->next_port_num += 2;
5299 case GST_RTSP_LOWER_TRANS_UDP:
5300 /* only allow unicast for other streams */
5301 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
5302 protocols = GST_RTSP_LOWER_TRANS_UDP;
5305 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
5306 transport.lower_transport);
5310 if (!stream->container || (!src->interleaved && !retry)) {
5311 /* now configure the stream with the selected transport */
5312 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
5313 GST_DEBUG_OBJECT (src,
5314 "could not configure stream %p transport, skipping stream",
5317 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
5318 /* retain the first allocated UDP port pair */
5319 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
5320 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
5323 /* we need to activate at least one streams when we detect activity */
5324 src->need_activate = TRUE;
5326 /* clean up our transport struct */
5327 gst_rtsp_transport_init (&transport);
5328 /* clean up used RTSP messages */
5329 gst_rtsp_message_unset (&request);
5330 gst_rtsp_message_unset (&response);
5334 /* store the transport protocol that was configured */
5335 src->cur_protocols = protocols;
5337 gst_rtsp_ext_list_stream_select (src->extensions, url);
5339 /* if there is nothing to activate, error out */
5340 if (!src->need_activate)
5341 goto nothing_to_activate;
5348 /* no transport possible, post an error and stop */
5349 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5350 ("Could not connect to server, no protocols left"));
5351 return GST_RTSP_ERROR;
5355 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5356 ("SDP contains no streams"));
5357 return GST_RTSP_ERROR;
5359 create_request_failed:
5361 gchar *str = gst_rtsp_strresult (res);
5363 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5364 ("Could not create request. (%s)", str));
5368 setup_transport_failed:
5370 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5371 ("Could not setup transport."));
5372 res = GST_RTSP_ERROR;
5377 const gchar *str = gst_rtsp_status_as_text (code);
5379 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5380 ("Error (%d): %s", code, GST_STR_NULL (str)));
5381 res = GST_RTSP_ERROR;
5386 gchar *str = gst_rtsp_strresult (res);
5388 if (res != GST_RTSP_EINTR) {
5389 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5390 ("Could not send message. (%s)", str));
5392 GST_WARNING_OBJECT (src, "send interrupted");
5399 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5400 ("Server did not select transport."));
5401 res = GST_RTSP_ERROR;
5404 nothing_to_activate:
5406 /* none of the available error codes is really right .. */
5407 if (unsupported_real) {
5408 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5409 (_("No supported stream was found. You might need to install a "
5410 "GStreamer RTSP extension plugin for Real media streams.")),
5413 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5414 (_("No supported stream was found. You might need to allow "
5415 "more transport protocols or may otherwise be missing "
5416 "the right GStreamer RTSP extension plugin.")), (NULL));
5418 return GST_RTSP_ERROR;
5422 gst_rtsp_message_unset (&request);
5423 gst_rtsp_message_unset (&response);
5429 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
5430 GstSegment * segment)
5433 GstRTSPTimeRange *therange;
5436 gst_rtsp_range_free (src->range);
5438 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
5439 GST_DEBUG_OBJECT (src, "parsed range %s", range);
5440 src->range = therange;
5442 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
5444 gst_segment_init (segment, GST_FORMAT_TIME);
5448 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
5449 therange->min.type, therange->min.seconds, therange->max.type,
5450 therange->max.seconds);
5452 if (therange->min.type == GST_RTSP_TIME_NOW)
5454 else if (therange->min.type == GST_RTSP_TIME_END)
5457 seconds = therange->min.seconds * GST_SECOND;
5459 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
5460 GST_TIME_ARGS (seconds));
5462 /* we need to start playback without clipping from the position reported by
5464 segment->start = seconds;
5465 segment->position = seconds;
5467 if (therange->max.type == GST_RTSP_TIME_NOW)
5469 else if (therange->max.type == GST_RTSP_TIME_END)
5472 seconds = therange->max.seconds * GST_SECOND;
5474 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
5475 GST_TIME_ARGS (seconds));
5477 /* live (WMS) server might send overflowed large max as its idea of infinity,
5478 * compensate to prevent problems later on */
5479 if (seconds != -1 && seconds < 0) {
5481 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
5484 /* live (WMS) might send min == max, which is not worth recording */
5485 if (segment->duration == -1 && seconds == segment->start)
5488 /* don't change duration with unknown value, we might have a valid value
5489 * there that we want to keep. */
5491 segment->duration = seconds;
5496 /* must be called with the RTSP state lock */
5497 static GstRTSPResult
5498 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
5504 /* prepare global stream caps properties */
5506 gst_structure_remove_all_fields (src->props);
5508 src->props = gst_structure_new_empty ("RTSPProperties");
5511 gst_sdp_message_dump (sdp);
5513 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
5515 gst_segment_init (&src->segment, GST_FORMAT_TIME);
5517 /* parse range for duration reporting. */
5522 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
5526 /* keep track of the range and configure it in the segment */
5527 if (gst_rtspsrc_parse_range (src, range, &src->segment))
5531 /* try to find a global control attribute. Note that a '*' means that we should
5532 * do aggregate control with the current url (so we don't do anything and
5533 * leave the current connection as is) */
5535 const gchar *control;
5538 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
5539 if (control == NULL)
5542 /* only take fully qualified urls */
5543 if (g_str_has_prefix (control, "rtsp://"))
5547 g_free (src->conninfo.location);
5548 src->conninfo.location = g_strdup (control);
5549 /* make a connection for this, if there was a connection already, nothing
5551 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
5552 GST_ERROR_OBJECT (src, "could not connect");
5555 /* we need to keep the control url separate from the connection url because
5556 * the rules for constructing the media control url need it */
5557 g_free (src->control);
5558 src->control = g_strdup (control);
5561 /* create streams */
5562 n_streams = gst_sdp_message_medias_len (sdp);
5563 for (i = 0; i < n_streams; i++) {
5564 gst_rtspsrc_create_stream (src, sdp, i);
5567 src->state = GST_RTSP_STATE_INIT;
5570 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
5573 /* reset our state */
5574 src->need_range = TRUE;
5577 src->state = GST_RTSP_STATE_READY;
5584 GST_ERROR_OBJECT (src, "setup failed");
5585 gst_rtspsrc_cleanup (src);
5590 static GstRTSPResult
5591 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
5595 GstRTSPMessage request = { 0 };
5596 GstRTSPMessage response = { 0 };
5599 gchar *respcont = NULL;
5602 src->need_redirect = FALSE;
5604 /* can't continue without a valid url */
5605 if (G_UNLIKELY (src->conninfo.url == NULL)) {
5606 res = GST_RTSP_EINVAL;
5609 src->tried_url_auth = FALSE;
5611 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
5612 goto connect_failed;
5614 /* create OPTIONS */
5615 GST_DEBUG_OBJECT (src, "create options...");
5617 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
5618 src->conninfo.url_str);
5620 goto create_request_failed;
5623 GST_DEBUG_OBJECT (src, "send options...");
5626 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
5629 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
5634 if (!gst_rtspsrc_parse_methods (src, &response))
5637 /* create DESCRIBE */
5638 GST_DEBUG_OBJECT (src, "create describe...");
5640 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
5641 src->conninfo.url_str);
5643 goto create_request_failed;
5645 /* we only accept SDP for now */
5646 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
5650 GST_DEBUG_OBJECT (src, "send describe...");
5653 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
5656 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
5660 /* we only perform redirect for the describe, currently */
5661 if (src->need_redirect) {
5662 /* close connection, we don't have to send a TEARDOWN yet, ignore the
5664 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5666 gst_rtsp_message_unset (&request);
5667 gst_rtsp_message_unset (&response);
5673 /* it could be that the DESCRIBE method was not implemented */
5674 if (!src->methods & GST_RTSP_DESCRIBE)
5677 /* check if reply is SDP */
5678 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
5680 /* could not be set but since the request returned OK, we assume it
5681 * was SDP, else check it. */
5683 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
5684 goto wrong_content_type;
5687 /* get message body and parse as SDP */
5688 gst_rtsp_message_get_body (&response, &data, &size);
5689 if (data == NULL || size == 0)
5692 GST_DEBUG_OBJECT (src, "parse SDP...");
5693 gst_sdp_message_new (sdp);
5694 gst_sdp_message_parse_buffer (data, size, *sdp);
5696 /* clean up any messages */
5697 gst_rtsp_message_unset (&request);
5698 gst_rtsp_message_unset (&response);
5705 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
5706 ("No valid RTSP URL was provided"));
5711 gchar *str = gst_rtsp_strresult (res);
5713 if (res != GST_RTSP_EINTR) {
5714 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
5715 ("Failed to connect. (%s)", str));
5717 GST_WARNING_OBJECT (src, "connect interrupted");
5722 create_request_failed:
5724 gchar *str = gst_rtsp_strresult (res);
5726 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5727 ("Could not create request. (%s)", str));
5733 /* Don't post a message - the rtsp_send method will have
5734 * taken care of it because we passed NULL for the response code */
5739 /* error was posted */
5740 res = GST_RTSP_ERROR;
5745 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5746 ("Server does not support SDP, got %s.", respcont));
5747 res = GST_RTSP_ERROR;
5752 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5753 ("Server can not provide an SDP."));
5754 res = GST_RTSP_ERROR;
5759 if (src->conninfo.connection) {
5760 GST_DEBUG_OBJECT (src, "free connection");
5761 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5763 gst_rtsp_message_unset (&request);
5764 gst_rtsp_message_unset (&response);
5769 static GstRTSPResult
5770 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
5775 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
5777 if (src->sdp == NULL) {
5778 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
5782 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
5787 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
5794 GST_WARNING_OBJECT (src, "can't get sdp");
5795 src->open_error = TRUE;
5800 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
5801 src->open_error = TRUE;
5806 static GstRTSPResult
5807 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
5809 GstRTSPMessage request = { 0 };
5810 GstRTSPMessage response = { 0 };
5811 GstRTSPResult res = GST_RTSP_OK;
5815 GST_DEBUG_OBJECT (src, "TEARDOWN...");
5817 if (src->state < GST_RTSP_STATE_READY) {
5818 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
5825 /* construct a control url */
5827 control = src->control;
5829 control = src->conninfo.url_str;
5831 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
5834 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5835 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5837 GstRTSPConnInfo *info;
5839 /* try aggregate control first but do non-aggregate control otherwise */
5841 setup_url = control;
5842 else if ((setup_url = stream->conninfo.location) == NULL)
5845 if (src->conninfo.connection) {
5846 info = &src->conninfo;
5847 } else if (stream->conninfo.connection) {
5848 info = &stream->conninfo;
5852 if (!info->connected)
5857 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
5859 goto create_request_failed;
5862 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
5865 gst_rtspsrc_send (src, info->connection, &request, &response,
5869 /* FIXME, parse result? */
5870 gst_rtsp_message_unset (&request);
5871 gst_rtsp_message_unset (&response);
5874 /* early exit when we did aggregate control */
5880 /* close connections */
5881 GST_DEBUG_OBJECT (src, "closing connection...");
5882 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5883 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5884 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5885 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
5889 gst_rtspsrc_cleanup (src);
5891 src->state = GST_RTSP_STATE_INVALID;
5894 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
5899 create_request_failed:
5901 gchar *str = gst_rtsp_strresult (res);
5903 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5904 ("Could not create request. (%s)", str));
5910 gchar *str = gst_rtsp_strresult (res);
5912 gst_rtsp_message_unset (&request);
5913 if (res != GST_RTSP_EINTR) {
5914 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5915 ("Could not send message. (%s)", str));
5917 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
5924 GST_DEBUG_OBJECT (src,
5925 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
5930 /* RTP-Info is of the format:
5932 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
5934 * rtptime corresponds to the timestamp for the NPT time given in the header
5935 * seqbase corresponds to the next sequence number we received. This number
5936 * indicates the first seqnum after the seek and should be used to discard
5937 * packets that are from before the seek.
5940 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
5945 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
5947 infos = g_strsplit (rtpinfo, ",", 0);
5948 for (i = 0; infos[i]; i++) {
5950 GstRTSPStream *stream;
5954 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
5956 /* init values, types of seqbase and timebase are bigger than needed so we
5957 * can store -1 as uninitialized values */
5962 /* parse url, find stream for url.
5963 * parse seq and rtptime. The seq number should be configured in the rtp
5964 * depayloader or session manager to detect gaps. Same for the rtptime, it
5965 * should be used to create an initial time newsegment. */
5966 fields = g_strsplit (infos[i], ";", 0);
5967 for (j = 0; fields[j]; j++) {
5968 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
5969 /* remove leading whitespace */
5970 fields[j] = g_strchug (fields[j]);
5971 if (g_str_has_prefix (fields[j], "url=")) {
5972 /* get the url and the stream */
5974 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
5975 } else if (g_str_has_prefix (fields[j], "seq=")) {
5976 seqbase = atoi (fields[j] + 4);
5977 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
5978 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
5981 g_strfreev (fields);
5982 /* now we need to store the values for the caps of the stream */
5983 if (stream != NULL) {
5984 GST_DEBUG_OBJECT (src,
5985 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
5986 stream, seqbase, timebase);
5988 /* we have a stream, configure detected params */
5989 stream->seqbase = seqbase;
5990 stream->timebase = timebase;
5999 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
6004 interval = strtoul (rtcp, NULL, 10);
6005 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
6010 interval *= GST_MSECOND;
6012 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6013 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6015 /* already (optionally) retrieved this when configuring manager */
6016 if (stream->session) {
6017 GObject *rtpsession = stream->session;
6019 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
6021 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
6025 /* now it happens that (Xenon) server sending this may also provide bogus
6026 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
6027 * and just use RTP-Info to sync */
6029 GObjectClass *klass;
6031 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
6032 if (g_object_class_find_property (klass, "rtcp-sync")) {
6033 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
6034 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
6040 gst_rtspsrc_get_float (const gchar * dstr)
6042 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6044 /* canonicalise floating point string so we can handle float strings
6045 * in the form "24.930" or "24,930" irrespective of the current locale */
6046 g_strlcpy (s, dstr, sizeof (s));
6047 g_strdelimit (s, ",", '.');
6048 return g_ascii_strtod (s, NULL);
6052 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
6054 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6056 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
6057 g_strlcpy (val_str, "now", sizeof (val_str));
6059 if (segment->position == 0) {
6060 g_strlcpy (val_str, "0", sizeof (val_str));
6062 g_ascii_dtostr (val_str, sizeof (val_str),
6063 ((gdouble) segment->position) / GST_SECOND);
6066 return g_strdup_printf ("npt=%s-", val_str);
6070 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
6072 stream->timebase = -1;
6073 stream->seqbase = -1;
6077 stream->caps = gst_caps_make_writable (stream->caps);
6078 s = gst_caps_get_structure (stream->caps, 0);
6079 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
6083 static GstRTSPResult
6084 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
6086 GstRTSPResult res = GST_RTSP_OK;
6088 if (src->state < GST_RTSP_STATE_READY) {
6089 res = GST_RTSP_ERROR;
6090 if (src->open_error) {
6091 GST_DEBUG_OBJECT (src, "the stream was in error");
6095 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
6097 if ((res = gst_rtspsrc_open (src, async)) < 0) {
6098 GST_DEBUG_OBJECT (src, "failed to open stream");
6107 static GstRTSPResult
6108 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
6110 GstRTSPMessage request = { 0 };
6111 GstRTSPMessage response = { 0 };
6112 GstRTSPResult res = GST_RTSP_OK;
6118 GST_DEBUG_OBJECT (src, "PLAY...");
6120 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6123 if (!(src->methods & GST_RTSP_PLAY))
6126 if (src->state == GST_RTSP_STATE_PLAYING)
6129 if (!src->conninfo.connection || !src->conninfo.connected)
6132 /* send some dummy packets before we activate the receive in the
6134 gst_rtspsrc_send_dummy_packets (src);
6136 /* activate receive elements;
6137 * only in async case, since receive elements may not have been affected
6138 * by overall state change (e.g. not around yet),
6139 * do not mess with state in sync case (e.g. seeking) */
6141 gst_element_set_state (GST_ELEMENT_CAST (src), GST_STATE_PLAYING);
6143 /* construct a control url */
6145 control = src->control;
6147 control = src->conninfo.url_str;
6149 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6150 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6152 GstRTSPConnection *conn;
6154 /* try aggregate control first but do non-aggregate control otherwise */
6156 setup_url = control;
6157 else if ((setup_url = stream->conninfo.location) == NULL)
6160 if (src->conninfo.connection) {
6161 conn = src->conninfo.connection;
6162 } else if (stream->conninfo.connection) {
6163 conn = stream->conninfo.connection;
6169 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
6171 goto create_request_failed;
6173 if (src->need_range) {
6174 hval = gen_range_header (src, segment);
6176 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_RANGE, hval);
6180 if (segment->rate != 1.0) {
6181 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
6183 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
6185 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
6187 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
6191 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
6193 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6196 /* seek may have silently failed as it is not supported */
6197 if (!(src->methods & GST_RTSP_PLAY)) {
6198 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
6199 /* obviously it is supported as we made it here */
6200 src->methods |= GST_RTSP_PLAY;
6201 src->seekable = FALSE;
6202 /* but there is nothing to parse in the response,
6203 * so convey we have no idea and not to expect anything particular */
6204 clear_rtp_base (src, stream);
6208 /* need to do for all streams */
6209 for (run = src->streams; run; run = g_list_next (run))
6210 clear_rtp_base (src, (GstRTSPStream *) run->data);
6212 /* NOTE the above also disables npt based eos detection */
6213 /* and below forces position to 0,
6214 * which is visible feedback we lost the plot */
6215 segment->start = segment->position = src->last_pos;
6218 gst_rtsp_message_unset (&request);
6220 /* parse RTP npt field. This is the current position in the stream (Normal
6221 * Play Time) and should be put in the NEWSEGMENT position field. */
6222 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
6224 gst_rtspsrc_parse_range (src, hval, segment);
6226 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
6227 segment->rate = 1.0;
6229 /* parse Speed header. This is the intended playback rate of the stream
6230 * and should be put in the NEWSEGMENT rate field. */
6231 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
6232 0) == GST_RTSP_OK) {
6233 segment->rate = gst_rtspsrc_get_float (hval);
6234 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
6235 &hval, 0) == GST_RTSP_OK) {
6236 segment->rate = gst_rtspsrc_get_float (hval);
6239 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
6240 * for the RTP packets. If this is not present, we assume all starts from 0...
6241 * This is info for the RTP session manager that we pass to it in caps. */
6243 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
6244 &hval, hval_idx++) == GST_RTSP_OK)
6245 gst_rtspsrc_parse_rtpinfo (src, hval);
6247 /* some servers indicate RTCP parameters in PLAY response,
6248 * rather than properly in SDP */
6249 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
6250 &hval, 0) == GST_RTSP_OK)
6251 gst_rtspsrc_handle_rtcp_interval (src, hval);
6253 gst_rtsp_message_unset (&response);
6255 /* early exit when we did aggregate control */
6259 /* configure the caps of the streams after we parsed all headers. Only reset
6260 * the manager object when we set a new Range header (we did a seek) */
6261 gst_rtspsrc_configure_caps (src, segment, src->need_range);
6263 /* set again when needed */
6264 src->need_range = FALSE;
6266 src->running = TRUE;
6267 src->base_time = -1;
6268 src->state = GST_RTSP_STATE_PLAYING;
6271 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
6272 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6273 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6274 stream->discont = TRUE;
6279 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
6286 GST_DEBUG_OBJECT (src, "failed to open stream");
6291 GST_DEBUG_OBJECT (src, "PLAY is not supported");
6296 GST_DEBUG_OBJECT (src, "we were already PLAYING");
6299 create_request_failed:
6301 gchar *str = gst_rtsp_strresult (res);
6303 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6304 ("Could not create request. (%s)", str));
6310 gchar *str = gst_rtsp_strresult (res);
6312 gst_rtsp_message_unset (&request);
6313 if (res != GST_RTSP_EINTR) {
6314 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6315 ("Could not send message. (%s)", str));
6317 GST_WARNING_OBJECT (src, "PLAY interrupted");
6324 static GstRTSPResult
6325 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
6327 GstRTSPResult res = GST_RTSP_OK;
6328 GstRTSPMessage request = { 0 };
6329 GstRTSPMessage response = { 0 };
6333 GST_DEBUG_OBJECT (src, "PAUSE...");
6335 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6338 if (!(src->methods & GST_RTSP_PAUSE))
6341 if (src->state == GST_RTSP_STATE_READY)
6344 if (!src->conninfo.connection || !src->conninfo.connected)
6347 /* construct a control url */
6349 control = src->control;
6351 control = src->conninfo.url_str;
6353 /* loop over the streams. We might exit the loop early when we could do an
6354 * aggregate control */
6355 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6356 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6357 GstRTSPConnection *conn;
6360 /* try aggregate control first but do non-aggregate control otherwise */
6362 setup_url = control;
6363 else if ((setup_url = stream->conninfo.location) == NULL)
6366 if (src->conninfo.connection) {
6367 conn = src->conninfo.connection;
6368 } else if (stream->conninfo.connection) {
6369 conn = stream->conninfo.connection;
6375 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
6376 ("Sending PAUSE request"));
6379 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
6381 goto create_request_failed;
6383 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6386 gst_rtsp_message_unset (&request);
6387 gst_rtsp_message_unset (&response);
6389 /* exit early when we did agregate control */
6395 src->state = GST_RTSP_STATE_READY;
6399 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
6406 GST_DEBUG_OBJECT (src, "failed to open stream");
6411 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
6416 GST_DEBUG_OBJECT (src, "we were already PAUSED");
6419 create_request_failed:
6421 gchar *str = gst_rtsp_strresult (res);
6423 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6424 ("Could not create request. (%s)", str));
6430 gchar *str = gst_rtsp_strresult (res);
6432 gst_rtsp_message_unset (&request);
6433 if (res != GST_RTSP_EINTR) {
6434 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6435 ("Could not send message. (%s)", str));
6437 GST_WARNING_OBJECT (src, "PAUSE interrupted");
6445 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
6447 GstRTSPSrc *rtspsrc;
6449 rtspsrc = GST_RTSPSRC (bin);
6451 switch (GST_MESSAGE_TYPE (message)) {
6452 case GST_MESSAGE_EOS:
6453 gst_message_unref (message);
6455 case GST_MESSAGE_ELEMENT:
6457 const GstStructure *s = gst_message_get_structure (message);
6459 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
6460 gboolean ignore_timeout;
6462 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
6464 GST_OBJECT_LOCK (rtspsrc);
6465 ignore_timeout = rtspsrc->ignore_timeout;
6466 rtspsrc->ignore_timeout = TRUE;
6467 GST_OBJECT_UNLOCK (rtspsrc);
6469 /* we only act on the first udp timeout message, others are irrelevant
6470 * and can be ignored. */
6471 if (!ignore_timeout)
6472 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
6474 gst_message_unref (message);
6477 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6480 case GST_MESSAGE_ERROR:
6483 GstRTSPStream *stream;
6486 udpsrc = GST_MESSAGE_SRC (message);
6488 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
6489 GST_ELEMENT_NAME (udpsrc));
6491 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
6495 /* we ignore the RTCP udpsrc */
6496 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
6499 /* if we get error messages from the udp sources, that's not a problem as
6500 * long as not all of them error out. We also don't really know what the
6501 * problem is, the message does not give enough detail... */
6502 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
6503 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
6504 if (ret != GST_FLOW_OK)
6508 gst_message_unref (message);
6512 /* fatal but not our message, forward */
6513 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6518 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6524 /* the thread where everything happens */
6526 gst_rtspsrc_thread (GstRTSPSrc * src)
6530 GST_OBJECT_LOCK (src);
6531 cmd = src->pending_cmd;
6532 if (cmd == CMD_PLAY || cmd == CMD_LOOP)
6533 src->pending_cmd = CMD_LOOP;
6535 src->pending_cmd = CMD_WAIT;
6536 GST_DEBUG_OBJECT (src, "got command %d", cmd);
6538 /* we got the message command, so ensure communication is possible again */
6539 gst_rtspsrc_connection_flush (src, FALSE);
6541 src->busy_cmd = cmd;
6542 GST_OBJECT_UNLOCK (src);
6546 gst_rtspsrc_open (src, TRUE);
6549 gst_rtspsrc_play (src, &src->segment, TRUE);
6552 gst_rtspsrc_pause (src, TRUE);
6555 gst_rtspsrc_close (src, TRUE, FALSE);
6558 gst_rtspsrc_loop (src);
6561 gst_rtspsrc_reconnect (src, FALSE);
6567 GST_OBJECT_LOCK (src);
6568 /* and go back to sleep */
6569 if (src->pending_cmd == CMD_WAIT) {
6571 gst_task_pause (src->task);
6574 src->busy_cmd = CMD_WAIT;
6575 GST_OBJECT_UNLOCK (src);
6579 gst_rtspsrc_start (GstRTSPSrc * src)
6581 GST_DEBUG_OBJECT (src, "starting");
6583 GST_OBJECT_LOCK (src);
6585 src->pending_cmd = CMD_WAIT;
6587 if (src->task == NULL) {
6588 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
6589 if (src->task == NULL)
6592 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
6594 GST_OBJECT_UNLOCK (src);
6601 GST_ERROR_OBJECT (src, "failed to create task");
6607 gst_rtspsrc_stop (GstRTSPSrc * src)
6611 GST_DEBUG_OBJECT (src, "stopping");
6613 /* also cancels pending task */
6614 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_CLOSE);
6616 GST_OBJECT_LOCK (src);
6617 if ((task = src->task)) {
6619 GST_OBJECT_UNLOCK (src);
6621 gst_task_stop (task);
6623 /* make sure it is not running */
6624 GST_RTSP_STREAM_LOCK (src);
6625 GST_RTSP_STREAM_UNLOCK (src);
6627 /* now wait for the task to finish */
6628 gst_task_join (task);
6630 /* and free the task */
6631 gst_object_unref (GST_OBJECT (task));
6633 GST_OBJECT_LOCK (src);
6635 GST_OBJECT_UNLOCK (src);
6637 /* ensure synchronously all is closed and clean */
6638 gst_rtspsrc_close (src, FALSE, TRUE);
6643 static GstStateChangeReturn
6644 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
6646 GstRTSPSrc *rtspsrc;
6647 GstStateChangeReturn ret;
6649 rtspsrc = GST_RTSPSRC (element);
6651 switch (transition) {
6652 case GST_STATE_CHANGE_NULL_TO_READY:
6653 if (!gst_rtspsrc_start (rtspsrc))
6656 case GST_STATE_CHANGE_READY_TO_PAUSED:
6657 /* init some state */
6658 rtspsrc->cur_protocols = rtspsrc->protocols;
6659 /* first attempt, don't ignore timeouts */
6660 rtspsrc->ignore_timeout = FALSE;
6661 rtspsrc->open_error = FALSE;
6662 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
6664 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
6665 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
6666 /* unblock the tcp tasks and make the loop waiting */
6667 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP);
6669 case GST_STATE_CHANGE_PAUSED_TO_READY:
6675 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
6676 if (ret == GST_STATE_CHANGE_FAILURE)
6679 switch (transition) {
6680 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
6681 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
6683 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
6684 /* send pause request and keep the idle task around */
6685 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
6686 ret = GST_STATE_CHANGE_NO_PREROLL;
6688 case GST_STATE_CHANGE_READY_TO_PAUSED:
6689 ret = GST_STATE_CHANGE_NO_PREROLL;
6691 case GST_STATE_CHANGE_PAUSED_TO_READY:
6692 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
6694 case GST_STATE_CHANGE_READY_TO_NULL:
6695 gst_rtspsrc_stop (rtspsrc);
6706 GST_DEBUG_OBJECT (rtspsrc, "start failed");
6707 return GST_STATE_CHANGE_FAILURE;
6712 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
6715 GstRTSPSrc *rtspsrc;
6717 rtspsrc = GST_RTSPSRC (element);
6719 if (GST_EVENT_IS_DOWNSTREAM (event)) {
6720 res = gst_rtspsrc_push_event (rtspsrc, event);
6722 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
6729 /*** GSTURIHANDLER INTERFACE *************************************************/
6732 gst_rtspsrc_uri_get_type (GType type)
6737 static const gchar *const *
6738 gst_rtspsrc_uri_get_protocols (GType type)
6740 static const gchar *protocols[] =
6741 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp", NULL };
6747 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
6749 GstRTSPSrc *src = GST_RTSPSRC (handler);
6751 /* FIXME: make thread-safe */
6752 return g_strdup (src->conninfo.location);
6756 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
6761 GstRTSPUrl *newurl = NULL;
6762 GstSDPMessage *sdp = NULL;
6764 src = GST_RTSPSRC (handler);
6766 /* same URI, we're fine */
6767 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
6770 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
6771 if ((res = gst_sdp_message_new (&sdp) < 0))
6774 GST_DEBUG_OBJECT (src, "parsing SDP message");
6775 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
6779 GST_DEBUG_OBJECT (src, "parsing URI");
6780 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
6784 /* if worked, free previous and store new url object along with the original
6786 GST_DEBUG_OBJECT (src, "configuring URI");
6787 g_free (src->conninfo.location);
6788 src->conninfo.location = g_strdup (uri);
6789 gst_rtsp_url_free (src->conninfo.url);
6790 src->conninfo.url = newurl;
6791 g_free (src->conninfo.url_str);
6793 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
6795 src->conninfo.url_str = NULL;
6798 gst_sdp_message_free (src->sdp);
6800 src->from_sdp = sdp != NULL;
6802 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
6803 GST_DEBUG_OBJECT (src, "request uri is: %s",
6804 GST_STR_NULL (src->conninfo.url_str));
6811 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
6816 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
6817 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
6818 "Could not create SDP");
6823 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
6824 GST_STR_NULL (uri));
6825 gst_sdp_message_free (sdp);
6826 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
6832 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
6833 GST_STR_NULL (uri), res);
6834 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
6835 "Invalid RTSP URI");
6841 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
6843 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
6845 iface->get_type = gst_rtspsrc_uri_get_type;
6846 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
6847 iface->get_uri = gst_rtspsrc_uri_get_uri;
6848 iface->set_uri = gst_rtspsrc_uri_set_uri;