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,
125 SIGNAL_SELECT_STREAM,
129 enum _GstRtspSrcRtcpSyncMode
136 enum _GstRtspSrcBufferMode
145 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
147 gst_rtsp_src_buffer_mode_get_type (void)
149 static GType buffer_mode_type = 0;
150 static const GEnumValue buffer_modes[] = {
151 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
152 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
153 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
154 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
155 {BUFFER_MODE_SYNCED, "Synchronized sender and receiver clocks", "synced"},
159 if (!buffer_mode_type) {
161 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
163 return buffer_mode_type;
166 #define DEFAULT_LOCATION NULL
167 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
168 #define DEFAULT_DEBUG FALSE
169 #define DEFAULT_RETRY 20
170 #define DEFAULT_TIMEOUT 5000000
171 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
172 #define DEFAULT_TCP_TIMEOUT 20000000
173 #define DEFAULT_LATENCY_MS 2000
174 #define DEFAULT_DROP_ON_LATENCY FALSE
175 #define DEFAULT_CONNECTION_SPEED 0
176 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
177 #define DEFAULT_DO_RTCP TRUE
178 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
179 #define DEFAULT_PROXY NULL
180 #define DEFAULT_RTP_BLOCKSIZE 0
181 #define DEFAULT_USER_ID NULL
182 #define DEFAULT_USER_PW NULL
183 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
184 #define DEFAULT_PORT_RANGE NULL
185 #define DEFAULT_SHORT_HEADER FALSE
186 #define DEFAULT_PROBATION 2
187 #define DEFAULT_UDP_RECONNECT TRUE
188 #define DEFAULT_MULTICAST_IFACE NULL
189 #define DEFAULT_NTP_SYNC FALSE
190 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
191 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
203 PROP_DROP_ON_LATENCY,
204 PROP_CONNECTION_SPEED,
207 PROP_DO_RTSP_KEEP_ALIVE,
216 PROP_UDP_BUFFER_SIZE,
220 PROP_MULTICAST_IFACE,
222 PROP_USE_PIPELINE_CLOCK,
224 PROP_TLS_VALIDATION_FLAGS,
228 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
230 gst_rtsp_nat_method_get_type (void)
232 static GType rtsp_nat_method_type = 0;
233 static const GEnumValue rtsp_nat_method[] = {
234 {GST_RTSP_NAT_NONE, "None", "none"},
235 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
239 if (!rtsp_nat_method_type) {
240 rtsp_nat_method_type =
241 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
243 return rtsp_nat_method_type;
246 static void gst_rtspsrc_finalize (GObject * object);
248 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
249 const GValue * value, GParamSpec * pspec);
250 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
251 GValue * value, GParamSpec * pspec);
253 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
255 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
256 gpointer iface_data);
258 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
261 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
262 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
264 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
266 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
267 GstStateChange transition);
268 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
269 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
271 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
272 GstRTSPMessage * response);
274 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
276 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
277 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
279 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
280 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
282 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
283 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
284 gboolean only_close);
286 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
287 const gchar * uri, GError ** error);
288 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
290 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
291 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
292 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
293 GstRTSPStream * stream, GstEvent * event);
294 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
296 /* commands we send to out loop to notify it of events */
297 #define CMD_OPEN (1 << 0)
298 #define CMD_PLAY (1 << 1)
299 #define CMD_PAUSE (1 << 2)
300 #define CMD_CLOSE (1 << 3)
301 #define CMD_WAIT (1 << 4)
302 #define CMD_RECONNECT (1 << 5)
303 #define CMD_LOOP (1 << 6)
305 /* mask for all commands */
306 #define CMD_ALL ((CMD_LOOP << 1) - 1)
308 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
310 gchar *__txt = _gst_element_error_printf text; \
311 gst_element_post_message (GST_ELEMENT_CAST (el), \
312 gst_message_new_progress (GST_OBJECT_CAST (el), \
313 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
317 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
319 #define gst_rtspsrc_parent_class parent_class
320 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
321 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
324 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
326 GST_DEBUG_OBJECT (src, "default handler");
331 select_stream_accum (GSignalInvocationHint * ihint,
332 GValue * return_accu, const GValue * handler_return, gpointer data)
336 myboolean = g_value_get_boolean (handler_return);
337 GST_DEBUG ("accum %d", myboolean);
338 g_value_set_boolean (return_accu, myboolean);
340 /* stop emission if FALSE */
345 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
347 GObjectClass *gobject_class;
348 GstElementClass *gstelement_class;
349 GstBinClass *gstbin_class;
351 gobject_class = (GObjectClass *) klass;
352 gstelement_class = (GstElementClass *) klass;
353 gstbin_class = (GstBinClass *) klass;
355 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
357 gobject_class->set_property = gst_rtspsrc_set_property;
358 gobject_class->get_property = gst_rtspsrc_get_property;
360 gobject_class->finalize = gst_rtspsrc_finalize;
362 g_object_class_install_property (gobject_class, PROP_LOCATION,
363 g_param_spec_string ("location", "RTSP Location",
364 "Location of the RTSP url to read",
365 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
367 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
368 g_param_spec_flags ("protocols", "Protocols",
369 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
370 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
372 g_object_class_install_property (gobject_class, PROP_DEBUG,
373 g_param_spec_boolean ("debug", "Debug",
374 "Dump request and response messages to stdout",
375 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
377 g_object_class_install_property (gobject_class, PROP_RETRY,
378 g_param_spec_uint ("retry", "Retry",
379 "Max number of retries when allocating RTP ports.",
380 0, G_MAXUINT16, DEFAULT_RETRY,
381 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
383 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
384 g_param_spec_uint64 ("timeout", "Timeout",
385 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
386 0, G_MAXUINT64, DEFAULT_TIMEOUT,
387 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
389 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
390 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
391 "Fail after timeout microseconds on TCP connections (0 = disabled)",
392 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
393 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
395 g_object_class_install_property (gobject_class, PROP_LATENCY,
396 g_param_spec_uint ("latency", "Buffer latency in ms",
397 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
398 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
400 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
401 g_param_spec_boolean ("drop-on-latency",
402 "Drop buffers when maximum latency is reached",
403 "Tells the jitterbuffer to never exceed the given latency in size",
404 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
406 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
407 g_param_spec_uint64 ("connection-speed", "Connection Speed",
408 "Network connection speed in kbps (0 = unknown)",
409 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
410 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
412 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
413 g_param_spec_enum ("nat-method", "NAT Method",
414 "Method to use for traversing firewalls and NAT",
415 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
416 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
419 * GstRTSPSrc::do-rtcp
421 * Enable RTCP support. Some old server don't like RTCP and then this property
422 * needs to be set to FALSE.
426 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
427 g_param_spec_boolean ("do-rtcp", "Do RTCP",
428 "Send RTCP packets, disable for old incompatible server.",
429 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
432 * GstRTSPSrc::do-rtsp-keep-alive
434 * Enable RTSP keep laive support. Some old server don't like RTSP
435 * keep alive and then this property needs to be set to FALSE.
439 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
440 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
441 "Send RTSP keep alive packets, disable for old incompatible server.",
442 DEFAULT_DO_RTSP_KEEP_ALIVE,
443 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
448 * Set the proxy parameters. This has to be a string of the format
449 * [http://][user:passwd@]host[:port].
453 g_object_class_install_property (gobject_class, PROP_PROXY,
454 g_param_spec_string ("proxy", "Proxy",
455 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
456 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
458 * GstRTSPSrc::proxy-id
460 * Sets the proxy URI user id for authentication. If the URI set via the
461 * "proxy" property contains a user-id already, that will take precedence.
465 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
466 g_param_spec_string ("proxy-id", "proxy-id",
467 "HTTP proxy URI user id for authentication", "",
468 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
470 * GstRTSPSrc::proxy-pw
472 * Sets the proxy URI password for authentication. If the URI set via the
473 * "proxy" property contains a password already, that will take precedence.
477 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
478 g_param_spec_string ("proxy-pw", "proxy-pw",
479 "HTTP proxy URI user password for authentication", "",
480 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
483 * GstRTSPSrc::rtp_blocksize
485 * RTP package size to suggest to server.
489 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
490 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
491 "RTP package size to suggest to server (0 = disabled)",
492 0, 65536, DEFAULT_RTP_BLOCKSIZE,
493 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
495 g_object_class_install_property (gobject_class,
497 g_param_spec_string ("user-id", "user-id",
498 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
499 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
500 g_object_class_install_property (gobject_class, PROP_USER_PW,
501 g_param_spec_string ("user-pw", "user-pw",
502 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
503 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
506 * GstRTSPSrc::buffer-mode:
508 * Control the buffering and timestamping mode used by the jitterbuffer.
512 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
513 g_param_spec_enum ("buffer-mode", "Buffer Mode",
514 "Control the buffering algorithm in use",
515 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
516 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
519 * GstRTSPSrc::port-range:
521 * Configure the client port numbers that can be used to recieve RTP and
526 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
527 g_param_spec_string ("port-range", "Port range",
528 "Client port range that can be used to receive RTP and RTCP data, "
529 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
530 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
533 * GstRTSPSrc::udp-buffer-size:
535 * Size of the kernel UDP receive buffer in bytes.
539 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
540 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
541 "Size of the kernel UDP receive buffer in bytes, 0=default",
542 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
543 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
546 * GstRTSPSrc::short-header:
548 * Only send the basic RTSP headers for broken encoders.
552 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
553 g_param_spec_boolean ("short-header", "Short Header",
554 "Only send the basic RTSP headers for broken encoders",
555 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
557 g_object_class_install_property (gobject_class, PROP_PROBATION,
558 g_param_spec_uint ("probation", "Number of probations",
559 "Consecutive packet sequence numbers to accept the source",
560 0, G_MAXUINT, DEFAULT_PROBATION,
561 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
563 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
564 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
565 "Reconnect to the server if RTSP connection is closed when doing UDP",
566 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
568 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
569 g_param_spec_string ("multicast-iface", "Multicast Interface",
570 "The network interface on which to join the multicast group",
571 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
573 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
574 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
575 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
576 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
578 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
579 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
580 "Use the pipeline running-time to set the NTP time in the RTCP SR messages",
581 DEFAULT_USE_PIPELINE_CLOCK,
582 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
584 g_object_class_install_property (gobject_class, PROP_SDES,
585 g_param_spec_boxed ("sdes", "SDES",
586 "The SDES items of this session",
587 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
590 * GstRTSPSrc::tls-validation-flags:
592 * TLS certificate validation flags used to validate server
597 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
598 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
599 "TLS certificate validation flags used to validate the server certificate",
600 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
601 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
604 * GstRTSPSrc::handle-request:
605 * @rtspsrc: a #GstRTSPSrc
606 * @request: a #GstRTSPMessage
607 * @response: a #GstRTSPMessage
609 * Handle a server request in @request and prepare @response.
611 * This signal is called from the streaming thread, you should therefore not
612 * do any state changes on @rtspsrc because this might deadlock. If you want
613 * to modify the state as a result of this signal, post a
614 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
619 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
620 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
621 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
622 G_TYPE_POINTER, G_TYPE_POINTER);
625 * GstRTSPSrc::on-sdp:
626 * @rtspsrc: a #GstRTSPSrc
627 * @sdp: a #GstSDPMessage
629 * Emited when the client has retrieved the SDP and before it configures the
630 * streams in the SDP. @sdp can be inspected and modified.
632 * This signal is called from the streaming thread, you should therefore not
633 * do any state changes on @rtspsrc because this might deadlock. If you want
634 * to modify the state as a result of this signal, post a
635 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
640 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
641 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
642 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
643 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
646 * GstRTSPSrc::select-stream:
647 * @rtspsrc: a #GstRTSPSrc
648 * @num: the stream number
649 * @caps: the stream caps
651 * Emited before the client decides to configure the stream @num with
654 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
659 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
660 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
661 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
662 (GCallback) default_select_stream, select_stream_accum, NULL,
663 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
666 gstelement_class->send_event = gst_rtspsrc_send_event;
667 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
668 gstelement_class->change_state = gst_rtspsrc_change_state;
670 gst_element_class_add_pad_template (gstelement_class,
671 gst_static_pad_template_get (&rtptemplate));
673 gst_element_class_set_static_metadata (gstelement_class,
674 "RTSP packet receiver", "Source/Network",
675 "Receive data over the network via RTSP (RFC 2326)",
676 "Wim Taymans <wim@fluendo.com>, "
677 "Thijs Vermeir <thijs.vermeir@barco.com>, "
678 "Lutz Mueller <lutz@topfrose.de>");
680 gstbin_class->handle_message = gst_rtspsrc_handle_message;
682 gst_rtsp_ext_list_init ();
686 gst_rtspsrc_init (GstRTSPSrc * src)
688 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
689 src->protocols = DEFAULT_PROTOCOLS;
690 src->debug = DEFAULT_DEBUG;
691 src->retry = DEFAULT_RETRY;
692 src->udp_timeout = DEFAULT_TIMEOUT;
693 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
694 src->latency = DEFAULT_LATENCY_MS;
695 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
696 src->connection_speed = DEFAULT_CONNECTION_SPEED;
697 src->nat_method = DEFAULT_NAT_METHOD;
698 src->do_rtcp = DEFAULT_DO_RTCP;
699 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
700 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
701 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
702 src->user_id = g_strdup (DEFAULT_USER_ID);
703 src->user_pw = g_strdup (DEFAULT_USER_PW);
704 src->buffer_mode = DEFAULT_BUFFER_MODE;
705 src->client_port_range.min = 0;
706 src->client_port_range.max = 0;
707 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
708 src->short_header = DEFAULT_SHORT_HEADER;
709 src->probation = DEFAULT_PROBATION;
710 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
711 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
712 src->ntp_sync = DEFAULT_NTP_SYNC;
713 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
715 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
717 /* get a list of all extensions */
718 src->extensions = gst_rtsp_ext_list_get ();
720 /* connect to send signal */
721 gst_rtsp_ext_list_connect (src->extensions, "send",
722 (GCallback) gst_rtspsrc_send_cb, src);
724 /* protects the streaming thread in interleaved mode or the polling
725 * thread in UDP mode. */
726 g_rec_mutex_init (&src->stream_rec_lock);
728 /* protects our state changes from multiple invocations */
729 g_rec_mutex_init (&src->state_rec_lock);
731 src->state = GST_RTSP_STATE_INVALID;
733 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
737 gst_rtspsrc_finalize (GObject * object)
741 rtspsrc = GST_RTSPSRC (object);
743 gst_rtsp_ext_list_free (rtspsrc->extensions);
744 g_free (rtspsrc->conninfo.location);
745 gst_rtsp_url_free (rtspsrc->conninfo.url);
746 g_free (rtspsrc->conninfo.url_str);
747 g_free (rtspsrc->user_id);
748 g_free (rtspsrc->user_pw);
749 g_free (rtspsrc->multi_iface);
752 gst_sdp_message_free (rtspsrc->sdp);
755 if (rtspsrc->provided_clock)
756 gst_object_unref (rtspsrc->provided_clock);
759 gst_structure_free (rtspsrc->sdes);
762 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
763 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
765 G_OBJECT_CLASS (parent_class)->finalize (object);
769 gst_rtspsrc_provide_clock (GstElement * element)
771 GstRTSPSrc *src = GST_RTSPSRC (element);
774 if ((clock = src->provided_clock) != NULL)
775 gst_object_ref (clock);
780 /* a proxy string of the format [user:passwd@]host[:port] */
782 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
786 g_free (rtsp->proxy_user);
787 rtsp->proxy_user = NULL;
788 g_free (rtsp->proxy_passwd);
789 rtsp->proxy_passwd = NULL;
790 g_free (rtsp->proxy_host);
791 rtsp->proxy_host = NULL;
792 rtsp->proxy_port = 0;
799 /* we allow http:// in front but ignore it */
800 if (g_str_has_prefix (p, "http://"))
803 at = strchr (p, '@');
805 /* look for user:passwd */
806 col = strchr (proxy, ':');
807 if (col == NULL || col > at)
810 rtsp->proxy_user = g_strndup (p, col - p);
812 rtsp->proxy_passwd = g_strndup (col, at - col);
817 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
818 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
819 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
820 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
821 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
822 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
823 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
826 col = strchr (p, ':');
829 /* everything before the colon is the hostname */
830 rtsp->proxy_host = g_strndup (p, col - p);
832 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
834 rtsp->proxy_host = g_strdup (p);
835 rtsp->proxy_port = 8080;
841 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
843 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
844 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
847 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
849 rtspsrc->ptcp_timeout = NULL;
853 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
858 rtspsrc = GST_RTSPSRC (object);
862 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
863 g_value_get_string (value), NULL);
866 rtspsrc->protocols = g_value_get_flags (value);
869 rtspsrc->debug = g_value_get_boolean (value);
872 rtspsrc->retry = g_value_get_uint (value);
875 rtspsrc->udp_timeout = g_value_get_uint64 (value);
877 case PROP_TCP_TIMEOUT:
878 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
881 rtspsrc->latency = g_value_get_uint (value);
883 case PROP_DROP_ON_LATENCY:
884 rtspsrc->drop_on_latency = g_value_get_boolean (value);
886 case PROP_CONNECTION_SPEED:
887 rtspsrc->connection_speed = g_value_get_uint64 (value);
889 case PROP_NAT_METHOD:
890 rtspsrc->nat_method = g_value_get_enum (value);
893 rtspsrc->do_rtcp = g_value_get_boolean (value);
895 case PROP_DO_RTSP_KEEP_ALIVE:
896 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
899 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
902 if (rtspsrc->prop_proxy_id)
903 g_free (rtspsrc->prop_proxy_id);
904 rtspsrc->prop_proxy_id = g_value_dup_string (value);
907 if (rtspsrc->prop_proxy_pw)
908 g_free (rtspsrc->prop_proxy_pw);
909 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
911 case PROP_RTP_BLOCKSIZE:
912 rtspsrc->rtp_blocksize = g_value_get_uint (value);
915 if (rtspsrc->user_id)
916 g_free (rtspsrc->user_id);
917 rtspsrc->user_id = g_value_dup_string (value);
920 if (rtspsrc->user_pw)
921 g_free (rtspsrc->user_pw);
922 rtspsrc->user_pw = g_value_dup_string (value);
924 case PROP_BUFFER_MODE:
925 rtspsrc->buffer_mode = g_value_get_enum (value);
927 case PROP_PORT_RANGE:
931 str = g_value_get_string (value);
933 sscanf (str, "%u-%u",
934 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
936 rtspsrc->client_port_range.min = 0;
937 rtspsrc->client_port_range.max = 0;
941 case PROP_UDP_BUFFER_SIZE:
942 rtspsrc->udp_buffer_size = g_value_get_int (value);
944 case PROP_SHORT_HEADER:
945 rtspsrc->short_header = g_value_get_boolean (value);
948 rtspsrc->probation = g_value_get_uint (value);
950 case PROP_UDP_RECONNECT:
951 rtspsrc->udp_reconnect = g_value_get_boolean (value);
953 case PROP_MULTICAST_IFACE:
954 g_free (rtspsrc->multi_iface);
956 if (g_value_get_string (value) == NULL)
957 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
959 rtspsrc->multi_iface = g_value_dup_string (value);
962 rtspsrc->ntp_sync = g_value_get_boolean (value);
964 case PROP_USE_PIPELINE_CLOCK:
965 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
968 rtspsrc->sdes = g_value_dup_boxed (value);
970 case PROP_TLS_VALIDATION_FLAGS:
971 rtspsrc->tls_validation_flags = g_value_get_flags (value);
974 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
980 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
985 rtspsrc = GST_RTSPSRC (object);
989 g_value_set_string (value, rtspsrc->conninfo.location);
992 g_value_set_flags (value, rtspsrc->protocols);
995 g_value_set_boolean (value, rtspsrc->debug);
998 g_value_set_uint (value, rtspsrc->retry);
1001 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1003 case PROP_TCP_TIMEOUT:
1007 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1008 rtspsrc->tcp_timeout.tv_usec;
1009 g_value_set_uint64 (value, timeout);
1013 g_value_set_uint (value, rtspsrc->latency);
1015 case PROP_DROP_ON_LATENCY:
1016 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1018 case PROP_CONNECTION_SPEED:
1019 g_value_set_uint64 (value, rtspsrc->connection_speed);
1021 case PROP_NAT_METHOD:
1022 g_value_set_enum (value, rtspsrc->nat_method);
1025 g_value_set_boolean (value, rtspsrc->do_rtcp);
1027 case PROP_DO_RTSP_KEEP_ALIVE:
1028 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1034 if (rtspsrc->proxy_host) {
1036 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1040 g_value_take_string (value, str);
1044 g_value_set_string (value, rtspsrc->prop_proxy_id);
1047 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1049 case PROP_RTP_BLOCKSIZE:
1050 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1053 g_value_set_string (value, rtspsrc->user_id);
1056 g_value_set_string (value, rtspsrc->user_pw);
1058 case PROP_BUFFER_MODE:
1059 g_value_set_enum (value, rtspsrc->buffer_mode);
1061 case PROP_PORT_RANGE:
1065 if (rtspsrc->client_port_range.min != 0) {
1066 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1067 rtspsrc->client_port_range.max);
1071 g_value_take_string (value, str);
1074 case PROP_UDP_BUFFER_SIZE:
1075 g_value_set_int (value, rtspsrc->udp_buffer_size);
1077 case PROP_SHORT_HEADER:
1078 g_value_set_boolean (value, rtspsrc->short_header);
1080 case PROP_PROBATION:
1081 g_value_set_uint (value, rtspsrc->probation);
1083 case PROP_UDP_RECONNECT:
1084 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1086 case PROP_MULTICAST_IFACE:
1087 g_value_set_string (value, rtspsrc->multi_iface);
1090 g_value_set_boolean (value, rtspsrc->ntp_sync);
1092 case PROP_USE_PIPELINE_CLOCK:
1093 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1096 g_value_set_boxed (value, rtspsrc->sdes);
1098 case PROP_TLS_VALIDATION_FLAGS:
1099 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1102 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1108 find_stream_by_id (GstRTSPStream * stream, gint * id)
1110 if (stream->id == *id)
1117 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1119 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1126 find_stream_by_pt (GstRTSPStream * stream, gint * pt)
1128 if (stream->pt == *pt)
1135 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1137 GstElement *src = (GstElement *) a;
1139 if (stream->udpsrc[0] == src)
1141 if (stream->udpsrc[1] == src)
1148 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1150 /* check qualified setup_url */
1151 if (!strcmp (stream->conninfo.location, (gchar *) a))
1153 /* check original control_url */
1154 if (!strcmp (stream->control_url, (gchar *) a))
1157 /* check if qualified setup_url ends with string */
1158 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1164 static GstRTSPStream *
1165 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1169 /* find and get stream */
1170 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1171 return (GstRTSPStream *) lstream->data;
1176 static const GstSDPBandwidth *
1177 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1178 const GstSDPMedia * media, const gchar * type)
1182 /* first look in the media specific section */
1183 len = gst_sdp_media_bandwidths_len (media);
1184 for (i = 0; i < len; i++) {
1185 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1187 if (strcmp (bw->bwtype, type) == 0)
1190 /* then look in the message specific section */
1191 len = gst_sdp_message_bandwidths_len (sdp);
1192 for (i = 0; i < len; i++) {
1193 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1195 if (strcmp (bw->bwtype, type) == 0)
1202 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1203 const GstSDPMedia * media, GstRTSPStream * stream)
1205 const GstSDPBandwidth *bw;
1207 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1208 stream->as_bandwidth = bw->bandwidth;
1210 stream->as_bandwidth = -1;
1212 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1213 stream->rr_bandwidth = bw->bandwidth;
1215 stream->rr_bandwidth = -1;
1217 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1218 stream->rs_bandwidth = bw->bandwidth;
1220 stream->rs_bandwidth = -1;
1224 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1225 const GstSDPConnection * conn)
1227 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1230 if (conn->addrtype == NULL)
1233 /* check for IPV6 */
1234 if (strcmp (conn->addrtype, "IP4") == 0)
1235 stream->is_ipv6 = FALSE;
1236 else if (strcmp (conn->addrtype, "IP6") == 0)
1237 stream->is_ipv6 = TRUE;
1242 g_free (stream->destination);
1243 stream->destination = g_strdup (conn->address);
1245 /* check for multicast */
1246 stream->is_multicast =
1247 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1249 stream->ttl = conn->ttl;
1252 /* Go over the connections for a stream.
1253 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1255 * - If we are dealing with a localhost address, we disable multicast
1258 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1259 const GstSDPMedia * media, GstRTSPStream * stream)
1261 const GstSDPConnection *conn;
1264 /* first look in the media specific section */
1265 len = gst_sdp_media_connections_len (media);
1266 for (i = 0; i < len; i++) {
1267 conn = gst_sdp_media_get_connection (media, i);
1269 gst_rtspsrc_do_stream_connection (src, stream, conn);
1271 /* then look in the message specific section */
1272 if ((conn = gst_sdp_message_get_connection (sdp))) {
1273 gst_rtspsrc_do_stream_connection (src, stream, conn);
1277 static GstRTSPStream *
1278 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1280 GstRTSPStream *stream;
1281 const gchar *control_url;
1282 const gchar *payload;
1283 const GstSDPMedia *media;
1285 /* get media, should not return NULL */
1286 media = gst_sdp_message_get_media (sdp, idx);
1290 stream = g_new0 (GstRTSPStream, 1);
1291 stream->parent = src;
1292 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1294 stream->last_ret = GST_FLOW_NOT_LINKED;
1295 stream->added = FALSE;
1296 stream->disabled = FALSE;
1297 stream->id = src->numstreams++;
1298 stream->eos = FALSE;
1299 stream->discont = TRUE;
1300 stream->seqbase = -1;
1301 stream->timebase = -1;
1303 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1304 * session manager to scale RTCP. */
1305 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1307 /* collect connection info */
1308 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1310 /* we must have a payload. No payload means we cannot create caps */
1311 /* FIXME, handle multiple formats. The problem here is that we just want to
1312 * take the first available format that we can handle but in order to do that
1313 * we need to scan for depayloader plugins. Scanning for payloader plugins is
1314 * also suboptimal because the user maybe just wants to save the raw stream
1315 * and then we don't care. */
1316 if ((payload = gst_sdp_media_get_format (media, 0))) {
1317 stream->pt = atoi (payload);
1319 stream->caps = gst_rtspsrc_media_to_caps (stream->pt, media);
1321 GST_DEBUG ("mapping sdp session level attributes to caps");
1322 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, stream->caps);
1323 GST_DEBUG ("mapping sdp media level attributes to caps");
1324 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, stream->caps);
1326 if (stream->pt >= 96) {
1327 /* If we have a dynamic payload type, see if we have a stream with the
1328 * same payload number. If there is one, they are part of the same
1329 * container and we only need to add one pad. */
1330 if (find_stream (src, &stream->pt, (gpointer) find_stream_by_pt)) {
1331 stream->container = TRUE;
1332 GST_DEBUG ("found another stream with pt %d, marking as container",
1337 /* collect port number */
1338 stream->port = gst_sdp_media_get_port (media);
1340 /* get control url to construct the setup url. The setup url is used to
1341 * configure the transport of the stream and is used to identity the stream in
1342 * the RTP-Info header field returned from PLAY. */
1343 control_url = gst_sdp_media_get_attribute_val (media, "control");
1344 if (control_url == NULL)
1345 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1347 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1348 GST_DEBUG_OBJECT (src, " pt: %d", stream->pt);
1349 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1350 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1351 GST_DEBUG_OBJECT (src, " caps: %" GST_PTR_FORMAT, stream->caps);
1352 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1354 if (control_url != NULL) {
1355 stream->control_url = g_strdup (control_url);
1356 /* Build a fully qualified url using the content_base if any or by prefixing
1357 * the original request.
1358 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1359 * likely build a URL that the server will fail to understand, this is ok,
1360 * we will fail then. */
1361 if (g_str_has_prefix (control_url, "rtsp://"))
1362 stream->conninfo.location = g_strdup (control_url);
1367 if (g_strcmp0 (control_url, "*") == 0)
1371 base = src->control;
1372 else if (src->content_base)
1373 base = src->content_base;
1374 else if (src->conninfo.url_str)
1375 base = src->conninfo.url_str;
1379 /* check if the base ends or control starts with / */
1380 has_slash = g_str_has_prefix (control_url, "/");
1381 has_slash = has_slash || g_str_has_suffix (base, "/");
1383 /* concatenate the two strings, insert / when not present */
1384 stream->conninfo.location =
1385 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1388 GST_DEBUG_OBJECT (src, " setup: %s",
1389 GST_STR_NULL (stream->conninfo.location));
1391 /* we keep track of all streams */
1392 src->streams = g_list_append (src->streams, stream);
1400 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1404 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1407 gst_caps_unref (stream->caps);
1409 g_free (stream->destination);
1410 g_free (stream->control_url);
1411 g_free (stream->conninfo.location);
1413 for (i = 0; i < 2; i++) {
1414 if (stream->udpsrc[i]) {
1415 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1416 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1417 gst_object_unref (stream->udpsrc[i]);
1418 stream->udpsrc[i] = NULL;
1420 if (stream->channelpad[i]) {
1421 gst_object_unref (stream->channelpad[i]);
1422 stream->channelpad[i] = NULL;
1424 if (stream->udpsink[i]) {
1425 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1426 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1427 gst_object_unref (stream->udpsink[i]);
1428 stream->udpsink[i] = NULL;
1431 if (stream->fakesrc) {
1432 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1433 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1434 gst_object_unref (stream->fakesrc);
1435 stream->fakesrc = NULL;
1437 if (stream->srcpad) {
1438 gst_pad_set_active (stream->srcpad, FALSE);
1439 if (stream->added) {
1440 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1441 stream->added = FALSE;
1443 stream->srcpad = NULL;
1445 if (stream->rtcppad) {
1446 gst_object_unref (stream->rtcppad);
1447 stream->rtcppad = NULL;
1449 if (stream->session) {
1450 g_object_unref (stream->session);
1451 stream->session = NULL;
1457 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1461 GST_DEBUG_OBJECT (src, "cleanup");
1463 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1464 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1466 gst_rtspsrc_stream_free (src, stream);
1468 g_list_free (src->streams);
1469 src->streams = NULL;
1471 if (src->manager_sig_id) {
1472 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1473 src->manager_sig_id = 0;
1475 gst_element_set_state (src->manager, GST_STATE_NULL);
1476 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1477 src->manager = NULL;
1479 src->numstreams = 0;
1481 gst_structure_free (src->props);
1484 g_free (src->content_base);
1485 src->content_base = NULL;
1487 g_free (src->control);
1488 src->control = NULL;
1491 gst_rtsp_range_free (src->range);
1494 /* don't clear the SDP when it was used in the url */
1495 if (src->sdp && !src->from_sdp) {
1496 gst_sdp_message_free (src->sdp);
1499 if (src->start_segment) {
1500 gst_event_unref (src->start_segment);
1501 src->start_segment = NULL;
1503 if (src->provided_clock) {
1504 gst_object_unref (src->provided_clock);
1505 src->provided_clock = NULL;
1509 #define PARSE_INT(p, del, res) \
1512 p = strstr (p, del); \
1522 #define PARSE_STRING(p, del, res) \
1525 p = strstr (p, del); \
1537 #define SKIP_SPACES(p) \
1538 while (*p && g_ascii_isspace (*p)) \
1543 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1546 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1547 gint * rate, gchar ** params)
1551 p = (gchar *) rtpmap;
1553 PARSE_INT (p, " ", *payload);
1561 PARSE_STRING (p, "/", *name);
1562 if (*name == NULL) {
1563 GST_DEBUG ("no rate, name %s", p);
1564 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1565 * streams seem to omit the rate. */
1572 p = strstr (p, "/");
1590 * Mapping SDP attributes to caps
1592 * prepend 'a-' to IANA registered sdp attributes names
1593 * (ie: not prefixed with 'x-') in order to avoid
1594 * collision with gstreamer standard caps properties names
1597 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1599 if (attributes->len > 0) {
1603 s = gst_caps_get_structure (caps, 0);
1605 for (i = 0; i < attributes->len; i++) {
1606 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1607 gchar *tofree, *key;
1611 /* skip some of the attribute we already handle */
1612 if (!strcmp (key, "fmtp"))
1614 if (!strcmp (key, "rtpmap"))
1616 if (!strcmp (key, "control"))
1618 if (!strcmp (key, "range"))
1621 /* string must be valid UTF8 */
1622 if (!g_utf8_validate (attr->value, -1, NULL))
1625 if (!g_str_has_prefix (key, "x-"))
1626 tofree = key = g_strdup_printf ("a-%s", key);
1630 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1631 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1638 * Mapping of caps to and from SDP fields:
1640 * m=<media> <UDP port> RTP/AVP <payload>
1641 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1642 * a=fmtp:<payload> <param>[=<value>];...
1645 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1648 const gchar *rtpmap;
1652 gchar *params = NULL;
1658 /* get and parse rtpmap */
1659 if ((rtpmap = gst_sdp_media_get_attribute_val (media, "rtpmap"))) {
1660 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
1662 if (payload != pt) {
1663 /* we ignore the rtpmap if the payload type is different. */
1664 g_warning ("rtpmap of wrong payload type, ignoring");
1670 /* if we failed to parse the rtpmap for a dynamic payload type, we have an
1674 /* else we can ignore */
1675 g_warning ("error parsing rtpmap, ignoring");
1678 /* dynamic payloads need rtpmap or we fail */
1682 /* check if we have a rate, if not, we need to look up the rate from the
1683 * default rates based on the payload types. */
1685 const GstRTPPayloadInfo *info;
1687 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
1688 /* dynamic types, use media and encoding_name */
1689 tmp = g_ascii_strdown (media->media, -1);
1690 info = gst_rtp_payload_info_for_name (tmp, name);
1693 /* static types, use payload type */
1694 info = gst_rtp_payload_info_for_pt (pt);
1698 if ((rate = info->clock_rate) == 0)
1701 /* we fail if we cannot find one */
1706 tmp = g_ascii_strdown (media->media, -1);
1707 caps = gst_caps_new_simple ("application/x-unknown",
1708 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
1710 s = gst_caps_get_structure (caps, 0);
1712 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
1714 /* encoding name must be upper case */
1716 tmp = g_ascii_strup (name, -1);
1717 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
1721 /* params must be lower case */
1722 if (params != NULL) {
1723 tmp = g_ascii_strdown (params, -1);
1724 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
1728 /* parse optional fmtp: field */
1729 if ((fmtp = gst_sdp_media_get_attribute_val (media, "fmtp"))) {
1735 /* p is now of the format <payload> <param>[=<value>];... */
1736 PARSE_INT (p, " ", payload);
1737 if (payload != -1 && payload == pt) {
1741 /* <param>[=<value>] are separated with ';' */
1742 pairs = g_strsplit (p, ";", 0);
1743 for (i = 0; pairs[i]; i++) {
1745 const gchar *val, *key;
1747 /* the key may not have a '=', the value can have other '='s */
1748 valpos = strstr (pairs[i], "=");
1750 /* we have a '=' and thus a value, remove the '=' with \0 */
1752 /* value is everything between '=' and ';'. We split the pairs at ;
1753 * boundaries so we can take the remainder of the value. Some servers
1754 * put spaces around the value which we strip off here. Alternatively
1755 * we could strip those spaces in the depayloaders should these spaces
1756 * actually carry any meaning in the future. */
1757 val = g_strstrip (valpos + 1);
1759 /* simple <param>;.. is translated into <param>=1;... */
1762 /* strip the key of spaces, convert key to lowercase but not the value. */
1763 key = g_strstrip (pairs[i]);
1764 if (strlen (key) > 1) {
1765 tmp = g_ascii_strdown (key, -1);
1766 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
1778 g_warning ("rtpmap type not given for dynamic payload %d", pt);
1783 g_warning ("rate unknown for payload type %d", pt);
1789 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1790 gint * rtpport, gint * rtcpport)
1793 GstStateChangeReturn ret;
1794 GstElement *udpsrc0, *udpsrc1;
1795 gint tmp_rtp, tmp_rtcp;
1799 src = stream->parent;
1805 /* Start at next port */
1806 tmp_rtp = src->next_port_num;
1808 if (stream->is_ipv6)
1809 host = "udp://[::0]";
1811 host = "udp://0.0.0.0";
1813 /* try to allocate 2 UDP ports, the RTP port should be an even
1814 * number and the RTCP port should be the next (uneven) port */
1817 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1818 tmp_rtp >= src->client_port_range.max)
1821 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1822 if (udpsrc0 == NULL)
1823 goto no_udp_protocol;
1824 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1826 if (src->udp_buffer_size != 0)
1827 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
1830 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
1831 if (ret == GST_STATE_CHANGE_FAILURE) {
1833 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
1836 if (++count > src->retry)
1839 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1840 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1841 gst_object_unref (udpsrc0);
1844 GST_DEBUG_OBJECT (src, "retry %d", count);
1847 goto no_udp_protocol;
1850 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
1851 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
1853 /* check if port is even */
1854 if ((tmp_rtp & 0x01) != 0) {
1855 /* port not even, close and allocate another */
1856 if (++count > src->retry)
1859 GST_DEBUG_OBJECT (src, "RTP port not even");
1861 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1862 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1863 gst_object_unref (udpsrc0);
1866 GST_DEBUG_OBJECT (src, "retry %d", count);
1871 /* allocate port+1 for RTCP now */
1872 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1873 if (udpsrc1 == NULL)
1874 goto no_udp_rtcp_protocol;
1877 tmp_rtcp = tmp_rtp + 1;
1878 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
1881 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
1883 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
1884 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
1885 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
1886 if (ret == GST_STATE_CHANGE_FAILURE) {
1887 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
1889 if (++count > src->retry)
1892 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1893 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1894 gst_object_unref (udpsrc0);
1897 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
1898 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1899 gst_object_unref (udpsrc1);
1903 GST_DEBUG_OBJECT (src, "retry %d", count);
1907 /* all fine, do port check */
1908 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
1909 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
1911 /* this should not happen... */
1912 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
1915 /* we keep these elements, we configure all in configure_transport when the
1916 * server told us to really use the UDP ports. */
1917 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
1918 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
1919 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
1920 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
1922 /* keep track of next available port number when we have a range
1924 if (src->next_port_num != 0)
1925 src->next_port_num = tmp_rtcp + 1;
1932 GST_DEBUG_OBJECT (src, "could not get UDP source");
1937 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
1941 no_udp_rtcp_protocol:
1943 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
1948 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
1949 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
1955 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1956 gst_object_unref (udpsrc0);
1959 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1960 gst_object_unref (udpsrc1);
1967 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
1972 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
1974 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1975 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1978 for (i = 0; i < 2; i++) {
1979 if (stream->udpsrc[i])
1980 gst_element_set_state (stream->udpsrc[i], state);
1986 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
1993 event = gst_event_new_flush_start ();
1994 GST_DEBUG_OBJECT (src, "start flush");
1996 state = GST_STATE_PAUSED;
1998 event = gst_event_new_flush_stop (FALSE);
1999 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2002 state = GST_STATE_PLAYING;
2004 state = GST_STATE_PAUSED;
2006 gst_rtspsrc_push_event (src, event);
2007 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2008 gst_rtspsrc_set_state (src, state);
2011 static GstRTSPResult
2012 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2013 GstRTSPMessage * message, GTimeVal * timeout)
2018 ret = gst_rtsp_connection_send (conn, message, timeout);
2020 ret = GST_RTSP_ERROR;
2025 static GstRTSPResult
2026 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2027 GstRTSPMessage * message, GTimeVal * timeout)
2032 ret = gst_rtsp_connection_receive (conn, message, timeout);
2034 ret = GST_RTSP_ERROR;
2040 gst_rtspsrc_get_position (GstRTSPSrc * src)
2045 query = gst_query_new_position (GST_FORMAT_TIME);
2046 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2047 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2048 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2052 if (stream->srcpad) {
2053 if (gst_pad_query (stream->srcpad, query)) {
2054 gst_query_parse_position (query, &fmt, &pos);
2055 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2056 GST_TIME_ARGS (pos));
2057 src->last_pos = pos;
2067 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
2069 src->state = GST_RTSP_STATE_SEEKING;
2070 /* PLAY will add the range header now. */
2071 src->need_range = TRUE;
2077 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2082 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2084 gboolean flush, skip;
2087 GstSegment seeksegment = { 0, };
2091 GST_DEBUG_OBJECT (src, "doing seek with event");
2093 gst_event_parse_seek (event, &rate, &format, &flags,
2094 &cur_type, &cur, &stop_type, &stop);
2096 /* no negative rates yet */
2100 /* we need TIME format */
2101 if (format != src->segment.format)
2104 GST_DEBUG_OBJECT (src, "doing seek without event");
2106 cur_type = GST_SEEK_TYPE_SET;
2107 stop_type = GST_SEEK_TYPE_SET;
2110 /* get flush flag */
2111 flush = flags & GST_SEEK_FLAG_FLUSH;
2112 skip = flags & GST_SEEK_FLAG_SKIP;
2114 /* now we need to make sure the streaming thread is stopped. We do this by
2115 * either sending a FLUSH_START event downstream which will cause the
2116 * streaming thread to stop with a WRONG_STATE.
2117 * For a non-flushing seek we simply pause the task, which will happen as soon
2118 * as it completes one iteration (and thus might block when the sink is
2119 * blocking in preroll). */
2121 GST_DEBUG_OBJECT (src, "starting flush");
2122 gst_rtspsrc_flush (src, TRUE, FALSE);
2125 gst_task_pause (src->task);
2129 /* we should now be able to grab the streaming thread because we stopped it
2130 * with the above flush/pause code */
2131 GST_RTSP_STREAM_LOCK (src);
2133 GST_DEBUG_OBJECT (src, "stopped streaming");
2135 /* copy segment, we need this because we still need the old
2136 * segment when we close the current segment. */
2137 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2139 /* configure the seek parameters in the seeksegment. We will then have the
2140 * right values in the segment to perform the seek */
2142 GST_DEBUG_OBJECT (src, "configuring seek");
2143 gst_segment_do_seek (&seeksegment, rate, format, flags,
2144 cur_type, cur, stop_type, stop, &update);
2147 /* figure out the last position we need to play. If it's configured (stop !=
2148 * -1), use that, else we play until the total duration of the file */
2149 if ((stop = seeksegment.stop) == -1)
2150 stop = seeksegment.duration;
2152 playing = (src->state == GST_RTSP_STATE_PLAYING);
2154 /* if we were playing, pause first */
2156 /* obtain current position in case seek fails */
2157 gst_rtspsrc_get_position (src);
2158 gst_rtspsrc_pause (src, FALSE);
2162 gst_rtspsrc_do_seek (src, &seeksegment);
2164 /* and continue playing */
2166 gst_rtspsrc_play (src, &seeksegment, FALSE);
2168 /* prepare for streaming again */
2170 /* if we started flush, we stop now */
2171 GST_DEBUG_OBJECT (src, "stopping flush");
2172 gst_rtspsrc_flush (src, FALSE, playing);
2175 /* now we did the seek and can activate the new segment values */
2176 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2178 /* if we're doing a segment seek, post a SEGMENT_START message */
2179 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2180 gst_element_post_message (GST_ELEMENT_CAST (src),
2181 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2182 src->segment.format, src->segment.position));
2185 /* now create the newsegment */
2186 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2187 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2190 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2191 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2192 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2193 stream->discont = TRUE;
2196 GST_RTSP_STREAM_UNLOCK (src);
2203 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2208 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2214 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2218 gboolean res = TRUE;
2221 src = GST_RTSPSRC_CAST (parent);
2223 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2224 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2226 switch (GST_EVENT_TYPE (event)) {
2227 case GST_EVENT_SEEK:
2228 res = gst_rtspsrc_perform_seek (src, event);
2232 case GST_EVENT_NAVIGATION:
2233 case GST_EVENT_LATENCY:
2241 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2242 res = gst_pad_send_event (target, event);
2243 gst_object_unref (target);
2245 gst_event_unref (event);
2248 gst_event_unref (event);
2254 /* this is the final event function we receive on the internal source pad when
2255 * we deal with TCP connections */
2257 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2262 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2264 switch (GST_EVENT_TYPE (event)) {
2265 case GST_EVENT_SEEK:
2267 case GST_EVENT_NAVIGATION:
2268 case GST_EVENT_LATENCY:
2270 gst_event_unref (event);
2277 /* this is the final query function we receive on the internal source pad when
2278 * we deal with TCP connections */
2280 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2284 gboolean res = TRUE;
2286 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2288 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2289 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2291 switch (GST_QUERY_TYPE (query)) {
2292 case GST_QUERY_POSITION:
2297 case GST_QUERY_DURATION:
2301 gst_query_parse_duration (query, &format, NULL);
2304 case GST_FORMAT_TIME:
2305 gst_query_set_duration (query, format, src->segment.duration);
2313 case GST_QUERY_LATENCY:
2315 /* we are live with a min latency of 0 and unlimited max latency, this
2316 * result will be updated by the session manager if there is any. */
2317 gst_query_set_latency (query, TRUE, 0, -1);
2327 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2329 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2333 gboolean res = FALSE;
2335 src = GST_RTSPSRC_CAST (parent);
2337 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2338 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2340 switch (GST_QUERY_TYPE (query)) {
2341 case GST_QUERY_DURATION:
2345 gst_query_parse_duration (query, &format, NULL);
2348 case GST_FORMAT_TIME:
2349 gst_query_set_duration (query, format, src->segment.duration);
2357 case GST_QUERY_SEEKING:
2361 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2362 if (format == GST_FORMAT_TIME) {
2364 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2366 /* seeking without duration is unlikely */
2367 seekable = seekable && src->seekable && src->segment.duration &&
2368 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2370 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2371 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2372 src->segment.start, src->segment.stop);
2381 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2383 gst_query_set_uri (query, uri);
2391 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2393 /* forward the query to the proxy target pad */
2395 res = gst_pad_query (target, query);
2396 gst_object_unref (target);
2405 /* callback for RTCP messages to be sent to the server when operating in TCP
2407 static GstFlowReturn
2408 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2411 GstRTSPStream *stream;
2412 GstFlowReturn res = GST_FLOW_OK;
2417 GstRTSPMessage message = { 0 };
2418 GstRTSPConnection *conn;
2420 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2421 src = stream->parent;
2423 gst_buffer_map (buffer, &map, GST_MAP_READ);
2427 gst_rtsp_message_init_data (&message, stream->channel[1]);
2429 /* lend the body data to the message */
2430 gst_rtsp_message_take_body (&message, data, size);
2432 if (stream->conninfo.connection)
2433 conn = stream->conninfo.connection;
2435 conn = src->conninfo.connection;
2437 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2438 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2439 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2441 /* and steal it away again because we will free it when unreffing the
2443 gst_rtsp_message_steal_body (&message, &data, &size);
2444 gst_rtsp_message_unset (&message);
2446 gst_buffer_unmap (buffer, &map);
2447 gst_buffer_unref (buffer);
2452 static GstPadProbeReturn
2453 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2455 GstRTSPSrc *src = user_data;
2457 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2458 GST_DEBUG_PAD_NAME (pad));
2460 /* activate the streams */
2461 GST_OBJECT_LOCK (src);
2462 if (!src->need_activate)
2465 src->need_activate = FALSE;
2466 GST_OBJECT_UNLOCK (src);
2468 gst_rtspsrc_activate_streams (src);
2470 return GST_PAD_PROBE_OK;
2474 GST_OBJECT_UNLOCK (src);
2475 return GST_PAD_PROBE_OK;
2479 /* this callback is called when the session manager generated a new src pad with
2480 * payloaded RTP packets. We simply ghost the pad here. */
2482 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2485 GstPadTemplate *template;
2488 GstRTSPStream *stream;
2491 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2493 GST_RTSP_STATE_LOCK (src);
2495 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2496 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2497 goto unknown_stream;
2499 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2501 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2503 goto unknown_stream;
2506 stream->ssrc = ssrc;
2508 /* we'll add it later see below */
2509 stream->added = TRUE;
2511 /* check if we added all streams */
2513 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2514 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2516 GST_DEBUG_OBJECT (src, "stream %p, container %d, disabled %d, added %d",
2517 ostream, ostream->container, ostream->disabled, ostream->added);
2519 /* a container stream only needs one pad added. Also disabled streams don't
2521 if (!ostream->container && !ostream->disabled && !ostream->added) {
2526 GST_RTSP_STATE_UNLOCK (src);
2528 /* create a new pad we will use to stream to */
2529 template = gst_static_pad_template_get (&rtptemplate);
2530 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2531 gst_object_unref (template);
2534 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2535 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2536 gst_pad_set_active (stream->srcpad, TRUE);
2537 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2540 GST_DEBUG_OBJECT (src, "We added all streams");
2541 /* when we get here, all stream are added and we can fire the no-more-pads
2543 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2551 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2552 GST_RTSP_STATE_UNLOCK (src);
2559 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2561 GstRTSPStream *stream;
2564 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2566 GST_RTSP_STATE_LOCK (src);
2567 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2569 goto unknown_stream;
2571 caps = stream->caps;
2573 gst_caps_ref (caps);
2574 GST_RTSP_STATE_UNLOCK (src);
2580 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2581 GST_RTSP_STATE_UNLOCK (src);
2587 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2589 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2595 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2601 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2607 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2609 GstRTSPSrc *src = stream->parent;
2612 g_object_get (source, "ssrc", &ssrc, NULL);
2614 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2615 ssrc, stream->ssrc, stream->id);
2617 if (ssrc == stream->ssrc)
2618 gst_rtspsrc_do_stream_eos (src, stream);
2622 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2624 GstRTSPSrc *src = stream->parent;
2627 g_object_get (source, "ssrc", &ssrc, NULL);
2629 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2630 ssrc, stream->ssrc, stream->id);
2632 if (ssrc == stream->ssrc)
2633 gst_rtspsrc_do_stream_eos (src, stream);
2637 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2639 GstRTSPStream *stream;
2641 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2643 /* get stream for session */
2644 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2646 gst_rtspsrc_do_stream_eos (src, stream);
2651 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2653 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2657 /* try to get and configure a manager */
2659 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
2660 GstRTSPTransport * transport)
2662 const gchar *manager;
2664 GstStateChangeReturn ret;
2666 /* find a manager */
2667 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
2671 GST_DEBUG_OBJECT (src, "using manager %s", manager);
2673 /* configure the manager */
2674 if (src->manager == NULL) {
2675 GObjectClass *klass;
2677 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
2679 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
2683 goto use_no_manager;
2685 if (!(src->manager = gst_element_factory_make (manager, "manager")))
2686 goto manager_failed;
2689 /* we manage this element */
2690 gst_element_set_locked_state (src->manager, TRUE);
2691 gst_bin_add (GST_BIN_CAST (src), src->manager);
2693 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
2694 if (ret == GST_STATE_CHANGE_FAILURE)
2695 goto start_manager_failure;
2697 g_object_set (src->manager, "latency", src->latency, NULL);
2699 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2701 if (g_object_class_find_property (klass, "ntp-sync")) {
2702 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
2705 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
2706 g_object_set (src->manager, "use-pipeline-clock",
2707 src->use_pipeline_clock, NULL);
2710 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
2711 g_object_set (src->manager, "sdes", src->sdes, NULL);
2714 if (g_object_class_find_property (klass, "drop-on-latency")) {
2715 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
2719 if (g_object_class_find_property (klass, "buffer-mode")) {
2720 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2721 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2723 gboolean need_slave;
2725 const gchar *encoding;
2727 /* buffer mode pauses are handled by adding offsets to buffer times,
2728 * but some depayloaders may have a hard time syncing output times
2729 * with such input times, e.g. container ones, most notably ASF */
2730 /* TODO alternatives are having an event that indicates these shifts,
2731 * or having rtsp extensions provide suggestion on buffer mode */
2732 need_slave = stream->container;
2733 if (stream->caps && (s = gst_caps_get_structure (stream->caps, 0)) &&
2734 (encoding = gst_structure_get_string (s, "encoding-name")))
2735 need_slave = need_slave || (strcmp (encoding, "X-ASF-PF") == 0);
2736 GST_DEBUG_OBJECT (src, "auto buffering mode, need_slave %d",
2738 /* valid duration implies not likely live pipeline,
2739 * so slaving in jitterbuffer does not make much sense
2740 * (and might mess things up due to bursts) */
2741 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
2742 src->segment.duration && !need_slave) {
2743 GST_DEBUG_OBJECT (src, "selected buffer");
2744 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER,
2747 GST_DEBUG_OBJECT (src, "selected slave");
2748 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2753 /* connect to signals if we did not already do so */
2754 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
2756 src->manager_sig_id =
2757 g_signal_connect (src->manager, "pad-added",
2758 (GCallback) new_manager_pad, src);
2759 src->manager_ptmap_id =
2760 g_signal_connect (src->manager, "request-pt-map",
2761 (GCallback) request_pt_map, src);
2763 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
2767 /* we stream directly to the manager, get some pads. Each RTSP stream goes
2768 * into a separate RTP session. */
2769 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
2770 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
2772 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
2773 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
2776 /* now configure the bandwidth in the manager */
2777 if (g_signal_lookup ("get-internal-session",
2778 G_OBJECT_TYPE (src->manager)) != 0) {
2779 GObject *rtpsession;
2781 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
2784 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
2786 stream->session = rtpsession;
2788 if (stream->as_bandwidth != -1) {
2789 GST_INFO_OBJECT (src, "setting AS: %f",
2790 (gdouble) (stream->as_bandwidth * 1000));
2791 g_object_set (rtpsession, "bandwidth",
2792 (gdouble) (stream->as_bandwidth * 1000), NULL);
2794 if (stream->rr_bandwidth != -1) {
2795 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
2796 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
2799 if (stream->rs_bandwidth != -1) {
2800 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
2801 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
2805 g_object_set (rtpsession, "probation", src->probation, NULL);
2807 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2809 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
2811 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
2813 g_signal_connect (rtpsession, "on-ssrc-active",
2814 (GCallback) on_ssrc_active, stream);
2825 GST_DEBUG_OBJECT (src, "cannot get a session manager");
2830 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
2833 start_manager_failure:
2835 GST_DEBUG_OBJECT (src, "could not start session manager");
2840 /* free the UDP sources allocated when negotiating a transport.
2841 * This function is called when the server negotiated to a transport where the
2842 * UDP sources are not needed anymore, such as TCP or multicast. */
2844 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
2848 for (i = 0; i < 2; i++) {
2849 if (stream->udpsrc[i]) {
2850 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
2851 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
2852 gst_object_unref (stream->udpsrc[i]);
2853 stream->udpsrc[i] = NULL;
2858 /* for TCP, create pads to send and receive data to and from the manager and to
2859 * intercept various events and queries
2862 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
2863 GstRTSPTransport * transport, GstPad ** outpad)
2866 GstPadTemplate *template;
2867 GstPad *pad0, *pad1;
2869 /* configure for interleaved delivery, nothing needs to be done
2870 * here, the loop function will call the chain functions of the
2871 * session manager. */
2872 stream->channel[0] = transport->interleaved.min;
2873 stream->channel[1] = transport->interleaved.max;
2874 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
2875 stream->channel[0], stream->channel[1]);
2877 /* we can remove the allocated UDP ports now */
2878 gst_rtspsrc_stream_free_udp (stream);
2880 /* no session manager, send data to srcpad directly */
2881 if (!stream->channelpad[0]) {
2882 GST_DEBUG_OBJECT (src, "no manager, creating pad");
2884 /* create a new pad we will use to stream to */
2885 name = g_strdup_printf ("stream_%u", stream->id);
2886 template = gst_static_pad_template_get (&rtptemplate);
2887 stream->channelpad[0] = gst_pad_new_from_template (template, name);
2888 gst_object_unref (template);
2891 /* set caps and activate */
2892 gst_pad_use_fixed_caps (stream->channelpad[0]);
2893 gst_pad_set_active (stream->channelpad[0], TRUE);
2895 *outpad = gst_object_ref (stream->channelpad[0]);
2897 GST_DEBUG_OBJECT (src, "using manager source pad");
2899 template = gst_static_pad_template_get (&anysrctemplate);
2901 /* allocate pads for sending the channel data into the manager */
2902 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
2903 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
2904 gst_object_unref (stream->channelpad[0]);
2905 stream->channelpad[0] = pad0;
2906 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
2907 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
2908 gst_pad_set_element_private (pad0, src);
2909 gst_pad_set_active (pad0, TRUE);
2911 if (stream->channelpad[1]) {
2912 /* if we have a sinkpad for the other channel, create a pad and link to the
2914 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
2915 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
2916 gst_pad_link_full (pad1, stream->channelpad[1],
2917 GST_PAD_LINK_CHECK_NOTHING);
2918 gst_object_unref (stream->channelpad[1]);
2919 stream->channelpad[1] = pad1;
2920 gst_pad_set_active (pad1, TRUE);
2922 gst_object_unref (template);
2924 /* setup RTCP transport back to the server if we have to. */
2925 if (src->manager && src->do_rtcp) {
2928 template = gst_static_pad_template_get (&anysinktemplate);
2930 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
2931 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
2932 gst_pad_set_element_private (stream->rtcppad, stream);
2933 gst_pad_set_active (stream->rtcppad, TRUE);
2935 /* get session RTCP pad */
2936 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
2937 pad = gst_element_get_request_pad (src->manager, name);
2942 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
2943 gst_object_unref (pad);
2946 gst_object_unref (template);
2952 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
2953 GstRTSPTransport * transport, const gchar ** destination, gint * min,
2954 gint * max, guint * ttl)
2956 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2958 if (!(*destination = transport->destination))
2959 *destination = stream->destination;
2962 /* transport first */
2963 *min = transport->port.min;
2964 *max = transport->port.max;
2965 if (*min == -1 && *max == -1) {
2966 /* then try from SDP */
2967 if (stream->port != 0) {
2968 *min = stream->port;
2969 *max = stream->port + 1;
2975 if (!(*ttl = transport->ttl))
2980 /* first take the source, then the endpoint to figure out where to send
2982 if (!(*destination = transport->source)) {
2983 if (src->conninfo.connection)
2984 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
2985 else if (stream->conninfo.connection)
2987 gst_rtsp_connection_get_ip (stream->conninfo.connection);
2991 /* for unicast we only expect the ports here */
2992 *min = transport->server_port.min;
2993 *max = transport->server_port.max;
2998 /* For multicast create UDP sources and join the multicast group. */
3000 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3001 GstRTSPTransport * transport, GstPad ** outpad)
3004 const gchar *destination;
3007 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3009 /* we can remove the allocated UDP ports now */
3010 gst_rtspsrc_stream_free_udp (stream);
3012 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3015 /* we need a destination now */
3016 if (destination == NULL)
3017 goto no_destination;
3019 /* we really need ports now or we won't be able to receive anything at all */
3020 if (min == -1 && max == -1)
3023 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3024 destination, min, max);
3026 /* creating UDP source for RTP */
3028 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3030 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3032 if (stream->udpsrc[0] == NULL)
3035 /* take ownership */
3036 gst_object_ref_sink (stream->udpsrc[0]);
3038 if (src->udp_buffer_size != 0)
3039 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3040 src->udp_buffer_size, NULL);
3042 if (src->multi_iface != NULL)
3043 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3044 src->multi_iface, NULL);
3047 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3048 gst_element_set_state (stream->udpsrc[0], GST_STATE_PAUSED);
3051 /* creating another UDP source for RTCP */
3055 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3057 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3059 if (stream->udpsrc[1] == NULL)
3062 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3063 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3064 gst_caps_unref (caps);
3066 /* take ownership */
3067 gst_object_ref_sink (stream->udpsrc[1]);
3069 if (src->multi_iface != NULL)
3070 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3071 src->multi_iface, NULL);
3073 gst_element_set_state (stream->udpsrc[1], GST_STATE_PAUSED);
3080 GST_DEBUG_OBJECT (src, "no UDP source element found");
3085 GST_DEBUG_OBJECT (src, "no destination found");
3090 GST_DEBUG_OBJECT (src, "no ports found");
3095 /* configure the remainder of the UDP ports */
3097 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3098 GstRTSPTransport * transport, GstPad ** outpad)
3100 /* we manage the UDP elements now. For unicast, the UDP sources where
3101 * allocated in the stream when we suggested a transport. */
3102 if (stream->udpsrc[0]) {
3103 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3104 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3106 GST_DEBUG_OBJECT (src, "setting up UDP source");
3108 /* configure a timeout on the UDP port. When the timeout message is
3109 * posted, we assume UDP transport is not possible. We reconnect using TCP
3111 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3112 src->udp_timeout * 1000, NULL);
3114 /* get output pad of the UDP source. */
3115 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3117 /* save it so we can unblock */
3118 stream->blockedpad = *outpad;
3120 /* configure pad block on the pad. As soon as there is dataflow on the
3121 * UDP source, we know that UDP is not blocked by a firewall and we can
3122 * configure all the streams to let the application autoplug decoders. */
3124 gst_pad_add_probe (stream->blockedpad,
3125 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, pad_blocked, src, NULL);
3127 if (stream->channelpad[0]) {
3128 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3129 /* configure for UDP delivery, we need to connect the UDP pads to
3130 * the session plugin. */
3131 gst_pad_link_full (*outpad, stream->channelpad[0],
3132 GST_PAD_LINK_CHECK_NOTHING);
3133 gst_object_unref (*outpad);
3135 /* we connected to pad-added signal to get pads from the manager */
3137 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3142 if (stream->udpsrc[1]) {
3145 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3146 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3148 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3149 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3150 gst_caps_unref (caps);
3152 if (stream->channelpad[1]) {
3155 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3157 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3158 gst_pad_link_full (pad, stream->channelpad[1],
3159 GST_PAD_LINK_CHECK_NOTHING);
3160 gst_object_unref (pad);
3162 /* leave unlinked */
3168 /* configure the UDP sink back to the server for status reports */
3170 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3171 GstRTSPStream * stream, GstRTSPTransport * transport)
3174 gint rtp_port, rtcp_port;
3175 gboolean do_rtp, do_rtcp;
3176 const gchar *destination;
3181 /* get transport info */
3182 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3183 &rtp_port, &rtcp_port, &ttl);
3185 /* see what we need to do */
3186 do_rtp = (rtp_port != -1);
3187 /* it's possible that the server does not want us to send RTCP in which case
3189 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3191 /* we need a destination when we have RTP or RTCP ports */
3192 if (destination == NULL && (do_rtp || do_rtcp))
3193 goto no_destination;
3195 /* try to construct the fakesrc to the RTP port of the server to open up any
3198 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3201 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3202 stream->udpsink[0] =
3203 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3205 if (stream->udpsink[0] == NULL)
3206 goto no_sink_element;
3208 /* don't join multicast group, we will have the source socket do that */
3209 /* no sync or async state changes needed */
3210 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3211 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3213 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3215 if (stream->udpsrc[0]) {
3216 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3217 * so that NAT firewalls will open a hole for us */
3218 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3219 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3220 /* configure socket and make sure udpsink does not close it when shutting
3221 * down, it belongs to udpsrc after all. */
3222 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3223 "close-socket", FALSE, NULL);
3224 g_object_unref (socket);
3227 /* the source for the dummy packets to open up NAT */
3228 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3229 if (stream->fakesrc == NULL)
3230 goto no_fakesrc_element;
3232 /* random data in 5 buffers, a size of 200 bytes should be fine */
3233 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3234 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3236 /* we don't want to consider this a sink */
3237 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
3239 /* keep everything locked */
3240 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3241 gst_element_set_locked_state (stream->fakesrc, TRUE);
3243 gst_object_ref (stream->udpsink[0]);
3244 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3245 gst_object_ref (stream->fakesrc);
3246 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3248 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3249 "sink", GST_PAD_LINK_CHECK_NOTHING);
3252 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3255 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3256 stream->udpsink[1] =
3257 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3259 if (stream->udpsink[1] == NULL)
3260 goto no_sink_element;
3262 /* don't join multicast group, we will have the source socket do that */
3263 /* no sync or async state changes needed */
3264 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3265 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3267 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3269 if (stream->udpsrc[1]) {
3270 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3271 * because some servers check the port number of where it sends RTCP to identify
3272 * the RTCP packets it receives */
3273 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3274 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3275 /* configure socket and make sure udpsink does not close it when shutting
3276 * down, it belongs to udpsrc after all. */
3277 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3278 "close-socket", FALSE, NULL);
3279 g_object_unref (socket);
3282 /* we don't want to consider this a sink */
3283 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3285 /* we keep this playing always */
3286 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3287 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3289 gst_object_ref (stream->udpsink[1]);
3290 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3292 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3294 /* get session RTCP pad */
3295 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3296 pad = gst_element_get_request_pad (src->manager, name);
3301 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3302 gst_object_unref (pad);
3311 GST_DEBUG_OBJECT (src, "no destination address specified");
3316 GST_DEBUG_OBJECT (src, "no UDP sink element found");
3321 GST_DEBUG_OBJECT (src, "no fakesrc element found");
3326 /* sets up all elements needed for streaming over the specified transport.
3327 * Does not yet expose the element pads, this will be done when there is actuall
3328 * dataflow detected, which might never happen when UDP is blocked in a
3329 * firewall, for example.
3332 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3333 GstRTSPTransport * transport)
3336 GstPad *outpad = NULL;
3337 GstPadTemplate *template;
3342 src = stream->parent;
3344 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3346 s = gst_caps_get_structure (stream->caps, 0);
3348 /* get the proper mime type for this stream now */
3349 if (gst_rtsp_transport_get_mime (transport->trans, &mime) < 0)
3350 goto unknown_transport;
3352 goto unknown_transport;
3354 /* configure the final mime type */
3355 GST_DEBUG_OBJECT (src, "setting mime to %s", mime);
3356 gst_structure_set_name (s, mime);
3358 /* try to get and configure a manager, channelpad[0-1] will be configured with
3359 * the pads for the manager, or NULL when no manager is needed. */
3360 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3363 switch (transport->lower_transport) {
3364 case GST_RTSP_LOWER_TRANS_TCP:
3365 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3366 goto transport_failed;
3368 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3369 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3370 goto transport_failed;
3371 /* fallthrough, the rest is the same for UDP and MCAST */
3372 case GST_RTSP_LOWER_TRANS_UDP:
3373 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3374 goto transport_failed;
3375 /* configure udpsinks back to the server for RTCP messages and for the
3376 * dummy RTP messages to open NAT. */
3377 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3378 goto transport_failed;
3381 goto unknown_transport;
3385 GST_DEBUG_OBJECT (src, "creating ghostpad");
3387 gst_pad_use_fixed_caps (outpad);
3389 /* create ghostpad, don't add just yet, this will be done when we activate
3391 name = g_strdup_printf ("stream_%u", stream->id);
3392 template = gst_static_pad_template_get (&rtptemplate);
3393 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3394 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3395 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3396 gst_object_unref (template);
3399 gst_object_unref (outpad);
3401 /* mark pad as ok */
3402 stream->last_ret = GST_FLOW_OK;
3409 GST_DEBUG_OBJECT (src, "failed to configure transport");
3414 GST_DEBUG_OBJECT (src, "unknown transport");
3419 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3424 /* send a couple of dummy random packets on the receiver RTP port to the server,
3425 * this should make a firewall think we initiated the data transfer and
3426 * hopefully allow packets to go from the sender port to our RTP receiver port */
3428 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3432 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3435 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3436 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3438 if (stream->fakesrc && stream->udpsink[0]) {
3439 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3440 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3441 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3442 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3443 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3449 /* Adds the source pads of all configured streams to the element.
3450 * This code is performed when we detected dataflow.
3452 * We detect dataflow from either the _loop function or with pad probes on the
3456 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3460 GST_DEBUG_OBJECT (src, "activating streams");
3462 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3463 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3465 if (stream->udpsrc[0]) {
3466 /* remove timeout, we are streaming now and timeouts will be handled by
3467 * the session manager and jitter buffer */
3468 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3470 if (stream->srcpad) {
3471 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3472 gst_pad_set_active (stream->srcpad, TRUE);
3474 /* if we don't have a session manager, set the caps now. If we have a
3475 * session, we will get a notification of the pad and the caps. */
3476 if (!src->manager) {
3477 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3478 gst_pad_set_caps (stream->srcpad, stream->caps);
3481 if (!stream->added) {
3482 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3483 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3484 stream->added = TRUE;
3489 /* unblock all pads */
3490 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3491 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3493 if (stream->blockid) {
3494 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3495 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3496 stream->blockid = 0;
3504 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
3505 gboolean reset_manager)
3508 guint64 start, stop;
3509 gdouble play_speed, play_scale;
3511 GST_DEBUG_OBJECT (src, "configuring stream caps");
3513 start = segment->position;
3514 stop = segment->duration;
3515 play_speed = segment->rate;
3516 play_scale = segment->applied_rate;
3518 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3519 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3522 if ((caps = stream->caps)) {
3523 caps = gst_caps_make_writable (caps);
3525 if (stream->timebase != -1)
3526 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3527 (guint) stream->timebase, NULL);
3528 if (stream->seqbase != -1)
3529 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3530 (guint) stream->seqbase, NULL);
3531 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3533 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3534 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3535 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3537 stream->caps = caps;
3539 GST_DEBUG_OBJECT (src, "stream %p, caps %" GST_PTR_FORMAT, stream, caps);
3541 if (reset_manager && src->manager) {
3542 GST_DEBUG_OBJECT (src, "clear session");
3543 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3547 static GstFlowReturn
3548 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3553 /* store the value */
3554 stream->last_ret = ret;
3556 /* if it's success we can return the value right away */
3557 if (ret == GST_FLOW_OK)
3560 /* any other error that is not-linked can be returned right
3562 if (ret != GST_FLOW_NOT_LINKED)
3565 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3566 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3567 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3569 ret = ostream->last_ret;
3570 /* some other return value (must be SUCCESS but we can return
3571 * other values as well) */
3572 if (ret != GST_FLOW_NOT_LINKED)
3575 /* if we get here, all other pads were unlinked and we return
3576 * NOT_LINKED then */
3582 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
3585 gboolean res = TRUE;
3587 /* only streams that have a connection to the outside world */
3588 if (stream->container || stream->disabled)
3591 if (stream->udpsrc[0]) {
3592 gst_event_ref (event);
3593 res = gst_element_send_event (stream->udpsrc[0], event);
3594 } else if (stream->channelpad[0]) {
3595 gst_event_ref (event);
3596 if (GST_PAD_IS_SRC (stream->channelpad[0]))
3597 res = gst_pad_push_event (stream->channelpad[0], event);
3599 res = gst_pad_send_event (stream->channelpad[0], event);
3602 if (stream->udpsrc[1]) {
3603 gst_event_ref (event);
3604 res &= gst_element_send_event (stream->udpsrc[1], event);
3605 } else if (stream->channelpad[1]) {
3606 gst_event_ref (event);
3607 if (GST_PAD_IS_SRC (stream->channelpad[1]))
3608 res &= gst_pad_push_event (stream->channelpad[1], event);
3610 res &= gst_pad_send_event (stream->channelpad[1], event);
3614 gst_event_unref (event);
3620 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
3623 gboolean res = TRUE;
3625 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3626 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3628 gst_event_ref (event);
3629 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
3631 gst_event_unref (event);
3636 static GstRTSPResult
3637 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3642 if (info->connection == NULL) {
3643 if (info->url == NULL) {
3644 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
3645 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
3649 /* create connection */
3650 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
3651 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
3652 goto could_not_create;
3655 g_free (info->url_str);
3656 info->url_str = gst_rtsp_url_get_request_uri (info->url);
3658 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
3660 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
3661 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
3662 src->tls_validation_flags))
3663 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
3666 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
3667 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
3669 if (src->proxy_host) {
3670 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
3672 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
3677 if (!info->connected) {
3680 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
3681 ("Connecting to %s", info->location));
3682 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
3684 gst_rtsp_connection_connect (info->connection,
3685 src->ptcp_timeout)) < 0)
3686 goto could_not_connect;
3688 info->connected = TRUE;
3695 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
3700 gchar *str = gst_rtsp_strresult (res);
3701 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
3707 gchar *str = gst_rtsp_strresult (res);
3708 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
3714 static GstRTSPResult
3715 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
3718 GST_RTSP_STATE_LOCK (src);
3719 if (info->connected) {
3720 GST_DEBUG_OBJECT (src, "closing connection...");
3721 gst_rtsp_connection_close (info->connection);
3722 info->connected = FALSE;
3724 if (free && info->connection) {
3725 /* free connection */
3726 GST_DEBUG_OBJECT (src, "freeing connection...");
3727 gst_rtsp_connection_free (info->connection);
3728 info->connection = NULL;
3730 GST_RTSP_STATE_UNLOCK (src);
3734 static GstRTSPResult
3735 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3740 GST_DEBUG_OBJECT (src, "reconnecting connection...");
3741 gst_rtsp_conninfo_close (src, info, FALSE);
3742 res = gst_rtsp_conninfo_connect (src, info, async);
3748 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
3752 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
3753 GST_RTSP_STATE_LOCK (src);
3754 if (src->conninfo.connection && src->conninfo.flushing != flush) {
3755 GST_DEBUG_OBJECT (src, "connection flush");
3756 gst_rtsp_connection_flush (src->conninfo.connection, flush);
3757 src->conninfo.flushing = flush;
3759 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3760 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3761 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
3762 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
3763 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
3764 stream->conninfo.flushing = flush;
3767 GST_RTSP_STATE_UNLOCK (src);
3770 /* FIXME, handle server request, reply with OK, for now */
3771 static GstRTSPResult
3772 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
3773 GstRTSPMessage * request)
3775 GstRTSPMessage response = { 0 };
3778 GST_DEBUG_OBJECT (src, "got server request message");
3781 gst_rtsp_message_dump (request);
3783 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
3785 if (res == GST_RTSP_ENOTIMPL) {
3786 /* default implementation, send OK */
3787 GST_DEBUG_OBJECT (src, "prepare OK reply");
3789 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
3794 /* let app parse and reply */
3795 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
3796 0, request, &response);
3799 gst_rtsp_message_dump (&response);
3801 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
3805 gst_rtsp_message_unset (&response);
3806 } else if (res == GST_RTSP_EEOF)
3814 gst_rtsp_message_unset (&response);
3819 /* send server keep-alive */
3820 static GstRTSPResult
3821 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
3823 GstRTSPMessage request = { 0 };
3825 GstRTSPMethod method;
3828 if (src->do_rtsp_keep_alive == FALSE) {
3829 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
3830 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3834 GST_DEBUG_OBJECT (src, "creating server keep-alive");
3836 /* find a method to use for keep-alive */
3837 if (src->methods & GST_RTSP_GET_PARAMETER)
3838 method = GST_RTSP_GET_PARAMETER;
3840 method = GST_RTSP_OPTIONS;
3843 control = src->control;
3845 control = src->conninfo.url_str;
3847 if (control == NULL)
3850 res = gst_rtsp_message_init_request (&request, method, control);
3855 gst_rtsp_message_dump (&request);
3858 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
3863 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3864 gst_rtsp_message_unset (&request);
3871 GST_WARNING_OBJECT (src, "no control url to send keepalive");
3876 gchar *str = gst_rtsp_strresult (res);
3878 gst_rtsp_message_unset (&request);
3879 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
3880 ("Could not send keep-alive. (%s)", str));
3886 static GstFlowReturn
3887 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
3889 GstFlowReturn ret = GST_FLOW_OK;
3891 GstRTSPStream *stream;
3892 GstPad *outpad = NULL;
3899 channel = message->type_data.data.channel;
3901 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
3903 goto unknown_stream;
3905 if (channel == stream->channel[0]) {
3906 outpad = stream->channelpad[0];
3908 } else if (channel == stream->channel[1]) {
3909 outpad = stream->channelpad[1];
3915 /* take a look at the body to figure out what we have */
3916 gst_rtsp_message_get_body (message, &data, &size);
3918 goto invalid_length;
3920 /* channels are not correct on some servers, do extra check */
3921 if (data[1] >= 200 && data[1] <= 204) {
3922 /* hmm RTCP message switch to the RTCP pad of the same stream. */
3923 outpad = stream->channelpad[1];
3927 /* we have no clue what this is, just ignore then. */
3929 goto unknown_stream;
3931 /* take the message body for further processing */
3932 gst_rtsp_message_steal_body (message, &data, &size);
3934 /* strip the trailing \0 */
3937 buf = gst_buffer_new ();
3938 gst_buffer_append_memory (buf,
3939 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
3941 /* don't need message anymore */
3942 gst_rtsp_message_unset (message);
3944 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
3947 if (src->need_activate) {
3953 guint group_id = gst_util_group_id_next ();
3955 /* generate an SHA256 sum of the URI */
3956 cs = g_checksum_new (G_CHECKSUM_SHA256);
3957 uri = src->conninfo.location;
3958 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
3960 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3961 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3964 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
3965 event = gst_event_new_stream_start (stream_id);
3966 gst_event_set_group_id (event, group_id);
3969 gst_rtspsrc_stream_push_event (src, ostream, event);
3971 g_checksum_free (cs);
3973 gst_rtspsrc_activate_streams (src);
3974 src->need_activate = FALSE;
3976 if ((event = src->start_segment) != NULL) {
3977 src->start_segment = NULL;
3978 gst_rtspsrc_push_event (src, event);
3981 if (src->base_time == -1) {
3982 /* Take current running_time. This timestamp will be put on
3983 * the first buffer of each stream because we are a live source and so we
3984 * timestamp with the running_time. When we are dealing with TCP, we also
3985 * only timestamp the first buffer (using the DISCONT flag) because a server
3986 * typically bursts data, for which we don't want to compensate by speeding
3987 * up the media. The other timestamps will be interpollated from this one
3988 * using the RTP timestamps. */
3989 GST_OBJECT_LOCK (src);
3990 if (GST_ELEMENT_CLOCK (src)) {
3992 GstClockTime base_time;
3994 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
3995 base_time = GST_ELEMENT_CAST (src)->base_time;
3997 src->base_time = now - base_time;
3999 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4000 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4002 GST_OBJECT_UNLOCK (src);
4005 if (stream->discont && !is_rtcp) {
4006 /* mark first RTP buffer as discont */
4007 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4008 stream->discont = FALSE;
4009 /* first buffer gets the timestamp, other buffers are not timestamped and
4010 * their presentation time will be interpollated from the rtp timestamps. */
4011 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4012 GST_TIME_ARGS (src->base_time));
4014 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4017 /* chain to the peer pad */
4018 if (GST_PAD_IS_SINK (outpad))
4019 ret = gst_pad_chain (outpad, buf);
4021 ret = gst_pad_push (outpad, buf);
4024 /* combine all stream flows for the data transport */
4025 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4032 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4033 gst_rtsp_message_unset (message);
4038 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4039 ("Short message received, ignoring."));
4040 gst_rtsp_message_unset (message);
4045 static GstFlowReturn
4046 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4048 GstRTSPMessage message = { 0 };
4050 GstFlowReturn ret = GST_FLOW_OK;
4051 GTimeVal tv_timeout;
4054 /* get the next timeout interval */
4055 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4057 /* see if the timeout period expired */
4058 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4059 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4060 /* send keep-alive, only act on interrupt, a warning will be posted for
4062 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4064 /* get new timeout */
4065 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4068 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4069 tv_timeout.tv_sec, tv_timeout.tv_usec);
4071 /* protect the connection with the connection lock so that we can see when
4072 * we are finished doing server communication */
4074 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4075 &message, src->ptcp_timeout);
4079 GST_DEBUG_OBJECT (src, "we received a server message");
4081 case GST_RTSP_EINTR:
4082 /* we got interrupted this means we need to stop */
4084 case GST_RTSP_ETIMEOUT:
4085 /* no reply, send keep alive */
4086 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4087 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4091 /* go EOS when the server closed the connection */
4097 switch (message.type) {
4098 case GST_RTSP_MESSAGE_REQUEST:
4099 /* server sends us a request message, handle it */
4101 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4103 if (res == GST_RTSP_EEOF)
4106 goto handle_request_failed;
4108 case GST_RTSP_MESSAGE_RESPONSE:
4109 /* we ignore response messages */
4110 GST_DEBUG_OBJECT (src, "ignoring response message");
4112 gst_rtsp_message_dump (&message);
4114 case GST_RTSP_MESSAGE_DATA:
4115 GST_DEBUG_OBJECT (src, "got data message");
4116 ret = gst_rtspsrc_handle_data (src, &message);
4117 if (ret != GST_FLOW_OK)
4118 goto handle_data_failed;
4121 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4126 g_assert_not_reached ();
4131 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4132 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4133 ("The server closed the connection."));
4134 src->conninfo.connected = FALSE;
4135 gst_rtsp_message_unset (&message);
4136 return GST_FLOW_EOS;
4140 gst_rtsp_message_unset (&message);
4141 GST_DEBUG_OBJECT (src, "got interrupted");
4142 return GST_FLOW_FLUSHING;
4146 gchar *str = gst_rtsp_strresult (res);
4148 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4149 ("Could not receive message. (%s)", str));
4152 gst_rtsp_message_unset (&message);
4153 return GST_FLOW_ERROR;
4155 handle_request_failed:
4157 gchar *str = gst_rtsp_strresult (res);
4159 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4160 ("Could not handle server message. (%s)", str));
4162 gst_rtsp_message_unset (&message);
4163 return GST_FLOW_ERROR;
4167 GST_DEBUG_OBJECT (src, "could no handle data message");
4172 static GstFlowReturn
4173 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4176 GstRTSPMessage message = { 0 };
4180 GTimeVal tv_timeout;
4182 /* get the next timeout interval */
4183 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4185 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4186 (gint) tv_timeout.tv_sec);
4188 gst_rtsp_message_unset (&message);
4190 /* we should continue reading the TCP socket because the server might
4191 * send us requests. When the session timeout expires, we need to send a
4192 * keep-alive request to keep the session open. */
4193 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4194 &message, &tv_timeout);
4198 GST_DEBUG_OBJECT (src, "we received a server message");
4200 case GST_RTSP_EINTR:
4201 /* we got interrupted, see what we have to do */
4203 case GST_RTSP_ETIMEOUT:
4204 /* send keep-alive, ignore the result, a warning will be posted. */
4205 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4206 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4210 /* server closed the connection. not very fatal for UDP, reconnect and
4211 * see what happens. */
4212 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4213 ("The server closed the connection."));
4214 if (src->udp_reconnect) {
4216 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4223 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4225 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4226 ("Unhandled return value %d.", res));
4230 switch (message.type) {
4231 case GST_RTSP_MESSAGE_REQUEST:
4232 /* server sends us a request message, handle it */
4234 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4236 if (res == GST_RTSP_EEOF)
4239 goto handle_request_failed;
4241 case GST_RTSP_MESSAGE_RESPONSE:
4242 /* we ignore response and data messages */
4243 GST_DEBUG_OBJECT (src, "ignoring response message");
4245 gst_rtsp_message_dump (&message);
4246 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4247 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4248 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4249 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4250 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4257 case GST_RTSP_MESSAGE_DATA:
4258 /* we ignore response and data messages */
4259 GST_DEBUG_OBJECT (src, "ignoring data message");
4262 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4267 g_assert_not_reached ();
4269 /* we get here when the connection got interrupted */
4272 gst_rtsp_message_unset (&message);
4273 GST_DEBUG_OBJECT (src, "got interrupted");
4274 return GST_FLOW_FLUSHING;
4278 gchar *str = gst_rtsp_strresult (res);
4281 src->conninfo.connected = FALSE;
4282 if (res != GST_RTSP_EINTR) {
4283 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4284 ("Could not connect to server. (%s)", str));
4286 ret = GST_FLOW_ERROR;
4288 ret = GST_FLOW_FLUSHING;
4294 gchar *str = gst_rtsp_strresult (res);
4296 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4297 ("Could not receive message. (%s)", str));
4299 return GST_FLOW_ERROR;
4301 handle_request_failed:
4303 gchar *str = gst_rtsp_strresult (res);
4306 gst_rtsp_message_unset (&message);
4307 if (res != GST_RTSP_EINTR) {
4308 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4309 ("Could not handle server message. (%s)", str));
4311 ret = GST_FLOW_ERROR;
4313 ret = GST_FLOW_FLUSHING;
4319 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4320 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4321 ("The server closed the connection."));
4322 src->conninfo.connected = FALSE;
4323 gst_rtsp_message_unset (&message);
4324 return GST_FLOW_EOS;
4328 static GstRTSPResult
4329 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4331 GstRTSPResult res = GST_RTSP_OK;
4334 GST_DEBUG_OBJECT (src, "doing reconnect");
4336 GST_OBJECT_LOCK (src);
4337 /* only restart when the pads were not yet activated, else we were
4338 * streaming over UDP */
4339 restart = src->need_activate;
4340 GST_OBJECT_UNLOCK (src);
4342 /* no need to restart, we're done */
4346 /* we can try only TCP now */
4347 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
4349 /* close and cleanup our state */
4350 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
4353 /* see if we have TCP left to try. Also don't try TCP when we were configured
4355 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
4358 /* We post a warning message now to inform the user
4359 * that nothing happened. It's most likely a firewall thing. */
4360 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4361 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4362 "firewall is blocking it. Retrying using a TCP connection.",
4363 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4365 /* open new connection using tcp */
4366 if (gst_rtspsrc_open (src, async) < 0)
4369 /* start playback */
4370 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
4379 src->cur_protocols = 0;
4380 /* no transport possible, post an error and stop */
4381 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4382 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4383 "firewall is blocking it. No other protocols to try.",
4384 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4385 return GST_RTSP_ERROR;
4389 GST_DEBUG_OBJECT (src, "open failed");
4394 GST_DEBUG_OBJECT (src, "play failed");
4400 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
4404 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
4407 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
4410 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
4413 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
4421 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
4425 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
4428 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
4431 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
4434 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4442 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4446 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4449 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4452 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4455 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4463 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4467 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
4470 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4473 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4476 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4484 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4486 if (ret == GST_RTSP_OK)
4487 gst_rtspsrc_loop_complete_cmd (src, cmd);
4488 else if (ret == GST_RTSP_EINTR)
4489 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4491 gst_rtspsrc_loop_error_cmd (src, cmd);
4495 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
4498 gboolean flushed = FALSE;
4500 /* start new request */
4501 gst_rtspsrc_loop_start_cmd (src, cmd);
4503 GST_DEBUG_OBJECT (src, "sending cmd %d", cmd);
4505 GST_OBJECT_LOCK (src);
4506 old = src->pending_cmd;
4507 if (old == CMD_RECONNECT) {
4508 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
4509 cmd = CMD_RECONNECT;
4511 if (old != CMD_WAIT) {
4512 src->pending_cmd = CMD_WAIT;
4513 GST_OBJECT_UNLOCK (src);
4514 /* cancel previous request */
4515 GST_DEBUG_OBJECT (src, "cancel previous request %d", old);
4516 gst_rtspsrc_loop_cancel_cmd (src, old);
4517 GST_OBJECT_LOCK (src);
4519 src->pending_cmd = cmd;
4520 /* interrupt if allowed */
4521 if (src->busy_cmd & mask) {
4522 GST_DEBUG_OBJECT (src, "connection flush busy %d", src->busy_cmd);
4523 gst_rtspsrc_connection_flush (src, TRUE);
4526 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %d", src->busy_cmd);
4529 gst_task_start (src->task);
4530 GST_OBJECT_UNLOCK (src);
4536 gst_rtspsrc_loop (GstRTSPSrc * src)
4540 if (!src->conninfo.connection || !src->conninfo.connected)
4543 if (src->interleaved)
4544 ret = gst_rtspsrc_loop_interleaved (src);
4546 ret = gst_rtspsrc_loop_udp (src);
4548 if (ret != GST_FLOW_OK)
4556 GST_WARNING_OBJECT (src, "we are not connected");
4557 ret = GST_FLOW_FLUSHING;
4562 const gchar *reason = gst_flow_get_name (ret);
4564 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
4565 src->running = FALSE;
4566 if (ret == GST_FLOW_EOS) {
4567 /* perform EOS logic */
4568 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4569 gst_element_post_message (GST_ELEMENT_CAST (src),
4570 gst_message_new_segment_done (GST_OBJECT_CAST (src),
4571 src->segment.format, src->segment.position));
4572 gst_rtspsrc_push_event (src,
4573 gst_event_new_segment_done (src->segment.format,
4574 src->segment.position));
4576 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4578 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
4579 /* for fatal errors we post an error message, post the error before the
4580 * EOS so the app knows about the error first. */
4581 GST_ELEMENT_ERROR (src, STREAM, FAILED,
4582 ("Internal data flow error."),
4583 ("streaming task paused, reason %s (%d)", reason, ret));
4584 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4586 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
4591 #ifndef GST_DISABLE_GST_DEBUG
4592 static const gchar *
4593 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
4597 while (method != 0) {
4614 static const gchar *
4615 gst_rtspsrc_skip_lws (const gchar * s)
4617 while (g_ascii_isspace (*s))
4622 static const gchar *
4623 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
4625 while (s > start && g_ascii_isspace (*(s - 1)))
4630 static const gchar *
4631 gst_rtspsrc_skip_commas (const gchar * s)
4633 /* The grammar allows for multiple commas */
4634 while (g_ascii_isspace (*s) || *s == ',')
4639 static const gchar *
4640 gst_rtspsrc_skip_item (const gchar * s)
4642 gboolean quoted = FALSE;
4643 const gchar *start = s;
4645 /* A list item ends at the last non-whitespace character
4646 * before a comma which is not inside a quoted-string. Or at
4647 * the end of the string.
4653 if (*s == '\\' && *(s + 1))
4662 return gst_rtspsrc_unskip_lws (s, start);
4666 gst_rtsp_decode_quoted_string (gchar * quoted_string)
4670 src = quoted_string + 1;
4671 dst = quoted_string;
4672 while (*src && *src != '"') {
4673 if (*src == '\\' && *(src + 1))
4680 /* Extract the authentication tokens that the server provided for each method
4681 * into an array of structures and give those to the connection object.
4684 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
4685 const gchar * header, gboolean * stale)
4687 GSList *list = NULL, *iter;
4689 gchar *item, *eq, *name_end, *value;
4691 g_return_if_fail (stale != NULL);
4693 gst_rtsp_connection_clear_auth_params (conn);
4696 /* Parse a header whose content is described by RFC2616 as
4697 * "#something", where "something" does not itself contain commas,
4698 * except as part of quoted-strings, into a list of allocated strings.
4700 header = gst_rtspsrc_skip_commas (header);
4702 end = gst_rtspsrc_skip_item (header);
4703 list = g_slist_prepend (list, g_strndup (header, end - header));
4704 header = gst_rtspsrc_skip_commas (end);
4709 list = g_slist_reverse (list);
4710 for (iter = list; iter; iter = iter->next) {
4713 eq = strchr (item, '=');
4715 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
4716 if (name_end == item) {
4717 /* That's no good... */
4724 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
4726 gst_rtsp_decode_quoted_string (value);
4730 if (item && (strcmp (item, "stale") == 0) &&
4731 value && (strcmp (value, "TRUE") == 0))
4733 gst_rtsp_connection_set_auth_param (conn, item, value);
4737 g_slist_free (list);
4740 /* Parse a WWW-Authenticate Response header and determine the
4741 * available authentication methods
4743 * This code should also cope with the fact that each WWW-Authenticate
4744 * header can contain multiple challenge methods + tokens
4746 * At the moment, for Basic auth, we just do a minimal check and don't
4747 * even parse out the realm */
4749 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
4750 GstRTSPConnection * conn, gboolean * stale)
4754 g_return_if_fail (hdr != NULL);
4755 g_return_if_fail (methods != NULL);
4756 g_return_if_fail (stale != NULL);
4758 /* Skip whitespace at the start of the string */
4759 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
4761 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
4762 *methods |= GST_RTSP_AUTH_BASIC;
4763 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
4764 *methods |= GST_RTSP_AUTH_DIGEST;
4765 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
4770 * gst_rtspsrc_setup_auth:
4771 * @src: the rtsp source
4773 * Configure a username and password and auth method on the
4774 * connection object based on a response we received from the
4777 * Currently, this requires that a username and password were supplied
4778 * in the uri. In the future, they may be requested on demand by sending
4779 * a message up the bus.
4781 * Returns: TRUE if authentication information could be set up correctly.
4784 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
4788 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
4789 GstRTSPAuthMethod method;
4790 GstRTSPResult auth_result;
4792 GstRTSPConnection *conn;
4794 gboolean stale = FALSE;
4796 conn = src->conninfo.connection;
4798 /* Identify the available auth methods and see if any are supported */
4799 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
4800 &hdr, 0) == GST_RTSP_OK) {
4801 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
4804 if (avail_methods == GST_RTSP_AUTH_NONE)
4805 goto no_auth_available;
4807 /* For digest auth, if the response indicates that the session
4808 * data are stale, we just update them in the connection object and
4809 * return TRUE to retry the request */
4811 src->tried_url_auth = FALSE;
4813 url = gst_rtsp_connection_get_url (conn);
4815 /* Do we have username and password available? */
4816 if (url != NULL && !src->tried_url_auth && url->user != NULL
4817 && url->passwd != NULL) {
4820 src->tried_url_auth = TRUE;
4821 GST_DEBUG_OBJECT (src,
4822 "Attempting authentication using credentials from the URL");
4824 user = src->user_id;
4825 pass = src->user_pw;
4826 GST_DEBUG_OBJECT (src,
4827 "Attempting authentication using credentials from the properties");
4830 /* FIXME: If the url didn't contain username and password or we tried them
4831 * already, request a username and passwd from the application via some kind
4832 * of credentials request message */
4834 /* If we don't have a username and passwd at this point, bail out. */
4835 if (user == NULL || pass == NULL)
4838 /* Try to configure for each available authentication method, strongest to
4840 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
4841 /* Check if this method is available on the server */
4842 if ((method & avail_methods) == 0)
4845 /* Pass the credentials to the connection to try on the next request */
4846 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
4847 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
4848 * ignore it and end up retrying later */
4849 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
4850 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
4851 gst_rtsp_auth_method_to_string (method));
4856 if (method == GST_RTSP_AUTH_NONE)
4857 goto no_auth_available;
4863 /* Output an error indicating that we couldn't connect because there were
4864 * no supported authentication protocols */
4865 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4866 ("No supported authentication protocol was found"));
4871 /* We don't fire an error message, we just return FALSE and let the
4872 * normal NOT_AUTHORIZED error be propagated */
4877 static GstRTSPResult
4878 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4879 GstRTSPMessage * request, GstRTSPMessage * response,
4880 GstRTSPStatusCode * code)
4883 GstRTSPStatusCode thecode;
4884 gchar *content_base = NULL;
4888 if (!src->short_header)
4889 gst_rtsp_ext_list_before_send (src->extensions, request);
4891 GST_DEBUG_OBJECT (src, "sending message");
4894 gst_rtsp_message_dump (request);
4896 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
4900 gst_rtsp_connection_reset_timeout (conn);
4903 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
4908 gst_rtsp_message_dump (response);
4910 switch (response->type) {
4911 case GST_RTSP_MESSAGE_REQUEST:
4912 res = gst_rtspsrc_handle_request (src, conn, response);
4913 if (res == GST_RTSP_EEOF)
4916 goto handle_request_failed;
4918 case GST_RTSP_MESSAGE_RESPONSE:
4919 /* ok, a response is good */
4920 GST_DEBUG_OBJECT (src, "received response message");
4922 case GST_RTSP_MESSAGE_DATA:
4923 /* get next response */
4924 GST_DEBUG_OBJECT (src, "handle data response message");
4925 gst_rtspsrc_handle_data (src, response);
4928 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4933 thecode = response->type_data.response.code;
4935 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
4937 /* if the caller wanted the result code, we store it. */
4941 /* If the request didn't succeed, bail out before doing any more */
4942 if (thecode != GST_RTSP_STS_OK)
4945 /* store new content base if any */
4946 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
4949 g_free (src->content_base);
4950 src->content_base = g_strdup (content_base);
4952 gst_rtsp_ext_list_after_send (src->extensions, request, response);
4959 gchar *str = gst_rtsp_strresult (res);
4961 if (res != GST_RTSP_EINTR) {
4962 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4963 ("Could not send message. (%s)", str));
4965 GST_WARNING_OBJECT (src, "send interrupted");
4974 GST_WARNING_OBJECT (src, "server closed connection");
4975 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
4977 /* if reconnect succeeds, try again */
4979 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
4983 /* only try once after reconnect, then fallthrough and error out */
4986 gchar *str = gst_rtsp_strresult (res);
4988 if (res != GST_RTSP_EINTR) {
4989 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4990 ("Could not receive message. (%s)", str));
4992 GST_WARNING_OBJECT (src, "receive interrupted");
5000 handle_request_failed:
5002 /* ERROR was posted */
5003 gst_rtsp_message_unset (response);
5008 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5009 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5010 ("The server closed the connection."));
5011 gst_rtsp_message_unset (response);
5018 * @src: the rtsp source
5019 * @conn: the connection to send on
5020 * @request: must point to a valid request
5021 * @response: must point to an empty #GstRTSPMessage
5022 * @code: an optional code result
5024 * send @request and retrieve the response in @response. optionally @code can be
5025 * non-NULL in which case it will contain the status code of the response.
5027 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5028 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5030 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5031 * @response message) if the response code was not 200 (OK).
5033 * If the attempt results in an authentication failure, then this will attempt
5034 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5037 * Returns: #GST_RTSP_OK if the processing was successful.
5039 static GstRTSPResult
5040 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5041 GstRTSPMessage * request, GstRTSPMessage * response,
5042 GstRTSPStatusCode * code)
5044 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5045 GstRTSPResult res = GST_RTSP_ERROR;
5048 GstRTSPMethod method = GST_RTSP_INVALID;
5054 /* make sure we don't loop forever */
5058 /* save method so we can disable it when the server complains */
5059 method = request->type_data.request.method;
5062 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5066 case GST_RTSP_STS_UNAUTHORIZED:
5067 if (gst_rtspsrc_setup_auth (src, response)) {
5068 /* Try the request/response again after configuring the auth info
5076 } while (retry == TRUE);
5078 /* If the user requested the code, let them handle errors, otherwise
5079 * post an error below */
5082 else if (int_code != GST_RTSP_STS_OK)
5083 goto error_response;
5090 GST_DEBUG_OBJECT (src, "got error %d", res);
5095 res = GST_RTSP_ERROR;
5097 switch (response->type_data.response.code) {
5098 case GST_RTSP_STS_NOT_FOUND:
5099 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5100 response->type_data.response.reason));
5102 case GST_RTSP_STS_MOVED_PERMANENTLY:
5103 case GST_RTSP_STS_MOVE_TEMPORARILY:
5105 gchar *new_location;
5106 GstRTSPLowerTrans transports;
5108 GST_DEBUG_OBJECT (src, "got redirection");
5109 /* if we don't have a Location Header, we must error */
5110 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5111 &new_location, 0) < 0)
5114 /* When we receive a redirect result, we go back to the INIT state after
5115 * parsing the new URI. The caller should do the needed steps to issue
5116 * a new setup when it detects this state change. */
5117 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5119 /* save current transports */
5120 if (src->conninfo.url)
5121 transports = src->conninfo.url->transports;
5123 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5125 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5127 /* set old transports */
5128 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5129 src->conninfo.url->transports = transports;
5131 src->need_redirect = TRUE;
5132 src->state = GST_RTSP_STATE_INIT;
5136 case GST_RTSP_STS_NOT_ACCEPTABLE:
5137 case GST_RTSP_STS_NOT_IMPLEMENTED:
5138 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5139 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5140 gst_rtsp_method_as_text (method));
5141 src->methods &= ~method;
5145 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5146 ("Got error response: %d (%s).", response->type_data.response.code,
5147 response->type_data.response.reason));
5150 /* if we return ERROR we should unset the response ourselves */
5151 if (res == GST_RTSP_ERROR)
5152 gst_rtsp_message_unset (response);
5158 static GstRTSPResult
5159 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5160 GstRTSPMessage * response, GstRTSPSrc * src)
5162 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5167 /* parse the response and collect all the supported methods. We need this
5168 * information so that we don't try to send an unsupported request to the
5172 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5174 GstRTSPHeaderField field;
5178 /* reset supported methods */
5181 /* Try Allow Header first */
5182 field = GST_RTSP_HDR_ALLOW;
5185 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5186 if (indx == 0 && !respoptions) {
5187 /* if no Allow header was found then try the Public header... */
5188 field = GST_RTSP_HDR_PUBLIC;
5189 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5194 src->methods |= gst_rtsp_options_from_text (respoptions);
5199 if (src->methods == 0) {
5200 /* neither Allow nor Public are required, assume the server supports
5201 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5203 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5204 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5206 /* always assume PLAY, FIXME, extensions should be able to override
5208 src->methods |= GST_RTSP_PLAY;
5209 /* also assume it will support Range */
5210 src->seekable = TRUE;
5212 /* we need describe and setup */
5213 if (!(src->methods & GST_RTSP_DESCRIBE))
5215 if (!(src->methods & GST_RTSP_SETUP))
5223 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5224 ("Server does not support DESCRIBE."));
5229 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5230 ("Server does not support SETUP."));
5235 /* masks to be kept in sync with the hardcoded protocol order of preference
5237 static guint protocol_masks[] = {
5238 GST_RTSP_LOWER_TRANS_UDP,
5239 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5240 GST_RTSP_LOWER_TRANS_TCP,
5244 static GstRTSPResult
5245 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5246 GstRTSPLowerTrans protocols, gchar ** transports)
5250 gboolean add_udp_str;
5255 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5260 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5262 /* extension listed transports, use those */
5263 if (*transports != NULL)
5266 /* it's the default */
5267 add_udp_str = FALSE;
5269 /* the default RTSP transports */
5270 result = g_string_new ("");
5271 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5272 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5274 g_string_append (result, "RTP/AVP");
5276 g_string_append (result, "/UDP");
5277 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5278 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5279 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5281 /* we don't have to allocate any UDP ports yet, if the selected transport
5282 * turns out to be multicast we can create them and join the multicast
5283 * group indicated in the transport reply */
5284 if (result->len > 0)
5285 g_string_append (result, ",");
5286 g_string_append (result, "RTP/AVP");
5288 g_string_append (result, "/UDP");
5289 g_string_append (result, ";multicast");
5290 if (src->next_port_num != 0) {
5291 if (src->client_port_range.max > 0 &&
5292 src->next_port_num >= src->client_port_range.max)
5295 g_string_append_printf (result, ";client_port=%d-%d",
5296 src->next_port_num, src->next_port_num + 1);
5298 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
5299 GST_DEBUG_OBJECT (src, "adding TCP");
5301 if (result->len > 0)
5302 g_string_append (result, ",");
5303 g_string_append (result, "RTP/AVP/TCP;unicast;interleaved=%%i1-%%i2");
5305 *transports = g_string_free (result, FALSE);
5307 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5314 GST_ERROR ("extension gave error %d", res);
5319 GST_ERROR ("no more ports available");
5320 return GST_RTSP_ERROR;
5324 static GstRTSPResult
5325 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5326 gint orig_rtpport, gint orig_rtcpport)
5329 gint nr_udp, nr_int;
5331 gint rtpport = 0, rtcpport = 0;
5334 src = stream->parent;
5336 /* find number of placeholders first */
5337 if (strstr (*transports, "%%i2"))
5339 else if (strstr (*transports, "%%i1"))
5344 if (strstr (*transports, "%%u2"))
5346 else if (strstr (*transports, "%%u1"))
5351 if (nr_udp == 0 && nr_int == 0)
5355 if (!orig_rtpport || !orig_rtcpport) {
5356 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5359 rtpport = orig_rtpport;
5360 rtcpport = orig_rtcpport;
5364 str = g_string_new ("");
5366 while ((next = strstr (p, "%%"))) {
5367 g_string_append_len (str, p, next - p);
5368 if (next[2] == 'u') {
5370 g_string_append_printf (str, "%d", rtpport);
5371 else if (next[3] == '2')
5372 g_string_append_printf (str, "%d", rtcpport);
5374 if (next[2] == 'i') {
5376 g_string_append_printf (str, "%d", src->free_channel);
5377 else if (next[3] == '2')
5378 g_string_append_printf (str, "%d", src->free_channel + 1);
5383 /* append final part */
5384 g_string_append (str, p);
5386 g_free (*transports);
5387 *transports = g_string_free (str, FALSE);
5395 GST_ERROR ("failed to allocate udp ports");
5396 return GST_RTSP_ERROR;
5401 gst_rtspsrc_stream_is_real_media (GstRTSPStream * stream)
5403 gboolean res = FALSE;
5407 const gchar *enc = NULL;
5409 s = gst_caps_get_structure (stream->caps, 0);
5410 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
5411 res = (strstr (enc, "-REAL") != NULL);
5417 /* Perform the SETUP request for all the streams.
5419 * We ask the server for a specific transport, which initially includes all the
5420 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
5421 * two local UDP ports that we send to the server.
5423 * Once the server replied with a transport, we configure the other streams
5424 * with the same transport.
5426 * This function will also configure the stream for the selected transport,
5427 * which basically means creating the pipeline.
5429 static GstRTSPResult
5430 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
5433 GstRTSPResult res = GST_RTSP_ERROR;
5434 GstRTSPMessage request = { 0 };
5435 GstRTSPMessage response = { 0 };
5436 GstRTSPStream *stream = NULL;
5437 GstRTSPLowerTrans protocols;
5438 GstRTSPStatusCode code;
5439 gboolean unsupported_real = FALSE;
5440 gint rtpport, rtcpport;
5444 if (src->conninfo.connection) {
5445 url = gst_rtsp_connection_get_url (src->conninfo.connection);
5446 /* we initially allow all configured lower transports. based on the URL
5447 * transports and the replies from the server we narrow them down. */
5448 protocols = url->transports & src->cur_protocols;
5451 protocols = src->cur_protocols;
5457 /* reset some state */
5458 src->free_channel = 0;
5459 src->interleaved = FALSE;
5460 src->need_activate = FALSE;
5461 /* keep track of next port number, 0 is random */
5462 src->next_port_num = src->client_port_range.min;
5463 rtpport = rtcpport = 0;
5465 if (G_UNLIKELY (src->streams == NULL))
5468 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5469 GstRTSPConnection *conn;
5475 stream = (GstRTSPStream *) walk->data;
5477 /* see if we need to configure this stream */
5478 if (!gst_rtsp_ext_list_configure_stream (src->extensions, stream->caps)) {
5479 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
5481 stream->disabled = TRUE;
5485 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
5486 stream->id, stream->caps, &selected);
5488 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
5489 stream->disabled = TRUE;
5492 stream->disabled = FALSE;
5494 /* merge/overwrite global caps */
5499 s = gst_caps_get_structure (stream->caps, 0);
5501 num = gst_structure_n_fields (src->props);
5502 for (j = 0; j < num; j++) {
5506 name = gst_structure_nth_field_name (src->props, j);
5507 val = gst_structure_get_value (src->props, name);
5508 gst_structure_set_value (s, name, val);
5510 GST_DEBUG_OBJECT (src, "copied %s", name);
5514 /* skip setup if we have no URL for it */
5515 if (stream->conninfo.location == NULL) {
5516 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
5520 if (src->conninfo.connection == NULL) {
5521 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
5522 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
5525 conn = stream->conninfo.connection;
5527 conn = src->conninfo.connection;
5529 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
5530 stream->conninfo.location);
5532 /* if we have a multicast connection, only suggest multicast from now on */
5533 if (stream->is_multicast)
5534 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
5537 /* first selectable protocol */
5538 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5540 if (!protocol_masks[mask])
5544 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
5545 protocol_masks[mask]);
5546 /* create a string with first transport in line */
5548 res = gst_rtspsrc_create_transports_string (src,
5549 protocols & protocol_masks[mask], &transports);
5550 if (res < 0 || transports == NULL)
5551 goto setup_transport_failed;
5553 if (strlen (transports) == 0) {
5554 g_free (transports);
5555 GST_DEBUG_OBJECT (src, "no transports found");
5560 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
5562 /* replace placeholders with real values, this function will optionally
5563 * allocate UDP ports and other info needed to execute the setup request */
5564 res = gst_rtspsrc_prepare_transports (stream, &transports,
5565 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
5567 g_free (transports);
5568 goto setup_transport_failed;
5571 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
5573 /* create SETUP request */
5575 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
5576 stream->conninfo.location);
5578 g_free (transports);
5579 goto create_request_failed;
5582 /* select transport */
5583 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
5585 /* if the user wants a non default RTP packet size we add the blocksize
5587 if (src->rtp_blocksize > 0) {
5588 hval = g_strdup_printf ("%d", src->rtp_blocksize);
5589 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
5593 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
5596 /* handle the code ourselves */
5597 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
5601 case GST_RTSP_STS_OK:
5603 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
5604 gst_rtsp_message_unset (&request);
5605 gst_rtsp_message_unset (&response);
5606 /* cleanup of leftover transport */
5607 gst_rtspsrc_stream_free_udp (stream);
5608 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
5609 * we might be in this case */
5610 if (stream->container && rtpport && rtcpport && !retry) {
5611 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
5616 /* this transport did not go down well, but we may have others to try
5617 * that we did not send yet, try those and only give up then
5618 * but not without checking for lost cause/extension so we can
5619 * post a nicer/more useful error message later */
5620 if (!unsupported_real)
5621 unsupported_real = gst_rtspsrc_stream_is_real_media (stream);
5622 /* select next available protocol, give up on this stream if none */
5624 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5626 if (!protocol_masks[mask] || unsupported_real)
5631 /* cleanup of leftover transport and move to the next stream */
5632 gst_rtspsrc_stream_free_udp (stream);
5633 goto response_error;
5636 /* parse response transport */
5638 gchar *resptrans = NULL;
5639 GstRTSPTransport transport = { 0 };
5641 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
5644 gst_rtspsrc_stream_free_udp (stream);
5648 /* parse transport, go to next stream on parse error */
5649 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
5650 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
5654 /* update allowed transports for other streams. once the transport of
5655 * one stream has been determined, we make sure that all other streams
5656 * are configured in the same way */
5657 switch (transport.lower_transport) {
5658 case GST_RTSP_LOWER_TRANS_TCP:
5659 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
5660 protocols = GST_RTSP_LOWER_TRANS_TCP;
5661 src->interleaved = TRUE;
5662 /* update free channels */
5664 MAX (transport.interleaved.min, src->free_channel);
5666 MAX (transport.interleaved.max, src->free_channel);
5667 src->free_channel++;
5669 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
5670 /* only allow multicast for other streams */
5671 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
5672 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
5673 /* if the server selected our ports, increment our counters so that
5674 * we select a new port later */
5675 if (src->next_port_num == transport.port.min &&
5676 src->next_port_num + 1 == transport.port.max) {
5677 src->next_port_num += 2;
5680 case GST_RTSP_LOWER_TRANS_UDP:
5681 /* only allow unicast for other streams */
5682 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
5683 protocols = GST_RTSP_LOWER_TRANS_UDP;
5686 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
5687 transport.lower_transport);
5691 if (!stream->container || (!src->interleaved && !retry)) {
5692 /* now configure the stream with the selected transport */
5693 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
5694 GST_DEBUG_OBJECT (src,
5695 "could not configure stream %p transport, skipping stream",
5698 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
5699 /* retain the first allocated UDP port pair */
5700 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
5701 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
5704 /* we need to activate at least one streams when we detect activity */
5705 src->need_activate = TRUE;
5707 /* clean up our transport struct */
5708 gst_rtsp_transport_init (&transport);
5709 /* clean up used RTSP messages */
5710 gst_rtsp_message_unset (&request);
5711 gst_rtsp_message_unset (&response);
5715 /* store the transport protocol that was configured */
5716 src->cur_protocols = protocols;
5718 gst_rtsp_ext_list_stream_select (src->extensions, url);
5720 /* if there is nothing to activate, error out */
5721 if (!src->need_activate)
5722 goto nothing_to_activate;
5729 /* no transport possible, post an error and stop */
5730 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5731 ("Could not connect to server, no protocols left"));
5732 return GST_RTSP_ERROR;
5736 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5737 ("SDP contains no streams"));
5738 return GST_RTSP_ERROR;
5740 create_request_failed:
5742 gchar *str = gst_rtsp_strresult (res);
5744 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5745 ("Could not create request. (%s)", str));
5749 setup_transport_failed:
5751 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5752 ("Could not setup transport."));
5753 res = GST_RTSP_ERROR;
5758 const gchar *str = gst_rtsp_status_as_text (code);
5760 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5761 ("Error (%d): %s", code, GST_STR_NULL (str)));
5762 res = GST_RTSP_ERROR;
5767 gchar *str = gst_rtsp_strresult (res);
5769 if (res != GST_RTSP_EINTR) {
5770 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5771 ("Could not send message. (%s)", str));
5773 GST_WARNING_OBJECT (src, "send interrupted");
5780 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5781 ("Server did not select transport."));
5782 res = GST_RTSP_ERROR;
5785 nothing_to_activate:
5787 /* none of the available error codes is really right .. */
5788 if (unsupported_real) {
5789 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5790 (_("No supported stream was found. You might need to install a "
5791 "GStreamer RTSP extension plugin for Real media streams.")),
5794 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5795 (_("No supported stream was found. You might need to allow "
5796 "more transport protocols or may otherwise be missing "
5797 "the right GStreamer RTSP extension plugin.")), (NULL));
5799 return GST_RTSP_ERROR;
5803 gst_rtsp_message_unset (&request);
5804 gst_rtsp_message_unset (&response);
5810 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
5811 GstSegment * segment)
5814 GstRTSPTimeRange *therange;
5817 gst_rtsp_range_free (src->range);
5819 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
5820 GST_DEBUG_OBJECT (src, "parsed range %s", range);
5821 src->range = therange;
5823 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
5825 gst_segment_init (segment, GST_FORMAT_TIME);
5829 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
5830 therange->min.type, therange->min.seconds, therange->max.type,
5831 therange->max.seconds);
5833 if (therange->min.type == GST_RTSP_TIME_NOW)
5835 else if (therange->min.type == GST_RTSP_TIME_END)
5838 seconds = therange->min.seconds * GST_SECOND;
5840 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
5841 GST_TIME_ARGS (seconds));
5843 /* we need to start playback without clipping from the position reported by
5845 segment->start = seconds;
5846 segment->position = seconds;
5848 if (therange->max.type == GST_RTSP_TIME_NOW)
5850 else if (therange->max.type == GST_RTSP_TIME_END)
5853 seconds = therange->max.seconds * GST_SECOND;
5855 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
5856 GST_TIME_ARGS (seconds));
5858 /* live (WMS) server might send overflowed large max as its idea of infinity,
5859 * compensate to prevent problems later on */
5860 if (seconds != -1 && seconds < 0) {
5862 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
5865 /* live (WMS) might send min == max, which is not worth recording */
5866 if (segment->duration == -1 && seconds == segment->start)
5869 /* don't change duration with unknown value, we might have a valid value
5870 * there that we want to keep. */
5872 segment->duration = seconds;
5877 /* Parse clock profived by the server with following syntax:
5879 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
5882 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
5884 gboolean res = FALSE;
5886 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
5887 gchar **fields = NULL, **parts = NULL;
5888 gchar *remote_ip, *str;
5890 GstClockTime base_time;
5893 fields = g_strsplit (gstclock, " ", 0);
5895 /* wrapped clock, not very interesting for now */
5896 if (fields[1] == NULL)
5899 /* remote IP address and port */
5900 if ((str = fields[2]) == NULL)
5903 parts = g_strsplit (str, ":", 0);
5905 if ((remote_ip = parts[0]) == NULL)
5908 if ((str = parts[1]) == NULL)
5916 if ((str = fields[3]) == NULL)
5919 base_time = g_ascii_strtoull (str, NULL, 10);
5922 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
5925 if (src->provided_clock)
5926 gst_object_unref (src->provided_clock);
5927 src->provided_clock = netclock;
5929 gst_element_post_message (GST_ELEMENT_CAST (src),
5930 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
5931 src->provided_clock, TRUE));
5935 g_strfreev (fields);
5941 /* must be called with the RTSP state lock */
5942 static GstRTSPResult
5943 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
5949 /* prepare global stream caps properties */
5951 gst_structure_remove_all_fields (src->props);
5953 src->props = gst_structure_new_empty ("RTSPProperties");
5956 gst_sdp_message_dump (sdp);
5958 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
5960 /* let the app inspect and change the SDP */
5961 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
5963 gst_segment_init (&src->segment, GST_FORMAT_TIME);
5965 /* parse range for duration reporting. */
5970 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
5974 /* keep track of the range and configure it in the segment */
5975 if (gst_rtspsrc_parse_range (src, range, &src->segment))
5979 /* parse clock information. This is GStreamer specific, a server can tell the
5980 * client what clock it is using and wrap that in a network clock. The
5981 * advantage of that is that we can slave to it. */
5983 const gchar *gstclock;
5986 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
5987 if (gstclock == NULL)
5990 /* parse the clock and expose it in the provide_clock method */
5991 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
5995 /* try to find a global control attribute. Note that a '*' means that we should
5996 * do aggregate control with the current url (so we don't do anything and
5997 * leave the current connection as is) */
5999 const gchar *control;
6002 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6003 if (control == NULL)
6006 /* only take fully qualified urls */
6007 if (g_str_has_prefix (control, "rtsp://"))
6011 g_free (src->conninfo.location);
6012 src->conninfo.location = g_strdup (control);
6013 /* make a connection for this, if there was a connection already, nothing
6015 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6016 GST_ERROR_OBJECT (src, "could not connect");
6019 /* we need to keep the control url separate from the connection url because
6020 * the rules for constructing the media control url need it */
6021 g_free (src->control);
6022 src->control = g_strdup (control);
6025 /* create streams */
6026 n_streams = gst_sdp_message_medias_len (sdp);
6027 for (i = 0; i < n_streams; i++) {
6028 gst_rtspsrc_create_stream (src, sdp, i);
6031 src->state = GST_RTSP_STATE_INIT;
6034 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6037 /* reset our state */
6038 src->need_range = TRUE;
6041 src->state = GST_RTSP_STATE_READY;
6048 GST_ERROR_OBJECT (src, "setup failed");
6049 gst_rtspsrc_cleanup (src);
6054 static GstRTSPResult
6055 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6059 GstRTSPMessage request = { 0 };
6060 GstRTSPMessage response = { 0 };
6063 gchar *respcont = NULL;
6066 src->need_redirect = FALSE;
6068 /* can't continue without a valid url */
6069 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6070 res = GST_RTSP_EINVAL;
6073 src->tried_url_auth = FALSE;
6075 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6076 goto connect_failed;
6078 /* create OPTIONS */
6079 GST_DEBUG_OBJECT (src, "create options...");
6081 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
6082 src->conninfo.url_str);
6084 goto create_request_failed;
6087 GST_DEBUG_OBJECT (src, "send options...");
6090 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6093 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6098 if (!gst_rtspsrc_parse_methods (src, &response))
6101 /* create DESCRIBE */
6102 GST_DEBUG_OBJECT (src, "create describe...");
6104 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
6105 src->conninfo.url_str);
6107 goto create_request_failed;
6109 /* we only accept SDP for now */
6110 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
6114 GST_DEBUG_OBJECT (src, "send describe...");
6117 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
6120 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6124 /* we only perform redirect for the describe, currently */
6125 if (src->need_redirect) {
6126 /* close connection, we don't have to send a TEARDOWN yet, ignore the
6128 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6130 gst_rtsp_message_unset (&request);
6131 gst_rtsp_message_unset (&response);
6137 /* it could be that the DESCRIBE method was not implemented */
6138 if (!src->methods & GST_RTSP_DESCRIBE)
6141 /* check if reply is SDP */
6142 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
6144 /* could not be set but since the request returned OK, we assume it
6145 * was SDP, else check it. */
6147 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
6148 goto wrong_content_type;
6151 /* get message body and parse as SDP */
6152 gst_rtsp_message_get_body (&response, &data, &size);
6153 if (data == NULL || size == 0)
6156 GST_DEBUG_OBJECT (src, "parse SDP...");
6157 gst_sdp_message_new (sdp);
6158 gst_sdp_message_parse_buffer (data, size, *sdp);
6160 /* clean up any messages */
6161 gst_rtsp_message_unset (&request);
6162 gst_rtsp_message_unset (&response);
6169 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
6170 ("No valid RTSP URL was provided"));
6175 gchar *str = gst_rtsp_strresult (res);
6177 if (res != GST_RTSP_EINTR) {
6178 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6179 ("Failed to connect. (%s)", str));
6181 GST_WARNING_OBJECT (src, "connect interrupted");
6186 create_request_failed:
6188 gchar *str = gst_rtsp_strresult (res);
6190 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6191 ("Could not create request. (%s)", str));
6197 /* Don't post a message - the rtsp_send method will have
6198 * taken care of it because we passed NULL for the response code */
6203 /* error was posted */
6204 res = GST_RTSP_ERROR;
6209 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6210 ("Server does not support SDP, got %s.", respcont));
6211 res = GST_RTSP_ERROR;
6216 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6217 ("Server can not provide an SDP."));
6218 res = GST_RTSP_ERROR;
6223 if (src->conninfo.connection) {
6224 GST_DEBUG_OBJECT (src, "free connection");
6225 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6227 gst_rtsp_message_unset (&request);
6228 gst_rtsp_message_unset (&response);
6233 static GstRTSPResult
6234 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
6239 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
6241 if (src->sdp == NULL) {
6242 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
6246 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
6251 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
6258 GST_WARNING_OBJECT (src, "can't get sdp");
6259 src->open_error = TRUE;
6264 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
6265 src->open_error = TRUE;
6270 static GstRTSPResult
6271 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
6273 GstRTSPMessage request = { 0 };
6274 GstRTSPMessage response = { 0 };
6275 GstRTSPResult res = GST_RTSP_OK;
6279 GST_DEBUG_OBJECT (src, "TEARDOWN...");
6281 gst_rtspsrc_set_state (src, GST_STATE_READY);
6283 if (src->state < GST_RTSP_STATE_READY) {
6284 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
6291 /* construct a control url */
6293 control = src->control;
6295 control = src->conninfo.url_str;
6297 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
6300 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6301 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6303 GstRTSPConnInfo *info;
6305 /* try aggregate control first but do non-aggregate control otherwise */
6307 setup_url = control;
6308 else if ((setup_url = stream->conninfo.location) == NULL)
6311 if (src->conninfo.connection) {
6312 info = &src->conninfo;
6313 } else if (stream->conninfo.connection) {
6314 info = &stream->conninfo;
6318 if (!info->connected)
6323 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
6325 goto create_request_failed;
6328 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
6331 gst_rtspsrc_send (src, info->connection, &request, &response,
6335 /* FIXME, parse result? */
6336 gst_rtsp_message_unset (&request);
6337 gst_rtsp_message_unset (&response);
6340 /* early exit when we did aggregate control */
6346 /* close connections */
6347 GST_DEBUG_OBJECT (src, "closing connection...");
6348 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6349 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6350 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6351 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
6355 gst_rtspsrc_cleanup (src);
6357 src->state = GST_RTSP_STATE_INVALID;
6360 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
6365 create_request_failed:
6367 gchar *str = gst_rtsp_strresult (res);
6369 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6370 ("Could not create request. (%s)", str));
6376 gchar *str = gst_rtsp_strresult (res);
6378 gst_rtsp_message_unset (&request);
6379 if (res != GST_RTSP_EINTR) {
6380 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6381 ("Could not send message. (%s)", str));
6383 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
6390 GST_DEBUG_OBJECT (src,
6391 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
6396 /* RTP-Info is of the format:
6398 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
6400 * rtptime corresponds to the timestamp for the NPT time given in the header
6401 * seqbase corresponds to the next sequence number we received. This number
6402 * indicates the first seqnum after the seek and should be used to discard
6403 * packets that are from before the seek.
6406 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
6411 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
6413 infos = g_strsplit (rtpinfo, ",", 0);
6414 for (i = 0; infos[i]; i++) {
6416 GstRTSPStream *stream;
6420 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
6422 /* init values, types of seqbase and timebase are bigger than needed so we
6423 * can store -1 as uninitialized values */
6428 /* parse url, find stream for url.
6429 * parse seq and rtptime. The seq number should be configured in the rtp
6430 * depayloader or session manager to detect gaps. Same for the rtptime, it
6431 * should be used to create an initial time newsegment. */
6432 fields = g_strsplit (infos[i], ";", 0);
6433 for (j = 0; fields[j]; j++) {
6434 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
6435 /* remove leading whitespace */
6436 fields[j] = g_strchug (fields[j]);
6437 if (g_str_has_prefix (fields[j], "url=")) {
6438 /* get the url and the stream */
6440 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
6441 } else if (g_str_has_prefix (fields[j], "seq=")) {
6442 seqbase = atoi (fields[j] + 4);
6443 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
6444 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
6447 g_strfreev (fields);
6448 /* now we need to store the values for the caps of the stream */
6449 if (stream != NULL) {
6450 GST_DEBUG_OBJECT (src,
6451 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
6452 stream, seqbase, timebase);
6454 /* we have a stream, configure detected params */
6455 stream->seqbase = seqbase;
6456 stream->timebase = timebase;
6465 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
6470 interval = strtoul (rtcp, NULL, 10);
6471 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
6476 interval *= GST_MSECOND;
6478 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6479 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6481 /* already (optionally) retrieved this when configuring manager */
6482 if (stream->session) {
6483 GObject *rtpsession = stream->session;
6485 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
6487 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
6491 /* now it happens that (Xenon) server sending this may also provide bogus
6492 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
6493 * and just use RTP-Info to sync */
6495 GObjectClass *klass;
6497 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
6498 if (g_object_class_find_property (klass, "rtcp-sync")) {
6499 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
6500 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
6506 gst_rtspsrc_get_float (const gchar * dstr)
6508 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6510 /* canonicalise floating point string so we can handle float strings
6511 * in the form "24.930" or "24,930" irrespective of the current locale */
6512 g_strlcpy (s, dstr, sizeof (s));
6513 g_strdelimit (s, ",", '.');
6514 return g_ascii_strtod (s, NULL);
6518 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
6520 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6522 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
6523 g_strlcpy (val_str, "now", sizeof (val_str));
6525 if (segment->position == 0) {
6526 g_strlcpy (val_str, "0", sizeof (val_str));
6528 g_ascii_dtostr (val_str, sizeof (val_str),
6529 ((gdouble) segment->position) / GST_SECOND);
6532 return g_strdup_printf ("npt=%s-", val_str);
6536 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
6538 stream->timebase = -1;
6539 stream->seqbase = -1;
6543 stream->caps = gst_caps_make_writable (stream->caps);
6544 s = gst_caps_get_structure (stream->caps, 0);
6545 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
6549 static GstRTSPResult
6550 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
6552 GstRTSPResult res = GST_RTSP_OK;
6554 if (src->state < GST_RTSP_STATE_READY) {
6555 res = GST_RTSP_ERROR;
6556 if (src->open_error) {
6557 GST_DEBUG_OBJECT (src, "the stream was in error");
6561 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
6563 if ((res = gst_rtspsrc_open (src, async)) < 0) {
6564 GST_DEBUG_OBJECT (src, "failed to open stream");
6573 static GstRTSPResult
6574 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
6576 GstRTSPMessage request = { 0 };
6577 GstRTSPMessage response = { 0 };
6578 GstRTSPResult res = GST_RTSP_OK;
6584 GST_DEBUG_OBJECT (src, "PLAY...");
6586 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6589 if (!(src->methods & GST_RTSP_PLAY))
6592 if (src->state == GST_RTSP_STATE_PLAYING)
6595 if (!src->conninfo.connection || !src->conninfo.connected)
6598 /* send some dummy packets before we activate the receive in the
6600 gst_rtspsrc_send_dummy_packets (src);
6602 /* require new SR packets */
6604 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
6606 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
6608 /* construct a control url */
6610 control = src->control;
6612 control = src->conninfo.url_str;
6614 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6615 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6617 GstRTSPConnection *conn;
6619 /* try aggregate control first but do non-aggregate control otherwise */
6621 setup_url = control;
6622 else if ((setup_url = stream->conninfo.location) == NULL)
6625 if (src->conninfo.connection) {
6626 conn = src->conninfo.connection;
6627 } else if (stream->conninfo.connection) {
6628 conn = stream->conninfo.connection;
6634 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
6636 goto create_request_failed;
6638 if (src->need_range) {
6639 hval = gen_range_header (src, segment);
6641 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
6643 /* store the newsegment event so it can be sent from the streaming thread. */
6644 if (src->start_segment)
6645 gst_event_unref (src->start_segment);
6646 src->start_segment = gst_event_new_segment (&src->segment);
6649 if (segment->rate != 1.0) {
6650 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
6652 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
6654 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
6656 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
6660 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
6662 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6665 /* seek may have silently failed as it is not supported */
6666 if (!(src->methods & GST_RTSP_PLAY)) {
6667 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
6668 /* obviously it is supported as we made it here */
6669 src->methods |= GST_RTSP_PLAY;
6670 src->seekable = FALSE;
6671 /* but there is nothing to parse in the response,
6672 * so convey we have no idea and not to expect anything particular */
6673 clear_rtp_base (src, stream);
6677 /* need to do for all streams */
6678 for (run = src->streams; run; run = g_list_next (run))
6679 clear_rtp_base (src, (GstRTSPStream *) run->data);
6681 /* NOTE the above also disables npt based eos detection */
6682 /* and below forces position to 0,
6683 * which is visible feedback we lost the plot */
6684 segment->start = segment->position = src->last_pos;
6687 gst_rtsp_message_unset (&request);
6689 /* parse RTP npt field. This is the current position in the stream (Normal
6690 * Play Time) and should be put in the NEWSEGMENT position field. */
6691 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
6693 gst_rtspsrc_parse_range (src, hval, segment);
6695 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
6696 segment->rate = 1.0;
6698 /* parse Speed header. This is the intended playback rate of the stream
6699 * and should be put in the NEWSEGMENT rate field. */
6700 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
6701 0) == GST_RTSP_OK) {
6702 segment->rate = gst_rtspsrc_get_float (hval);
6703 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
6704 &hval, 0) == GST_RTSP_OK) {
6705 segment->rate = gst_rtspsrc_get_float (hval);
6708 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
6709 * for the RTP packets. If this is not present, we assume all starts from 0...
6710 * This is info for the RTP session manager that we pass to it in caps. */
6712 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
6713 &hval, hval_idx++) == GST_RTSP_OK)
6714 gst_rtspsrc_parse_rtpinfo (src, hval);
6716 /* some servers indicate RTCP parameters in PLAY response,
6717 * rather than properly in SDP */
6718 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
6719 &hval, 0) == GST_RTSP_OK)
6720 gst_rtspsrc_handle_rtcp_interval (src, hval);
6722 gst_rtsp_message_unset (&response);
6724 /* early exit when we did aggregate control */
6728 /* configure the caps of the streams after we parsed all headers. Only reset
6729 * the manager object when we set a new Range header (we did a seek) */
6730 gst_rtspsrc_configure_caps (src, segment, src->need_range);
6732 /* set again when needed */
6733 src->need_range = FALSE;
6735 src->running = TRUE;
6736 src->base_time = -1;
6737 src->state = GST_RTSP_STATE_PLAYING;
6740 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
6741 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6742 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6743 stream->discont = TRUE;
6748 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
6755 GST_DEBUG_OBJECT (src, "failed to open stream");
6760 GST_DEBUG_OBJECT (src, "PLAY is not supported");
6765 GST_DEBUG_OBJECT (src, "we were already PLAYING");
6768 create_request_failed:
6770 gchar *str = gst_rtsp_strresult (res);
6772 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6773 ("Could not create request. (%s)", str));
6779 gchar *str = gst_rtsp_strresult (res);
6781 gst_rtsp_message_unset (&request);
6782 if (res != GST_RTSP_EINTR) {
6783 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6784 ("Could not send message. (%s)", str));
6786 GST_WARNING_OBJECT (src, "PLAY interrupted");
6793 static GstRTSPResult
6794 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
6796 GstRTSPResult res = GST_RTSP_OK;
6797 GstRTSPMessage request = { 0 };
6798 GstRTSPMessage response = { 0 };
6802 GST_DEBUG_OBJECT (src, "PAUSE...");
6804 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6807 if (!(src->methods & GST_RTSP_PAUSE))
6810 if (src->state == GST_RTSP_STATE_READY)
6813 if (!src->conninfo.connection || !src->conninfo.connected)
6816 /* construct a control url */
6818 control = src->control;
6820 control = src->conninfo.url_str;
6822 /* loop over the streams. We might exit the loop early when we could do an
6823 * aggregate control */
6824 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6825 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6826 GstRTSPConnection *conn;
6829 /* try aggregate control first but do non-aggregate control otherwise */
6831 setup_url = control;
6832 else if ((setup_url = stream->conninfo.location) == NULL)
6835 if (src->conninfo.connection) {
6836 conn = src->conninfo.connection;
6837 } else if (stream->conninfo.connection) {
6838 conn = stream->conninfo.connection;
6844 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
6845 ("Sending PAUSE request"));
6848 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
6850 goto create_request_failed;
6852 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6855 gst_rtsp_message_unset (&request);
6856 gst_rtsp_message_unset (&response);
6858 /* exit early when we did agregate control */
6863 /* change element states now */
6864 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
6867 src->state = GST_RTSP_STATE_READY;
6871 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
6878 GST_DEBUG_OBJECT (src, "failed to open stream");
6883 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
6888 GST_DEBUG_OBJECT (src, "we were already PAUSED");
6891 create_request_failed:
6893 gchar *str = gst_rtsp_strresult (res);
6895 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6896 ("Could not create request. (%s)", str));
6902 gchar *str = gst_rtsp_strresult (res);
6904 gst_rtsp_message_unset (&request);
6905 if (res != GST_RTSP_EINTR) {
6906 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6907 ("Could not send message. (%s)", str));
6909 GST_WARNING_OBJECT (src, "PAUSE interrupted");
6917 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
6919 GstRTSPSrc *rtspsrc;
6921 rtspsrc = GST_RTSPSRC (bin);
6923 switch (GST_MESSAGE_TYPE (message)) {
6924 case GST_MESSAGE_EOS:
6925 gst_message_unref (message);
6927 case GST_MESSAGE_ELEMENT:
6929 const GstStructure *s = gst_message_get_structure (message);
6931 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
6932 gboolean ignore_timeout;
6934 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
6936 GST_OBJECT_LOCK (rtspsrc);
6937 ignore_timeout = rtspsrc->ignore_timeout;
6938 rtspsrc->ignore_timeout = TRUE;
6939 GST_OBJECT_UNLOCK (rtspsrc);
6941 /* we only act on the first udp timeout message, others are irrelevant
6942 * and can be ignored. */
6943 if (!ignore_timeout)
6944 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
6946 gst_message_unref (message);
6949 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6952 case GST_MESSAGE_ERROR:
6955 GstRTSPStream *stream;
6958 udpsrc = GST_MESSAGE_SRC (message);
6960 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
6961 GST_ELEMENT_NAME (udpsrc));
6963 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
6967 /* we ignore the RTCP udpsrc */
6968 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
6971 /* if we get error messages from the udp sources, that's not a problem as
6972 * long as not all of them error out. We also don't really know what the
6973 * problem is, the message does not give enough detail... */
6974 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
6975 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
6976 if (ret != GST_FLOW_OK)
6980 gst_message_unref (message);
6984 /* fatal but not our message, forward */
6985 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6990 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6996 /* the thread where everything happens */
6998 gst_rtspsrc_thread (GstRTSPSrc * src)
7002 GST_OBJECT_LOCK (src);
7003 cmd = src->pending_cmd;
7004 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7005 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7006 src->pending_cmd = CMD_LOOP;
7008 src->pending_cmd = CMD_WAIT;
7009 GST_DEBUG_OBJECT (src, "got command %d", cmd);
7011 /* we got the message command, so ensure communication is possible again */
7012 gst_rtspsrc_connection_flush (src, FALSE);
7014 src->busy_cmd = cmd;
7015 GST_OBJECT_UNLOCK (src);
7019 gst_rtspsrc_open (src, TRUE);
7022 gst_rtspsrc_play (src, &src->segment, TRUE);
7025 gst_rtspsrc_pause (src, TRUE);
7028 gst_rtspsrc_close (src, TRUE, FALSE);
7031 gst_rtspsrc_loop (src);
7034 gst_rtspsrc_reconnect (src, FALSE);
7040 GST_OBJECT_LOCK (src);
7041 /* and go back to sleep */
7042 if (src->pending_cmd == CMD_WAIT) {
7044 gst_task_pause (src->task);
7047 src->busy_cmd = CMD_WAIT;
7048 GST_OBJECT_UNLOCK (src);
7052 gst_rtspsrc_start (GstRTSPSrc * src)
7054 GST_DEBUG_OBJECT (src, "starting");
7056 GST_OBJECT_LOCK (src);
7058 src->pending_cmd = CMD_WAIT;
7060 if (src->task == NULL) {
7061 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7062 if (src->task == NULL)
7065 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7067 GST_OBJECT_UNLOCK (src);
7074 GST_ERROR_OBJECT (src, "failed to create task");
7080 gst_rtspsrc_stop (GstRTSPSrc * src)
7084 GST_DEBUG_OBJECT (src, "stopping");
7086 /* also cancels pending task */
7087 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7089 GST_OBJECT_LOCK (src);
7090 if ((task = src->task)) {
7092 GST_OBJECT_UNLOCK (src);
7094 gst_task_stop (task);
7096 /* make sure it is not running */
7097 GST_RTSP_STREAM_LOCK (src);
7098 GST_RTSP_STREAM_UNLOCK (src);
7100 /* now wait for the task to finish */
7101 gst_task_join (task);
7103 /* and free the task */
7104 gst_object_unref (GST_OBJECT (task));
7106 GST_OBJECT_LOCK (src);
7108 GST_OBJECT_UNLOCK (src);
7110 /* ensure synchronously all is closed and clean */
7111 gst_rtspsrc_close (src, FALSE, TRUE);
7116 static GstStateChangeReturn
7117 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
7119 GstRTSPSrc *rtspsrc;
7120 GstStateChangeReturn ret;
7122 rtspsrc = GST_RTSPSRC (element);
7124 switch (transition) {
7125 case GST_STATE_CHANGE_NULL_TO_READY:
7126 if (!gst_rtspsrc_start (rtspsrc))
7129 case GST_STATE_CHANGE_READY_TO_PAUSED:
7130 /* init some state */
7131 rtspsrc->cur_protocols = rtspsrc->protocols;
7132 /* first attempt, don't ignore timeouts */
7133 rtspsrc->ignore_timeout = FALSE;
7134 rtspsrc->open_error = FALSE;
7135 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
7137 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7138 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7139 /* unblock the tcp tasks and make the loop waiting */
7140 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
7141 /* make sure it is waiting before we send PAUSE or PLAY below */
7142 GST_RTSP_STREAM_LOCK (rtspsrc);
7143 GST_RTSP_STREAM_UNLOCK (rtspsrc);
7146 case GST_STATE_CHANGE_PAUSED_TO_READY:
7152 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
7153 if (ret == GST_STATE_CHANGE_FAILURE)
7156 switch (transition) {
7157 case GST_STATE_CHANGE_NULL_TO_READY:
7158 ret = GST_STATE_CHANGE_SUCCESS;
7160 case GST_STATE_CHANGE_READY_TO_PAUSED:
7161 ret = GST_STATE_CHANGE_NO_PREROLL;
7163 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7164 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
7165 ret = GST_STATE_CHANGE_SUCCESS;
7167 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7168 /* send pause request and keep the idle task around */
7169 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
7170 ret = GST_STATE_CHANGE_NO_PREROLL;
7172 case GST_STATE_CHANGE_PAUSED_TO_READY:
7173 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
7174 ret = GST_STATE_CHANGE_SUCCESS;
7176 case GST_STATE_CHANGE_READY_TO_NULL:
7177 gst_rtspsrc_stop (rtspsrc);
7178 ret = GST_STATE_CHANGE_SUCCESS;
7189 GST_DEBUG_OBJECT (rtspsrc, "start failed");
7190 return GST_STATE_CHANGE_FAILURE;
7195 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
7198 GstRTSPSrc *rtspsrc;
7200 rtspsrc = GST_RTSPSRC (element);
7202 if (GST_EVENT_IS_DOWNSTREAM (event)) {
7203 res = gst_rtspsrc_push_event (rtspsrc, event);
7205 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
7212 /*** GSTURIHANDLER INTERFACE *************************************************/
7215 gst_rtspsrc_uri_get_type (GType type)
7220 static const gchar *const *
7221 gst_rtspsrc_uri_get_protocols (GType type)
7223 static const gchar *protocols[] =
7224 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
7225 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
7232 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
7234 GstRTSPSrc *src = GST_RTSPSRC (handler);
7236 /* FIXME: make thread-safe */
7237 return g_strdup (src->conninfo.location);
7241 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
7246 GstRTSPUrl *newurl = NULL;
7247 GstSDPMessage *sdp = NULL;
7249 src = GST_RTSPSRC (handler);
7251 /* same URI, we're fine */
7252 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
7255 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
7256 if ((res = gst_sdp_message_new (&sdp) < 0))
7259 GST_DEBUG_OBJECT (src, "parsing SDP message");
7260 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
7264 GST_DEBUG_OBJECT (src, "parsing URI");
7265 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
7269 /* if worked, free previous and store new url object along with the original
7271 GST_DEBUG_OBJECT (src, "configuring URI");
7272 g_free (src->conninfo.location);
7273 src->conninfo.location = g_strdup (uri);
7274 gst_rtsp_url_free (src->conninfo.url);
7275 src->conninfo.url = newurl;
7276 g_free (src->conninfo.url_str);
7278 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
7280 src->conninfo.url_str = NULL;
7283 gst_sdp_message_free (src->sdp);
7285 src->from_sdp = sdp != NULL;
7287 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
7288 GST_DEBUG_OBJECT (src, "request uri is: %s",
7289 GST_STR_NULL (src->conninfo.url_str));
7296 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
7301 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
7302 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7303 "Could not create SDP");
7308 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
7309 GST_STR_NULL (uri));
7310 gst_sdp_message_free (sdp);
7311 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7317 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
7318 GST_STR_NULL (uri), res);
7319 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7320 "Invalid RTSP URI");
7326 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
7328 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
7330 iface->get_type = gst_rtspsrc_uri_get_type;
7331 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
7332 iface->get_uri = gst_rtspsrc_uri_get_uri;
7333 iface->set_uri = gst_rtspsrc_uri_set_uri;