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
182 #define DEFAULT_UDP_RECONNECT TRUE
194 PROP_DROP_ON_LATENCY,
195 PROP_CONNECTION_SPEED,
198 PROP_DO_RTSP_KEEP_ALIVE,
207 PROP_UDP_BUFFER_SIZE,
214 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
216 gst_rtsp_nat_method_get_type (void)
218 static GType rtsp_nat_method_type = 0;
219 static const GEnumValue rtsp_nat_method[] = {
220 {GST_RTSP_NAT_NONE, "None", "none"},
221 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
225 if (!rtsp_nat_method_type) {
226 rtsp_nat_method_type =
227 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
229 return rtsp_nat_method_type;
232 static void gst_rtspsrc_finalize (GObject * object);
234 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
235 const GValue * value, GParamSpec * pspec);
236 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
237 GValue * value, GParamSpec * pspec);
239 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
240 gpointer iface_data);
242 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
245 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
246 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
248 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
250 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
251 GstStateChange transition);
252 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
253 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
255 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
256 GstRTSPMessage * response);
258 static void gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask);
259 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
260 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
262 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
263 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
265 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
266 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
267 gboolean only_close);
269 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
270 const gchar * uri, GError ** error);
271 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
273 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
274 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
275 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
276 GstRTSPStream * stream, GstEvent * event);
277 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
279 /* commands we send to out loop to notify it of events */
280 #define CMD_OPEN (1 << 0)
281 #define CMD_PLAY (1 << 1)
282 #define CMD_PAUSE (1 << 2)
283 #define CMD_CLOSE (1 << 3)
284 #define CMD_WAIT (1 << 4)
285 #define CMD_RECONNECT (1 << 5)
286 #define CMD_LOOP (1 << 6)
288 /* mask for all commands */
289 #define CMD_ALL ((CMD_LOOP << 1) - 1)
291 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
293 gchar *__txt = _gst_element_error_printf text; \
294 gst_element_post_message (GST_ELEMENT_CAST (el), \
295 gst_message_new_progress (GST_OBJECT_CAST (el), \
296 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
300 /*static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 }; */
301 #define gst_rtspsrc_parent_class parent_class
302 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
303 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
306 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
308 GObjectClass *gobject_class;
309 GstElementClass *gstelement_class;
310 GstBinClass *gstbin_class;
312 gobject_class = (GObjectClass *) klass;
313 gstelement_class = (GstElementClass *) klass;
314 gstbin_class = (GstBinClass *) klass;
316 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
318 gobject_class->set_property = gst_rtspsrc_set_property;
319 gobject_class->get_property = gst_rtspsrc_get_property;
321 gobject_class->finalize = gst_rtspsrc_finalize;
323 g_object_class_install_property (gobject_class, PROP_LOCATION,
324 g_param_spec_string ("location", "RTSP Location",
325 "Location of the RTSP url to read",
326 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
328 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
329 g_param_spec_flags ("protocols", "Protocols",
330 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
331 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
333 g_object_class_install_property (gobject_class, PROP_DEBUG,
334 g_param_spec_boolean ("debug", "Debug",
335 "Dump request and response messages to stdout",
336 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
338 g_object_class_install_property (gobject_class, PROP_RETRY,
339 g_param_spec_uint ("retry", "Retry",
340 "Max number of retries when allocating RTP ports.",
341 0, G_MAXUINT16, DEFAULT_RETRY,
342 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
344 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
345 g_param_spec_uint64 ("timeout", "Timeout",
346 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
347 0, G_MAXUINT64, DEFAULT_TIMEOUT,
348 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
350 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
351 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
352 "Fail after timeout microseconds on TCP connections (0 = disabled)",
353 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
354 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
356 g_object_class_install_property (gobject_class, PROP_LATENCY,
357 g_param_spec_uint ("latency", "Buffer latency in ms",
358 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
359 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
361 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
362 g_param_spec_boolean ("drop-on-latency",
363 "Drop buffers when maximum latency is reached",
364 "Tells the jitterbuffer to never exceed the given latency in size",
365 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
367 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
368 g_param_spec_uint64 ("connection-speed", "Connection Speed",
369 "Network connection speed in kbps (0 = unknown)",
370 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
371 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
373 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
374 g_param_spec_enum ("nat-method", "NAT Method",
375 "Method to use for traversing firewalls and NAT",
376 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
377 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
380 * GstRTSPSrc::do-rtcp
382 * Enable RTCP support. Some old server don't like RTCP and then this property
383 * needs to be set to FALSE.
387 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
388 g_param_spec_boolean ("do-rtcp", "Do RTCP",
389 "Send RTCP packets, disable for old incompatible server.",
390 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
393 * GstRTSPSrc::do-rtsp-keep-alive
395 * Enable RTSP keep laive support. Some old server don't like RTSP
396 * keep alive and then this property needs to be set to FALSE.
400 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
401 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
402 "Send RTSP keep alive packets, disable for old incompatible server.",
403 DEFAULT_DO_RTSP_KEEP_ALIVE,
404 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
409 * Set the proxy parameters. This has to be a string of the format
410 * [http://][user:passwd@]host[:port].
414 g_object_class_install_property (gobject_class, PROP_PROXY,
415 g_param_spec_string ("proxy", "Proxy",
416 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
417 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
419 * GstRTSPSrc::proxy-id
421 * Sets the proxy URI user id for authentication. If the URI set via the
422 * "proxy" property contains a user-id already, that will take precedence.
426 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
427 g_param_spec_string ("proxy-id", "proxy-id",
428 "HTTP proxy URI user id for authentication", "",
429 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
431 * GstRTSPSrc::proxy-pw
433 * Sets the proxy URI password for authentication. If the URI set via the
434 * "proxy" property contains a password already, that will take precedence.
438 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
439 g_param_spec_string ("proxy-pw", "proxy-pw",
440 "HTTP proxy URI user password for authentication", "",
441 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
444 * GstRTSPSrc::rtp_blocksize
446 * RTP package size to suggest to server.
450 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
451 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
452 "RTP package size to suggest to server (0 = disabled)",
453 0, 65536, DEFAULT_RTP_BLOCKSIZE,
454 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
456 g_object_class_install_property (gobject_class,
458 g_param_spec_string ("user-id", "user-id",
459 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
460 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
461 g_object_class_install_property (gobject_class, PROP_USER_PW,
462 g_param_spec_string ("user-pw", "user-pw",
463 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
464 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
467 * GstRTSPSrc::buffer-mode:
469 * Control the buffering and timestamping mode used by the jitterbuffer.
473 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
474 g_param_spec_enum ("buffer-mode", "Buffer Mode",
475 "Control the buffering algorithm in use",
476 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
477 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
480 * GstRTSPSrc::port-range:
482 * Configure the client port numbers that can be used to recieve RTP and
487 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
488 g_param_spec_string ("port-range", "Port range",
489 "Client port range that can be used to receive RTP and RTCP data, "
490 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
491 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
494 * GstRTSPSrc::udp-buffer-size:
496 * Size of the kernel UDP receive buffer in bytes.
500 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
501 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
502 "Size of the kernel UDP receive buffer in bytes, 0=default",
503 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
504 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
507 * GstRTSPSrc::short-header:
509 * Only send the basic RTSP headers for broken encoders.
513 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
514 g_param_spec_boolean ("short-header", "Short Header",
515 "Only send the basic RTSP headers for broken encoders",
516 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
518 g_object_class_install_property (gobject_class, PROP_PROBATION,
519 g_param_spec_uint ("probation", "Number of probations",
520 "Consecutive packet sequence numbers to accept the source",
521 0, G_MAXUINT, DEFAULT_PROBATION,
522 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
524 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
525 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
526 "Reconnect to the server if RTSP connection is closed when doing UDP",
527 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
529 gstelement_class->send_event = gst_rtspsrc_send_event;
530 gstelement_class->change_state = gst_rtspsrc_change_state;
532 gst_element_class_add_pad_template (gstelement_class,
533 gst_static_pad_template_get (&rtptemplate));
535 gst_element_class_set_static_metadata (gstelement_class,
536 "RTSP packet receiver", "Source/Network",
537 "Receive data over the network via RTSP (RFC 2326)",
538 "Wim Taymans <wim@fluendo.com>, "
539 "Thijs Vermeir <thijs.vermeir@barco.com>, "
540 "Lutz Mueller <lutz@topfrose.de>");
542 gstbin_class->handle_message = gst_rtspsrc_handle_message;
544 gst_rtsp_ext_list_init ();
549 gst_rtspsrc_init (GstRTSPSrc * src)
551 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
552 src->protocols = DEFAULT_PROTOCOLS;
553 src->debug = DEFAULT_DEBUG;
554 src->retry = DEFAULT_RETRY;
555 src->udp_timeout = DEFAULT_TIMEOUT;
556 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
557 src->latency = DEFAULT_LATENCY_MS;
558 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
559 src->connection_speed = DEFAULT_CONNECTION_SPEED;
560 src->nat_method = DEFAULT_NAT_METHOD;
561 src->do_rtcp = DEFAULT_DO_RTCP;
562 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
563 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
564 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
565 src->user_id = g_strdup (DEFAULT_USER_ID);
566 src->user_pw = g_strdup (DEFAULT_USER_PW);
567 src->buffer_mode = DEFAULT_BUFFER_MODE;
568 src->client_port_range.min = 0;
569 src->client_port_range.max = 0;
570 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
571 src->short_header = DEFAULT_SHORT_HEADER;
572 src->probation = DEFAULT_PROBATION;
573 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
575 /* get a list of all extensions */
576 src->extensions = gst_rtsp_ext_list_get ();
578 /* connect to send signal */
579 gst_rtsp_ext_list_connect (src->extensions, "send",
580 (GCallback) gst_rtspsrc_send_cb, src);
582 /* protects the streaming thread in interleaved mode or the polling
583 * thread in UDP mode. */
584 g_rec_mutex_init (&src->stream_rec_lock);
586 /* protects our state changes from multiple invocations */
587 g_rec_mutex_init (&src->state_rec_lock);
589 src->state = GST_RTSP_STATE_INVALID;
591 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
595 gst_rtspsrc_finalize (GObject * object)
599 rtspsrc = GST_RTSPSRC (object);
601 gst_rtsp_ext_list_free (rtspsrc->extensions);
602 g_free (rtspsrc->conninfo.location);
603 gst_rtsp_url_free (rtspsrc->conninfo.url);
604 g_free (rtspsrc->conninfo.url_str);
605 g_free (rtspsrc->user_id);
606 g_free (rtspsrc->user_pw);
609 gst_sdp_message_free (rtspsrc->sdp);
614 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
615 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
617 G_OBJECT_CLASS (parent_class)->finalize (object);
620 /* a proxy string of the format [user:passwd@]host[:port] */
622 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
626 g_free (rtsp->proxy_user);
627 rtsp->proxy_user = NULL;
628 g_free (rtsp->proxy_passwd);
629 rtsp->proxy_passwd = NULL;
630 g_free (rtsp->proxy_host);
631 rtsp->proxy_host = NULL;
632 rtsp->proxy_port = 0;
639 /* we allow http:// in front but ignore it */
640 if (g_str_has_prefix (p, "http://"))
643 at = strchr (p, '@');
645 /* look for user:passwd */
646 col = strchr (proxy, ':');
647 if (col == NULL || col > at)
650 rtsp->proxy_user = g_strndup (p, col - p);
652 rtsp->proxy_passwd = g_strndup (col, at - col);
657 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
658 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
659 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
660 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
661 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
662 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
663 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
666 col = strchr (p, ':');
669 /* everything before the colon is the hostname */
670 rtsp->proxy_host = g_strndup (p, col - p);
672 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
674 rtsp->proxy_host = g_strdup (p);
675 rtsp->proxy_port = 8080;
681 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
683 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
684 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
687 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
689 rtspsrc->ptcp_timeout = NULL;
693 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
698 rtspsrc = GST_RTSPSRC (object);
702 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
703 g_value_get_string (value), NULL);
706 rtspsrc->protocols = g_value_get_flags (value);
709 rtspsrc->debug = g_value_get_boolean (value);
712 rtspsrc->retry = g_value_get_uint (value);
715 rtspsrc->udp_timeout = g_value_get_uint64 (value);
717 case PROP_TCP_TIMEOUT:
718 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
721 rtspsrc->latency = g_value_get_uint (value);
723 case PROP_DROP_ON_LATENCY:
724 rtspsrc->drop_on_latency = g_value_get_boolean (value);
726 case PROP_CONNECTION_SPEED:
727 rtspsrc->connection_speed = g_value_get_uint64 (value);
729 case PROP_NAT_METHOD:
730 rtspsrc->nat_method = g_value_get_enum (value);
733 rtspsrc->do_rtcp = g_value_get_boolean (value);
735 case PROP_DO_RTSP_KEEP_ALIVE:
736 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
739 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
742 if (rtspsrc->prop_proxy_id)
743 g_free (rtspsrc->prop_proxy_id);
744 rtspsrc->prop_proxy_id = g_value_dup_string (value);
747 if (rtspsrc->prop_proxy_pw)
748 g_free (rtspsrc->prop_proxy_pw);
749 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
751 case PROP_RTP_BLOCKSIZE:
752 rtspsrc->rtp_blocksize = g_value_get_uint (value);
755 if (rtspsrc->user_id)
756 g_free (rtspsrc->user_id);
757 rtspsrc->user_id = g_value_dup_string (value);
760 if (rtspsrc->user_pw)
761 g_free (rtspsrc->user_pw);
762 rtspsrc->user_pw = g_value_dup_string (value);
764 case PROP_BUFFER_MODE:
765 rtspsrc->buffer_mode = g_value_get_enum (value);
767 case PROP_PORT_RANGE:
771 str = g_value_get_string (value);
773 sscanf (str, "%u-%u",
774 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
776 rtspsrc->client_port_range.min = 0;
777 rtspsrc->client_port_range.max = 0;
781 case PROP_UDP_BUFFER_SIZE:
782 rtspsrc->udp_buffer_size = g_value_get_int (value);
784 case PROP_SHORT_HEADER:
785 rtspsrc->short_header = g_value_get_boolean (value);
788 rtspsrc->probation = g_value_get_uint (value);
790 case PROP_UDP_RECONNECT:
791 rtspsrc->udp_reconnect = g_value_get_boolean (value);
794 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
800 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
805 rtspsrc = GST_RTSPSRC (object);
809 g_value_set_string (value, rtspsrc->conninfo.location);
812 g_value_set_flags (value, rtspsrc->protocols);
815 g_value_set_boolean (value, rtspsrc->debug);
818 g_value_set_uint (value, rtspsrc->retry);
821 g_value_set_uint64 (value, rtspsrc->udp_timeout);
823 case PROP_TCP_TIMEOUT:
827 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
828 rtspsrc->tcp_timeout.tv_usec;
829 g_value_set_uint64 (value, timeout);
833 g_value_set_uint (value, rtspsrc->latency);
835 case PROP_DROP_ON_LATENCY:
836 g_value_set_boolean (value, rtspsrc->drop_on_latency);
838 case PROP_CONNECTION_SPEED:
839 g_value_set_uint64 (value, rtspsrc->connection_speed);
841 case PROP_NAT_METHOD:
842 g_value_set_enum (value, rtspsrc->nat_method);
845 g_value_set_boolean (value, rtspsrc->do_rtcp);
847 case PROP_DO_RTSP_KEEP_ALIVE:
848 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
854 if (rtspsrc->proxy_host) {
856 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
860 g_value_take_string (value, str);
864 g_value_set_string (value, rtspsrc->prop_proxy_id);
867 g_value_set_string (value, rtspsrc->prop_proxy_pw);
869 case PROP_RTP_BLOCKSIZE:
870 g_value_set_uint (value, rtspsrc->rtp_blocksize);
873 g_value_set_string (value, rtspsrc->user_id);
876 g_value_set_string (value, rtspsrc->user_pw);
878 case PROP_BUFFER_MODE:
879 g_value_set_enum (value, rtspsrc->buffer_mode);
881 case PROP_PORT_RANGE:
885 if (rtspsrc->client_port_range.min != 0) {
886 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
887 rtspsrc->client_port_range.max);
891 g_value_take_string (value, str);
894 case PROP_UDP_BUFFER_SIZE:
895 g_value_set_int (value, rtspsrc->udp_buffer_size);
897 case PROP_SHORT_HEADER:
898 g_value_set_boolean (value, rtspsrc->short_header);
901 g_value_set_uint (value, rtspsrc->probation);
903 case PROP_UDP_RECONNECT:
904 g_value_set_boolean (value, rtspsrc->udp_reconnect);
907 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
913 find_stream_by_id (GstRTSPStream * stream, gint * id)
915 if (stream->id == *id)
922 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
924 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
931 find_stream_by_pt (GstRTSPStream * stream, gint * pt)
933 if (stream->pt == *pt)
940 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
942 GstElement *src = (GstElement *) a;
944 if (stream->udpsrc[0] == src)
946 if (stream->udpsrc[1] == src)
953 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
955 /* check qualified setup_url */
956 if (!strcmp (stream->conninfo.location, (gchar *) a))
958 /* check original control_url */
959 if (!strcmp (stream->control_url, (gchar *) a))
962 /* check if qualified setup_url ends with string */
963 if (g_str_has_suffix (stream->control_url, (gchar *) a))
969 static GstRTSPStream *
970 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
974 /* find and get stream */
975 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
976 return (GstRTSPStream *) lstream->data;
981 static const GstSDPBandwidth *
982 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
983 const GstSDPMedia * media, const gchar * type)
987 /* first look in the media specific section */
988 len = gst_sdp_media_bandwidths_len (media);
989 for (i = 0; i < len; i++) {
990 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
992 if (strcmp (bw->bwtype, type) == 0)
995 /* then look in the message specific section */
996 len = gst_sdp_message_bandwidths_len (sdp);
997 for (i = 0; i < len; i++) {
998 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1000 if (strcmp (bw->bwtype, type) == 0)
1007 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1008 const GstSDPMedia * media, GstRTSPStream * stream)
1010 const GstSDPBandwidth *bw;
1012 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1013 stream->as_bandwidth = bw->bandwidth;
1015 stream->as_bandwidth = -1;
1017 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1018 stream->rr_bandwidth = bw->bandwidth;
1020 stream->rr_bandwidth = -1;
1022 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1023 stream->rs_bandwidth = bw->bandwidth;
1025 stream->rs_bandwidth = -1;
1029 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1030 const GstSDPConnection * conn)
1032 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1035 if (conn->addrtype == NULL)
1038 /* check for IPV6 */
1039 if (strcmp (conn->addrtype, "IP4") == 0)
1040 stream->is_ipv6 = FALSE;
1041 else if (strcmp (conn->addrtype, "IP6") == 0)
1042 stream->is_ipv6 = TRUE;
1047 g_free (stream->destination);
1048 stream->destination = g_strdup (conn->address);
1050 /* check for multicast */
1051 stream->is_multicast =
1052 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1054 stream->ttl = conn->ttl;
1057 /* Go over the connections for a stream.
1058 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1060 * - If we are dealing with a localhost address, we disable multicast
1063 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1064 const GstSDPMedia * media, GstRTSPStream * stream)
1066 const GstSDPConnection *conn;
1069 /* first look in the media specific section */
1070 len = gst_sdp_media_connections_len (media);
1071 for (i = 0; i < len; i++) {
1072 conn = gst_sdp_media_get_connection (media, i);
1074 gst_rtspsrc_do_stream_connection (src, stream, conn);
1076 /* then look in the message specific section */
1077 if ((conn = gst_sdp_message_get_connection (sdp))) {
1078 gst_rtspsrc_do_stream_connection (src, stream, conn);
1082 static GstRTSPStream *
1083 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1085 GstRTSPStream *stream;
1086 const gchar *control_url;
1087 const gchar *payload;
1088 const GstSDPMedia *media;
1090 /* get media, should not return NULL */
1091 media = gst_sdp_message_get_media (sdp, idx);
1095 stream = g_new0 (GstRTSPStream, 1);
1096 stream->parent = src;
1097 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1099 stream->last_ret = GST_FLOW_NOT_LINKED;
1100 stream->added = FALSE;
1101 stream->disabled = FALSE;
1102 stream->id = src->numstreams++;
1103 stream->eos = FALSE;
1104 stream->discont = TRUE;
1105 stream->seqbase = -1;
1106 stream->timebase = -1;
1108 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1109 * session manager to scale RTCP. */
1110 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1112 /* collect connection info */
1113 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1115 /* we must have a payload. No payload means we cannot create caps */
1116 /* FIXME, handle multiple formats. The problem here is that we just want to
1117 * take the first available format that we can handle but in order to do that
1118 * we need to scan for depayloader plugins. Scanning for payloader plugins is
1119 * also suboptimal because the user maybe just wants to save the raw stream
1120 * and then we don't care. */
1121 if ((payload = gst_sdp_media_get_format (media, 0))) {
1122 stream->pt = atoi (payload);
1124 stream->caps = gst_rtspsrc_media_to_caps (stream->pt, media);
1126 GST_DEBUG ("mapping sdp session level attributes to caps");
1127 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, stream->caps);
1128 GST_DEBUG ("mapping sdp media level attributes to caps");
1129 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, stream->caps);
1131 if (stream->pt >= 96) {
1132 /* If we have a dynamic payload type, see if we have a stream with the
1133 * same payload number. If there is one, they are part of the same
1134 * container and we only need to add one pad. */
1135 if (find_stream (src, &stream->pt, (gpointer) find_stream_by_pt)) {
1136 stream->container = TRUE;
1137 GST_DEBUG ("found another stream with pt %d, marking as container",
1142 /* collect port number */
1143 stream->port = gst_sdp_media_get_port (media);
1145 /* get control url to construct the setup url. The setup url is used to
1146 * configure the transport of the stream and is used to identity the stream in
1147 * the RTP-Info header field returned from PLAY. */
1148 control_url = gst_sdp_media_get_attribute_val (media, "control");
1149 if (control_url == NULL)
1150 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1152 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1153 GST_DEBUG_OBJECT (src, " pt: %d", stream->pt);
1154 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1155 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1156 GST_DEBUG_OBJECT (src, " caps: %" GST_PTR_FORMAT, stream->caps);
1157 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1159 if (control_url != NULL) {
1160 stream->control_url = g_strdup (control_url);
1161 /* Build a fully qualified url using the content_base if any or by prefixing
1162 * the original request.
1163 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1164 * likely build a URL that the server will fail to understand, this is ok,
1165 * we will fail then. */
1166 if (g_str_has_prefix (control_url, "rtsp://"))
1167 stream->conninfo.location = g_strdup (control_url);
1172 if (g_strcmp0 (control_url, "*") == 0)
1176 base = src->control;
1177 else if (src->content_base)
1178 base = src->content_base;
1179 else if (src->conninfo.url_str)
1180 base = src->conninfo.url_str;
1184 /* check if the base ends or control starts with / */
1185 has_slash = g_str_has_prefix (control_url, "/");
1186 has_slash = has_slash || g_str_has_suffix (base, "/");
1188 /* concatenate the two strings, insert / when not present */
1189 stream->conninfo.location =
1190 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1193 GST_DEBUG_OBJECT (src, " setup: %s",
1194 GST_STR_NULL (stream->conninfo.location));
1196 /* we keep track of all streams */
1197 src->streams = g_list_append (src->streams, stream);
1205 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1209 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1212 gst_caps_unref (stream->caps);
1214 g_free (stream->destination);
1215 g_free (stream->control_url);
1216 g_free (stream->conninfo.location);
1218 for (i = 0; i < 2; i++) {
1219 if (stream->udpsrc[i]) {
1220 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1221 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1222 gst_object_unref (stream->udpsrc[i]);
1223 stream->udpsrc[i] = NULL;
1225 if (stream->channelpad[i]) {
1226 gst_object_unref (stream->channelpad[i]);
1227 stream->channelpad[i] = NULL;
1229 if (stream->udpsink[i]) {
1230 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1231 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1232 gst_object_unref (stream->udpsink[i]);
1233 stream->udpsink[i] = NULL;
1236 if (stream->fakesrc) {
1237 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1238 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1239 gst_object_unref (stream->fakesrc);
1240 stream->fakesrc = NULL;
1242 if (stream->srcpad) {
1243 gst_pad_set_active (stream->srcpad, FALSE);
1244 if (stream->added) {
1245 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1246 stream->added = FALSE;
1248 stream->srcpad = NULL;
1250 if (stream->rtcppad) {
1251 gst_object_unref (stream->rtcppad);
1252 stream->rtcppad = NULL;
1254 if (stream->session) {
1255 g_object_unref (stream->session);
1256 stream->session = NULL;
1262 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1266 GST_DEBUG_OBJECT (src, "cleanup");
1268 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1269 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1271 gst_rtspsrc_stream_free (src, stream);
1273 g_list_free (src->streams);
1274 src->streams = NULL;
1276 if (src->manager_sig_id) {
1277 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1278 src->manager_sig_id = 0;
1280 gst_element_set_state (src->manager, GST_STATE_NULL);
1281 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1282 src->manager = NULL;
1284 src->numstreams = 0;
1286 gst_structure_free (src->props);
1289 g_free (src->content_base);
1290 src->content_base = NULL;
1292 g_free (src->control);
1293 src->control = NULL;
1296 gst_rtsp_range_free (src->range);
1299 /* don't clear the SDP when it was used in the url */
1300 if (src->sdp && !src->from_sdp) {
1301 gst_sdp_message_free (src->sdp);
1304 if (src->start_segment) {
1305 gst_event_unref (src->start_segment);
1306 src->start_segment = NULL;
1310 #define PARSE_INT(p, del, res) \
1313 p = strstr (p, del); \
1323 #define PARSE_STRING(p, del, res) \
1326 p = strstr (p, del); \
1338 #define SKIP_SPACES(p) \
1339 while (*p && g_ascii_isspace (*p)) \
1344 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1347 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1348 gint * rate, gchar ** params)
1352 p = (gchar *) rtpmap;
1354 PARSE_INT (p, " ", *payload);
1362 PARSE_STRING (p, "/", *name);
1363 if (*name == NULL) {
1364 GST_DEBUG ("no rate, name %s", p);
1365 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1366 * streams seem to omit the rate. */
1373 p = strstr (p, "/");
1391 * Mapping SDP attributes to caps
1393 * prepend 'a-' to IANA registered sdp attributes names
1394 * (ie: not prefixed with 'x-') in order to avoid
1395 * collision with gstreamer standard caps properties names
1398 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1400 if (attributes->len > 0) {
1404 s = gst_caps_get_structure (caps, 0);
1406 for (i = 0; i < attributes->len; i++) {
1407 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1408 gchar *tofree, *key;
1412 /* skip some of the attribute we already handle */
1413 if (!strcmp (key, "fmtp"))
1415 if (!strcmp (key, "rtpmap"))
1417 if (!strcmp (key, "control"))
1419 if (!strcmp (key, "range"))
1422 /* string must be valid UTF8 */
1423 if (!g_utf8_validate (attr->value, -1, NULL))
1426 if (!g_str_has_prefix (key, "x-"))
1427 tofree = key = g_strdup_printf ("a-%s", key);
1431 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1432 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1439 * Mapping of caps to and from SDP fields:
1441 * m=<media> <UDP port> RTP/AVP <payload>
1442 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1443 * a=fmtp:<payload> <param>[=<value>];...
1446 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1449 const gchar *rtpmap;
1453 gchar *params = NULL;
1459 /* get and parse rtpmap */
1460 if ((rtpmap = gst_sdp_media_get_attribute_val (media, "rtpmap"))) {
1461 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
1463 if (payload != pt) {
1464 /* we ignore the rtpmap if the payload type is different. */
1465 g_warning ("rtpmap of wrong payload type, ignoring");
1471 /* if we failed to parse the rtpmap for a dynamic payload type, we have an
1475 /* else we can ignore */
1476 g_warning ("error parsing rtpmap, ignoring");
1479 /* dynamic payloads need rtpmap or we fail */
1483 /* check if we have a rate, if not, we need to look up the rate from the
1484 * default rates based on the payload types. */
1486 const GstRTPPayloadInfo *info;
1488 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
1489 /* dynamic types, use media and encoding_name */
1490 tmp = g_ascii_strdown (media->media, -1);
1491 info = gst_rtp_payload_info_for_name (tmp, name);
1494 /* static types, use payload type */
1495 info = gst_rtp_payload_info_for_pt (pt);
1499 if ((rate = info->clock_rate) == 0)
1502 /* we fail if we cannot find one */
1507 tmp = g_ascii_strdown (media->media, -1);
1508 caps = gst_caps_new_simple ("application/x-unknown",
1509 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
1511 s = gst_caps_get_structure (caps, 0);
1513 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
1515 /* encoding name must be upper case */
1517 tmp = g_ascii_strup (name, -1);
1518 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
1522 /* params must be lower case */
1523 if (params != NULL) {
1524 tmp = g_ascii_strdown (params, -1);
1525 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
1529 /* parse optional fmtp: field */
1530 if ((fmtp = gst_sdp_media_get_attribute_val (media, "fmtp"))) {
1536 /* p is now of the format <payload> <param>[=<value>];... */
1537 PARSE_INT (p, " ", payload);
1538 if (payload != -1 && payload == pt) {
1542 /* <param>[=<value>] are separated with ';' */
1543 pairs = g_strsplit (p, ";", 0);
1544 for (i = 0; pairs[i]; i++) {
1546 const gchar *val, *key;
1548 /* the key may not have a '=', the value can have other '='s */
1549 valpos = strstr (pairs[i], "=");
1551 /* we have a '=' and thus a value, remove the '=' with \0 */
1553 /* value is everything between '=' and ';'. We split the pairs at ;
1554 * boundaries so we can take the remainder of the value. Some servers
1555 * put spaces around the value which we strip off here. Alternatively
1556 * we could strip those spaces in the depayloaders should these spaces
1557 * actually carry any meaning in the future. */
1558 val = g_strstrip (valpos + 1);
1560 /* simple <param>;.. is translated into <param>=1;... */
1563 /* strip the key of spaces, convert key to lowercase but not the value. */
1564 key = g_strstrip (pairs[i]);
1565 if (strlen (key) > 1) {
1566 tmp = g_ascii_strdown (key, -1);
1567 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
1579 g_warning ("rtpmap type not given for dynamic payload %d", pt);
1584 g_warning ("rate unknown for payload type %d", pt);
1590 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1591 gint * rtpport, gint * rtcpport)
1594 GstStateChangeReturn ret;
1595 GstElement *udpsrc0, *udpsrc1;
1596 gint tmp_rtp, tmp_rtcp;
1600 src = stream->parent;
1606 /* Start at next port */
1607 tmp_rtp = src->next_port_num;
1609 if (stream->is_ipv6)
1610 host = "udp://[::0]";
1612 host = "udp://0.0.0.0";
1614 /* try to allocate 2 UDP ports, the RTP port should be an even
1615 * number and the RTCP port should be the next (uneven) port */
1618 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1619 tmp_rtp >= src->client_port_range.max)
1622 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1623 if (udpsrc0 == NULL)
1624 goto no_udp_protocol;
1625 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1627 if (src->udp_buffer_size != 0)
1628 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
1631 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
1632 if (ret == GST_STATE_CHANGE_FAILURE) {
1634 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
1637 if (++count > src->retry)
1640 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1641 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1642 gst_object_unref (udpsrc0);
1645 GST_DEBUG_OBJECT (src, "retry %d", count);
1648 goto no_udp_protocol;
1651 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
1652 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
1654 /* check if port is even */
1655 if ((tmp_rtp & 0x01) != 0) {
1656 /* port not even, close and allocate another */
1657 if (++count > src->retry)
1660 GST_DEBUG_OBJECT (src, "RTP port not even");
1662 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1663 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1664 gst_object_unref (udpsrc0);
1667 GST_DEBUG_OBJECT (src, "retry %d", count);
1672 /* allocate port+1 for RTCP now */
1673 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1674 if (udpsrc1 == NULL)
1675 goto no_udp_rtcp_protocol;
1678 tmp_rtcp = tmp_rtp + 1;
1679 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
1682 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
1684 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
1685 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
1686 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
1687 if (ret == GST_STATE_CHANGE_FAILURE) {
1688 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
1690 if (++count > src->retry)
1693 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1694 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1695 gst_object_unref (udpsrc0);
1698 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
1699 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1700 gst_object_unref (udpsrc1);
1704 GST_DEBUG_OBJECT (src, "retry %d", count);
1708 /* all fine, do port check */
1709 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
1710 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
1712 /* this should not happen... */
1713 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
1716 /* we keep these elements, we configure all in configure_transport when the
1717 * server told us to really use the UDP ports. */
1718 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
1719 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
1721 /* keep track of next available port number when we have a range
1723 if (src->next_port_num != 0)
1724 src->next_port_num = tmp_rtcp + 1;
1731 GST_DEBUG_OBJECT (src, "could not get UDP source");
1736 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
1740 no_udp_rtcp_protocol:
1742 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
1747 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
1748 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
1754 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1755 gst_object_unref (udpsrc0);
1758 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1759 gst_object_unref (udpsrc1);
1766 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
1774 event = gst_event_new_flush_start ();
1775 GST_DEBUG_OBJECT (src, "start flush");
1777 state = GST_STATE_PAUSED;
1779 event = gst_event_new_flush_stop (FALSE);
1780 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
1783 state = GST_STATE_PLAYING;
1785 state = GST_STATE_PAUSED;
1787 gst_rtspsrc_push_event (src, event);
1788 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
1790 /* to manage jitterbuffer buffer mode */
1792 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
1794 /* make running time start start at 0 again */
1795 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1796 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1798 for (i = 0; i < 2; i++) {
1800 if (stream->udpsrc[i]) {
1801 gst_element_set_state (stream->udpsrc[i], state);
1807 static GstRTSPResult
1808 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
1809 GstRTSPMessage * message, GTimeVal * timeout)
1814 ret = gst_rtsp_connection_send (conn, message, timeout);
1816 ret = GST_RTSP_ERROR;
1821 static GstRTSPResult
1822 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
1823 GstRTSPMessage * message, GTimeVal * timeout)
1828 ret = gst_rtsp_connection_receive (conn, message, timeout);
1830 ret = GST_RTSP_ERROR;
1836 gst_rtspsrc_get_position (GstRTSPSrc * src)
1841 query = gst_query_new_position (GST_FORMAT_TIME);
1842 /* should be known somewhere down the stream (e.g. jitterbuffer) */
1843 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1844 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1848 if (stream->srcpad) {
1849 if (gst_pad_query (stream->srcpad, query)) {
1850 gst_query_parse_position (query, &fmt, &pos);
1851 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
1852 GST_TIME_ARGS (pos));
1853 src->last_pos = pos;
1863 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
1865 src->state = GST_RTSP_STATE_SEEKING;
1866 /* PLAY will add the range header now. */
1867 src->need_range = TRUE;
1873 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
1878 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
1880 gboolean flush, skip;
1883 GstSegment seeksegment = { 0, };
1887 GST_DEBUG_OBJECT (src, "doing seek with event");
1889 gst_event_parse_seek (event, &rate, &format, &flags,
1890 &cur_type, &cur, &stop_type, &stop);
1892 /* no negative rates yet */
1896 /* we need TIME format */
1897 if (format != src->segment.format)
1900 GST_DEBUG_OBJECT (src, "doing seek without event");
1902 cur_type = GST_SEEK_TYPE_SET;
1903 stop_type = GST_SEEK_TYPE_SET;
1906 /* get flush flag */
1907 flush = flags & GST_SEEK_FLAG_FLUSH;
1908 skip = flags & GST_SEEK_FLAG_SKIP;
1910 /* now we need to make sure the streaming thread is stopped. We do this by
1911 * either sending a FLUSH_START event downstream which will cause the
1912 * streaming thread to stop with a WRONG_STATE.
1913 * For a non-flushing seek we simply pause the task, which will happen as soon
1914 * as it completes one iteration (and thus might block when the sink is
1915 * blocking in preroll). */
1917 GST_DEBUG_OBJECT (src, "starting flush");
1918 gst_rtspsrc_flush (src, TRUE, FALSE);
1921 gst_task_pause (src->task);
1925 /* we should now be able to grab the streaming thread because we stopped it
1926 * with the above flush/pause code */
1927 GST_RTSP_STREAM_LOCK (src);
1929 GST_DEBUG_OBJECT (src, "stopped streaming");
1931 /* copy segment, we need this because we still need the old
1932 * segment when we close the current segment. */
1933 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
1935 /* configure the seek parameters in the seeksegment. We will then have the
1936 * right values in the segment to perform the seek */
1938 GST_DEBUG_OBJECT (src, "configuring seek");
1939 gst_segment_do_seek (&seeksegment, rate, format, flags,
1940 cur_type, cur, stop_type, stop, &update);
1943 /* figure out the last position we need to play. If it's configured (stop !=
1944 * -1), use that, else we play until the total duration of the file */
1945 if ((stop = seeksegment.stop) == -1)
1946 stop = seeksegment.duration;
1948 playing = (src->state == GST_RTSP_STATE_PLAYING);
1950 /* if we were playing, pause first */
1952 /* obtain current position in case seek fails */
1953 gst_rtspsrc_get_position (src);
1954 gst_rtspsrc_pause (src, FALSE);
1958 gst_rtspsrc_do_seek (src, &seeksegment);
1960 /* and continue playing */
1962 gst_rtspsrc_play (src, &seeksegment, FALSE);
1964 /* prepare for streaming again */
1966 /* if we started flush, we stop now */
1967 GST_DEBUG_OBJECT (src, "stopping flush");
1968 gst_rtspsrc_flush (src, FALSE, playing);
1971 /* now we did the seek and can activate the new segment values */
1972 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
1974 /* if we're doing a segment seek, post a SEGMENT_START message */
1975 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1976 gst_element_post_message (GST_ELEMENT_CAST (src),
1977 gst_message_new_segment_start (GST_OBJECT_CAST (src),
1978 src->segment.format, src->segment.position));
1981 /* now create the newsegment */
1982 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
1983 " to %" G_GINT64_FORMAT, src->segment.position, stop);
1986 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
1987 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1988 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1989 stream->discont = TRUE;
1992 GST_RTSP_STREAM_UNLOCK (src);
1999 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2004 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2010 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2014 gboolean res = TRUE;
2017 src = GST_RTSPSRC_CAST (parent);
2019 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2020 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2022 switch (GST_EVENT_TYPE (event)) {
2023 case GST_EVENT_SEEK:
2024 res = gst_rtspsrc_perform_seek (src, event);
2028 case GST_EVENT_NAVIGATION:
2029 case GST_EVENT_LATENCY:
2037 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2038 res = gst_pad_send_event (target, event);
2039 gst_object_unref (target);
2041 gst_event_unref (event);
2044 gst_event_unref (event);
2050 /* this is the final event function we receive on the internal source pad when
2051 * we deal with TCP connections */
2053 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2058 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2060 switch (GST_EVENT_TYPE (event)) {
2061 case GST_EVENT_SEEK:
2063 case GST_EVENT_NAVIGATION:
2064 case GST_EVENT_LATENCY:
2066 gst_event_unref (event);
2073 /* this is the final query function we receive on the internal source pad when
2074 * we deal with TCP connections */
2076 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2080 gboolean res = TRUE;
2082 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2084 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2085 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2087 switch (GST_QUERY_TYPE (query)) {
2088 case GST_QUERY_POSITION:
2093 case GST_QUERY_DURATION:
2097 gst_query_parse_duration (query, &format, NULL);
2100 case GST_FORMAT_TIME:
2101 gst_query_set_duration (query, format, src->segment.duration);
2109 case GST_QUERY_LATENCY:
2111 /* we are live with a min latency of 0 and unlimited max latency, this
2112 * result will be updated by the session manager if there is any. */
2113 gst_query_set_latency (query, TRUE, 0, -1);
2123 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2125 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2129 gboolean res = FALSE;
2131 src = GST_RTSPSRC_CAST (parent);
2133 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2134 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2136 switch (GST_QUERY_TYPE (query)) {
2137 case GST_QUERY_DURATION:
2141 gst_query_parse_duration (query, &format, NULL);
2144 case GST_FORMAT_TIME:
2145 gst_query_set_duration (query, format, src->segment.duration);
2153 case GST_QUERY_SEEKING:
2157 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2158 if (format == GST_FORMAT_TIME) {
2160 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2162 /* seeking without duration is unlikely */
2163 seekable = seekable && src->seekable && src->segment.duration &&
2164 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2166 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2167 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2168 src->segment.start, src->segment.stop);
2177 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2179 gst_query_set_uri (query, uri);
2187 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2189 /* forward the query to the proxy target pad */
2191 res = gst_pad_query (target, query);
2192 gst_object_unref (target);
2201 /* callback for RTCP messages to be sent to the server when operating in TCP
2203 static GstFlowReturn
2204 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2207 GstRTSPStream *stream;
2208 GstFlowReturn res = GST_FLOW_OK;
2213 GstRTSPMessage message = { 0 };
2214 GstRTSPConnection *conn;
2216 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2217 src = stream->parent;
2219 gst_buffer_map (buffer, &map, GST_MAP_READ);
2223 gst_rtsp_message_init_data (&message, stream->channel[1]);
2225 /* lend the body data to the message */
2226 gst_rtsp_message_take_body (&message, data, size);
2228 if (stream->conninfo.connection)
2229 conn = stream->conninfo.connection;
2231 conn = src->conninfo.connection;
2233 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2234 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2235 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2237 /* and steal it away again because we will free it when unreffing the
2239 gst_rtsp_message_steal_body (&message, &data, &size);
2240 gst_rtsp_message_unset (&message);
2242 gst_buffer_unmap (buffer, &map);
2243 gst_buffer_unref (buffer);
2248 static GstPadProbeReturn
2249 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2251 GstRTSPSrc *src = user_data;
2253 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2254 GST_DEBUG_PAD_NAME (pad));
2256 /* activate the streams */
2257 GST_OBJECT_LOCK (src);
2258 if (!src->need_activate)
2261 src->need_activate = FALSE;
2262 GST_OBJECT_UNLOCK (src);
2264 gst_rtspsrc_activate_streams (src);
2266 return GST_PAD_PROBE_OK;
2270 GST_OBJECT_UNLOCK (src);
2271 return GST_PAD_PROBE_OK;
2275 /* this callback is called when the session manager generated a new src pad with
2276 * payloaded RTP packets. We simply ghost the pad here. */
2278 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2281 GstPadTemplate *template;
2284 GstRTSPStream *stream;
2287 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2289 GST_RTSP_STATE_LOCK (src);
2291 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2292 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2293 goto unknown_stream;
2295 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2297 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2299 goto unknown_stream;
2301 /* we'll add it later see below */
2302 stream->added = TRUE;
2304 /* check if we added all streams */
2306 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2307 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2309 GST_DEBUG_OBJECT (src, "stream %p, container %d, disabled %d, added %d",
2310 ostream, ostream->container, ostream->disabled, ostream->added);
2312 /* a container stream only needs one pad added. Also disabled streams don't
2314 if (!ostream->container && !ostream->disabled && !ostream->added) {
2319 GST_RTSP_STATE_UNLOCK (src);
2321 /* create a new pad we will use to stream to */
2322 template = gst_static_pad_template_get (&rtptemplate);
2323 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2324 gst_object_unref (template);
2327 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2328 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2329 gst_pad_set_active (stream->srcpad, TRUE);
2330 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2333 GST_DEBUG_OBJECT (src, "We added all streams");
2334 /* when we get here, all stream are added and we can fire the no-more-pads
2336 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2344 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2345 GST_RTSP_STATE_UNLOCK (src);
2352 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2354 GstRTSPStream *stream;
2357 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2359 GST_RTSP_STATE_LOCK (src);
2360 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2362 goto unknown_stream;
2364 caps = stream->caps;
2366 gst_caps_ref (caps);
2367 GST_RTSP_STATE_UNLOCK (src);
2373 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2374 GST_RTSP_STATE_UNLOCK (src);
2380 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2382 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2388 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2394 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2400 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2402 GstRTSPSrc *src = stream->parent;
2404 GST_DEBUG_OBJECT (src, "source in session %u received BYE", stream->id);
2406 gst_rtspsrc_do_stream_eos (src, stream);
2410 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2412 GstRTSPSrc *src = stream->parent;
2414 GST_DEBUG_OBJECT (src, "source in session %u timed out", stream->id);
2416 gst_rtspsrc_do_stream_eos (src, stream);
2420 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2422 GstRTSPStream *stream;
2424 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2426 /* get stream for session */
2427 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2429 gst_rtspsrc_do_stream_eos (src, stream);
2434 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2436 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2440 /* try to get and configure a manager */
2442 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
2443 GstRTSPTransport * transport)
2445 const gchar *manager;
2447 GstStateChangeReturn ret;
2449 /* find a manager */
2450 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
2454 GST_DEBUG_OBJECT (src, "using manager %s", manager);
2456 /* configure the manager */
2457 if (src->manager == NULL) {
2458 GObjectClass *klass;
2461 if (!(src->manager = gst_element_factory_make (manager, NULL))) {
2463 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
2467 goto use_no_manager;
2469 if (!(src->manager = gst_element_factory_make (manager, NULL)))
2470 goto manager_failed;
2473 /* we manage this element */
2474 gst_bin_add (GST_BIN_CAST (src), src->manager);
2476 GST_OBJECT_LOCK (src);
2477 target = GST_STATE_TARGET (src);
2478 GST_OBJECT_UNLOCK (src);
2480 ret = gst_element_set_state (src->manager, target);
2481 if (ret == GST_STATE_CHANGE_FAILURE)
2482 goto start_manager_failure;
2484 g_object_set (src->manager, "latency", src->latency, NULL);
2486 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2487 if (g_object_class_find_property (klass, "drop-on-latency")) {
2488 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
2492 if (g_object_class_find_property (klass, "buffer-mode")) {
2493 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2494 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2496 gboolean need_slave;
2498 const gchar *encoding;
2500 /* buffer mode pauses are handled by adding offsets to buffer times,
2501 * but some depayloaders may have a hard time syncing output times
2502 * with such input times, e.g. container ones, most notably ASF */
2503 /* TODO alternatives are having an event that indicates these shifts,
2504 * or having rtsp extensions provide suggestion on buffer mode */
2505 need_slave = stream->container;
2506 if (stream->caps && (s = gst_caps_get_structure (stream->caps, 0)) &&
2507 (encoding = gst_structure_get_string (s, "encoding-name")))
2508 need_slave = need_slave || (strcmp (encoding, "X-ASF-PF") == 0);
2509 GST_DEBUG_OBJECT (src, "auto buffering mode, need_slave %d",
2511 /* valid duration implies not likely live pipeline,
2512 * so slaving in jitterbuffer does not make much sense
2513 * (and might mess things up due to bursts) */
2514 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
2515 src->segment.duration && !need_slave) {
2516 GST_DEBUG_OBJECT (src, "selected buffer");
2517 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER,
2520 GST_DEBUG_OBJECT (src, "selected slave");
2521 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2526 /* connect to signals if we did not already do so */
2527 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
2529 src->manager_sig_id =
2530 g_signal_connect (src->manager, "pad-added",
2531 (GCallback) new_manager_pad, src);
2532 src->manager_ptmap_id =
2533 g_signal_connect (src->manager, "request-pt-map",
2534 (GCallback) request_pt_map, src);
2536 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
2540 /* we stream directly to the manager, get some pads. Each RTSP stream goes
2541 * into a separate RTP session. */
2542 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
2543 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
2545 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
2546 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
2549 /* now configure the bandwidth in the manager */
2550 if (g_signal_lookup ("get-internal-session",
2551 G_OBJECT_TYPE (src->manager)) != 0) {
2552 GObject *rtpsession;
2554 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
2557 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
2559 stream->session = rtpsession;
2561 if (stream->as_bandwidth != -1) {
2562 GST_INFO_OBJECT (src, "setting AS: %f",
2563 (gdouble) (stream->as_bandwidth * 1000));
2564 g_object_set (rtpsession, "bandwidth",
2565 (gdouble) (stream->as_bandwidth * 1000), NULL);
2567 if (stream->rr_bandwidth != -1) {
2568 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
2569 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
2572 if (stream->rs_bandwidth != -1) {
2573 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
2574 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
2578 g_object_set (rtpsession, "probation", src->probation, NULL);
2580 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2582 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
2584 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
2586 g_signal_connect (rtpsession, "on-ssrc-active",
2587 (GCallback) on_ssrc_active, stream);
2598 GST_DEBUG_OBJECT (src, "cannot get a session manager");
2603 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
2606 start_manager_failure:
2608 GST_DEBUG_OBJECT (src, "could not start session manager");
2613 /* free the UDP sources allocated when negotiating a transport.
2614 * This function is called when the server negotiated to a transport where the
2615 * UDP sources are not needed anymore, such as TCP or multicast. */
2617 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
2621 for (i = 0; i < 2; i++) {
2622 if (stream->udpsrc[i]) {
2623 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
2624 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
2625 gst_object_unref (stream->udpsrc[i]);
2626 stream->udpsrc[i] = NULL;
2631 /* for TCP, create pads to send and receive data to and from the manager and to
2632 * intercept various events and queries
2635 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
2636 GstRTSPTransport * transport, GstPad ** outpad)
2639 GstPadTemplate *template;
2640 GstPad *pad0, *pad1;
2642 /* configure for interleaved delivery, nothing needs to be done
2643 * here, the loop function will call the chain functions of the
2644 * session manager. */
2645 stream->channel[0] = transport->interleaved.min;
2646 stream->channel[1] = transport->interleaved.max;
2647 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
2648 stream->channel[0], stream->channel[1]);
2650 /* we can remove the allocated UDP ports now */
2651 gst_rtspsrc_stream_free_udp (stream);
2653 /* no session manager, send data to srcpad directly */
2654 if (!stream->channelpad[0]) {
2655 GST_DEBUG_OBJECT (src, "no manager, creating pad");
2657 /* create a new pad we will use to stream to */
2658 name = g_strdup_printf ("stream_%u", stream->id);
2659 template = gst_static_pad_template_get (&rtptemplate);
2660 stream->channelpad[0] = gst_pad_new_from_template (template, name);
2661 gst_object_unref (template);
2664 /* set caps and activate */
2665 gst_pad_use_fixed_caps (stream->channelpad[0]);
2666 gst_pad_set_active (stream->channelpad[0], TRUE);
2668 *outpad = gst_object_ref (stream->channelpad[0]);
2670 GST_DEBUG_OBJECT (src, "using manager source pad");
2672 template = gst_static_pad_template_get (&anysrctemplate);
2674 /* allocate pads for sending the channel data into the manager */
2675 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
2676 gst_pad_link (pad0, stream->channelpad[0]);
2677 gst_object_unref (stream->channelpad[0]);
2678 stream->channelpad[0] = pad0;
2679 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
2680 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
2681 gst_pad_set_element_private (pad0, src);
2682 gst_pad_set_active (pad0, TRUE);
2684 if (stream->channelpad[1]) {
2685 /* if we have a sinkpad for the other channel, create a pad and link to the
2687 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
2688 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
2689 gst_pad_link (pad1, stream->channelpad[1]);
2690 gst_object_unref (stream->channelpad[1]);
2691 stream->channelpad[1] = pad1;
2692 gst_pad_set_active (pad1, TRUE);
2694 gst_object_unref (template);
2696 /* setup RTCP transport back to the server if we have to. */
2697 if (src->manager && src->do_rtcp) {
2700 template = gst_static_pad_template_get (&anysinktemplate);
2702 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
2703 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
2704 gst_pad_set_element_private (stream->rtcppad, stream);
2705 gst_pad_set_active (stream->rtcppad, TRUE);
2707 /* get session RTCP pad */
2708 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
2709 pad = gst_element_get_request_pad (src->manager, name);
2714 gst_pad_link (pad, stream->rtcppad);
2715 gst_object_unref (pad);
2718 gst_object_unref (template);
2724 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
2725 GstRTSPTransport * transport, const gchar ** destination, gint * min,
2726 gint * max, guint * ttl)
2728 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2730 if (!(*destination = transport->destination))
2731 *destination = stream->destination;
2734 /* transport first */
2735 *min = transport->port.min;
2736 *max = transport->port.max;
2737 if (*min == -1 && *max == -1) {
2738 /* then try from SDP */
2739 if (stream->port != 0) {
2740 *min = stream->port;
2741 *max = stream->port + 1;
2747 if (!(*ttl = transport->ttl))
2752 /* first take the source, then the endpoint to figure out where to send
2754 if (!(*destination = transport->source)) {
2755 if (src->conninfo.connection)
2756 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
2757 else if (stream->conninfo.connection)
2759 gst_rtsp_connection_get_ip (stream->conninfo.connection);
2763 /* for unicast we only expect the ports here */
2764 *min = transport->server_port.min;
2765 *max = transport->server_port.max;
2770 /* For multicast create UDP sources and join the multicast group. */
2772 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
2773 GstRTSPTransport * transport, GstPad ** outpad)
2776 const gchar *destination;
2779 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
2781 /* we can remove the allocated UDP ports now */
2782 gst_rtspsrc_stream_free_udp (stream);
2784 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
2787 /* we need a destination now */
2788 if (destination == NULL)
2789 goto no_destination;
2791 /* we really need ports now or we won't be able to receive anything at all */
2792 if (min == -1 && max == -1)
2795 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
2796 destination, min, max);
2798 /* creating UDP source for RTP */
2800 uri = g_strdup_printf ("udp://%s:%d", destination, min);
2802 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
2804 if (stream->udpsrc[0] == NULL)
2807 /* take ownership */
2808 gst_object_ref_sink (stream->udpsrc[0]);
2810 if (src->udp_buffer_size != 0)
2811 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
2812 src->udp_buffer_size, NULL);
2815 gst_element_set_state (stream->udpsrc[0], GST_STATE_PAUSED);
2818 /* creating another UDP source for RTCP */
2820 uri = g_strdup_printf ("udp://%s:%d", destination, max);
2822 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
2824 if (stream->udpsrc[1] == NULL)
2827 /* take ownership */
2828 gst_object_ref_sink (stream->udpsrc[1]);
2830 gst_element_set_state (stream->udpsrc[1], GST_STATE_PAUSED);
2837 GST_DEBUG_OBJECT (src, "no UDP source element found");
2842 GST_DEBUG_OBJECT (src, "no destination found");
2847 GST_DEBUG_OBJECT (src, "no ports found");
2852 /* configure the remainder of the UDP ports */
2854 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
2855 GstRTSPTransport * transport, GstPad ** outpad)
2857 /* we manage the UDP elements now. For unicast, the UDP sources where
2858 * allocated in the stream when we suggested a transport. */
2859 if (stream->udpsrc[0]) {
2860 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
2862 GST_DEBUG_OBJECT (src, "setting up UDP source");
2864 /* configure a timeout on the UDP port. When the timeout message is
2865 * posted, we assume UDP transport is not possible. We reconnect using TCP
2867 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
2868 src->udp_timeout * 1000, NULL);
2870 /* get output pad of the UDP source. */
2871 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
2873 /* save it so we can unblock */
2874 stream->blockedpad = *outpad;
2876 /* configure pad block on the pad. As soon as there is dataflow on the
2877 * UDP source, we know that UDP is not blocked by a firewall and we can
2878 * configure all the streams to let the application autoplug decoders. */
2880 gst_pad_add_probe (stream->blockedpad,
2881 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, pad_blocked, src, NULL);
2883 if (stream->channelpad[0]) {
2884 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
2885 /* configure for UDP delivery, we need to connect the UDP pads to
2886 * the session plugin. */
2887 gst_pad_link (*outpad, stream->channelpad[0]);
2888 gst_object_unref (*outpad);
2890 /* we connected to pad-added signal to get pads from the manager */
2892 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
2897 if (stream->udpsrc[1]) {
2898 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
2900 if (stream->channelpad[1]) {
2903 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
2905 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
2906 gst_pad_link (pad, stream->channelpad[1]);
2907 gst_object_unref (pad);
2909 /* leave unlinked */
2915 /* configure the UDP sink back to the server for status reports */
2917 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
2918 GstRTSPStream * stream, GstRTSPTransport * transport)
2921 gint rtp_port, rtcp_port;
2922 gboolean do_rtp, do_rtcp;
2923 const gchar *destination;
2928 /* get transport info */
2929 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
2930 &rtp_port, &rtcp_port, &ttl);
2932 /* see what we need to do */
2933 do_rtp = (rtp_port != -1);
2934 /* it's possible that the server does not want us to send RTCP in which case
2936 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
2938 /* we need a destination when we have RTP or RTCP ports */
2939 if (destination == NULL && (do_rtp || do_rtcp))
2940 goto no_destination;
2942 /* try to construct the fakesrc to the RTP port of the server to open up any
2945 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
2948 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
2949 stream->udpsink[0] =
2950 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
2952 if (stream->udpsink[0] == NULL)
2953 goto no_sink_element;
2955 /* don't join multicast group, we will have the source socket do that */
2956 /* no sync or async state changes needed */
2957 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
2958 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
2960 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
2962 if (stream->udpsrc[0]) {
2963 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
2964 * so that NAT firewalls will open a hole for us */
2965 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
2966 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
2967 /* configure socket and make sure udpsink does not close it when shutting
2968 * down, it belongs to udpsrc after all. */
2969 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
2970 "close-socket", FALSE, NULL);
2971 g_object_unref (socket);
2974 /* the source for the dummy packets to open up NAT */
2975 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
2976 if (stream->fakesrc == NULL)
2977 goto no_fakesrc_element;
2979 /* random data in 5 buffers, a size of 200 bytes should be fine */
2980 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
2981 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
2983 /* we don't want to consider this a sink */
2984 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
2986 /* keep everything locked */
2987 gst_element_set_locked_state (stream->udpsink[0], TRUE);
2988 gst_element_set_locked_state (stream->fakesrc, TRUE);
2990 gst_object_ref (stream->udpsink[0]);
2991 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
2992 gst_object_ref (stream->fakesrc);
2993 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
2995 gst_element_link (stream->fakesrc, stream->udpsink[0]);
2998 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3001 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3002 stream->udpsink[1] =
3003 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3005 if (stream->udpsink[1] == NULL)
3006 goto no_sink_element;
3008 /* don't join multicast group, we will have the source socket do that */
3009 /* no sync or async state changes needed */
3010 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3011 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3013 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3015 if (stream->udpsrc[1]) {
3016 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3017 * because some servers check the port number of where it sends RTCP to identify
3018 * the RTCP packets it receives */
3019 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3020 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3021 /* configure socket and make sure udpsink does not close it when shutting
3022 * down, it belongs to udpsrc after all. */
3023 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3024 "close-socket", FALSE, NULL);
3025 g_object_unref (socket);
3028 /* we don't want to consider this a sink */
3029 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3031 /* we keep this playing always */
3032 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3033 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3035 gst_object_ref (stream->udpsink[1]);
3036 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3038 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3040 /* get session RTCP pad */
3041 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3042 pad = gst_element_get_request_pad (src->manager, name);
3047 gst_pad_link (pad, stream->rtcppad);
3048 gst_object_unref (pad);
3057 GST_DEBUG_OBJECT (src, "no destination address specified");
3062 GST_DEBUG_OBJECT (src, "no UDP sink element found");
3067 GST_DEBUG_OBJECT (src, "no fakesrc element found");
3072 /* sets up all elements needed for streaming over the specified transport.
3073 * Does not yet expose the element pads, this will be done when there is actuall
3074 * dataflow detected, which might never happen when UDP is blocked in a
3075 * firewall, for example.
3078 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3079 GstRTSPTransport * transport)
3082 GstPad *outpad = NULL;
3083 GstPadTemplate *template;
3088 src = stream->parent;
3090 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3092 s = gst_caps_get_structure (stream->caps, 0);
3094 /* get the proper mime type for this stream now */
3095 if (gst_rtsp_transport_get_mime (transport->trans, &mime) < 0)
3096 goto unknown_transport;
3098 goto unknown_transport;
3100 /* configure the final mime type */
3101 GST_DEBUG_OBJECT (src, "setting mime to %s", mime);
3102 gst_structure_set_name (s, mime);
3104 /* try to get and configure a manager, channelpad[0-1] will be configured with
3105 * the pads for the manager, or NULL when no manager is needed. */
3106 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3109 switch (transport->lower_transport) {
3110 case GST_RTSP_LOWER_TRANS_TCP:
3111 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3112 goto transport_failed;
3114 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3115 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3116 goto transport_failed;
3117 /* fallthrough, the rest is the same for UDP and MCAST */
3118 case GST_RTSP_LOWER_TRANS_UDP:
3119 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3120 goto transport_failed;
3121 /* configure udpsinks back to the server for RTCP messages and for the
3122 * dummy RTP messages to open NAT. */
3123 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3124 goto transport_failed;
3127 goto unknown_transport;
3131 GST_DEBUG_OBJECT (src, "creating ghostpad");
3133 gst_pad_use_fixed_caps (outpad);
3135 /* create ghostpad, don't add just yet, this will be done when we activate
3137 name = g_strdup_printf ("stream_%u", stream->id);
3138 template = gst_static_pad_template_get (&rtptemplate);
3139 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3140 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3141 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3142 gst_object_unref (template);
3145 gst_object_unref (outpad);
3147 /* mark pad as ok */
3148 stream->last_ret = GST_FLOW_OK;
3155 GST_DEBUG_OBJECT (src, "failed to configure transport");
3160 GST_DEBUG_OBJECT (src, "unknown transport");
3165 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3170 /* send a couple of dummy random packets on the receiver RTP port to the server,
3171 * this should make a firewall think we initiated the data transfer and
3172 * hopefully allow packets to go from the sender port to our RTP receiver port */
3174 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3178 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3181 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3182 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3184 if (stream->fakesrc && stream->udpsink[0]) {
3185 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3186 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3187 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3188 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3189 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3195 /* Adds the source pads of all configured streams to the element.
3196 * This code is performed when we detected dataflow.
3198 * We detect dataflow from either the _loop function or with pad probes on the
3202 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3206 GST_DEBUG_OBJECT (src, "activating streams");
3208 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3209 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3211 if (stream->udpsrc[0]) {
3212 /* remove timeout, we are streaming now and timeouts will be handled by
3213 * the session manager and jitter buffer */
3214 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3216 if (stream->srcpad) {
3217 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3218 gst_pad_set_active (stream->srcpad, TRUE);
3220 /* if we don't have a session manager, set the caps now. If we have a
3221 * session, we will get a notification of the pad and the caps. */
3222 if (!src->manager) {
3223 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3224 gst_pad_set_caps (stream->srcpad, stream->caps);
3227 if (!stream->added) {
3228 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3229 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3230 stream->added = TRUE;
3235 /* unblock all pads */
3236 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3237 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3239 if (stream->blockid) {
3240 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3241 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3242 stream->blockid = 0;
3250 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
3251 gboolean reset_manager)
3254 guint64 start, stop;
3255 gdouble play_speed, play_scale;
3257 GST_DEBUG_OBJECT (src, "configuring stream caps");
3259 start = segment->position;
3260 stop = segment->duration;
3261 play_speed = segment->rate;
3262 play_scale = segment->applied_rate;
3264 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3265 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3268 if ((caps = stream->caps)) {
3269 caps = gst_caps_make_writable (caps);
3271 if (stream->timebase != -1)
3272 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3273 (guint) stream->timebase, NULL);
3274 if (stream->seqbase != -1)
3275 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3276 (guint) stream->seqbase, NULL);
3277 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3279 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3280 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3281 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3283 stream->caps = caps;
3285 GST_DEBUG_OBJECT (src, "stream %p, caps %" GST_PTR_FORMAT, stream, caps);
3287 if (reset_manager && src->manager) {
3288 GST_DEBUG_OBJECT (src, "clear session");
3289 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3293 static GstFlowReturn
3294 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3299 /* store the value */
3300 stream->last_ret = ret;
3302 /* if it's success we can return the value right away */
3303 if (ret == GST_FLOW_OK)
3306 /* any other error that is not-linked can be returned right
3308 if (ret != GST_FLOW_NOT_LINKED)
3311 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3312 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3313 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3315 ret = ostream->last_ret;
3316 /* some other return value (must be SUCCESS but we can return
3317 * other values as well) */
3318 if (ret != GST_FLOW_NOT_LINKED)
3321 /* if we get here, all other pads were unlinked and we return
3322 * NOT_LINKED then */
3328 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
3331 gboolean res = TRUE;
3333 /* only streams that have a connection to the outside world */
3334 if (stream->container || stream->disabled)
3337 if (stream->udpsrc[0]) {
3338 gst_event_ref (event);
3339 res = gst_element_send_event (stream->udpsrc[0], event);
3340 } else if (stream->channelpad[0]) {
3341 gst_event_ref (event);
3342 if (GST_PAD_IS_SRC (stream->channelpad[0]))
3343 res = gst_pad_push_event (stream->channelpad[0], event);
3345 res = gst_pad_send_event (stream->channelpad[0], event);
3348 if (stream->udpsrc[1]) {
3349 gst_event_ref (event);
3350 res &= gst_element_send_event (stream->udpsrc[1], event);
3351 } else if (stream->channelpad[1]) {
3352 gst_event_ref (event);
3353 if (GST_PAD_IS_SRC (stream->channelpad[1]))
3354 res &= gst_pad_push_event (stream->channelpad[1], event);
3356 res &= gst_pad_send_event (stream->channelpad[1], event);
3360 gst_event_unref (event);
3366 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
3369 gboolean res = TRUE;
3371 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3372 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3374 gst_event_ref (event);
3375 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
3377 gst_event_unref (event);
3382 static GstRTSPResult
3383 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3388 if (info->connection == NULL) {
3389 if (info->url == NULL) {
3390 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
3391 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
3395 /* create connection */
3396 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
3397 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
3398 goto could_not_create;
3401 g_free (info->url_str);
3402 info->url_str = gst_rtsp_url_get_request_uri (info->url);
3404 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
3406 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
3407 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
3409 if (src->proxy_host) {
3410 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
3412 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
3417 if (!info->connected) {
3420 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
3421 ("Connecting to %s", info->location));
3422 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
3424 gst_rtsp_connection_connect (info->connection,
3425 src->ptcp_timeout)) < 0)
3426 goto could_not_connect;
3428 info->connected = TRUE;
3435 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
3440 gchar *str = gst_rtsp_strresult (res);
3441 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
3447 gchar *str = gst_rtsp_strresult (res);
3448 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
3454 static GstRTSPResult
3455 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
3458 GST_RTSP_STATE_LOCK (src);
3459 if (info->connected) {
3460 GST_DEBUG_OBJECT (src, "closing connection...");
3461 gst_rtsp_connection_close (info->connection);
3462 info->connected = FALSE;
3464 if (free && info->connection) {
3465 /* free connection */
3466 GST_DEBUG_OBJECT (src, "freeing connection...");
3467 gst_rtsp_connection_free (info->connection);
3468 info->connection = NULL;
3470 GST_RTSP_STATE_UNLOCK (src);
3474 static GstRTSPResult
3475 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3480 GST_DEBUG_OBJECT (src, "reconnecting connection...");
3481 gst_rtsp_conninfo_close (src, info, FALSE);
3482 res = gst_rtsp_conninfo_connect (src, info, async);
3488 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
3492 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
3493 GST_RTSP_STATE_LOCK (src);
3494 if (src->conninfo.connection) {
3495 GST_DEBUG_OBJECT (src, "connection flush");
3496 gst_rtsp_connection_flush (src->conninfo.connection, flush);
3498 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3499 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3500 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
3501 if (stream->conninfo.connection)
3502 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
3504 GST_RTSP_STATE_UNLOCK (src);
3507 /* FIXME, handle server request, reply with OK, for now */
3508 static GstRTSPResult
3509 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
3510 GstRTSPMessage * request)
3512 GstRTSPMessage response = { 0 };
3515 GST_DEBUG_OBJECT (src, "got server request message");
3518 gst_rtsp_message_dump (request);
3520 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
3522 if (res == GST_RTSP_ENOTIMPL) {
3523 /* default implementation, send OK */
3525 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
3530 GST_DEBUG_OBJECT (src, "replying with OK");
3533 gst_rtsp_message_dump (&response);
3535 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
3539 gst_rtsp_message_unset (&response);
3540 } else if (res == GST_RTSP_EEOF)
3548 gst_rtsp_message_unset (&response);
3553 /* send server keep-alive */
3554 static GstRTSPResult
3555 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
3557 GstRTSPMessage request = { 0 };
3559 GstRTSPMethod method;
3562 if (src->do_rtsp_keep_alive == FALSE) {
3563 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
3564 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3568 GST_DEBUG_OBJECT (src, "creating server keep-alive");
3570 /* find a method to use for keep-alive */
3571 if (src->methods & GST_RTSP_GET_PARAMETER)
3572 method = GST_RTSP_GET_PARAMETER;
3574 method = GST_RTSP_OPTIONS;
3577 control = src->control;
3579 control = src->conninfo.url_str;
3581 if (control == NULL)
3584 res = gst_rtsp_message_init_request (&request, method, control);
3589 gst_rtsp_message_dump (&request);
3592 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
3597 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3598 gst_rtsp_message_unset (&request);
3605 GST_WARNING_OBJECT (src, "no control url to send keepalive");
3610 gchar *str = gst_rtsp_strresult (res);
3612 gst_rtsp_message_unset (&request);
3613 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
3614 ("Could not send keep-alive. (%s)", str));
3620 static GstFlowReturn
3621 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
3623 GstRTSPMessage message = { 0 };
3626 GstRTSPStream *stream;
3627 GstPad *outpad = NULL;
3630 GstFlowReturn ret = GST_FLOW_OK;
3632 gboolean is_rtcp, have_data;
3635 /* here we are only interested in data messages */
3638 GTimeVal tv_timeout;
3640 /* get the next timeout interval */
3641 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3643 /* see if the timeout period expired */
3644 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
3645 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
3646 /* send keep-alive, only act on interrupt, a warning will be posted for
3648 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3650 /* get new timeout */
3651 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3654 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
3655 tv_timeout.tv_sec, tv_timeout.tv_usec);
3657 /* protect the connection with the connection lock so that we can see when
3658 * we are finished doing server communication */
3660 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
3661 &message, src->ptcp_timeout);
3665 GST_DEBUG_OBJECT (src, "we received a server message");
3667 case GST_RTSP_EINTR:
3668 /* we got interrupted this means we need to stop */
3670 case GST_RTSP_ETIMEOUT:
3671 /* no reply, send keep alive */
3672 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
3673 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3677 /* go EOS when the server closed the connection */
3683 switch (message.type) {
3684 case GST_RTSP_MESSAGE_REQUEST:
3685 /* server sends us a request message, handle it */
3687 gst_rtspsrc_handle_request (src, src->conninfo.connection,
3689 if (res == GST_RTSP_EEOF)
3692 goto handle_request_failed;
3694 case GST_RTSP_MESSAGE_RESPONSE:
3695 /* we ignore response messages */
3696 GST_DEBUG_OBJECT (src, "ignoring response message");
3698 gst_rtsp_message_dump (&message);
3700 case GST_RTSP_MESSAGE_DATA:
3701 GST_DEBUG_OBJECT (src, "got data message");
3705 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
3712 channel = message.type_data.data.channel;
3714 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
3716 goto unknown_stream;
3718 if (channel == stream->channel[0]) {
3719 outpad = stream->channelpad[0];
3721 } else if (channel == stream->channel[1]) {
3722 outpad = stream->channelpad[1];
3728 /* take a look at the body to figure out what we have */
3729 gst_rtsp_message_get_body (&message, &data, &size);
3731 goto invalid_length;
3733 /* channels are not correct on some servers, do extra check */
3734 if (data[1] >= 200 && data[1] <= 204) {
3735 /* hmm RTCP message switch to the RTCP pad of the same stream. */
3736 outpad = stream->channelpad[1];
3740 /* we have no clue what this is, just ignore then. */
3742 goto unknown_stream;
3744 /* take the message body for further processing */
3745 gst_rtsp_message_steal_body (&message, &data, &size);
3747 /* strip the trailing \0 */
3750 buf = gst_buffer_new ();
3751 gst_buffer_append_memory (buf,
3752 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
3754 /* don't need message anymore */
3755 gst_rtsp_message_unset (&message);
3757 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
3760 if (src->need_activate) {
3761 gst_rtspsrc_activate_streams (src);
3762 src->need_activate = FALSE;
3764 if ((event = src->start_segment) != NULL) {
3765 src->start_segment = NULL;
3766 gst_rtspsrc_push_event (src, event);
3769 if (src->base_time == -1) {
3770 /* Take current running_time. This timestamp will be put on
3771 * the first buffer of each stream because we are a live source and so we
3772 * timestamp with the running_time. When we are dealing with TCP, we also
3773 * only timestamp the first buffer (using the DISCONT flag) because a server
3774 * typically bursts data, for which we don't want to compensate by speeding
3775 * up the media. The other timestamps will be interpollated from this one
3776 * using the RTP timestamps. */
3777 GST_OBJECT_LOCK (src);
3778 if (GST_ELEMENT_CLOCK (src)) {
3780 GstClockTime base_time;
3782 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
3783 base_time = GST_ELEMENT_CAST (src)->base_time;
3785 src->base_time = now - base_time;
3787 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
3788 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
3790 GST_OBJECT_UNLOCK (src);
3793 if (stream->discont && !is_rtcp) {
3794 /* mark first RTP buffer as discont */
3795 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
3796 stream->discont = FALSE;
3797 /* first buffer gets the timestamp, other buffers are not timestamped and
3798 * their presentation time will be interpollated from the rtp timestamps. */
3799 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
3800 GST_TIME_ARGS (src->base_time));
3802 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
3805 /* chain to the peer pad */
3806 if (GST_PAD_IS_SINK (outpad))
3807 ret = gst_pad_chain (outpad, buf);
3809 ret = gst_pad_push (outpad, buf);
3812 /* combine all stream flows for the data transport */
3813 ret = gst_rtspsrc_combine_flows (src, stream, ret);
3820 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
3821 gst_rtsp_message_unset (&message);
3826 GST_DEBUG_OBJECT (src, "we got an eof from the server");
3827 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3828 ("The server closed the connection."));
3829 src->conninfo.connected = FALSE;
3830 gst_rtsp_message_unset (&message);
3831 return GST_FLOW_EOS;
3835 gst_rtsp_message_unset (&message);
3836 GST_DEBUG_OBJECT (src, "got interrupted: stop connection flush");
3837 gst_rtspsrc_connection_flush (src, FALSE);
3838 return GST_FLOW_FLUSHING;
3842 gchar *str = gst_rtsp_strresult (res);
3844 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
3845 ("Could not receive message. (%s)", str));
3848 gst_rtsp_message_unset (&message);
3849 return GST_FLOW_ERROR;
3851 handle_request_failed:
3853 gchar *str = gst_rtsp_strresult (res);
3855 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
3856 ("Could not handle server message. (%s)", str));
3858 gst_rtsp_message_unset (&message);
3859 return GST_FLOW_ERROR;
3863 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3864 ("Short message received, ignoring."));
3865 gst_rtsp_message_unset (&message);
3870 static GstFlowReturn
3871 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
3874 GstRTSPMessage message = { 0 };
3878 GTimeVal tv_timeout;
3880 /* get the next timeout interval */
3881 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3883 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
3884 (gint) tv_timeout.tv_sec);
3886 gst_rtsp_message_unset (&message);
3888 /* we should continue reading the TCP socket because the server might
3889 * send us requests. When the session timeout expires, we need to send a
3890 * keep-alive request to keep the session open. */
3891 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
3892 &message, &tv_timeout);
3896 GST_DEBUG_OBJECT (src, "we received a server message");
3898 case GST_RTSP_EINTR:
3899 /* we got interrupted, see what we have to do */
3901 case GST_RTSP_ETIMEOUT:
3902 /* send keep-alive, ignore the result, a warning will be posted. */
3903 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
3904 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3908 /* server closed the connection. not very fatal for UDP, reconnect and
3909 * see what happens. */
3910 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3911 ("The server closed the connection."));
3912 if (src->udp_reconnect) {
3914 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
3921 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
3923 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3924 ("Unhandled return value %d.", res));
3928 switch (message.type) {
3929 case GST_RTSP_MESSAGE_REQUEST:
3930 /* server sends us a request message, handle it */
3932 gst_rtspsrc_handle_request (src, src->conninfo.connection,
3934 if (res == GST_RTSP_EEOF)
3937 goto handle_request_failed;
3939 case GST_RTSP_MESSAGE_RESPONSE:
3940 /* we ignore response and data messages */
3941 GST_DEBUG_OBJECT (src, "ignoring response message");
3943 gst_rtsp_message_dump (&message);
3944 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
3945 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
3946 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
3947 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
3948 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3955 case GST_RTSP_MESSAGE_DATA:
3956 /* we ignore response and data messages */
3957 GST_DEBUG_OBJECT (src, "ignoring data message");
3960 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
3966 /* we get here when the connection got interrupted */
3969 gst_rtsp_message_unset (&message);
3970 GST_DEBUG_OBJECT (src, "got interrupted: stop connection flush");
3971 gst_rtspsrc_connection_flush (src, FALSE);
3972 return GST_FLOW_FLUSHING;
3976 gchar *str = gst_rtsp_strresult (res);
3979 src->conninfo.connected = FALSE;
3980 if (res != GST_RTSP_EINTR) {
3981 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
3982 ("Could not connect to server. (%s)", str));
3984 ret = GST_FLOW_ERROR;
3986 ret = GST_FLOW_FLUSHING;
3992 gchar *str = gst_rtsp_strresult (res);
3994 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
3995 ("Could not receive message. (%s)", str));
3997 return GST_FLOW_ERROR;
3999 handle_request_failed:
4001 gchar *str = gst_rtsp_strresult (res);
4004 gst_rtsp_message_unset (&message);
4005 if (res != GST_RTSP_EINTR) {
4006 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4007 ("Could not handle server message. (%s)", str));
4009 ret = GST_FLOW_ERROR;
4011 ret = GST_FLOW_FLUSHING;
4017 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4018 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4019 ("The server closed the connection."));
4020 src->conninfo.connected = FALSE;
4021 gst_rtsp_message_unset (&message);
4022 return GST_FLOW_EOS;
4026 static GstRTSPResult
4027 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4029 GstRTSPResult res = GST_RTSP_OK;
4032 GST_DEBUG_OBJECT (src, "doing reconnect");
4034 GST_OBJECT_LOCK (src);
4035 /* only restart when the pads were not yet activated, else we were
4036 * streaming over UDP */
4037 restart = src->need_activate;
4038 GST_OBJECT_UNLOCK (src);
4040 /* no need to restart, we're done */
4044 /* we can try only TCP now */
4045 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
4047 /* close and cleanup our state */
4048 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
4051 /* see if we have TCP left to try. Also don't try TCP when we were configured
4053 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
4056 /* We post a warning message now to inform the user
4057 * that nothing happened. It's most likely a firewall thing. */
4058 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4059 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4060 "firewall is blocking it. Retrying using a TCP connection.",
4061 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4063 /* open new connection using tcp */
4064 if (gst_rtspsrc_open (src, async) < 0)
4067 /* start playback */
4068 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
4077 src->cur_protocols = 0;
4078 /* no transport possible, post an error and stop */
4079 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4080 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4081 "firewall is blocking it. No other protocols to try.",
4082 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4083 return GST_RTSP_ERROR;
4087 GST_DEBUG_OBJECT (src, "open failed");
4092 GST_DEBUG_OBJECT (src, "play failed");
4098 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
4102 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
4105 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
4108 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
4111 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
4119 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
4123 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
4126 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
4129 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
4132 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4140 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4144 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4147 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4150 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4153 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4161 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4165 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
4168 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4171 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4174 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4182 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4184 if (ret == GST_RTSP_OK)
4185 gst_rtspsrc_loop_complete_cmd (src, cmd);
4186 else if (ret == GST_RTSP_EINTR)
4187 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4189 gst_rtspsrc_loop_error_cmd (src, cmd);
4193 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
4197 /* start new request */
4198 gst_rtspsrc_loop_start_cmd (src, cmd);
4200 GST_DEBUG_OBJECT (src, "sending cmd %d", cmd);
4202 GST_OBJECT_LOCK (src);
4203 old = src->pending_cmd;
4204 if (old == CMD_RECONNECT) {
4205 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
4206 cmd = CMD_RECONNECT;
4208 if (old != CMD_WAIT) {
4209 src->pending_cmd = CMD_WAIT;
4210 GST_OBJECT_UNLOCK (src);
4211 /* cancel previous request */
4212 GST_DEBUG_OBJECT (src, "cancel previous request");
4213 gst_rtspsrc_loop_cancel_cmd (src, old);
4214 GST_OBJECT_LOCK (src);
4216 src->pending_cmd = cmd;
4217 /* interrupt if allowed */
4218 if (src->busy_cmd & mask) {
4219 GST_DEBUG_OBJECT (src, "connection flush busy %d", src->busy_cmd);
4220 gst_rtspsrc_connection_flush (src, TRUE);
4222 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %d", src->busy_cmd);
4225 gst_task_start (src->task);
4226 GST_OBJECT_UNLOCK (src);
4230 gst_rtspsrc_loop (GstRTSPSrc * src)
4234 if (!src->conninfo.connection || !src->conninfo.connected)
4237 if (src->interleaved)
4238 ret = gst_rtspsrc_loop_interleaved (src);
4240 ret = gst_rtspsrc_loop_udp (src);
4242 if (ret != GST_FLOW_OK)
4250 GST_WARNING_OBJECT (src, "we are not connected");
4251 ret = GST_FLOW_FLUSHING;
4256 const gchar *reason = gst_flow_get_name (ret);
4258 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
4259 src->running = FALSE;
4260 if (ret == GST_FLOW_EOS) {
4261 /* perform EOS logic */
4262 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4263 gst_element_post_message (GST_ELEMENT_CAST (src),
4264 gst_message_new_segment_done (GST_OBJECT_CAST (src),
4265 src->segment.format, src->segment.position));
4266 gst_rtspsrc_push_event (src,
4267 gst_event_new_segment_done (src->segment.format,
4268 src->segment.position));
4270 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4272 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
4273 /* for fatal errors we post an error message, post the error before the
4274 * EOS so the app knows about the error first. */
4275 GST_ELEMENT_ERROR (src, STREAM, FAILED,
4276 ("Internal data flow error."),
4277 ("streaming task paused, reason %s (%d)", reason, ret));
4278 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4280 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
4285 #ifndef GST_DISABLE_GST_DEBUG
4286 static const gchar *
4287 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
4291 while (method != 0) {
4308 static const gchar *
4309 gst_rtspsrc_skip_lws (const gchar * s)
4311 while (g_ascii_isspace (*s))
4316 static const gchar *
4317 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
4319 while (s > start && g_ascii_isspace (*(s - 1)))
4324 static const gchar *
4325 gst_rtspsrc_skip_commas (const gchar * s)
4327 /* The grammar allows for multiple commas */
4328 while (g_ascii_isspace (*s) || *s == ',')
4333 static const gchar *
4334 gst_rtspsrc_skip_item (const gchar * s)
4336 gboolean quoted = FALSE;
4337 const gchar *start = s;
4339 /* A list item ends at the last non-whitespace character
4340 * before a comma which is not inside a quoted-string. Or at
4341 * the end of the string.
4347 if (*s == '\\' && *(s + 1))
4356 return gst_rtspsrc_unskip_lws (s, start);
4360 gst_rtsp_decode_quoted_string (gchar * quoted_string)
4364 src = quoted_string + 1;
4365 dst = quoted_string;
4366 while (*src && *src != '"') {
4367 if (*src == '\\' && *(src + 1))
4374 /* Extract the authentication tokens that the server provided for each method
4375 * into an array of structures and give those to the connection object.
4378 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
4379 const gchar * header, gboolean * stale)
4381 GSList *list = NULL, *iter;
4383 gchar *item, *eq, *name_end, *value;
4385 g_return_if_fail (stale != NULL);
4387 gst_rtsp_connection_clear_auth_params (conn);
4390 /* Parse a header whose content is described by RFC2616 as
4391 * "#something", where "something" does not itself contain commas,
4392 * except as part of quoted-strings, into a list of allocated strings.
4394 header = gst_rtspsrc_skip_commas (header);
4396 end = gst_rtspsrc_skip_item (header);
4397 list = g_slist_prepend (list, g_strndup (header, end - header));
4398 header = gst_rtspsrc_skip_commas (end);
4403 list = g_slist_reverse (list);
4404 for (iter = list; iter; iter = iter->next) {
4407 eq = strchr (item, '=');
4409 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
4410 if (name_end == item) {
4411 /* That's no good... */
4418 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
4420 gst_rtsp_decode_quoted_string (value);
4424 if (item && (strcmp (item, "stale") == 0) &&
4425 value && (strcmp (value, "TRUE") == 0))
4427 gst_rtsp_connection_set_auth_param (conn, item, value);
4431 g_slist_free (list);
4434 /* Parse a WWW-Authenticate Response header and determine the
4435 * available authentication methods
4437 * This code should also cope with the fact that each WWW-Authenticate
4438 * header can contain multiple challenge methods + tokens
4440 * At the moment, for Basic auth, we just do a minimal check and don't
4441 * even parse out the realm */
4443 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
4444 GstRTSPConnection * conn, gboolean * stale)
4448 g_return_if_fail (hdr != NULL);
4449 g_return_if_fail (methods != NULL);
4450 g_return_if_fail (stale != NULL);
4452 /* Skip whitespace at the start of the string */
4453 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
4455 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
4456 *methods |= GST_RTSP_AUTH_BASIC;
4457 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
4458 *methods |= GST_RTSP_AUTH_DIGEST;
4459 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
4464 * gst_rtspsrc_setup_auth:
4465 * @src: the rtsp source
4467 * Configure a username and password and auth method on the
4468 * connection object based on a response we received from the
4471 * Currently, this requires that a username and password were supplied
4472 * in the uri. In the future, they may be requested on demand by sending
4473 * a message up the bus.
4475 * Returns: TRUE if authentication information could be set up correctly.
4478 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
4482 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
4483 GstRTSPAuthMethod method;
4484 GstRTSPResult auth_result;
4486 GstRTSPConnection *conn;
4488 gboolean stale = FALSE;
4490 conn = src->conninfo.connection;
4492 /* Identify the available auth methods and see if any are supported */
4493 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
4494 &hdr, 0) == GST_RTSP_OK) {
4495 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
4498 if (avail_methods == GST_RTSP_AUTH_NONE)
4499 goto no_auth_available;
4501 /* For digest auth, if the response indicates that the session
4502 * data are stale, we just update them in the connection object and
4503 * return TRUE to retry the request */
4505 src->tried_url_auth = FALSE;
4507 url = gst_rtsp_connection_get_url (conn);
4509 /* Do we have username and password available? */
4510 if (url != NULL && !src->tried_url_auth && url->user != NULL
4511 && url->passwd != NULL) {
4514 src->tried_url_auth = TRUE;
4515 GST_DEBUG_OBJECT (src,
4516 "Attempting authentication using credentials from the URL");
4518 user = src->user_id;
4519 pass = src->user_pw;
4520 GST_DEBUG_OBJECT (src,
4521 "Attempting authentication using credentials from the properties");
4524 /* FIXME: If the url didn't contain username and password or we tried them
4525 * already, request a username and passwd from the application via some kind
4526 * of credentials request message */
4528 /* If we don't have a username and passwd at this point, bail out. */
4529 if (user == NULL || pass == NULL)
4532 /* Try to configure for each available authentication method, strongest to
4534 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
4535 /* Check if this method is available on the server */
4536 if ((method & avail_methods) == 0)
4539 /* Pass the credentials to the connection to try on the next request */
4540 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
4541 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
4542 * ignore it and end up retrying later */
4543 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
4544 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
4545 gst_rtsp_auth_method_to_string (method));
4550 if (method == GST_RTSP_AUTH_NONE)
4551 goto no_auth_available;
4557 /* Output an error indicating that we couldn't connect because there were
4558 * no supported authentication protocols */
4559 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4560 ("No supported authentication protocol was found"));
4565 /* We don't fire an error message, we just return FALSE and let the
4566 * normal NOT_AUTHORIZED error be propagated */
4571 static GstRTSPResult
4572 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4573 GstRTSPMessage * request, GstRTSPMessage * response,
4574 GstRTSPStatusCode * code)
4577 GstRTSPStatusCode thecode;
4578 gchar *content_base = NULL;
4582 if (!src->short_header)
4583 gst_rtsp_ext_list_before_send (src->extensions, request);
4585 GST_DEBUG_OBJECT (src, "sending message");
4588 gst_rtsp_message_dump (request);
4590 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
4594 gst_rtsp_connection_reset_timeout (conn);
4597 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
4602 gst_rtsp_message_dump (response);
4604 switch (response->type) {
4605 case GST_RTSP_MESSAGE_REQUEST:
4606 res = gst_rtspsrc_handle_request (src, conn, response);
4607 if (res == GST_RTSP_EEOF)
4610 goto handle_request_failed;
4612 case GST_RTSP_MESSAGE_RESPONSE:
4613 /* ok, a response is good */
4614 GST_DEBUG_OBJECT (src, "received response message");
4616 case GST_RTSP_MESSAGE_DATA:
4617 /* get next response */
4618 GST_DEBUG_OBJECT (src, "ignoring data response message");
4621 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4626 thecode = response->type_data.response.code;
4628 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
4630 /* if the caller wanted the result code, we store it. */
4634 /* If the request didn't succeed, bail out before doing any more */
4635 if (thecode != GST_RTSP_STS_OK)
4638 /* store new content base if any */
4639 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
4642 g_free (src->content_base);
4643 src->content_base = g_strdup (content_base);
4645 gst_rtsp_ext_list_after_send (src->extensions, request, response);
4652 gchar *str = gst_rtsp_strresult (res);
4654 if (res != GST_RTSP_EINTR) {
4655 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4656 ("Could not send message. (%s)", str));
4658 GST_WARNING_OBJECT (src, "send interrupted");
4667 GST_WARNING_OBJECT (src, "server closed connection");
4668 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
4670 /* if reconnect succeeds, try again */
4672 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
4676 /* only try once after reconnect, then fallthrough and error out */
4679 gchar *str = gst_rtsp_strresult (res);
4681 if (res != GST_RTSP_EINTR) {
4682 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4683 ("Could not receive message. (%s)", str));
4685 GST_WARNING_OBJECT (src, "receive interrupted");
4693 handle_request_failed:
4695 /* ERROR was posted */
4696 gst_rtsp_message_unset (response);
4701 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4702 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4703 ("The server closed the connection."));
4704 gst_rtsp_message_unset (response);
4711 * @src: the rtsp source
4712 * @conn: the connection to send on
4713 * @request: must point to a valid request
4714 * @response: must point to an empty #GstRTSPMessage
4715 * @code: an optional code result
4717 * send @request and retrieve the response in @response. optionally @code can be
4718 * non-NULL in which case it will contain the status code of the response.
4720 * If This function returns #GST_RTSP_OK, @response will contain a valid response
4721 * message that should be cleaned with gst_rtsp_message_unset() after usage.
4723 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
4724 * @response message) if the response code was not 200 (OK).
4726 * If the attempt results in an authentication failure, then this will attempt
4727 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
4730 * Returns: #GST_RTSP_OK if the processing was successful.
4732 static GstRTSPResult
4733 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4734 GstRTSPMessage * request, GstRTSPMessage * response,
4735 GstRTSPStatusCode * code)
4737 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
4738 GstRTSPResult res = GST_RTSP_ERROR;
4741 GstRTSPMethod method = GST_RTSP_INVALID;
4747 /* make sure we don't loop forever */
4751 /* save method so we can disable it when the server complains */
4752 method = request->type_data.request.method;
4755 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
4759 case GST_RTSP_STS_UNAUTHORIZED:
4760 if (gst_rtspsrc_setup_auth (src, response)) {
4761 /* Try the request/response again after configuring the auth info
4769 } while (retry == TRUE);
4771 /* If the user requested the code, let them handle errors, otherwise
4772 * post an error below */
4775 else if (int_code != GST_RTSP_STS_OK)
4776 goto error_response;
4783 GST_DEBUG_OBJECT (src, "got error %d", res);
4788 res = GST_RTSP_ERROR;
4790 switch (response->type_data.response.code) {
4791 case GST_RTSP_STS_NOT_FOUND:
4792 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
4793 response->type_data.response.reason));
4795 case GST_RTSP_STS_MOVED_PERMANENTLY:
4796 case GST_RTSP_STS_MOVE_TEMPORARILY:
4798 gchar *new_location;
4799 GstRTSPLowerTrans transports;
4801 GST_DEBUG_OBJECT (src, "got redirection");
4802 /* if we don't have a Location Header, we must error */
4803 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
4804 &new_location, 0) < 0)
4807 /* When we receive a redirect result, we go back to the INIT state after
4808 * parsing the new URI. The caller should do the needed steps to issue
4809 * a new setup when it detects this state change. */
4810 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
4812 /* save current transports */
4813 if (src->conninfo.url)
4814 transports = src->conninfo.url->transports;
4816 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
4818 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
4820 /* set old transports */
4821 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
4822 src->conninfo.url->transports = transports;
4824 src->need_redirect = TRUE;
4825 src->state = GST_RTSP_STATE_INIT;
4829 case GST_RTSP_STS_NOT_ACCEPTABLE:
4830 case GST_RTSP_STS_NOT_IMPLEMENTED:
4831 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
4832 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
4833 gst_rtsp_method_as_text (method));
4834 src->methods &= ~method;
4838 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4839 ("Got error response: %d (%s).", response->type_data.response.code,
4840 response->type_data.response.reason));
4843 /* if we return ERROR we should unset the response ourselves */
4844 if (res == GST_RTSP_ERROR)
4845 gst_rtsp_message_unset (response);
4851 static GstRTSPResult
4852 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
4853 GstRTSPMessage * response, GstRTSPSrc * src)
4855 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
4860 /* parse the response and collect all the supported methods. We need this
4861 * information so that we don't try to send an unsupported request to the
4865 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
4867 GstRTSPHeaderField field;
4871 /* reset supported methods */
4874 /* Try Allow Header first */
4875 field = GST_RTSP_HDR_ALLOW;
4878 gst_rtsp_message_get_header (response, field, &respoptions, indx);
4879 if (indx == 0 && !respoptions) {
4880 /* if no Allow header was found then try the Public header... */
4881 field = GST_RTSP_HDR_PUBLIC;
4882 gst_rtsp_message_get_header (response, field, &respoptions, indx);
4887 src->methods |= gst_rtsp_options_from_text (respoptions);
4892 if (src->methods == 0) {
4893 /* neither Allow nor Public are required, assume the server supports
4894 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
4896 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
4897 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
4899 /* always assume PLAY, FIXME, extensions should be able to override
4901 src->methods |= GST_RTSP_PLAY;
4902 /* also assume it will support Range */
4903 src->seekable = TRUE;
4905 /* we need describe and setup */
4906 if (!(src->methods & GST_RTSP_DESCRIBE))
4908 if (!(src->methods & GST_RTSP_SETUP))
4916 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4917 ("Server does not support DESCRIBE."));
4922 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4923 ("Server does not support SETUP."));
4928 /* masks to be kept in sync with the hardcoded protocol order of preference
4930 static guint protocol_masks[] = {
4931 GST_RTSP_LOWER_TRANS_UDP,
4932 GST_RTSP_LOWER_TRANS_UDP_MCAST,
4933 GST_RTSP_LOWER_TRANS_TCP,
4937 static GstRTSPResult
4938 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
4939 GstRTSPLowerTrans protocols, gchar ** transports)
4943 gboolean add_udp_str;
4948 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
4953 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
4955 /* extension listed transports, use those */
4956 if (*transports != NULL)
4959 /* it's the default */
4960 add_udp_str = FALSE;
4962 /* the default RTSP transports */
4963 result = g_string_new ("");
4964 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
4965 GST_DEBUG_OBJECT (src, "adding UDP unicast");
4967 g_string_append (result, "RTP/AVP");
4969 g_string_append (result, "/UDP");
4970 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
4971 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
4972 GST_DEBUG_OBJECT (src, "adding UDP multicast");
4974 /* we don't have to allocate any UDP ports yet, if the selected transport
4975 * turns out to be multicast we can create them and join the multicast
4976 * group indicated in the transport reply */
4977 if (result->len > 0)
4978 g_string_append (result, ",");
4979 g_string_append (result, "RTP/AVP");
4981 g_string_append (result, "/UDP");
4982 g_string_append (result, ";multicast");
4983 if (src->next_port_num != 0) {
4984 if (src->client_port_range.max > 0 &&
4985 src->next_port_num >= src->client_port_range.max)
4988 g_string_append_printf (result, ";client_port=%d-%d",
4989 src->next_port_num, src->next_port_num + 1);
4991 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
4992 GST_DEBUG_OBJECT (src, "adding TCP");
4994 if (result->len > 0)
4995 g_string_append (result, ",");
4996 g_string_append (result, "RTP/AVP/TCP;unicast;interleaved=%%i1-%%i2");
4998 *transports = g_string_free (result, FALSE);
5000 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5007 GST_ERROR ("extension gave error %d", res);
5012 GST_ERROR ("no more ports available");
5013 return GST_RTSP_ERROR;
5017 static GstRTSPResult
5018 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5019 gint orig_rtpport, gint orig_rtcpport)
5022 gint nr_udp, nr_int;
5024 gint rtpport = 0, rtcpport = 0;
5027 src = stream->parent;
5029 /* find number of placeholders first */
5030 if (strstr (*transports, "%%i2"))
5032 else if (strstr (*transports, "%%i1"))
5037 if (strstr (*transports, "%%u2"))
5039 else if (strstr (*transports, "%%u1"))
5044 if (nr_udp == 0 && nr_int == 0)
5048 if (!orig_rtpport || !orig_rtcpport) {
5049 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5052 rtpport = orig_rtpport;
5053 rtcpport = orig_rtcpport;
5057 str = g_string_new ("");
5059 while ((next = strstr (p, "%%"))) {
5060 g_string_append_len (str, p, next - p);
5061 if (next[2] == 'u') {
5063 g_string_append_printf (str, "%d", rtpport);
5064 else if (next[3] == '2')
5065 g_string_append_printf (str, "%d", rtcpport);
5067 if (next[2] == 'i') {
5069 g_string_append_printf (str, "%d", src->free_channel);
5070 else if (next[3] == '2')
5071 g_string_append_printf (str, "%d", src->free_channel + 1);
5076 /* append final part */
5077 g_string_append (str, p);
5079 g_free (*transports);
5080 *transports = g_string_free (str, FALSE);
5088 GST_ERROR ("failed to allocate udp ports");
5089 return GST_RTSP_ERROR;
5094 gst_rtspsrc_stream_is_real_media (GstRTSPStream * stream)
5096 gboolean res = FALSE;
5100 const gchar *enc = NULL;
5102 s = gst_caps_get_structure (stream->caps, 0);
5103 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
5104 res = (strstr (enc, "-REAL") != NULL);
5110 /* Perform the SETUP request for all the streams.
5112 * We ask the server for a specific transport, which initially includes all the
5113 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
5114 * two local UDP ports that we send to the server.
5116 * Once the server replied with a transport, we configure the other streams
5117 * with the same transport.
5119 * This function will also configure the stream for the selected transport,
5120 * which basically means creating the pipeline.
5122 static GstRTSPResult
5123 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
5126 GstRTSPResult res = GST_RTSP_ERROR;
5127 GstRTSPMessage request = { 0 };
5128 GstRTSPMessage response = { 0 };
5129 GstRTSPStream *stream = NULL;
5130 GstRTSPLowerTrans protocols;
5131 GstRTSPStatusCode code;
5132 gboolean unsupported_real = FALSE;
5133 gint rtpport, rtcpport;
5137 if (src->conninfo.connection) {
5138 url = gst_rtsp_connection_get_url (src->conninfo.connection);
5139 /* we initially allow all configured lower transports. based on the URL
5140 * transports and the replies from the server we narrow them down. */
5141 protocols = url->transports & src->cur_protocols;
5144 protocols = src->cur_protocols;
5150 /* reset some state */
5151 src->free_channel = 0;
5152 src->interleaved = FALSE;
5153 src->need_activate = FALSE;
5154 /* keep track of next port number, 0 is random */
5155 src->next_port_num = src->client_port_range.min;
5156 rtpport = rtcpport = 0;
5158 if (G_UNLIKELY (src->streams == NULL))
5161 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5162 GstRTSPConnection *conn;
5167 stream = (GstRTSPStream *) walk->data;
5169 /* see if we need to configure this stream */
5170 if (!gst_rtsp_ext_list_configure_stream (src->extensions, stream->caps)) {
5171 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
5173 stream->disabled = TRUE;
5177 /* merge/overwrite global caps */
5182 s = gst_caps_get_structure (stream->caps, 0);
5184 num = gst_structure_n_fields (src->props);
5185 for (j = 0; j < num; j++) {
5189 name = gst_structure_nth_field_name (src->props, j);
5190 val = gst_structure_get_value (src->props, name);
5191 gst_structure_set_value (s, name, val);
5193 GST_DEBUG_OBJECT (src, "copied %s", name);
5197 /* skip setup if we have no URL for it */
5198 if (stream->conninfo.location == NULL) {
5199 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
5203 if (src->conninfo.connection == NULL) {
5204 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
5205 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
5208 conn = stream->conninfo.connection;
5210 conn = src->conninfo.connection;
5212 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
5213 stream->conninfo.location);
5215 /* if we have a multicast connection, only suggest multicast from now on */
5216 if (stream->is_multicast)
5217 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
5220 /* first selectable protocol */
5221 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5223 if (!protocol_masks[mask])
5227 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
5228 protocol_masks[mask]);
5229 /* create a string with first transport in line */
5231 res = gst_rtspsrc_create_transports_string (src,
5232 protocols & protocol_masks[mask], &transports);
5233 if (res < 0 || transports == NULL)
5234 goto setup_transport_failed;
5236 if (strlen (transports) == 0) {
5237 g_free (transports);
5238 GST_DEBUG_OBJECT (src, "no transports found");
5243 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
5245 /* replace placeholders with real values, this function will optionally
5246 * allocate UDP ports and other info needed to execute the setup request */
5247 res = gst_rtspsrc_prepare_transports (stream, &transports,
5248 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
5250 g_free (transports);
5251 goto setup_transport_failed;
5254 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
5256 /* create SETUP request */
5258 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
5259 stream->conninfo.location);
5261 g_free (transports);
5262 goto create_request_failed;
5265 /* select transport, copy is made when adding to header so we can free it. */
5266 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
5267 g_free (transports);
5269 /* if the user wants a non default RTP packet size we add the blocksize
5271 if (src->rtp_blocksize > 0) {
5272 hval = g_strdup_printf ("%d", src->rtp_blocksize);
5273 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
5278 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
5281 /* handle the code ourselves */
5282 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
5286 case GST_RTSP_STS_OK:
5288 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
5289 gst_rtsp_message_unset (&request);
5290 gst_rtsp_message_unset (&response);
5291 /* cleanup of leftover transport */
5292 gst_rtspsrc_stream_free_udp (stream);
5293 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
5294 * we might be in this case */
5295 if (stream->container && rtpport && rtcpport && !retry) {
5296 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
5301 /* this transport did not go down well, but we may have others to try
5302 * that we did not send yet, try those and only give up then
5303 * but not without checking for lost cause/extension so we can
5304 * post a nicer/more useful error message later */
5305 if (!unsupported_real)
5306 unsupported_real = gst_rtspsrc_stream_is_real_media (stream);
5307 /* select next available protocol, give up on this stream if none */
5309 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5311 if (!protocol_masks[mask] || unsupported_real)
5316 /* cleanup of leftover transport and move to the next stream */
5317 gst_rtspsrc_stream_free_udp (stream);
5318 goto response_error;
5321 /* parse response transport */
5323 gchar *resptrans = NULL;
5324 GstRTSPTransport transport = { 0 };
5326 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
5329 gst_rtspsrc_stream_free_udp (stream);
5333 /* parse transport, go to next stream on parse error */
5334 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
5335 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
5339 /* update allowed transports for other streams. once the transport of
5340 * one stream has been determined, we make sure that all other streams
5341 * are configured in the same way */
5342 switch (transport.lower_transport) {
5343 case GST_RTSP_LOWER_TRANS_TCP:
5344 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
5345 protocols = GST_RTSP_LOWER_TRANS_TCP;
5346 src->interleaved = TRUE;
5347 /* update free channels */
5349 MAX (transport.interleaved.min, src->free_channel);
5351 MAX (transport.interleaved.max, src->free_channel);
5352 src->free_channel++;
5354 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
5355 /* only allow multicast for other streams */
5356 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
5357 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
5358 /* if the server selected our ports, increment our counters so that
5359 * we select a new port later */
5360 if (src->next_port_num == transport.port.min &&
5361 src->next_port_num + 1 == transport.port.max) {
5362 src->next_port_num += 2;
5365 case GST_RTSP_LOWER_TRANS_UDP:
5366 /* only allow unicast for other streams */
5367 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
5368 protocols = GST_RTSP_LOWER_TRANS_UDP;
5371 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
5372 transport.lower_transport);
5376 if (!stream->container || (!src->interleaved && !retry)) {
5377 /* now configure the stream with the selected transport */
5378 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
5379 GST_DEBUG_OBJECT (src,
5380 "could not configure stream %p transport, skipping stream",
5383 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
5384 /* retain the first allocated UDP port pair */
5385 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
5386 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
5389 /* we need to activate at least one streams when we detect activity */
5390 src->need_activate = TRUE;
5392 /* clean up our transport struct */
5393 gst_rtsp_transport_init (&transport);
5394 /* clean up used RTSP messages */
5395 gst_rtsp_message_unset (&request);
5396 gst_rtsp_message_unset (&response);
5400 /* store the transport protocol that was configured */
5401 src->cur_protocols = protocols;
5403 gst_rtsp_ext_list_stream_select (src->extensions, url);
5405 /* if there is nothing to activate, error out */
5406 if (!src->need_activate)
5407 goto nothing_to_activate;
5414 /* no transport possible, post an error and stop */
5415 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5416 ("Could not connect to server, no protocols left"));
5417 return GST_RTSP_ERROR;
5421 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5422 ("SDP contains no streams"));
5423 return GST_RTSP_ERROR;
5425 create_request_failed:
5427 gchar *str = gst_rtsp_strresult (res);
5429 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5430 ("Could not create request. (%s)", str));
5434 setup_transport_failed:
5436 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5437 ("Could not setup transport."));
5438 res = GST_RTSP_ERROR;
5443 const gchar *str = gst_rtsp_status_as_text (code);
5445 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5446 ("Error (%d): %s", code, GST_STR_NULL (str)));
5447 res = GST_RTSP_ERROR;
5452 gchar *str = gst_rtsp_strresult (res);
5454 if (res != GST_RTSP_EINTR) {
5455 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5456 ("Could not send message. (%s)", str));
5458 GST_WARNING_OBJECT (src, "send interrupted");
5465 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5466 ("Server did not select transport."));
5467 res = GST_RTSP_ERROR;
5470 nothing_to_activate:
5472 /* none of the available error codes is really right .. */
5473 if (unsupported_real) {
5474 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5475 (_("No supported stream was found. You might need to install a "
5476 "GStreamer RTSP extension plugin for Real media streams.")),
5479 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5480 (_("No supported stream was found. You might need to allow "
5481 "more transport protocols or may otherwise be missing "
5482 "the right GStreamer RTSP extension plugin.")), (NULL));
5484 return GST_RTSP_ERROR;
5488 gst_rtsp_message_unset (&request);
5489 gst_rtsp_message_unset (&response);
5495 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
5496 GstSegment * segment)
5499 GstRTSPTimeRange *therange;
5502 gst_rtsp_range_free (src->range);
5504 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
5505 GST_DEBUG_OBJECT (src, "parsed range %s", range);
5506 src->range = therange;
5508 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
5510 gst_segment_init (segment, GST_FORMAT_TIME);
5514 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
5515 therange->min.type, therange->min.seconds, therange->max.type,
5516 therange->max.seconds);
5518 if (therange->min.type == GST_RTSP_TIME_NOW)
5520 else if (therange->min.type == GST_RTSP_TIME_END)
5523 seconds = therange->min.seconds * GST_SECOND;
5525 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
5526 GST_TIME_ARGS (seconds));
5528 /* we need to start playback without clipping from the position reported by
5530 segment->start = seconds;
5531 segment->position = seconds;
5533 if (therange->max.type == GST_RTSP_TIME_NOW)
5535 else if (therange->max.type == GST_RTSP_TIME_END)
5538 seconds = therange->max.seconds * GST_SECOND;
5540 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
5541 GST_TIME_ARGS (seconds));
5543 /* live (WMS) server might send overflowed large max as its idea of infinity,
5544 * compensate to prevent problems later on */
5545 if (seconds != -1 && seconds < 0) {
5547 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
5550 /* live (WMS) might send min == max, which is not worth recording */
5551 if (segment->duration == -1 && seconds == segment->start)
5554 /* don't change duration with unknown value, we might have a valid value
5555 * there that we want to keep. */
5557 segment->duration = seconds;
5562 /* must be called with the RTSP state lock */
5563 static GstRTSPResult
5564 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
5570 /* prepare global stream caps properties */
5572 gst_structure_remove_all_fields (src->props);
5574 src->props = gst_structure_new_empty ("RTSPProperties");
5577 gst_sdp_message_dump (sdp);
5579 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
5581 gst_segment_init (&src->segment, GST_FORMAT_TIME);
5583 /* parse range for duration reporting. */
5588 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
5592 /* keep track of the range and configure it in the segment */
5593 if (gst_rtspsrc_parse_range (src, range, &src->segment))
5597 /* try to find a global control attribute. Note that a '*' means that we should
5598 * do aggregate control with the current url (so we don't do anything and
5599 * leave the current connection as is) */
5601 const gchar *control;
5604 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
5605 if (control == NULL)
5608 /* only take fully qualified urls */
5609 if (g_str_has_prefix (control, "rtsp://"))
5613 g_free (src->conninfo.location);
5614 src->conninfo.location = g_strdup (control);
5615 /* make a connection for this, if there was a connection already, nothing
5617 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
5618 GST_ERROR_OBJECT (src, "could not connect");
5621 /* we need to keep the control url separate from the connection url because
5622 * the rules for constructing the media control url need it */
5623 g_free (src->control);
5624 src->control = g_strdup (control);
5627 /* create streams */
5628 n_streams = gst_sdp_message_medias_len (sdp);
5629 for (i = 0; i < n_streams; i++) {
5630 gst_rtspsrc_create_stream (src, sdp, i);
5633 src->state = GST_RTSP_STATE_INIT;
5636 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
5639 /* reset our state */
5640 src->need_range = TRUE;
5643 src->state = GST_RTSP_STATE_READY;
5650 GST_ERROR_OBJECT (src, "setup failed");
5651 gst_rtspsrc_cleanup (src);
5656 static GstRTSPResult
5657 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
5661 GstRTSPMessage request = { 0 };
5662 GstRTSPMessage response = { 0 };
5665 gchar *respcont = NULL;
5668 src->need_redirect = FALSE;
5670 /* can't continue without a valid url */
5671 if (G_UNLIKELY (src->conninfo.url == NULL)) {
5672 res = GST_RTSP_EINVAL;
5675 src->tried_url_auth = FALSE;
5677 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
5678 goto connect_failed;
5680 /* create OPTIONS */
5681 GST_DEBUG_OBJECT (src, "create options...");
5683 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
5684 src->conninfo.url_str);
5686 goto create_request_failed;
5689 GST_DEBUG_OBJECT (src, "send options...");
5692 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
5695 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
5700 if (!gst_rtspsrc_parse_methods (src, &response))
5703 /* create DESCRIBE */
5704 GST_DEBUG_OBJECT (src, "create describe...");
5706 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
5707 src->conninfo.url_str);
5709 goto create_request_failed;
5711 /* we only accept SDP for now */
5712 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
5716 GST_DEBUG_OBJECT (src, "send describe...");
5719 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
5722 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
5726 /* we only perform redirect for the describe, currently */
5727 if (src->need_redirect) {
5728 /* close connection, we don't have to send a TEARDOWN yet, ignore the
5730 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5732 gst_rtsp_message_unset (&request);
5733 gst_rtsp_message_unset (&response);
5739 /* it could be that the DESCRIBE method was not implemented */
5740 if (!src->methods & GST_RTSP_DESCRIBE)
5743 /* check if reply is SDP */
5744 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
5746 /* could not be set but since the request returned OK, we assume it
5747 * was SDP, else check it. */
5749 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
5750 goto wrong_content_type;
5753 /* get message body and parse as SDP */
5754 gst_rtsp_message_get_body (&response, &data, &size);
5755 if (data == NULL || size == 0)
5758 GST_DEBUG_OBJECT (src, "parse SDP...");
5759 gst_sdp_message_new (sdp);
5760 gst_sdp_message_parse_buffer (data, size, *sdp);
5762 /* clean up any messages */
5763 gst_rtsp_message_unset (&request);
5764 gst_rtsp_message_unset (&response);
5771 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
5772 ("No valid RTSP URL was provided"));
5777 gchar *str = gst_rtsp_strresult (res);
5779 if (res != GST_RTSP_EINTR) {
5780 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
5781 ("Failed to connect. (%s)", str));
5783 GST_WARNING_OBJECT (src, "connect interrupted");
5788 create_request_failed:
5790 gchar *str = gst_rtsp_strresult (res);
5792 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5793 ("Could not create request. (%s)", str));
5799 /* Don't post a message - the rtsp_send method will have
5800 * taken care of it because we passed NULL for the response code */
5805 /* error was posted */
5806 res = GST_RTSP_ERROR;
5811 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5812 ("Server does not support SDP, got %s.", respcont));
5813 res = GST_RTSP_ERROR;
5818 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5819 ("Server can not provide an SDP."));
5820 res = GST_RTSP_ERROR;
5825 if (src->conninfo.connection) {
5826 GST_DEBUG_OBJECT (src, "free connection");
5827 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5829 gst_rtsp_message_unset (&request);
5830 gst_rtsp_message_unset (&response);
5835 static GstRTSPResult
5836 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
5841 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
5843 if (src->sdp == NULL) {
5844 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
5848 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
5853 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
5860 GST_WARNING_OBJECT (src, "can't get sdp");
5861 src->open_error = TRUE;
5866 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
5867 src->open_error = TRUE;
5872 static GstRTSPResult
5873 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
5875 GstRTSPMessage request = { 0 };
5876 GstRTSPMessage response = { 0 };
5877 GstRTSPResult res = GST_RTSP_OK;
5881 GST_DEBUG_OBJECT (src, "TEARDOWN...");
5883 if (src->state < GST_RTSP_STATE_READY) {
5884 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
5891 /* construct a control url */
5893 control = src->control;
5895 control = src->conninfo.url_str;
5897 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
5900 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5901 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5903 GstRTSPConnInfo *info;
5905 /* try aggregate control first but do non-aggregate control otherwise */
5907 setup_url = control;
5908 else if ((setup_url = stream->conninfo.location) == NULL)
5911 if (src->conninfo.connection) {
5912 info = &src->conninfo;
5913 } else if (stream->conninfo.connection) {
5914 info = &stream->conninfo;
5918 if (!info->connected)
5923 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
5925 goto create_request_failed;
5928 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
5931 gst_rtspsrc_send (src, info->connection, &request, &response,
5935 /* FIXME, parse result? */
5936 gst_rtsp_message_unset (&request);
5937 gst_rtsp_message_unset (&response);
5940 /* early exit when we did aggregate control */
5946 /* close connections */
5947 GST_DEBUG_OBJECT (src, "closing connection...");
5948 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
5949 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5950 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
5951 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
5955 gst_rtspsrc_cleanup (src);
5957 src->state = GST_RTSP_STATE_INVALID;
5960 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
5965 create_request_failed:
5967 gchar *str = gst_rtsp_strresult (res);
5969 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5970 ("Could not create request. (%s)", str));
5976 gchar *str = gst_rtsp_strresult (res);
5978 gst_rtsp_message_unset (&request);
5979 if (res != GST_RTSP_EINTR) {
5980 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5981 ("Could not send message. (%s)", str));
5983 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
5990 GST_DEBUG_OBJECT (src,
5991 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
5996 /* RTP-Info is of the format:
5998 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
6000 * rtptime corresponds to the timestamp for the NPT time given in the header
6001 * seqbase corresponds to the next sequence number we received. This number
6002 * indicates the first seqnum after the seek and should be used to discard
6003 * packets that are from before the seek.
6006 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
6011 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
6013 infos = g_strsplit (rtpinfo, ",", 0);
6014 for (i = 0; infos[i]; i++) {
6016 GstRTSPStream *stream;
6020 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
6022 /* init values, types of seqbase and timebase are bigger than needed so we
6023 * can store -1 as uninitialized values */
6028 /* parse url, find stream for url.
6029 * parse seq and rtptime. The seq number should be configured in the rtp
6030 * depayloader or session manager to detect gaps. Same for the rtptime, it
6031 * should be used to create an initial time newsegment. */
6032 fields = g_strsplit (infos[i], ";", 0);
6033 for (j = 0; fields[j]; j++) {
6034 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
6035 /* remove leading whitespace */
6036 fields[j] = g_strchug (fields[j]);
6037 if (g_str_has_prefix (fields[j], "url=")) {
6038 /* get the url and the stream */
6040 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
6041 } else if (g_str_has_prefix (fields[j], "seq=")) {
6042 seqbase = atoi (fields[j] + 4);
6043 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
6044 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
6047 g_strfreev (fields);
6048 /* now we need to store the values for the caps of the stream */
6049 if (stream != NULL) {
6050 GST_DEBUG_OBJECT (src,
6051 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
6052 stream, seqbase, timebase);
6054 /* we have a stream, configure detected params */
6055 stream->seqbase = seqbase;
6056 stream->timebase = timebase;
6065 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
6070 interval = strtoul (rtcp, NULL, 10);
6071 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
6076 interval *= GST_MSECOND;
6078 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6079 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6081 /* already (optionally) retrieved this when configuring manager */
6082 if (stream->session) {
6083 GObject *rtpsession = stream->session;
6085 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
6087 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
6091 /* now it happens that (Xenon) server sending this may also provide bogus
6092 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
6093 * and just use RTP-Info to sync */
6095 GObjectClass *klass;
6097 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
6098 if (g_object_class_find_property (klass, "rtcp-sync")) {
6099 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
6100 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
6106 gst_rtspsrc_get_float (const gchar * dstr)
6108 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6110 /* canonicalise floating point string so we can handle float strings
6111 * in the form "24.930" or "24,930" irrespective of the current locale */
6112 g_strlcpy (s, dstr, sizeof (s));
6113 g_strdelimit (s, ",", '.');
6114 return g_ascii_strtod (s, NULL);
6118 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
6120 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6122 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
6123 g_strlcpy (val_str, "now", sizeof (val_str));
6125 if (segment->position == 0) {
6126 g_strlcpy (val_str, "0", sizeof (val_str));
6128 g_ascii_dtostr (val_str, sizeof (val_str),
6129 ((gdouble) segment->position) / GST_SECOND);
6132 return g_strdup_printf ("npt=%s-", val_str);
6136 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
6138 stream->timebase = -1;
6139 stream->seqbase = -1;
6143 stream->caps = gst_caps_make_writable (stream->caps);
6144 s = gst_caps_get_structure (stream->caps, 0);
6145 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
6149 static GstRTSPResult
6150 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
6152 GstRTSPResult res = GST_RTSP_OK;
6154 if (src->state < GST_RTSP_STATE_READY) {
6155 res = GST_RTSP_ERROR;
6156 if (src->open_error) {
6157 GST_DEBUG_OBJECT (src, "the stream was in error");
6161 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
6163 if ((res = gst_rtspsrc_open (src, async)) < 0) {
6164 GST_DEBUG_OBJECT (src, "failed to open stream");
6173 static GstRTSPResult
6174 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
6176 GstRTSPMessage request = { 0 };
6177 GstRTSPMessage response = { 0 };
6178 GstRTSPResult res = GST_RTSP_OK;
6184 GST_DEBUG_OBJECT (src, "PLAY...");
6186 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6189 if (!(src->methods & GST_RTSP_PLAY))
6192 if (src->state == GST_RTSP_STATE_PLAYING)
6195 if (!src->conninfo.connection || !src->conninfo.connected)
6198 /* send some dummy packets before we activate the receive in the
6200 gst_rtspsrc_send_dummy_packets (src);
6202 /* activate receive elements;
6203 * only in async case, since receive elements may not have been affected
6204 * by overall state change (e.g. not around yet),
6205 * do not mess with state in sync case (e.g. seeking) */
6207 /* state change might be happening in the application thread. A
6208 * specific case is when chaging state to NULL where we will wait
6209 * for this task to finish (gst_rtspsrc_stop). However this task
6210 * will try to change the state to PLAYING causing a deadlock. */
6212 /* make sure we are not in the middle of a state change. The
6213 * state lock is a recursive lock so it's safe to lock twice from
6214 * the same thread */
6215 if (GST_STATE_TRYLOCK (src)) {
6216 gst_element_set_state (GST_ELEMENT_CAST (src), GST_STATE_PLAYING);
6217 GST_STATE_UNLOCK (src);
6219 res = GST_RTSP_ERROR;
6220 goto changing_state;
6224 /* construct a control url */
6226 control = src->control;
6228 control = src->conninfo.url_str;
6230 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6231 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6233 GstRTSPConnection *conn;
6235 /* try aggregate control first but do non-aggregate control otherwise */
6237 setup_url = control;
6238 else if ((setup_url = stream->conninfo.location) == NULL)
6241 if (src->conninfo.connection) {
6242 conn = src->conninfo.connection;
6243 } else if (stream->conninfo.connection) {
6244 conn = stream->conninfo.connection;
6250 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
6252 goto create_request_failed;
6254 if (src->need_range) {
6255 hval = gen_range_header (src, segment);
6257 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_RANGE, hval);
6260 /* store the newsegment event so it can be sent from the streaming thread. */
6261 if (src->start_segment)
6262 gst_event_unref (src->start_segment);
6263 src->start_segment = gst_event_new_segment (&src->segment);
6266 if (segment->rate != 1.0) {
6267 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
6269 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
6271 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
6273 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
6277 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
6279 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6282 /* seek may have silently failed as it is not supported */
6283 if (!(src->methods & GST_RTSP_PLAY)) {
6284 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
6285 /* obviously it is supported as we made it here */
6286 src->methods |= GST_RTSP_PLAY;
6287 src->seekable = FALSE;
6288 /* but there is nothing to parse in the response,
6289 * so convey we have no idea and not to expect anything particular */
6290 clear_rtp_base (src, stream);
6294 /* need to do for all streams */
6295 for (run = src->streams; run; run = g_list_next (run))
6296 clear_rtp_base (src, (GstRTSPStream *) run->data);
6298 /* NOTE the above also disables npt based eos detection */
6299 /* and below forces position to 0,
6300 * which is visible feedback we lost the plot */
6301 segment->start = segment->position = src->last_pos;
6304 gst_rtsp_message_unset (&request);
6306 /* parse RTP npt field. This is the current position in the stream (Normal
6307 * Play Time) and should be put in the NEWSEGMENT position field. */
6308 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
6310 gst_rtspsrc_parse_range (src, hval, segment);
6312 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
6313 segment->rate = 1.0;
6315 /* parse Speed header. This is the intended playback rate of the stream
6316 * and should be put in the NEWSEGMENT rate field. */
6317 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
6318 0) == GST_RTSP_OK) {
6319 segment->rate = gst_rtspsrc_get_float (hval);
6320 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
6321 &hval, 0) == GST_RTSP_OK) {
6322 segment->rate = gst_rtspsrc_get_float (hval);
6325 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
6326 * for the RTP packets. If this is not present, we assume all starts from 0...
6327 * This is info for the RTP session manager that we pass to it in caps. */
6329 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
6330 &hval, hval_idx++) == GST_RTSP_OK)
6331 gst_rtspsrc_parse_rtpinfo (src, hval);
6333 /* some servers indicate RTCP parameters in PLAY response,
6334 * rather than properly in SDP */
6335 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
6336 &hval, 0) == GST_RTSP_OK)
6337 gst_rtspsrc_handle_rtcp_interval (src, hval);
6339 gst_rtsp_message_unset (&response);
6341 /* early exit when we did aggregate control */
6345 /* configure the caps of the streams after we parsed all headers. Only reset
6346 * the manager object when we set a new Range header (we did a seek) */
6347 gst_rtspsrc_configure_caps (src, segment, src->need_range);
6349 /* set again when needed */
6350 src->need_range = FALSE;
6352 src->running = TRUE;
6353 src->base_time = -1;
6354 src->state = GST_RTSP_STATE_PLAYING;
6357 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
6358 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6359 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6360 stream->discont = TRUE;
6365 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
6372 GST_DEBUG_OBJECT (src, "failed to open stream");
6377 GST_DEBUG_OBJECT (src, "PLAY is not supported");
6382 GST_DEBUG_OBJECT (src, "we were already PLAYING");
6387 GST_DEBUG_OBJECT (src, "failed going to PLAYING, already changing state");
6390 create_request_failed:
6392 gchar *str = gst_rtsp_strresult (res);
6394 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6395 ("Could not create request. (%s)", str));
6401 gchar *str = gst_rtsp_strresult (res);
6403 gst_rtsp_message_unset (&request);
6404 if (res != GST_RTSP_EINTR) {
6405 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6406 ("Could not send message. (%s)", str));
6408 GST_WARNING_OBJECT (src, "PLAY interrupted");
6415 static GstRTSPResult
6416 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
6418 GstRTSPResult res = GST_RTSP_OK;
6419 GstRTSPMessage request = { 0 };
6420 GstRTSPMessage response = { 0 };
6424 GST_DEBUG_OBJECT (src, "PAUSE...");
6426 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6429 if (!(src->methods & GST_RTSP_PAUSE))
6432 if (src->state == GST_RTSP_STATE_READY)
6435 if (!src->conninfo.connection || !src->conninfo.connected)
6438 /* construct a control url */
6440 control = src->control;
6442 control = src->conninfo.url_str;
6444 /* loop over the streams. We might exit the loop early when we could do an
6445 * aggregate control */
6446 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6447 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6448 GstRTSPConnection *conn;
6451 /* try aggregate control first but do non-aggregate control otherwise */
6453 setup_url = control;
6454 else if ((setup_url = stream->conninfo.location) == NULL)
6457 if (src->conninfo.connection) {
6458 conn = src->conninfo.connection;
6459 } else if (stream->conninfo.connection) {
6460 conn = stream->conninfo.connection;
6466 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
6467 ("Sending PAUSE request"));
6470 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
6472 goto create_request_failed;
6474 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6477 gst_rtsp_message_unset (&request);
6478 gst_rtsp_message_unset (&response);
6480 /* exit early when we did agregate control */
6486 src->state = GST_RTSP_STATE_READY;
6490 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
6497 GST_DEBUG_OBJECT (src, "failed to open stream");
6502 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
6507 GST_DEBUG_OBJECT (src, "we were already PAUSED");
6510 create_request_failed:
6512 gchar *str = gst_rtsp_strresult (res);
6514 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6515 ("Could not create request. (%s)", str));
6521 gchar *str = gst_rtsp_strresult (res);
6523 gst_rtsp_message_unset (&request);
6524 if (res != GST_RTSP_EINTR) {
6525 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6526 ("Could not send message. (%s)", str));
6528 GST_WARNING_OBJECT (src, "PAUSE interrupted");
6536 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
6538 GstRTSPSrc *rtspsrc;
6540 rtspsrc = GST_RTSPSRC (bin);
6542 switch (GST_MESSAGE_TYPE (message)) {
6543 case GST_MESSAGE_EOS:
6544 gst_message_unref (message);
6546 case GST_MESSAGE_ELEMENT:
6548 const GstStructure *s = gst_message_get_structure (message);
6550 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
6551 gboolean ignore_timeout;
6553 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
6555 GST_OBJECT_LOCK (rtspsrc);
6556 ignore_timeout = rtspsrc->ignore_timeout;
6557 rtspsrc->ignore_timeout = TRUE;
6558 GST_OBJECT_UNLOCK (rtspsrc);
6560 /* we only act on the first udp timeout message, others are irrelevant
6561 * and can be ignored. */
6562 if (!ignore_timeout)
6563 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
6565 gst_message_unref (message);
6568 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6571 case GST_MESSAGE_ERROR:
6574 GstRTSPStream *stream;
6577 udpsrc = GST_MESSAGE_SRC (message);
6579 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
6580 GST_ELEMENT_NAME (udpsrc));
6582 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
6586 /* we ignore the RTCP udpsrc */
6587 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
6590 /* if we get error messages from the udp sources, that's not a problem as
6591 * long as not all of them error out. We also don't really know what the
6592 * problem is, the message does not give enough detail... */
6593 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
6594 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
6595 if (ret != GST_FLOW_OK)
6599 gst_message_unref (message);
6603 /* fatal but not our message, forward */
6604 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6609 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6615 /* the thread where everything happens */
6617 gst_rtspsrc_thread (GstRTSPSrc * src)
6621 GST_OBJECT_LOCK (src);
6622 cmd = src->pending_cmd;
6623 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_LOOP)
6624 src->pending_cmd = CMD_LOOP;
6626 src->pending_cmd = CMD_WAIT;
6627 GST_DEBUG_OBJECT (src, "got command %d", cmd);
6629 /* we got the message command, so ensure communication is possible again */
6630 gst_rtspsrc_connection_flush (src, FALSE);
6632 src->busy_cmd = cmd;
6633 GST_OBJECT_UNLOCK (src);
6637 gst_rtspsrc_open (src, TRUE);
6640 gst_rtspsrc_play (src, &src->segment, TRUE);
6643 gst_rtspsrc_pause (src, TRUE);
6646 gst_rtspsrc_close (src, TRUE, FALSE);
6649 gst_rtspsrc_loop (src);
6652 gst_rtspsrc_reconnect (src, FALSE);
6658 GST_OBJECT_LOCK (src);
6659 /* and go back to sleep */
6660 if (src->pending_cmd == CMD_WAIT) {
6662 gst_task_pause (src->task);
6665 src->busy_cmd = CMD_WAIT;
6666 GST_OBJECT_UNLOCK (src);
6670 gst_rtspsrc_start (GstRTSPSrc * src)
6672 GST_DEBUG_OBJECT (src, "starting");
6674 GST_OBJECT_LOCK (src);
6676 src->pending_cmd = CMD_WAIT;
6678 if (src->task == NULL) {
6679 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
6680 if (src->task == NULL)
6683 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
6685 GST_OBJECT_UNLOCK (src);
6692 GST_ERROR_OBJECT (src, "failed to create task");
6698 gst_rtspsrc_stop (GstRTSPSrc * src)
6702 GST_DEBUG_OBJECT (src, "stopping");
6704 /* also cancels pending task */
6705 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
6707 GST_OBJECT_LOCK (src);
6708 if ((task = src->task)) {
6710 GST_OBJECT_UNLOCK (src);
6712 gst_task_stop (task);
6714 /* make sure it is not running */
6715 GST_RTSP_STREAM_LOCK (src);
6716 GST_RTSP_STREAM_UNLOCK (src);
6718 /* now wait for the task to finish */
6719 gst_task_join (task);
6721 /* and free the task */
6722 gst_object_unref (GST_OBJECT (task));
6724 GST_OBJECT_LOCK (src);
6726 GST_OBJECT_UNLOCK (src);
6728 /* ensure synchronously all is closed and clean */
6729 gst_rtspsrc_close (src, FALSE, TRUE);
6734 static GstStateChangeReturn
6735 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
6737 GstRTSPSrc *rtspsrc;
6738 GstStateChangeReturn ret;
6740 rtspsrc = GST_RTSPSRC (element);
6742 switch (transition) {
6743 case GST_STATE_CHANGE_NULL_TO_READY:
6744 if (!gst_rtspsrc_start (rtspsrc))
6747 case GST_STATE_CHANGE_READY_TO_PAUSED:
6748 /* init some state */
6749 rtspsrc->cur_protocols = rtspsrc->protocols;
6750 /* first attempt, don't ignore timeouts */
6751 rtspsrc->ignore_timeout = FALSE;
6752 rtspsrc->open_error = FALSE;
6753 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
6755 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
6756 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
6757 /* unblock the tcp tasks and make the loop waiting */
6758 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP);
6760 case GST_STATE_CHANGE_PAUSED_TO_READY:
6766 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
6767 if (ret == GST_STATE_CHANGE_FAILURE)
6770 switch (transition) {
6771 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
6772 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
6774 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
6775 /* send pause request and keep the idle task around */
6776 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
6777 ret = GST_STATE_CHANGE_NO_PREROLL;
6779 case GST_STATE_CHANGE_READY_TO_PAUSED:
6780 ret = GST_STATE_CHANGE_NO_PREROLL;
6782 case GST_STATE_CHANGE_PAUSED_TO_READY:
6783 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
6785 case GST_STATE_CHANGE_READY_TO_NULL:
6786 gst_rtspsrc_stop (rtspsrc);
6797 GST_DEBUG_OBJECT (rtspsrc, "start failed");
6798 return GST_STATE_CHANGE_FAILURE;
6803 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
6806 GstRTSPSrc *rtspsrc;
6808 rtspsrc = GST_RTSPSRC (element);
6810 if (GST_EVENT_IS_DOWNSTREAM (event)) {
6811 res = gst_rtspsrc_push_event (rtspsrc, event);
6813 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
6820 /*** GSTURIHANDLER INTERFACE *************************************************/
6823 gst_rtspsrc_uri_get_type (GType type)
6828 static const gchar *const *
6829 gst_rtspsrc_uri_get_protocols (GType type)
6831 static const gchar *protocols[] =
6832 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp", NULL };
6838 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
6840 GstRTSPSrc *src = GST_RTSPSRC (handler);
6842 /* FIXME: make thread-safe */
6843 return g_strdup (src->conninfo.location);
6847 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
6852 GstRTSPUrl *newurl = NULL;
6853 GstSDPMessage *sdp = NULL;
6855 src = GST_RTSPSRC (handler);
6857 /* same URI, we're fine */
6858 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
6861 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
6862 if ((res = gst_sdp_message_new (&sdp) < 0))
6865 GST_DEBUG_OBJECT (src, "parsing SDP message");
6866 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
6870 GST_DEBUG_OBJECT (src, "parsing URI");
6871 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
6875 /* if worked, free previous and store new url object along with the original
6877 GST_DEBUG_OBJECT (src, "configuring URI");
6878 g_free (src->conninfo.location);
6879 src->conninfo.location = g_strdup (uri);
6880 gst_rtsp_url_free (src->conninfo.url);
6881 src->conninfo.url = newurl;
6882 g_free (src->conninfo.url_str);
6884 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
6886 src->conninfo.url_str = NULL;
6889 gst_sdp_message_free (src->sdp);
6891 src->from_sdp = sdp != NULL;
6893 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
6894 GST_DEBUG_OBJECT (src, "request uri is: %s",
6895 GST_STR_NULL (src->conninfo.url_str));
6902 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
6907 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
6908 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
6909 "Could not create SDP");
6914 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
6915 GST_STR_NULL (uri));
6916 gst_sdp_message_free (sdp);
6917 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
6923 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
6924 GST_STR_NULL (uri), res);
6925 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
6926 "Invalid RTSP URI");
6932 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
6934 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
6936 iface->get_type = gst_rtspsrc_uri_get_type;
6937 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
6938 iface->get_uri = gst_rtspsrc_uri_get_uri;
6939 iface->set_uri = gst_rtspsrc_uri_set_uri;