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/net/gstnet.h>
93 #include <gst/sdp/gstsdpmessage.h>
94 #include <gst/rtp/gstrtppayloads.h>
96 #include "gst/gst-i18n-plugin.h"
98 #include "gstrtspsrc.h"
100 GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
101 #define GST_CAT_DEFAULT (rtspsrc_debug)
103 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream_%u",
106 GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
108 /* templates used internally */
109 static GstStaticPadTemplate anysrctemplate =
110 GST_STATIC_PAD_TEMPLATE ("internalsrc_%u",
113 GST_STATIC_CAPS_ANY);
115 static GstStaticPadTemplate anysinktemplate =
116 GST_STATIC_PAD_TEMPLATE ("internalsink_%u",
119 GST_STATIC_CAPS_ANY);
123 SIGNAL_HANDLE_REQUEST,
128 enum _GstRtspSrcRtcpSyncMode
135 enum _GstRtspSrcBufferMode
143 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
145 gst_rtsp_src_buffer_mode_get_type (void)
147 static GType buffer_mode_type = 0;
148 static const GEnumValue buffer_modes[] = {
149 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
150 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
151 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
152 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
156 if (!buffer_mode_type) {
158 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
160 return buffer_mode_type;
163 #define DEFAULT_LOCATION NULL
164 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
165 #define DEFAULT_DEBUG FALSE
166 #define DEFAULT_RETRY 20
167 #define DEFAULT_TIMEOUT 5000000
168 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
169 #define DEFAULT_TCP_TIMEOUT 20000000
170 #define DEFAULT_LATENCY_MS 2000
171 #define DEFAULT_DROP_ON_LATENCY FALSE
172 #define DEFAULT_CONNECTION_SPEED 0
173 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
174 #define DEFAULT_DO_RTCP TRUE
175 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
176 #define DEFAULT_PROXY NULL
177 #define DEFAULT_RTP_BLOCKSIZE 0
178 #define DEFAULT_USER_ID NULL
179 #define DEFAULT_USER_PW NULL
180 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
181 #define DEFAULT_PORT_RANGE NULL
182 #define DEFAULT_SHORT_HEADER FALSE
183 #define DEFAULT_PROBATION 2
184 #define DEFAULT_UDP_RECONNECT TRUE
185 #define DEFAULT_MULTICAST_IFACE NULL
186 #define DEFAULT_NTP_SYNC FALSE
187 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
199 PROP_DROP_ON_LATENCY,
200 PROP_CONNECTION_SPEED,
203 PROP_DO_RTSP_KEEP_ALIVE,
212 PROP_UDP_BUFFER_SIZE,
216 PROP_MULTICAST_IFACE,
218 PROP_USE_PIPELINE_CLOCK,
222 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
224 gst_rtsp_nat_method_get_type (void)
226 static GType rtsp_nat_method_type = 0;
227 static const GEnumValue rtsp_nat_method[] = {
228 {GST_RTSP_NAT_NONE, "None", "none"},
229 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
233 if (!rtsp_nat_method_type) {
234 rtsp_nat_method_type =
235 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
237 return rtsp_nat_method_type;
240 static void gst_rtspsrc_finalize (GObject * object);
242 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
243 const GValue * value, GParamSpec * pspec);
244 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
245 GValue * value, GParamSpec * pspec);
247 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
249 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
250 gpointer iface_data);
252 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
255 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
256 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
258 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
260 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
261 GstStateChange transition);
262 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
263 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
265 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
266 GstRTSPMessage * response);
268 static void gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask);
269 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
270 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
272 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
273 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
275 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
276 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
277 gboolean only_close);
279 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
280 const gchar * uri, GError ** error);
281 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
283 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
284 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
285 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
286 GstRTSPStream * stream, GstEvent * event);
287 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
289 /* commands we send to out loop to notify it of events */
290 #define CMD_OPEN (1 << 0)
291 #define CMD_PLAY (1 << 1)
292 #define CMD_PAUSE (1 << 2)
293 #define CMD_CLOSE (1 << 3)
294 #define CMD_WAIT (1 << 4)
295 #define CMD_RECONNECT (1 << 5)
296 #define CMD_LOOP (1 << 6)
298 /* mask for all commands */
299 #define CMD_ALL ((CMD_LOOP << 1) - 1)
301 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
303 gchar *__txt = _gst_element_error_printf text; \
304 gst_element_post_message (GST_ELEMENT_CAST (el), \
305 gst_message_new_progress (GST_OBJECT_CAST (el), \
306 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
310 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
312 #define gst_rtspsrc_parent_class parent_class
313 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
314 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
317 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
319 GObjectClass *gobject_class;
320 GstElementClass *gstelement_class;
321 GstBinClass *gstbin_class;
323 gobject_class = (GObjectClass *) klass;
324 gstelement_class = (GstElementClass *) klass;
325 gstbin_class = (GstBinClass *) klass;
327 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
329 gobject_class->set_property = gst_rtspsrc_set_property;
330 gobject_class->get_property = gst_rtspsrc_get_property;
332 gobject_class->finalize = gst_rtspsrc_finalize;
334 g_object_class_install_property (gobject_class, PROP_LOCATION,
335 g_param_spec_string ("location", "RTSP Location",
336 "Location of the RTSP url to read",
337 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
339 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
340 g_param_spec_flags ("protocols", "Protocols",
341 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
342 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
344 g_object_class_install_property (gobject_class, PROP_DEBUG,
345 g_param_spec_boolean ("debug", "Debug",
346 "Dump request and response messages to stdout",
347 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
349 g_object_class_install_property (gobject_class, PROP_RETRY,
350 g_param_spec_uint ("retry", "Retry",
351 "Max number of retries when allocating RTP ports.",
352 0, G_MAXUINT16, DEFAULT_RETRY,
353 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
355 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
356 g_param_spec_uint64 ("timeout", "Timeout",
357 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
358 0, G_MAXUINT64, DEFAULT_TIMEOUT,
359 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
361 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
362 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
363 "Fail after timeout microseconds on TCP connections (0 = disabled)",
364 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
365 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
367 g_object_class_install_property (gobject_class, PROP_LATENCY,
368 g_param_spec_uint ("latency", "Buffer latency in ms",
369 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
370 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
372 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
373 g_param_spec_boolean ("drop-on-latency",
374 "Drop buffers when maximum latency is reached",
375 "Tells the jitterbuffer to never exceed the given latency in size",
376 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
378 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
379 g_param_spec_uint64 ("connection-speed", "Connection Speed",
380 "Network connection speed in kbps (0 = unknown)",
381 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
382 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
384 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
385 g_param_spec_enum ("nat-method", "NAT Method",
386 "Method to use for traversing firewalls and NAT",
387 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
388 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
391 * GstRTSPSrc::do-rtcp
393 * Enable RTCP support. Some old server don't like RTCP and then this property
394 * needs to be set to FALSE.
398 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
399 g_param_spec_boolean ("do-rtcp", "Do RTCP",
400 "Send RTCP packets, disable for old incompatible server.",
401 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
404 * GstRTSPSrc::do-rtsp-keep-alive
406 * Enable RTSP keep laive support. Some old server don't like RTSP
407 * keep alive and then this property needs to be set to FALSE.
411 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
412 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
413 "Send RTSP keep alive packets, disable for old incompatible server.",
414 DEFAULT_DO_RTSP_KEEP_ALIVE,
415 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
420 * Set the proxy parameters. This has to be a string of the format
421 * [http://][user:passwd@]host[:port].
425 g_object_class_install_property (gobject_class, PROP_PROXY,
426 g_param_spec_string ("proxy", "Proxy",
427 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
428 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
430 * GstRTSPSrc::proxy-id
432 * Sets the proxy URI user id for authentication. If the URI set via the
433 * "proxy" property contains a user-id already, that will take precedence.
437 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
438 g_param_spec_string ("proxy-id", "proxy-id",
439 "HTTP proxy URI user id for authentication", "",
440 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
442 * GstRTSPSrc::proxy-pw
444 * Sets the proxy URI password for authentication. If the URI set via the
445 * "proxy" property contains a password already, that will take precedence.
449 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
450 g_param_spec_string ("proxy-pw", "proxy-pw",
451 "HTTP proxy URI user password for authentication", "",
452 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
455 * GstRTSPSrc::rtp_blocksize
457 * RTP package size to suggest to server.
461 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
462 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
463 "RTP package size to suggest to server (0 = disabled)",
464 0, 65536, DEFAULT_RTP_BLOCKSIZE,
465 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
467 g_object_class_install_property (gobject_class,
469 g_param_spec_string ("user-id", "user-id",
470 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
471 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
472 g_object_class_install_property (gobject_class, PROP_USER_PW,
473 g_param_spec_string ("user-pw", "user-pw",
474 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
475 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
478 * GstRTSPSrc::buffer-mode:
480 * Control the buffering and timestamping mode used by the jitterbuffer.
484 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
485 g_param_spec_enum ("buffer-mode", "Buffer Mode",
486 "Control the buffering algorithm in use",
487 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
488 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
491 * GstRTSPSrc::port-range:
493 * Configure the client port numbers that can be used to recieve RTP and
498 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
499 g_param_spec_string ("port-range", "Port range",
500 "Client port range that can be used to receive RTP and RTCP data, "
501 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
502 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
505 * GstRTSPSrc::udp-buffer-size:
507 * Size of the kernel UDP receive buffer in bytes.
511 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
512 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
513 "Size of the kernel UDP receive buffer in bytes, 0=default",
514 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
515 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
518 * GstRTSPSrc::short-header:
520 * Only send the basic RTSP headers for broken encoders.
524 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
525 g_param_spec_boolean ("short-header", "Short Header",
526 "Only send the basic RTSP headers for broken encoders",
527 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
529 g_object_class_install_property (gobject_class, PROP_PROBATION,
530 g_param_spec_uint ("probation", "Number of probations",
531 "Consecutive packet sequence numbers to accept the source",
532 0, G_MAXUINT, DEFAULT_PROBATION,
533 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
535 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
536 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
537 "Reconnect to the server if RTSP connection is closed when doing UDP",
538 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
540 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
541 g_param_spec_string ("multicast-iface", "Multicast Interface",
542 "The network interface on which to join the multicast group",
543 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
545 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
546 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
547 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
548 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
550 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
551 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
552 "Use the pipeline running-time to set the NTP time in the RTCP SR messages",
553 DEFAULT_USE_PIPELINE_CLOCK,
554 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
557 * GstRTSPSrc::handle-request:
558 * @rtspsrc: a #GstRTSPSrc
559 * @request: a #GstRTSPMessage
560 * @response: a #GstRTSPMessage
562 * Handle a server request in @request and prepare @response.
564 * This signal is called from the streaming thread, you should therefore not
565 * do any state changes on @rtspsrc because this might deadlock. If you want
566 * to modify the state as a result of this signal, post a
567 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
572 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
573 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
574 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
575 G_TYPE_POINTER, G_TYPE_POINTER);
578 * GstRTSPSrc::on-sdp:
579 * @rtspsrc: a #GstRTSPSrc
580 * @sdp: a #GstSDPMessage
582 * Emited when the client has retrieved the SDP and before it configures the
583 * streams in the SDP. @sdp can be inspected and modified.
585 * This signal is called from the streaming thread, you should therefore not
586 * do any state changes on @rtspsrc because this might deadlock. If you want
587 * to modify the state as a result of this signal, post a
588 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
593 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
594 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
595 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
596 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
598 gstelement_class->send_event = gst_rtspsrc_send_event;
599 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
600 gstelement_class->change_state = gst_rtspsrc_change_state;
602 gst_element_class_add_pad_template (gstelement_class,
603 gst_static_pad_template_get (&rtptemplate));
605 gst_element_class_set_static_metadata (gstelement_class,
606 "RTSP packet receiver", "Source/Network",
607 "Receive data over the network via RTSP (RFC 2326)",
608 "Wim Taymans <wim@fluendo.com>, "
609 "Thijs Vermeir <thijs.vermeir@barco.com>, "
610 "Lutz Mueller <lutz@topfrose.de>");
612 gstbin_class->handle_message = gst_rtspsrc_handle_message;
614 gst_rtsp_ext_list_init ();
619 gst_rtspsrc_init (GstRTSPSrc * src)
621 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
622 src->protocols = DEFAULT_PROTOCOLS;
623 src->debug = DEFAULT_DEBUG;
624 src->retry = DEFAULT_RETRY;
625 src->udp_timeout = DEFAULT_TIMEOUT;
626 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
627 src->latency = DEFAULT_LATENCY_MS;
628 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
629 src->connection_speed = DEFAULT_CONNECTION_SPEED;
630 src->nat_method = DEFAULT_NAT_METHOD;
631 src->do_rtcp = DEFAULT_DO_RTCP;
632 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
633 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
634 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
635 src->user_id = g_strdup (DEFAULT_USER_ID);
636 src->user_pw = g_strdup (DEFAULT_USER_PW);
637 src->buffer_mode = DEFAULT_BUFFER_MODE;
638 src->client_port_range.min = 0;
639 src->client_port_range.max = 0;
640 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
641 src->short_header = DEFAULT_SHORT_HEADER;
642 src->probation = DEFAULT_PROBATION;
643 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
644 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
645 src->ntp_sync = DEFAULT_NTP_SYNC;
646 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
648 /* get a list of all extensions */
649 src->extensions = gst_rtsp_ext_list_get ();
651 /* connect to send signal */
652 gst_rtsp_ext_list_connect (src->extensions, "send",
653 (GCallback) gst_rtspsrc_send_cb, src);
655 /* protects the streaming thread in interleaved mode or the polling
656 * thread in UDP mode. */
657 g_rec_mutex_init (&src->stream_rec_lock);
659 /* protects our state changes from multiple invocations */
660 g_rec_mutex_init (&src->state_rec_lock);
662 src->state = GST_RTSP_STATE_INVALID;
664 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
668 gst_rtspsrc_finalize (GObject * object)
672 rtspsrc = GST_RTSPSRC (object);
674 gst_rtsp_ext_list_free (rtspsrc->extensions);
675 g_free (rtspsrc->conninfo.location);
676 gst_rtsp_url_free (rtspsrc->conninfo.url);
677 g_free (rtspsrc->conninfo.url_str);
678 g_free (rtspsrc->user_id);
679 g_free (rtspsrc->user_pw);
680 g_free (rtspsrc->multi_iface);
683 gst_sdp_message_free (rtspsrc->sdp);
686 if (rtspsrc->provided_clock)
687 gst_object_unref (rtspsrc->provided_clock);
690 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
691 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
693 G_OBJECT_CLASS (parent_class)->finalize (object);
697 gst_rtspsrc_provide_clock (GstElement * element)
699 GstRTSPSrc *src = GST_RTSPSRC (element);
702 if ((clock = src->provided_clock) != NULL)
703 gst_object_ref (clock);
708 /* a proxy string of the format [user:passwd@]host[:port] */
710 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
714 g_free (rtsp->proxy_user);
715 rtsp->proxy_user = NULL;
716 g_free (rtsp->proxy_passwd);
717 rtsp->proxy_passwd = NULL;
718 g_free (rtsp->proxy_host);
719 rtsp->proxy_host = NULL;
720 rtsp->proxy_port = 0;
727 /* we allow http:// in front but ignore it */
728 if (g_str_has_prefix (p, "http://"))
731 at = strchr (p, '@');
733 /* look for user:passwd */
734 col = strchr (proxy, ':');
735 if (col == NULL || col > at)
738 rtsp->proxy_user = g_strndup (p, col - p);
740 rtsp->proxy_passwd = g_strndup (col, at - col);
745 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
746 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
747 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
748 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
749 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
750 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
751 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
754 col = strchr (p, ':');
757 /* everything before the colon is the hostname */
758 rtsp->proxy_host = g_strndup (p, col - p);
760 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
762 rtsp->proxy_host = g_strdup (p);
763 rtsp->proxy_port = 8080;
769 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
771 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
772 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
775 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
777 rtspsrc->ptcp_timeout = NULL;
781 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
786 rtspsrc = GST_RTSPSRC (object);
790 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
791 g_value_get_string (value), NULL);
794 rtspsrc->protocols = g_value_get_flags (value);
797 rtspsrc->debug = g_value_get_boolean (value);
800 rtspsrc->retry = g_value_get_uint (value);
803 rtspsrc->udp_timeout = g_value_get_uint64 (value);
805 case PROP_TCP_TIMEOUT:
806 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
809 rtspsrc->latency = g_value_get_uint (value);
811 case PROP_DROP_ON_LATENCY:
812 rtspsrc->drop_on_latency = g_value_get_boolean (value);
814 case PROP_CONNECTION_SPEED:
815 rtspsrc->connection_speed = g_value_get_uint64 (value);
817 case PROP_NAT_METHOD:
818 rtspsrc->nat_method = g_value_get_enum (value);
821 rtspsrc->do_rtcp = g_value_get_boolean (value);
823 case PROP_DO_RTSP_KEEP_ALIVE:
824 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
827 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
830 if (rtspsrc->prop_proxy_id)
831 g_free (rtspsrc->prop_proxy_id);
832 rtspsrc->prop_proxy_id = g_value_dup_string (value);
835 if (rtspsrc->prop_proxy_pw)
836 g_free (rtspsrc->prop_proxy_pw);
837 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
839 case PROP_RTP_BLOCKSIZE:
840 rtspsrc->rtp_blocksize = g_value_get_uint (value);
843 if (rtspsrc->user_id)
844 g_free (rtspsrc->user_id);
845 rtspsrc->user_id = g_value_dup_string (value);
848 if (rtspsrc->user_pw)
849 g_free (rtspsrc->user_pw);
850 rtspsrc->user_pw = g_value_dup_string (value);
852 case PROP_BUFFER_MODE:
853 rtspsrc->buffer_mode = g_value_get_enum (value);
855 case PROP_PORT_RANGE:
859 str = g_value_get_string (value);
861 sscanf (str, "%u-%u",
862 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
864 rtspsrc->client_port_range.min = 0;
865 rtspsrc->client_port_range.max = 0;
869 case PROP_UDP_BUFFER_SIZE:
870 rtspsrc->udp_buffer_size = g_value_get_int (value);
872 case PROP_SHORT_HEADER:
873 rtspsrc->short_header = g_value_get_boolean (value);
876 rtspsrc->probation = g_value_get_uint (value);
878 case PROP_UDP_RECONNECT:
879 rtspsrc->udp_reconnect = g_value_get_boolean (value);
881 case PROP_MULTICAST_IFACE:
882 g_free (rtspsrc->multi_iface);
884 if (g_value_get_string (value) == NULL)
885 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
887 rtspsrc->multi_iface = g_value_dup_string (value);
890 rtspsrc->ntp_sync = g_value_get_boolean (value);
892 case PROP_USE_PIPELINE_CLOCK:
893 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
896 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
902 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
907 rtspsrc = GST_RTSPSRC (object);
911 g_value_set_string (value, rtspsrc->conninfo.location);
914 g_value_set_flags (value, rtspsrc->protocols);
917 g_value_set_boolean (value, rtspsrc->debug);
920 g_value_set_uint (value, rtspsrc->retry);
923 g_value_set_uint64 (value, rtspsrc->udp_timeout);
925 case PROP_TCP_TIMEOUT:
929 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
930 rtspsrc->tcp_timeout.tv_usec;
931 g_value_set_uint64 (value, timeout);
935 g_value_set_uint (value, rtspsrc->latency);
937 case PROP_DROP_ON_LATENCY:
938 g_value_set_boolean (value, rtspsrc->drop_on_latency);
940 case PROP_CONNECTION_SPEED:
941 g_value_set_uint64 (value, rtspsrc->connection_speed);
943 case PROP_NAT_METHOD:
944 g_value_set_enum (value, rtspsrc->nat_method);
947 g_value_set_boolean (value, rtspsrc->do_rtcp);
949 case PROP_DO_RTSP_KEEP_ALIVE:
950 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
956 if (rtspsrc->proxy_host) {
958 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
962 g_value_take_string (value, str);
966 g_value_set_string (value, rtspsrc->prop_proxy_id);
969 g_value_set_string (value, rtspsrc->prop_proxy_pw);
971 case PROP_RTP_BLOCKSIZE:
972 g_value_set_uint (value, rtspsrc->rtp_blocksize);
975 g_value_set_string (value, rtspsrc->user_id);
978 g_value_set_string (value, rtspsrc->user_pw);
980 case PROP_BUFFER_MODE:
981 g_value_set_enum (value, rtspsrc->buffer_mode);
983 case PROP_PORT_RANGE:
987 if (rtspsrc->client_port_range.min != 0) {
988 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
989 rtspsrc->client_port_range.max);
993 g_value_take_string (value, str);
996 case PROP_UDP_BUFFER_SIZE:
997 g_value_set_int (value, rtspsrc->udp_buffer_size);
999 case PROP_SHORT_HEADER:
1000 g_value_set_boolean (value, rtspsrc->short_header);
1002 case PROP_PROBATION:
1003 g_value_set_uint (value, rtspsrc->probation);
1005 case PROP_UDP_RECONNECT:
1006 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1008 case PROP_MULTICAST_IFACE:
1009 g_value_set_string (value, rtspsrc->multi_iface);
1012 g_value_set_boolean (value, rtspsrc->ntp_sync);
1014 case PROP_USE_PIPELINE_CLOCK:
1015 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1018 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1024 find_stream_by_id (GstRTSPStream * stream, gint * id)
1026 if (stream->id == *id)
1033 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1035 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1042 find_stream_by_pt (GstRTSPStream * stream, gint * pt)
1044 if (stream->pt == *pt)
1051 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1053 GstElement *src = (GstElement *) a;
1055 if (stream->udpsrc[0] == src)
1057 if (stream->udpsrc[1] == src)
1064 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1066 /* check qualified setup_url */
1067 if (!strcmp (stream->conninfo.location, (gchar *) a))
1069 /* check original control_url */
1070 if (!strcmp (stream->control_url, (gchar *) a))
1073 /* check if qualified setup_url ends with string */
1074 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1080 static GstRTSPStream *
1081 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1085 /* find and get stream */
1086 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1087 return (GstRTSPStream *) lstream->data;
1092 static const GstSDPBandwidth *
1093 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1094 const GstSDPMedia * media, const gchar * type)
1098 /* first look in the media specific section */
1099 len = gst_sdp_media_bandwidths_len (media);
1100 for (i = 0; i < len; i++) {
1101 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1103 if (strcmp (bw->bwtype, type) == 0)
1106 /* then look in the message specific section */
1107 len = gst_sdp_message_bandwidths_len (sdp);
1108 for (i = 0; i < len; i++) {
1109 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1111 if (strcmp (bw->bwtype, type) == 0)
1118 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1119 const GstSDPMedia * media, GstRTSPStream * stream)
1121 const GstSDPBandwidth *bw;
1123 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1124 stream->as_bandwidth = bw->bandwidth;
1126 stream->as_bandwidth = -1;
1128 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1129 stream->rr_bandwidth = bw->bandwidth;
1131 stream->rr_bandwidth = -1;
1133 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1134 stream->rs_bandwidth = bw->bandwidth;
1136 stream->rs_bandwidth = -1;
1140 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1141 const GstSDPConnection * conn)
1143 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1146 if (conn->addrtype == NULL)
1149 /* check for IPV6 */
1150 if (strcmp (conn->addrtype, "IP4") == 0)
1151 stream->is_ipv6 = FALSE;
1152 else if (strcmp (conn->addrtype, "IP6") == 0)
1153 stream->is_ipv6 = TRUE;
1158 g_free (stream->destination);
1159 stream->destination = g_strdup (conn->address);
1161 /* check for multicast */
1162 stream->is_multicast =
1163 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1165 stream->ttl = conn->ttl;
1168 /* Go over the connections for a stream.
1169 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1171 * - If we are dealing with a localhost address, we disable multicast
1174 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1175 const GstSDPMedia * media, GstRTSPStream * stream)
1177 const GstSDPConnection *conn;
1180 /* first look in the media specific section */
1181 len = gst_sdp_media_connections_len (media);
1182 for (i = 0; i < len; i++) {
1183 conn = gst_sdp_media_get_connection (media, i);
1185 gst_rtspsrc_do_stream_connection (src, stream, conn);
1187 /* then look in the message specific section */
1188 if ((conn = gst_sdp_message_get_connection (sdp))) {
1189 gst_rtspsrc_do_stream_connection (src, stream, conn);
1193 static GstRTSPStream *
1194 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1196 GstRTSPStream *stream;
1197 const gchar *control_url;
1198 const gchar *payload;
1199 const GstSDPMedia *media;
1201 /* get media, should not return NULL */
1202 media = gst_sdp_message_get_media (sdp, idx);
1206 stream = g_new0 (GstRTSPStream, 1);
1207 stream->parent = src;
1208 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1210 stream->last_ret = GST_FLOW_NOT_LINKED;
1211 stream->added = FALSE;
1212 stream->disabled = FALSE;
1213 stream->id = src->numstreams++;
1214 stream->eos = FALSE;
1215 stream->discont = TRUE;
1216 stream->seqbase = -1;
1217 stream->timebase = -1;
1219 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1220 * session manager to scale RTCP. */
1221 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1223 /* collect connection info */
1224 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1226 /* we must have a payload. No payload means we cannot create caps */
1227 /* FIXME, handle multiple formats. The problem here is that we just want to
1228 * take the first available format that we can handle but in order to do that
1229 * we need to scan for depayloader plugins. Scanning for payloader plugins is
1230 * also suboptimal because the user maybe just wants to save the raw stream
1231 * and then we don't care. */
1232 if ((payload = gst_sdp_media_get_format (media, 0))) {
1233 stream->pt = atoi (payload);
1235 stream->caps = gst_rtspsrc_media_to_caps (stream->pt, media);
1237 GST_DEBUG ("mapping sdp session level attributes to caps");
1238 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, stream->caps);
1239 GST_DEBUG ("mapping sdp media level attributes to caps");
1240 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, stream->caps);
1242 if (stream->pt >= 96) {
1243 /* If we have a dynamic payload type, see if we have a stream with the
1244 * same payload number. If there is one, they are part of the same
1245 * container and we only need to add one pad. */
1246 if (find_stream (src, &stream->pt, (gpointer) find_stream_by_pt)) {
1247 stream->container = TRUE;
1248 GST_DEBUG ("found another stream with pt %d, marking as container",
1253 /* collect port number */
1254 stream->port = gst_sdp_media_get_port (media);
1256 /* get control url to construct the setup url. The setup url is used to
1257 * configure the transport of the stream and is used to identity the stream in
1258 * the RTP-Info header field returned from PLAY. */
1259 control_url = gst_sdp_media_get_attribute_val (media, "control");
1260 if (control_url == NULL)
1261 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1263 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1264 GST_DEBUG_OBJECT (src, " pt: %d", stream->pt);
1265 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1266 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1267 GST_DEBUG_OBJECT (src, " caps: %" GST_PTR_FORMAT, stream->caps);
1268 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1270 if (control_url != NULL) {
1271 stream->control_url = g_strdup (control_url);
1272 /* Build a fully qualified url using the content_base if any or by prefixing
1273 * the original request.
1274 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1275 * likely build a URL that the server will fail to understand, this is ok,
1276 * we will fail then. */
1277 if (g_str_has_prefix (control_url, "rtsp://"))
1278 stream->conninfo.location = g_strdup (control_url);
1283 if (g_strcmp0 (control_url, "*") == 0)
1287 base = src->control;
1288 else if (src->content_base)
1289 base = src->content_base;
1290 else if (src->conninfo.url_str)
1291 base = src->conninfo.url_str;
1295 /* check if the base ends or control starts with / */
1296 has_slash = g_str_has_prefix (control_url, "/");
1297 has_slash = has_slash || g_str_has_suffix (base, "/");
1299 /* concatenate the two strings, insert / when not present */
1300 stream->conninfo.location =
1301 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1304 GST_DEBUG_OBJECT (src, " setup: %s",
1305 GST_STR_NULL (stream->conninfo.location));
1307 /* we keep track of all streams */
1308 src->streams = g_list_append (src->streams, stream);
1316 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1320 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1323 gst_caps_unref (stream->caps);
1325 g_free (stream->destination);
1326 g_free (stream->control_url);
1327 g_free (stream->conninfo.location);
1329 for (i = 0; i < 2; i++) {
1330 if (stream->udpsrc[i]) {
1331 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1332 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1333 gst_object_unref (stream->udpsrc[i]);
1334 stream->udpsrc[i] = NULL;
1336 if (stream->channelpad[i]) {
1337 gst_object_unref (stream->channelpad[i]);
1338 stream->channelpad[i] = NULL;
1340 if (stream->udpsink[i]) {
1341 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1342 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1343 gst_object_unref (stream->udpsink[i]);
1344 stream->udpsink[i] = NULL;
1347 if (stream->fakesrc) {
1348 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1349 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1350 gst_object_unref (stream->fakesrc);
1351 stream->fakesrc = NULL;
1353 if (stream->srcpad) {
1354 gst_pad_set_active (stream->srcpad, FALSE);
1355 if (stream->added) {
1356 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1357 stream->added = FALSE;
1359 stream->srcpad = NULL;
1361 if (stream->rtcppad) {
1362 gst_object_unref (stream->rtcppad);
1363 stream->rtcppad = NULL;
1365 if (stream->session) {
1366 g_object_unref (stream->session);
1367 stream->session = NULL;
1373 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1377 GST_DEBUG_OBJECT (src, "cleanup");
1379 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1380 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1382 gst_rtspsrc_stream_free (src, stream);
1384 g_list_free (src->streams);
1385 src->streams = NULL;
1387 if (src->manager_sig_id) {
1388 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1389 src->manager_sig_id = 0;
1391 gst_element_set_state (src->manager, GST_STATE_NULL);
1392 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1393 src->manager = NULL;
1395 src->numstreams = 0;
1397 gst_structure_free (src->props);
1400 g_free (src->content_base);
1401 src->content_base = NULL;
1403 g_free (src->control);
1404 src->control = NULL;
1407 gst_rtsp_range_free (src->range);
1410 /* don't clear the SDP when it was used in the url */
1411 if (src->sdp && !src->from_sdp) {
1412 gst_sdp_message_free (src->sdp);
1415 if (src->start_segment) {
1416 gst_event_unref (src->start_segment);
1417 src->start_segment = NULL;
1419 if (src->provided_clock) {
1420 gst_object_unref (src->provided_clock);
1421 src->provided_clock = NULL;
1425 #define PARSE_INT(p, del, res) \
1428 p = strstr (p, del); \
1438 #define PARSE_STRING(p, del, res) \
1441 p = strstr (p, del); \
1453 #define SKIP_SPACES(p) \
1454 while (*p && g_ascii_isspace (*p)) \
1459 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1462 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1463 gint * rate, gchar ** params)
1467 p = (gchar *) rtpmap;
1469 PARSE_INT (p, " ", *payload);
1477 PARSE_STRING (p, "/", *name);
1478 if (*name == NULL) {
1479 GST_DEBUG ("no rate, name %s", p);
1480 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1481 * streams seem to omit the rate. */
1488 p = strstr (p, "/");
1506 * Mapping SDP attributes to caps
1508 * prepend 'a-' to IANA registered sdp attributes names
1509 * (ie: not prefixed with 'x-') in order to avoid
1510 * collision with gstreamer standard caps properties names
1513 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1515 if (attributes->len > 0) {
1519 s = gst_caps_get_structure (caps, 0);
1521 for (i = 0; i < attributes->len; i++) {
1522 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1523 gchar *tofree, *key;
1527 /* skip some of the attribute we already handle */
1528 if (!strcmp (key, "fmtp"))
1530 if (!strcmp (key, "rtpmap"))
1532 if (!strcmp (key, "control"))
1534 if (!strcmp (key, "range"))
1537 /* string must be valid UTF8 */
1538 if (!g_utf8_validate (attr->value, -1, NULL))
1541 if (!g_str_has_prefix (key, "x-"))
1542 tofree = key = g_strdup_printf ("a-%s", key);
1546 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1547 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1554 * Mapping of caps to and from SDP fields:
1556 * m=<media> <UDP port> RTP/AVP <payload>
1557 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1558 * a=fmtp:<payload> <param>[=<value>];...
1561 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1564 const gchar *rtpmap;
1568 gchar *params = NULL;
1574 /* get and parse rtpmap */
1575 if ((rtpmap = gst_sdp_media_get_attribute_val (media, "rtpmap"))) {
1576 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
1578 if (payload != pt) {
1579 /* we ignore the rtpmap if the payload type is different. */
1580 g_warning ("rtpmap of wrong payload type, ignoring");
1586 /* if we failed to parse the rtpmap for a dynamic payload type, we have an
1590 /* else we can ignore */
1591 g_warning ("error parsing rtpmap, ignoring");
1594 /* dynamic payloads need rtpmap or we fail */
1598 /* check if we have a rate, if not, we need to look up the rate from the
1599 * default rates based on the payload types. */
1601 const GstRTPPayloadInfo *info;
1603 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
1604 /* dynamic types, use media and encoding_name */
1605 tmp = g_ascii_strdown (media->media, -1);
1606 info = gst_rtp_payload_info_for_name (tmp, name);
1609 /* static types, use payload type */
1610 info = gst_rtp_payload_info_for_pt (pt);
1614 if ((rate = info->clock_rate) == 0)
1617 /* we fail if we cannot find one */
1622 tmp = g_ascii_strdown (media->media, -1);
1623 caps = gst_caps_new_simple ("application/x-unknown",
1624 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
1626 s = gst_caps_get_structure (caps, 0);
1628 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
1630 /* encoding name must be upper case */
1632 tmp = g_ascii_strup (name, -1);
1633 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
1637 /* params must be lower case */
1638 if (params != NULL) {
1639 tmp = g_ascii_strdown (params, -1);
1640 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
1644 /* parse optional fmtp: field */
1645 if ((fmtp = gst_sdp_media_get_attribute_val (media, "fmtp"))) {
1651 /* p is now of the format <payload> <param>[=<value>];... */
1652 PARSE_INT (p, " ", payload);
1653 if (payload != -1 && payload == pt) {
1657 /* <param>[=<value>] are separated with ';' */
1658 pairs = g_strsplit (p, ";", 0);
1659 for (i = 0; pairs[i]; i++) {
1661 const gchar *val, *key;
1663 /* the key may not have a '=', the value can have other '='s */
1664 valpos = strstr (pairs[i], "=");
1666 /* we have a '=' and thus a value, remove the '=' with \0 */
1668 /* value is everything between '=' and ';'. We split the pairs at ;
1669 * boundaries so we can take the remainder of the value. Some servers
1670 * put spaces around the value which we strip off here. Alternatively
1671 * we could strip those spaces in the depayloaders should these spaces
1672 * actually carry any meaning in the future. */
1673 val = g_strstrip (valpos + 1);
1675 /* simple <param>;.. is translated into <param>=1;... */
1678 /* strip the key of spaces, convert key to lowercase but not the value. */
1679 key = g_strstrip (pairs[i]);
1680 if (strlen (key) > 1) {
1681 tmp = g_ascii_strdown (key, -1);
1682 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
1694 g_warning ("rtpmap type not given for dynamic payload %d", pt);
1699 g_warning ("rate unknown for payload type %d", pt);
1705 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1706 gint * rtpport, gint * rtcpport)
1709 GstStateChangeReturn ret;
1710 GstElement *udpsrc0, *udpsrc1;
1711 gint tmp_rtp, tmp_rtcp;
1715 src = stream->parent;
1721 /* Start at next port */
1722 tmp_rtp = src->next_port_num;
1724 if (stream->is_ipv6)
1725 host = "udp://[::0]";
1727 host = "udp://0.0.0.0";
1729 /* try to allocate 2 UDP ports, the RTP port should be an even
1730 * number and the RTCP port should be the next (uneven) port */
1733 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1734 tmp_rtp >= src->client_port_range.max)
1737 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1738 if (udpsrc0 == NULL)
1739 goto no_udp_protocol;
1740 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1742 if (src->udp_buffer_size != 0)
1743 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
1746 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
1747 if (ret == GST_STATE_CHANGE_FAILURE) {
1749 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
1752 if (++count > src->retry)
1755 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1756 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1757 gst_object_unref (udpsrc0);
1760 GST_DEBUG_OBJECT (src, "retry %d", count);
1763 goto no_udp_protocol;
1766 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
1767 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
1769 /* check if port is even */
1770 if ((tmp_rtp & 0x01) != 0) {
1771 /* port not even, close and allocate another */
1772 if (++count > src->retry)
1775 GST_DEBUG_OBJECT (src, "RTP port not even");
1777 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1778 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1779 gst_object_unref (udpsrc0);
1782 GST_DEBUG_OBJECT (src, "retry %d", count);
1787 /* allocate port+1 for RTCP now */
1788 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1789 if (udpsrc1 == NULL)
1790 goto no_udp_rtcp_protocol;
1793 tmp_rtcp = tmp_rtp + 1;
1794 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
1797 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
1799 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
1800 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
1801 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
1802 if (ret == GST_STATE_CHANGE_FAILURE) {
1803 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
1805 if (++count > src->retry)
1808 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1809 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1810 gst_object_unref (udpsrc0);
1813 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
1814 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1815 gst_object_unref (udpsrc1);
1819 GST_DEBUG_OBJECT (src, "retry %d", count);
1823 /* all fine, do port check */
1824 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
1825 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
1827 /* this should not happen... */
1828 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
1831 /* we keep these elements, we configure all in configure_transport when the
1832 * server told us to really use the UDP ports. */
1833 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
1834 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
1835 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
1836 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
1838 /* keep track of next available port number when we have a range
1840 if (src->next_port_num != 0)
1841 src->next_port_num = tmp_rtcp + 1;
1848 GST_DEBUG_OBJECT (src, "could not get UDP source");
1853 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
1857 no_udp_rtcp_protocol:
1859 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
1864 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
1865 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
1871 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1872 gst_object_unref (udpsrc0);
1875 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1876 gst_object_unref (udpsrc1);
1883 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
1888 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
1890 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1891 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1894 for (i = 0; i < 2; i++) {
1895 if (stream->udpsrc[i])
1896 gst_element_set_state (stream->udpsrc[i], state);
1902 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
1909 event = gst_event_new_flush_start ();
1910 GST_DEBUG_OBJECT (src, "start flush");
1912 state = GST_STATE_PAUSED;
1914 event = gst_event_new_flush_stop (FALSE);
1915 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
1918 state = GST_STATE_PLAYING;
1920 state = GST_STATE_PAUSED;
1922 gst_rtspsrc_push_event (src, event);
1923 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
1924 gst_rtspsrc_set_state (src, state);
1927 static GstRTSPResult
1928 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
1929 GstRTSPMessage * message, GTimeVal * timeout)
1934 ret = gst_rtsp_connection_send (conn, message, timeout);
1936 ret = GST_RTSP_ERROR;
1941 static GstRTSPResult
1942 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
1943 GstRTSPMessage * message, GTimeVal * timeout)
1948 ret = gst_rtsp_connection_receive (conn, message, timeout);
1950 ret = GST_RTSP_ERROR;
1956 gst_rtspsrc_get_position (GstRTSPSrc * src)
1961 query = gst_query_new_position (GST_FORMAT_TIME);
1962 /* should be known somewhere down the stream (e.g. jitterbuffer) */
1963 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1964 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1968 if (stream->srcpad) {
1969 if (gst_pad_query (stream->srcpad, query)) {
1970 gst_query_parse_position (query, &fmt, &pos);
1971 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
1972 GST_TIME_ARGS (pos));
1973 src->last_pos = pos;
1983 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
1985 src->state = GST_RTSP_STATE_SEEKING;
1986 /* PLAY will add the range header now. */
1987 src->need_range = TRUE;
1993 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
1998 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2000 gboolean flush, skip;
2003 GstSegment seeksegment = { 0, };
2007 GST_DEBUG_OBJECT (src, "doing seek with event");
2009 gst_event_parse_seek (event, &rate, &format, &flags,
2010 &cur_type, &cur, &stop_type, &stop);
2012 /* no negative rates yet */
2016 /* we need TIME format */
2017 if (format != src->segment.format)
2020 GST_DEBUG_OBJECT (src, "doing seek without event");
2022 cur_type = GST_SEEK_TYPE_SET;
2023 stop_type = GST_SEEK_TYPE_SET;
2026 /* get flush flag */
2027 flush = flags & GST_SEEK_FLAG_FLUSH;
2028 skip = flags & GST_SEEK_FLAG_SKIP;
2030 /* now we need to make sure the streaming thread is stopped. We do this by
2031 * either sending a FLUSH_START event downstream which will cause the
2032 * streaming thread to stop with a WRONG_STATE.
2033 * For a non-flushing seek we simply pause the task, which will happen as soon
2034 * as it completes one iteration (and thus might block when the sink is
2035 * blocking in preroll). */
2037 GST_DEBUG_OBJECT (src, "starting flush");
2038 gst_rtspsrc_flush (src, TRUE, FALSE);
2041 gst_task_pause (src->task);
2045 /* we should now be able to grab the streaming thread because we stopped it
2046 * with the above flush/pause code */
2047 GST_RTSP_STREAM_LOCK (src);
2049 GST_DEBUG_OBJECT (src, "stopped streaming");
2051 /* copy segment, we need this because we still need the old
2052 * segment when we close the current segment. */
2053 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2055 /* configure the seek parameters in the seeksegment. We will then have the
2056 * right values in the segment to perform the seek */
2058 GST_DEBUG_OBJECT (src, "configuring seek");
2059 gst_segment_do_seek (&seeksegment, rate, format, flags,
2060 cur_type, cur, stop_type, stop, &update);
2063 /* figure out the last position we need to play. If it's configured (stop !=
2064 * -1), use that, else we play until the total duration of the file */
2065 if ((stop = seeksegment.stop) == -1)
2066 stop = seeksegment.duration;
2068 playing = (src->state == GST_RTSP_STATE_PLAYING);
2070 /* if we were playing, pause first */
2072 /* obtain current position in case seek fails */
2073 gst_rtspsrc_get_position (src);
2074 gst_rtspsrc_pause (src, FALSE);
2078 gst_rtspsrc_do_seek (src, &seeksegment);
2080 /* and continue playing */
2082 gst_rtspsrc_play (src, &seeksegment, FALSE);
2084 /* prepare for streaming again */
2086 /* if we started flush, we stop now */
2087 GST_DEBUG_OBJECT (src, "stopping flush");
2088 gst_rtspsrc_flush (src, FALSE, playing);
2091 /* now we did the seek and can activate the new segment values */
2092 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2094 /* if we're doing a segment seek, post a SEGMENT_START message */
2095 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2096 gst_element_post_message (GST_ELEMENT_CAST (src),
2097 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2098 src->segment.format, src->segment.position));
2101 /* now create the newsegment */
2102 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2103 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2106 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2107 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2108 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2109 stream->discont = TRUE;
2112 GST_RTSP_STREAM_UNLOCK (src);
2119 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2124 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2130 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2134 gboolean res = TRUE;
2137 src = GST_RTSPSRC_CAST (parent);
2139 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2140 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2142 switch (GST_EVENT_TYPE (event)) {
2143 case GST_EVENT_SEEK:
2144 res = gst_rtspsrc_perform_seek (src, event);
2148 case GST_EVENT_NAVIGATION:
2149 case GST_EVENT_LATENCY:
2157 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2158 res = gst_pad_send_event (target, event);
2159 gst_object_unref (target);
2161 gst_event_unref (event);
2164 gst_event_unref (event);
2170 /* this is the final event function we receive on the internal source pad when
2171 * we deal with TCP connections */
2173 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2178 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2180 switch (GST_EVENT_TYPE (event)) {
2181 case GST_EVENT_SEEK:
2183 case GST_EVENT_NAVIGATION:
2184 case GST_EVENT_LATENCY:
2186 gst_event_unref (event);
2193 /* this is the final query function we receive on the internal source pad when
2194 * we deal with TCP connections */
2196 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2200 gboolean res = TRUE;
2202 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2204 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2205 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2207 switch (GST_QUERY_TYPE (query)) {
2208 case GST_QUERY_POSITION:
2213 case GST_QUERY_DURATION:
2217 gst_query_parse_duration (query, &format, NULL);
2220 case GST_FORMAT_TIME:
2221 gst_query_set_duration (query, format, src->segment.duration);
2229 case GST_QUERY_LATENCY:
2231 /* we are live with a min latency of 0 and unlimited max latency, this
2232 * result will be updated by the session manager if there is any. */
2233 gst_query_set_latency (query, TRUE, 0, -1);
2243 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2245 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2249 gboolean res = FALSE;
2251 src = GST_RTSPSRC_CAST (parent);
2253 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2254 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2256 switch (GST_QUERY_TYPE (query)) {
2257 case GST_QUERY_DURATION:
2261 gst_query_parse_duration (query, &format, NULL);
2264 case GST_FORMAT_TIME:
2265 gst_query_set_duration (query, format, src->segment.duration);
2273 case GST_QUERY_SEEKING:
2277 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2278 if (format == GST_FORMAT_TIME) {
2280 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2282 /* seeking without duration is unlikely */
2283 seekable = seekable && src->seekable && src->segment.duration &&
2284 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2286 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2287 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2288 src->segment.start, src->segment.stop);
2297 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2299 gst_query_set_uri (query, uri);
2307 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2309 /* forward the query to the proxy target pad */
2311 res = gst_pad_query (target, query);
2312 gst_object_unref (target);
2321 /* callback for RTCP messages to be sent to the server when operating in TCP
2323 static GstFlowReturn
2324 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2327 GstRTSPStream *stream;
2328 GstFlowReturn res = GST_FLOW_OK;
2333 GstRTSPMessage message = { 0 };
2334 GstRTSPConnection *conn;
2336 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2337 src = stream->parent;
2339 gst_buffer_map (buffer, &map, GST_MAP_READ);
2343 gst_rtsp_message_init_data (&message, stream->channel[1]);
2345 /* lend the body data to the message */
2346 gst_rtsp_message_take_body (&message, data, size);
2348 if (stream->conninfo.connection)
2349 conn = stream->conninfo.connection;
2351 conn = src->conninfo.connection;
2353 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2354 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2355 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2357 /* and steal it away again because we will free it when unreffing the
2359 gst_rtsp_message_steal_body (&message, &data, &size);
2360 gst_rtsp_message_unset (&message);
2362 gst_buffer_unmap (buffer, &map);
2363 gst_buffer_unref (buffer);
2368 static GstPadProbeReturn
2369 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2371 GstRTSPSrc *src = user_data;
2373 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2374 GST_DEBUG_PAD_NAME (pad));
2376 /* activate the streams */
2377 GST_OBJECT_LOCK (src);
2378 if (!src->need_activate)
2381 src->need_activate = FALSE;
2382 GST_OBJECT_UNLOCK (src);
2384 gst_rtspsrc_activate_streams (src);
2386 return GST_PAD_PROBE_OK;
2390 GST_OBJECT_UNLOCK (src);
2391 return GST_PAD_PROBE_OK;
2395 /* this callback is called when the session manager generated a new src pad with
2396 * payloaded RTP packets. We simply ghost the pad here. */
2398 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2401 GstPadTemplate *template;
2404 GstRTSPStream *stream;
2407 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2409 GST_RTSP_STATE_LOCK (src);
2411 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2412 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2413 goto unknown_stream;
2415 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2417 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2419 goto unknown_stream;
2422 stream->ssrc = ssrc;
2424 /* we'll add it later see below */
2425 stream->added = TRUE;
2427 /* check if we added all streams */
2429 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2430 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2432 GST_DEBUG_OBJECT (src, "stream %p, container %d, disabled %d, added %d",
2433 ostream, ostream->container, ostream->disabled, ostream->added);
2435 /* a container stream only needs one pad added. Also disabled streams don't
2437 if (!ostream->container && !ostream->disabled && !ostream->added) {
2442 GST_RTSP_STATE_UNLOCK (src);
2444 /* create a new pad we will use to stream to */
2445 template = gst_static_pad_template_get (&rtptemplate);
2446 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2447 gst_object_unref (template);
2450 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2451 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2452 gst_pad_set_active (stream->srcpad, TRUE);
2453 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2456 GST_DEBUG_OBJECT (src, "We added all streams");
2457 /* when we get here, all stream are added and we can fire the no-more-pads
2459 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2467 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2468 GST_RTSP_STATE_UNLOCK (src);
2475 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2477 GstRTSPStream *stream;
2480 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2482 GST_RTSP_STATE_LOCK (src);
2483 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2485 goto unknown_stream;
2487 caps = stream->caps;
2489 gst_caps_ref (caps);
2490 GST_RTSP_STATE_UNLOCK (src);
2496 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2497 GST_RTSP_STATE_UNLOCK (src);
2503 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2505 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2511 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2517 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2523 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2525 GstRTSPSrc *src = stream->parent;
2528 g_object_get (source, "ssrc", &ssrc, NULL);
2530 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2531 ssrc, stream->ssrc, stream->id);
2533 if (ssrc == stream->ssrc)
2534 gst_rtspsrc_do_stream_eos (src, stream);
2538 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2540 GstRTSPSrc *src = stream->parent;
2543 g_object_get (source, "ssrc", &ssrc, NULL);
2545 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2546 ssrc, stream->ssrc, stream->id);
2548 if (ssrc == stream->ssrc)
2549 gst_rtspsrc_do_stream_eos (src, stream);
2553 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2555 GstRTSPStream *stream;
2557 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2559 /* get stream for session */
2560 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2562 gst_rtspsrc_do_stream_eos (src, stream);
2567 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2569 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2573 /* try to get and configure a manager */
2575 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
2576 GstRTSPTransport * transport)
2578 const gchar *manager;
2580 GstStateChangeReturn ret;
2582 /* find a manager */
2583 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
2587 GST_DEBUG_OBJECT (src, "using manager %s", manager);
2589 /* configure the manager */
2590 if (src->manager == NULL) {
2591 GObjectClass *klass;
2593 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
2595 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
2599 goto use_no_manager;
2601 if (!(src->manager = gst_element_factory_make (manager, "manager")))
2602 goto manager_failed;
2605 /* we manage this element */
2606 gst_element_set_locked_state (src->manager, TRUE);
2607 gst_bin_add (GST_BIN_CAST (src), src->manager);
2609 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
2610 if (ret == GST_STATE_CHANGE_FAILURE)
2611 goto start_manager_failure;
2613 g_object_set (src->manager, "latency", src->latency, NULL);
2615 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2617 if (g_object_class_find_property (klass, "ntp-sync")) {
2618 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
2621 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
2622 g_object_set (src->manager, "use-pipeline-clock",
2623 src->use_pipeline_clock, NULL);
2626 if (g_object_class_find_property (klass, "drop-on-latency")) {
2627 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
2631 if (g_object_class_find_property (klass, "buffer-mode")) {
2632 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2633 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2635 gboolean need_slave;
2637 const gchar *encoding;
2639 /* buffer mode pauses are handled by adding offsets to buffer times,
2640 * but some depayloaders may have a hard time syncing output times
2641 * with such input times, e.g. container ones, most notably ASF */
2642 /* TODO alternatives are having an event that indicates these shifts,
2643 * or having rtsp extensions provide suggestion on buffer mode */
2644 need_slave = stream->container;
2645 if (stream->caps && (s = gst_caps_get_structure (stream->caps, 0)) &&
2646 (encoding = gst_structure_get_string (s, "encoding-name")))
2647 need_slave = need_slave || (strcmp (encoding, "X-ASF-PF") == 0);
2648 GST_DEBUG_OBJECT (src, "auto buffering mode, need_slave %d",
2650 /* valid duration implies not likely live pipeline,
2651 * so slaving in jitterbuffer does not make much sense
2652 * (and might mess things up due to bursts) */
2653 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
2654 src->segment.duration && !need_slave) {
2655 GST_DEBUG_OBJECT (src, "selected buffer");
2656 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER,
2659 GST_DEBUG_OBJECT (src, "selected slave");
2660 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2665 /* connect to signals if we did not already do so */
2666 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
2668 src->manager_sig_id =
2669 g_signal_connect (src->manager, "pad-added",
2670 (GCallback) new_manager_pad, src);
2671 src->manager_ptmap_id =
2672 g_signal_connect (src->manager, "request-pt-map",
2673 (GCallback) request_pt_map, src);
2675 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
2679 /* we stream directly to the manager, get some pads. Each RTSP stream goes
2680 * into a separate RTP session. */
2681 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
2682 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
2684 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
2685 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
2688 /* now configure the bandwidth in the manager */
2689 if (g_signal_lookup ("get-internal-session",
2690 G_OBJECT_TYPE (src->manager)) != 0) {
2691 GObject *rtpsession;
2693 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
2696 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
2698 stream->session = rtpsession;
2700 if (stream->as_bandwidth != -1) {
2701 GST_INFO_OBJECT (src, "setting AS: %f",
2702 (gdouble) (stream->as_bandwidth * 1000));
2703 g_object_set (rtpsession, "bandwidth",
2704 (gdouble) (stream->as_bandwidth * 1000), NULL);
2706 if (stream->rr_bandwidth != -1) {
2707 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
2708 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
2711 if (stream->rs_bandwidth != -1) {
2712 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
2713 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
2717 g_object_set (rtpsession, "probation", src->probation, NULL);
2719 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2721 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
2723 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
2725 g_signal_connect (rtpsession, "on-ssrc-active",
2726 (GCallback) on_ssrc_active, stream);
2737 GST_DEBUG_OBJECT (src, "cannot get a session manager");
2742 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
2745 start_manager_failure:
2747 GST_DEBUG_OBJECT (src, "could not start session manager");
2752 /* free the UDP sources allocated when negotiating a transport.
2753 * This function is called when the server negotiated to a transport where the
2754 * UDP sources are not needed anymore, such as TCP or multicast. */
2756 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
2760 for (i = 0; i < 2; i++) {
2761 if (stream->udpsrc[i]) {
2762 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
2763 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
2764 gst_object_unref (stream->udpsrc[i]);
2765 stream->udpsrc[i] = NULL;
2770 /* for TCP, create pads to send and receive data to and from the manager and to
2771 * intercept various events and queries
2774 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
2775 GstRTSPTransport * transport, GstPad ** outpad)
2778 GstPadTemplate *template;
2779 GstPad *pad0, *pad1;
2781 /* configure for interleaved delivery, nothing needs to be done
2782 * here, the loop function will call the chain functions of the
2783 * session manager. */
2784 stream->channel[0] = transport->interleaved.min;
2785 stream->channel[1] = transport->interleaved.max;
2786 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
2787 stream->channel[0], stream->channel[1]);
2789 /* we can remove the allocated UDP ports now */
2790 gst_rtspsrc_stream_free_udp (stream);
2792 /* no session manager, send data to srcpad directly */
2793 if (!stream->channelpad[0]) {
2794 GST_DEBUG_OBJECT (src, "no manager, creating pad");
2796 /* create a new pad we will use to stream to */
2797 name = g_strdup_printf ("stream_%u", stream->id);
2798 template = gst_static_pad_template_get (&rtptemplate);
2799 stream->channelpad[0] = gst_pad_new_from_template (template, name);
2800 gst_object_unref (template);
2803 /* set caps and activate */
2804 gst_pad_use_fixed_caps (stream->channelpad[0]);
2805 gst_pad_set_active (stream->channelpad[0], TRUE);
2807 *outpad = gst_object_ref (stream->channelpad[0]);
2809 GST_DEBUG_OBJECT (src, "using manager source pad");
2811 template = gst_static_pad_template_get (&anysrctemplate);
2813 /* allocate pads for sending the channel data into the manager */
2814 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
2815 gst_pad_link (pad0, stream->channelpad[0]);
2816 gst_object_unref (stream->channelpad[0]);
2817 stream->channelpad[0] = pad0;
2818 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
2819 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
2820 gst_pad_set_element_private (pad0, src);
2821 gst_pad_set_active (pad0, TRUE);
2823 if (stream->channelpad[1]) {
2824 /* if we have a sinkpad for the other channel, create a pad and link to the
2826 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
2827 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
2828 gst_pad_link (pad1, stream->channelpad[1]);
2829 gst_object_unref (stream->channelpad[1]);
2830 stream->channelpad[1] = pad1;
2831 gst_pad_set_active (pad1, TRUE);
2833 gst_object_unref (template);
2835 /* setup RTCP transport back to the server if we have to. */
2836 if (src->manager && src->do_rtcp) {
2839 template = gst_static_pad_template_get (&anysinktemplate);
2841 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
2842 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
2843 gst_pad_set_element_private (stream->rtcppad, stream);
2844 gst_pad_set_active (stream->rtcppad, TRUE);
2846 /* get session RTCP pad */
2847 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
2848 pad = gst_element_get_request_pad (src->manager, name);
2853 gst_pad_link (pad, stream->rtcppad);
2854 gst_object_unref (pad);
2857 gst_object_unref (template);
2863 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
2864 GstRTSPTransport * transport, const gchar ** destination, gint * min,
2865 gint * max, guint * ttl)
2867 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2869 if (!(*destination = transport->destination))
2870 *destination = stream->destination;
2873 /* transport first */
2874 *min = transport->port.min;
2875 *max = transport->port.max;
2876 if (*min == -1 && *max == -1) {
2877 /* then try from SDP */
2878 if (stream->port != 0) {
2879 *min = stream->port;
2880 *max = stream->port + 1;
2886 if (!(*ttl = transport->ttl))
2891 /* first take the source, then the endpoint to figure out where to send
2893 if (!(*destination = transport->source)) {
2894 if (src->conninfo.connection)
2895 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
2896 else if (stream->conninfo.connection)
2898 gst_rtsp_connection_get_ip (stream->conninfo.connection);
2902 /* for unicast we only expect the ports here */
2903 *min = transport->server_port.min;
2904 *max = transport->server_port.max;
2909 /* For multicast create UDP sources and join the multicast group. */
2911 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
2912 GstRTSPTransport * transport, GstPad ** outpad)
2915 const gchar *destination;
2918 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
2920 /* we can remove the allocated UDP ports now */
2921 gst_rtspsrc_stream_free_udp (stream);
2923 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
2926 /* we need a destination now */
2927 if (destination == NULL)
2928 goto no_destination;
2930 /* we really need ports now or we won't be able to receive anything at all */
2931 if (min == -1 && max == -1)
2934 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
2935 destination, min, max);
2937 /* creating UDP source for RTP */
2939 uri = g_strdup_printf ("udp://%s:%d", destination, min);
2941 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
2943 if (stream->udpsrc[0] == NULL)
2946 /* take ownership */
2947 gst_object_ref_sink (stream->udpsrc[0]);
2949 if (src->udp_buffer_size != 0)
2950 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
2951 src->udp_buffer_size, NULL);
2953 if (src->multi_iface != NULL)
2954 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
2955 src->multi_iface, NULL);
2958 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2959 gst_element_set_state (stream->udpsrc[0], GST_STATE_PAUSED);
2962 /* creating another UDP source for RTCP */
2966 uri = g_strdup_printf ("udp://%s:%d", destination, max);
2968 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
2970 if (stream->udpsrc[1] == NULL)
2973 caps = gst_caps_new_empty_simple ("application/x-rtcp");
2974 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
2975 gst_caps_unref (caps);
2977 /* take ownership */
2978 gst_object_ref_sink (stream->udpsrc[1]);
2980 if (src->multi_iface != NULL)
2981 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
2982 src->multi_iface, NULL);
2984 gst_element_set_state (stream->udpsrc[1], GST_STATE_PAUSED);
2991 GST_DEBUG_OBJECT (src, "no UDP source element found");
2996 GST_DEBUG_OBJECT (src, "no destination found");
3001 GST_DEBUG_OBJECT (src, "no ports found");
3006 /* configure the remainder of the UDP ports */
3008 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3009 GstRTSPTransport * transport, GstPad ** outpad)
3011 /* we manage the UDP elements now. For unicast, the UDP sources where
3012 * allocated in the stream when we suggested a transport. */
3013 if (stream->udpsrc[0]) {
3014 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3015 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3017 GST_DEBUG_OBJECT (src, "setting up UDP source");
3019 /* configure a timeout on the UDP port. When the timeout message is
3020 * posted, we assume UDP transport is not possible. We reconnect using TCP
3022 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3023 src->udp_timeout * 1000, NULL);
3025 /* get output pad of the UDP source. */
3026 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3028 /* save it so we can unblock */
3029 stream->blockedpad = *outpad;
3031 /* configure pad block on the pad. As soon as there is dataflow on the
3032 * UDP source, we know that UDP is not blocked by a firewall and we can
3033 * configure all the streams to let the application autoplug decoders. */
3035 gst_pad_add_probe (stream->blockedpad,
3036 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, pad_blocked, src, NULL);
3038 if (stream->channelpad[0]) {
3039 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3040 /* configure for UDP delivery, we need to connect the UDP pads to
3041 * the session plugin. */
3042 gst_pad_link (*outpad, stream->channelpad[0]);
3043 gst_object_unref (*outpad);
3045 /* we connected to pad-added signal to get pads from the manager */
3047 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3052 if (stream->udpsrc[1]) {
3055 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3056 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3058 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3059 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3060 gst_caps_unref (caps);
3062 if (stream->channelpad[1]) {
3065 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3067 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3068 gst_pad_link (pad, stream->channelpad[1]);
3069 gst_object_unref (pad);
3071 /* leave unlinked */
3077 /* configure the UDP sink back to the server for status reports */
3079 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3080 GstRTSPStream * stream, GstRTSPTransport * transport)
3083 gint rtp_port, rtcp_port;
3084 gboolean do_rtp, do_rtcp;
3085 const gchar *destination;
3090 /* get transport info */
3091 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3092 &rtp_port, &rtcp_port, &ttl);
3094 /* see what we need to do */
3095 do_rtp = (rtp_port != -1);
3096 /* it's possible that the server does not want us to send RTCP in which case
3098 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3100 /* we need a destination when we have RTP or RTCP ports */
3101 if (destination == NULL && (do_rtp || do_rtcp))
3102 goto no_destination;
3104 /* try to construct the fakesrc to the RTP port of the server to open up any
3107 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3110 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3111 stream->udpsink[0] =
3112 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3114 if (stream->udpsink[0] == NULL)
3115 goto no_sink_element;
3117 /* don't join multicast group, we will have the source socket do that */
3118 /* no sync or async state changes needed */
3119 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3120 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3122 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3124 if (stream->udpsrc[0]) {
3125 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3126 * so that NAT firewalls will open a hole for us */
3127 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3128 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3129 /* configure socket and make sure udpsink does not close it when shutting
3130 * down, it belongs to udpsrc after all. */
3131 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3132 "close-socket", FALSE, NULL);
3133 g_object_unref (socket);
3136 /* the source for the dummy packets to open up NAT */
3137 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3138 if (stream->fakesrc == NULL)
3139 goto no_fakesrc_element;
3141 /* random data in 5 buffers, a size of 200 bytes should be fine */
3142 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3143 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3145 /* we don't want to consider this a sink */
3146 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
3148 /* keep everything locked */
3149 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3150 gst_element_set_locked_state (stream->fakesrc, TRUE);
3152 gst_object_ref (stream->udpsink[0]);
3153 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3154 gst_object_ref (stream->fakesrc);
3155 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3157 gst_element_link (stream->fakesrc, stream->udpsink[0]);
3160 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3163 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3164 stream->udpsink[1] =
3165 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3167 if (stream->udpsink[1] == NULL)
3168 goto no_sink_element;
3170 /* don't join multicast group, we will have the source socket do that */
3171 /* no sync or async state changes needed */
3172 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3173 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3175 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3177 if (stream->udpsrc[1]) {
3178 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3179 * because some servers check the port number of where it sends RTCP to identify
3180 * the RTCP packets it receives */
3181 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3182 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3183 /* configure socket and make sure udpsink does not close it when shutting
3184 * down, it belongs to udpsrc after all. */
3185 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3186 "close-socket", FALSE, NULL);
3187 g_object_unref (socket);
3190 /* we don't want to consider this a sink */
3191 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3193 /* we keep this playing always */
3194 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3195 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3197 gst_object_ref (stream->udpsink[1]);
3198 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3200 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3202 /* get session RTCP pad */
3203 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3204 pad = gst_element_get_request_pad (src->manager, name);
3209 gst_pad_link (pad, stream->rtcppad);
3210 gst_object_unref (pad);
3219 GST_DEBUG_OBJECT (src, "no destination address specified");
3224 GST_DEBUG_OBJECT (src, "no UDP sink element found");
3229 GST_DEBUG_OBJECT (src, "no fakesrc element found");
3234 /* sets up all elements needed for streaming over the specified transport.
3235 * Does not yet expose the element pads, this will be done when there is actuall
3236 * dataflow detected, which might never happen when UDP is blocked in a
3237 * firewall, for example.
3240 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3241 GstRTSPTransport * transport)
3244 GstPad *outpad = NULL;
3245 GstPadTemplate *template;
3250 src = stream->parent;
3252 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3254 s = gst_caps_get_structure (stream->caps, 0);
3256 /* get the proper mime type for this stream now */
3257 if (gst_rtsp_transport_get_mime (transport->trans, &mime) < 0)
3258 goto unknown_transport;
3260 goto unknown_transport;
3262 /* configure the final mime type */
3263 GST_DEBUG_OBJECT (src, "setting mime to %s", mime);
3264 gst_structure_set_name (s, mime);
3266 /* try to get and configure a manager, channelpad[0-1] will be configured with
3267 * the pads for the manager, or NULL when no manager is needed. */
3268 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3271 switch (transport->lower_transport) {
3272 case GST_RTSP_LOWER_TRANS_TCP:
3273 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3274 goto transport_failed;
3276 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3277 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3278 goto transport_failed;
3279 /* fallthrough, the rest is the same for UDP and MCAST */
3280 case GST_RTSP_LOWER_TRANS_UDP:
3281 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3282 goto transport_failed;
3283 /* configure udpsinks back to the server for RTCP messages and for the
3284 * dummy RTP messages to open NAT. */
3285 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3286 goto transport_failed;
3289 goto unknown_transport;
3293 GST_DEBUG_OBJECT (src, "creating ghostpad");
3295 gst_pad_use_fixed_caps (outpad);
3297 /* create ghostpad, don't add just yet, this will be done when we activate
3299 name = g_strdup_printf ("stream_%u", stream->id);
3300 template = gst_static_pad_template_get (&rtptemplate);
3301 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3302 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3303 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3304 gst_object_unref (template);
3307 gst_object_unref (outpad);
3309 /* mark pad as ok */
3310 stream->last_ret = GST_FLOW_OK;
3317 GST_DEBUG_OBJECT (src, "failed to configure transport");
3322 GST_DEBUG_OBJECT (src, "unknown transport");
3327 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3332 /* send a couple of dummy random packets on the receiver RTP port to the server,
3333 * this should make a firewall think we initiated the data transfer and
3334 * hopefully allow packets to go from the sender port to our RTP receiver port */
3336 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3340 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3343 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3344 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3346 if (stream->fakesrc && stream->udpsink[0]) {
3347 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3348 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3349 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3350 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3351 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3357 /* Adds the source pads of all configured streams to the element.
3358 * This code is performed when we detected dataflow.
3360 * We detect dataflow from either the _loop function or with pad probes on the
3364 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3368 GST_DEBUG_OBJECT (src, "activating streams");
3370 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3371 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3373 if (stream->udpsrc[0]) {
3374 /* remove timeout, we are streaming now and timeouts will be handled by
3375 * the session manager and jitter buffer */
3376 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3378 if (stream->srcpad) {
3379 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3380 gst_pad_set_active (stream->srcpad, TRUE);
3382 /* if we don't have a session manager, set the caps now. If we have a
3383 * session, we will get a notification of the pad and the caps. */
3384 if (!src->manager) {
3385 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3386 gst_pad_set_caps (stream->srcpad, stream->caps);
3389 if (!stream->added) {
3390 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3391 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3392 stream->added = TRUE;
3397 /* unblock all pads */
3398 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3399 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3401 if (stream->blockid) {
3402 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3403 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3404 stream->blockid = 0;
3412 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
3413 gboolean reset_manager)
3416 guint64 start, stop;
3417 gdouble play_speed, play_scale;
3419 GST_DEBUG_OBJECT (src, "configuring stream caps");
3421 start = segment->position;
3422 stop = segment->duration;
3423 play_speed = segment->rate;
3424 play_scale = segment->applied_rate;
3426 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3427 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3430 if ((caps = stream->caps)) {
3431 caps = gst_caps_make_writable (caps);
3433 if (stream->timebase != -1)
3434 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3435 (guint) stream->timebase, NULL);
3436 if (stream->seqbase != -1)
3437 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3438 (guint) stream->seqbase, NULL);
3439 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3441 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3442 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3443 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3445 stream->caps = caps;
3447 GST_DEBUG_OBJECT (src, "stream %p, caps %" GST_PTR_FORMAT, stream, caps);
3449 if (reset_manager && src->manager) {
3450 GST_DEBUG_OBJECT (src, "clear session");
3451 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3455 static GstFlowReturn
3456 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3461 /* store the value */
3462 stream->last_ret = ret;
3464 /* if it's success we can return the value right away */
3465 if (ret == GST_FLOW_OK)
3468 /* any other error that is not-linked can be returned right
3470 if (ret != GST_FLOW_NOT_LINKED)
3473 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3474 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3475 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3477 ret = ostream->last_ret;
3478 /* some other return value (must be SUCCESS but we can return
3479 * other values as well) */
3480 if (ret != GST_FLOW_NOT_LINKED)
3483 /* if we get here, all other pads were unlinked and we return
3484 * NOT_LINKED then */
3490 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
3493 gboolean res = TRUE;
3495 /* only streams that have a connection to the outside world */
3496 if (stream->container || stream->disabled)
3499 if (stream->udpsrc[0]) {
3500 gst_event_ref (event);
3501 res = gst_element_send_event (stream->udpsrc[0], event);
3502 } else if (stream->channelpad[0]) {
3503 gst_event_ref (event);
3504 if (GST_PAD_IS_SRC (stream->channelpad[0]))
3505 res = gst_pad_push_event (stream->channelpad[0], event);
3507 res = gst_pad_send_event (stream->channelpad[0], event);
3510 if (stream->udpsrc[1]) {
3511 gst_event_ref (event);
3512 res &= gst_element_send_event (stream->udpsrc[1], event);
3513 } else if (stream->channelpad[1]) {
3514 gst_event_ref (event);
3515 if (GST_PAD_IS_SRC (stream->channelpad[1]))
3516 res &= gst_pad_push_event (stream->channelpad[1], event);
3518 res &= gst_pad_send_event (stream->channelpad[1], event);
3522 gst_event_unref (event);
3528 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
3531 gboolean res = TRUE;
3533 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3534 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3536 gst_event_ref (event);
3537 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
3539 gst_event_unref (event);
3544 static GstRTSPResult
3545 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3550 if (info->connection == NULL) {
3551 if (info->url == NULL) {
3552 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
3553 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
3557 /* create connection */
3558 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
3559 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
3560 goto could_not_create;
3563 g_free (info->url_str);
3564 info->url_str = gst_rtsp_url_get_request_uri (info->url);
3566 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
3568 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
3569 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
3571 if (src->proxy_host) {
3572 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
3574 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
3579 if (!info->connected) {
3582 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
3583 ("Connecting to %s", info->location));
3584 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
3586 gst_rtsp_connection_connect (info->connection,
3587 src->ptcp_timeout)) < 0)
3588 goto could_not_connect;
3590 info->connected = TRUE;
3597 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
3602 gchar *str = gst_rtsp_strresult (res);
3603 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
3609 gchar *str = gst_rtsp_strresult (res);
3610 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
3616 static GstRTSPResult
3617 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
3620 GST_RTSP_STATE_LOCK (src);
3621 if (info->connected) {
3622 GST_DEBUG_OBJECT (src, "closing connection...");
3623 gst_rtsp_connection_close (info->connection);
3624 info->connected = FALSE;
3626 if (free && info->connection) {
3627 /* free connection */
3628 GST_DEBUG_OBJECT (src, "freeing connection...");
3629 gst_rtsp_connection_free (info->connection);
3630 info->connection = NULL;
3632 GST_RTSP_STATE_UNLOCK (src);
3636 static GstRTSPResult
3637 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3642 GST_DEBUG_OBJECT (src, "reconnecting connection...");
3643 gst_rtsp_conninfo_close (src, info, FALSE);
3644 res = gst_rtsp_conninfo_connect (src, info, async);
3650 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
3654 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
3655 GST_RTSP_STATE_LOCK (src);
3656 if (src->conninfo.connection && src->conninfo.flushing != flush) {
3657 GST_DEBUG_OBJECT (src, "connection flush");
3658 gst_rtsp_connection_flush (src->conninfo.connection, flush);
3659 src->conninfo.flushing = flush;
3661 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3662 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3663 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
3664 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
3665 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
3666 stream->conninfo.flushing = flush;
3669 GST_RTSP_STATE_UNLOCK (src);
3672 /* FIXME, handle server request, reply with OK, for now */
3673 static GstRTSPResult
3674 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
3675 GstRTSPMessage * request)
3677 GstRTSPMessage response = { 0 };
3680 GST_DEBUG_OBJECT (src, "got server request message");
3683 gst_rtsp_message_dump (request);
3685 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
3687 if (res == GST_RTSP_ENOTIMPL) {
3688 /* default implementation, send OK */
3689 GST_DEBUG_OBJECT (src, "prepare OK reply");
3691 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
3696 /* let app parse and reply */
3697 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
3698 0, request, response);
3701 gst_rtsp_message_dump (&response);
3703 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
3707 gst_rtsp_message_unset (&response);
3708 } else if (res == GST_RTSP_EEOF)
3716 gst_rtsp_message_unset (&response);
3721 /* send server keep-alive */
3722 static GstRTSPResult
3723 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
3725 GstRTSPMessage request = { 0 };
3727 GstRTSPMethod method;
3730 if (src->do_rtsp_keep_alive == FALSE) {
3731 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
3732 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3736 GST_DEBUG_OBJECT (src, "creating server keep-alive");
3738 /* find a method to use for keep-alive */
3739 if (src->methods & GST_RTSP_GET_PARAMETER)
3740 method = GST_RTSP_GET_PARAMETER;
3742 method = GST_RTSP_OPTIONS;
3745 control = src->control;
3747 control = src->conninfo.url_str;
3749 if (control == NULL)
3752 res = gst_rtsp_message_init_request (&request, method, control);
3757 gst_rtsp_message_dump (&request);
3760 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
3765 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3766 gst_rtsp_message_unset (&request);
3773 GST_WARNING_OBJECT (src, "no control url to send keepalive");
3778 gchar *str = gst_rtsp_strresult (res);
3780 gst_rtsp_message_unset (&request);
3781 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
3782 ("Could not send keep-alive. (%s)", str));
3788 static GstFlowReturn
3789 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
3791 GstFlowReturn ret = GST_FLOW_OK;
3793 GstRTSPStream *stream;
3794 GstPad *outpad = NULL;
3801 channel = message->type_data.data.channel;
3803 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
3805 goto unknown_stream;
3807 if (channel == stream->channel[0]) {
3808 outpad = stream->channelpad[0];
3810 } else if (channel == stream->channel[1]) {
3811 outpad = stream->channelpad[1];
3817 /* take a look at the body to figure out what we have */
3818 gst_rtsp_message_get_body (message, &data, &size);
3820 goto invalid_length;
3822 /* channels are not correct on some servers, do extra check */
3823 if (data[1] >= 200 && data[1] <= 204) {
3824 /* hmm RTCP message switch to the RTCP pad of the same stream. */
3825 outpad = stream->channelpad[1];
3829 /* we have no clue what this is, just ignore then. */
3831 goto unknown_stream;
3833 /* take the message body for further processing */
3834 gst_rtsp_message_steal_body (message, &data, &size);
3836 /* strip the trailing \0 */
3839 buf = gst_buffer_new ();
3840 gst_buffer_append_memory (buf,
3841 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
3843 /* don't need message anymore */
3844 gst_rtsp_message_unset (message);
3846 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
3849 if (src->need_activate) {
3855 /* generate an SHA256 sum of the URI */
3856 cs = g_checksum_new (G_CHECKSUM_SHA256);
3857 uri = src->conninfo.location;
3858 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
3860 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), stream->id);
3861 g_checksum_free (cs);
3862 event = gst_event_new_stream_start (stream_id);
3864 gst_rtspsrc_push_event (src, event);
3866 gst_rtspsrc_activate_streams (src);
3867 src->need_activate = FALSE;
3869 if ((event = src->start_segment) != NULL) {
3870 src->start_segment = NULL;
3871 gst_rtspsrc_push_event (src, event);
3874 if (src->base_time == -1) {
3875 /* Take current running_time. This timestamp will be put on
3876 * the first buffer of each stream because we are a live source and so we
3877 * timestamp with the running_time. When we are dealing with TCP, we also
3878 * only timestamp the first buffer (using the DISCONT flag) because a server
3879 * typically bursts data, for which we don't want to compensate by speeding
3880 * up the media. The other timestamps will be interpollated from this one
3881 * using the RTP timestamps. */
3882 GST_OBJECT_LOCK (src);
3883 if (GST_ELEMENT_CLOCK (src)) {
3885 GstClockTime base_time;
3887 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
3888 base_time = GST_ELEMENT_CAST (src)->base_time;
3890 src->base_time = now - base_time;
3892 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
3893 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
3895 GST_OBJECT_UNLOCK (src);
3898 if (stream->discont && !is_rtcp) {
3899 /* mark first RTP buffer as discont */
3900 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
3901 stream->discont = FALSE;
3902 /* first buffer gets the timestamp, other buffers are not timestamped and
3903 * their presentation time will be interpollated from the rtp timestamps. */
3904 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
3905 GST_TIME_ARGS (src->base_time));
3907 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
3910 /* chain to the peer pad */
3911 if (GST_PAD_IS_SINK (outpad))
3912 ret = gst_pad_chain (outpad, buf);
3914 ret = gst_pad_push (outpad, buf);
3917 /* combine all stream flows for the data transport */
3918 ret = gst_rtspsrc_combine_flows (src, stream, ret);
3925 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
3926 gst_rtsp_message_unset (message);
3931 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
3932 ("Short message received, ignoring."));
3933 gst_rtsp_message_unset (message);
3938 static GstFlowReturn
3939 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
3941 GstRTSPMessage message = { 0 };
3943 GstFlowReturn ret = GST_FLOW_OK;
3944 GTimeVal tv_timeout;
3947 /* get the next timeout interval */
3948 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3950 /* see if the timeout period expired */
3951 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
3952 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
3953 /* send keep-alive, only act on interrupt, a warning will be posted for
3955 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3957 /* get new timeout */
3958 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
3961 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
3962 tv_timeout.tv_sec, tv_timeout.tv_usec);
3964 /* protect the connection with the connection lock so that we can see when
3965 * we are finished doing server communication */
3967 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
3968 &message, src->ptcp_timeout);
3972 GST_DEBUG_OBJECT (src, "we received a server message");
3974 case GST_RTSP_EINTR:
3975 /* we got interrupted this means we need to stop */
3977 case GST_RTSP_ETIMEOUT:
3978 /* no reply, send keep alive */
3979 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
3980 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
3984 /* go EOS when the server closed the connection */
3990 switch (message.type) {
3991 case GST_RTSP_MESSAGE_REQUEST:
3992 /* server sends us a request message, handle it */
3994 gst_rtspsrc_handle_request (src, src->conninfo.connection,
3996 if (res == GST_RTSP_EEOF)
3999 goto handle_request_failed;
4001 case GST_RTSP_MESSAGE_RESPONSE:
4002 /* we ignore response messages */
4003 GST_DEBUG_OBJECT (src, "ignoring response message");
4005 gst_rtsp_message_dump (&message);
4007 case GST_RTSP_MESSAGE_DATA:
4008 GST_DEBUG_OBJECT (src, "got data message");
4009 ret = gst_rtspsrc_handle_data (src, &message);
4010 if (ret != GST_FLOW_OK)
4011 goto handle_data_failed;
4014 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4019 g_assert_not_reached ();
4024 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4025 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4026 ("The server closed the connection."));
4027 src->conninfo.connected = FALSE;
4028 gst_rtsp_message_unset (&message);
4029 return GST_FLOW_EOS;
4033 gst_rtsp_message_unset (&message);
4034 GST_DEBUG_OBJECT (src, "got interrupted");
4035 return GST_FLOW_FLUSHING;
4039 gchar *str = gst_rtsp_strresult (res);
4041 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4042 ("Could not receive message. (%s)", str));
4045 gst_rtsp_message_unset (&message);
4046 return GST_FLOW_ERROR;
4048 handle_request_failed:
4050 gchar *str = gst_rtsp_strresult (res);
4052 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4053 ("Could not handle server message. (%s)", str));
4055 gst_rtsp_message_unset (&message);
4056 return GST_FLOW_ERROR;
4060 GST_DEBUG_OBJECT (src, "could no handle data message");
4065 static GstFlowReturn
4066 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4069 GstRTSPMessage message = { 0 };
4073 GTimeVal tv_timeout;
4075 /* get the next timeout interval */
4076 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4078 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4079 (gint) tv_timeout.tv_sec);
4081 gst_rtsp_message_unset (&message);
4083 /* we should continue reading the TCP socket because the server might
4084 * send us requests. When the session timeout expires, we need to send a
4085 * keep-alive request to keep the session open. */
4086 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4087 &message, &tv_timeout);
4091 GST_DEBUG_OBJECT (src, "we received a server message");
4093 case GST_RTSP_EINTR:
4094 /* we got interrupted, see what we have to do */
4096 case GST_RTSP_ETIMEOUT:
4097 /* send keep-alive, ignore the result, a warning will be posted. */
4098 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4099 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4103 /* server closed the connection. not very fatal for UDP, reconnect and
4104 * see what happens. */
4105 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4106 ("The server closed the connection."));
4107 if (src->udp_reconnect) {
4109 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4116 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4118 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4119 ("Unhandled return value %d.", res));
4123 switch (message.type) {
4124 case GST_RTSP_MESSAGE_REQUEST:
4125 /* server sends us a request message, handle it */
4127 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4129 if (res == GST_RTSP_EEOF)
4132 goto handle_request_failed;
4134 case GST_RTSP_MESSAGE_RESPONSE:
4135 /* we ignore response and data messages */
4136 GST_DEBUG_OBJECT (src, "ignoring response message");
4138 gst_rtsp_message_dump (&message);
4139 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4140 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4141 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4142 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4143 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4150 case GST_RTSP_MESSAGE_DATA:
4151 /* we ignore response and data messages */
4152 GST_DEBUG_OBJECT (src, "ignoring data message");
4155 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4160 g_assert_not_reached ();
4162 /* we get here when the connection got interrupted */
4165 gst_rtsp_message_unset (&message);
4166 GST_DEBUG_OBJECT (src, "got interrupted");
4167 return GST_FLOW_FLUSHING;
4171 gchar *str = gst_rtsp_strresult (res);
4174 src->conninfo.connected = FALSE;
4175 if (res != GST_RTSP_EINTR) {
4176 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4177 ("Could not connect to server. (%s)", str));
4179 ret = GST_FLOW_ERROR;
4181 ret = GST_FLOW_FLUSHING;
4187 gchar *str = gst_rtsp_strresult (res);
4189 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4190 ("Could not receive message. (%s)", str));
4192 return GST_FLOW_ERROR;
4194 handle_request_failed:
4196 gchar *str = gst_rtsp_strresult (res);
4199 gst_rtsp_message_unset (&message);
4200 if (res != GST_RTSP_EINTR) {
4201 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4202 ("Could not handle server message. (%s)", str));
4204 ret = GST_FLOW_ERROR;
4206 ret = GST_FLOW_FLUSHING;
4212 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4213 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4214 ("The server closed the connection."));
4215 src->conninfo.connected = FALSE;
4216 gst_rtsp_message_unset (&message);
4217 return GST_FLOW_EOS;
4221 static GstRTSPResult
4222 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4224 GstRTSPResult res = GST_RTSP_OK;
4227 GST_DEBUG_OBJECT (src, "doing reconnect");
4229 GST_OBJECT_LOCK (src);
4230 /* only restart when the pads were not yet activated, else we were
4231 * streaming over UDP */
4232 restart = src->need_activate;
4233 GST_OBJECT_UNLOCK (src);
4235 /* no need to restart, we're done */
4239 /* we can try only TCP now */
4240 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
4242 /* close and cleanup our state */
4243 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
4246 /* see if we have TCP left to try. Also don't try TCP when we were configured
4248 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
4251 /* We post a warning message now to inform the user
4252 * that nothing happened. It's most likely a firewall thing. */
4253 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4254 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4255 "firewall is blocking it. Retrying using a TCP connection.",
4256 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4258 /* open new connection using tcp */
4259 if (gst_rtspsrc_open (src, async) < 0)
4262 /* start playback */
4263 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
4272 src->cur_protocols = 0;
4273 /* no transport possible, post an error and stop */
4274 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4275 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4276 "firewall is blocking it. No other protocols to try.",
4277 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4278 return GST_RTSP_ERROR;
4282 GST_DEBUG_OBJECT (src, "open failed");
4287 GST_DEBUG_OBJECT (src, "play failed");
4293 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
4297 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
4300 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
4303 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
4306 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
4314 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
4318 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
4321 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
4324 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
4327 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4335 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4339 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4342 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4345 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4348 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4356 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4360 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
4363 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4366 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4369 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4377 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4379 if (ret == GST_RTSP_OK)
4380 gst_rtspsrc_loop_complete_cmd (src, cmd);
4381 else if (ret == GST_RTSP_EINTR)
4382 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4384 gst_rtspsrc_loop_error_cmd (src, cmd);
4388 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
4392 /* start new request */
4393 gst_rtspsrc_loop_start_cmd (src, cmd);
4395 GST_DEBUG_OBJECT (src, "sending cmd %d", cmd);
4397 GST_OBJECT_LOCK (src);
4398 old = src->pending_cmd;
4399 if (old == CMD_RECONNECT) {
4400 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
4401 cmd = CMD_RECONNECT;
4403 if (old != CMD_WAIT) {
4404 src->pending_cmd = CMD_WAIT;
4405 GST_OBJECT_UNLOCK (src);
4406 /* cancel previous request */
4407 GST_DEBUG_OBJECT (src, "cancel previous request %d", old);
4408 gst_rtspsrc_loop_cancel_cmd (src, old);
4409 GST_OBJECT_LOCK (src);
4411 src->pending_cmd = cmd;
4412 /* interrupt if allowed */
4413 if (src->busy_cmd & mask) {
4414 GST_DEBUG_OBJECT (src, "connection flush busy %d", src->busy_cmd);
4415 gst_rtspsrc_connection_flush (src, TRUE);
4417 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %d", src->busy_cmd);
4420 gst_task_start (src->task);
4421 GST_OBJECT_UNLOCK (src);
4425 gst_rtspsrc_loop (GstRTSPSrc * src)
4429 if (!src->conninfo.connection || !src->conninfo.connected)
4432 if (src->interleaved)
4433 ret = gst_rtspsrc_loop_interleaved (src);
4435 ret = gst_rtspsrc_loop_udp (src);
4437 if (ret != GST_FLOW_OK)
4445 GST_WARNING_OBJECT (src, "we are not connected");
4446 ret = GST_FLOW_FLUSHING;
4451 const gchar *reason = gst_flow_get_name (ret);
4453 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
4454 src->running = FALSE;
4455 if (ret == GST_FLOW_EOS) {
4456 /* perform EOS logic */
4457 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4458 gst_element_post_message (GST_ELEMENT_CAST (src),
4459 gst_message_new_segment_done (GST_OBJECT_CAST (src),
4460 src->segment.format, src->segment.position));
4461 gst_rtspsrc_push_event (src,
4462 gst_event_new_segment_done (src->segment.format,
4463 src->segment.position));
4465 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4467 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
4468 /* for fatal errors we post an error message, post the error before the
4469 * EOS so the app knows about the error first. */
4470 GST_ELEMENT_ERROR (src, STREAM, FAILED,
4471 ("Internal data flow error."),
4472 ("streaming task paused, reason %s (%d)", reason, ret));
4473 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4475 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
4480 #ifndef GST_DISABLE_GST_DEBUG
4481 static const gchar *
4482 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
4486 while (method != 0) {
4503 static const gchar *
4504 gst_rtspsrc_skip_lws (const gchar * s)
4506 while (g_ascii_isspace (*s))
4511 static const gchar *
4512 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
4514 while (s > start && g_ascii_isspace (*(s - 1)))
4519 static const gchar *
4520 gst_rtspsrc_skip_commas (const gchar * s)
4522 /* The grammar allows for multiple commas */
4523 while (g_ascii_isspace (*s) || *s == ',')
4528 static const gchar *
4529 gst_rtspsrc_skip_item (const gchar * s)
4531 gboolean quoted = FALSE;
4532 const gchar *start = s;
4534 /* A list item ends at the last non-whitespace character
4535 * before a comma which is not inside a quoted-string. Or at
4536 * the end of the string.
4542 if (*s == '\\' && *(s + 1))
4551 return gst_rtspsrc_unskip_lws (s, start);
4555 gst_rtsp_decode_quoted_string (gchar * quoted_string)
4559 src = quoted_string + 1;
4560 dst = quoted_string;
4561 while (*src && *src != '"') {
4562 if (*src == '\\' && *(src + 1))
4569 /* Extract the authentication tokens that the server provided for each method
4570 * into an array of structures and give those to the connection object.
4573 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
4574 const gchar * header, gboolean * stale)
4576 GSList *list = NULL, *iter;
4578 gchar *item, *eq, *name_end, *value;
4580 g_return_if_fail (stale != NULL);
4582 gst_rtsp_connection_clear_auth_params (conn);
4585 /* Parse a header whose content is described by RFC2616 as
4586 * "#something", where "something" does not itself contain commas,
4587 * except as part of quoted-strings, into a list of allocated strings.
4589 header = gst_rtspsrc_skip_commas (header);
4591 end = gst_rtspsrc_skip_item (header);
4592 list = g_slist_prepend (list, g_strndup (header, end - header));
4593 header = gst_rtspsrc_skip_commas (end);
4598 list = g_slist_reverse (list);
4599 for (iter = list; iter; iter = iter->next) {
4602 eq = strchr (item, '=');
4604 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
4605 if (name_end == item) {
4606 /* That's no good... */
4613 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
4615 gst_rtsp_decode_quoted_string (value);
4619 if (item && (strcmp (item, "stale") == 0) &&
4620 value && (strcmp (value, "TRUE") == 0))
4622 gst_rtsp_connection_set_auth_param (conn, item, value);
4626 g_slist_free (list);
4629 /* Parse a WWW-Authenticate Response header and determine the
4630 * available authentication methods
4632 * This code should also cope with the fact that each WWW-Authenticate
4633 * header can contain multiple challenge methods + tokens
4635 * At the moment, for Basic auth, we just do a minimal check and don't
4636 * even parse out the realm */
4638 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
4639 GstRTSPConnection * conn, gboolean * stale)
4643 g_return_if_fail (hdr != NULL);
4644 g_return_if_fail (methods != NULL);
4645 g_return_if_fail (stale != NULL);
4647 /* Skip whitespace at the start of the string */
4648 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
4650 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
4651 *methods |= GST_RTSP_AUTH_BASIC;
4652 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
4653 *methods |= GST_RTSP_AUTH_DIGEST;
4654 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
4659 * gst_rtspsrc_setup_auth:
4660 * @src: the rtsp source
4662 * Configure a username and password and auth method on the
4663 * connection object based on a response we received from the
4666 * Currently, this requires that a username and password were supplied
4667 * in the uri. In the future, they may be requested on demand by sending
4668 * a message up the bus.
4670 * Returns: TRUE if authentication information could be set up correctly.
4673 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
4677 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
4678 GstRTSPAuthMethod method;
4679 GstRTSPResult auth_result;
4681 GstRTSPConnection *conn;
4683 gboolean stale = FALSE;
4685 conn = src->conninfo.connection;
4687 /* Identify the available auth methods and see if any are supported */
4688 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
4689 &hdr, 0) == GST_RTSP_OK) {
4690 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
4693 if (avail_methods == GST_RTSP_AUTH_NONE)
4694 goto no_auth_available;
4696 /* For digest auth, if the response indicates that the session
4697 * data are stale, we just update them in the connection object and
4698 * return TRUE to retry the request */
4700 src->tried_url_auth = FALSE;
4702 url = gst_rtsp_connection_get_url (conn);
4704 /* Do we have username and password available? */
4705 if (url != NULL && !src->tried_url_auth && url->user != NULL
4706 && url->passwd != NULL) {
4709 src->tried_url_auth = TRUE;
4710 GST_DEBUG_OBJECT (src,
4711 "Attempting authentication using credentials from the URL");
4713 user = src->user_id;
4714 pass = src->user_pw;
4715 GST_DEBUG_OBJECT (src,
4716 "Attempting authentication using credentials from the properties");
4719 /* FIXME: If the url didn't contain username and password or we tried them
4720 * already, request a username and passwd from the application via some kind
4721 * of credentials request message */
4723 /* If we don't have a username and passwd at this point, bail out. */
4724 if (user == NULL || pass == NULL)
4727 /* Try to configure for each available authentication method, strongest to
4729 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
4730 /* Check if this method is available on the server */
4731 if ((method & avail_methods) == 0)
4734 /* Pass the credentials to the connection to try on the next request */
4735 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
4736 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
4737 * ignore it and end up retrying later */
4738 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
4739 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
4740 gst_rtsp_auth_method_to_string (method));
4745 if (method == GST_RTSP_AUTH_NONE)
4746 goto no_auth_available;
4752 /* Output an error indicating that we couldn't connect because there were
4753 * no supported authentication protocols */
4754 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4755 ("No supported authentication protocol was found"));
4760 /* We don't fire an error message, we just return FALSE and let the
4761 * normal NOT_AUTHORIZED error be propagated */
4766 static GstRTSPResult
4767 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4768 GstRTSPMessage * request, GstRTSPMessage * response,
4769 GstRTSPStatusCode * code)
4772 GstRTSPStatusCode thecode;
4773 gchar *content_base = NULL;
4777 if (!src->short_header)
4778 gst_rtsp_ext_list_before_send (src->extensions, request);
4780 GST_DEBUG_OBJECT (src, "sending message");
4783 gst_rtsp_message_dump (request);
4785 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
4789 gst_rtsp_connection_reset_timeout (conn);
4792 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
4797 gst_rtsp_message_dump (response);
4799 switch (response->type) {
4800 case GST_RTSP_MESSAGE_REQUEST:
4801 res = gst_rtspsrc_handle_request (src, conn, response);
4802 if (res == GST_RTSP_EEOF)
4805 goto handle_request_failed;
4807 case GST_RTSP_MESSAGE_RESPONSE:
4808 /* ok, a response is good */
4809 GST_DEBUG_OBJECT (src, "received response message");
4811 case GST_RTSP_MESSAGE_DATA:
4812 /* get next response */
4813 GST_DEBUG_OBJECT (src, "handle data response message");
4814 gst_rtspsrc_handle_data (src, response);
4817 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4822 thecode = response->type_data.response.code;
4824 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
4826 /* if the caller wanted the result code, we store it. */
4830 /* If the request didn't succeed, bail out before doing any more */
4831 if (thecode != GST_RTSP_STS_OK)
4834 /* store new content base if any */
4835 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
4838 g_free (src->content_base);
4839 src->content_base = g_strdup (content_base);
4841 gst_rtsp_ext_list_after_send (src->extensions, request, response);
4848 gchar *str = gst_rtsp_strresult (res);
4850 if (res != GST_RTSP_EINTR) {
4851 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4852 ("Could not send message. (%s)", str));
4854 GST_WARNING_OBJECT (src, "send interrupted");
4863 GST_WARNING_OBJECT (src, "server closed connection");
4864 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
4866 /* if reconnect succeeds, try again */
4868 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
4872 /* only try once after reconnect, then fallthrough and error out */
4875 gchar *str = gst_rtsp_strresult (res);
4877 if (res != GST_RTSP_EINTR) {
4878 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4879 ("Could not receive message. (%s)", str));
4881 GST_WARNING_OBJECT (src, "receive interrupted");
4889 handle_request_failed:
4891 /* ERROR was posted */
4892 gst_rtsp_message_unset (response);
4897 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4898 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4899 ("The server closed the connection."));
4900 gst_rtsp_message_unset (response);
4907 * @src: the rtsp source
4908 * @conn: the connection to send on
4909 * @request: must point to a valid request
4910 * @response: must point to an empty #GstRTSPMessage
4911 * @code: an optional code result
4913 * send @request and retrieve the response in @response. optionally @code can be
4914 * non-NULL in which case it will contain the status code of the response.
4916 * If This function returns #GST_RTSP_OK, @response will contain a valid response
4917 * message that should be cleaned with gst_rtsp_message_unset() after usage.
4919 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
4920 * @response message) if the response code was not 200 (OK).
4922 * If the attempt results in an authentication failure, then this will attempt
4923 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
4926 * Returns: #GST_RTSP_OK if the processing was successful.
4928 static GstRTSPResult
4929 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4930 GstRTSPMessage * request, GstRTSPMessage * response,
4931 GstRTSPStatusCode * code)
4933 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
4934 GstRTSPResult res = GST_RTSP_ERROR;
4937 GstRTSPMethod method = GST_RTSP_INVALID;
4943 /* make sure we don't loop forever */
4947 /* save method so we can disable it when the server complains */
4948 method = request->type_data.request.method;
4951 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
4955 case GST_RTSP_STS_UNAUTHORIZED:
4956 if (gst_rtspsrc_setup_auth (src, response)) {
4957 /* Try the request/response again after configuring the auth info
4965 } while (retry == TRUE);
4967 /* If the user requested the code, let them handle errors, otherwise
4968 * post an error below */
4971 else if (int_code != GST_RTSP_STS_OK)
4972 goto error_response;
4979 GST_DEBUG_OBJECT (src, "got error %d", res);
4984 res = GST_RTSP_ERROR;
4986 switch (response->type_data.response.code) {
4987 case GST_RTSP_STS_NOT_FOUND:
4988 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
4989 response->type_data.response.reason));
4991 case GST_RTSP_STS_MOVED_PERMANENTLY:
4992 case GST_RTSP_STS_MOVE_TEMPORARILY:
4994 gchar *new_location;
4995 GstRTSPLowerTrans transports;
4997 GST_DEBUG_OBJECT (src, "got redirection");
4998 /* if we don't have a Location Header, we must error */
4999 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5000 &new_location, 0) < 0)
5003 /* When we receive a redirect result, we go back to the INIT state after
5004 * parsing the new URI. The caller should do the needed steps to issue
5005 * a new setup when it detects this state change. */
5006 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5008 /* save current transports */
5009 if (src->conninfo.url)
5010 transports = src->conninfo.url->transports;
5012 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5014 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5016 /* set old transports */
5017 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5018 src->conninfo.url->transports = transports;
5020 src->need_redirect = TRUE;
5021 src->state = GST_RTSP_STATE_INIT;
5025 case GST_RTSP_STS_NOT_ACCEPTABLE:
5026 case GST_RTSP_STS_NOT_IMPLEMENTED:
5027 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5028 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5029 gst_rtsp_method_as_text (method));
5030 src->methods &= ~method;
5034 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5035 ("Got error response: %d (%s).", response->type_data.response.code,
5036 response->type_data.response.reason));
5039 /* if we return ERROR we should unset the response ourselves */
5040 if (res == GST_RTSP_ERROR)
5041 gst_rtsp_message_unset (response);
5047 static GstRTSPResult
5048 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5049 GstRTSPMessage * response, GstRTSPSrc * src)
5051 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5056 /* parse the response and collect all the supported methods. We need this
5057 * information so that we don't try to send an unsupported request to the
5061 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5063 GstRTSPHeaderField field;
5067 /* reset supported methods */
5070 /* Try Allow Header first */
5071 field = GST_RTSP_HDR_ALLOW;
5074 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5075 if (indx == 0 && !respoptions) {
5076 /* if no Allow header was found then try the Public header... */
5077 field = GST_RTSP_HDR_PUBLIC;
5078 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5083 src->methods |= gst_rtsp_options_from_text (respoptions);
5088 if (src->methods == 0) {
5089 /* neither Allow nor Public are required, assume the server supports
5090 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5092 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5093 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5095 /* always assume PLAY, FIXME, extensions should be able to override
5097 src->methods |= GST_RTSP_PLAY;
5098 /* also assume it will support Range */
5099 src->seekable = TRUE;
5101 /* we need describe and setup */
5102 if (!(src->methods & GST_RTSP_DESCRIBE))
5104 if (!(src->methods & GST_RTSP_SETUP))
5112 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5113 ("Server does not support DESCRIBE."));
5118 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5119 ("Server does not support SETUP."));
5124 /* masks to be kept in sync with the hardcoded protocol order of preference
5126 static guint protocol_masks[] = {
5127 GST_RTSP_LOWER_TRANS_UDP,
5128 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5129 GST_RTSP_LOWER_TRANS_TCP,
5133 static GstRTSPResult
5134 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5135 GstRTSPLowerTrans protocols, gchar ** transports)
5139 gboolean add_udp_str;
5144 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5149 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5151 /* extension listed transports, use those */
5152 if (*transports != NULL)
5155 /* it's the default */
5156 add_udp_str = FALSE;
5158 /* the default RTSP transports */
5159 result = g_string_new ("");
5160 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5161 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5163 g_string_append (result, "RTP/AVP");
5165 g_string_append (result, "/UDP");
5166 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5167 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5168 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5170 /* we don't have to allocate any UDP ports yet, if the selected transport
5171 * turns out to be multicast we can create them and join the multicast
5172 * group indicated in the transport reply */
5173 if (result->len > 0)
5174 g_string_append (result, ",");
5175 g_string_append (result, "RTP/AVP");
5177 g_string_append (result, "/UDP");
5178 g_string_append (result, ";multicast");
5179 if (src->next_port_num != 0) {
5180 if (src->client_port_range.max > 0 &&
5181 src->next_port_num >= src->client_port_range.max)
5184 g_string_append_printf (result, ";client_port=%d-%d",
5185 src->next_port_num, src->next_port_num + 1);
5187 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
5188 GST_DEBUG_OBJECT (src, "adding TCP");
5190 if (result->len > 0)
5191 g_string_append (result, ",");
5192 g_string_append (result, "RTP/AVP/TCP;unicast;interleaved=%%i1-%%i2");
5194 *transports = g_string_free (result, FALSE);
5196 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5203 GST_ERROR ("extension gave error %d", res);
5208 GST_ERROR ("no more ports available");
5209 return GST_RTSP_ERROR;
5213 static GstRTSPResult
5214 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5215 gint orig_rtpport, gint orig_rtcpport)
5218 gint nr_udp, nr_int;
5220 gint rtpport = 0, rtcpport = 0;
5223 src = stream->parent;
5225 /* find number of placeholders first */
5226 if (strstr (*transports, "%%i2"))
5228 else if (strstr (*transports, "%%i1"))
5233 if (strstr (*transports, "%%u2"))
5235 else if (strstr (*transports, "%%u1"))
5240 if (nr_udp == 0 && nr_int == 0)
5244 if (!orig_rtpport || !orig_rtcpport) {
5245 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5248 rtpport = orig_rtpport;
5249 rtcpport = orig_rtcpport;
5253 str = g_string_new ("");
5255 while ((next = strstr (p, "%%"))) {
5256 g_string_append_len (str, p, next - p);
5257 if (next[2] == 'u') {
5259 g_string_append_printf (str, "%d", rtpport);
5260 else if (next[3] == '2')
5261 g_string_append_printf (str, "%d", rtcpport);
5263 if (next[2] == 'i') {
5265 g_string_append_printf (str, "%d", src->free_channel);
5266 else if (next[3] == '2')
5267 g_string_append_printf (str, "%d", src->free_channel + 1);
5272 /* append final part */
5273 g_string_append (str, p);
5275 g_free (*transports);
5276 *transports = g_string_free (str, FALSE);
5284 GST_ERROR ("failed to allocate udp ports");
5285 return GST_RTSP_ERROR;
5290 gst_rtspsrc_stream_is_real_media (GstRTSPStream * stream)
5292 gboolean res = FALSE;
5296 const gchar *enc = NULL;
5298 s = gst_caps_get_structure (stream->caps, 0);
5299 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
5300 res = (strstr (enc, "-REAL") != NULL);
5306 /* Perform the SETUP request for all the streams.
5308 * We ask the server for a specific transport, which initially includes all the
5309 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
5310 * two local UDP ports that we send to the server.
5312 * Once the server replied with a transport, we configure the other streams
5313 * with the same transport.
5315 * This function will also configure the stream for the selected transport,
5316 * which basically means creating the pipeline.
5318 static GstRTSPResult
5319 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
5322 GstRTSPResult res = GST_RTSP_ERROR;
5323 GstRTSPMessage request = { 0 };
5324 GstRTSPMessage response = { 0 };
5325 GstRTSPStream *stream = NULL;
5326 GstRTSPLowerTrans protocols;
5327 GstRTSPStatusCode code;
5328 gboolean unsupported_real = FALSE;
5329 gint rtpport, rtcpport;
5333 if (src->conninfo.connection) {
5334 url = gst_rtsp_connection_get_url (src->conninfo.connection);
5335 /* we initially allow all configured lower transports. based on the URL
5336 * transports and the replies from the server we narrow them down. */
5337 protocols = url->transports & src->cur_protocols;
5340 protocols = src->cur_protocols;
5346 /* reset some state */
5347 src->free_channel = 0;
5348 src->interleaved = FALSE;
5349 src->need_activate = FALSE;
5350 /* keep track of next port number, 0 is random */
5351 src->next_port_num = src->client_port_range.min;
5352 rtpport = rtcpport = 0;
5354 if (G_UNLIKELY (src->streams == NULL))
5357 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5358 GstRTSPConnection *conn;
5363 stream = (GstRTSPStream *) walk->data;
5365 /* see if we need to configure this stream */
5366 if (!gst_rtsp_ext_list_configure_stream (src->extensions, stream->caps)) {
5367 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
5369 stream->disabled = TRUE;
5373 /* merge/overwrite global caps */
5378 s = gst_caps_get_structure (stream->caps, 0);
5380 num = gst_structure_n_fields (src->props);
5381 for (j = 0; j < num; j++) {
5385 name = gst_structure_nth_field_name (src->props, j);
5386 val = gst_structure_get_value (src->props, name);
5387 gst_structure_set_value (s, name, val);
5389 GST_DEBUG_OBJECT (src, "copied %s", name);
5393 /* skip setup if we have no URL for it */
5394 if (stream->conninfo.location == NULL) {
5395 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
5399 if (src->conninfo.connection == NULL) {
5400 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
5401 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
5404 conn = stream->conninfo.connection;
5406 conn = src->conninfo.connection;
5408 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
5409 stream->conninfo.location);
5411 /* if we have a multicast connection, only suggest multicast from now on */
5412 if (stream->is_multicast)
5413 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
5416 /* first selectable protocol */
5417 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5419 if (!protocol_masks[mask])
5423 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
5424 protocol_masks[mask]);
5425 /* create a string with first transport in line */
5427 res = gst_rtspsrc_create_transports_string (src,
5428 protocols & protocol_masks[mask], &transports);
5429 if (res < 0 || transports == NULL)
5430 goto setup_transport_failed;
5432 if (strlen (transports) == 0) {
5433 g_free (transports);
5434 GST_DEBUG_OBJECT (src, "no transports found");
5439 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
5441 /* replace placeholders with real values, this function will optionally
5442 * allocate UDP ports and other info needed to execute the setup request */
5443 res = gst_rtspsrc_prepare_transports (stream, &transports,
5444 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
5446 g_free (transports);
5447 goto setup_transport_failed;
5450 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
5452 /* create SETUP request */
5454 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
5455 stream->conninfo.location);
5457 g_free (transports);
5458 goto create_request_failed;
5461 /* select transport */
5462 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
5464 /* if the user wants a non default RTP packet size we add the blocksize
5466 if (src->rtp_blocksize > 0) {
5467 hval = g_strdup_printf ("%d", src->rtp_blocksize);
5468 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
5473 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
5476 /* handle the code ourselves */
5477 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
5481 case GST_RTSP_STS_OK:
5483 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
5484 gst_rtsp_message_unset (&request);
5485 gst_rtsp_message_unset (&response);
5486 /* cleanup of leftover transport */
5487 gst_rtspsrc_stream_free_udp (stream);
5488 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
5489 * we might be in this case */
5490 if (stream->container && rtpport && rtcpport && !retry) {
5491 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
5496 /* this transport did not go down well, but we may have others to try
5497 * that we did not send yet, try those and only give up then
5498 * but not without checking for lost cause/extension so we can
5499 * post a nicer/more useful error message later */
5500 if (!unsupported_real)
5501 unsupported_real = gst_rtspsrc_stream_is_real_media (stream);
5502 /* select next available protocol, give up on this stream if none */
5504 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5506 if (!protocol_masks[mask] || unsupported_real)
5511 /* cleanup of leftover transport and move to the next stream */
5512 gst_rtspsrc_stream_free_udp (stream);
5513 goto response_error;
5516 /* parse response transport */
5518 gchar *resptrans = NULL;
5519 GstRTSPTransport transport = { 0 };
5521 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
5524 gst_rtspsrc_stream_free_udp (stream);
5528 /* parse transport, go to next stream on parse error */
5529 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
5530 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
5534 /* update allowed transports for other streams. once the transport of
5535 * one stream has been determined, we make sure that all other streams
5536 * are configured in the same way */
5537 switch (transport.lower_transport) {
5538 case GST_RTSP_LOWER_TRANS_TCP:
5539 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
5540 protocols = GST_RTSP_LOWER_TRANS_TCP;
5541 src->interleaved = TRUE;
5542 /* update free channels */
5544 MAX (transport.interleaved.min, src->free_channel);
5546 MAX (transport.interleaved.max, src->free_channel);
5547 src->free_channel++;
5549 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
5550 /* only allow multicast for other streams */
5551 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
5552 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
5553 /* if the server selected our ports, increment our counters so that
5554 * we select a new port later */
5555 if (src->next_port_num == transport.port.min &&
5556 src->next_port_num + 1 == transport.port.max) {
5557 src->next_port_num += 2;
5560 case GST_RTSP_LOWER_TRANS_UDP:
5561 /* only allow unicast for other streams */
5562 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
5563 protocols = GST_RTSP_LOWER_TRANS_UDP;
5566 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
5567 transport.lower_transport);
5571 if (!stream->container || (!src->interleaved && !retry)) {
5572 /* now configure the stream with the selected transport */
5573 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
5574 GST_DEBUG_OBJECT (src,
5575 "could not configure stream %p transport, skipping stream",
5578 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
5579 /* retain the first allocated UDP port pair */
5580 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
5581 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
5584 /* we need to activate at least one streams when we detect activity */
5585 src->need_activate = TRUE;
5587 /* clean up our transport struct */
5588 gst_rtsp_transport_init (&transport);
5589 /* clean up used RTSP messages */
5590 gst_rtsp_message_unset (&request);
5591 gst_rtsp_message_unset (&response);
5595 /* store the transport protocol that was configured */
5596 src->cur_protocols = protocols;
5598 gst_rtsp_ext_list_stream_select (src->extensions, url);
5600 /* if there is nothing to activate, error out */
5601 if (!src->need_activate)
5602 goto nothing_to_activate;
5609 /* no transport possible, post an error and stop */
5610 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5611 ("Could not connect to server, no protocols left"));
5612 return GST_RTSP_ERROR;
5616 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5617 ("SDP contains no streams"));
5618 return GST_RTSP_ERROR;
5620 create_request_failed:
5622 gchar *str = gst_rtsp_strresult (res);
5624 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5625 ("Could not create request. (%s)", str));
5629 setup_transport_failed:
5631 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5632 ("Could not setup transport."));
5633 res = GST_RTSP_ERROR;
5638 const gchar *str = gst_rtsp_status_as_text (code);
5640 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5641 ("Error (%d): %s", code, GST_STR_NULL (str)));
5642 res = GST_RTSP_ERROR;
5647 gchar *str = gst_rtsp_strresult (res);
5649 if (res != GST_RTSP_EINTR) {
5650 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5651 ("Could not send message. (%s)", str));
5653 GST_WARNING_OBJECT (src, "send interrupted");
5660 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5661 ("Server did not select transport."));
5662 res = GST_RTSP_ERROR;
5665 nothing_to_activate:
5667 /* none of the available error codes is really right .. */
5668 if (unsupported_real) {
5669 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5670 (_("No supported stream was found. You might need to install a "
5671 "GStreamer RTSP extension plugin for Real media streams.")),
5674 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5675 (_("No supported stream was found. You might need to allow "
5676 "more transport protocols or may otherwise be missing "
5677 "the right GStreamer RTSP extension plugin.")), (NULL));
5679 return GST_RTSP_ERROR;
5683 gst_rtsp_message_unset (&request);
5684 gst_rtsp_message_unset (&response);
5690 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
5691 GstSegment * segment)
5694 GstRTSPTimeRange *therange;
5697 gst_rtsp_range_free (src->range);
5699 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
5700 GST_DEBUG_OBJECT (src, "parsed range %s", range);
5701 src->range = therange;
5703 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
5705 gst_segment_init (segment, GST_FORMAT_TIME);
5709 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
5710 therange->min.type, therange->min.seconds, therange->max.type,
5711 therange->max.seconds);
5713 if (therange->min.type == GST_RTSP_TIME_NOW)
5715 else if (therange->min.type == GST_RTSP_TIME_END)
5718 seconds = therange->min.seconds * GST_SECOND;
5720 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
5721 GST_TIME_ARGS (seconds));
5723 /* we need to start playback without clipping from the position reported by
5725 segment->start = seconds;
5726 segment->position = seconds;
5728 if (therange->max.type == GST_RTSP_TIME_NOW)
5730 else if (therange->max.type == GST_RTSP_TIME_END)
5733 seconds = therange->max.seconds * GST_SECOND;
5735 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
5736 GST_TIME_ARGS (seconds));
5738 /* live (WMS) server might send overflowed large max as its idea of infinity,
5739 * compensate to prevent problems later on */
5740 if (seconds != -1 && seconds < 0) {
5742 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
5745 /* live (WMS) might send min == max, which is not worth recording */
5746 if (segment->duration == -1 && seconds == segment->start)
5749 /* don't change duration with unknown value, we might have a valid value
5750 * there that we want to keep. */
5752 segment->duration = seconds;
5757 /* Parse clock profived by the server with following syntax:
5759 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
5762 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
5764 gboolean res = FALSE;
5766 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
5767 gchar **fields = NULL, **parts = NULL;
5768 gchar *remote_ip, *str;
5770 GstClockTime base_time;
5773 fields = g_strsplit (gstclock, " ", 0);
5775 /* wrapped clock, not very interesting for now */
5776 if (fields[1] == NULL)
5779 /* remote IP address and port */
5780 if ((str = fields[2]) == NULL)
5783 parts = g_strsplit (str, ":", 0);
5785 if ((remote_ip = parts[0]) == NULL)
5788 if ((str = parts[1]) == NULL)
5796 if ((str = fields[3]) == NULL)
5799 base_time = g_ascii_strtoull (str, NULL, 10);
5802 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
5805 if (src->provided_clock)
5806 gst_object_unref (src->provided_clock);
5807 src->provided_clock = netclock;
5809 gst_element_post_message (GST_ELEMENT_CAST (src),
5810 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
5811 src->provided_clock, TRUE));
5815 g_strfreev (fields);
5821 /* must be called with the RTSP state lock */
5822 static GstRTSPResult
5823 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
5829 /* prepare global stream caps properties */
5831 gst_structure_remove_all_fields (src->props);
5833 src->props = gst_structure_new_empty ("RTSPProperties");
5836 gst_sdp_message_dump (sdp);
5838 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
5840 /* let the app inspect and change the SDP */
5841 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
5843 gst_segment_init (&src->segment, GST_FORMAT_TIME);
5845 /* parse range for duration reporting. */
5850 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
5854 /* keep track of the range and configure it in the segment */
5855 if (gst_rtspsrc_parse_range (src, range, &src->segment))
5859 /* parse clock information. This is GStreamer specific, a server can tell the
5860 * client what clock it is using and wrap that in a network clock. The
5861 * advantage of that is that we can slave to it. */
5863 const gchar *gstclock;
5866 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
5867 if (gstclock == NULL)
5870 /* parse the clock and expose it in the provide_clock method */
5871 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
5875 /* try to find a global control attribute. Note that a '*' means that we should
5876 * do aggregate control with the current url (so we don't do anything and
5877 * leave the current connection as is) */
5879 const gchar *control;
5882 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
5883 if (control == NULL)
5886 /* only take fully qualified urls */
5887 if (g_str_has_prefix (control, "rtsp://"))
5891 g_free (src->conninfo.location);
5892 src->conninfo.location = g_strdup (control);
5893 /* make a connection for this, if there was a connection already, nothing
5895 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
5896 GST_ERROR_OBJECT (src, "could not connect");
5899 /* we need to keep the control url separate from the connection url because
5900 * the rules for constructing the media control url need it */
5901 g_free (src->control);
5902 src->control = g_strdup (control);
5905 /* create streams */
5906 n_streams = gst_sdp_message_medias_len (sdp);
5907 for (i = 0; i < n_streams; i++) {
5908 gst_rtspsrc_create_stream (src, sdp, i);
5911 src->state = GST_RTSP_STATE_INIT;
5914 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
5917 /* reset our state */
5918 src->need_range = TRUE;
5921 src->state = GST_RTSP_STATE_READY;
5928 GST_ERROR_OBJECT (src, "setup failed");
5929 gst_rtspsrc_cleanup (src);
5934 static GstRTSPResult
5935 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
5939 GstRTSPMessage request = { 0 };
5940 GstRTSPMessage response = { 0 };
5943 gchar *respcont = NULL;
5946 src->need_redirect = FALSE;
5948 /* can't continue without a valid url */
5949 if (G_UNLIKELY (src->conninfo.url == NULL)) {
5950 res = GST_RTSP_EINVAL;
5953 src->tried_url_auth = FALSE;
5955 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
5956 goto connect_failed;
5958 /* create OPTIONS */
5959 GST_DEBUG_OBJECT (src, "create options...");
5961 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
5962 src->conninfo.url_str);
5964 goto create_request_failed;
5967 GST_DEBUG_OBJECT (src, "send options...");
5970 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
5973 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
5978 if (!gst_rtspsrc_parse_methods (src, &response))
5981 /* create DESCRIBE */
5982 GST_DEBUG_OBJECT (src, "create describe...");
5984 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
5985 src->conninfo.url_str);
5987 goto create_request_failed;
5989 /* we only accept SDP for now */
5990 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
5994 GST_DEBUG_OBJECT (src, "send describe...");
5997 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
6000 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6004 /* we only perform redirect for the describe, currently */
6005 if (src->need_redirect) {
6006 /* close connection, we don't have to send a TEARDOWN yet, ignore the
6008 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6010 gst_rtsp_message_unset (&request);
6011 gst_rtsp_message_unset (&response);
6017 /* it could be that the DESCRIBE method was not implemented */
6018 if (!src->methods & GST_RTSP_DESCRIBE)
6021 /* check if reply is SDP */
6022 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
6024 /* could not be set but since the request returned OK, we assume it
6025 * was SDP, else check it. */
6027 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
6028 goto wrong_content_type;
6031 /* get message body and parse as SDP */
6032 gst_rtsp_message_get_body (&response, &data, &size);
6033 if (data == NULL || size == 0)
6036 GST_DEBUG_OBJECT (src, "parse SDP...");
6037 gst_sdp_message_new (sdp);
6038 gst_sdp_message_parse_buffer (data, size, *sdp);
6040 /* clean up any messages */
6041 gst_rtsp_message_unset (&request);
6042 gst_rtsp_message_unset (&response);
6049 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
6050 ("No valid RTSP URL was provided"));
6055 gchar *str = gst_rtsp_strresult (res);
6057 if (res != GST_RTSP_EINTR) {
6058 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6059 ("Failed to connect. (%s)", str));
6061 GST_WARNING_OBJECT (src, "connect interrupted");
6066 create_request_failed:
6068 gchar *str = gst_rtsp_strresult (res);
6070 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6071 ("Could not create request. (%s)", str));
6077 /* Don't post a message - the rtsp_send method will have
6078 * taken care of it because we passed NULL for the response code */
6083 /* error was posted */
6084 res = GST_RTSP_ERROR;
6089 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6090 ("Server does not support SDP, got %s.", respcont));
6091 res = GST_RTSP_ERROR;
6096 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6097 ("Server can not provide an SDP."));
6098 res = GST_RTSP_ERROR;
6103 if (src->conninfo.connection) {
6104 GST_DEBUG_OBJECT (src, "free connection");
6105 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6107 gst_rtsp_message_unset (&request);
6108 gst_rtsp_message_unset (&response);
6113 static GstRTSPResult
6114 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
6119 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
6121 if (src->sdp == NULL) {
6122 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
6126 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
6131 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
6138 GST_WARNING_OBJECT (src, "can't get sdp");
6139 src->open_error = TRUE;
6144 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
6145 src->open_error = TRUE;
6150 static GstRTSPResult
6151 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
6153 GstRTSPMessage request = { 0 };
6154 GstRTSPMessage response = { 0 };
6155 GstRTSPResult res = GST_RTSP_OK;
6159 GST_DEBUG_OBJECT (src, "TEARDOWN...");
6161 gst_rtspsrc_set_state (src, GST_STATE_READY);
6163 if (src->state < GST_RTSP_STATE_READY) {
6164 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
6171 /* construct a control url */
6173 control = src->control;
6175 control = src->conninfo.url_str;
6177 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
6180 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6181 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6183 GstRTSPConnInfo *info;
6185 /* try aggregate control first but do non-aggregate control otherwise */
6187 setup_url = control;
6188 else if ((setup_url = stream->conninfo.location) == NULL)
6191 if (src->conninfo.connection) {
6192 info = &src->conninfo;
6193 } else if (stream->conninfo.connection) {
6194 info = &stream->conninfo;
6198 if (!info->connected)
6203 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
6205 goto create_request_failed;
6208 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
6211 gst_rtspsrc_send (src, info->connection, &request, &response,
6215 /* FIXME, parse result? */
6216 gst_rtsp_message_unset (&request);
6217 gst_rtsp_message_unset (&response);
6220 /* early exit when we did aggregate control */
6226 /* close connections */
6227 GST_DEBUG_OBJECT (src, "closing connection...");
6228 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6229 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6230 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6231 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
6235 gst_rtspsrc_cleanup (src);
6237 src->state = GST_RTSP_STATE_INVALID;
6240 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
6245 create_request_failed:
6247 gchar *str = gst_rtsp_strresult (res);
6249 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6250 ("Could not create request. (%s)", str));
6256 gchar *str = gst_rtsp_strresult (res);
6258 gst_rtsp_message_unset (&request);
6259 if (res != GST_RTSP_EINTR) {
6260 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6261 ("Could not send message. (%s)", str));
6263 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
6270 GST_DEBUG_OBJECT (src,
6271 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
6276 /* RTP-Info is of the format:
6278 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
6280 * rtptime corresponds to the timestamp for the NPT time given in the header
6281 * seqbase corresponds to the next sequence number we received. This number
6282 * indicates the first seqnum after the seek and should be used to discard
6283 * packets that are from before the seek.
6286 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
6291 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
6293 infos = g_strsplit (rtpinfo, ",", 0);
6294 for (i = 0; infos[i]; i++) {
6296 GstRTSPStream *stream;
6300 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
6302 /* init values, types of seqbase and timebase are bigger than needed so we
6303 * can store -1 as uninitialized values */
6308 /* parse url, find stream for url.
6309 * parse seq and rtptime. The seq number should be configured in the rtp
6310 * depayloader or session manager to detect gaps. Same for the rtptime, it
6311 * should be used to create an initial time newsegment. */
6312 fields = g_strsplit (infos[i], ";", 0);
6313 for (j = 0; fields[j]; j++) {
6314 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
6315 /* remove leading whitespace */
6316 fields[j] = g_strchug (fields[j]);
6317 if (g_str_has_prefix (fields[j], "url=")) {
6318 /* get the url and the stream */
6320 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
6321 } else if (g_str_has_prefix (fields[j], "seq=")) {
6322 seqbase = atoi (fields[j] + 4);
6323 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
6324 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
6327 g_strfreev (fields);
6328 /* now we need to store the values for the caps of the stream */
6329 if (stream != NULL) {
6330 GST_DEBUG_OBJECT (src,
6331 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
6332 stream, seqbase, timebase);
6334 /* we have a stream, configure detected params */
6335 stream->seqbase = seqbase;
6336 stream->timebase = timebase;
6345 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
6350 interval = strtoul (rtcp, NULL, 10);
6351 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
6356 interval *= GST_MSECOND;
6358 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6359 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6361 /* already (optionally) retrieved this when configuring manager */
6362 if (stream->session) {
6363 GObject *rtpsession = stream->session;
6365 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
6367 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
6371 /* now it happens that (Xenon) server sending this may also provide bogus
6372 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
6373 * and just use RTP-Info to sync */
6375 GObjectClass *klass;
6377 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
6378 if (g_object_class_find_property (klass, "rtcp-sync")) {
6379 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
6380 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
6386 gst_rtspsrc_get_float (const gchar * dstr)
6388 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6390 /* canonicalise floating point string so we can handle float strings
6391 * in the form "24.930" or "24,930" irrespective of the current locale */
6392 g_strlcpy (s, dstr, sizeof (s));
6393 g_strdelimit (s, ",", '.');
6394 return g_ascii_strtod (s, NULL);
6398 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
6400 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6402 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
6403 g_strlcpy (val_str, "now", sizeof (val_str));
6405 if (segment->position == 0) {
6406 g_strlcpy (val_str, "0", sizeof (val_str));
6408 g_ascii_dtostr (val_str, sizeof (val_str),
6409 ((gdouble) segment->position) / GST_SECOND);
6412 return g_strdup_printf ("npt=%s-", val_str);
6416 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
6418 stream->timebase = -1;
6419 stream->seqbase = -1;
6423 stream->caps = gst_caps_make_writable (stream->caps);
6424 s = gst_caps_get_structure (stream->caps, 0);
6425 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
6429 static GstRTSPResult
6430 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
6432 GstRTSPResult res = GST_RTSP_OK;
6434 if (src->state < GST_RTSP_STATE_READY) {
6435 res = GST_RTSP_ERROR;
6436 if (src->open_error) {
6437 GST_DEBUG_OBJECT (src, "the stream was in error");
6441 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
6443 if ((res = gst_rtspsrc_open (src, async)) < 0) {
6444 GST_DEBUG_OBJECT (src, "failed to open stream");
6453 static GstRTSPResult
6454 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
6456 GstRTSPMessage request = { 0 };
6457 GstRTSPMessage response = { 0 };
6458 GstRTSPResult res = GST_RTSP_OK;
6464 GST_DEBUG_OBJECT (src, "PLAY...");
6466 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6469 if (!(src->methods & GST_RTSP_PLAY))
6472 if (src->state == GST_RTSP_STATE_PLAYING)
6475 if (!src->conninfo.connection || !src->conninfo.connected)
6478 /* send some dummy packets before we activate the receive in the
6480 gst_rtspsrc_send_dummy_packets (src);
6482 /* require new SR packets */
6484 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
6486 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
6488 /* construct a control url */
6490 control = src->control;
6492 control = src->conninfo.url_str;
6494 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6495 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6497 GstRTSPConnection *conn;
6499 /* try aggregate control first but do non-aggregate control otherwise */
6501 setup_url = control;
6502 else if ((setup_url = stream->conninfo.location) == NULL)
6505 if (src->conninfo.connection) {
6506 conn = src->conninfo.connection;
6507 } else if (stream->conninfo.connection) {
6508 conn = stream->conninfo.connection;
6514 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
6516 goto create_request_failed;
6518 if (src->need_range) {
6519 hval = gen_range_header (src, segment);
6521 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_RANGE, hval);
6524 /* store the newsegment event so it can be sent from the streaming thread. */
6525 if (src->start_segment)
6526 gst_event_unref (src->start_segment);
6527 src->start_segment = gst_event_new_segment (&src->segment);
6530 if (segment->rate != 1.0) {
6531 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
6533 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
6535 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
6537 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
6541 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
6543 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6546 /* seek may have silently failed as it is not supported */
6547 if (!(src->methods & GST_RTSP_PLAY)) {
6548 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
6549 /* obviously it is supported as we made it here */
6550 src->methods |= GST_RTSP_PLAY;
6551 src->seekable = FALSE;
6552 /* but there is nothing to parse in the response,
6553 * so convey we have no idea and not to expect anything particular */
6554 clear_rtp_base (src, stream);
6558 /* need to do for all streams */
6559 for (run = src->streams; run; run = g_list_next (run))
6560 clear_rtp_base (src, (GstRTSPStream *) run->data);
6562 /* NOTE the above also disables npt based eos detection */
6563 /* and below forces position to 0,
6564 * which is visible feedback we lost the plot */
6565 segment->start = segment->position = src->last_pos;
6568 gst_rtsp_message_unset (&request);
6570 /* parse RTP npt field. This is the current position in the stream (Normal
6571 * Play Time) and should be put in the NEWSEGMENT position field. */
6572 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
6574 gst_rtspsrc_parse_range (src, hval, segment);
6576 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
6577 segment->rate = 1.0;
6579 /* parse Speed header. This is the intended playback rate of the stream
6580 * and should be put in the NEWSEGMENT rate field. */
6581 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
6582 0) == GST_RTSP_OK) {
6583 segment->rate = gst_rtspsrc_get_float (hval);
6584 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
6585 &hval, 0) == GST_RTSP_OK) {
6586 segment->rate = gst_rtspsrc_get_float (hval);
6589 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
6590 * for the RTP packets. If this is not present, we assume all starts from 0...
6591 * This is info for the RTP session manager that we pass to it in caps. */
6593 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
6594 &hval, hval_idx++) == GST_RTSP_OK)
6595 gst_rtspsrc_parse_rtpinfo (src, hval);
6597 /* some servers indicate RTCP parameters in PLAY response,
6598 * rather than properly in SDP */
6599 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
6600 &hval, 0) == GST_RTSP_OK)
6601 gst_rtspsrc_handle_rtcp_interval (src, hval);
6603 gst_rtsp_message_unset (&response);
6605 /* early exit when we did aggregate control */
6609 /* configure the caps of the streams after we parsed all headers. Only reset
6610 * the manager object when we set a new Range header (we did a seek) */
6611 gst_rtspsrc_configure_caps (src, segment, src->need_range);
6613 /* set again when needed */
6614 src->need_range = FALSE;
6616 src->running = TRUE;
6617 src->base_time = -1;
6618 src->state = GST_RTSP_STATE_PLAYING;
6621 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
6622 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6623 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6624 stream->discont = TRUE;
6629 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
6636 GST_DEBUG_OBJECT (src, "failed to open stream");
6641 GST_DEBUG_OBJECT (src, "PLAY is not supported");
6646 GST_DEBUG_OBJECT (src, "we were already PLAYING");
6649 create_request_failed:
6651 gchar *str = gst_rtsp_strresult (res);
6653 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6654 ("Could not create request. (%s)", str));
6660 gchar *str = gst_rtsp_strresult (res);
6662 gst_rtsp_message_unset (&request);
6663 if (res != GST_RTSP_EINTR) {
6664 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6665 ("Could not send message. (%s)", str));
6667 GST_WARNING_OBJECT (src, "PLAY interrupted");
6674 static GstRTSPResult
6675 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
6677 GstRTSPResult res = GST_RTSP_OK;
6678 GstRTSPMessage request = { 0 };
6679 GstRTSPMessage response = { 0 };
6683 GST_DEBUG_OBJECT (src, "PAUSE...");
6685 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6688 if (!(src->methods & GST_RTSP_PAUSE))
6691 if (src->state == GST_RTSP_STATE_READY)
6694 if (!src->conninfo.connection || !src->conninfo.connected)
6697 /* construct a control url */
6699 control = src->control;
6701 control = src->conninfo.url_str;
6703 /* loop over the streams. We might exit the loop early when we could do an
6704 * aggregate control */
6705 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6706 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6707 GstRTSPConnection *conn;
6710 /* try aggregate control first but do non-aggregate control otherwise */
6712 setup_url = control;
6713 else if ((setup_url = stream->conninfo.location) == NULL)
6716 if (src->conninfo.connection) {
6717 conn = src->conninfo.connection;
6718 } else if (stream->conninfo.connection) {
6719 conn = stream->conninfo.connection;
6725 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
6726 ("Sending PAUSE request"));
6729 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
6731 goto create_request_failed;
6733 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6736 gst_rtsp_message_unset (&request);
6737 gst_rtsp_message_unset (&response);
6739 /* exit early when we did agregate control */
6744 /* change element states now */
6745 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
6748 src->state = GST_RTSP_STATE_READY;
6752 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
6759 GST_DEBUG_OBJECT (src, "failed to open stream");
6764 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
6769 GST_DEBUG_OBJECT (src, "we were already PAUSED");
6772 create_request_failed:
6774 gchar *str = gst_rtsp_strresult (res);
6776 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6777 ("Could not create request. (%s)", str));
6783 gchar *str = gst_rtsp_strresult (res);
6785 gst_rtsp_message_unset (&request);
6786 if (res != GST_RTSP_EINTR) {
6787 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6788 ("Could not send message. (%s)", str));
6790 GST_WARNING_OBJECT (src, "PAUSE interrupted");
6798 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
6800 GstRTSPSrc *rtspsrc;
6802 rtspsrc = GST_RTSPSRC (bin);
6804 switch (GST_MESSAGE_TYPE (message)) {
6805 case GST_MESSAGE_EOS:
6806 gst_message_unref (message);
6808 case GST_MESSAGE_ELEMENT:
6810 const GstStructure *s = gst_message_get_structure (message);
6812 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
6813 gboolean ignore_timeout;
6815 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
6817 GST_OBJECT_LOCK (rtspsrc);
6818 ignore_timeout = rtspsrc->ignore_timeout;
6819 rtspsrc->ignore_timeout = TRUE;
6820 GST_OBJECT_UNLOCK (rtspsrc);
6822 /* we only act on the first udp timeout message, others are irrelevant
6823 * and can be ignored. */
6824 if (!ignore_timeout)
6825 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
6827 gst_message_unref (message);
6830 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6833 case GST_MESSAGE_ERROR:
6836 GstRTSPStream *stream;
6839 udpsrc = GST_MESSAGE_SRC (message);
6841 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
6842 GST_ELEMENT_NAME (udpsrc));
6844 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
6848 /* we ignore the RTCP udpsrc */
6849 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
6852 /* if we get error messages from the udp sources, that's not a problem as
6853 * long as not all of them error out. We also don't really know what the
6854 * problem is, the message does not give enough detail... */
6855 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
6856 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
6857 if (ret != GST_FLOW_OK)
6861 gst_message_unref (message);
6865 /* fatal but not our message, forward */
6866 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6871 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6877 /* the thread where everything happens */
6879 gst_rtspsrc_thread (GstRTSPSrc * src)
6883 GST_OBJECT_LOCK (src);
6884 cmd = src->pending_cmd;
6885 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
6887 src->pending_cmd = CMD_LOOP;
6889 src->pending_cmd = CMD_WAIT;
6890 GST_DEBUG_OBJECT (src, "got command %d", cmd);
6892 /* we got the message command, so ensure communication is possible again */
6893 gst_rtspsrc_connection_flush (src, FALSE);
6895 src->busy_cmd = cmd;
6896 GST_OBJECT_UNLOCK (src);
6900 gst_rtspsrc_open (src, TRUE);
6903 gst_rtspsrc_play (src, &src->segment, TRUE);
6906 gst_rtspsrc_pause (src, TRUE);
6909 gst_rtspsrc_close (src, TRUE, FALSE);
6912 gst_rtspsrc_loop (src);
6915 gst_rtspsrc_reconnect (src, FALSE);
6921 GST_OBJECT_LOCK (src);
6922 /* and go back to sleep */
6923 if (src->pending_cmd == CMD_WAIT) {
6925 gst_task_pause (src->task);
6928 src->busy_cmd = CMD_WAIT;
6929 GST_OBJECT_UNLOCK (src);
6933 gst_rtspsrc_start (GstRTSPSrc * src)
6935 GST_DEBUG_OBJECT (src, "starting");
6937 GST_OBJECT_LOCK (src);
6939 src->pending_cmd = CMD_WAIT;
6941 if (src->task == NULL) {
6942 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
6943 if (src->task == NULL)
6946 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
6948 GST_OBJECT_UNLOCK (src);
6955 GST_ERROR_OBJECT (src, "failed to create task");
6961 gst_rtspsrc_stop (GstRTSPSrc * src)
6965 GST_DEBUG_OBJECT (src, "stopping");
6967 /* also cancels pending task */
6968 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
6970 GST_OBJECT_LOCK (src);
6971 if ((task = src->task)) {
6973 GST_OBJECT_UNLOCK (src);
6975 gst_task_stop (task);
6977 /* make sure it is not running */
6978 GST_RTSP_STREAM_LOCK (src);
6979 GST_RTSP_STREAM_UNLOCK (src);
6981 /* now wait for the task to finish */
6982 gst_task_join (task);
6984 /* and free the task */
6985 gst_object_unref (GST_OBJECT (task));
6987 GST_OBJECT_LOCK (src);
6989 GST_OBJECT_UNLOCK (src);
6991 /* ensure synchronously all is closed and clean */
6992 gst_rtspsrc_close (src, FALSE, TRUE);
6997 static GstStateChangeReturn
6998 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
7000 GstRTSPSrc *rtspsrc;
7001 GstStateChangeReturn ret;
7003 rtspsrc = GST_RTSPSRC (element);
7005 switch (transition) {
7006 case GST_STATE_CHANGE_NULL_TO_READY:
7007 if (!gst_rtspsrc_start (rtspsrc))
7010 case GST_STATE_CHANGE_READY_TO_PAUSED:
7011 /* init some state */
7012 rtspsrc->cur_protocols = rtspsrc->protocols;
7013 /* first attempt, don't ignore timeouts */
7014 rtspsrc->ignore_timeout = FALSE;
7015 rtspsrc->open_error = FALSE;
7016 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
7018 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7019 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7020 /* unblock the tcp tasks and make the loop waiting */
7021 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP);
7022 /* make sure it is waiting before we send PAUSE or PLAY below */
7023 GST_RTSP_STREAM_LOCK (rtspsrc);
7024 GST_RTSP_STREAM_UNLOCK (rtspsrc);
7026 case GST_STATE_CHANGE_PAUSED_TO_READY:
7032 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
7033 if (ret == GST_STATE_CHANGE_FAILURE)
7036 switch (transition) {
7037 case GST_STATE_CHANGE_NULL_TO_READY:
7038 ret = GST_STATE_CHANGE_SUCCESS;
7040 case GST_STATE_CHANGE_READY_TO_PAUSED:
7041 ret = GST_STATE_CHANGE_NO_PREROLL;
7043 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7044 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
7045 ret = GST_STATE_CHANGE_SUCCESS;
7047 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7048 /* send pause request and keep the idle task around */
7049 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
7050 ret = GST_STATE_CHANGE_NO_PREROLL;
7052 case GST_STATE_CHANGE_PAUSED_TO_READY:
7053 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
7054 ret = GST_STATE_CHANGE_SUCCESS;
7056 case GST_STATE_CHANGE_READY_TO_NULL:
7057 gst_rtspsrc_stop (rtspsrc);
7058 ret = GST_STATE_CHANGE_SUCCESS;
7069 GST_DEBUG_OBJECT (rtspsrc, "start failed");
7070 return GST_STATE_CHANGE_FAILURE;
7075 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
7078 GstRTSPSrc *rtspsrc;
7080 rtspsrc = GST_RTSPSRC (element);
7082 if (GST_EVENT_IS_DOWNSTREAM (event)) {
7083 res = gst_rtspsrc_push_event (rtspsrc, event);
7085 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
7092 /*** GSTURIHANDLER INTERFACE *************************************************/
7095 gst_rtspsrc_uri_get_type (GType type)
7100 static const gchar *const *
7101 gst_rtspsrc_uri_get_protocols (GType type)
7103 static const gchar *protocols[] =
7104 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
7105 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
7112 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
7114 GstRTSPSrc *src = GST_RTSPSRC (handler);
7116 /* FIXME: make thread-safe */
7117 return g_strdup (src->conninfo.location);
7121 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
7126 GstRTSPUrl *newurl = NULL;
7127 GstSDPMessage *sdp = NULL;
7129 src = GST_RTSPSRC (handler);
7131 /* same URI, we're fine */
7132 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
7135 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
7136 if ((res = gst_sdp_message_new (&sdp) < 0))
7139 GST_DEBUG_OBJECT (src, "parsing SDP message");
7140 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
7144 GST_DEBUG_OBJECT (src, "parsing URI");
7145 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
7149 /* if worked, free previous and store new url object along with the original
7151 GST_DEBUG_OBJECT (src, "configuring URI");
7152 g_free (src->conninfo.location);
7153 src->conninfo.location = g_strdup (uri);
7154 gst_rtsp_url_free (src->conninfo.url);
7155 src->conninfo.url = newurl;
7156 g_free (src->conninfo.url_str);
7158 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
7160 src->conninfo.url_str = NULL;
7163 gst_sdp_message_free (src->sdp);
7165 src->from_sdp = sdp != NULL;
7167 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
7168 GST_DEBUG_OBJECT (src, "request uri is: %s",
7169 GST_STR_NULL (src->conninfo.url_str));
7176 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
7181 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
7182 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7183 "Could not create SDP");
7188 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
7189 GST_STR_NULL (uri));
7190 gst_sdp_message_free (sdp);
7191 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7197 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
7198 GST_STR_NULL (uri), res);
7199 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7200 "Invalid RTSP URI");
7206 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
7208 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
7210 iface->get_type = gst_rtspsrc_uri_get_type;
7211 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
7212 iface->get_uri = gst_rtspsrc_uri_get_uri;
7213 iface->set_uri = gst_rtspsrc_uri_set_uri;