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
84 #endif /* HAVE_UNISTD_H */
90 #include <gst/net/gstnet.h>
91 #include <gst/sdp/gstsdpmessage.h>
92 #include <gst/sdp/gstmikey.h>
93 #include <gst/rtp/gstrtppayloads.h>
95 #include "gst/gst-i18n-plugin.h"
97 #include "gstrtspsrc.h"
99 GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
100 #define GST_CAT_DEFAULT (rtspsrc_debug)
102 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream_%u",
105 GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
107 /* templates used internally */
108 static GstStaticPadTemplate anysrctemplate =
109 GST_STATIC_PAD_TEMPLATE ("internalsrc_%u",
112 GST_STATIC_CAPS_ANY);
114 static GstStaticPadTemplate anysinktemplate =
115 GST_STATIC_PAD_TEMPLATE ("internalsink_%u",
118 GST_STATIC_CAPS_ANY);
122 SIGNAL_HANDLE_REQUEST,
124 SIGNAL_SELECT_STREAM,
126 SIGNAL_REQUEST_RTCP_KEY,
130 enum _GstRtspSrcRtcpSyncMode
137 enum _GstRtspSrcBufferMode
146 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
148 gst_rtsp_src_buffer_mode_get_type (void)
150 static GType buffer_mode_type = 0;
151 static const GEnumValue buffer_modes[] = {
152 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
153 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
154 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
155 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
156 {BUFFER_MODE_SYNCED, "Synchronized sender and receiver clocks", "synced"},
160 if (!buffer_mode_type) {
162 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
164 return buffer_mode_type;
167 #define AES_128_KEY_LEN 16
168 #define AES_256_KEY_LEN 32
170 #define HMAC_32_KEY_LEN 4
171 #define HMAC_80_KEY_LEN 10
173 #define DEFAULT_LOCATION NULL
174 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
175 #define DEFAULT_DEBUG FALSE
176 #define DEFAULT_RETRY 20
177 #define DEFAULT_TIMEOUT 5000000
178 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
179 #define DEFAULT_TCP_TIMEOUT 20000000
180 #define DEFAULT_LATENCY_MS 2000
181 #define DEFAULT_DROP_ON_LATENCY FALSE
182 #define DEFAULT_CONNECTION_SPEED 0
183 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
184 #define DEFAULT_DO_RTCP TRUE
185 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
186 #define DEFAULT_PROXY NULL
187 #define DEFAULT_RTP_BLOCKSIZE 0
188 #define DEFAULT_USER_ID NULL
189 #define DEFAULT_USER_PW NULL
190 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
191 #define DEFAULT_PORT_RANGE NULL
192 #define DEFAULT_SHORT_HEADER FALSE
193 #define DEFAULT_PROBATION 2
194 #define DEFAULT_UDP_RECONNECT TRUE
195 #define DEFAULT_MULTICAST_IFACE NULL
196 #define DEFAULT_NTP_SYNC FALSE
197 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
198 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
199 #define DEFAULT_TLS_DATABASE NULL
211 PROP_DROP_ON_LATENCY,
212 PROP_CONNECTION_SPEED,
215 PROP_DO_RTSP_KEEP_ALIVE,
224 PROP_UDP_BUFFER_SIZE,
228 PROP_MULTICAST_IFACE,
230 PROP_USE_PIPELINE_CLOCK,
232 PROP_TLS_VALIDATION_FLAGS,
237 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
239 gst_rtsp_nat_method_get_type (void)
241 static GType rtsp_nat_method_type = 0;
242 static const GEnumValue rtsp_nat_method[] = {
243 {GST_RTSP_NAT_NONE, "None", "none"},
244 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
248 if (!rtsp_nat_method_type) {
249 rtsp_nat_method_type =
250 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
252 return rtsp_nat_method_type;
255 static void gst_rtspsrc_finalize (GObject * object);
257 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
258 const GValue * value, GParamSpec * pspec);
259 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
260 GValue * value, GParamSpec * pspec);
262 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
264 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
265 gpointer iface_data);
267 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
270 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
271 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
273 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
275 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
276 GstStateChange transition);
277 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
278 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
280 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
281 GstRTSPMessage * response);
283 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
285 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
286 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
288 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
289 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
291 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
292 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
293 gboolean only_close);
295 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
296 const gchar * uri, GError ** error);
297 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
299 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
300 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
301 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
302 GstRTSPStream * stream, GstEvent * event);
303 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
304 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
312 /* commands we send to out loop to notify it of events */
313 #define CMD_OPEN (1 << 0)
314 #define CMD_PLAY (1 << 1)
315 #define CMD_PAUSE (1 << 2)
316 #define CMD_CLOSE (1 << 3)
317 #define CMD_WAIT (1 << 4)
318 #define CMD_RECONNECT (1 << 5)
319 #define CMD_LOOP (1 << 6)
321 /* mask for all commands */
322 #define CMD_ALL ((CMD_LOOP << 1) - 1)
324 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
326 gchar *__txt = _gst_element_error_printf text; \
327 gst_element_post_message (GST_ELEMENT_CAST (el), \
328 gst_message_new_progress (GST_OBJECT_CAST (el), \
329 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
333 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
335 #define gst_rtspsrc_parent_class parent_class
336 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
337 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
340 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
342 GST_DEBUG_OBJECT (src, "default handler");
347 select_stream_accum (GSignalInvocationHint * ihint,
348 GValue * return_accu, const GValue * handler_return, gpointer data)
352 myboolean = g_value_get_boolean (handler_return);
353 GST_DEBUG ("accum %d", myboolean);
354 g_value_set_boolean (return_accu, myboolean);
356 /* stop emission if FALSE */
361 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
363 GObjectClass *gobject_class;
364 GstElementClass *gstelement_class;
365 GstBinClass *gstbin_class;
367 gobject_class = (GObjectClass *) klass;
368 gstelement_class = (GstElementClass *) klass;
369 gstbin_class = (GstBinClass *) klass;
371 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
373 gobject_class->set_property = gst_rtspsrc_set_property;
374 gobject_class->get_property = gst_rtspsrc_get_property;
376 gobject_class->finalize = gst_rtspsrc_finalize;
378 g_object_class_install_property (gobject_class, PROP_LOCATION,
379 g_param_spec_string ("location", "RTSP Location",
380 "Location of the RTSP url to read",
381 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
383 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
384 g_param_spec_flags ("protocols", "Protocols",
385 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
386 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
388 g_object_class_install_property (gobject_class, PROP_DEBUG,
389 g_param_spec_boolean ("debug", "Debug",
390 "Dump request and response messages to stdout",
391 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
393 g_object_class_install_property (gobject_class, PROP_RETRY,
394 g_param_spec_uint ("retry", "Retry",
395 "Max number of retries when allocating RTP ports.",
396 0, G_MAXUINT16, DEFAULT_RETRY,
397 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
399 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
400 g_param_spec_uint64 ("timeout", "Timeout",
401 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
402 0, G_MAXUINT64, DEFAULT_TIMEOUT,
403 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
405 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
406 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
407 "Fail after timeout microseconds on TCP connections (0 = disabled)",
408 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
409 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
411 g_object_class_install_property (gobject_class, PROP_LATENCY,
412 g_param_spec_uint ("latency", "Buffer latency in ms",
413 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
414 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
416 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
417 g_param_spec_boolean ("drop-on-latency",
418 "Drop buffers when maximum latency is reached",
419 "Tells the jitterbuffer to never exceed the given latency in size",
420 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
422 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
423 g_param_spec_uint64 ("connection-speed", "Connection Speed",
424 "Network connection speed in kbps (0 = unknown)",
425 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
426 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
428 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
429 g_param_spec_enum ("nat-method", "NAT Method",
430 "Method to use for traversing firewalls and NAT",
431 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
432 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
435 * GstRTSPSrc:do-rtcp:
437 * Enable RTCP support. Some old server don't like RTCP and then this property
438 * needs to be set to FALSE.
440 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
441 g_param_spec_boolean ("do-rtcp", "Do RTCP",
442 "Send RTCP packets, disable for old incompatible server.",
443 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
446 * GstRTSPSrc:do-rtsp-keep-alive:
448 * Enable RTSP keep alive support. Some old server don't like RTSP
449 * keep alive and then this property needs to be set to FALSE.
451 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
452 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
453 "Send RTSP keep alive packets, disable for old incompatible server.",
454 DEFAULT_DO_RTSP_KEEP_ALIVE,
455 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
460 * Set the proxy parameters. This has to be a string of the format
461 * [http://][user:passwd@]host[:port].
463 g_object_class_install_property (gobject_class, PROP_PROXY,
464 g_param_spec_string ("proxy", "Proxy",
465 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
466 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
468 * GstRTSPSrc:proxy-id:
470 * Sets the proxy URI user id for authentication. If the URI set via the
471 * "proxy" property contains a user-id already, that will take precedence.
475 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
476 g_param_spec_string ("proxy-id", "proxy-id",
477 "HTTP proxy URI user id for authentication", "",
478 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
480 * GstRTSPSrc:proxy-pw:
482 * Sets the proxy URI password for authentication. If the URI set via the
483 * "proxy" property contains a password already, that will take precedence.
487 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
488 g_param_spec_string ("proxy-pw", "proxy-pw",
489 "HTTP proxy URI user password for authentication", "",
490 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
493 * GstRTSPSrc:rtp-blocksize:
495 * RTP package size to suggest to server.
497 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
498 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
499 "RTP package size to suggest to server (0 = disabled)",
500 0, 65536, DEFAULT_RTP_BLOCKSIZE,
501 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
503 g_object_class_install_property (gobject_class,
505 g_param_spec_string ("user-id", "user-id",
506 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
507 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
508 g_object_class_install_property (gobject_class, PROP_USER_PW,
509 g_param_spec_string ("user-pw", "user-pw",
510 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
511 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
514 * GstRTSPSrc:buffer-mode:
516 * Control the buffering and timestamping mode used by the jitterbuffer.
518 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
519 g_param_spec_enum ("buffer-mode", "Buffer Mode",
520 "Control the buffering algorithm in use",
521 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
522 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
525 * GstRTSPSrc:port-range:
527 * Configure the client port numbers that can be used to recieve RTP and
530 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
531 g_param_spec_string ("port-range", "Port range",
532 "Client port range that can be used to receive RTP and RTCP data, "
533 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
534 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
537 * GstRTSPSrc:udp-buffer-size:
539 * Size of the kernel UDP receive buffer in bytes.
541 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
542 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
543 "Size of the kernel UDP receive buffer in bytes, 0=default",
544 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
545 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
548 * GstRTSPSrc:short-header:
550 * 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::tls-database:
606 * TLS database with anchor certificate authorities used to validate
607 * the server certificate.
611 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
612 g_param_spec_object ("tls-database", "TLS database",
613 "TLS database with anchor certificate authorities used to validate the server certificate",
614 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
617 * GstRTSPSrc::handle-request:
618 * @rtspsrc: a #GstRTSPSrc
619 * @request: a #GstRTSPMessage
620 * @response: a #GstRTSPMessage
622 * Handle a server request in @request and prepare @response.
624 * This signal is called from the streaming thread, you should therefore not
625 * do any state changes on @rtspsrc because this might deadlock. If you want
626 * to modify the state as a result of this signal, post a
627 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
632 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
633 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
634 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
635 G_TYPE_POINTER, G_TYPE_POINTER);
638 * GstRTSPSrc::on-sdp:
639 * @rtspsrc: a #GstRTSPSrc
640 * @sdp: a #GstSDPMessage
642 * Emited when the client has retrieved the SDP and before it configures the
643 * streams in the SDP. @sdp can be inspected and modified.
645 * This signal is called from the streaming thread, you should therefore not
646 * do any state changes on @rtspsrc because this might deadlock. If you want
647 * to modify the state as a result of this signal, post a
648 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
653 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
654 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
655 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
656 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
659 * GstRTSPSrc::select-stream:
660 * @rtspsrc: a #GstRTSPSrc
661 * @num: the stream number
662 * @caps: the stream caps
664 * Emited before the client decides to configure the stream @num with
667 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
672 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
673 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
674 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
675 (GCallback) default_select_stream, select_stream_accum, NULL,
676 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
679 * GstRTSPSrc::new-manager:
680 * @rtspsrc: a #GstRTSPSrc
681 * @manager: a #GstElement
683 * Emited after a new manager (like rtpbin) was created and the default
684 * properties were configured.
688 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
689 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
690 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
691 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
694 * GstRTSPSrc::request-rtcp-key:
695 * @rtspsrc: a #GstRTSPSrc
696 * @num: the stream number
698 * Signal emited to get the crypto parameters relevant to the RTCP
699 * stream. User should provide the key and the RTCP encryption ciphers
700 * and authentication, and return them wrapped in a GstCaps.
704 gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY] =
705 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
706 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
708 gstelement_class->send_event = gst_rtspsrc_send_event;
709 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
710 gstelement_class->change_state = gst_rtspsrc_change_state;
712 gst_element_class_add_pad_template (gstelement_class,
713 gst_static_pad_template_get (&rtptemplate));
715 gst_element_class_set_static_metadata (gstelement_class,
716 "RTSP packet receiver", "Source/Network",
717 "Receive data over the network via RTSP (RFC 2326)",
718 "Wim Taymans <wim@fluendo.com>, "
719 "Thijs Vermeir <thijs.vermeir@barco.com>, "
720 "Lutz Mueller <lutz@topfrose.de>");
722 gstbin_class->handle_message = gst_rtspsrc_handle_message;
724 gst_rtsp_ext_list_init ();
728 gst_rtspsrc_init (GstRTSPSrc * src)
730 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
731 src->protocols = DEFAULT_PROTOCOLS;
732 src->debug = DEFAULT_DEBUG;
733 src->retry = DEFAULT_RETRY;
734 src->udp_timeout = DEFAULT_TIMEOUT;
735 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
736 src->latency = DEFAULT_LATENCY_MS;
737 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
738 src->connection_speed = DEFAULT_CONNECTION_SPEED;
739 src->nat_method = DEFAULT_NAT_METHOD;
740 src->do_rtcp = DEFAULT_DO_RTCP;
741 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
742 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
743 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
744 src->user_id = g_strdup (DEFAULT_USER_ID);
745 src->user_pw = g_strdup (DEFAULT_USER_PW);
746 src->buffer_mode = DEFAULT_BUFFER_MODE;
747 src->client_port_range.min = 0;
748 src->client_port_range.max = 0;
749 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
750 src->short_header = DEFAULT_SHORT_HEADER;
751 src->probation = DEFAULT_PROBATION;
752 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
753 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
754 src->ntp_sync = DEFAULT_NTP_SYNC;
755 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
757 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
758 src->tls_database = DEFAULT_TLS_DATABASE;
760 /* get a list of all extensions */
761 src->extensions = gst_rtsp_ext_list_get ();
763 /* connect to send signal */
764 gst_rtsp_ext_list_connect (src->extensions, "send",
765 (GCallback) gst_rtspsrc_send_cb, src);
767 /* protects the streaming thread in interleaved mode or the polling
768 * thread in UDP mode. */
769 g_rec_mutex_init (&src->stream_rec_lock);
771 /* protects our state changes from multiple invocations */
772 g_rec_mutex_init (&src->state_rec_lock);
774 src->state = GST_RTSP_STATE_INVALID;
776 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
780 gst_rtspsrc_finalize (GObject * object)
784 rtspsrc = GST_RTSPSRC (object);
786 gst_rtsp_ext_list_free (rtspsrc->extensions);
787 g_free (rtspsrc->conninfo.location);
788 gst_rtsp_url_free (rtspsrc->conninfo.url);
789 g_free (rtspsrc->conninfo.url_str);
790 g_free (rtspsrc->user_id);
791 g_free (rtspsrc->user_pw);
792 g_free (rtspsrc->multi_iface);
795 gst_sdp_message_free (rtspsrc->sdp);
798 if (rtspsrc->provided_clock)
799 gst_object_unref (rtspsrc->provided_clock);
802 gst_structure_free (rtspsrc->sdes);
804 if (rtspsrc->tls_database)
805 g_object_unref (rtspsrc->tls_database);
808 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
809 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
811 G_OBJECT_CLASS (parent_class)->finalize (object);
815 gst_rtspsrc_provide_clock (GstElement * element)
817 GstRTSPSrc *src = GST_RTSPSRC (element);
820 if ((clock = src->provided_clock) != NULL)
821 gst_object_ref (clock);
826 /* a proxy string of the format [user:passwd@]host[:port] */
828 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
832 g_free (rtsp->proxy_user);
833 rtsp->proxy_user = NULL;
834 g_free (rtsp->proxy_passwd);
835 rtsp->proxy_passwd = NULL;
836 g_free (rtsp->proxy_host);
837 rtsp->proxy_host = NULL;
838 rtsp->proxy_port = 0;
845 /* we allow http:// in front but ignore it */
846 if (g_str_has_prefix (p, "http://"))
849 at = strchr (p, '@');
851 /* look for user:passwd */
852 col = strchr (proxy, ':');
853 if (col == NULL || col > at)
856 rtsp->proxy_user = g_strndup (p, col - p);
858 rtsp->proxy_passwd = g_strndup (col, at - col);
863 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
864 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
865 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
866 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
867 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
868 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
869 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
872 col = strchr (p, ':');
875 /* everything before the colon is the hostname */
876 rtsp->proxy_host = g_strndup (p, col - p);
878 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
880 rtsp->proxy_host = g_strdup (p);
881 rtsp->proxy_port = 8080;
887 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
889 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
890 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
893 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
895 rtspsrc->ptcp_timeout = NULL;
899 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
904 rtspsrc = GST_RTSPSRC (object);
908 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
909 g_value_get_string (value), NULL);
912 rtspsrc->protocols = g_value_get_flags (value);
915 rtspsrc->debug = g_value_get_boolean (value);
918 rtspsrc->retry = g_value_get_uint (value);
921 rtspsrc->udp_timeout = g_value_get_uint64 (value);
923 case PROP_TCP_TIMEOUT:
924 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
927 rtspsrc->latency = g_value_get_uint (value);
929 case PROP_DROP_ON_LATENCY:
930 rtspsrc->drop_on_latency = g_value_get_boolean (value);
932 case PROP_CONNECTION_SPEED:
933 rtspsrc->connection_speed = g_value_get_uint64 (value);
935 case PROP_NAT_METHOD:
936 rtspsrc->nat_method = g_value_get_enum (value);
939 rtspsrc->do_rtcp = g_value_get_boolean (value);
941 case PROP_DO_RTSP_KEEP_ALIVE:
942 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
945 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
948 if (rtspsrc->prop_proxy_id)
949 g_free (rtspsrc->prop_proxy_id);
950 rtspsrc->prop_proxy_id = g_value_dup_string (value);
953 if (rtspsrc->prop_proxy_pw)
954 g_free (rtspsrc->prop_proxy_pw);
955 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
957 case PROP_RTP_BLOCKSIZE:
958 rtspsrc->rtp_blocksize = g_value_get_uint (value);
961 if (rtspsrc->user_id)
962 g_free (rtspsrc->user_id);
963 rtspsrc->user_id = g_value_dup_string (value);
966 if (rtspsrc->user_pw)
967 g_free (rtspsrc->user_pw);
968 rtspsrc->user_pw = g_value_dup_string (value);
970 case PROP_BUFFER_MODE:
971 rtspsrc->buffer_mode = g_value_get_enum (value);
973 case PROP_PORT_RANGE:
977 str = g_value_get_string (value);
979 sscanf (str, "%u-%u",
980 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
982 rtspsrc->client_port_range.min = 0;
983 rtspsrc->client_port_range.max = 0;
987 case PROP_UDP_BUFFER_SIZE:
988 rtspsrc->udp_buffer_size = g_value_get_int (value);
990 case PROP_SHORT_HEADER:
991 rtspsrc->short_header = g_value_get_boolean (value);
994 rtspsrc->probation = g_value_get_uint (value);
996 case PROP_UDP_RECONNECT:
997 rtspsrc->udp_reconnect = g_value_get_boolean (value);
999 case PROP_MULTICAST_IFACE:
1000 g_free (rtspsrc->multi_iface);
1002 if (g_value_get_string (value) == NULL)
1003 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1005 rtspsrc->multi_iface = g_value_dup_string (value);
1008 rtspsrc->ntp_sync = g_value_get_boolean (value);
1010 case PROP_USE_PIPELINE_CLOCK:
1011 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
1014 rtspsrc->sdes = g_value_dup_boxed (value);
1016 case PROP_TLS_VALIDATION_FLAGS:
1017 rtspsrc->tls_validation_flags = g_value_get_flags (value);
1019 case PROP_TLS_DATABASE:
1020 g_clear_object (&rtspsrc->tls_database);
1021 rtspsrc->tls_database = g_value_dup_object (value);
1024 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1030 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1033 GstRTSPSrc *rtspsrc;
1035 rtspsrc = GST_RTSPSRC (object);
1039 g_value_set_string (value, rtspsrc->conninfo.location);
1041 case PROP_PROTOCOLS:
1042 g_value_set_flags (value, rtspsrc->protocols);
1045 g_value_set_boolean (value, rtspsrc->debug);
1048 g_value_set_uint (value, rtspsrc->retry);
1051 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1053 case PROP_TCP_TIMEOUT:
1057 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1058 rtspsrc->tcp_timeout.tv_usec;
1059 g_value_set_uint64 (value, timeout);
1063 g_value_set_uint (value, rtspsrc->latency);
1065 case PROP_DROP_ON_LATENCY:
1066 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1068 case PROP_CONNECTION_SPEED:
1069 g_value_set_uint64 (value, rtspsrc->connection_speed);
1071 case PROP_NAT_METHOD:
1072 g_value_set_enum (value, rtspsrc->nat_method);
1075 g_value_set_boolean (value, rtspsrc->do_rtcp);
1077 case PROP_DO_RTSP_KEEP_ALIVE:
1078 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1084 if (rtspsrc->proxy_host) {
1086 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1090 g_value_take_string (value, str);
1094 g_value_set_string (value, rtspsrc->prop_proxy_id);
1097 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1099 case PROP_RTP_BLOCKSIZE:
1100 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1103 g_value_set_string (value, rtspsrc->user_id);
1106 g_value_set_string (value, rtspsrc->user_pw);
1108 case PROP_BUFFER_MODE:
1109 g_value_set_enum (value, rtspsrc->buffer_mode);
1111 case PROP_PORT_RANGE:
1115 if (rtspsrc->client_port_range.min != 0) {
1116 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1117 rtspsrc->client_port_range.max);
1121 g_value_take_string (value, str);
1124 case PROP_UDP_BUFFER_SIZE:
1125 g_value_set_int (value, rtspsrc->udp_buffer_size);
1127 case PROP_SHORT_HEADER:
1128 g_value_set_boolean (value, rtspsrc->short_header);
1130 case PROP_PROBATION:
1131 g_value_set_uint (value, rtspsrc->probation);
1133 case PROP_UDP_RECONNECT:
1134 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1136 case PROP_MULTICAST_IFACE:
1137 g_value_set_string (value, rtspsrc->multi_iface);
1140 g_value_set_boolean (value, rtspsrc->ntp_sync);
1142 case PROP_USE_PIPELINE_CLOCK:
1143 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1146 g_value_set_boxed (value, rtspsrc->sdes);
1148 case PROP_TLS_VALIDATION_FLAGS:
1149 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1151 case PROP_TLS_DATABASE:
1152 g_value_set_object (value, rtspsrc->tls_database);
1155 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1161 find_stream_by_id (GstRTSPStream * stream, gint * id)
1163 if (stream->id == *id)
1170 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1172 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1179 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1181 GstElement *src = (GstElement *) a;
1183 if (stream->udpsrc[0] == src)
1185 if (stream->udpsrc[1] == src)
1192 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1194 if (stream->conninfo.location) {
1195 /* check qualified setup_url */
1196 if (!strcmp (stream->conninfo.location, (gchar *) a))
1199 if (stream->control_url) {
1200 /* check original control_url */
1201 if (!strcmp (stream->control_url, (gchar *) a))
1204 /* check if qualified setup_url ends with string */
1205 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1212 static GstRTSPStream *
1213 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1217 /* find and get stream */
1218 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1219 return (GstRTSPStream *) lstream->data;
1224 static const GstSDPBandwidth *
1225 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1226 const GstSDPMedia * media, const gchar * type)
1230 /* first look in the media specific section */
1231 len = gst_sdp_media_bandwidths_len (media);
1232 for (i = 0; i < len; i++) {
1233 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1235 if (strcmp (bw->bwtype, type) == 0)
1238 /* then look in the message specific section */
1239 len = gst_sdp_message_bandwidths_len (sdp);
1240 for (i = 0; i < len; i++) {
1241 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1243 if (strcmp (bw->bwtype, type) == 0)
1250 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1251 const GstSDPMedia * media, GstRTSPStream * stream)
1253 const GstSDPBandwidth *bw;
1255 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1256 stream->as_bandwidth = bw->bandwidth;
1258 stream->as_bandwidth = -1;
1260 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1261 stream->rr_bandwidth = bw->bandwidth;
1263 stream->rr_bandwidth = -1;
1265 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1266 stream->rs_bandwidth = bw->bandwidth;
1268 stream->rs_bandwidth = -1;
1272 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1273 const GstSDPConnection * conn)
1275 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1278 if (conn->addrtype == NULL)
1281 /* check for IPV6 */
1282 if (strcmp (conn->addrtype, "IP4") == 0)
1283 stream->is_ipv6 = FALSE;
1284 else if (strcmp (conn->addrtype, "IP6") == 0)
1285 stream->is_ipv6 = TRUE;
1290 g_free (stream->destination);
1291 stream->destination = g_strdup (conn->address);
1293 /* check for multicast */
1294 stream->is_multicast =
1295 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1297 stream->ttl = conn->ttl;
1300 /* Go over the connections for a stream.
1301 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1303 * - If we are dealing with a localhost address, we disable multicast
1306 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1307 const GstSDPMedia * media, GstRTSPStream * stream)
1309 const GstSDPConnection *conn;
1312 /* first look in the media specific section */
1313 len = gst_sdp_media_connections_len (media);
1314 for (i = 0; i < len; i++) {
1315 conn = gst_sdp_media_get_connection (media, i);
1317 gst_rtspsrc_do_stream_connection (src, stream, conn);
1319 /* then look in the message specific section */
1320 if ((conn = gst_sdp_message_get_connection (sdp))) {
1321 gst_rtspsrc_do_stream_connection (src, stream, conn);
1325 /* m=<media> <UDP port> RTP/AVP <payload>
1328 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
1329 const GstSDPMedia * media, GstRTSPStream * stream)
1335 proto = gst_sdp_media_get_proto (media);
1339 if (g_str_equal (proto, "RTP/AVP"))
1340 stream->profile = GST_RTSP_PROFILE_AVP;
1341 else if (g_str_equal (proto, "RTP/SAVP"))
1342 stream->profile = GST_RTSP_PROFILE_SAVP;
1343 else if (g_str_equal (proto, "RTP/AVPF"))
1344 stream->profile = GST_RTSP_PROFILE_AVPF;
1345 else if (g_str_equal (proto, "RTP/SAVPF"))
1346 stream->profile = GST_RTSP_PROFILE_SAVPF;
1350 len = gst_sdp_media_formats_len (media);
1351 for (i = 0; i < len; i++) {
1358 pt = atoi (gst_sdp_media_get_format (media, i));
1360 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
1363 caps = gst_rtspsrc_media_to_caps (pt, media);
1365 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
1369 /* do some tweaks */
1370 s = gst_caps_get_structure (caps, 0);
1371 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
1372 stream->is_real = (strstr (enc, "-REAL") != NULL);
1373 if (strcmp (enc, "X-ASF-PF") == 0)
1374 stream->container = TRUE;
1376 GST_DEBUG ("mapping sdp session level attributes to caps");
1377 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, caps);
1378 GST_DEBUG ("mapping sdp media level attributes to caps");
1379 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, caps);
1381 /* the first pt will be the default */
1382 if (stream->ptmap->len == 0)
1383 stream->default_pt = pt;
1387 g_array_append_val (stream->ptmap, item);
1393 GST_ERROR_OBJECT (src, "can't find proto in media");
1398 GST_ERROR_OBJECT (src, "unknown proto in media %s", proto);
1403 static const gchar *
1404 get_aggregate_control (GstRTSPSrc * src)
1409 base = src->control;
1410 else if (src->content_base)
1411 base = src->content_base;
1412 else if (src->conninfo.url_str)
1413 base = src->conninfo.url_str;
1421 clear_ptmap_item (PtMapItem * item)
1424 gst_caps_unref (item->caps);
1427 static GstRTSPStream *
1428 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1430 GstRTSPStream *stream;
1431 const gchar *control_url;
1432 const GstSDPMedia *media;
1434 /* get media, should not return NULL */
1435 media = gst_sdp_message_get_media (sdp, idx);
1439 stream = g_new0 (GstRTSPStream, 1);
1440 stream->parent = src;
1441 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1443 stream->last_ret = GST_FLOW_NOT_LINKED;
1444 stream->added = FALSE;
1445 stream->setup = FALSE;
1446 stream->skipped = FALSE;
1448 stream->eos = FALSE;
1449 stream->discont = TRUE;
1450 stream->seqbase = -1;
1451 stream->timebase = -1;
1452 stream->send_ssrc = g_random_int ();
1453 stream->profile = GST_RTSP_PROFILE_AVP;
1454 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1455 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
1457 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1458 * session manager to scale RTCP. */
1459 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1461 /* collect connection info */
1462 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1464 /* make the payload type map */
1465 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1467 /* collect port number */
1468 stream->port = gst_sdp_media_get_port (media);
1470 /* get control url to construct the setup url. The setup url is used to
1471 * configure the transport of the stream and is used to identity the stream in
1472 * the RTP-Info header field returned from PLAY. */
1473 control_url = gst_sdp_media_get_attribute_val (media, "control");
1474 if (control_url == NULL)
1475 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1477 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1478 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1479 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1480 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1482 if (control_url != NULL) {
1483 stream->control_url = g_strdup (control_url);
1484 /* Build a fully qualified url using the content_base if any or by prefixing
1485 * the original request.
1486 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1487 * likely build a URL that the server will fail to understand, this is ok,
1488 * we will fail then. */
1489 if (g_str_has_prefix (control_url, "rtsp://"))
1490 stream->conninfo.location = g_strdup (control_url);
1495 if (g_strcmp0 (control_url, "*") == 0)
1498 base = get_aggregate_control (src);
1500 /* check if the base ends or control starts with / */
1501 has_slash = g_str_has_prefix (control_url, "/");
1502 has_slash = has_slash || g_str_has_suffix (base, "/");
1504 /* concatenate the two strings, insert / when not present */
1505 stream->conninfo.location =
1506 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1509 GST_DEBUG_OBJECT (src, " setup: %s",
1510 GST_STR_NULL (stream->conninfo.location));
1512 /* we keep track of all streams */
1513 src->streams = g_list_append (src->streams, stream);
1521 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1525 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1527 g_array_free (stream->ptmap, TRUE);
1529 g_free (stream->destination);
1530 g_free (stream->control_url);
1531 g_free (stream->conninfo.location);
1533 for (i = 0; i < 2; i++) {
1534 if (stream->udpsrc[i]) {
1535 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1536 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1537 gst_object_unref (stream->udpsrc[i]);
1539 if (stream->channelpad[i])
1540 gst_object_unref (stream->channelpad[i]);
1542 if (stream->udpsink[i]) {
1543 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1544 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1545 gst_object_unref (stream->udpsink[i]);
1548 if (stream->fakesrc) {
1549 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1550 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1551 gst_object_unref (stream->fakesrc);
1553 if (stream->srcpad) {
1554 gst_pad_set_active (stream->srcpad, FALSE);
1556 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1558 if (stream->srtpenc)
1559 gst_object_unref (stream->srtpenc);
1560 if (stream->srtpdec)
1561 gst_object_unref (stream->srtpdec);
1562 if (stream->srtcpparams)
1563 gst_caps_unref (stream->srtcpparams);
1564 if (stream->rtcppad)
1565 gst_object_unref (stream->rtcppad);
1566 if (stream->session)
1567 g_object_unref (stream->session);
1572 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1576 GST_DEBUG_OBJECT (src, "cleanup");
1578 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1579 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1581 gst_rtspsrc_stream_free (src, stream);
1583 g_list_free (src->streams);
1584 src->streams = NULL;
1586 if (src->manager_sig_id) {
1587 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1588 src->manager_sig_id = 0;
1590 gst_element_set_state (src->manager, GST_STATE_NULL);
1591 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1592 src->manager = NULL;
1595 gst_structure_free (src->props);
1598 g_free (src->content_base);
1599 src->content_base = NULL;
1601 g_free (src->control);
1602 src->control = NULL;
1605 gst_rtsp_range_free (src->range);
1608 /* don't clear the SDP when it was used in the url */
1609 if (src->sdp && !src->from_sdp) {
1610 gst_sdp_message_free (src->sdp);
1613 if (src->start_segment) {
1614 gst_event_unref (src->start_segment);
1615 src->start_segment = NULL;
1617 if (src->provided_clock) {
1618 gst_object_unref (src->provided_clock);
1619 src->provided_clock = NULL;
1623 #define PARSE_INT(p, del, res) \
1626 p = strstr (p, del); \
1636 #define PARSE_STRING(p, del, res) \
1639 p = strstr (p, del); \
1651 #define SKIP_SPACES(p) \
1652 while (*p && g_ascii_isspace (*p)) \
1657 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1660 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1661 gint * rate, gchar ** params)
1665 p = (gchar *) rtpmap;
1667 PARSE_INT (p, " ", *payload);
1675 PARSE_STRING (p, "/", *name);
1676 if (*name == NULL) {
1677 GST_DEBUG ("no rate, name %s", p);
1678 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1679 * streams seem to omit the rate. */
1686 p = strstr (p, "/");
1704 parse_keymgmt (const gchar * keymgmt, GstCaps * caps)
1706 gboolean res = FALSE;
1710 GstMIKEYMessage *msg;
1711 const GstMIKEYPayload *payload;
1712 const gchar *srtp_cipher;
1713 const gchar *srtp_auth;
1715 p = (gchar *) keymgmt;
1721 PARSE_STRING (p, " ", kmpid);
1722 if (!g_str_equal (kmpid, "mikey"))
1725 data = g_base64_decode (p, &size);
1729 msg = gst_mikey_message_new_from_data (data, size, NULL, NULL);
1733 srtp_cipher = "aes-128-icm";
1734 srtp_auth = "hmac-sha1-80";
1736 /* check the Security policy if any */
1737 if ((payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, 0))) {
1738 GstMIKEYPayloadSP *p = (GstMIKEYPayloadSP *) payload;
1741 if (p->proto != GST_MIKEY_SEC_PROTO_SRTP)
1744 len = gst_mikey_payload_sp_get_n_params (payload);
1745 for (i = 0; i < len; i++) {
1746 const GstMIKEYPayloadSPParam *param =
1747 gst_mikey_payload_sp_get_param (payload, i);
1749 switch (param->type) {
1750 case GST_MIKEY_SP_SRTP_ENC_ALG:
1751 switch (param->val[0]) {
1753 srtp_cipher = "null";
1757 srtp_cipher = "aes-128-icm";
1763 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
1764 switch (param->val[0]) {
1765 case AES_128_KEY_LEN:
1766 srtp_cipher = "aes-128-icm";
1768 case AES_256_KEY_LEN:
1769 srtp_cipher = "aes-256-icm";
1775 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1776 switch (param->val[0]) {
1782 srtp_auth = "hmac-sha1-80";
1788 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
1789 switch (param->val[0]) {
1790 case HMAC_32_KEY_LEN:
1791 srtp_auth = "hmac-sha1-32";
1793 case HMAC_80_KEY_LEN:
1794 srtp_auth = "hmac-sha1-80";
1800 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1802 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1810 if (!(payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_KEMAC, 0)))
1813 GstMIKEYPayloadKEMAC *p = (GstMIKEYPayloadKEMAC *) payload;
1814 const GstMIKEYPayload *sub;
1815 GstMIKEYPayloadKeyData *pkd;
1818 if (p->enc_alg != GST_MIKEY_ENC_NULL || p->mac_alg != GST_MIKEY_MAC_NULL)
1821 if (!(sub = gst_mikey_payload_kemac_get_sub (payload, 0)))
1824 if (sub->type != GST_MIKEY_PT_KEY_DATA)
1827 pkd = (GstMIKEYPayloadKeyData *) sub;
1829 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
1831 gst_caps_set_simple (caps, "srtp-key", GST_TYPE_BUFFER, buf, NULL);
1834 gst_caps_set_simple (caps,
1835 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1836 "srtp-auth", G_TYPE_STRING, srtp_auth,
1837 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1838 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1842 gst_mikey_message_unref (msg);
1848 * Mapping SDP attributes to caps
1850 * prepend 'a-' to IANA registered sdp attributes names
1851 * (ie: not prefixed with 'x-') in order to avoid
1852 * collision with gstreamer standard caps properties names
1855 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1857 if (attributes->len > 0) {
1861 s = gst_caps_get_structure (caps, 0);
1863 for (i = 0; i < attributes->len; i++) {
1864 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1865 gchar *tofree, *key;
1869 /* skip some of the attribute we already handle */
1870 if (!strcmp (key, "fmtp"))
1872 if (!strcmp (key, "rtpmap"))
1874 if (!strcmp (key, "control"))
1876 if (!strcmp (key, "range"))
1878 if (g_str_equal (key, "key-mgmt")) {
1879 parse_keymgmt (attr->value, caps);
1883 /* string must be valid UTF8 */
1884 if (!g_utf8_validate (attr->value, -1, NULL))
1887 if (!g_str_has_prefix (key, "x-"))
1888 tofree = key = g_strdup_printf ("a-%s", key);
1892 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1893 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1899 static const gchar *
1900 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
1909 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
1912 if (sscanf (attr, "%d ", &val) != 1)
1922 * Mapping of caps to and from SDP fields:
1924 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1925 * a=fmtp:<payload> <param>[=<value>];...
1928 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1931 const gchar *rtpmap;
1935 gchar *params = NULL;
1941 /* get and parse rtpmap */
1942 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
1945 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
1947 g_warning ("error parsing rtpmap, ignoring");
1951 /* dynamic payloads need rtpmap or we fail */
1952 if (rtpmap == NULL && pt >= 96)
1955 /* check if we have a rate, if not, we need to look up the rate from the
1956 * default rates based on the payload types. */
1958 const GstRTPPayloadInfo *info;
1960 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
1961 /* dynamic types, use media and encoding_name */
1962 tmp = g_ascii_strdown (media->media, -1);
1963 info = gst_rtp_payload_info_for_name (tmp, name);
1966 /* static types, use payload type */
1967 info = gst_rtp_payload_info_for_pt (pt);
1971 if ((rate = info->clock_rate) == 0)
1974 /* we fail if we cannot find one */
1979 tmp = g_ascii_strdown (media->media, -1);
1980 caps = gst_caps_new_simple ("application/x-unknown",
1981 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
1983 s = gst_caps_get_structure (caps, 0);
1985 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
1987 /* encoding name must be upper case */
1989 tmp = g_ascii_strup (name, -1);
1990 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
1994 /* params must be lower case */
1995 if (params != NULL) {
1996 tmp = g_ascii_strdown (params, -1);
1997 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
2001 /* parse optional fmtp: field */
2002 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
2008 /* p is now of the format <payload> <param>[=<value>];... */
2009 PARSE_INT (p, " ", payload);
2010 if (payload != -1 && payload == pt) {
2014 /* <param>[=<value>] are separated with ';' */
2015 pairs = g_strsplit (p, ";", 0);
2016 for (i = 0; pairs[i]; i++) {
2018 const gchar *val, *key;
2020 /* the key may not have a '=', the value can have other '='s */
2021 valpos = strstr (pairs[i], "=");
2023 /* we have a '=' and thus a value, remove the '=' with \0 */
2025 /* value is everything between '=' and ';'. We split the pairs at ;
2026 * boundaries so we can take the remainder of the value. Some servers
2027 * put spaces around the value which we strip off here. Alternatively
2028 * we could strip those spaces in the depayloaders should these spaces
2029 * actually carry any meaning in the future. */
2030 val = g_strstrip (valpos + 1);
2032 /* simple <param>;.. is translated into <param>=1;... */
2035 /* strip the key of spaces, convert key to lowercase but not the value. */
2036 key = g_strstrip (pairs[i]);
2037 if (strlen (key) > 1) {
2038 tmp = g_ascii_strdown (key, -1);
2039 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
2051 g_warning ("rtpmap type not given for dynamic payload %d", pt);
2056 g_warning ("rate unknown for payload type %d", pt);
2062 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
2063 gint * rtpport, gint * rtcpport)
2066 GstStateChangeReturn ret;
2067 GstElement *udpsrc0, *udpsrc1;
2068 gint tmp_rtp, tmp_rtcp;
2072 src = stream->parent;
2078 /* Start at next port */
2079 tmp_rtp = src->next_port_num;
2081 if (stream->is_ipv6)
2082 host = "udp://[::0]";
2084 host = "udp://0.0.0.0";
2086 /* try to allocate 2 UDP ports, the RTP port should be an even
2087 * number and the RTCP port should be the next (uneven) port */
2090 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
2091 tmp_rtp >= src->client_port_range.max)
2094 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2095 if (udpsrc0 == NULL)
2096 goto no_udp_protocol;
2097 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
2099 if (src->udp_buffer_size != 0)
2100 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
2103 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
2104 if (ret == GST_STATE_CHANGE_FAILURE) {
2106 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
2109 if (++count > src->retry)
2112 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2113 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2114 gst_object_unref (udpsrc0);
2117 GST_DEBUG_OBJECT (src, "retry %d", count);
2120 goto no_udp_protocol;
2123 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
2124 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
2126 /* check if port is even */
2127 if ((tmp_rtp & 0x01) != 0) {
2128 /* port not even, close and allocate another */
2129 if (++count > src->retry)
2132 GST_DEBUG_OBJECT (src, "RTP port not even");
2134 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2135 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2136 gst_object_unref (udpsrc0);
2139 GST_DEBUG_OBJECT (src, "retry %d", count);
2144 /* allocate port+1 for RTCP now */
2145 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2146 if (udpsrc1 == NULL)
2147 goto no_udp_rtcp_protocol;
2150 tmp_rtcp = tmp_rtp + 1;
2151 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
2154 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
2156 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
2157 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
2158 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
2159 if (ret == GST_STATE_CHANGE_FAILURE) {
2160 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
2162 if (++count > src->retry)
2165 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2166 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2167 gst_object_unref (udpsrc0);
2170 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2171 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2172 gst_object_unref (udpsrc1);
2176 GST_DEBUG_OBJECT (src, "retry %d", count);
2180 /* all fine, do port check */
2181 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2182 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2184 /* this should not happen... */
2185 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2188 /* we keep these elements, we configure all in configure_transport when the
2189 * server told us to really use the UDP ports. */
2190 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2191 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2192 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2193 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2195 /* keep track of next available port number when we have a range
2197 if (src->next_port_num != 0)
2198 src->next_port_num = tmp_rtcp + 1;
2205 GST_DEBUG_OBJECT (src, "could not get UDP source");
2210 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2214 no_udp_rtcp_protocol:
2216 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2221 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2222 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2228 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2229 gst_object_unref (udpsrc0);
2232 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2233 gst_object_unref (udpsrc1);
2240 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2245 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2247 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2248 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2251 for (i = 0; i < 2; i++) {
2252 if (stream->udpsrc[i])
2253 gst_element_set_state (stream->udpsrc[i], state);
2259 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2266 event = gst_event_new_flush_start ();
2267 GST_DEBUG_OBJECT (src, "start flush");
2269 state = GST_STATE_PAUSED;
2271 event = gst_event_new_flush_stop (FALSE);
2272 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2275 state = GST_STATE_PLAYING;
2277 state = GST_STATE_PAUSED;
2279 gst_rtspsrc_push_event (src, event);
2280 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2281 gst_rtspsrc_set_state (src, state);
2284 static GstRTSPResult
2285 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2286 GstRTSPMessage * message, GTimeVal * timeout)
2291 ret = gst_rtsp_connection_send (conn, message, timeout);
2293 ret = GST_RTSP_ERROR;
2298 static GstRTSPResult
2299 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2300 GstRTSPMessage * message, GTimeVal * timeout)
2305 ret = gst_rtsp_connection_receive (conn, message, timeout);
2307 ret = GST_RTSP_ERROR;
2313 gst_rtspsrc_get_position (GstRTSPSrc * src)
2318 query = gst_query_new_position (GST_FORMAT_TIME);
2319 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2320 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2321 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2325 if (stream->srcpad) {
2326 if (gst_pad_query (stream->srcpad, query)) {
2327 gst_query_parse_position (query, &fmt, &pos);
2328 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2329 GST_TIME_ARGS (pos));
2330 src->last_pos = pos;
2340 gst_query_unref (query);
2344 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
2346 src->state = GST_RTSP_STATE_SEEKING;
2347 /* PLAY will add the range header now. */
2348 src->need_range = TRUE;
2354 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2359 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2361 gboolean flush, skip;
2364 GstSegment seeksegment = { 0, };
2368 GST_DEBUG_OBJECT (src, "doing seek with event");
2370 gst_event_parse_seek (event, &rate, &format, &flags,
2371 &cur_type, &cur, &stop_type, &stop);
2373 /* no negative rates yet */
2377 /* we need TIME format */
2378 if (format != src->segment.format)
2381 GST_DEBUG_OBJECT (src, "doing seek without event");
2383 cur_type = GST_SEEK_TYPE_SET;
2384 stop_type = GST_SEEK_TYPE_SET;
2387 /* get flush flag */
2388 flush = flags & GST_SEEK_FLAG_FLUSH;
2389 skip = flags & GST_SEEK_FLAG_SKIP;
2391 /* now we need to make sure the streaming thread is stopped. We do this by
2392 * either sending a FLUSH_START event downstream which will cause the
2393 * streaming thread to stop with a WRONG_STATE.
2394 * For a non-flushing seek we simply pause the task, which will happen as soon
2395 * as it completes one iteration (and thus might block when the sink is
2396 * blocking in preroll). */
2398 GST_DEBUG_OBJECT (src, "starting flush");
2399 gst_rtspsrc_flush (src, TRUE, FALSE);
2402 gst_task_pause (src->task);
2406 /* we should now be able to grab the streaming thread because we stopped it
2407 * with the above flush/pause code */
2408 GST_RTSP_STREAM_LOCK (src);
2410 GST_DEBUG_OBJECT (src, "stopped streaming");
2412 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2413 gst_rtspsrc_connection_flush (src, FALSE);
2415 /* copy segment, we need this because we still need the old
2416 * segment when we close the current segment. */
2417 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2419 /* configure the seek parameters in the seeksegment. We will then have the
2420 * right values in the segment to perform the seek */
2422 GST_DEBUG_OBJECT (src, "configuring seek");
2423 gst_segment_do_seek (&seeksegment, rate, format, flags,
2424 cur_type, cur, stop_type, stop, &update);
2427 /* figure out the last position we need to play. If it's configured (stop !=
2428 * -1), use that, else we play until the total duration of the file */
2429 if ((stop = seeksegment.stop) == -1)
2430 stop = seeksegment.duration;
2432 playing = (src->state == GST_RTSP_STATE_PLAYING);
2434 /* if we were playing, pause first */
2436 /* obtain current position in case seek fails */
2437 gst_rtspsrc_get_position (src);
2438 gst_rtspsrc_pause (src, FALSE);
2442 gst_rtspsrc_do_seek (src, &seeksegment);
2444 /* and continue playing */
2446 gst_rtspsrc_play (src, &seeksegment, FALSE);
2448 /* prepare for streaming again */
2450 /* if we started flush, we stop now */
2451 GST_DEBUG_OBJECT (src, "stopping flush");
2452 gst_rtspsrc_flush (src, FALSE, playing);
2455 /* now we did the seek and can activate the new segment values */
2456 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2458 /* if we're doing a segment seek, post a SEGMENT_START message */
2459 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2460 gst_element_post_message (GST_ELEMENT_CAST (src),
2461 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2462 src->segment.format, src->segment.position));
2465 /* now create the newsegment */
2466 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2467 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2470 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2471 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2472 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2473 stream->discont = TRUE;
2476 GST_RTSP_STREAM_UNLOCK (src);
2483 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2488 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2494 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2498 gboolean res = TRUE;
2501 src = GST_RTSPSRC_CAST (parent);
2503 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2504 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2506 switch (GST_EVENT_TYPE (event)) {
2507 case GST_EVENT_SEEK:
2508 res = gst_rtspsrc_perform_seek (src, event);
2512 case GST_EVENT_NAVIGATION:
2513 case GST_EVENT_LATENCY:
2521 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2522 res = gst_pad_send_event (target, event);
2523 gst_object_unref (target);
2525 gst_event_unref (event);
2528 gst_event_unref (event);
2534 /* this is the final event function we receive on the internal source pad when
2535 * we deal with TCP connections */
2537 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2542 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2544 switch (GST_EVENT_TYPE (event)) {
2545 case GST_EVENT_SEEK:
2547 case GST_EVENT_NAVIGATION:
2548 case GST_EVENT_LATENCY:
2550 gst_event_unref (event);
2557 /* this is the final query function we receive on the internal source pad when
2558 * we deal with TCP connections */
2560 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2564 gboolean res = TRUE;
2566 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2568 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2569 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2571 switch (GST_QUERY_TYPE (query)) {
2572 case GST_QUERY_POSITION:
2577 case GST_QUERY_DURATION:
2581 gst_query_parse_duration (query, &format, NULL);
2584 case GST_FORMAT_TIME:
2585 gst_query_set_duration (query, format, src->segment.duration);
2593 case GST_QUERY_LATENCY:
2595 /* we are live with a min latency of 0 and unlimited max latency, this
2596 * result will be updated by the session manager if there is any. */
2597 gst_query_set_latency (query, TRUE, 0, -1);
2607 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2609 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2613 gboolean res = FALSE;
2615 src = GST_RTSPSRC_CAST (parent);
2617 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2618 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2620 switch (GST_QUERY_TYPE (query)) {
2621 case GST_QUERY_DURATION:
2625 gst_query_parse_duration (query, &format, NULL);
2628 case GST_FORMAT_TIME:
2629 gst_query_set_duration (query, format, src->segment.duration);
2637 case GST_QUERY_SEEKING:
2641 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2642 if (format == GST_FORMAT_TIME) {
2644 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2646 /* seeking without duration is unlikely */
2647 seekable = seekable && src->seekable && src->segment.duration &&
2648 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2650 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2651 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2652 src->segment.start, src->segment.stop);
2661 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2663 gst_query_set_uri (query, uri);
2671 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2673 /* forward the query to the proxy target pad */
2675 res = gst_pad_query (target, query);
2676 gst_object_unref (target);
2685 /* callback for RTCP messages to be sent to the server when operating in TCP
2687 static GstFlowReturn
2688 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2691 GstRTSPStream *stream;
2692 GstFlowReturn res = GST_FLOW_OK;
2697 GstRTSPMessage message = { 0 };
2698 GstRTSPConnection *conn;
2700 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2701 src = stream->parent;
2703 gst_buffer_map (buffer, &map, GST_MAP_READ);
2707 gst_rtsp_message_init_data (&message, stream->channel[1]);
2709 /* lend the body data to the message */
2710 gst_rtsp_message_take_body (&message, data, size);
2712 if (stream->conninfo.connection)
2713 conn = stream->conninfo.connection;
2715 conn = src->conninfo.connection;
2717 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2718 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2719 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2721 /* and steal it away again because we will free it when unreffing the
2723 gst_rtsp_message_steal_body (&message, &data, &size);
2724 gst_rtsp_message_unset (&message);
2726 gst_buffer_unmap (buffer, &map);
2727 gst_buffer_unref (buffer);
2732 static GstPadProbeReturn
2733 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2735 GstRTSPSrc *src = user_data;
2737 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2738 GST_DEBUG_PAD_NAME (pad));
2740 /* activate the streams */
2741 GST_OBJECT_LOCK (src);
2742 if (!src->need_activate)
2745 src->need_activate = FALSE;
2746 GST_OBJECT_UNLOCK (src);
2748 gst_rtspsrc_activate_streams (src);
2750 return GST_PAD_PROBE_OK;
2754 GST_OBJECT_UNLOCK (src);
2755 return GST_PAD_PROBE_OK;
2760 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
2762 GstPad *gpad = GST_PAD_CAST (user_data);
2764 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
2765 gst_pad_store_sticky_event (gpad, *event);
2770 /* this callback is called when the session manager generated a new src pad with
2771 * payloaded RTP packets. We simply ghost the pad here. */
2773 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2776 GstPadTemplate *template;
2779 GstRTSPStream *stream;
2782 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2784 GST_RTSP_STATE_LOCK (src);
2786 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2787 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2788 goto unknown_stream;
2790 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2792 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2794 goto unknown_stream;
2797 stream->ssrc = ssrc;
2799 /* we'll add it later see below */
2800 stream->added = TRUE;
2802 /* check if we added all streams */
2804 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2805 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2807 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
2808 ostream, ostream->container, ostream->added, ostream->setup);
2810 /* if we find a stream for which we did a setup that is not added, we
2811 * need to wait some more */
2812 if (ostream->setup && !ostream->added) {
2817 GST_RTSP_STATE_UNLOCK (src);
2819 /* create a new pad we will use to stream to */
2820 template = gst_static_pad_template_get (&rtptemplate);
2821 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2822 gst_object_unref (template);
2825 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2826 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2827 gst_pad_set_active (stream->srcpad, TRUE);
2828 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
2829 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2832 GST_DEBUG_OBJECT (src, "We added all streams");
2833 /* when we get here, all stream are added and we can fire the no-more-pads
2835 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2843 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2844 GST_RTSP_STATE_UNLOCK (src);
2851 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
2855 len = stream->ptmap->len;
2856 for (i = 0; i < len; i++) {
2857 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2865 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2867 GstRTSPStream *stream;
2870 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2872 GST_RTSP_STATE_LOCK (src);
2873 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2875 goto unknown_stream;
2877 if ((caps = stream_get_caps_for_pt (stream, pt)))
2878 gst_caps_ref (caps);
2879 GST_RTSP_STATE_UNLOCK (src);
2885 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2886 GST_RTSP_STATE_UNLOCK (src);
2892 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2894 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2900 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2906 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2912 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2914 GstRTSPSrc *src = stream->parent;
2917 g_object_get (source, "ssrc", &ssrc, NULL);
2919 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2920 ssrc, stream->ssrc, stream->id);
2922 if (ssrc == stream->ssrc)
2923 gst_rtspsrc_do_stream_eos (src, stream);
2927 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2929 GstRTSPSrc *src = stream->parent;
2932 g_object_get (source, "ssrc", &ssrc, NULL);
2934 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2935 ssrc, stream->ssrc, stream->id);
2937 if (ssrc == stream->ssrc)
2938 gst_rtspsrc_do_stream_eos (src, stream);
2942 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2944 GstRTSPStream *stream;
2946 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2948 /* get stream for session */
2949 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2951 gst_rtspsrc_do_stream_eos (src, stream);
2956 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2958 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2963 set_manager_buffer_mode (GstRTSPSrc * src)
2965 GObjectClass *klass;
2967 if (src->manager == NULL)
2970 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2972 if (!g_object_class_find_property (klass, "buffer-mode"))
2975 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2976 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2981 GST_DEBUG_OBJECT (src,
2982 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
2984 if (src->provided_clock) {
2985 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
2987 if (clock == src->provided_clock) {
2988 GST_DEBUG_OBJECT (src, "selected synced");
2989 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
2992 gst_object_unref (clock);
2997 /* Otherwise fall-through and use another buffer mode */
2999 gst_object_unref (clock);
3002 GST_DEBUG_OBJECT (src, "auto buffering mode");
3003 if (src->use_buffering) {
3004 GST_DEBUG_OBJECT (src, "selected buffer");
3005 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
3007 GST_DEBUG_OBJECT (src, "selected slave");
3008 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
3013 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
3015 GST_DEBUG ("request key %u", ssrc);
3016 return gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
3020 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
3022 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3023 if (stream->id != session)
3026 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3027 stream->profile != GST_RTSP_PROFILE_SAVPF)
3030 if (stream->srtpdec == NULL) {
3033 name = g_strdup_printf ("srtpdec_%u", session);
3034 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
3037 g_signal_connect (stream->srtpdec, "request-key",
3038 (GCallback) request_key, stream);
3040 return gst_object_ref (stream->srtpdec);
3044 request_rtcp_encoder (GstElement * rtpbin, guint session,
3045 GstRTSPStream * stream)
3050 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3051 if (stream->id != session)
3054 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3055 stream->profile != GST_RTSP_PROFILE_SAVPF)
3058 if (stream->srtpenc == NULL) {
3061 name = g_strdup_printf ("srtpenc_%u", session);
3062 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
3065 /* get RTCP crypto parameters from caps */
3066 s = gst_caps_get_structure (stream->srtcpparams, 0);
3070 GType ciphertype, authtype;
3071 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
3073 ciphertype = g_type_from_name ("GstSrtpCipherType");
3074 authtype = g_type_from_name ("GstSrtpAuthType");
3075 g_value_init (&rtcp_cipher, ciphertype);
3076 g_value_init (&rtcp_auth, authtype);
3078 str = gst_structure_get_string (s, "srtcp-cipher");
3079 gst_value_deserialize (&rtcp_cipher, str);
3080 str = gst_structure_get_string (s, "srtcp-auth");
3081 gst_value_deserialize (&rtcp_auth, str);
3082 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
3084 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
3086 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
3088 g_object_set (stream->srtpenc, "key", buf, NULL);
3090 g_value_unset (&rtcp_cipher);
3091 g_value_unset (&rtcp_auth);
3094 name = g_strdup_printf ("rtcp_sink_%d", session);
3095 pad = gst_element_get_request_pad (stream->srtpenc, name);
3097 gst_object_unref (pad);
3099 return gst_object_ref (stream->srtpenc);
3103 /* try to get and configure a manager */
3105 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3106 GstRTSPTransport * transport)
3108 const gchar *manager;
3110 GstStateChangeReturn ret;
3112 /* find a manager */
3113 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3117 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3119 /* configure the manager */
3120 if (src->manager == NULL) {
3121 GObjectClass *klass;
3123 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3125 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3129 goto use_no_manager;
3131 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3132 goto manager_failed;
3135 /* we manage this element */
3136 gst_element_set_locked_state (src->manager, TRUE);
3137 gst_bin_add (GST_BIN_CAST (src), src->manager);
3139 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3140 if (ret == GST_STATE_CHANGE_FAILURE)
3141 goto start_manager_failure;
3143 g_object_set (src->manager, "latency", src->latency, NULL);
3145 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3147 if (g_object_class_find_property (klass, "ntp-sync")) {
3148 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3151 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3152 g_object_set (src->manager, "use-pipeline-clock",
3153 src->use_pipeline_clock, NULL);
3156 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3157 g_object_set (src->manager, "sdes", src->sdes, NULL);
3160 if (g_object_class_find_property (klass, "drop-on-latency")) {
3161 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3165 /* buffer mode pauses are handled by adding offsets to buffer times,
3166 * but some depayloaders may have a hard time syncing output times
3167 * with such input times, e.g. container ones, most notably ASF */
3168 /* TODO alternatives are having an event that indicates these shifts,
3169 * or having rtsp extensions provide suggestion on buffer mode */
3170 /* valid duration implies not likely live pipeline,
3171 * so slaving in jitterbuffer does not make much sense
3172 * (and might mess things up due to bursts) */
3173 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3174 src->segment.duration && !stream->container) {
3175 src->use_buffering = TRUE;
3177 src->use_buffering = FALSE;
3180 set_manager_buffer_mode (src);
3182 /* connect to signals */
3183 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3185 src->manager_sig_id =
3186 g_signal_connect (src->manager, "pad-added",
3187 (GCallback) new_manager_pad, src);
3188 src->manager_ptmap_id =
3189 g_signal_connect (src->manager, "request-pt-map",
3190 (GCallback) request_pt_map, src);
3192 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3195 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3198 g_signal_connect (src->manager, "request-rtp-decoder",
3199 (GCallback) request_rtp_decoder, stream);
3200 g_signal_connect (src->manager, "request-rtcp-decoder",
3201 (GCallback) request_rtp_decoder, stream);
3202 g_signal_connect (src->manager, "request-rtcp-encoder",
3203 (GCallback) request_rtcp_encoder, stream);
3205 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3206 * into a separate RTP session. */
3207 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3208 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3210 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3211 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3214 /* now configure the bandwidth in the manager */
3215 if (g_signal_lookup ("get-internal-session",
3216 G_OBJECT_TYPE (src->manager)) != 0) {
3217 GObject *rtpsession;
3219 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3222 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3224 stream->session = rtpsession;
3226 if (stream->as_bandwidth != -1) {
3227 GST_INFO_OBJECT (src, "setting AS: %f",
3228 (gdouble) (stream->as_bandwidth * 1000));
3229 g_object_set (rtpsession, "bandwidth",
3230 (gdouble) (stream->as_bandwidth * 1000), NULL);
3232 if (stream->rr_bandwidth != -1) {
3233 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3234 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3237 if (stream->rs_bandwidth != -1) {
3238 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3239 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3243 g_object_set (rtpsession, "probation", src->probation, NULL);
3245 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3247 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3249 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3251 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3253 g_signal_connect (rtpsession, "on-ssrc-active",
3254 (GCallback) on_ssrc_active, stream);
3265 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3270 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3273 start_manager_failure:
3275 GST_DEBUG_OBJECT (src, "could not start session manager");
3280 /* free the UDP sources allocated when negotiating a transport.
3281 * This function is called when the server negotiated to a transport where the
3282 * UDP sources are not needed anymore, such as TCP or multicast. */
3284 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3288 for (i = 0; i < 2; i++) {
3289 if (stream->udpsrc[i]) {
3290 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3291 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3292 gst_object_unref (stream->udpsrc[i]);
3293 stream->udpsrc[i] = NULL;
3298 /* for TCP, create pads to send and receive data to and from the manager and to
3299 * intercept various events and queries
3302 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3303 GstRTSPTransport * transport, GstPad ** outpad)
3306 GstPadTemplate *template;
3307 GstPad *pad0, *pad1;
3309 /* configure for interleaved delivery, nothing needs to be done
3310 * here, the loop function will call the chain functions of the
3311 * session manager. */
3312 stream->channel[0] = transport->interleaved.min;
3313 stream->channel[1] = transport->interleaved.max;
3314 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3315 stream->channel[0], stream->channel[1]);
3317 /* we can remove the allocated UDP ports now */
3318 gst_rtspsrc_stream_free_udp (stream);
3320 /* no session manager, send data to srcpad directly */
3321 if (!stream->channelpad[0]) {
3322 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3324 /* create a new pad we will use to stream to */
3325 name = g_strdup_printf ("stream_%u", stream->id);
3326 template = gst_static_pad_template_get (&rtptemplate);
3327 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3328 gst_object_unref (template);
3331 /* set caps and activate */
3332 gst_pad_use_fixed_caps (stream->channelpad[0]);
3333 gst_pad_set_active (stream->channelpad[0], TRUE);
3335 *outpad = gst_object_ref (stream->channelpad[0]);
3337 GST_DEBUG_OBJECT (src, "using manager source pad");
3339 template = gst_static_pad_template_get (&anysrctemplate);
3341 /* allocate pads for sending the channel data into the manager */
3342 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3343 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3344 gst_object_unref (stream->channelpad[0]);
3345 stream->channelpad[0] = pad0;
3346 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3347 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3348 gst_pad_set_element_private (pad0, src);
3349 gst_pad_set_active (pad0, TRUE);
3351 if (stream->channelpad[1]) {
3352 /* if we have a sinkpad for the other channel, create a pad and link to the
3354 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3355 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3356 gst_pad_link_full (pad1, stream->channelpad[1],
3357 GST_PAD_LINK_CHECK_NOTHING);
3358 gst_object_unref (stream->channelpad[1]);
3359 stream->channelpad[1] = pad1;
3360 gst_pad_set_active (pad1, TRUE);
3362 gst_object_unref (template);
3364 /* setup RTCP transport back to the server if we have to. */
3365 if (src->manager && src->do_rtcp) {
3368 template = gst_static_pad_template_get (&anysinktemplate);
3370 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3371 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3372 gst_pad_set_element_private (stream->rtcppad, stream);
3373 gst_pad_set_active (stream->rtcppad, TRUE);
3375 /* get session RTCP pad */
3376 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3377 pad = gst_element_get_request_pad (src->manager, name);
3382 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3383 gst_object_unref (pad);
3386 gst_object_unref (template);
3392 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3393 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3394 gint * max, guint * ttl)
3396 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3398 if (!(*destination = transport->destination))
3399 *destination = stream->destination;
3402 /* transport first */
3403 *min = transport->port.min;
3404 *max = transport->port.max;
3405 if (*min == -1 && *max == -1) {
3406 /* then try from SDP */
3407 if (stream->port != 0) {
3408 *min = stream->port;
3409 *max = stream->port + 1;
3415 if (!(*ttl = transport->ttl))
3420 /* first take the source, then the endpoint to figure out where to send
3422 if (!(*destination = transport->source)) {
3423 if (src->conninfo.connection)
3424 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3425 else if (stream->conninfo.connection)
3427 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3431 /* for unicast we only expect the ports here */
3432 *min = transport->server_port.min;
3433 *max = transport->server_port.max;
3438 /* For multicast create UDP sources and join the multicast group. */
3440 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3441 GstRTSPTransport * transport, GstPad ** outpad)
3444 const gchar *destination;
3447 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3449 /* we can remove the allocated UDP ports now */
3450 gst_rtspsrc_stream_free_udp (stream);
3452 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3455 /* we need a destination now */
3456 if (destination == NULL)
3457 goto no_destination;
3459 /* we really need ports now or we won't be able to receive anything at all */
3460 if (min == -1 && max == -1)
3463 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3464 destination, min, max);
3466 /* creating UDP source for RTP */
3468 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3470 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3472 if (stream->udpsrc[0] == NULL)
3475 /* take ownership */
3476 gst_object_ref_sink (stream->udpsrc[0]);
3478 if (src->udp_buffer_size != 0)
3479 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3480 src->udp_buffer_size, NULL);
3482 if (src->multi_iface != NULL)
3483 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3484 src->multi_iface, NULL);
3487 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3488 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3491 /* creating another UDP source for RTCP */
3495 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3497 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3499 if (stream->udpsrc[1] == NULL)
3502 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3503 stream->profile == GST_RTSP_PROFILE_SAVPF)
3504 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3506 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3507 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3508 gst_caps_unref (caps);
3510 /* take ownership */
3511 gst_object_ref_sink (stream->udpsrc[1]);
3513 if (src->multi_iface != NULL)
3514 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3515 src->multi_iface, NULL);
3517 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
3524 GST_DEBUG_OBJECT (src, "no UDP source element found");
3529 GST_DEBUG_OBJECT (src, "no destination found");
3534 GST_DEBUG_OBJECT (src, "no ports found");
3539 /* configure the remainder of the UDP ports */
3541 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3542 GstRTSPTransport * transport, GstPad ** outpad)
3544 /* we manage the UDP elements now. For unicast, the UDP sources where
3545 * allocated in the stream when we suggested a transport. */
3546 if (stream->udpsrc[0]) {
3549 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3550 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3552 GST_DEBUG_OBJECT (src, "setting up UDP source");
3554 /* configure a timeout on the UDP port. When the timeout message is
3555 * posted, we assume UDP transport is not possible. We reconnect using TCP
3557 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3558 src->udp_timeout * 1000, NULL);
3560 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
3561 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3563 /* get output pad of the UDP source. */
3564 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3566 /* save it so we can unblock */
3567 stream->blockedpad = *outpad;
3569 /* configure pad block on the pad. As soon as there is dataflow on the
3570 * UDP source, we know that UDP is not blocked by a firewall and we can
3571 * configure all the streams to let the application autoplug decoders. */
3573 gst_pad_add_probe (stream->blockedpad,
3574 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3575 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
3577 if (stream->channelpad[0]) {
3578 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3579 /* configure for UDP delivery, we need to connect the UDP pads to
3580 * the session plugin. */
3581 gst_pad_link_full (*outpad, stream->channelpad[0],
3582 GST_PAD_LINK_CHECK_NOTHING);
3583 gst_object_unref (*outpad);
3585 /* we connected to pad-added signal to get pads from the manager */
3587 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3592 if (stream->udpsrc[1]) {
3595 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3596 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3598 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3599 stream->profile == GST_RTSP_PROFILE_SAVPF)
3600 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3602 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3603 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3604 gst_caps_unref (caps);
3606 if (stream->channelpad[1]) {
3609 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3611 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3612 gst_pad_link_full (pad, stream->channelpad[1],
3613 GST_PAD_LINK_CHECK_NOTHING);
3614 gst_object_unref (pad);
3616 /* leave unlinked */
3622 /* configure the UDP sink back to the server for status reports */
3624 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3625 GstRTSPStream * stream, GstRTSPTransport * transport)
3628 gint rtp_port, rtcp_port;
3629 gboolean do_rtp, do_rtcp;
3630 const gchar *destination;
3635 /* get transport info */
3636 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3637 &rtp_port, &rtcp_port, &ttl);
3639 /* see what we need to do */
3640 do_rtp = (rtp_port != -1);
3641 /* it's possible that the server does not want us to send RTCP in which case
3643 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3645 /* we need a destination when we have RTP or RTCP ports */
3646 if (destination == NULL && (do_rtp || do_rtcp))
3647 goto no_destination;
3649 /* try to construct the fakesrc to the RTP port of the server to open up any
3652 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3655 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3656 stream->udpsink[0] =
3657 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3659 if (stream->udpsink[0] == NULL)
3660 goto no_sink_element;
3662 /* don't join multicast group, we will have the source socket do that */
3663 /* no sync or async state changes needed */
3664 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3665 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3667 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3669 if (stream->udpsrc[0]) {
3670 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3671 * so that NAT firewalls will open a hole for us */
3672 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3673 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3674 /* configure socket and make sure udpsink does not close it when shutting
3675 * down, it belongs to udpsrc after all. */
3676 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3677 "close-socket", FALSE, NULL);
3678 g_object_unref (socket);
3681 /* the source for the dummy packets to open up NAT */
3682 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3683 if (stream->fakesrc == NULL)
3684 goto no_fakesrc_element;
3686 /* random data in 5 buffers, a size of 200 bytes should be fine */
3687 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3688 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3690 /* we don't want to consider this a sink */
3691 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
3693 /* keep everything locked */
3694 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3695 gst_element_set_locked_state (stream->fakesrc, TRUE);
3697 gst_object_ref (stream->udpsink[0]);
3698 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3699 gst_object_ref (stream->fakesrc);
3700 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3702 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3703 "sink", GST_PAD_LINK_CHECK_NOTHING);
3706 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3709 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3710 stream->udpsink[1] =
3711 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3713 if (stream->udpsink[1] == NULL)
3714 goto no_sink_element;
3716 /* don't join multicast group, we will have the source socket do that */
3717 /* no sync or async state changes needed */
3718 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3719 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3721 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3723 if (stream->udpsrc[1]) {
3724 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3725 * because some servers check the port number of where it sends RTCP to identify
3726 * the RTCP packets it receives */
3727 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3728 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3729 /* configure socket and make sure udpsink does not close it when shutting
3730 * down, it belongs to udpsrc after all. */
3731 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3732 "close-socket", FALSE, NULL);
3733 g_object_unref (socket);
3736 /* we don't want to consider this a sink */
3737 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3739 /* we keep this playing always */
3740 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3741 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3743 gst_object_ref (stream->udpsink[1]);
3744 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3746 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3748 /* get session RTCP pad */
3749 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3750 pad = gst_element_get_request_pad (src->manager, name);
3755 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3756 gst_object_unref (pad);
3765 GST_DEBUG_OBJECT (src, "no destination address specified");
3770 GST_DEBUG_OBJECT (src, "no UDP sink element found");
3775 GST_DEBUG_OBJECT (src, "no fakesrc element found");
3780 /* sets up all elements needed for streaming over the specified transport.
3781 * Does not yet expose the element pads, this will be done when there is actuall
3782 * dataflow detected, which might never happen when UDP is blocked in a
3783 * firewall, for example.
3786 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3787 GstRTSPTransport * transport)
3790 GstPad *outpad = NULL;
3791 GstPadTemplate *template;
3793 const gchar *media_type;
3796 src = stream->parent;
3798 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3800 /* get the proper media type for this stream now */
3801 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
3802 goto unknown_transport;
3804 goto unknown_transport;
3806 /* configure the final media type */
3807 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
3809 len = stream->ptmap->len;
3810 for (i = 0; i < len; i++) {
3812 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3814 if (item->caps == NULL)
3817 s = gst_caps_get_structure (item->caps, 0);
3818 gst_structure_set_name (s, media_type);
3819 /* set ssrc if known */
3820 if (transport->ssrc)
3821 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
3824 /* try to get and configure a manager, channelpad[0-1] will be configured with
3825 * the pads for the manager, or NULL when no manager is needed. */
3826 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3829 switch (transport->lower_transport) {
3830 case GST_RTSP_LOWER_TRANS_TCP:
3831 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3832 goto transport_failed;
3834 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3835 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3836 goto transport_failed;
3837 /* fallthrough, the rest is the same for UDP and MCAST */
3838 case GST_RTSP_LOWER_TRANS_UDP:
3839 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3840 goto transport_failed;
3841 /* configure udpsinks back to the server for RTCP messages and for the
3842 * dummy RTP messages to open NAT. */
3843 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3844 goto transport_failed;
3847 goto unknown_transport;
3851 GST_DEBUG_OBJECT (src, "creating ghostpad");
3853 gst_pad_use_fixed_caps (outpad);
3855 /* create ghostpad, don't add just yet, this will be done when we activate
3857 name = g_strdup_printf ("stream_%u", stream->id);
3858 template = gst_static_pad_template_get (&rtptemplate);
3859 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3860 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3861 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3862 gst_object_unref (template);
3865 gst_object_unref (outpad);
3867 /* mark pad as ok */
3868 stream->last_ret = GST_FLOW_OK;
3875 GST_DEBUG_OBJECT (src, "failed to configure transport");
3880 GST_DEBUG_OBJECT (src, "unknown transport");
3885 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3890 /* send a couple of dummy random packets on the receiver RTP port to the server,
3891 * this should make a firewall think we initiated the data transfer and
3892 * hopefully allow packets to go from the sender port to our RTP receiver port */
3894 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3898 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3901 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3902 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3904 if (stream->fakesrc && stream->udpsink[0]) {
3905 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3906 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3907 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3908 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3909 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3915 /* Adds the source pads of all configured streams to the element.
3916 * This code is performed when we detected dataflow.
3918 * We detect dataflow from either the _loop function or with pad probes on the
3922 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3926 GST_DEBUG_OBJECT (src, "activating streams");
3928 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3929 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3931 if (stream->udpsrc[0]) {
3932 /* remove timeout, we are streaming now and timeouts will be handled by
3933 * the session manager and jitter buffer */
3934 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3936 if (stream->srcpad) {
3937 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3938 gst_pad_set_active (stream->srcpad, TRUE);
3940 /* if we don't have a session manager, set the caps now. If we have a
3941 * session, we will get a notification of the pad and the caps. */
3942 if (!src->manager) {
3945 caps = stream_get_caps_for_pt (stream, stream->default_pt);
3946 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3947 gst_pad_set_caps (stream->srcpad, caps);
3950 if (!stream->added) {
3951 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3952 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3953 stream->added = TRUE;
3958 /* unblock all pads */
3959 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3960 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3962 if (stream->blockid) {
3963 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3964 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3965 stream->blockid = 0;
3973 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
3974 gboolean reset_manager)
3977 guint64 start, stop;
3978 gdouble play_speed, play_scale;
3980 GST_DEBUG_OBJECT (src, "configuring stream caps");
3982 start = segment->position;
3983 stop = segment->duration;
3984 play_speed = segment->rate;
3985 play_scale = segment->applied_rate;
3987 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3988 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3994 len = stream->ptmap->len;
3995 for (j = 0; j < len; j++) {
3997 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
3999 if (item->caps == NULL)
4002 caps = gst_caps_make_writable (item->caps);
4004 if (stream->timebase != -1)
4005 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
4006 (guint) stream->timebase, NULL);
4007 if (stream->seqbase != -1)
4008 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
4009 (guint) stream->seqbase, NULL);
4010 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
4012 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
4013 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
4014 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
4017 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
4020 if (item->pt == stream->default_pt && stream->udpsrc[0]) {
4021 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4025 if (reset_manager && src->manager) {
4026 GST_DEBUG_OBJECT (src, "clear session");
4027 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
4031 static GstFlowReturn
4032 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
4037 /* store the value */
4038 stream->last_ret = ret;
4040 /* if it's success we can return the value right away */
4041 if (ret == GST_FLOW_OK)
4044 /* any other error that is not-linked can be returned right
4046 if (ret != GST_FLOW_NOT_LINKED)
4049 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4050 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4051 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4053 ret = ostream->last_ret;
4054 /* some other return value (must be SUCCESS but we can return
4055 * other values as well) */
4056 if (ret != GST_FLOW_NOT_LINKED)
4059 /* if we get here, all other pads were unlinked and we return
4060 * NOT_LINKED then */
4066 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4069 gboolean res = TRUE;
4071 /* only streams that have a connection to the outside world */
4075 if (stream->udpsrc[0]) {
4076 gst_event_ref (event);
4077 res = gst_element_send_event (stream->udpsrc[0], event);
4078 } else if (stream->channelpad[0]) {
4079 gst_event_ref (event);
4080 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4081 res = gst_pad_push_event (stream->channelpad[0], event);
4083 res = gst_pad_send_event (stream->channelpad[0], event);
4086 if (stream->udpsrc[1]) {
4087 gst_event_ref (event);
4088 res &= gst_element_send_event (stream->udpsrc[1], event);
4089 } else if (stream->channelpad[1]) {
4090 gst_event_ref (event);
4091 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4092 res &= gst_pad_push_event (stream->channelpad[1], event);
4094 res &= gst_pad_send_event (stream->channelpad[1], event);
4098 gst_event_unref (event);
4104 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4107 gboolean res = TRUE;
4109 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4110 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4112 gst_event_ref (event);
4113 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4115 gst_event_unref (event);
4120 static GstRTSPResult
4121 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4126 if (info->connection == NULL) {
4127 if (info->url == NULL) {
4128 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4129 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4133 /* create connection */
4134 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4135 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4136 goto could_not_create;
4139 g_free (info->url_str);
4140 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4142 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4144 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4145 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4146 src->tls_validation_flags))
4147 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4149 if (src->tls_database)
4150 gst_rtsp_connection_set_tls_database (info->connection,
4154 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4155 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4157 if (src->proxy_host) {
4158 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4160 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4165 if (!info->connected) {
4168 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4169 ("Connecting to %s", info->location));
4170 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4172 gst_rtsp_connection_connect (info->connection,
4173 src->ptcp_timeout)) < 0)
4174 goto could_not_connect;
4176 info->connected = TRUE;
4183 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4188 gchar *str = gst_rtsp_strresult (res);
4189 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4195 gchar *str = gst_rtsp_strresult (res);
4196 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4202 static GstRTSPResult
4203 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4206 GST_RTSP_STATE_LOCK (src);
4207 if (info->connected) {
4208 GST_DEBUG_OBJECT (src, "closing connection...");
4209 gst_rtsp_connection_close (info->connection);
4210 info->connected = FALSE;
4212 if (free && info->connection) {
4213 /* free connection */
4214 GST_DEBUG_OBJECT (src, "freeing connection...");
4215 gst_rtsp_connection_free (info->connection);
4216 info->connection = NULL;
4218 GST_RTSP_STATE_UNLOCK (src);
4222 static GstRTSPResult
4223 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4228 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4229 gst_rtsp_conninfo_close (src, info, FALSE);
4230 res = gst_rtsp_conninfo_connect (src, info, async);
4236 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4240 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4241 GST_RTSP_STATE_LOCK (src);
4242 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4243 GST_DEBUG_OBJECT (src, "connection flush");
4244 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4245 src->conninfo.flushing = flush;
4247 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4248 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4249 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4250 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4251 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4252 stream->conninfo.flushing = flush;
4255 GST_RTSP_STATE_UNLOCK (src);
4258 /* FIXME, handle server request, reply with OK, for now */
4259 static GstRTSPResult
4260 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
4261 GstRTSPMessage * request)
4263 GstRTSPMessage response = { 0 };
4266 GST_DEBUG_OBJECT (src, "got server request message");
4269 gst_rtsp_message_dump (request);
4271 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4273 if (res == GST_RTSP_ENOTIMPL) {
4274 /* default implementation, send OK */
4275 GST_DEBUG_OBJECT (src, "prepare OK reply");
4277 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4282 /* let app parse and reply */
4283 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4284 0, request, &response);
4287 gst_rtsp_message_dump (&response);
4289 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
4293 gst_rtsp_message_unset (&response);
4294 } else if (res == GST_RTSP_EEOF)
4302 gst_rtsp_message_unset (&response);
4307 /* send server keep-alive */
4308 static GstRTSPResult
4309 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4311 GstRTSPMessage request = { 0 };
4313 GstRTSPMethod method;
4314 const gchar *control;
4316 if (src->do_rtsp_keep_alive == FALSE) {
4317 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4318 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4322 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4324 /* find a method to use for keep-alive */
4325 if (src->methods & GST_RTSP_GET_PARAMETER)
4326 method = GST_RTSP_GET_PARAMETER;
4328 method = GST_RTSP_OPTIONS;
4330 control = get_aggregate_control (src);
4331 if (control == NULL)
4334 res = gst_rtsp_message_init_request (&request, method, control);
4339 gst_rtsp_message_dump (&request);
4342 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
4347 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4348 gst_rtsp_message_unset (&request);
4355 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4360 gchar *str = gst_rtsp_strresult (res);
4362 gst_rtsp_message_unset (&request);
4363 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4364 ("Could not send keep-alive. (%s)", str));
4370 static GstFlowReturn
4371 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4373 GstFlowReturn ret = GST_FLOW_OK;
4375 GstRTSPStream *stream;
4376 GstPad *outpad = NULL;
4383 channel = message->type_data.data.channel;
4385 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4387 goto unknown_stream;
4389 if (channel == stream->channel[0]) {
4390 outpad = stream->channelpad[0];
4392 } else if (channel == stream->channel[1]) {
4393 outpad = stream->channelpad[1];
4399 /* take a look at the body to figure out what we have */
4400 gst_rtsp_message_get_body (message, &data, &size);
4402 goto invalid_length;
4404 /* channels are not correct on some servers, do extra check */
4405 if (data[1] >= 200 && data[1] <= 204) {
4406 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4407 outpad = stream->channelpad[1];
4411 /* we have no clue what this is, just ignore then. */
4413 goto unknown_stream;
4415 /* take the message body for further processing */
4416 gst_rtsp_message_steal_body (message, &data, &size);
4418 /* strip the trailing \0 */
4421 buf = gst_buffer_new ();
4422 gst_buffer_append_memory (buf,
4423 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4425 /* don't need message anymore */
4426 gst_rtsp_message_unset (message);
4428 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4431 if (src->need_activate) {
4437 guint group_id = gst_util_group_id_next ();
4439 /* generate an SHA256 sum of the URI */
4440 cs = g_checksum_new (G_CHECKSUM_SHA256);
4441 uri = src->conninfo.location;
4442 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4444 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4445 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4448 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4449 event = gst_event_new_stream_start (stream_id);
4450 gst_event_set_group_id (event, group_id);
4453 gst_rtspsrc_stream_push_event (src, ostream, event);
4455 g_checksum_free (cs);
4457 gst_rtspsrc_activate_streams (src);
4458 src->need_activate = FALSE;
4460 if ((event = src->start_segment) != NULL) {
4461 src->start_segment = NULL;
4462 gst_rtspsrc_push_event (src, event);
4465 if (src->base_time == -1) {
4466 /* Take current running_time. This timestamp will be put on
4467 * the first buffer of each stream because we are a live source and so we
4468 * timestamp with the running_time. When we are dealing with TCP, we also
4469 * only timestamp the first buffer (using the DISCONT flag) because a server
4470 * typically bursts data, for which we don't want to compensate by speeding
4471 * up the media. The other timestamps will be interpollated from this one
4472 * using the RTP timestamps. */
4473 GST_OBJECT_LOCK (src);
4474 if (GST_ELEMENT_CLOCK (src)) {
4476 GstClockTime base_time;
4478 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4479 base_time = GST_ELEMENT_CAST (src)->base_time;
4481 src->base_time = now - base_time;
4483 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4484 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4486 GST_OBJECT_UNLOCK (src);
4489 if (stream->discont && !is_rtcp) {
4490 /* mark first RTP buffer as discont */
4491 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4492 stream->discont = FALSE;
4493 /* first buffer gets the timestamp, other buffers are not timestamped and
4494 * their presentation time will be interpollated from the rtp timestamps. */
4495 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4496 GST_TIME_ARGS (src->base_time));
4498 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4501 /* chain to the peer pad */
4502 if (GST_PAD_IS_SINK (outpad))
4503 ret = gst_pad_chain (outpad, buf);
4505 ret = gst_pad_push (outpad, buf);
4508 /* combine all stream flows for the data transport */
4509 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4516 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4517 gst_rtsp_message_unset (message);
4522 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4523 ("Short message received, ignoring."));
4524 gst_rtsp_message_unset (message);
4529 static GstFlowReturn
4530 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4532 GstRTSPMessage message = { 0 };
4534 GstFlowReturn ret = GST_FLOW_OK;
4535 GTimeVal tv_timeout;
4538 /* get the next timeout interval */
4539 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4541 /* see if the timeout period expired */
4542 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4543 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4544 /* send keep-alive, only act on interrupt, a warning will be posted for
4546 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4548 /* get new timeout */
4549 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4552 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4553 tv_timeout.tv_sec, tv_timeout.tv_usec);
4555 /* protect the connection with the connection lock so that we can see when
4556 * we are finished doing server communication */
4558 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4559 &message, src->ptcp_timeout);
4563 GST_DEBUG_OBJECT (src, "we received a server message");
4565 case GST_RTSP_EINTR:
4566 /* we got interrupted this means we need to stop */
4568 case GST_RTSP_ETIMEOUT:
4569 /* no reply, send keep alive */
4570 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4571 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4575 /* go EOS when the server closed the connection */
4581 switch (message.type) {
4582 case GST_RTSP_MESSAGE_REQUEST:
4583 /* server sends us a request message, handle it */
4585 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4587 if (res == GST_RTSP_EEOF)
4590 goto handle_request_failed;
4592 case GST_RTSP_MESSAGE_RESPONSE:
4593 /* we ignore response messages */
4594 GST_DEBUG_OBJECT (src, "ignoring response message");
4596 gst_rtsp_message_dump (&message);
4598 case GST_RTSP_MESSAGE_DATA:
4599 GST_DEBUG_OBJECT (src, "got data message");
4600 ret = gst_rtspsrc_handle_data (src, &message);
4601 if (ret != GST_FLOW_OK)
4602 goto handle_data_failed;
4605 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4610 g_assert_not_reached ();
4615 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4616 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4617 ("The server closed the connection."));
4618 src->conninfo.connected = FALSE;
4619 gst_rtsp_message_unset (&message);
4620 return GST_FLOW_EOS;
4624 gst_rtsp_message_unset (&message);
4625 GST_DEBUG_OBJECT (src, "got interrupted");
4626 return GST_FLOW_FLUSHING;
4630 gchar *str = gst_rtsp_strresult (res);
4632 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4633 ("Could not receive message. (%s)", str));
4636 gst_rtsp_message_unset (&message);
4637 return GST_FLOW_ERROR;
4639 handle_request_failed:
4641 gchar *str = gst_rtsp_strresult (res);
4643 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4644 ("Could not handle server message. (%s)", str));
4646 gst_rtsp_message_unset (&message);
4647 return GST_FLOW_ERROR;
4651 GST_DEBUG_OBJECT (src, "could no handle data message");
4656 static GstFlowReturn
4657 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4660 GstRTSPMessage message = { 0 };
4664 GTimeVal tv_timeout;
4666 /* get the next timeout interval */
4667 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4669 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4670 (gint) tv_timeout.tv_sec);
4672 gst_rtsp_message_unset (&message);
4674 /* we should continue reading the TCP socket because the server might
4675 * send us requests. When the session timeout expires, we need to send a
4676 * keep-alive request to keep the session open. */
4677 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4678 &message, &tv_timeout);
4682 GST_DEBUG_OBJECT (src, "we received a server message");
4684 case GST_RTSP_EINTR:
4685 /* we got interrupted, see what we have to do */
4687 case GST_RTSP_ETIMEOUT:
4688 /* send keep-alive, ignore the result, a warning will be posted. */
4689 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4690 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4694 /* server closed the connection. not very fatal for UDP, reconnect and
4695 * see what happens. */
4696 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4697 ("The server closed the connection."));
4698 if (src->udp_reconnect) {
4700 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4707 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4709 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4710 ("Unhandled return value %d.", res));
4714 switch (message.type) {
4715 case GST_RTSP_MESSAGE_REQUEST:
4716 /* server sends us a request message, handle it */
4718 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4720 if (res == GST_RTSP_EEOF)
4723 goto handle_request_failed;
4725 case GST_RTSP_MESSAGE_RESPONSE:
4726 /* we ignore response and data messages */
4727 GST_DEBUG_OBJECT (src, "ignoring response message");
4729 gst_rtsp_message_dump (&message);
4730 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4731 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4732 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4733 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4734 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4741 case GST_RTSP_MESSAGE_DATA:
4742 /* we ignore response and data messages */
4743 GST_DEBUG_OBJECT (src, "ignoring data message");
4746 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4751 g_assert_not_reached ();
4753 /* we get here when the connection got interrupted */
4756 gst_rtsp_message_unset (&message);
4757 GST_DEBUG_OBJECT (src, "got interrupted");
4758 return GST_FLOW_FLUSHING;
4762 gchar *str = gst_rtsp_strresult (res);
4765 src->conninfo.connected = FALSE;
4766 if (res != GST_RTSP_EINTR) {
4767 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4768 ("Could not connect to server. (%s)", str));
4770 ret = GST_FLOW_ERROR;
4772 ret = GST_FLOW_FLUSHING;
4778 gchar *str = gst_rtsp_strresult (res);
4780 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4781 ("Could not receive message. (%s)", str));
4783 return GST_FLOW_ERROR;
4785 handle_request_failed:
4787 gchar *str = gst_rtsp_strresult (res);
4790 gst_rtsp_message_unset (&message);
4791 if (res != GST_RTSP_EINTR) {
4792 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4793 ("Could not handle server message. (%s)", str));
4795 ret = GST_FLOW_ERROR;
4797 ret = GST_FLOW_FLUSHING;
4803 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4804 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4805 ("The server closed the connection."));
4806 src->conninfo.connected = FALSE;
4807 gst_rtsp_message_unset (&message);
4808 return GST_FLOW_EOS;
4812 static GstRTSPResult
4813 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4815 GstRTSPResult res = GST_RTSP_OK;
4818 GST_DEBUG_OBJECT (src, "doing reconnect");
4820 GST_OBJECT_LOCK (src);
4821 /* only restart when the pads were not yet activated, else we were
4822 * streaming over UDP */
4823 restart = src->need_activate;
4824 GST_OBJECT_UNLOCK (src);
4826 /* no need to restart, we're done */
4830 /* we can try only TCP now */
4831 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
4833 /* close and cleanup our state */
4834 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
4837 /* see if we have TCP left to try. Also don't try TCP when we were configured
4839 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
4842 /* We post a warning message now to inform the user
4843 * that nothing happened. It's most likely a firewall thing. */
4844 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4845 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4846 "firewall is blocking it. Retrying using a TCP connection.",
4847 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4849 /* open new connection using tcp */
4850 if (gst_rtspsrc_open (src, async) < 0)
4853 /* start playback */
4854 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
4863 src->cur_protocols = 0;
4864 /* no transport possible, post an error and stop */
4865 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4866 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4867 "firewall is blocking it. No other protocols to try.",
4868 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4869 return GST_RTSP_ERROR;
4873 GST_DEBUG_OBJECT (src, "open failed");
4878 GST_DEBUG_OBJECT (src, "play failed");
4884 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
4888 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
4891 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
4894 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
4897 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
4905 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
4909 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
4912 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
4915 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
4918 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4926 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4930 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4933 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4936 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4939 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4947 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4951 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
4954 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4957 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4960 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4968 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4970 if (ret == GST_RTSP_OK)
4971 gst_rtspsrc_loop_complete_cmd (src, cmd);
4972 else if (ret == GST_RTSP_EINTR)
4973 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4975 gst_rtspsrc_loop_error_cmd (src, cmd);
4979 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
4982 gboolean flushed = FALSE;
4984 /* start new request */
4985 gst_rtspsrc_loop_start_cmd (src, cmd);
4987 GST_DEBUG_OBJECT (src, "sending cmd %d", cmd);
4989 GST_OBJECT_LOCK (src);
4990 old = src->pending_cmd;
4991 if (old == CMD_RECONNECT) {
4992 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
4993 cmd = CMD_RECONNECT;
4995 if (old != CMD_WAIT) {
4996 src->pending_cmd = CMD_WAIT;
4997 GST_OBJECT_UNLOCK (src);
4998 /* cancel previous request */
4999 GST_DEBUG_OBJECT (src, "cancel previous request %d", old);
5000 gst_rtspsrc_loop_cancel_cmd (src, old);
5001 GST_OBJECT_LOCK (src);
5003 src->pending_cmd = cmd;
5004 /* interrupt if allowed */
5005 if (src->busy_cmd & mask) {
5006 GST_DEBUG_OBJECT (src, "connection flush busy %d", src->busy_cmd);
5007 gst_rtspsrc_connection_flush (src, TRUE);
5010 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %d", src->busy_cmd);
5013 gst_task_start (src->task);
5014 GST_OBJECT_UNLOCK (src);
5020 gst_rtspsrc_loop (GstRTSPSrc * src)
5024 if (!src->conninfo.connection || !src->conninfo.connected)
5027 if (src->interleaved)
5028 ret = gst_rtspsrc_loop_interleaved (src);
5030 ret = gst_rtspsrc_loop_udp (src);
5032 if (ret != GST_FLOW_OK)
5040 GST_WARNING_OBJECT (src, "we are not connected");
5041 ret = GST_FLOW_FLUSHING;
5046 const gchar *reason = gst_flow_get_name (ret);
5048 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
5049 src->running = FALSE;
5050 if (ret == GST_FLOW_EOS) {
5051 /* perform EOS logic */
5052 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5053 gst_element_post_message (GST_ELEMENT_CAST (src),
5054 gst_message_new_segment_done (GST_OBJECT_CAST (src),
5055 src->segment.format, src->segment.position));
5056 gst_rtspsrc_push_event (src,
5057 gst_event_new_segment_done (src->segment.format,
5058 src->segment.position));
5060 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5062 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
5063 /* for fatal errors we post an error message, post the error before the
5064 * EOS so the app knows about the error first. */
5065 GST_ELEMENT_ERROR (src, STREAM, FAILED,
5066 ("Internal data flow error."),
5067 ("streaming task paused, reason %s (%d)", reason, ret));
5068 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5070 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
5075 #ifndef GST_DISABLE_GST_DEBUG
5076 static const gchar *
5077 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
5081 while (method != 0) {
5098 static const gchar *
5099 gst_rtspsrc_skip_lws (const gchar * s)
5101 while (g_ascii_isspace (*s))
5106 static const gchar *
5107 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
5109 while (s > start && g_ascii_isspace (*(s - 1)))
5114 static const gchar *
5115 gst_rtspsrc_skip_commas (const gchar * s)
5117 /* The grammar allows for multiple commas */
5118 while (g_ascii_isspace (*s) || *s == ',')
5123 static const gchar *
5124 gst_rtspsrc_skip_item (const gchar * s)
5126 gboolean quoted = FALSE;
5127 const gchar *start = s;
5129 /* A list item ends at the last non-whitespace character
5130 * before a comma which is not inside a quoted-string. Or at
5131 * the end of the string.
5137 if (*s == '\\' && *(s + 1))
5146 return gst_rtspsrc_unskip_lws (s, start);
5150 gst_rtsp_decode_quoted_string (gchar * quoted_string)
5154 src = quoted_string + 1;
5155 dst = quoted_string;
5156 while (*src && *src != '"') {
5157 if (*src == '\\' && *(src + 1))
5164 /* Extract the authentication tokens that the server provided for each method
5165 * into an array of structures and give those to the connection object.
5168 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
5169 const gchar * header, gboolean * stale)
5171 GSList *list = NULL, *iter;
5173 gchar *item, *eq, *name_end, *value;
5175 g_return_if_fail (stale != NULL);
5177 gst_rtsp_connection_clear_auth_params (conn);
5180 /* Parse a header whose content is described by RFC2616 as
5181 * "#something", where "something" does not itself contain commas,
5182 * except as part of quoted-strings, into a list of allocated strings.
5184 header = gst_rtspsrc_skip_commas (header);
5186 end = gst_rtspsrc_skip_item (header);
5187 list = g_slist_prepend (list, g_strndup (header, end - header));
5188 header = gst_rtspsrc_skip_commas (end);
5193 list = g_slist_reverse (list);
5194 for (iter = list; iter; iter = iter->next) {
5197 eq = strchr (item, '=');
5199 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
5200 if (name_end == item) {
5201 /* That's no good... */
5208 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
5210 gst_rtsp_decode_quoted_string (value);
5214 if (value && strcmp (item, "stale") == 0 && strcmp (value, "TRUE") == 0)
5216 gst_rtsp_connection_set_auth_param (conn, item, value);
5220 g_slist_free (list);
5223 /* Parse a WWW-Authenticate Response header and determine the
5224 * available authentication methods
5226 * This code should also cope with the fact that each WWW-Authenticate
5227 * header can contain multiple challenge methods + tokens
5229 * At the moment, for Basic auth, we just do a minimal check and don't
5230 * even parse out the realm */
5232 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
5233 GstRTSPConnection * conn, gboolean * stale)
5237 g_return_if_fail (hdr != NULL);
5238 g_return_if_fail (methods != NULL);
5239 g_return_if_fail (stale != NULL);
5241 /* Skip whitespace at the start of the string */
5242 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
5244 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
5245 *methods |= GST_RTSP_AUTH_BASIC;
5246 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
5247 *methods |= GST_RTSP_AUTH_DIGEST;
5248 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
5253 * gst_rtspsrc_setup_auth:
5254 * @src: the rtsp source
5256 * Configure a username and password and auth method on the
5257 * connection object based on a response we received from the
5260 * Currently, this requires that a username and password were supplied
5261 * in the uri. In the future, they may be requested on demand by sending
5262 * a message up the bus.
5264 * Returns: TRUE if authentication information could be set up correctly.
5267 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5271 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5272 GstRTSPAuthMethod method;
5273 GstRTSPResult auth_result;
5275 GstRTSPConnection *conn;
5277 gboolean stale = FALSE;
5279 conn = src->conninfo.connection;
5281 /* Identify the available auth methods and see if any are supported */
5282 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
5283 &hdr, 0) == GST_RTSP_OK) {
5284 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
5287 if (avail_methods == GST_RTSP_AUTH_NONE)
5288 goto no_auth_available;
5290 /* For digest auth, if the response indicates that the session
5291 * data are stale, we just update them in the connection object and
5292 * return TRUE to retry the request */
5294 src->tried_url_auth = FALSE;
5296 url = gst_rtsp_connection_get_url (conn);
5298 /* Do we have username and password available? */
5299 if (url != NULL && !src->tried_url_auth && url->user != NULL
5300 && url->passwd != NULL) {
5303 src->tried_url_auth = TRUE;
5304 GST_DEBUG_OBJECT (src,
5305 "Attempting authentication using credentials from the URL");
5307 user = src->user_id;
5308 pass = src->user_pw;
5309 GST_DEBUG_OBJECT (src,
5310 "Attempting authentication using credentials from the properties");
5313 /* FIXME: If the url didn't contain username and password or we tried them
5314 * already, request a username and passwd from the application via some kind
5315 * of credentials request message */
5317 /* If we don't have a username and passwd at this point, bail out. */
5318 if (user == NULL || pass == NULL)
5321 /* Try to configure for each available authentication method, strongest to
5323 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5324 /* Check if this method is available on the server */
5325 if ((method & avail_methods) == 0)
5328 /* Pass the credentials to the connection to try on the next request */
5329 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5330 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5331 * ignore it and end up retrying later */
5332 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5333 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5334 gst_rtsp_auth_method_to_string (method));
5339 if (method == GST_RTSP_AUTH_NONE)
5340 goto no_auth_available;
5346 /* Output an error indicating that we couldn't connect because there were
5347 * no supported authentication protocols */
5348 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5349 ("No supported authentication protocol was found"));
5354 /* We don't fire an error message, we just return FALSE and let the
5355 * normal NOT_AUTHORIZED error be propagated */
5360 static GstRTSPResult
5361 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5362 GstRTSPMessage * request, GstRTSPMessage * response,
5363 GstRTSPStatusCode * code)
5366 GstRTSPStatusCode thecode;
5367 gchar *content_base = NULL;
5371 if (!src->short_header)
5372 gst_rtsp_ext_list_before_send (src->extensions, request);
5374 GST_DEBUG_OBJECT (src, "sending message");
5377 gst_rtsp_message_dump (request);
5379 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
5383 gst_rtsp_connection_reset_timeout (conn);
5386 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
5391 gst_rtsp_message_dump (response);
5393 switch (response->type) {
5394 case GST_RTSP_MESSAGE_REQUEST:
5395 res = gst_rtspsrc_handle_request (src, conn, response);
5396 if (res == GST_RTSP_EEOF)
5399 goto handle_request_failed;
5401 case GST_RTSP_MESSAGE_RESPONSE:
5402 /* ok, a response is good */
5403 GST_DEBUG_OBJECT (src, "received response message");
5405 case GST_RTSP_MESSAGE_DATA:
5406 /* get next response */
5407 GST_DEBUG_OBJECT (src, "handle data response message");
5408 gst_rtspsrc_handle_data (src, response);
5411 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5416 thecode = response->type_data.response.code;
5418 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5420 /* if the caller wanted the result code, we store it. */
5424 /* If the request didn't succeed, bail out before doing any more */
5425 if (thecode != GST_RTSP_STS_OK)
5428 /* store new content base if any */
5429 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5432 g_free (src->content_base);
5433 src->content_base = g_strdup (content_base);
5435 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5442 gchar *str = gst_rtsp_strresult (res);
5444 if (res != GST_RTSP_EINTR) {
5445 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5446 ("Could not send message. (%s)", str));
5448 GST_WARNING_OBJECT (src, "send interrupted");
5457 GST_WARNING_OBJECT (src, "server closed connection");
5458 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5460 /* if reconnect succeeds, try again */
5462 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5466 /* only try once after reconnect, then fallthrough and error out */
5469 gchar *str = gst_rtsp_strresult (res);
5471 if (res != GST_RTSP_EINTR) {
5472 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5473 ("Could not receive message. (%s)", str));
5475 GST_WARNING_OBJECT (src, "receive interrupted");
5483 handle_request_failed:
5485 /* ERROR was posted */
5486 gst_rtsp_message_unset (response);
5491 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5492 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5493 ("The server closed the connection."));
5494 gst_rtsp_message_unset (response);
5501 * @src: the rtsp source
5502 * @conn: the connection to send on
5503 * @request: must point to a valid request
5504 * @response: must point to an empty #GstRTSPMessage
5505 * @code: an optional code result
5507 * send @request and retrieve the response in @response. optionally @code can be
5508 * non-NULL in which case it will contain the status code of the response.
5510 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5511 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5513 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5514 * @response message) if the response code was not 200 (OK).
5516 * If the attempt results in an authentication failure, then this will attempt
5517 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5520 * Returns: #GST_RTSP_OK if the processing was successful.
5522 static GstRTSPResult
5523 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5524 GstRTSPMessage * request, GstRTSPMessage * response,
5525 GstRTSPStatusCode * code)
5527 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5528 GstRTSPResult res = GST_RTSP_ERROR;
5531 GstRTSPMethod method = GST_RTSP_INVALID;
5537 /* make sure we don't loop forever */
5541 /* save method so we can disable it when the server complains */
5542 method = request->type_data.request.method;
5545 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5549 case GST_RTSP_STS_UNAUTHORIZED:
5550 if (gst_rtspsrc_setup_auth (src, response)) {
5551 /* Try the request/response again after configuring the auth info
5559 } while (retry == TRUE);
5561 /* If the user requested the code, let them handle errors, otherwise
5562 * post an error below */
5565 else if (int_code != GST_RTSP_STS_OK)
5566 goto error_response;
5573 GST_DEBUG_OBJECT (src, "got error %d", res);
5578 res = GST_RTSP_ERROR;
5580 switch (response->type_data.response.code) {
5581 case GST_RTSP_STS_NOT_FOUND:
5582 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5583 response->type_data.response.reason));
5585 case GST_RTSP_STS_MOVED_PERMANENTLY:
5586 case GST_RTSP_STS_MOVE_TEMPORARILY:
5588 gchar *new_location;
5589 GstRTSPLowerTrans transports;
5591 GST_DEBUG_OBJECT (src, "got redirection");
5592 /* if we don't have a Location Header, we must error */
5593 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5594 &new_location, 0) < 0)
5597 /* When we receive a redirect result, we go back to the INIT state after
5598 * parsing the new URI. The caller should do the needed steps to issue
5599 * a new setup when it detects this state change. */
5600 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5602 /* save current transports */
5603 if (src->conninfo.url)
5604 transports = src->conninfo.url->transports;
5606 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5608 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5610 /* set old transports */
5611 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5612 src->conninfo.url->transports = transports;
5614 src->need_redirect = TRUE;
5615 src->state = GST_RTSP_STATE_INIT;
5619 case GST_RTSP_STS_NOT_ACCEPTABLE:
5620 case GST_RTSP_STS_NOT_IMPLEMENTED:
5621 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5622 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5623 gst_rtsp_method_as_text (method));
5624 src->methods &= ~method;
5628 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5629 ("Got error response: %d (%s).", response->type_data.response.code,
5630 response->type_data.response.reason));
5633 /* if we return ERROR we should unset the response ourselves */
5634 if (res == GST_RTSP_ERROR)
5635 gst_rtsp_message_unset (response);
5641 static GstRTSPResult
5642 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5643 GstRTSPMessage * response, GstRTSPSrc * src)
5645 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5650 /* parse the response and collect all the supported methods. We need this
5651 * information so that we don't try to send an unsupported request to the
5655 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5657 GstRTSPHeaderField field;
5661 /* reset supported methods */
5664 /* Try Allow Header first */
5665 field = GST_RTSP_HDR_ALLOW;
5668 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5669 if (indx == 0 && !respoptions) {
5670 /* if no Allow header was found then try the Public header... */
5671 field = GST_RTSP_HDR_PUBLIC;
5672 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5677 src->methods |= gst_rtsp_options_from_text (respoptions);
5682 if (src->methods == 0) {
5683 /* neither Allow nor Public are required, assume the server supports
5684 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5686 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5687 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5689 /* always assume PLAY, FIXME, extensions should be able to override
5691 src->methods |= GST_RTSP_PLAY;
5692 /* also assume it will support Range */
5693 src->seekable = TRUE;
5695 /* we need describe and setup */
5696 if (!(src->methods & GST_RTSP_DESCRIBE))
5698 if (!(src->methods & GST_RTSP_SETUP))
5706 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5707 ("Server does not support DESCRIBE."));
5712 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5713 ("Server does not support SETUP."));
5718 /* masks to be kept in sync with the hardcoded protocol order of preference
5720 static guint protocol_masks[] = {
5721 GST_RTSP_LOWER_TRANS_UDP,
5722 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5723 GST_RTSP_LOWER_TRANS_TCP,
5727 static GstRTSPResult
5728 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5729 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
5733 gboolean add_udp_str;
5738 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5743 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5745 /* extension listed transports, use those */
5746 if (*transports != NULL)
5749 /* it's the default */
5750 add_udp_str = FALSE;
5752 /* the default RTSP transports */
5753 result = g_string_new ("RTP");
5756 case GST_RTSP_PROFILE_AVP:
5757 g_string_append (result, "/AVP");
5759 case GST_RTSP_PROFILE_SAVP:
5760 g_string_append (result, "/SAVP");
5762 case GST_RTSP_PROFILE_AVPF:
5763 g_string_append (result, "/AVPF");
5765 case GST_RTSP_PROFILE_SAVPF:
5766 g_string_append (result, "/SAVPF");
5772 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5773 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5775 g_string_append (result, "/UDP");
5776 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5777 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5778 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5779 /* we don't have to allocate any UDP ports yet, if the selected transport
5780 * turns out to be multicast we can create them and join the multicast
5781 * group indicated in the transport reply */
5783 g_string_append (result, "/UDP");
5784 g_string_append (result, ";multicast");
5785 if (src->next_port_num != 0) {
5786 if (src->client_port_range.max > 0 &&
5787 src->next_port_num >= src->client_port_range.max)
5790 g_string_append_printf (result, ";client_port=%d-%d",
5791 src->next_port_num, src->next_port_num + 1);
5793 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
5794 GST_DEBUG_OBJECT (src, "adding TCP");
5796 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
5798 *transports = g_string_free (result, FALSE);
5800 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5807 GST_ERROR ("extension gave error %d", res);
5812 GST_ERROR ("no more ports available");
5813 return GST_RTSP_ERROR;
5817 static GstRTSPResult
5818 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5819 gint orig_rtpport, gint orig_rtcpport)
5822 gint nr_udp, nr_int;
5824 gint rtpport = 0, rtcpport = 0;
5827 src = stream->parent;
5829 /* find number of placeholders first */
5830 if (strstr (*transports, "%%i2"))
5832 else if (strstr (*transports, "%%i1"))
5837 if (strstr (*transports, "%%u2"))
5839 else if (strstr (*transports, "%%u1"))
5844 if (nr_udp == 0 && nr_int == 0)
5848 if (!orig_rtpport || !orig_rtcpport) {
5849 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5852 rtpport = orig_rtpport;
5853 rtcpport = orig_rtcpport;
5857 str = g_string_new ("");
5859 while ((next = strstr (p, "%%"))) {
5860 g_string_append_len (str, p, next - p);
5861 if (next[2] == 'u') {
5863 g_string_append_printf (str, "%d", rtpport);
5864 else if (next[3] == '2')
5865 g_string_append_printf (str, "%d", rtcpport);
5867 if (next[2] == 'i') {
5869 g_string_append_printf (str, "%d", src->free_channel);
5870 else if (next[3] == '2')
5871 g_string_append_printf (str, "%d", src->free_channel + 1);
5876 /* append final part */
5877 g_string_append (str, p);
5879 g_free (*transports);
5880 *transports = g_string_free (str, FALSE);
5888 GST_ERROR ("failed to allocate udp ports");
5889 return GST_RTSP_ERROR;
5894 enc_key_length_from_cipher_name (const gchar * cipher)
5896 if (g_strcmp0 (cipher, "aes-128-icm") == 0)
5897 return AES_128_KEY_LEN;
5898 else if (g_strcmp0 (cipher, "aes-256-icm") == 0)
5899 return AES_256_KEY_LEN;
5901 GST_ERROR ("encryption algorithm '%s' not supported", cipher);
5907 auth_key_length_from_auth_name (const gchar * auth)
5909 if (g_strcmp0 (auth, "hmac-sha1-32") == 0)
5910 return HMAC_32_KEY_LEN;
5911 else if (g_strcmp0 (auth, "hmac-sha1-80") == 0)
5912 return HMAC_80_KEY_LEN;
5914 GST_ERROR ("authentication algorithm '%s' not supported", auth);
5920 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
5922 GstCaps *caps = NULL;
5924 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
5928 GST_DEBUG_OBJECT (src, "SRTP parameters received");
5934 default_srtcp_params (void)
5942 /* create a random key */
5943 key_data = g_malloc (KEY_SIZE);
5944 for (i = 0; i < KEY_SIZE; i += 4)
5945 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
5947 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
5949 caps = gst_caps_new_simple ("application/x-srtp",
5950 "srtp-key", GST_TYPE_BUFFER, buf,
5951 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
5952 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
5958 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
5961 gchar *result, *base64;
5964 GstMIKEYMessage *msg;
5965 GstMIKEYPayload *payload, *pkd;
5971 const gchar *srtcpcipher, *srtcpauth;
5973 stream->srtcpparams = signal_get_srtcp_params (src, stream);
5974 if (stream->srtcpparams == NULL)
5975 stream->srtcpparams = default_srtcp_params ();
5977 s = gst_caps_get_structure (stream->srtcpparams, 0);
5979 srtcpcipher = gst_structure_get_string (s, "srtcp-cipher");
5980 srtcpauth = gst_structure_get_string (s, "srtcp-auth");
5981 val = gst_structure_get_value (s, "srtp-key");
5983 if (srtcpcipher == NULL || srtcpauth == NULL || val == NULL) {
5984 GST_ERROR_OBJECT (src, "could not find the right SRTP parameters in caps");
5988 srtpkey = gst_value_get_buffer (val);
5990 msg = gst_mikey_message_new ();
5991 /* unencrypted MIKEY message, we send this over TLS so this is allowed */
5992 gst_mikey_message_set_info (msg, GST_MIKEY_VERSION, GST_MIKEY_TYPE_PSK_INIT,
5993 FALSE, GST_MIKEY_PRF_MIKEY_1, g_random_int (), GST_MIKEY_MAP_TYPE_SRTP);
5994 /* add policy '0' for our SSRC */
5995 gst_mikey_message_add_cs_srtp (msg, 0, stream->send_ssrc, 0);
5996 /* timestamp is now */
5997 gst_mikey_message_add_t_now_ntp_utc (msg);
5998 /* add some random data */
5999 gst_mikey_message_add_rand_len (msg, 16);
6001 /* the policy '0' is SRTP */
6002 payload = gst_mikey_payload_new (GST_MIKEY_PT_SP);
6003 gst_mikey_payload_sp_set (payload, 0, GST_MIKEY_SEC_PROTO_SRTP);
6005 /* only AES-CM is supported */
6007 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_ALG, 1, &byte);
6008 /* encryption key length */
6009 byte = enc_key_length_from_cipher_name (srtcpcipher);
6010 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_KEY_LEN, 1,
6012 /* only HMAC-SHA1 */
6013 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_ALG, 1,
6015 /* authentication key length */
6016 byte = auth_key_length_from_auth_name (srtcpauth);
6017 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_KEY_LEN, 1,
6019 /* we enable encryption on RTP and RTCP */
6020 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_ENC, 1,
6022 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTCP_ENC, 1,
6024 /* we enable authentication on RTP and RTCP */
6025 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_AUTH, 1,
6027 gst_mikey_message_add_payload (msg, payload);
6029 /* make unencrypted KEMAC */
6030 payload = gst_mikey_payload_new (GST_MIKEY_PT_KEMAC);
6031 gst_mikey_payload_kemac_set (payload, GST_MIKEY_ENC_NULL, GST_MIKEY_MAC_NULL);
6032 /* add the key in KEMAC */
6033 pkd = gst_mikey_payload_new (GST_MIKEY_PT_KEY_DATA);
6034 gst_buffer_map (srtpkey, &info, GST_MAP_READ);
6035 gst_mikey_payload_key_data_set_key (pkd, GST_MIKEY_KD_TEK, info.size,
6037 gst_buffer_unmap (srtpkey, &info);
6038 gst_mikey_payload_kemac_add_sub (payload, pkd);
6039 gst_mikey_message_add_payload (msg, payload);
6041 /* now serialize this to bytes */
6042 bytes = gst_mikey_message_to_bytes (msg, NULL, NULL);
6043 gst_mikey_message_unref (msg);
6044 /* and make it into base64 */
6045 data = g_bytes_get_data (bytes, &size);
6046 base64 = g_base64_encode (data, size);
6047 g_bytes_unref (bytes);
6049 result = g_strdup_printf ("prot=mikey;uri=\"%s\";data=\"%s\"",
6050 stream->conninfo.location, base64);
6057 /* Perform the SETUP request for all the streams.
6059 * We ask the server for a specific transport, which initially includes all the
6060 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
6061 * two local UDP ports that we send to the server.
6063 * Once the server replied with a transport, we configure the other streams
6064 * with the same transport.
6066 * This function will also configure the stream for the selected transport,
6067 * which basically means creating the pipeline.
6069 static GstRTSPResult
6070 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
6073 GstRTSPResult res = GST_RTSP_ERROR;
6074 GstRTSPMessage request = { 0 };
6075 GstRTSPMessage response = { 0 };
6076 GstRTSPStream *stream = NULL;
6077 GstRTSPLowerTrans protocols;
6078 GstRTSPStatusCode code;
6079 gboolean unsupported_real = FALSE;
6080 gint rtpport, rtcpport;
6084 if (src->conninfo.connection) {
6085 url = gst_rtsp_connection_get_url (src->conninfo.connection);
6086 /* we initially allow all configured lower transports. based on the URL
6087 * transports and the replies from the server we narrow them down. */
6088 protocols = url->transports & src->cur_protocols;
6091 protocols = src->cur_protocols;
6097 /* reset some state */
6098 src->free_channel = 0;
6099 src->interleaved = FALSE;
6100 src->need_activate = FALSE;
6101 /* keep track of next port number, 0 is random */
6102 src->next_port_num = src->client_port_range.min;
6103 rtpport = rtcpport = 0;
6105 if (G_UNLIKELY (src->streams == NULL))
6108 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6109 GstRTSPConnection *conn;
6116 stream = (GstRTSPStream *) walk->data;
6118 caps = stream_get_caps_for_pt (stream, stream->default_pt);
6120 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
6124 if (stream->skipped) {
6125 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
6129 /* see if we need to configure this stream */
6130 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
6131 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
6136 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
6137 stream->id, caps, &selected);
6139 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
6143 /* merge/overwrite global caps */
6148 s = gst_caps_get_structure (caps, 0);
6150 num = gst_structure_n_fields (src->props);
6151 for (j = 0; j < num; j++) {
6155 name = gst_structure_nth_field_name (src->props, j);
6156 val = gst_structure_get_value (src->props, name);
6157 gst_structure_set_value (s, name, val);
6159 GST_DEBUG_OBJECT (src, "copied %s", name);
6163 /* skip setup if we have no URL for it */
6164 if (stream->conninfo.location == NULL) {
6165 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
6169 if (src->conninfo.connection == NULL) {
6170 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
6171 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
6174 conn = stream->conninfo.connection;
6176 conn = src->conninfo.connection;
6178 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
6179 stream->conninfo.location);
6181 /* if we have a multicast connection, only suggest multicast from now on */
6182 if (stream->is_multicast)
6183 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
6186 /* first selectable protocol */
6187 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6189 if (!protocol_masks[mask])
6193 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
6194 protocol_masks[mask]);
6195 /* create a string with first transport in line */
6197 res = gst_rtspsrc_create_transports_string (src,
6198 protocols & protocol_masks[mask], stream->profile, &transports);
6199 if (res < 0 || transports == NULL)
6200 goto setup_transport_failed;
6202 if (strlen (transports) == 0) {
6203 g_free (transports);
6204 GST_DEBUG_OBJECT (src, "no transports found");
6209 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
6211 /* replace placeholders with real values, this function will optionally
6212 * allocate UDP ports and other info needed to execute the setup request */
6213 res = gst_rtspsrc_prepare_transports (stream, &transports,
6214 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
6216 g_free (transports);
6217 goto setup_transport_failed;
6220 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
6222 /* create SETUP request */
6224 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
6225 stream->conninfo.location);
6227 g_free (transports);
6228 goto create_request_failed;
6231 /* select transport */
6232 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
6235 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
6236 stream->profile == GST_RTSP_PROFILE_SAVPF) {
6237 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
6238 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
6241 /* if the user wants a non default RTP packet size we add the blocksize
6243 if (src->rtp_blocksize > 0) {
6244 hval = g_strdup_printf ("%d", src->rtp_blocksize);
6245 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
6249 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
6252 /* handle the code ourselves */
6253 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
6257 case GST_RTSP_STS_OK:
6259 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
6260 gst_rtsp_message_unset (&request);
6261 gst_rtsp_message_unset (&response);
6262 /* cleanup of leftover transport */
6263 gst_rtspsrc_stream_free_udp (stream);
6264 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
6265 * we might be in this case */
6266 if (stream->container && rtpport && rtcpport && !retry) {
6267 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
6272 /* this transport did not go down well, but we may have others to try
6273 * that we did not send yet, try those and only give up then
6274 * but not without checking for lost cause/extension so we can
6275 * post a nicer/more useful error message later */
6276 if (!unsupported_real)
6277 unsupported_real = stream->is_real;
6278 /* select next available protocol, give up on this stream if none */
6280 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6282 if (!protocol_masks[mask] || unsupported_real)
6287 /* cleanup of leftover transport and move to the next stream */
6288 gst_rtspsrc_stream_free_udp (stream);
6289 goto response_error;
6292 /* parse response transport */
6294 gchar *resptrans = NULL;
6295 GstRTSPTransport transport = { 0 };
6297 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
6300 gst_rtspsrc_stream_free_udp (stream);
6304 /* parse transport, go to next stream on parse error */
6305 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6306 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6310 /* update allowed transports for other streams. once the transport of
6311 * one stream has been determined, we make sure that all other streams
6312 * are configured in the same way */
6313 switch (transport.lower_transport) {
6314 case GST_RTSP_LOWER_TRANS_TCP:
6315 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6316 protocols = GST_RTSP_LOWER_TRANS_TCP;
6317 src->interleaved = TRUE;
6318 /* update free channels */
6320 MAX (transport.interleaved.min, src->free_channel);
6322 MAX (transport.interleaved.max, src->free_channel);
6323 src->free_channel++;
6325 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6326 /* only allow multicast for other streams */
6327 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6328 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6329 /* if the server selected our ports, increment our counters so that
6330 * we select a new port later */
6331 if (src->next_port_num == transport.port.min &&
6332 src->next_port_num + 1 == transport.port.max) {
6333 src->next_port_num += 2;
6336 case GST_RTSP_LOWER_TRANS_UDP:
6337 /* only allow unicast for other streams */
6338 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6339 protocols = GST_RTSP_LOWER_TRANS_UDP;
6342 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6343 transport.lower_transport);
6347 if (!stream->container || (!src->interleaved && !retry)) {
6348 /* now configure the stream with the selected transport */
6349 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6350 GST_DEBUG_OBJECT (src,
6351 "could not configure stream %p transport, skipping stream",
6354 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
6355 /* retain the first allocated UDP port pair */
6356 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
6357 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
6360 /* we need to activate at least one streams when we detect activity */
6361 src->need_activate = TRUE;
6363 /* stream is setup now */
6364 stream->setup = TRUE;
6369 GstRTSPStream *sskip;
6371 skip = g_list_next (skip);
6375 sskip = (GstRTSPStream *) skip->data;
6377 /* skip all streams with the same control url */
6378 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
6379 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
6380 sskip, sskip->conninfo.location);
6381 sskip->skipped = TRUE;
6386 /* clean up our transport struct */
6387 gst_rtsp_transport_init (&transport);
6388 /* clean up used RTSP messages */
6389 gst_rtsp_message_unset (&request);
6390 gst_rtsp_message_unset (&response);
6394 /* store the transport protocol that was configured */
6395 src->cur_protocols = protocols;
6397 gst_rtsp_ext_list_stream_select (src->extensions, url);
6399 /* if there is nothing to activate, error out */
6400 if (!src->need_activate)
6401 goto nothing_to_activate;
6408 /* no transport possible, post an error and stop */
6409 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6410 ("Could not connect to server, no protocols left"));
6411 return GST_RTSP_ERROR;
6415 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6416 ("SDP contains no streams"));
6417 return GST_RTSP_ERROR;
6419 create_request_failed:
6421 gchar *str = gst_rtsp_strresult (res);
6423 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6424 ("Could not create request. (%s)", str));
6428 setup_transport_failed:
6430 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6431 ("Could not setup transport."));
6432 res = GST_RTSP_ERROR;
6437 const gchar *str = gst_rtsp_status_as_text (code);
6439 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6440 ("Error (%d): %s", code, GST_STR_NULL (str)));
6441 res = GST_RTSP_ERROR;
6446 gchar *str = gst_rtsp_strresult (res);
6448 if (res != GST_RTSP_EINTR) {
6449 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6450 ("Could not send message. (%s)", str));
6452 GST_WARNING_OBJECT (src, "send interrupted");
6459 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6460 ("Server did not select transport."));
6461 res = GST_RTSP_ERROR;
6464 nothing_to_activate:
6466 /* none of the available error codes is really right .. */
6467 if (unsupported_real) {
6468 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6469 (_("No supported stream was found. You might need to install a "
6470 "GStreamer RTSP extension plugin for Real media streams.")),
6473 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6474 (_("No supported stream was found. You might need to allow "
6475 "more transport protocols or may otherwise be missing "
6476 "the right GStreamer RTSP extension plugin.")), (NULL));
6478 return GST_RTSP_ERROR;
6482 gst_rtsp_message_unset (&request);
6483 gst_rtsp_message_unset (&response);
6489 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
6490 GstSegment * segment)
6493 GstRTSPTimeRange *therange;
6496 gst_rtsp_range_free (src->range);
6498 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
6499 GST_DEBUG_OBJECT (src, "parsed range %s", range);
6500 src->range = therange;
6502 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6504 gst_segment_init (segment, GST_FORMAT_TIME);
6508 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6509 therange->min.type, therange->min.seconds, therange->max.type,
6510 therange->max.seconds);
6512 if (therange->min.type == GST_RTSP_TIME_NOW)
6514 else if (therange->min.type == GST_RTSP_TIME_END)
6517 seconds = therange->min.seconds * GST_SECOND;
6519 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6520 GST_TIME_ARGS (seconds));
6522 /* we need to start playback without clipping from the position reported by
6524 segment->start = seconds;
6525 segment->position = seconds;
6527 if (therange->max.type == GST_RTSP_TIME_NOW)
6529 else if (therange->max.type == GST_RTSP_TIME_END)
6532 seconds = therange->max.seconds * GST_SECOND;
6534 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6535 GST_TIME_ARGS (seconds));
6537 /* live (WMS) server might send overflowed large max as its idea of infinity,
6538 * compensate to prevent problems later on */
6539 if (seconds != -1 && seconds < 0) {
6541 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6544 /* live (WMS) might send min == max, which is not worth recording */
6545 if (segment->duration == -1 && seconds == segment->start)
6548 /* don't change duration with unknown value, we might have a valid value
6549 * there that we want to keep. */
6551 segment->duration = seconds;
6556 /* Parse clock profived by the server with following syntax:
6558 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6561 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6563 gboolean res = FALSE;
6565 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6566 gchar **fields = NULL, **parts = NULL;
6567 gchar *remote_ip, *str;
6569 GstClockTime base_time;
6572 fields = g_strsplit (gstclock, " ", 0);
6574 /* wrapped clock, not very interesting for now */
6575 if (fields[1] == NULL)
6578 /* remote IP address and port */
6579 if ((str = fields[2]) == NULL)
6582 parts = g_strsplit (str, ":", 0);
6584 if ((remote_ip = parts[0]) == NULL)
6587 if ((str = parts[1]) == NULL)
6595 if ((str = fields[3]) == NULL)
6598 base_time = g_ascii_strtoull (str, NULL, 10);
6601 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
6604 if (src->provided_clock)
6605 gst_object_unref (src->provided_clock);
6606 src->provided_clock = netclock;
6608 gst_element_post_message (GST_ELEMENT_CAST (src),
6609 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
6610 src->provided_clock, TRUE));
6614 g_strfreev (fields);
6620 /* must be called with the RTSP state lock */
6621 static GstRTSPResult
6622 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
6628 /* prepare global stream caps properties */
6630 gst_structure_remove_all_fields (src->props);
6632 src->props = gst_structure_new_empty ("RTSPProperties");
6635 gst_sdp_message_dump (sdp);
6637 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
6639 /* let the app inspect and change the SDP */
6640 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6642 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6644 /* parse range for duration reporting. */
6649 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6653 /* keep track of the range and configure it in the segment */
6654 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6658 /* parse clock information. This is GStreamer specific, a server can tell the
6659 * client what clock it is using and wrap that in a network clock. The
6660 * advantage of that is that we can slave to it. */
6662 const gchar *gstclock;
6665 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6666 if (gstclock == NULL)
6669 /* parse the clock and expose it in the provide_clock method */
6670 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6674 /* try to find a global control attribute. Note that a '*' means that we should
6675 * do aggregate control with the current url (so we don't do anything and
6676 * leave the current connection as is) */
6678 const gchar *control;
6681 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6682 if (control == NULL)
6685 /* only take fully qualified urls */
6686 if (g_str_has_prefix (control, "rtsp://"))
6690 g_free (src->conninfo.location);
6691 src->conninfo.location = g_strdup (control);
6692 /* make a connection for this, if there was a connection already, nothing
6694 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6695 GST_ERROR_OBJECT (src, "could not connect");
6698 /* we need to keep the control url separate from the connection url because
6699 * the rules for constructing the media control url need it */
6700 g_free (src->control);
6701 src->control = g_strdup (control);
6704 /* create streams */
6705 n_streams = gst_sdp_message_medias_len (sdp);
6706 for (i = 0; i < n_streams; i++) {
6707 gst_rtspsrc_create_stream (src, sdp, i);
6710 src->state = GST_RTSP_STATE_INIT;
6713 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6716 /* reset our state */
6717 src->need_range = TRUE;
6720 src->state = GST_RTSP_STATE_READY;
6727 GST_ERROR_OBJECT (src, "setup failed");
6728 gst_rtspsrc_cleanup (src);
6733 static GstRTSPResult
6734 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6738 GstRTSPMessage request = { 0 };
6739 GstRTSPMessage response = { 0 };
6742 gchar *respcont = NULL;
6745 src->need_redirect = FALSE;
6747 /* can't continue without a valid url */
6748 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6749 res = GST_RTSP_EINVAL;
6752 src->tried_url_auth = FALSE;
6754 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6755 goto connect_failed;
6757 /* create OPTIONS */
6758 GST_DEBUG_OBJECT (src, "create options...");
6760 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
6761 src->conninfo.url_str);
6763 goto create_request_failed;
6766 GST_DEBUG_OBJECT (src, "send options...");
6769 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6772 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6777 if (!gst_rtspsrc_parse_methods (src, &response))
6780 /* create DESCRIBE */
6781 GST_DEBUG_OBJECT (src, "create describe...");
6783 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
6784 src->conninfo.url_str);
6786 goto create_request_failed;
6788 /* we only accept SDP for now */
6789 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
6793 GST_DEBUG_OBJECT (src, "send describe...");
6796 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
6799 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6803 /* we only perform redirect for the describe, currently */
6804 if (src->need_redirect) {
6805 /* close connection, we don't have to send a TEARDOWN yet, ignore the
6807 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6809 gst_rtsp_message_unset (&request);
6810 gst_rtsp_message_unset (&response);
6816 /* it could be that the DESCRIBE method was not implemented */
6817 if (!src->methods & GST_RTSP_DESCRIBE)
6820 /* check if reply is SDP */
6821 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
6823 /* could not be set but since the request returned OK, we assume it
6824 * was SDP, else check it. */
6826 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
6827 goto wrong_content_type;
6830 /* get message body and parse as SDP */
6831 gst_rtsp_message_get_body (&response, &data, &size);
6832 if (data == NULL || size == 0)
6835 GST_DEBUG_OBJECT (src, "parse SDP...");
6836 gst_sdp_message_new (sdp);
6837 gst_sdp_message_parse_buffer (data, size, *sdp);
6839 /* clean up any messages */
6840 gst_rtsp_message_unset (&request);
6841 gst_rtsp_message_unset (&response);
6848 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
6849 ("No valid RTSP URL was provided"));
6854 gchar *str = gst_rtsp_strresult (res);
6856 if (res != GST_RTSP_EINTR) {
6857 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6858 ("Failed to connect. (%s)", str));
6860 GST_WARNING_OBJECT (src, "connect interrupted");
6865 create_request_failed:
6867 gchar *str = gst_rtsp_strresult (res);
6869 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6870 ("Could not create request. (%s)", str));
6876 /* Don't post a message - the rtsp_send method will have
6877 * taken care of it because we passed NULL for the response code */
6882 /* error was posted */
6883 res = GST_RTSP_ERROR;
6888 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6889 ("Server does not support SDP, got %s.", respcont));
6890 res = GST_RTSP_ERROR;
6895 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6896 ("Server can not provide an SDP."));
6897 res = GST_RTSP_ERROR;
6902 if (src->conninfo.connection) {
6903 GST_DEBUG_OBJECT (src, "free connection");
6904 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6906 gst_rtsp_message_unset (&request);
6907 gst_rtsp_message_unset (&response);
6912 static GstRTSPResult
6913 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
6918 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
6920 if (src->sdp == NULL) {
6921 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
6925 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
6930 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
6937 GST_WARNING_OBJECT (src, "can't get sdp");
6938 src->open_error = TRUE;
6943 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
6944 src->open_error = TRUE;
6949 static GstRTSPResult
6950 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
6952 GstRTSPMessage request = { 0 };
6953 GstRTSPMessage response = { 0 };
6954 GstRTSPResult res = GST_RTSP_OK;
6956 const gchar *control;
6958 GST_DEBUG_OBJECT (src, "TEARDOWN...");
6960 gst_rtspsrc_set_state (src, GST_STATE_READY);
6962 if (src->state < GST_RTSP_STATE_READY) {
6963 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
6970 /* construct a control url */
6971 control = get_aggregate_control (src);
6973 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
6976 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6977 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6978 const gchar *setup_url;
6979 GstRTSPConnInfo *info;
6981 /* try aggregate control first but do non-aggregate control otherwise */
6983 setup_url = control;
6984 else if ((setup_url = stream->conninfo.location) == NULL)
6987 if (src->conninfo.connection) {
6988 info = &src->conninfo;
6989 } else if (stream->conninfo.connection) {
6990 info = &stream->conninfo;
6994 if (!info->connected)
6999 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
7001 goto create_request_failed;
7004 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
7007 gst_rtspsrc_send (src, info->connection, &request, &response,
7011 /* FIXME, parse result? */
7012 gst_rtsp_message_unset (&request);
7013 gst_rtsp_message_unset (&response);
7016 /* early exit when we did aggregate control */
7022 /* close connections */
7023 GST_DEBUG_OBJECT (src, "closing connection...");
7024 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7025 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7026 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7027 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
7031 gst_rtspsrc_cleanup (src);
7033 src->state = GST_RTSP_STATE_INVALID;
7036 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
7041 create_request_failed:
7043 gchar *str = gst_rtsp_strresult (res);
7045 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7046 ("Could not create request. (%s)", str));
7052 gchar *str = gst_rtsp_strresult (res);
7054 gst_rtsp_message_unset (&request);
7055 if (res != GST_RTSP_EINTR) {
7056 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7057 ("Could not send message. (%s)", str));
7059 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
7066 GST_DEBUG_OBJECT (src,
7067 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
7072 /* RTP-Info is of the format:
7074 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
7076 * rtptime corresponds to the timestamp for the NPT time given in the header
7077 * seqbase corresponds to the next sequence number we received. This number
7078 * indicates the first seqnum after the seek and should be used to discard
7079 * packets that are from before the seek.
7082 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
7087 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
7089 infos = g_strsplit (rtpinfo, ",", 0);
7090 for (i = 0; infos[i]; i++) {
7092 GstRTSPStream *stream;
7096 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
7098 /* init values, types of seqbase and timebase are bigger than needed so we
7099 * can store -1 as uninitialized values */
7104 /* parse url, find stream for url.
7105 * parse seq and rtptime. The seq number should be configured in the rtp
7106 * depayloader or session manager to detect gaps. Same for the rtptime, it
7107 * should be used to create an initial time newsegment. */
7108 fields = g_strsplit (infos[i], ";", 0);
7109 for (j = 0; fields[j]; j++) {
7110 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
7111 /* remove leading whitespace */
7112 fields[j] = g_strchug (fields[j]);
7113 if (g_str_has_prefix (fields[j], "url=")) {
7114 /* get the url and the stream */
7116 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
7117 } else if (g_str_has_prefix (fields[j], "seq=")) {
7118 seqbase = atoi (fields[j] + 4);
7119 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
7120 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
7123 g_strfreev (fields);
7124 /* now we need to store the values for the caps of the stream */
7125 if (stream != NULL) {
7126 GST_DEBUG_OBJECT (src,
7127 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
7128 stream, seqbase, timebase);
7130 /* we have a stream, configure detected params */
7131 stream->seqbase = seqbase;
7132 stream->timebase = timebase;
7141 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
7146 interval = strtoul (rtcp, NULL, 10);
7147 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
7152 interval *= GST_MSECOND;
7154 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7155 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7157 /* already (optionally) retrieved this when configuring manager */
7158 if (stream->session) {
7159 GObject *rtpsession = stream->session;
7161 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
7163 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
7167 /* now it happens that (Xenon) server sending this may also provide bogus
7168 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
7169 * and just use RTP-Info to sync */
7171 GObjectClass *klass;
7173 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
7174 if (g_object_class_find_property (klass, "rtcp-sync")) {
7175 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
7176 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
7182 gst_rtspsrc_get_float (const gchar * dstr)
7184 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7186 /* canonicalise floating point string so we can handle float strings
7187 * in the form "24.930" or "24,930" irrespective of the current locale */
7188 g_strlcpy (s, dstr, sizeof (s));
7189 g_strdelimit (s, ",", '.');
7190 return g_ascii_strtod (s, NULL);
7194 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
7196 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7198 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
7199 g_strlcpy (val_str, "now", sizeof (val_str));
7201 if (segment->position == 0) {
7202 g_strlcpy (val_str, "0", sizeof (val_str));
7204 g_ascii_dtostr (val_str, sizeof (val_str),
7205 ((gdouble) segment->position) / GST_SECOND);
7208 return g_strdup_printf ("npt=%s-", val_str);
7212 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
7216 stream->timebase = -1;
7217 stream->seqbase = -1;
7219 len = stream->ptmap->len;
7220 for (i = 0; i < len; i++) {
7221 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
7224 if (item->caps == NULL)
7227 item->caps = gst_caps_make_writable (item->caps);
7228 s = gst_caps_get_structure (item->caps, 0);
7229 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
7233 static GstRTSPResult
7234 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
7236 GstRTSPResult res = GST_RTSP_OK;
7238 if (src->state < GST_RTSP_STATE_READY) {
7239 res = GST_RTSP_ERROR;
7240 if (src->open_error) {
7241 GST_DEBUG_OBJECT (src, "the stream was in error");
7245 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
7247 if ((res = gst_rtspsrc_open (src, async)) < 0) {
7248 GST_DEBUG_OBJECT (src, "failed to open stream");
7257 static GstRTSPResult
7258 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
7260 GstRTSPMessage request = { 0 };
7261 GstRTSPMessage response = { 0 };
7262 GstRTSPResult res = GST_RTSP_OK;
7266 const gchar *control;
7268 GST_DEBUG_OBJECT (src, "PLAY...");
7270 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7273 if (!(src->methods & GST_RTSP_PLAY))
7276 if (src->state == GST_RTSP_STATE_PLAYING)
7279 if (!src->conninfo.connection || !src->conninfo.connected)
7282 /* send some dummy packets before we activate the receive in the
7284 gst_rtspsrc_send_dummy_packets (src);
7286 /* require new SR packets */
7288 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
7290 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
7292 /* construct a control url */
7293 control = get_aggregate_control (src);
7295 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7296 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7297 const gchar *setup_url;
7298 GstRTSPConnection *conn;
7300 /* try aggregate control first but do non-aggregate control otherwise */
7302 setup_url = control;
7303 else if ((setup_url = stream->conninfo.location) == NULL)
7306 if (src->conninfo.connection) {
7307 conn = src->conninfo.connection;
7308 } else if (stream->conninfo.connection) {
7309 conn = stream->conninfo.connection;
7315 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
7317 goto create_request_failed;
7319 if (src->need_range) {
7320 hval = gen_range_header (src, segment);
7322 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
7324 /* store the newsegment event so it can be sent from the streaming thread. */
7325 if (src->start_segment)
7326 gst_event_unref (src->start_segment);
7327 src->start_segment = gst_event_new_segment (&src->segment);
7330 if (segment->rate != 1.0) {
7331 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
7333 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
7335 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
7337 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
7341 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
7343 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7346 /* seek may have silently failed as it is not supported */
7347 if (!(src->methods & GST_RTSP_PLAY)) {
7348 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
7349 /* obviously it is supported as we made it here */
7350 src->methods |= GST_RTSP_PLAY;
7351 src->seekable = FALSE;
7352 /* but there is nothing to parse in the response,
7353 * so convey we have no idea and not to expect anything particular */
7354 clear_rtp_base (src, stream);
7358 /* need to do for all streams */
7359 for (run = src->streams; run; run = g_list_next (run))
7360 clear_rtp_base (src, (GstRTSPStream *) run->data);
7362 /* NOTE the above also disables npt based eos detection */
7363 /* and below forces position to 0,
7364 * which is visible feedback we lost the plot */
7365 segment->start = segment->position = src->last_pos;
7368 gst_rtsp_message_unset (&request);
7370 /* parse RTP npt field. This is the current position in the stream (Normal
7371 * Play Time) and should be put in the NEWSEGMENT position field. */
7372 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
7374 gst_rtspsrc_parse_range (src, hval, segment);
7376 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
7377 segment->rate = 1.0;
7379 /* parse Speed header. This is the intended playback rate of the stream
7380 * and should be put in the NEWSEGMENT rate field. */
7381 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
7382 0) == GST_RTSP_OK) {
7383 segment->rate = gst_rtspsrc_get_float (hval);
7384 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
7385 &hval, 0) == GST_RTSP_OK) {
7386 segment->rate = gst_rtspsrc_get_float (hval);
7389 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
7390 * for the RTP packets. If this is not present, we assume all starts from 0...
7391 * This is info for the RTP session manager that we pass to it in caps. */
7393 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
7394 &hval, hval_idx++) == GST_RTSP_OK)
7395 gst_rtspsrc_parse_rtpinfo (src, hval);
7397 /* some servers indicate RTCP parameters in PLAY response,
7398 * rather than properly in SDP */
7399 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
7400 &hval, 0) == GST_RTSP_OK)
7401 gst_rtspsrc_handle_rtcp_interval (src, hval);
7403 gst_rtsp_message_unset (&response);
7405 /* early exit when we did aggregate control */
7409 /* configure the caps of the streams after we parsed all headers. Only reset
7410 * the manager object when we set a new Range header (we did a seek) */
7411 gst_rtspsrc_configure_caps (src, segment, src->need_range);
7413 /* set again when needed */
7414 src->need_range = FALSE;
7416 src->running = TRUE;
7417 src->base_time = -1;
7418 src->state = GST_RTSP_STATE_PLAYING;
7421 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
7422 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7423 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7424 stream->discont = TRUE;
7429 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
7436 GST_DEBUG_OBJECT (src, "failed to open stream");
7441 GST_DEBUG_OBJECT (src, "PLAY is not supported");
7446 GST_DEBUG_OBJECT (src, "we were already PLAYING");
7449 create_request_failed:
7451 gchar *str = gst_rtsp_strresult (res);
7453 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7454 ("Could not create request. (%s)", str));
7460 gchar *str = gst_rtsp_strresult (res);
7462 gst_rtsp_message_unset (&request);
7463 if (res != GST_RTSP_EINTR) {
7464 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7465 ("Could not send message. (%s)", str));
7467 GST_WARNING_OBJECT (src, "PLAY interrupted");
7474 static GstRTSPResult
7475 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
7477 GstRTSPResult res = GST_RTSP_OK;
7478 GstRTSPMessage request = { 0 };
7479 GstRTSPMessage response = { 0 };
7481 const gchar *control;
7483 GST_DEBUG_OBJECT (src, "PAUSE...");
7485 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7488 if (!(src->methods & GST_RTSP_PAUSE))
7491 if (src->state == GST_RTSP_STATE_READY)
7494 if (!src->conninfo.connection || !src->conninfo.connected)
7497 /* construct a control url */
7498 control = get_aggregate_control (src);
7500 /* loop over the streams. We might exit the loop early when we could do an
7501 * aggregate control */
7502 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7503 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7504 GstRTSPConnection *conn;
7505 const gchar *setup_url;
7507 /* try aggregate control first but do non-aggregate control otherwise */
7509 setup_url = control;
7510 else if ((setup_url = stream->conninfo.location) == NULL)
7513 if (src->conninfo.connection) {
7514 conn = src->conninfo.connection;
7515 } else if (stream->conninfo.connection) {
7516 conn = stream->conninfo.connection;
7522 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7523 ("Sending PAUSE request"));
7526 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
7528 goto create_request_failed;
7530 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7533 gst_rtsp_message_unset (&request);
7534 gst_rtsp_message_unset (&response);
7536 /* exit early when we did agregate control */
7541 /* change element states now */
7542 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7545 src->state = GST_RTSP_STATE_READY;
7549 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7556 GST_DEBUG_OBJECT (src, "failed to open stream");
7561 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7566 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7569 create_request_failed:
7571 gchar *str = gst_rtsp_strresult (res);
7573 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7574 ("Could not create request. (%s)", str));
7580 gchar *str = gst_rtsp_strresult (res);
7582 gst_rtsp_message_unset (&request);
7583 if (res != GST_RTSP_EINTR) {
7584 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7585 ("Could not send message. (%s)", str));
7587 GST_WARNING_OBJECT (src, "PAUSE interrupted");
7595 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
7597 GstRTSPSrc *rtspsrc;
7599 rtspsrc = GST_RTSPSRC (bin);
7601 switch (GST_MESSAGE_TYPE (message)) {
7602 case GST_MESSAGE_EOS:
7603 gst_message_unref (message);
7605 case GST_MESSAGE_ELEMENT:
7607 const GstStructure *s = gst_message_get_structure (message);
7609 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
7610 gboolean ignore_timeout;
7612 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
7614 GST_OBJECT_LOCK (rtspsrc);
7615 ignore_timeout = rtspsrc->ignore_timeout;
7616 rtspsrc->ignore_timeout = TRUE;
7617 GST_OBJECT_UNLOCK (rtspsrc);
7619 /* we only act on the first udp timeout message, others are irrelevant
7620 * and can be ignored. */
7621 if (!ignore_timeout)
7622 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
7624 gst_message_unref (message);
7627 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7630 case GST_MESSAGE_ERROR:
7633 GstRTSPStream *stream;
7636 udpsrc = GST_MESSAGE_SRC (message);
7638 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7639 GST_ELEMENT_NAME (udpsrc));
7641 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7645 /* we ignore the RTCP udpsrc */
7646 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7649 /* if we get error messages from the udp sources, that's not a problem as
7650 * long as not all of them error out. We also don't really know what the
7651 * problem is, the message does not give enough detail... */
7652 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7653 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7654 if (ret != GST_FLOW_OK)
7658 gst_message_unref (message);
7662 /* fatal but not our message, forward */
7663 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7668 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7674 /* the thread where everything happens */
7676 gst_rtspsrc_thread (GstRTSPSrc * src)
7680 GST_OBJECT_LOCK (src);
7681 cmd = src->pending_cmd;
7682 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7683 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7684 src->pending_cmd = CMD_LOOP;
7686 src->pending_cmd = CMD_WAIT;
7687 GST_DEBUG_OBJECT (src, "got command %d", cmd);
7689 /* we got the message command, so ensure communication is possible again */
7690 gst_rtspsrc_connection_flush (src, FALSE);
7692 src->busy_cmd = cmd;
7693 GST_OBJECT_UNLOCK (src);
7697 gst_rtspsrc_open (src, TRUE);
7700 gst_rtspsrc_play (src, &src->segment, TRUE);
7703 gst_rtspsrc_pause (src, TRUE);
7706 gst_rtspsrc_close (src, TRUE, FALSE);
7709 gst_rtspsrc_loop (src);
7712 gst_rtspsrc_reconnect (src, FALSE);
7718 GST_OBJECT_LOCK (src);
7719 /* and go back to sleep */
7720 if (src->pending_cmd == CMD_WAIT) {
7722 gst_task_pause (src->task);
7725 src->busy_cmd = CMD_WAIT;
7726 GST_OBJECT_UNLOCK (src);
7730 gst_rtspsrc_start (GstRTSPSrc * src)
7732 GST_DEBUG_OBJECT (src, "starting");
7734 GST_OBJECT_LOCK (src);
7736 src->pending_cmd = CMD_WAIT;
7738 if (src->task == NULL) {
7739 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7740 if (src->task == NULL)
7743 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7745 GST_OBJECT_UNLOCK (src);
7752 GST_OBJECT_UNLOCK (src);
7753 GST_ERROR_OBJECT (src, "failed to create task");
7759 gst_rtspsrc_stop (GstRTSPSrc * src)
7763 GST_DEBUG_OBJECT (src, "stopping");
7765 /* also cancels pending task */
7766 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7768 GST_OBJECT_LOCK (src);
7769 if ((task = src->task)) {
7771 GST_OBJECT_UNLOCK (src);
7773 gst_task_stop (task);
7775 /* make sure it is not running */
7776 GST_RTSP_STREAM_LOCK (src);
7777 GST_RTSP_STREAM_UNLOCK (src);
7779 /* now wait for the task to finish */
7780 gst_task_join (task);
7782 /* and free the task */
7783 gst_object_unref (GST_OBJECT (task));
7785 GST_OBJECT_LOCK (src);
7787 GST_OBJECT_UNLOCK (src);
7789 /* ensure synchronously all is closed and clean */
7790 gst_rtspsrc_close (src, FALSE, TRUE);
7795 static GstStateChangeReturn
7796 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
7798 GstRTSPSrc *rtspsrc;
7799 GstStateChangeReturn ret;
7801 rtspsrc = GST_RTSPSRC (element);
7803 switch (transition) {
7804 case GST_STATE_CHANGE_NULL_TO_READY:
7805 if (!gst_rtspsrc_start (rtspsrc))
7808 case GST_STATE_CHANGE_READY_TO_PAUSED:
7809 /* init some state */
7810 rtspsrc->cur_protocols = rtspsrc->protocols;
7811 /* first attempt, don't ignore timeouts */
7812 rtspsrc->ignore_timeout = FALSE;
7813 rtspsrc->open_error = FALSE;
7814 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
7816 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7817 set_manager_buffer_mode (rtspsrc);
7819 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7820 /* unblock the tcp tasks and make the loop waiting */
7821 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
7822 /* make sure it is waiting before we send PAUSE or PLAY below */
7823 GST_RTSP_STREAM_LOCK (rtspsrc);
7824 GST_RTSP_STREAM_UNLOCK (rtspsrc);
7827 case GST_STATE_CHANGE_PAUSED_TO_READY:
7833 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
7834 if (ret == GST_STATE_CHANGE_FAILURE)
7837 switch (transition) {
7838 case GST_STATE_CHANGE_NULL_TO_READY:
7839 ret = GST_STATE_CHANGE_SUCCESS;
7841 case GST_STATE_CHANGE_READY_TO_PAUSED:
7842 ret = GST_STATE_CHANGE_NO_PREROLL;
7844 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7845 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
7846 ret = GST_STATE_CHANGE_SUCCESS;
7848 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7849 /* send pause request and keep the idle task around */
7850 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
7851 ret = GST_STATE_CHANGE_NO_PREROLL;
7853 case GST_STATE_CHANGE_PAUSED_TO_READY:
7854 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
7855 ret = GST_STATE_CHANGE_SUCCESS;
7857 case GST_STATE_CHANGE_READY_TO_NULL:
7858 gst_rtspsrc_stop (rtspsrc);
7859 ret = GST_STATE_CHANGE_SUCCESS;
7870 GST_DEBUG_OBJECT (rtspsrc, "start failed");
7871 return GST_STATE_CHANGE_FAILURE;
7876 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
7879 GstRTSPSrc *rtspsrc;
7881 rtspsrc = GST_RTSPSRC (element);
7883 if (GST_EVENT_IS_DOWNSTREAM (event)) {
7884 res = gst_rtspsrc_push_event (rtspsrc, event);
7886 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
7893 /*** GSTURIHANDLER INTERFACE *************************************************/
7896 gst_rtspsrc_uri_get_type (GType type)
7901 static const gchar *const *
7902 gst_rtspsrc_uri_get_protocols (GType type)
7904 static const gchar *protocols[] =
7905 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
7906 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
7913 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
7915 GstRTSPSrc *src = GST_RTSPSRC (handler);
7917 /* FIXME: make thread-safe */
7918 return g_strdup (src->conninfo.location);
7922 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
7927 GstRTSPUrl *newurl = NULL;
7928 GstSDPMessage *sdp = NULL;
7930 src = GST_RTSPSRC (handler);
7932 /* same URI, we're fine */
7933 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
7936 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
7937 if ((res = gst_sdp_message_new (&sdp) < 0))
7940 GST_DEBUG_OBJECT (src, "parsing SDP message");
7941 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
7945 GST_DEBUG_OBJECT (src, "parsing URI");
7946 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
7950 /* if worked, free previous and store new url object along with the original
7952 GST_DEBUG_OBJECT (src, "configuring URI");
7953 g_free (src->conninfo.location);
7954 src->conninfo.location = g_strdup (uri);
7955 gst_rtsp_url_free (src->conninfo.url);
7956 src->conninfo.url = newurl;
7957 g_free (src->conninfo.url_str);
7959 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
7961 src->conninfo.url_str = NULL;
7964 gst_sdp_message_free (src->sdp);
7966 src->from_sdp = sdp != NULL;
7968 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
7969 GST_DEBUG_OBJECT (src, "request uri is: %s",
7970 GST_STR_NULL (src->conninfo.url_str));
7977 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
7982 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
7983 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7984 "Could not create SDP");
7989 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
7990 GST_STR_NULL (uri));
7991 gst_sdp_message_free (sdp);
7992 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7998 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
7999 GST_STR_NULL (uri), res);
8000 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8001 "Invalid RTSP URI");
8007 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
8009 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
8011 iface->get_type = gst_rtspsrc_uri_get_type;
8012 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
8013 iface->get_uri = gst_rtspsrc_uri_get_uri;
8014 iface->set_uri = gst_rtspsrc_uri_set_uri;