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/rtp.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 enum _GstRtspSrcNtpTimeSource
170 NTP_TIME_SOURCE_UNIX,
171 NTP_TIME_SOURCE_RUNNING_TIME,
172 NTP_TIME_SOURCE_CLOCK_TIME
175 #define GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE (gst_rtsp_src_ntp_time_source_get_type())
177 gst_rtsp_src_ntp_time_source_get_type (void)
179 static GType ntp_time_source_type = 0;
180 static const GEnumValue ntp_time_source_values[] = {
181 {NTP_TIME_SOURCE_NTP, "NTP time based on realtime clock", "ntp"},
182 {NTP_TIME_SOURCE_UNIX, "UNIX time based on realtime clock", "unix"},
183 {NTP_TIME_SOURCE_RUNNING_TIME,
184 "Running time based on pipeline clock",
186 {NTP_TIME_SOURCE_CLOCK_TIME, "Pipeline clock time", "clock-time"},
190 if (!ntp_time_source_type) {
191 ntp_time_source_type =
192 g_enum_register_static ("GstRTSPSrcNtpTimeSource",
193 ntp_time_source_values);
195 return ntp_time_source_type;
198 #define AES_128_KEY_LEN 16
199 #define AES_256_KEY_LEN 32
201 #define HMAC_32_KEY_LEN 4
202 #define HMAC_80_KEY_LEN 10
204 #define DEFAULT_LOCATION NULL
205 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
206 #define DEFAULT_DEBUG FALSE
207 #define DEFAULT_RETRY 20
208 #define DEFAULT_TIMEOUT 5000000
209 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
210 #define DEFAULT_TCP_TIMEOUT 20000000
211 #define DEFAULT_LATENCY_MS 2000
212 #define DEFAULT_DROP_ON_LATENCY FALSE
213 #define DEFAULT_CONNECTION_SPEED 0
214 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
215 #define DEFAULT_DO_RTCP TRUE
216 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
217 #define DEFAULT_PROXY NULL
218 #define DEFAULT_RTP_BLOCKSIZE 0
219 #define DEFAULT_USER_ID NULL
220 #define DEFAULT_USER_PW NULL
221 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
222 #define DEFAULT_PORT_RANGE NULL
223 #define DEFAULT_SHORT_HEADER FALSE
224 #define DEFAULT_PROBATION 2
225 #define DEFAULT_UDP_RECONNECT TRUE
226 #define DEFAULT_MULTICAST_IFACE NULL
227 #define DEFAULT_NTP_SYNC FALSE
228 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
229 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
230 #define DEFAULT_TLS_DATABASE NULL
231 #define DEFAULT_TLS_INTERACTION NULL
232 #define DEFAULT_DO_RETRANSMISSION TRUE
233 #define DEFAULT_NTP_TIME_SOURCE NTP_TIME_SOURCE_NTP
234 #define DEFAULT_USER_AGENT "GStreamer/" PACKAGE_VERSION
246 PROP_DROP_ON_LATENCY,
247 PROP_CONNECTION_SPEED,
250 PROP_DO_RTSP_KEEP_ALIVE,
259 PROP_UDP_BUFFER_SIZE,
263 PROP_MULTICAST_IFACE,
265 PROP_USE_PIPELINE_CLOCK,
267 PROP_TLS_VALIDATION_FLAGS,
269 PROP_TLS_INTERACTION,
270 PROP_DO_RETRANSMISSION,
271 PROP_NTP_TIME_SOURCE,
275 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
277 gst_rtsp_nat_method_get_type (void)
279 static GType rtsp_nat_method_type = 0;
280 static const GEnumValue rtsp_nat_method[] = {
281 {GST_RTSP_NAT_NONE, "None", "none"},
282 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
286 if (!rtsp_nat_method_type) {
287 rtsp_nat_method_type =
288 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
290 return rtsp_nat_method_type;
293 static void gst_rtspsrc_finalize (GObject * object);
295 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
296 const GValue * value, GParamSpec * pspec);
297 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
298 GValue * value, GParamSpec * pspec);
300 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
302 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
303 gpointer iface_data);
305 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
308 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
309 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
311 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
313 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
314 GstStateChange transition);
315 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
316 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
318 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
319 GstRTSPMessage * response);
321 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
323 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
324 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
326 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
327 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
329 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
330 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
331 gboolean only_close);
333 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
334 const gchar * uri, GError ** error);
335 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
337 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
338 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
339 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
340 GstRTSPStream * stream, GstEvent * event);
341 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
342 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
350 /* commands we send to out loop to notify it of events */
351 #define CMD_OPEN (1 << 0)
352 #define CMD_PLAY (1 << 1)
353 #define CMD_PAUSE (1 << 2)
354 #define CMD_CLOSE (1 << 3)
355 #define CMD_WAIT (1 << 4)
356 #define CMD_RECONNECT (1 << 5)
357 #define CMD_LOOP (1 << 6)
359 /* mask for all commands */
360 #define CMD_ALL ((CMD_LOOP << 1) - 1)
362 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
364 gchar *__txt = _gst_element_error_printf text; \
365 gst_element_post_message (GST_ELEMENT_CAST (el), \
366 gst_message_new_progress (GST_OBJECT_CAST (el), \
367 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
371 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
373 #define gst_rtspsrc_parent_class parent_class
374 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
375 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
377 #ifndef GST_DISABLE_GST_DEBUG
378 static inline const char *
379 cmd_to_string (guint cmd)
403 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
405 GST_DEBUG_OBJECT (src, "default handler");
410 select_stream_accum (GSignalInvocationHint * ihint,
411 GValue * return_accu, const GValue * handler_return, gpointer data)
415 myboolean = g_value_get_boolean (handler_return);
416 GST_DEBUG ("accum %d", myboolean);
417 g_value_set_boolean (return_accu, myboolean);
419 /* stop emission if FALSE */
424 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
426 GObjectClass *gobject_class;
427 GstElementClass *gstelement_class;
428 GstBinClass *gstbin_class;
430 gobject_class = (GObjectClass *) klass;
431 gstelement_class = (GstElementClass *) klass;
432 gstbin_class = (GstBinClass *) klass;
434 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
436 gobject_class->set_property = gst_rtspsrc_set_property;
437 gobject_class->get_property = gst_rtspsrc_get_property;
439 gobject_class->finalize = gst_rtspsrc_finalize;
441 g_object_class_install_property (gobject_class, PROP_LOCATION,
442 g_param_spec_string ("location", "RTSP Location",
443 "Location of the RTSP url to read",
444 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
446 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
447 g_param_spec_flags ("protocols", "Protocols",
448 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
449 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
451 g_object_class_install_property (gobject_class, PROP_DEBUG,
452 g_param_spec_boolean ("debug", "Debug",
453 "Dump request and response messages to stdout",
454 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
456 g_object_class_install_property (gobject_class, PROP_RETRY,
457 g_param_spec_uint ("retry", "Retry",
458 "Max number of retries when allocating RTP ports.",
459 0, G_MAXUINT16, DEFAULT_RETRY,
460 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
462 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
463 g_param_spec_uint64 ("timeout", "Timeout",
464 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
465 0, G_MAXUINT64, DEFAULT_TIMEOUT,
466 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
468 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
469 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
470 "Fail after timeout microseconds on TCP connections (0 = disabled)",
471 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
472 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
474 g_object_class_install_property (gobject_class, PROP_LATENCY,
475 g_param_spec_uint ("latency", "Buffer latency in ms",
476 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
477 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
479 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
480 g_param_spec_boolean ("drop-on-latency",
481 "Drop buffers when maximum latency is reached",
482 "Tells the jitterbuffer to never exceed the given latency in size",
483 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
485 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
486 g_param_spec_uint64 ("connection-speed", "Connection Speed",
487 "Network connection speed in kbps (0 = unknown)",
488 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
489 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
491 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
492 g_param_spec_enum ("nat-method", "NAT Method",
493 "Method to use for traversing firewalls and NAT",
494 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
495 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
498 * GstRTSPSrc:do-rtcp:
500 * Enable RTCP support. Some old server don't like RTCP and then this property
501 * needs to be set to FALSE.
503 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
504 g_param_spec_boolean ("do-rtcp", "Do RTCP",
505 "Send RTCP packets, disable for old incompatible server.",
506 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
509 * GstRTSPSrc:do-rtsp-keep-alive:
511 * Enable RTSP keep alive support. Some old server don't like RTSP
512 * keep alive and then this property needs to be set to FALSE.
514 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
515 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
516 "Send RTSP keep alive packets, disable for old incompatible server.",
517 DEFAULT_DO_RTSP_KEEP_ALIVE,
518 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
523 * Set the proxy parameters. This has to be a string of the format
524 * [http://][user:passwd@]host[:port].
526 g_object_class_install_property (gobject_class, PROP_PROXY,
527 g_param_spec_string ("proxy", "Proxy",
528 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
529 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
531 * GstRTSPSrc:proxy-id:
533 * Sets the proxy URI user id for authentication. If the URI set via the
534 * "proxy" property contains a user-id already, that will take precedence.
538 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
539 g_param_spec_string ("proxy-id", "proxy-id",
540 "HTTP proxy URI user id for authentication", "",
541 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
543 * GstRTSPSrc:proxy-pw:
545 * Sets the proxy URI password for authentication. If the URI set via the
546 * "proxy" property contains a password already, that will take precedence.
550 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
551 g_param_spec_string ("proxy-pw", "proxy-pw",
552 "HTTP proxy URI user password for authentication", "",
553 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
556 * GstRTSPSrc:rtp-blocksize:
558 * RTP package size to suggest to server.
560 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
561 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
562 "RTP package size to suggest to server (0 = disabled)",
563 0, 65536, DEFAULT_RTP_BLOCKSIZE,
564 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
566 g_object_class_install_property (gobject_class,
568 g_param_spec_string ("user-id", "user-id",
569 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
570 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
571 g_object_class_install_property (gobject_class, PROP_USER_PW,
572 g_param_spec_string ("user-pw", "user-pw",
573 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
574 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
577 * GstRTSPSrc:buffer-mode:
579 * Control the buffering and timestamping mode used by the jitterbuffer.
581 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
582 g_param_spec_enum ("buffer-mode", "Buffer Mode",
583 "Control the buffering algorithm in use",
584 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
585 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
588 * GstRTSPSrc:port-range:
590 * Configure the client port numbers that can be used to recieve RTP and
593 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
594 g_param_spec_string ("port-range", "Port range",
595 "Client port range that can be used to receive RTP and RTCP data, "
596 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
597 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
600 * GstRTSPSrc:udp-buffer-size:
602 * Size of the kernel UDP receive buffer in bytes.
604 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
605 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
606 "Size of the kernel UDP receive buffer in bytes, 0=default",
607 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
608 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
611 * GstRTSPSrc:short-header:
613 * Only send the basic RTSP headers for broken encoders.
615 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
616 g_param_spec_boolean ("short-header", "Short Header",
617 "Only send the basic RTSP headers for broken encoders",
618 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
620 g_object_class_install_property (gobject_class, PROP_PROBATION,
621 g_param_spec_uint ("probation", "Number of probations",
622 "Consecutive packet sequence numbers to accept the source",
623 0, G_MAXUINT, DEFAULT_PROBATION,
624 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
626 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
627 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
628 "Reconnect to the server if RTSP connection is closed when doing UDP",
629 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
631 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
632 g_param_spec_string ("multicast-iface", "Multicast Interface",
633 "The network interface on which to join the multicast group",
634 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
636 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
637 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
638 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
639 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
641 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
642 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
643 "Use the pipeline running-time to set the NTP time in the RTCP SR messages"
644 "(DEPRECATED: Use ntp-time-source property)",
645 DEFAULT_USE_PIPELINE_CLOCK,
646 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
648 g_object_class_install_property (gobject_class, PROP_SDES,
649 g_param_spec_boxed ("sdes", "SDES",
650 "The SDES items of this session",
651 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
654 * GstRTSPSrc::tls-validation-flags:
656 * TLS certificate validation flags used to validate server
661 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
662 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
663 "TLS certificate validation flags used to validate the server certificate",
664 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
665 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
668 * GstRTSPSrc::tls-database:
670 * TLS database with anchor certificate authorities used to validate
671 * the server certificate.
675 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
676 g_param_spec_object ("tls-database", "TLS database",
677 "TLS database with anchor certificate authorities used to validate the server certificate",
678 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
681 * GstRTSPSrc::tls-interaction:
683 * A #GTlsInteraction object to be used when the connection or certificate
684 * database need to interact with the user. This will be used to prompt the
685 * user for passwords where necessary.
689 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
690 g_param_spec_object ("tls-interaction", "TLS interaction",
691 "A GTlsInteraction object to promt the user for password or certificate",
692 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
695 * GstRTSPSrc::do-retransmission:
697 * Attempt to ask the server to retransmit lost packets according to RFC4588.
699 * Note: currently only works with SSRC-multiplexed retransmission streams
703 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
704 g_param_spec_boolean ("do-retransmission", "Retransmission",
705 "Ask the server to retransmit lost packets",
706 DEFAULT_DO_RETRANSMISSION,
707 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
710 * GstRTSPSrc::ntp-time-source:
712 * allows to select the time source that should be used
713 * for the NTP time in RTCP packets
717 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
718 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
719 "NTP time source for RTCP packets",
720 GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE, DEFAULT_NTP_TIME_SOURCE,
721 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
724 * GstRTSPSrc::user-agent:
726 * The string to set in the User-Agent header.
730 g_object_class_install_property (gobject_class, PROP_USER_AGENT,
731 g_param_spec_string ("user-agent", "User Agent",
732 "The User-Agent string to send to the server",
733 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
736 * GstRTSPSrc::handle-request:
737 * @rtspsrc: a #GstRTSPSrc
738 * @request: a #GstRTSPMessage
739 * @response: a #GstRTSPMessage
741 * Handle a server request in @request and prepare @response.
743 * This signal is called from the streaming thread, you should therefore not
744 * do any state changes on @rtspsrc because this might deadlock. If you want
745 * to modify the state as a result of this signal, post a
746 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
751 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
752 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
753 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
754 G_TYPE_POINTER, G_TYPE_POINTER);
757 * GstRTSPSrc::on-sdp:
758 * @rtspsrc: a #GstRTSPSrc
759 * @sdp: a #GstSDPMessage
761 * Emited when the client has retrieved the SDP and before it configures the
762 * streams in the SDP. @sdp can be inspected and modified.
764 * This signal is called from the streaming thread, you should therefore not
765 * do any state changes on @rtspsrc because this might deadlock. If you want
766 * to modify the state as a result of this signal, post a
767 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
772 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
773 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
774 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
775 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
778 * GstRTSPSrc::select-stream:
779 * @rtspsrc: a #GstRTSPSrc
780 * @num: the stream number
781 * @caps: the stream caps
783 * Emited before the client decides to configure the stream @num with
786 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
791 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
792 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
793 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
794 (GCallback) default_select_stream, select_stream_accum, NULL,
795 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
798 * GstRTSPSrc::new-manager:
799 * @rtspsrc: a #GstRTSPSrc
800 * @manager: a #GstElement
802 * Emited after a new manager (like rtpbin) was created and the default
803 * properties were configured.
807 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
808 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
809 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
810 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
813 * GstRTSPSrc::request-rtcp-key:
814 * @rtspsrc: a #GstRTSPSrc
815 * @num: the stream number
817 * Signal emited to get the crypto parameters relevant to the RTCP
818 * stream. User should provide the key and the RTCP encryption ciphers
819 * and authentication, and return them wrapped in a GstCaps.
823 gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY] =
824 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
825 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
827 gstelement_class->send_event = gst_rtspsrc_send_event;
828 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
829 gstelement_class->change_state = gst_rtspsrc_change_state;
831 gst_element_class_add_pad_template (gstelement_class,
832 gst_static_pad_template_get (&rtptemplate));
834 gst_element_class_set_static_metadata (gstelement_class,
835 "RTSP packet receiver", "Source/Network",
836 "Receive data over the network via RTSP (RFC 2326)",
837 "Wim Taymans <wim@fluendo.com>, "
838 "Thijs Vermeir <thijs.vermeir@barco.com>, "
839 "Lutz Mueller <lutz@topfrose.de>");
841 gstbin_class->handle_message = gst_rtspsrc_handle_message;
843 gst_rtsp_ext_list_init ();
847 gst_rtspsrc_init (GstRTSPSrc * src)
849 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
850 src->protocols = DEFAULT_PROTOCOLS;
851 src->debug = DEFAULT_DEBUG;
852 src->retry = DEFAULT_RETRY;
853 src->udp_timeout = DEFAULT_TIMEOUT;
854 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
855 src->latency = DEFAULT_LATENCY_MS;
856 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
857 src->connection_speed = DEFAULT_CONNECTION_SPEED;
858 src->nat_method = DEFAULT_NAT_METHOD;
859 src->do_rtcp = DEFAULT_DO_RTCP;
860 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
861 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
862 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
863 src->user_id = g_strdup (DEFAULT_USER_ID);
864 src->user_pw = g_strdup (DEFAULT_USER_PW);
865 src->buffer_mode = DEFAULT_BUFFER_MODE;
866 src->client_port_range.min = 0;
867 src->client_port_range.max = 0;
868 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
869 src->short_header = DEFAULT_SHORT_HEADER;
870 src->probation = DEFAULT_PROBATION;
871 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
872 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
873 src->ntp_sync = DEFAULT_NTP_SYNC;
874 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
876 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
877 src->tls_database = DEFAULT_TLS_DATABASE;
878 src->tls_interaction = DEFAULT_TLS_INTERACTION;
879 src->do_retransmission = DEFAULT_DO_RETRANSMISSION;
880 src->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
881 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
883 /* get a list of all extensions */
884 src->extensions = gst_rtsp_ext_list_get ();
886 /* connect to send signal */
887 gst_rtsp_ext_list_connect (src->extensions, "send",
888 (GCallback) gst_rtspsrc_send_cb, src);
890 /* protects the streaming thread in interleaved mode or the polling
891 * thread in UDP mode. */
892 g_rec_mutex_init (&src->stream_rec_lock);
894 /* protects our state changes from multiple invocations */
895 g_rec_mutex_init (&src->state_rec_lock);
897 src->state = GST_RTSP_STATE_INVALID;
899 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
903 gst_rtspsrc_finalize (GObject * object)
907 rtspsrc = GST_RTSPSRC (object);
909 gst_rtsp_ext_list_free (rtspsrc->extensions);
910 g_free (rtspsrc->conninfo.location);
911 gst_rtsp_url_free (rtspsrc->conninfo.url);
912 g_free (rtspsrc->conninfo.url_str);
913 g_free (rtspsrc->user_id);
914 g_free (rtspsrc->user_pw);
915 g_free (rtspsrc->multi_iface);
916 g_free (rtspsrc->user_agent);
919 gst_sdp_message_free (rtspsrc->sdp);
922 if (rtspsrc->provided_clock)
923 gst_object_unref (rtspsrc->provided_clock);
926 gst_structure_free (rtspsrc->sdes);
928 if (rtspsrc->tls_database)
929 g_object_unref (rtspsrc->tls_database);
931 if (rtspsrc->tls_interaction)
932 g_object_unref (rtspsrc->tls_interaction);
935 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
936 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
938 G_OBJECT_CLASS (parent_class)->finalize (object);
942 gst_rtspsrc_provide_clock (GstElement * element)
944 GstRTSPSrc *src = GST_RTSPSRC (element);
947 if ((clock = src->provided_clock) != NULL)
948 gst_object_ref (clock);
953 /* a proxy string of the format [user:passwd@]host[:port] */
955 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
959 g_free (rtsp->proxy_user);
960 rtsp->proxy_user = NULL;
961 g_free (rtsp->proxy_passwd);
962 rtsp->proxy_passwd = NULL;
963 g_free (rtsp->proxy_host);
964 rtsp->proxy_host = NULL;
965 rtsp->proxy_port = 0;
972 /* we allow http:// in front but ignore it */
973 if (g_str_has_prefix (p, "http://"))
976 at = strchr (p, '@');
978 /* look for user:passwd */
979 col = strchr (proxy, ':');
980 if (col == NULL || col > at)
983 rtsp->proxy_user = g_strndup (p, col - p);
985 rtsp->proxy_passwd = g_strndup (col, at - col);
990 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
991 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
992 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
993 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
994 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
995 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
996 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
999 col = strchr (p, ':');
1002 /* everything before the colon is the hostname */
1003 rtsp->proxy_host = g_strndup (p, col - p);
1005 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
1007 rtsp->proxy_host = g_strdup (p);
1008 rtsp->proxy_port = 8080;
1014 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
1016 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
1017 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
1020 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
1022 rtspsrc->ptcp_timeout = NULL;
1026 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
1029 GstRTSPSrc *rtspsrc;
1031 rtspsrc = GST_RTSPSRC (object);
1035 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
1036 g_value_get_string (value), NULL);
1038 case PROP_PROTOCOLS:
1039 rtspsrc->protocols = g_value_get_flags (value);
1042 rtspsrc->debug = g_value_get_boolean (value);
1045 rtspsrc->retry = g_value_get_uint (value);
1048 rtspsrc->udp_timeout = g_value_get_uint64 (value);
1050 case PROP_TCP_TIMEOUT:
1051 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
1054 rtspsrc->latency = g_value_get_uint (value);
1056 case PROP_DROP_ON_LATENCY:
1057 rtspsrc->drop_on_latency = g_value_get_boolean (value);
1059 case PROP_CONNECTION_SPEED:
1060 rtspsrc->connection_speed = g_value_get_uint64 (value);
1062 case PROP_NAT_METHOD:
1063 rtspsrc->nat_method = g_value_get_enum (value);
1066 rtspsrc->do_rtcp = g_value_get_boolean (value);
1068 case PROP_DO_RTSP_KEEP_ALIVE:
1069 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
1072 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
1075 if (rtspsrc->prop_proxy_id)
1076 g_free (rtspsrc->prop_proxy_id);
1077 rtspsrc->prop_proxy_id = g_value_dup_string (value);
1080 if (rtspsrc->prop_proxy_pw)
1081 g_free (rtspsrc->prop_proxy_pw);
1082 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
1084 case PROP_RTP_BLOCKSIZE:
1085 rtspsrc->rtp_blocksize = g_value_get_uint (value);
1088 if (rtspsrc->user_id)
1089 g_free (rtspsrc->user_id);
1090 rtspsrc->user_id = g_value_dup_string (value);
1093 if (rtspsrc->user_pw)
1094 g_free (rtspsrc->user_pw);
1095 rtspsrc->user_pw = g_value_dup_string (value);
1097 case PROP_BUFFER_MODE:
1098 rtspsrc->buffer_mode = g_value_get_enum (value);
1100 case PROP_PORT_RANGE:
1104 str = g_value_get_string (value);
1106 sscanf (str, "%u-%u",
1107 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
1109 rtspsrc->client_port_range.min = 0;
1110 rtspsrc->client_port_range.max = 0;
1114 case PROP_UDP_BUFFER_SIZE:
1115 rtspsrc->udp_buffer_size = g_value_get_int (value);
1117 case PROP_SHORT_HEADER:
1118 rtspsrc->short_header = g_value_get_boolean (value);
1120 case PROP_PROBATION:
1121 rtspsrc->probation = g_value_get_uint (value);
1123 case PROP_UDP_RECONNECT:
1124 rtspsrc->udp_reconnect = g_value_get_boolean (value);
1126 case PROP_MULTICAST_IFACE:
1127 g_free (rtspsrc->multi_iface);
1129 if (g_value_get_string (value) == NULL)
1130 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1132 rtspsrc->multi_iface = g_value_dup_string (value);
1135 rtspsrc->ntp_sync = g_value_get_boolean (value);
1137 case PROP_USE_PIPELINE_CLOCK:
1138 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
1141 rtspsrc->sdes = g_value_dup_boxed (value);
1143 case PROP_TLS_VALIDATION_FLAGS:
1144 rtspsrc->tls_validation_flags = g_value_get_flags (value);
1146 case PROP_TLS_DATABASE:
1147 g_clear_object (&rtspsrc->tls_database);
1148 rtspsrc->tls_database = g_value_dup_object (value);
1150 case PROP_TLS_INTERACTION:
1151 g_clear_object (&rtspsrc->tls_interaction);
1152 rtspsrc->tls_interaction = g_value_dup_object (value);
1154 case PROP_DO_RETRANSMISSION:
1155 rtspsrc->do_retransmission = g_value_get_boolean (value);
1157 case PROP_NTP_TIME_SOURCE:
1158 rtspsrc->ntp_time_source = g_value_get_enum (value);
1160 case PROP_USER_AGENT:
1161 g_free (rtspsrc->user_agent);
1162 rtspsrc->user_agent = g_value_dup_string (value);
1165 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1171 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1174 GstRTSPSrc *rtspsrc;
1176 rtspsrc = GST_RTSPSRC (object);
1180 g_value_set_string (value, rtspsrc->conninfo.location);
1182 case PROP_PROTOCOLS:
1183 g_value_set_flags (value, rtspsrc->protocols);
1186 g_value_set_boolean (value, rtspsrc->debug);
1189 g_value_set_uint (value, rtspsrc->retry);
1192 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1194 case PROP_TCP_TIMEOUT:
1198 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1199 rtspsrc->tcp_timeout.tv_usec;
1200 g_value_set_uint64 (value, timeout);
1204 g_value_set_uint (value, rtspsrc->latency);
1206 case PROP_DROP_ON_LATENCY:
1207 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1209 case PROP_CONNECTION_SPEED:
1210 g_value_set_uint64 (value, rtspsrc->connection_speed);
1212 case PROP_NAT_METHOD:
1213 g_value_set_enum (value, rtspsrc->nat_method);
1216 g_value_set_boolean (value, rtspsrc->do_rtcp);
1218 case PROP_DO_RTSP_KEEP_ALIVE:
1219 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1225 if (rtspsrc->proxy_host) {
1227 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1231 g_value_take_string (value, str);
1235 g_value_set_string (value, rtspsrc->prop_proxy_id);
1238 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1240 case PROP_RTP_BLOCKSIZE:
1241 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1244 g_value_set_string (value, rtspsrc->user_id);
1247 g_value_set_string (value, rtspsrc->user_pw);
1249 case PROP_BUFFER_MODE:
1250 g_value_set_enum (value, rtspsrc->buffer_mode);
1252 case PROP_PORT_RANGE:
1256 if (rtspsrc->client_port_range.min != 0) {
1257 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1258 rtspsrc->client_port_range.max);
1262 g_value_take_string (value, str);
1265 case PROP_UDP_BUFFER_SIZE:
1266 g_value_set_int (value, rtspsrc->udp_buffer_size);
1268 case PROP_SHORT_HEADER:
1269 g_value_set_boolean (value, rtspsrc->short_header);
1271 case PROP_PROBATION:
1272 g_value_set_uint (value, rtspsrc->probation);
1274 case PROP_UDP_RECONNECT:
1275 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1277 case PROP_MULTICAST_IFACE:
1278 g_value_set_string (value, rtspsrc->multi_iface);
1281 g_value_set_boolean (value, rtspsrc->ntp_sync);
1283 case PROP_USE_PIPELINE_CLOCK:
1284 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1287 g_value_set_boxed (value, rtspsrc->sdes);
1289 case PROP_TLS_VALIDATION_FLAGS:
1290 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1292 case PROP_TLS_DATABASE:
1293 g_value_set_object (value, rtspsrc->tls_database);
1295 case PROP_TLS_INTERACTION:
1296 g_value_set_object (value, rtspsrc->tls_interaction);
1298 case PROP_DO_RETRANSMISSION:
1299 g_value_set_boolean (value, rtspsrc->do_retransmission);
1301 case PROP_NTP_TIME_SOURCE:
1302 g_value_set_enum (value, rtspsrc->ntp_time_source);
1304 case PROP_USER_AGENT:
1305 g_value_set_string (value, rtspsrc->user_agent);
1308 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1314 find_stream_by_id (GstRTSPStream * stream, gint * id)
1316 if (stream->id == *id)
1323 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1325 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1332 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1334 GstElement *src = (GstElement *) a;
1336 if (stream->udpsrc[0] == src)
1338 if (stream->udpsrc[1] == src)
1345 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1347 if (stream->conninfo.location) {
1348 /* check qualified setup_url */
1349 if (!strcmp (stream->conninfo.location, (gchar *) a))
1352 if (stream->control_url) {
1353 /* check original control_url */
1354 if (!strcmp (stream->control_url, (gchar *) a))
1357 /* check if qualified setup_url ends with string */
1358 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1365 static GstRTSPStream *
1366 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1370 /* find and get stream */
1371 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1372 return (GstRTSPStream *) lstream->data;
1377 static const GstSDPBandwidth *
1378 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1379 const GstSDPMedia * media, const gchar * type)
1383 /* first look in the media specific section */
1384 len = gst_sdp_media_bandwidths_len (media);
1385 for (i = 0; i < len; i++) {
1386 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1388 if (strcmp (bw->bwtype, type) == 0)
1391 /* then look in the message specific section */
1392 len = gst_sdp_message_bandwidths_len (sdp);
1393 for (i = 0; i < len; i++) {
1394 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1396 if (strcmp (bw->bwtype, type) == 0)
1403 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1404 const GstSDPMedia * media, GstRTSPStream * stream)
1406 const GstSDPBandwidth *bw;
1408 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1409 stream->as_bandwidth = bw->bandwidth;
1411 stream->as_bandwidth = -1;
1413 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1414 stream->rr_bandwidth = bw->bandwidth;
1416 stream->rr_bandwidth = -1;
1418 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1419 stream->rs_bandwidth = bw->bandwidth;
1421 stream->rs_bandwidth = -1;
1425 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1426 const GstSDPConnection * conn)
1428 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1431 if (conn->addrtype == NULL)
1434 /* check for IPV6 */
1435 if (strcmp (conn->addrtype, "IP4") == 0)
1436 stream->is_ipv6 = FALSE;
1437 else if (strcmp (conn->addrtype, "IP6") == 0)
1438 stream->is_ipv6 = TRUE;
1443 g_free (stream->destination);
1444 stream->destination = g_strdup (conn->address);
1446 /* check for multicast */
1447 stream->is_multicast =
1448 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1450 stream->ttl = conn->ttl;
1453 /* Go over the connections for a stream.
1454 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1456 * - If we are dealing with a localhost address, we disable multicast
1459 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1460 const GstSDPMedia * media, GstRTSPStream * stream)
1462 const GstSDPConnection *conn;
1465 /* first look in the media specific section */
1466 len = gst_sdp_media_connections_len (media);
1467 for (i = 0; i < len; i++) {
1468 conn = gst_sdp_media_get_connection (media, i);
1470 gst_rtspsrc_do_stream_connection (src, stream, conn);
1472 /* then look in the message specific section */
1473 if ((conn = gst_sdp_message_get_connection (sdp))) {
1474 gst_rtspsrc_do_stream_connection (src, stream, conn);
1478 /* m=<media> <UDP port> RTP/AVP <payload>
1481 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
1482 const GstSDPMedia * media, GstRTSPStream * stream)
1486 GstCaps *global_caps;
1489 proto = gst_sdp_media_get_proto (media);
1493 if (g_str_equal (proto, "RTP/AVP"))
1494 stream->profile = GST_RTSP_PROFILE_AVP;
1495 else if (g_str_equal (proto, "RTP/SAVP"))
1496 stream->profile = GST_RTSP_PROFILE_SAVP;
1497 else if (g_str_equal (proto, "RTP/AVPF"))
1498 stream->profile = GST_RTSP_PROFILE_AVPF;
1499 else if (g_str_equal (proto, "RTP/SAVPF"))
1500 stream->profile = GST_RTSP_PROFILE_SAVPF;
1504 /* Parse global SDP attributes once */
1505 global_caps = gst_caps_new_empty_simple ("application/x-unknown");
1506 GST_DEBUG ("mapping sdp session level attributes to caps");
1507 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, global_caps);
1508 GST_DEBUG ("mapping sdp media level attributes to caps");
1509 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, global_caps);
1511 len = gst_sdp_media_formats_len (media);
1512 for (i = 0; i < len; i++) {
1514 GstCaps *caps, *outcaps;
1519 pt = atoi (gst_sdp_media_get_format (media, i));
1521 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
1524 caps = gst_rtspsrc_media_to_caps (pt, media);
1526 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
1530 /* do some tweaks */
1531 s = gst_caps_get_structure (caps, 0);
1532 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
1533 stream->is_real = (strstr (enc, "-REAL") != NULL);
1534 if (strcmp (enc, "X-ASF-PF") == 0)
1535 stream->container = TRUE;
1538 /* Merge in global caps */
1539 /* Intersect will merge in missing fields to the current caps */
1540 outcaps = gst_caps_intersect (caps, global_caps);
1541 gst_caps_unref (caps);
1543 /* the first pt will be the default */
1544 if (stream->ptmap->len == 0)
1545 stream->default_pt = pt;
1548 item.caps = outcaps;
1550 g_array_append_val (stream->ptmap, item);
1553 gst_caps_unref (global_caps);
1558 GST_ERROR_OBJECT (src, "can't find proto in media");
1563 GST_ERROR_OBJECT (src, "unknown proto in media %s", proto);
1568 static const gchar *
1569 get_aggregate_control (GstRTSPSrc * src)
1574 base = src->control;
1575 else if (src->content_base)
1576 base = src->content_base;
1577 else if (src->conninfo.url_str)
1578 base = src->conninfo.url_str;
1586 clear_ptmap_item (PtMapItem * item)
1589 gst_caps_unref (item->caps);
1592 static GstRTSPStream *
1593 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1595 GstRTSPStream *stream;
1596 const gchar *control_url;
1597 const GstSDPMedia *media;
1599 /* get media, should not return NULL */
1600 media = gst_sdp_message_get_media (sdp, idx);
1604 stream = g_new0 (GstRTSPStream, 1);
1605 stream->parent = src;
1606 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1608 stream->last_ret = GST_FLOW_NOT_LINKED;
1609 stream->added = FALSE;
1610 stream->setup = FALSE;
1611 stream->skipped = FALSE;
1613 stream->eos = FALSE;
1614 stream->discont = TRUE;
1615 stream->seqbase = -1;
1616 stream->timebase = -1;
1617 stream->send_ssrc = g_random_int ();
1618 stream->profile = GST_RTSP_PROFILE_AVP;
1619 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1620 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
1622 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1623 * session manager to scale RTCP. */
1624 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1626 /* collect connection info */
1627 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1629 /* make the payload type map */
1630 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1632 /* collect port number */
1633 stream->port = gst_sdp_media_get_port (media);
1635 /* get control url to construct the setup url. The setup url is used to
1636 * configure the transport of the stream and is used to identity the stream in
1637 * the RTP-Info header field returned from PLAY. */
1638 control_url = gst_sdp_media_get_attribute_val (media, "control");
1639 if (control_url == NULL)
1640 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1642 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1643 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1644 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1645 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1647 if (control_url != NULL) {
1648 stream->control_url = g_strdup (control_url);
1649 /* Build a fully qualified url using the content_base if any or by prefixing
1650 * the original request.
1651 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1652 * likely build a URL that the server will fail to understand, this is ok,
1653 * we will fail then. */
1654 if (g_str_has_prefix (control_url, "rtsp://"))
1655 stream->conninfo.location = g_strdup (control_url);
1660 if (g_strcmp0 (control_url, "*") == 0)
1663 base = get_aggregate_control (src);
1665 /* check if the base ends or control starts with / */
1666 has_slash = g_str_has_prefix (control_url, "/");
1667 has_slash = has_slash || g_str_has_suffix (base, "/");
1669 /* concatenate the two strings, insert / when not present */
1670 stream->conninfo.location =
1671 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1674 GST_DEBUG_OBJECT (src, " setup: %s",
1675 GST_STR_NULL (stream->conninfo.location));
1677 /* we keep track of all streams */
1678 src->streams = g_list_append (src->streams, stream);
1686 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1690 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1692 g_array_free (stream->ptmap, TRUE);
1694 g_free (stream->destination);
1695 g_free (stream->control_url);
1696 g_free (stream->conninfo.location);
1698 for (i = 0; i < 2; i++) {
1699 if (stream->udpsrc[i]) {
1700 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1701 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1702 gst_object_unref (stream->udpsrc[i]);
1704 if (stream->channelpad[i])
1705 gst_object_unref (stream->channelpad[i]);
1707 if (stream->udpsink[i]) {
1708 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1709 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1710 gst_object_unref (stream->udpsink[i]);
1713 if (stream->fakesrc) {
1714 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1715 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1716 gst_object_unref (stream->fakesrc);
1718 if (stream->srcpad) {
1719 gst_pad_set_active (stream->srcpad, FALSE);
1721 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1723 if (stream->srtpenc)
1724 gst_object_unref (stream->srtpenc);
1725 if (stream->srtpdec)
1726 gst_object_unref (stream->srtpdec);
1727 if (stream->srtcpparams)
1728 gst_caps_unref (stream->srtcpparams);
1729 if (stream->rtcppad)
1730 gst_object_unref (stream->rtcppad);
1731 if (stream->session)
1732 g_object_unref (stream->session);
1733 if (stream->rtx_pt_map)
1734 gst_structure_free (stream->rtx_pt_map);
1739 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1743 GST_DEBUG_OBJECT (src, "cleanup");
1745 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1746 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1748 gst_rtspsrc_stream_free (src, stream);
1750 g_list_free (src->streams);
1751 src->streams = NULL;
1753 if (src->manager_sig_id) {
1754 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1755 src->manager_sig_id = 0;
1757 gst_element_set_state (src->manager, GST_STATE_NULL);
1758 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1759 src->manager = NULL;
1762 gst_structure_free (src->props);
1765 g_free (src->content_base);
1766 src->content_base = NULL;
1768 g_free (src->control);
1769 src->control = NULL;
1772 gst_rtsp_range_free (src->range);
1775 /* don't clear the SDP when it was used in the url */
1776 if (src->sdp && !src->from_sdp) {
1777 gst_sdp_message_free (src->sdp);
1781 src->need_segment = FALSE;
1783 if (src->provided_clock) {
1784 gst_object_unref (src->provided_clock);
1785 src->provided_clock = NULL;
1789 #define PARSE_INT(p, del, res) \
1792 p = strstr (p, del); \
1802 #define PARSE_STRING(p, del, res) \
1805 p = strstr (p, del); \
1817 #define SKIP_SPACES(p) \
1818 while (*p && g_ascii_isspace (*p)) \
1823 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1826 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1827 gint * rate, gchar ** params)
1831 p = (gchar *) rtpmap;
1833 PARSE_INT (p, " ", *payload);
1841 PARSE_STRING (p, "/", *name);
1842 if (*name == NULL) {
1843 GST_DEBUG ("no rate, name %s", p);
1844 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1845 * streams seem to omit the rate. */
1852 p = strstr (p, "/");
1870 parse_keymgmt (const gchar * keymgmt, GstCaps * caps)
1872 gboolean res = FALSE;
1875 GstMIKEYMessage *msg;
1876 const GstMIKEYPayload *payload;
1877 const gchar *srtp_cipher;
1878 const gchar *srtp_auth;
1884 p = orig_value = g_strdup (keymgmt);
1888 g_free (orig_value);
1892 PARSE_STRING (p, " ", kmpid);
1893 if (kmpid == NULL || !g_str_equal (kmpid, "mikey")) {
1894 g_free (orig_value);
1897 data = g_base64_decode (p, &size);
1899 g_free (orig_value); /* Don't need this any more */
1905 msg = gst_mikey_message_new_from_data (data, size, NULL, NULL);
1910 srtp_cipher = "aes-128-icm";
1911 srtp_auth = "hmac-sha1-80";
1913 /* check the Security policy if any */
1914 if ((payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, 0))) {
1915 GstMIKEYPayloadSP *p = (GstMIKEYPayloadSP *) payload;
1918 if (p->proto != GST_MIKEY_SEC_PROTO_SRTP)
1921 len = gst_mikey_payload_sp_get_n_params (payload);
1922 for (i = 0; i < len; i++) {
1923 const GstMIKEYPayloadSPParam *param =
1924 gst_mikey_payload_sp_get_param (payload, i);
1926 switch (param->type) {
1927 case GST_MIKEY_SP_SRTP_ENC_ALG:
1928 switch (param->val[0]) {
1930 srtp_cipher = "null";
1934 srtp_cipher = "aes-128-icm";
1940 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
1941 switch (param->val[0]) {
1942 case AES_128_KEY_LEN:
1943 srtp_cipher = "aes-128-icm";
1945 case AES_256_KEY_LEN:
1946 srtp_cipher = "aes-256-icm";
1952 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1953 switch (param->val[0]) {
1959 srtp_auth = "hmac-sha1-80";
1965 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
1966 switch (param->val[0]) {
1967 case HMAC_32_KEY_LEN:
1968 srtp_auth = "hmac-sha1-32";
1970 case HMAC_80_KEY_LEN:
1971 srtp_auth = "hmac-sha1-80";
1977 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1979 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1987 if (!(payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_KEMAC, 0)))
1990 GstMIKEYPayloadKEMAC *p = (GstMIKEYPayloadKEMAC *) payload;
1991 const GstMIKEYPayload *sub;
1992 GstMIKEYPayloadKeyData *pkd;
1995 if (p->enc_alg != GST_MIKEY_ENC_NULL || p->mac_alg != GST_MIKEY_MAC_NULL)
1998 if (!(sub = gst_mikey_payload_kemac_get_sub (payload, 0)))
2001 if (sub->type != GST_MIKEY_PT_KEY_DATA)
2004 pkd = (GstMIKEYPayloadKeyData *) sub;
2006 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
2008 gst_caps_set_simple (caps, "srtp-key", GST_TYPE_BUFFER, buf, NULL);
2009 gst_buffer_unref (buf);
2012 gst_caps_set_simple (caps,
2013 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
2014 "srtp-auth", G_TYPE_STRING, srtp_auth,
2015 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
2016 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
2020 gst_mikey_message_unref (msg);
2026 * Mapping SDP attributes to caps
2028 * prepend 'a-' to IANA registered sdp attributes names
2029 * (ie: not prefixed with 'x-') in order to avoid
2030 * collision with gstreamer standard caps properties names
2033 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
2035 if (attributes->len > 0) {
2039 s = gst_caps_get_structure (caps, 0);
2041 for (i = 0; i < attributes->len; i++) {
2042 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
2043 gchar *tofree, *key;
2047 /* skip some of the attribute we already handle */
2048 if (!strcmp (key, "fmtp"))
2050 if (!strcmp (key, "rtpmap"))
2052 if (!strcmp (key, "control"))
2054 if (!strcmp (key, "range"))
2056 if (!strcmp (key, "framesize"))
2058 if (g_str_equal (key, "key-mgmt")) {
2059 parse_keymgmt (attr->value, caps);
2063 /* string must be valid UTF8 */
2064 if (!g_utf8_validate (attr->value, -1, NULL))
2067 if (!g_str_has_prefix (key, "x-"))
2068 tofree = key = g_strdup_printf ("a-%s", key);
2072 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
2073 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
2079 static const gchar *
2080 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
2089 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
2092 if (sscanf (attr, "%d ", &val) != 1)
2102 * Mapping of caps to and from SDP fields:
2104 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
2105 * a=framesize:<payload> <width>-<height>
2106 * a=fmtp:<payload> <param>[=<value>];...
2109 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
2112 const gchar *rtpmap;
2114 const gchar *framesize;
2117 gchar *params = NULL;
2123 /* get and parse rtpmap */
2124 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
2127 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
2129 g_warning ("error parsing rtpmap, ignoring");
2133 /* dynamic payloads need rtpmap or we fail */
2134 if (rtpmap == NULL && pt >= 96)
2137 /* check if we have a rate, if not, we need to look up the rate from the
2138 * default rates based on the payload types. */
2140 const GstRTPPayloadInfo *info;
2142 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
2143 /* dynamic types, use media and encoding_name */
2144 tmp = g_ascii_strdown (media->media, -1);
2145 info = gst_rtp_payload_info_for_name (tmp, name);
2148 /* static types, use payload type */
2149 info = gst_rtp_payload_info_for_pt (pt);
2153 if ((rate = info->clock_rate) == 0)
2156 /* we fail if we cannot find one */
2161 tmp = g_ascii_strdown (media->media, -1);
2162 caps = gst_caps_new_simple ("application/x-unknown",
2163 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
2165 s = gst_caps_get_structure (caps, 0);
2167 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
2169 /* encoding name must be upper case */
2171 tmp = g_ascii_strup (name, -1);
2172 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
2176 /* params must be lower case */
2177 if (params != NULL) {
2178 tmp = g_ascii_strdown (params, -1);
2179 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
2183 /* parse optional fmtp: field */
2184 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
2190 /* p is now of the format <payload> <param>[=<value>];... */
2191 PARSE_INT (p, " ", payload);
2192 if (payload != -1 && payload == pt) {
2196 /* <param>[=<value>] are separated with ';' */
2197 pairs = g_strsplit (p, ";", 0);
2198 for (i = 0; pairs[i]; i++) {
2200 const gchar *val, *key;
2202 const gchar *reserved_keys[] =
2203 { "media", "payload", "clock-rate", "encoding-name",
2207 /* the key may not have a '=', the value can have other '='s */
2208 valpos = strstr (pairs[i], "=");
2210 /* we have a '=' and thus a value, remove the '=' with \0 */
2212 /* value is everything between '=' and ';'. We split the pairs at ;
2213 * boundaries so we can take the remainder of the value. Some servers
2214 * put spaces around the value which we strip off here. Alternatively
2215 * we could strip those spaces in the depayloaders should these spaces
2216 * actually carry any meaning in the future. */
2217 val = g_strstrip (valpos + 1);
2219 /* simple <param>;.. is translated into <param>=1;... */
2222 /* strip the key of spaces, convert key to lowercase but not the value. */
2223 key = g_strstrip (pairs[i]);
2225 /* skip keys from the fmtp, which we already use ourselves for the
2226 * caps. Some software is adding random things like clock-rate into
2227 * the fmtp, and we would otherwise here set a string-typed clock-rate
2228 * in the caps... and thus fail to create valid RTP caps
2230 for (j = 0; j < G_N_ELEMENTS (reserved_keys); j++) {
2231 if (g_ascii_strcasecmp (reserved_keys[j], key) == 0) {
2237 if (strlen (key) > 1) {
2238 tmp = g_ascii_strdown (key, -1);
2239 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
2247 /* parse framesize: field */
2248 if ((framesize = gst_sdp_media_get_attribute_val (media, "framesize"))) {
2251 /* p is now of the format <payload> <width>-<height> */
2252 p = (gchar *) framesize;
2254 PARSE_INT (p, " ", payload);
2255 if (payload != -1 && payload == pt) {
2256 gst_structure_set (s, "a-framesize", G_TYPE_STRING, p, NULL);
2264 g_warning ("rtpmap type not given for dynamic payload %d", pt);
2269 g_warning ("rate unknown for payload type %d", pt);
2275 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
2276 gint * rtpport, gint * rtcpport)
2279 GstStateChangeReturn ret;
2280 GstElement *udpsrc0, *udpsrc1;
2281 gint tmp_rtp, tmp_rtcp;
2285 src = stream->parent;
2291 /* Start at next port */
2292 tmp_rtp = src->next_port_num;
2294 if (stream->is_ipv6)
2295 host = "udp://[::0]";
2297 host = "udp://0.0.0.0";
2299 /* try to allocate 2 UDP ports, the RTP port should be an even
2300 * number and the RTCP port should be the next (uneven) port */
2303 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
2304 tmp_rtp >= src->client_port_range.max)
2307 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2308 if (udpsrc0 == NULL)
2309 goto no_udp_protocol;
2310 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
2312 if (src->udp_buffer_size != 0)
2313 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
2316 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
2317 if (ret == GST_STATE_CHANGE_FAILURE) {
2319 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
2322 if (++count > src->retry)
2325 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2326 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2327 gst_object_unref (udpsrc0);
2330 GST_DEBUG_OBJECT (src, "retry %d", count);
2333 goto no_udp_protocol;
2336 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
2337 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
2339 /* check if port is even */
2340 if ((tmp_rtp & 0x01) != 0) {
2341 /* port not even, close and allocate another */
2342 if (++count > src->retry)
2345 GST_DEBUG_OBJECT (src, "RTP port not even");
2347 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2348 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2349 gst_object_unref (udpsrc0);
2352 GST_DEBUG_OBJECT (src, "retry %d", count);
2357 /* allocate port+1 for RTCP now */
2358 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2359 if (udpsrc1 == NULL)
2360 goto no_udp_rtcp_protocol;
2363 tmp_rtcp = tmp_rtp + 1;
2364 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
2367 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
2369 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
2370 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
2371 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
2372 if (ret == GST_STATE_CHANGE_FAILURE) {
2373 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
2375 if (++count > src->retry)
2378 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2379 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2380 gst_object_unref (udpsrc0);
2383 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2384 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2385 gst_object_unref (udpsrc1);
2389 GST_DEBUG_OBJECT (src, "retry %d", count);
2393 /* all fine, do port check */
2394 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2395 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2397 /* this should not happen... */
2398 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2401 /* we keep these elements, we configure all in configure_transport when the
2402 * server told us to really use the UDP ports. */
2403 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2404 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2405 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2406 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2408 /* keep track of next available port number when we have a range
2410 if (src->next_port_num != 0)
2411 src->next_port_num = tmp_rtcp + 1;
2418 GST_DEBUG_OBJECT (src, "could not get UDP source");
2423 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2427 no_udp_rtcp_protocol:
2429 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2434 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2435 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2441 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2442 gst_object_unref (udpsrc0);
2445 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2446 gst_object_unref (udpsrc1);
2453 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2458 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2460 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2461 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2464 for (i = 0; i < 2; i++) {
2465 if (stream->udpsrc[i])
2466 gst_element_set_state (stream->udpsrc[i], state);
2472 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2479 event = gst_event_new_flush_start ();
2480 GST_DEBUG_OBJECT (src, "start flush");
2482 state = GST_STATE_PAUSED;
2484 event = gst_event_new_flush_stop (FALSE);
2485 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2488 state = GST_STATE_PLAYING;
2490 state = GST_STATE_PAUSED;
2492 gst_rtspsrc_push_event (src, event);
2493 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2494 gst_rtspsrc_set_state (src, state);
2497 static GstRTSPResult
2498 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2499 GstRTSPMessage * message, GTimeVal * timeout)
2504 ret = gst_rtsp_connection_send (conn, message, timeout);
2506 ret = GST_RTSP_ERROR;
2511 static GstRTSPResult
2512 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2513 GstRTSPMessage * message, GTimeVal * timeout)
2518 ret = gst_rtsp_connection_receive (conn, message, timeout);
2520 ret = GST_RTSP_ERROR;
2526 gst_rtspsrc_get_position (GstRTSPSrc * src)
2531 query = gst_query_new_position (GST_FORMAT_TIME);
2532 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2533 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2534 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2538 if (stream->srcpad) {
2539 if (gst_pad_query (stream->srcpad, query)) {
2540 gst_query_parse_position (query, &fmt, &pos);
2541 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2542 GST_TIME_ARGS (pos));
2543 src->last_pos = pos;
2553 gst_query_unref (query);
2557 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2562 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2564 gboolean flush, skip;
2567 GstSegment seeksegment = { 0, };
2571 GST_DEBUG_OBJECT (src, "doing seek with event");
2573 gst_event_parse_seek (event, &rate, &format, &flags,
2574 &cur_type, &cur, &stop_type, &stop);
2576 /* no negative rates yet */
2580 /* we need TIME format */
2581 if (format != src->segment.format)
2584 GST_DEBUG_OBJECT (src, "doing seek without event");
2586 cur_type = GST_SEEK_TYPE_SET;
2587 stop_type = GST_SEEK_TYPE_SET;
2590 /* get flush flag */
2591 flush = flags & GST_SEEK_FLAG_FLUSH;
2592 skip = flags & GST_SEEK_FLAG_SKIP;
2594 /* now we need to make sure the streaming thread is stopped. We do this by
2595 * either sending a FLUSH_START event downstream which will cause the
2596 * streaming thread to stop with a WRONG_STATE.
2597 * For a non-flushing seek we simply pause the task, which will happen as soon
2598 * as it completes one iteration (and thus might block when the sink is
2599 * blocking in preroll). */
2601 GST_DEBUG_OBJECT (src, "starting flush");
2602 gst_rtspsrc_flush (src, TRUE, FALSE);
2605 gst_task_pause (src->task);
2609 /* we should now be able to grab the streaming thread because we stopped it
2610 * with the above flush/pause code */
2611 GST_RTSP_STREAM_LOCK (src);
2613 GST_DEBUG_OBJECT (src, "stopped streaming");
2615 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2616 gst_rtspsrc_connection_flush (src, FALSE);
2618 /* copy segment, we need this because we still need the old
2619 * segment when we close the current segment. */
2620 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2622 /* configure the seek parameters in the seeksegment. We will then have the
2623 * right values in the segment to perform the seek */
2625 GST_DEBUG_OBJECT (src, "configuring seek");
2626 gst_segment_do_seek (&seeksegment, rate, format, flags,
2627 cur_type, cur, stop_type, stop, &update);
2630 /* figure out the last position we need to play. If it's configured (stop !=
2631 * -1), use that, else we play until the total duration of the file */
2632 if ((stop = seeksegment.stop) == -1)
2633 stop = seeksegment.duration;
2635 playing = (src->state == GST_RTSP_STATE_PLAYING);
2637 /* if we were playing, pause first */
2639 /* obtain current position in case seek fails */
2640 gst_rtspsrc_get_position (src);
2641 gst_rtspsrc_pause (src, FALSE);
2645 src->state = GST_RTSP_STATE_SEEKING;
2647 /* PLAY will add the range header now. */
2648 src->need_range = TRUE;
2650 /* and continue playing */
2652 gst_rtspsrc_play (src, &seeksegment, FALSE);
2654 /* prepare for streaming again */
2656 /* if we started flush, we stop now */
2657 GST_DEBUG_OBJECT (src, "stopping flush");
2658 gst_rtspsrc_flush (src, FALSE, playing);
2661 /* now we did the seek and can activate the new segment values */
2662 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2664 /* if we're doing a segment seek, post a SEGMENT_START message */
2665 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2666 gst_element_post_message (GST_ELEMENT_CAST (src),
2667 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2668 src->segment.format, src->segment.position));
2671 /* now create the newsegment */
2672 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2673 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2676 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2677 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2678 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2679 stream->discont = TRUE;
2682 GST_RTSP_STREAM_UNLOCK (src);
2689 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2694 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2700 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2704 gboolean res = TRUE;
2707 src = GST_RTSPSRC_CAST (parent);
2709 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2710 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2712 switch (GST_EVENT_TYPE (event)) {
2713 case GST_EVENT_SEEK:
2714 res = gst_rtspsrc_perform_seek (src, event);
2718 case GST_EVENT_NAVIGATION:
2719 case GST_EVENT_LATENCY:
2727 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2728 res = gst_pad_send_event (target, event);
2729 gst_object_unref (target);
2731 gst_event_unref (event);
2734 gst_event_unref (event);
2740 /* this is the final event function we receive on the internal source pad when
2741 * we deal with TCP connections */
2743 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2748 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2750 switch (GST_EVENT_TYPE (event)) {
2751 case GST_EVENT_SEEK:
2753 case GST_EVENT_NAVIGATION:
2754 case GST_EVENT_LATENCY:
2756 gst_event_unref (event);
2763 /* this is the final query function we receive on the internal source pad when
2764 * we deal with TCP connections */
2766 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2770 gboolean res = TRUE;
2772 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2774 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2775 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2777 switch (GST_QUERY_TYPE (query)) {
2778 case GST_QUERY_POSITION:
2783 case GST_QUERY_DURATION:
2787 gst_query_parse_duration (query, &format, NULL);
2790 case GST_FORMAT_TIME:
2791 gst_query_set_duration (query, format, src->segment.duration);
2799 case GST_QUERY_LATENCY:
2801 /* we are live with a min latency of 0 and unlimited max latency, this
2802 * result will be updated by the session manager if there is any. */
2803 gst_query_set_latency (query, TRUE, 0, -1);
2813 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2815 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2819 gboolean res = FALSE;
2821 src = GST_RTSPSRC_CAST (parent);
2823 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2824 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2826 switch (GST_QUERY_TYPE (query)) {
2827 case GST_QUERY_DURATION:
2831 gst_query_parse_duration (query, &format, NULL);
2834 case GST_FORMAT_TIME:
2835 gst_query_set_duration (query, format, src->segment.duration);
2843 case GST_QUERY_SEEKING:
2847 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2848 if (format == GST_FORMAT_TIME) {
2850 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2852 /* seeking without duration is unlikely */
2853 seekable = seekable && src->seekable && src->segment.duration &&
2854 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2856 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0,
2857 src->segment.duration);
2866 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2868 gst_query_set_uri (query, uri);
2876 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2878 /* forward the query to the proxy target pad */
2880 res = gst_pad_query (target, query);
2881 gst_object_unref (target);
2890 /* callback for RTCP messages to be sent to the server when operating in TCP
2892 static GstFlowReturn
2893 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2896 GstRTSPStream *stream;
2897 GstFlowReturn res = GST_FLOW_OK;
2902 GstRTSPMessage message = { 0 };
2903 GstRTSPConnection *conn;
2905 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2906 src = stream->parent;
2908 gst_buffer_map (buffer, &map, GST_MAP_READ);
2912 gst_rtsp_message_init_data (&message, stream->channel[1]);
2914 /* lend the body data to the message */
2915 gst_rtsp_message_take_body (&message, data, size);
2917 if (stream->conninfo.connection)
2918 conn = stream->conninfo.connection;
2920 conn = src->conninfo.connection;
2922 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2923 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2924 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2926 /* and steal it away again because we will free it when unreffing the
2928 gst_rtsp_message_steal_body (&message, &data, &size);
2929 gst_rtsp_message_unset (&message);
2931 gst_buffer_unmap (buffer, &map);
2932 gst_buffer_unref (buffer);
2937 static GstPadProbeReturn
2938 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2940 GstRTSPSrc *src = user_data;
2942 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2943 GST_DEBUG_PAD_NAME (pad));
2945 /* activate the streams */
2946 GST_OBJECT_LOCK (src);
2947 if (!src->need_activate)
2950 src->need_activate = FALSE;
2951 GST_OBJECT_UNLOCK (src);
2953 gst_rtspsrc_activate_streams (src);
2955 return GST_PAD_PROBE_OK;
2959 GST_OBJECT_UNLOCK (src);
2960 return GST_PAD_PROBE_OK;
2965 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
2967 GstPad *gpad = GST_PAD_CAST (user_data);
2969 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
2970 gst_pad_store_sticky_event (gpad, *event);
2975 /* this callback is called when the session manager generated a new src pad with
2976 * payloaded RTP packets. We simply ghost the pad here. */
2978 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2981 GstPadTemplate *template;
2984 GstRTSPStream *stream;
2987 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2989 GST_RTSP_STATE_LOCK (src);
2991 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2992 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2993 goto unknown_stream;
2995 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2997 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2999 goto unknown_stream;
3002 stream->ssrc = ssrc;
3004 /* we'll add it later see below */
3005 stream->added = TRUE;
3007 /* check if we added all streams */
3009 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
3010 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
3012 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
3013 ostream, ostream->container, ostream->added, ostream->setup);
3015 /* if we find a stream for which we did a setup that is not added, we
3016 * need to wait some more */
3017 if (ostream->setup && !ostream->added) {
3022 GST_RTSP_STATE_UNLOCK (src);
3024 /* create a new pad we will use to stream to */
3025 template = gst_static_pad_template_get (&rtptemplate);
3026 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
3027 gst_object_unref (template);
3030 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3031 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3032 gst_pad_set_active (stream->srcpad, TRUE);
3033 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
3034 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3037 GST_DEBUG_OBJECT (src, "We added all streams");
3038 /* when we get here, all stream are added and we can fire the no-more-pads
3040 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
3048 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
3049 GST_RTSP_STATE_UNLOCK (src);
3056 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
3060 len = stream->ptmap->len;
3061 for (i = 0; i < len; i++) {
3062 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3070 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
3072 GstRTSPStream *stream;
3075 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
3077 GST_RTSP_STATE_LOCK (src);
3078 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3080 goto unknown_stream;
3082 if ((caps = stream_get_caps_for_pt (stream, pt)))
3083 gst_caps_ref (caps);
3084 GST_RTSP_STATE_UNLOCK (src);
3090 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
3091 GST_RTSP_STATE_UNLOCK (src);
3097 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
3099 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
3105 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
3111 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
3117 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
3119 GstRTSPSrc *src = stream->parent;
3122 g_object_get (source, "ssrc", &ssrc, NULL);
3124 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
3125 ssrc, stream->ssrc, stream->id);
3127 if (ssrc == stream->ssrc)
3128 gst_rtspsrc_do_stream_eos (src, stream);
3132 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
3134 GstRTSPSrc *src = stream->parent;
3137 g_object_get (source, "ssrc", &ssrc, NULL);
3139 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
3140 ssrc, stream->ssrc, stream->id);
3142 if (ssrc == stream->ssrc)
3143 gst_rtspsrc_do_stream_eos (src, stream);
3147 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
3149 GstRTSPStream *stream;
3151 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
3153 /* get stream for session */
3154 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3156 gst_rtspsrc_do_stream_eos (src, stream);
3161 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
3163 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
3168 set_manager_buffer_mode (GstRTSPSrc * src)
3170 GObjectClass *klass;
3172 if (src->manager == NULL)
3175 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3177 if (!g_object_class_find_property (klass, "buffer-mode"))
3180 if (src->buffer_mode != BUFFER_MODE_AUTO) {
3181 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
3186 GST_DEBUG_OBJECT (src,
3187 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
3189 if (src->provided_clock) {
3190 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
3192 if (clock == src->provided_clock) {
3193 GST_DEBUG_OBJECT (src, "selected synced");
3194 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
3197 gst_object_unref (clock);
3202 /* Otherwise fall-through and use another buffer mode */
3204 gst_object_unref (clock);
3207 GST_DEBUG_OBJECT (src, "auto buffering mode");
3208 if (src->use_buffering) {
3209 GST_DEBUG_OBJECT (src, "selected buffer");
3210 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
3212 GST_DEBUG_OBJECT (src, "selected slave");
3213 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
3218 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
3220 GST_DEBUG ("request key %u", ssrc);
3221 return gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
3225 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
3227 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3228 if (stream->id != session)
3231 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3232 stream->profile != GST_RTSP_PROFILE_SAVPF)
3235 if (stream->srtpdec == NULL) {
3238 name = g_strdup_printf ("srtpdec_%u", session);
3239 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
3242 g_signal_connect (stream->srtpdec, "request-key",
3243 (GCallback) request_key, stream);
3245 return gst_object_ref (stream->srtpdec);
3249 request_rtcp_encoder (GstElement * rtpbin, guint session,
3250 GstRTSPStream * stream)
3255 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3256 if (stream->id != session)
3259 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3260 stream->profile != GST_RTSP_PROFILE_SAVPF)
3263 if (stream->srtpenc == NULL) {
3266 name = g_strdup_printf ("srtpenc_%u", session);
3267 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
3270 /* get RTCP crypto parameters from caps */
3271 s = gst_caps_get_structure (stream->srtcpparams, 0);
3275 GType ciphertype, authtype;
3276 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
3278 ciphertype = g_type_from_name ("GstSrtpCipherType");
3279 authtype = g_type_from_name ("GstSrtpAuthType");
3280 g_value_init (&rtcp_cipher, ciphertype);
3281 g_value_init (&rtcp_auth, authtype);
3283 str = gst_structure_get_string (s, "srtcp-cipher");
3284 gst_value_deserialize (&rtcp_cipher, str);
3285 str = gst_structure_get_string (s, "srtcp-auth");
3286 gst_value_deserialize (&rtcp_auth, str);
3287 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
3289 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
3291 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
3293 g_object_set (stream->srtpenc, "key", buf, NULL);
3295 g_value_unset (&rtcp_cipher);
3296 g_value_unset (&rtcp_auth);
3297 gst_buffer_unref (buf);
3300 name = g_strdup_printf ("rtcp_sink_%d", session);
3301 pad = gst_element_get_request_pad (stream->srtpenc, name);
3303 gst_object_unref (pad);
3305 return gst_object_ref (stream->srtpenc);
3309 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
3311 GstElement *rtx, *bin;
3314 GstRTSPStream *stream;
3316 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
3318 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
3322 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
3323 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
3324 bin = gst_bin_new (NULL);
3325 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
3326 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
3327 gst_bin_add (GST_BIN (bin), rtx);
3329 pad = gst_element_get_static_pad (rtx, "src");
3330 name = g_strdup_printf ("src_%u", sessid);
3331 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3333 gst_object_unref (pad);
3335 pad = gst_element_get_static_pad (rtx, "sink");
3336 name = g_strdup_printf ("sink_%u", sessid);
3337 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3339 gst_object_unref (pad);
3345 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
3349 gboolean do_retransmission = FALSE;
3351 if (transport->trans != GST_RTSP_TRANS_RTP)
3353 if (transport->profile != GST_RTSP_PROFILE_AVPF &&
3354 transport->profile != GST_RTSP_PROFILE_SAVPF)
3357 signal_id = g_signal_lookup ("request-aux-receiver",
3358 G_OBJECT_TYPE (src->manager));
3359 /* there's already something connected */
3360 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
3361 NULL, NULL, NULL) != 0) {
3362 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
3363 "\"request-aux-receiver\" signal is "
3364 "already used by the application");
3368 /* build the retransmission payload type map */
3369 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3370 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3371 gboolean do_retransmission_stream = FALSE;
3374 if (stream->rtx_pt_map)
3375 gst_structure_free (stream->rtx_pt_map);
3376 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
3378 for (i = 0; i < stream->ptmap->len; i++) {
3379 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3380 GstStructure *s = gst_caps_get_structure (item->caps, 0);
3381 const gchar *encoding;
3383 /* we only care about RTX streams */
3384 if ((encoding = gst_structure_get_string (s, "encoding-name"))
3385 && g_strcmp0 (encoding, "RTX") == 0) {
3386 const gchar *stream_pt_s;
3389 if (gst_structure_get_int (s, "payload", &rtx_pt)
3390 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
3393 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
3395 do_retransmission_stream = TRUE;
3401 if (do_retransmission_stream) {
3402 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
3403 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
3404 do_retransmission = TRUE;
3406 GST_DEBUG_OBJECT (src, "no retransmission payload map for stream "
3407 "id %i", stream->id);
3408 gst_structure_free (stream->rtx_pt_map);
3409 stream->rtx_pt_map = NULL;
3413 if (do_retransmission) {
3414 GST_DEBUG_OBJECT (src, "Enabling retransmissions");
3416 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
3418 /* enable RFC4588 retransmission handling by setting rtprtxreceive
3419 * as the "aux" element of rtpbin */
3420 g_signal_connect (src->manager, "request-aux-receiver",
3421 (GCallback) request_aux_receiver, src);
3423 GST_DEBUG_OBJECT (src,
3424 "Not enabling retransmissions as no stream had a retransmission payload map");
3428 /* try to get and configure a manager */
3430 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3431 GstRTSPTransport * transport)
3433 const gchar *manager;
3435 GstStateChangeReturn ret;
3437 /* find a manager */
3438 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3442 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3444 /* configure the manager */
3445 if (src->manager == NULL) {
3446 GObjectClass *klass;
3448 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3450 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3454 goto use_no_manager;
3456 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3457 goto manager_failed;
3460 /* we manage this element */
3461 gst_element_set_locked_state (src->manager, TRUE);
3462 gst_bin_add (GST_BIN_CAST (src), src->manager);
3464 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3465 if (ret == GST_STATE_CHANGE_FAILURE)
3466 goto start_manager_failure;
3468 g_object_set (src->manager, "latency", src->latency, NULL);
3470 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3472 if (g_object_class_find_property (klass, "ntp-sync")) {
3473 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3476 if (src->use_pipeline_clock) {
3477 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3478 g_object_set (src->manager, "use-pipeline-clock", TRUE, NULL);
3481 if (g_object_class_find_property (klass, "ntp-time-source")) {
3482 g_object_set (src->manager, "ntp-time-source", src->ntp_time_source,
3487 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3488 g_object_set (src->manager, "sdes", src->sdes, NULL);
3491 if (g_object_class_find_property (klass, "drop-on-latency")) {
3492 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3496 /* buffer mode pauses are handled by adding offsets to buffer times,
3497 * but some depayloaders may have a hard time syncing output times
3498 * with such input times, e.g. container ones, most notably ASF */
3499 /* TODO alternatives are having an event that indicates these shifts,
3500 * or having rtsp extensions provide suggestion on buffer mode */
3501 /* valid duration implies not likely live pipeline,
3502 * so slaving in jitterbuffer does not make much sense
3503 * (and might mess things up due to bursts) */
3504 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3505 src->segment.duration && stream->container) {
3506 src->use_buffering = TRUE;
3508 src->use_buffering = FALSE;
3511 set_manager_buffer_mode (src);
3513 /* connect to signals */
3514 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3516 src->manager_sig_id =
3517 g_signal_connect (src->manager, "pad-added",
3518 (GCallback) new_manager_pad, src);
3519 src->manager_ptmap_id =
3520 g_signal_connect (src->manager, "request-pt-map",
3521 (GCallback) request_pt_map, src);
3523 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3526 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3529 if (src->do_retransmission)
3530 add_retransmission (src, transport);
3532 g_signal_connect (src->manager, "request-rtp-decoder",
3533 (GCallback) request_rtp_decoder, stream);
3534 g_signal_connect (src->manager, "request-rtcp-decoder",
3535 (GCallback) request_rtp_decoder, stream);
3536 g_signal_connect (src->manager, "request-rtcp-encoder",
3537 (GCallback) request_rtcp_encoder, stream);
3539 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3540 * into a separate RTP session. */
3541 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3542 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3544 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3545 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3548 /* now configure the bandwidth in the manager */
3549 if (g_signal_lookup ("get-internal-session",
3550 G_OBJECT_TYPE (src->manager)) != 0) {
3551 GObject *rtpsession;
3553 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3556 GstRTPProfile rtp_profile;
3558 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3560 stream->session = rtpsession;
3562 if (stream->as_bandwidth != -1) {
3563 GST_INFO_OBJECT (src, "setting AS: %f",
3564 (gdouble) (stream->as_bandwidth * 1000));
3565 g_object_set (rtpsession, "bandwidth",
3566 (gdouble) (stream->as_bandwidth * 1000), NULL);
3568 if (stream->rr_bandwidth != -1) {
3569 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3570 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3573 if (stream->rs_bandwidth != -1) {
3574 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3575 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3579 switch (stream->profile) {
3580 case GST_RTSP_PROFILE_AVPF:
3581 rtp_profile = GST_RTP_PROFILE_AVPF;
3583 case GST_RTSP_PROFILE_SAVP:
3584 rtp_profile = GST_RTP_PROFILE_SAVP;
3586 case GST_RTSP_PROFILE_SAVPF:
3587 rtp_profile = GST_RTP_PROFILE_SAVPF;
3589 case GST_RTSP_PROFILE_AVP:
3591 rtp_profile = GST_RTP_PROFILE_AVP;
3595 g_object_set (rtpsession, "rtp-profile", rtp_profile, NULL);
3597 g_object_set (rtpsession, "probation", src->probation, NULL);
3599 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3601 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3603 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3605 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3607 g_signal_connect (rtpsession, "on-ssrc-active",
3608 (GCallback) on_ssrc_active, stream);
3619 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3624 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3627 start_manager_failure:
3629 GST_DEBUG_OBJECT (src, "could not start session manager");
3634 /* free the UDP sources allocated when negotiating a transport.
3635 * This function is called when the server negotiated to a transport where the
3636 * UDP sources are not needed anymore, such as TCP or multicast. */
3638 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3642 for (i = 0; i < 2; i++) {
3643 if (stream->udpsrc[i]) {
3644 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3645 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3646 gst_object_unref (stream->udpsrc[i]);
3647 stream->udpsrc[i] = NULL;
3652 /* for TCP, create pads to send and receive data to and from the manager and to
3653 * intercept various events and queries
3656 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3657 GstRTSPTransport * transport, GstPad ** outpad)
3660 GstPadTemplate *template;
3661 GstPad *pad0, *pad1;
3663 /* configure for interleaved delivery, nothing needs to be done
3664 * here, the loop function will call the chain functions of the
3665 * session manager. */
3666 stream->channel[0] = transport->interleaved.min;
3667 stream->channel[1] = transport->interleaved.max;
3668 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3669 stream->channel[0], stream->channel[1]);
3671 /* we can remove the allocated UDP ports now */
3672 gst_rtspsrc_stream_free_udp (stream);
3674 /* no session manager, send data to srcpad directly */
3675 if (!stream->channelpad[0]) {
3676 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3678 /* create a new pad we will use to stream to */
3679 name = g_strdup_printf ("stream_%u", stream->id);
3680 template = gst_static_pad_template_get (&rtptemplate);
3681 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3682 gst_object_unref (template);
3685 /* set caps and activate */
3686 gst_pad_use_fixed_caps (stream->channelpad[0]);
3687 gst_pad_set_active (stream->channelpad[0], TRUE);
3689 *outpad = gst_object_ref (stream->channelpad[0]);
3691 GST_DEBUG_OBJECT (src, "using manager source pad");
3693 template = gst_static_pad_template_get (&anysrctemplate);
3695 /* allocate pads for sending the channel data into the manager */
3696 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3697 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3698 gst_object_unref (stream->channelpad[0]);
3699 stream->channelpad[0] = pad0;
3700 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3701 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3702 gst_pad_set_element_private (pad0, src);
3703 gst_pad_set_active (pad0, TRUE);
3705 if (stream->channelpad[1]) {
3706 /* if we have a sinkpad for the other channel, create a pad and link to the
3708 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3709 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3710 gst_pad_link_full (pad1, stream->channelpad[1],
3711 GST_PAD_LINK_CHECK_NOTHING);
3712 gst_object_unref (stream->channelpad[1]);
3713 stream->channelpad[1] = pad1;
3714 gst_pad_set_active (pad1, TRUE);
3716 gst_object_unref (template);
3718 /* setup RTCP transport back to the server if we have to. */
3719 if (src->manager && src->do_rtcp) {
3722 template = gst_static_pad_template_get (&anysinktemplate);
3724 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3725 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3726 gst_pad_set_element_private (stream->rtcppad, stream);
3727 gst_pad_set_active (stream->rtcppad, TRUE);
3729 /* get session RTCP pad */
3730 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3731 pad = gst_element_get_request_pad (src->manager, name);
3736 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3737 gst_object_unref (pad);
3740 gst_object_unref (template);
3746 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3747 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3748 gint * max, guint * ttl)
3750 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3752 if (!(*destination = transport->destination))
3753 *destination = stream->destination;
3756 /* transport first */
3757 *min = transport->port.min;
3758 *max = transport->port.max;
3759 if (*min == -1 && *max == -1) {
3760 /* then try from SDP */
3761 if (stream->port != 0) {
3762 *min = stream->port;
3763 *max = stream->port + 1;
3769 if (!(*ttl = transport->ttl))
3774 /* first take the source, then the endpoint to figure out where to send
3776 if (!(*destination = transport->source)) {
3777 if (src->conninfo.connection)
3778 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3779 else if (stream->conninfo.connection)
3781 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3785 /* for unicast we only expect the ports here */
3786 *min = transport->server_port.min;
3787 *max = transport->server_port.max;
3792 /* For multicast create UDP sources and join the multicast group. */
3794 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3795 GstRTSPTransport * transport, GstPad ** outpad)
3798 const gchar *destination;
3801 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3803 /* we can remove the allocated UDP ports now */
3804 gst_rtspsrc_stream_free_udp (stream);
3806 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3809 /* we need a destination now */
3810 if (destination == NULL)
3811 goto no_destination;
3813 /* we really need ports now or we won't be able to receive anything at all */
3814 if (min == -1 && max == -1)
3817 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3818 destination, min, max);
3820 /* creating UDP source for RTP */
3822 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3824 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3826 if (stream->udpsrc[0] == NULL)
3829 /* take ownership */
3830 gst_object_ref_sink (stream->udpsrc[0]);
3832 if (src->udp_buffer_size != 0)
3833 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3834 src->udp_buffer_size, NULL);
3836 if (src->multi_iface != NULL)
3837 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3838 src->multi_iface, NULL);
3841 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3842 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3845 /* creating another UDP source for RTCP */
3849 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3851 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3853 if (stream->udpsrc[1] == NULL)
3856 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3857 stream->profile == GST_RTSP_PROFILE_SAVPF)
3858 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3860 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3861 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3862 gst_caps_unref (caps);
3864 /* take ownership */
3865 gst_object_ref_sink (stream->udpsrc[1]);
3867 if (src->multi_iface != NULL)
3868 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3869 src->multi_iface, NULL);
3871 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
3878 GST_DEBUG_OBJECT (src, "no UDP source element found");
3883 GST_DEBUG_OBJECT (src, "no destination found");
3888 GST_DEBUG_OBJECT (src, "no ports found");
3893 /* configure the remainder of the UDP ports */
3895 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3896 GstRTSPTransport * transport, GstPad ** outpad)
3898 /* we manage the UDP elements now. For unicast, the UDP sources where
3899 * allocated in the stream when we suggested a transport. */
3900 if (stream->udpsrc[0]) {
3903 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3904 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3906 GST_DEBUG_OBJECT (src, "setting up UDP source");
3908 /* configure a timeout on the UDP port. When the timeout message is
3909 * posted, we assume UDP transport is not possible. We reconnect using TCP
3911 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3912 src->udp_timeout * 1000, NULL);
3914 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
3915 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3917 /* get output pad of the UDP source. */
3918 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3920 /* save it so we can unblock */
3921 stream->blockedpad = *outpad;
3923 /* configure pad block on the pad. As soon as there is dataflow on the
3924 * UDP source, we know that UDP is not blocked by a firewall and we can
3925 * configure all the streams to let the application autoplug decoders. */
3927 gst_pad_add_probe (stream->blockedpad,
3928 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3929 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
3931 if (stream->channelpad[0]) {
3932 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3933 /* configure for UDP delivery, we need to connect the UDP pads to
3934 * the session plugin. */
3935 gst_pad_link_full (*outpad, stream->channelpad[0],
3936 GST_PAD_LINK_CHECK_NOTHING);
3937 gst_object_unref (*outpad);
3939 /* we connected to pad-added signal to get pads from the manager */
3941 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3946 if (stream->udpsrc[1]) {
3949 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3950 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3952 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3953 stream->profile == GST_RTSP_PROFILE_SAVPF)
3954 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3956 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3957 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3958 gst_caps_unref (caps);
3960 if (stream->channelpad[1]) {
3963 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3965 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3966 gst_pad_link_full (pad, stream->channelpad[1],
3967 GST_PAD_LINK_CHECK_NOTHING);
3968 gst_object_unref (pad);
3970 /* leave unlinked */
3976 /* configure the UDP sink back to the server for status reports */
3978 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3979 GstRTSPStream * stream, GstRTSPTransport * transport)
3982 gint rtp_port, rtcp_port;
3983 gboolean do_rtp, do_rtcp;
3984 const gchar *destination;
3989 /* get transport info */
3990 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3991 &rtp_port, &rtcp_port, &ttl);
3993 /* see what we need to do */
3994 do_rtp = (rtp_port != -1);
3995 /* it's possible that the server does not want us to send RTCP in which case
3997 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3999 /* we need a destination when we have RTP or RTCP ports */
4000 if (destination == NULL && (do_rtp || do_rtcp))
4001 goto no_destination;
4003 /* try to construct the fakesrc to the RTP port of the server to open up any
4006 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
4009 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
4010 stream->udpsink[0] =
4011 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
4013 if (stream->udpsink[0] == NULL)
4014 goto no_sink_element;
4016 /* don't join multicast group, we will have the source socket do that */
4017 /* no sync or async state changes needed */
4018 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
4019 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
4021 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4023 if (stream->udpsrc[0]) {
4024 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
4025 * so that NAT firewalls will open a hole for us */
4026 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
4027 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
4028 /* configure socket and make sure udpsink does not close it when shutting
4029 * down, it belongs to udpsrc after all. */
4030 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
4031 "close-socket", FALSE, NULL);
4032 g_object_unref (socket);
4035 /* the source for the dummy packets to open up NAT */
4036 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
4037 if (stream->fakesrc == NULL)
4038 goto no_fakesrc_element;
4040 /* random data in 5 buffers, a size of 200 bytes should be fine */
4041 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
4042 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
4044 /* we don't want to consider this a sink */
4045 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
4047 /* keep everything locked */
4048 gst_element_set_locked_state (stream->udpsink[0], TRUE);
4049 gst_element_set_locked_state (stream->fakesrc, TRUE);
4051 gst_object_ref (stream->udpsink[0]);
4052 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
4053 gst_object_ref (stream->fakesrc);
4054 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
4056 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
4057 "sink", GST_PAD_LINK_CHECK_NOTHING);
4060 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
4063 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
4064 stream->udpsink[1] =
4065 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
4067 if (stream->udpsink[1] == NULL)
4068 goto no_sink_element;
4070 /* don't join multicast group, we will have the source socket do that */
4071 /* no sync or async state changes needed */
4072 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
4073 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
4075 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4077 if (stream->udpsrc[1]) {
4078 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
4079 * because some servers check the port number of where it sends RTCP to identify
4080 * the RTCP packets it receives */
4081 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
4082 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
4083 /* configure socket and make sure udpsink does not close it when shutting
4084 * down, it belongs to udpsrc after all. */
4085 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
4086 "close-socket", FALSE, NULL);
4087 g_object_unref (socket);
4090 /* we don't want to consider this a sink */
4091 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
4093 /* we keep this playing always */
4094 gst_element_set_locked_state (stream->udpsink[1], TRUE);
4095 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
4097 gst_object_ref (stream->udpsink[1]);
4098 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
4100 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
4102 /* get session RTCP pad */
4103 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
4104 pad = gst_element_get_request_pad (src->manager, name);
4109 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
4110 gst_object_unref (pad);
4119 GST_DEBUG_OBJECT (src, "no destination address specified");
4124 GST_DEBUG_OBJECT (src, "no UDP sink element found");
4129 GST_DEBUG_OBJECT (src, "no fakesrc element found");
4134 /* sets up all elements needed for streaming over the specified transport.
4135 * Does not yet expose the element pads, this will be done when there is actuall
4136 * dataflow detected, which might never happen when UDP is blocked in a
4137 * firewall, for example.
4140 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
4141 GstRTSPTransport * transport)
4144 GstPad *outpad = NULL;
4145 GstPadTemplate *template;
4147 const gchar *media_type;
4150 src = stream->parent;
4152 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
4154 /* get the proper media type for this stream now */
4155 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
4156 goto unknown_transport;
4158 goto unknown_transport;
4160 /* configure the final media type */
4161 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
4163 len = stream->ptmap->len;
4164 for (i = 0; i < len; i++) {
4166 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
4168 if (item->caps == NULL)
4171 s = gst_caps_get_structure (item->caps, 0);
4172 gst_structure_set_name (s, media_type);
4173 /* set ssrc if known */
4174 if (transport->ssrc)
4175 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
4178 /* try to get and configure a manager, channelpad[0-1] will be configured with
4179 * the pads for the manager, or NULL when no manager is needed. */
4180 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
4183 switch (transport->lower_transport) {
4184 case GST_RTSP_LOWER_TRANS_TCP:
4185 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
4186 goto transport_failed;
4188 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
4189 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
4190 goto transport_failed;
4191 /* fallthrough, the rest is the same for UDP and MCAST */
4192 case GST_RTSP_LOWER_TRANS_UDP:
4193 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
4194 goto transport_failed;
4195 /* configure udpsinks back to the server for RTCP messages and for the
4196 * dummy RTP messages to open NAT. */
4197 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
4198 goto transport_failed;
4201 goto unknown_transport;
4205 GST_DEBUG_OBJECT (src, "creating ghostpad");
4207 gst_pad_use_fixed_caps (outpad);
4209 /* create ghostpad, don't add just yet, this will be done when we activate
4211 name = g_strdup_printf ("stream_%u", stream->id);
4212 template = gst_static_pad_template_get (&rtptemplate);
4213 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
4214 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
4215 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
4216 gst_object_unref (template);
4219 gst_object_unref (outpad);
4221 /* mark pad as ok */
4222 stream->last_ret = GST_FLOW_OK;
4229 GST_DEBUG_OBJECT (src, "failed to configure transport");
4234 GST_DEBUG_OBJECT (src, "unknown transport");
4239 GST_DEBUG_OBJECT (src, "cannot get a session manager");
4244 /* send a couple of dummy random packets on the receiver RTP port to the server,
4245 * this should make a firewall think we initiated the data transfer and
4246 * hopefully allow packets to go from the sender port to our RTP receiver port */
4248 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
4252 if (src->nat_method != GST_RTSP_NAT_DUMMY)
4255 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4256 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4258 if (stream->fakesrc && stream->udpsink[0]) {
4259 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
4260 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
4261 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
4262 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
4263 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
4269 /* Adds the source pads of all configured streams to the element.
4270 * This code is performed when we detected dataflow.
4272 * We detect dataflow from either the _loop function or with pad probes on the
4276 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
4280 GST_DEBUG_OBJECT (src, "activating streams");
4282 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4283 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4285 if (stream->udpsrc[0]) {
4286 /* remove timeout, we are streaming now and timeouts will be handled by
4287 * the session manager and jitter buffer */
4288 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
4290 if (stream->srcpad) {
4291 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
4292 gst_pad_set_active (stream->srcpad, TRUE);
4294 /* if we don't have a session manager, set the caps now. If we have a
4295 * session, we will get a notification of the pad and the caps. */
4296 if (!src->manager) {
4299 caps = stream_get_caps_for_pt (stream, stream->default_pt);
4300 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
4301 gst_pad_set_caps (stream->srcpad, caps);
4304 if (!stream->added) {
4305 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
4306 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
4307 stream->added = TRUE;
4312 /* unblock all pads */
4313 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4314 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4316 if (stream->blockid) {
4317 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
4318 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
4319 stream->blockid = 0;
4327 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
4328 gboolean reset_manager)
4331 guint64 start, stop;
4332 gdouble play_speed, play_scale;
4334 GST_DEBUG_OBJECT (src, "configuring stream caps");
4336 start = segment->position;
4337 stop = segment->duration;
4338 play_speed = segment->rate;
4339 play_scale = segment->applied_rate;
4341 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4342 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4348 len = stream->ptmap->len;
4349 for (j = 0; j < len; j++) {
4351 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
4353 if (item->caps == NULL)
4356 caps = gst_caps_make_writable (item->caps);
4358 if (stream->timebase != -1)
4359 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
4360 (guint) stream->timebase, NULL);
4361 if (stream->seqbase != -1)
4362 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
4363 (guint) stream->seqbase, NULL);
4364 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
4366 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
4367 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
4368 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
4371 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
4374 if (item->pt == stream->default_pt && stream->udpsrc[0]) {
4375 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4379 if (reset_manager && src->manager) {
4380 GST_DEBUG_OBJECT (src, "clear session");
4381 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
4385 static GstFlowReturn
4386 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
4391 /* store the value */
4392 stream->last_ret = ret;
4394 /* if it's success we can return the value right away */
4395 if (ret == GST_FLOW_OK)
4398 /* any other error that is not-linked can be returned right
4400 if (ret != GST_FLOW_NOT_LINKED)
4403 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4404 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4405 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4407 ret = ostream->last_ret;
4408 /* some other return value (must be SUCCESS but we can return
4409 * other values as well) */
4410 if (ret != GST_FLOW_NOT_LINKED)
4413 /* if we get here, all other pads were unlinked and we return
4414 * NOT_LINKED then */
4420 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4423 gboolean res = TRUE;
4425 /* only streams that have a connection to the outside world */
4429 if (stream->udpsrc[0]) {
4430 gst_event_ref (event);
4431 res = gst_element_send_event (stream->udpsrc[0], event);
4432 } else if (stream->channelpad[0]) {
4433 gst_event_ref (event);
4434 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4435 res = gst_pad_push_event (stream->channelpad[0], event);
4437 res = gst_pad_send_event (stream->channelpad[0], event);
4440 if (stream->udpsrc[1]) {
4441 gst_event_ref (event);
4442 res &= gst_element_send_event (stream->udpsrc[1], event);
4443 } else if (stream->channelpad[1]) {
4444 gst_event_ref (event);
4445 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4446 res &= gst_pad_push_event (stream->channelpad[1], event);
4448 res &= gst_pad_send_event (stream->channelpad[1], event);
4452 gst_event_unref (event);
4458 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4461 gboolean res = TRUE;
4463 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4464 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4466 gst_event_ref (event);
4467 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4469 gst_event_unref (event);
4474 static GstRTSPResult
4475 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4480 if (info->connection == NULL) {
4481 if (info->url == NULL) {
4482 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4483 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4487 /* create connection */
4488 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4489 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4490 goto could_not_create;
4493 g_free (info->url_str);
4494 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4496 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4498 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4499 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4500 src->tls_validation_flags))
4501 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4503 if (src->tls_database)
4504 gst_rtsp_connection_set_tls_database (info->connection,
4507 if (src->tls_interaction)
4508 gst_rtsp_connection_set_tls_interaction (info->connection,
4509 src->tls_interaction);
4512 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4513 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4515 if (src->proxy_host) {
4516 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4518 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4523 if (!info->connected) {
4526 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4527 ("Connecting to %s", info->location));
4528 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4530 gst_rtsp_connection_connect (info->connection,
4531 src->ptcp_timeout)) < 0)
4532 goto could_not_connect;
4534 info->connected = TRUE;
4541 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4546 gchar *str = gst_rtsp_strresult (res);
4547 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4553 gchar *str = gst_rtsp_strresult (res);
4554 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4560 static GstRTSPResult
4561 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4564 GST_RTSP_STATE_LOCK (src);
4565 if (info->connected) {
4566 GST_DEBUG_OBJECT (src, "closing connection...");
4567 gst_rtsp_connection_close (info->connection);
4568 info->connected = FALSE;
4570 if (free && info->connection) {
4571 /* free connection */
4572 GST_DEBUG_OBJECT (src, "freeing connection...");
4573 gst_rtsp_connection_free (info->connection);
4574 info->connection = NULL;
4576 GST_RTSP_STATE_UNLOCK (src);
4580 static GstRTSPResult
4581 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4586 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4587 gst_rtsp_conninfo_close (src, info, FALSE);
4588 res = gst_rtsp_conninfo_connect (src, info, async);
4594 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4598 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4599 GST_RTSP_STATE_LOCK (src);
4600 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4601 GST_DEBUG_OBJECT (src, "connection flush");
4602 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4603 src->conninfo.flushing = flush;
4605 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4606 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4607 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4608 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4609 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4610 stream->conninfo.flushing = flush;
4613 GST_RTSP_STATE_UNLOCK (src);
4616 static GstRTSPResult
4617 gst_rtspsrc_init_request (GstRTSPSrc * src, GstRTSPMessage * msg,
4618 GstRTSPMethod method, const gchar * uri)
4622 res = gst_rtsp_message_init_request (msg, method, uri);
4626 /* set user-agent */
4627 if (src->user_agent)
4628 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, src->user_agent);
4633 /* FIXME, handle server request, reply with OK, for now */
4634 static GstRTSPResult
4635 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
4636 GstRTSPMessage * request)
4638 GstRTSPMessage response = { 0 };
4641 GST_DEBUG_OBJECT (src, "got server request message");
4644 gst_rtsp_message_dump (request);
4646 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4648 if (res == GST_RTSP_ENOTIMPL) {
4649 /* default implementation, send OK */
4650 GST_DEBUG_OBJECT (src, "prepare OK reply");
4652 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4657 /* let app parse and reply */
4658 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4659 0, request, &response);
4662 gst_rtsp_message_dump (&response);
4664 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
4668 gst_rtsp_message_unset (&response);
4669 } else if (res == GST_RTSP_EEOF)
4677 gst_rtsp_message_unset (&response);
4682 /* send server keep-alive */
4683 static GstRTSPResult
4684 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4686 GstRTSPMessage request = { 0 };
4688 GstRTSPMethod method;
4689 const gchar *control;
4691 if (src->do_rtsp_keep_alive == FALSE) {
4692 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4693 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4697 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4699 /* find a method to use for keep-alive */
4700 if (src->methods & GST_RTSP_GET_PARAMETER)
4701 method = GST_RTSP_GET_PARAMETER;
4703 method = GST_RTSP_OPTIONS;
4705 control = get_aggregate_control (src);
4706 if (control == NULL)
4709 res = gst_rtspsrc_init_request (src, &request, method, control);
4714 gst_rtsp_message_dump (&request);
4717 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
4722 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4723 gst_rtsp_message_unset (&request);
4730 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4735 gchar *str = gst_rtsp_strresult (res);
4737 gst_rtsp_message_unset (&request);
4738 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4739 ("Could not send keep-alive. (%s)", str));
4745 static GstFlowReturn
4746 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4748 GstFlowReturn ret = GST_FLOW_OK;
4750 GstRTSPStream *stream;
4751 GstPad *outpad = NULL;
4757 channel = message->type_data.data.channel;
4759 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4761 goto unknown_stream;
4763 if (channel == stream->channel[0]) {
4764 outpad = stream->channelpad[0];
4766 } else if (channel == stream->channel[1]) {
4767 outpad = stream->channelpad[1];
4773 /* take a look at the body to figure out what we have */
4774 gst_rtsp_message_get_body (message, &data, &size);
4776 goto invalid_length;
4778 /* channels are not correct on some servers, do extra check */
4779 if (data[1] >= 200 && data[1] <= 204) {
4780 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4781 outpad = stream->channelpad[1];
4785 /* we have no clue what this is, just ignore then. */
4787 goto unknown_stream;
4789 /* take the message body for further processing */
4790 gst_rtsp_message_steal_body (message, &data, &size);
4792 /* strip the trailing \0 */
4795 buf = gst_buffer_new ();
4796 gst_buffer_append_memory (buf,
4797 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4799 /* don't need message anymore */
4800 gst_rtsp_message_unset (message);
4802 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4805 if (src->need_activate) {
4811 guint group_id = gst_util_group_id_next ();
4813 /* generate an SHA256 sum of the URI */
4814 cs = g_checksum_new (G_CHECKSUM_SHA256);
4815 uri = src->conninfo.location;
4816 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4818 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4819 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4823 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4824 event = gst_event_new_stream_start (stream_id);
4825 gst_event_set_group_id (event, group_id);
4828 gst_rtspsrc_stream_push_event (src, ostream, event);
4830 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
4831 /* only streams that have a connection to the outside world */
4832 if (ostream->setup) {
4833 if (ostream->udpsrc[0]) {
4834 gst_element_send_event (ostream->udpsrc[0],
4835 gst_event_new_caps (caps));
4836 } else if (ostream->channelpad[0]) {
4837 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
4838 gst_pad_push_event (ostream->channelpad[0],
4839 gst_event_new_caps (caps));
4841 gst_pad_send_event (ostream->channelpad[0],
4842 gst_event_new_caps (caps));
4845 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4847 if (ostream->udpsrc[1]) {
4848 gst_element_send_event (ostream->udpsrc[1],
4849 gst_event_new_caps (caps));
4850 } else if (ostream->channelpad[1]) {
4851 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
4852 gst_pad_push_event (ostream->channelpad[1],
4853 gst_event_new_caps (caps));
4855 gst_pad_send_event (ostream->channelpad[1],
4856 gst_event_new_caps (caps));
4859 gst_caps_unref (caps);
4863 g_checksum_free (cs);
4865 gst_rtspsrc_activate_streams (src);
4866 src->need_activate = FALSE;
4867 src->need_segment = TRUE;
4870 if (src->base_time == -1) {
4871 /* Take current running_time. This timestamp will be put on
4872 * the first buffer of each stream because we are a live source and so we
4873 * timestamp with the running_time. When we are dealing with TCP, we also
4874 * only timestamp the first buffer (using the DISCONT flag) because a server
4875 * typically bursts data, for which we don't want to compensate by speeding
4876 * up the media. The other timestamps will be interpollated from this one
4877 * using the RTP timestamps. */
4878 GST_OBJECT_LOCK (src);
4879 if (GST_ELEMENT_CLOCK (src)) {
4881 GstClockTime base_time;
4883 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4884 base_time = GST_ELEMENT_CAST (src)->base_time;
4886 src->base_time = now - base_time;
4888 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4889 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4891 GST_OBJECT_UNLOCK (src);
4894 /* If needed send a new segment, don't forget we are live and buffer are
4895 * timestamped with running time */
4896 if (src->need_segment) {
4898 src->need_segment = FALSE;
4899 gst_segment_init (&segment, GST_FORMAT_TIME);
4900 gst_rtspsrc_push_event (src, gst_event_new_segment (&segment));
4903 if (stream->discont && !is_rtcp) {
4904 /* mark first RTP buffer as discont */
4905 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4906 stream->discont = FALSE;
4907 /* first buffer gets the timestamp, other buffers are not timestamped and
4908 * their presentation time will be interpollated from the rtp timestamps. */
4909 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4910 GST_TIME_ARGS (src->base_time));
4912 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4915 /* chain to the peer pad */
4916 if (GST_PAD_IS_SINK (outpad))
4917 ret = gst_pad_chain (outpad, buf);
4919 ret = gst_pad_push (outpad, buf);
4922 /* combine all stream flows for the data transport */
4923 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4930 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4931 gst_rtsp_message_unset (message);
4936 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4937 ("Short message received, ignoring."));
4938 gst_rtsp_message_unset (message);
4943 static GstFlowReturn
4944 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4946 GstRTSPMessage message = { 0 };
4948 GstFlowReturn ret = GST_FLOW_OK;
4949 GTimeVal tv_timeout;
4952 /* get the next timeout interval */
4953 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4955 /* see if the timeout period expired */
4956 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4957 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4958 /* send keep-alive, only act on interrupt, a warning will be posted for
4960 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4962 /* get new timeout */
4963 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4966 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4967 tv_timeout.tv_sec, tv_timeout.tv_usec);
4969 /* protect the connection with the connection lock so that we can see when
4970 * we are finished doing server communication */
4972 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4973 &message, src->ptcp_timeout);
4977 GST_DEBUG_OBJECT (src, "we received a server message");
4979 case GST_RTSP_EINTR:
4980 /* we got interrupted this means we need to stop */
4982 case GST_RTSP_ETIMEOUT:
4983 /* no reply, send keep alive */
4984 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4985 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4989 /* go EOS when the server closed the connection */
4995 switch (message.type) {
4996 case GST_RTSP_MESSAGE_REQUEST:
4997 /* server sends us a request message, handle it */
4999 gst_rtspsrc_handle_request (src, src->conninfo.connection,
5001 if (res == GST_RTSP_EEOF)
5004 goto handle_request_failed;
5006 case GST_RTSP_MESSAGE_RESPONSE:
5007 /* we ignore response messages */
5008 GST_DEBUG_OBJECT (src, "ignoring response message");
5010 gst_rtsp_message_dump (&message);
5012 case GST_RTSP_MESSAGE_DATA:
5013 GST_DEBUG_OBJECT (src, "got data message");
5014 ret = gst_rtspsrc_handle_data (src, &message);
5015 if (ret != GST_FLOW_OK)
5016 goto handle_data_failed;
5019 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5024 g_assert_not_reached ();
5029 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5030 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5031 ("The server closed the connection."));
5032 src->conninfo.connected = FALSE;
5033 gst_rtsp_message_unset (&message);
5034 return GST_FLOW_EOS;
5038 gst_rtsp_message_unset (&message);
5039 GST_DEBUG_OBJECT (src, "got interrupted");
5040 return GST_FLOW_FLUSHING;
5044 gchar *str = gst_rtsp_strresult (res);
5046 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5047 ("Could not receive message. (%s)", str));
5050 gst_rtsp_message_unset (&message);
5051 return GST_FLOW_ERROR;
5053 handle_request_failed:
5055 gchar *str = gst_rtsp_strresult (res);
5057 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5058 ("Could not handle server message. (%s)", str));
5060 gst_rtsp_message_unset (&message);
5061 return GST_FLOW_ERROR;
5065 GST_DEBUG_OBJECT (src, "could no handle data message");
5070 static GstFlowReturn
5071 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
5074 GstRTSPMessage message = { 0 };
5078 GTimeVal tv_timeout;
5080 /* get the next timeout interval */
5081 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
5083 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
5084 (gint) tv_timeout.tv_sec);
5086 gst_rtsp_message_unset (&message);
5088 /* we should continue reading the TCP socket because the server might
5089 * send us requests. When the session timeout expires, we need to send a
5090 * keep-alive request to keep the session open. */
5091 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
5092 &message, &tv_timeout);
5096 GST_DEBUG_OBJECT (src, "we received a server message");
5098 case GST_RTSP_EINTR:
5099 /* we got interrupted, see what we have to do */
5101 case GST_RTSP_ETIMEOUT:
5102 /* send keep-alive, ignore the result, a warning will be posted. */
5103 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
5104 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5108 /* server closed the connection. not very fatal for UDP, reconnect and
5109 * see what happens. */
5110 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5111 ("The server closed the connection."));
5112 if (src->udp_reconnect) {
5114 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
5121 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
5123 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5124 ("Unhandled return value %d.", res));
5128 switch (message.type) {
5129 case GST_RTSP_MESSAGE_REQUEST:
5130 /* server sends us a request message, handle it */
5132 gst_rtspsrc_handle_request (src, src->conninfo.connection,
5134 if (res == GST_RTSP_EEOF)
5137 goto handle_request_failed;
5139 case GST_RTSP_MESSAGE_RESPONSE:
5140 /* we ignore response and data messages */
5141 GST_DEBUG_OBJECT (src, "ignoring response message");
5143 gst_rtsp_message_dump (&message);
5144 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
5145 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
5146 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
5147 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
5148 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5155 case GST_RTSP_MESSAGE_DATA:
5156 /* we ignore response and data messages */
5157 GST_DEBUG_OBJECT (src, "ignoring data message");
5160 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5165 g_assert_not_reached ();
5167 /* we get here when the connection got interrupted */
5170 gst_rtsp_message_unset (&message);
5171 GST_DEBUG_OBJECT (src, "got interrupted");
5172 return GST_FLOW_FLUSHING;
5176 gchar *str = gst_rtsp_strresult (res);
5179 src->conninfo.connected = FALSE;
5180 if (res != GST_RTSP_EINTR) {
5181 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
5182 ("Could not connect to server. (%s)", str));
5184 ret = GST_FLOW_ERROR;
5186 ret = GST_FLOW_FLUSHING;
5192 gchar *str = gst_rtsp_strresult (res);
5194 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5195 ("Could not receive message. (%s)", str));
5197 return GST_FLOW_ERROR;
5199 handle_request_failed:
5201 gchar *str = gst_rtsp_strresult (res);
5204 gst_rtsp_message_unset (&message);
5205 if (res != GST_RTSP_EINTR) {
5206 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5207 ("Could not handle server message. (%s)", str));
5209 ret = GST_FLOW_ERROR;
5211 ret = GST_FLOW_FLUSHING;
5217 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5218 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5219 ("The server closed the connection."));
5220 src->conninfo.connected = FALSE;
5221 gst_rtsp_message_unset (&message);
5222 return GST_FLOW_EOS;
5226 static GstRTSPResult
5227 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
5229 GstRTSPResult res = GST_RTSP_OK;
5232 GST_DEBUG_OBJECT (src, "doing reconnect");
5234 GST_OBJECT_LOCK (src);
5235 /* only restart when the pads were not yet activated, else we were
5236 * streaming over UDP */
5237 restart = src->need_activate;
5238 GST_OBJECT_UNLOCK (src);
5240 /* no need to restart, we're done */
5244 /* we can try only TCP now */
5245 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
5247 /* close and cleanup our state */
5248 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
5251 /* see if we have TCP left to try. Also don't try TCP when we were configured
5253 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
5256 /* We post a warning message now to inform the user
5257 * that nothing happened. It's most likely a firewall thing. */
5258 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5259 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5260 "firewall is blocking it. Retrying using a TCP connection.",
5261 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
5263 /* open new connection using tcp */
5264 if (gst_rtspsrc_open (src, async) < 0)
5267 /* start playback */
5268 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
5277 src->cur_protocols = 0;
5278 /* no transport possible, post an error and stop */
5279 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5280 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5281 "firewall is blocking it. No other protocols to try.",
5282 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
5283 return GST_RTSP_ERROR;
5287 GST_DEBUG_OBJECT (src, "open failed");
5292 GST_DEBUG_OBJECT (src, "play failed");
5298 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
5302 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
5305 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
5308 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
5311 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
5319 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
5323 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
5326 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
5329 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
5332 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
5340 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
5344 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
5347 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
5350 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
5353 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
5361 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
5365 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
5368 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
5371 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
5374 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
5382 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
5384 if (ret == GST_RTSP_OK)
5385 gst_rtspsrc_loop_complete_cmd (src, cmd);
5386 else if (ret == GST_RTSP_EINTR)
5387 gst_rtspsrc_loop_cancel_cmd (src, cmd);
5389 gst_rtspsrc_loop_error_cmd (src, cmd);
5393 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
5396 gboolean flushed = FALSE;
5398 /* start new request */
5399 gst_rtspsrc_loop_start_cmd (src, cmd);
5401 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
5403 GST_OBJECT_LOCK (src);
5404 old = src->pending_cmd;
5405 if (old == CMD_RECONNECT) {
5406 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
5407 cmd = CMD_RECONNECT;
5409 if (old != CMD_WAIT) {
5410 src->pending_cmd = CMD_WAIT;
5411 GST_OBJECT_UNLOCK (src);
5412 /* cancel previous request */
5413 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
5414 gst_rtspsrc_loop_cancel_cmd (src, old);
5415 GST_OBJECT_LOCK (src);
5417 src->pending_cmd = cmd;
5418 /* interrupt if allowed */
5419 if (src->busy_cmd & mask) {
5420 GST_DEBUG_OBJECT (src, "connection flush busy %s",
5421 cmd_to_string (src->busy_cmd));
5422 gst_rtspsrc_connection_flush (src, TRUE);
5425 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
5426 cmd_to_string (src->busy_cmd));
5429 gst_task_start (src->task);
5430 GST_OBJECT_UNLOCK (src);
5436 gst_rtspsrc_loop (GstRTSPSrc * src)
5440 if (!src->conninfo.connection || !src->conninfo.connected)
5443 if (src->interleaved)
5444 ret = gst_rtspsrc_loop_interleaved (src);
5446 ret = gst_rtspsrc_loop_udp (src);
5448 if (ret != GST_FLOW_OK)
5456 GST_WARNING_OBJECT (src, "we are not connected");
5457 ret = GST_FLOW_FLUSHING;
5462 const gchar *reason = gst_flow_get_name (ret);
5464 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
5465 src->running = FALSE;
5466 if (ret == GST_FLOW_EOS) {
5467 /* perform EOS logic */
5468 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5469 gst_element_post_message (GST_ELEMENT_CAST (src),
5470 gst_message_new_segment_done (GST_OBJECT_CAST (src),
5471 src->segment.format, src->segment.position));
5472 gst_rtspsrc_push_event (src,
5473 gst_event_new_segment_done (src->segment.format,
5474 src->segment.position));
5476 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5478 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
5479 /* for fatal errors we post an error message, post the error before the
5480 * EOS so the app knows about the error first. */
5481 GST_ELEMENT_ERROR (src, STREAM, FAILED,
5482 ("Internal data flow error."),
5483 ("streaming task paused, reason %s (%d)", reason, ret));
5484 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5486 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
5491 #ifndef GST_DISABLE_GST_DEBUG
5492 static const gchar *
5493 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
5497 while (method != 0) {
5514 static const gchar *
5515 gst_rtspsrc_skip_lws (const gchar * s)
5517 while (g_ascii_isspace (*s))
5522 static const gchar *
5523 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
5525 while (s > start && g_ascii_isspace (*(s - 1)))
5530 static const gchar *
5531 gst_rtspsrc_skip_commas (const gchar * s)
5533 /* The grammar allows for multiple commas */
5534 while (g_ascii_isspace (*s) || *s == ',')
5539 static const gchar *
5540 gst_rtspsrc_skip_item (const gchar * s)
5542 gboolean quoted = FALSE;
5543 const gchar *start = s;
5545 /* A list item ends at the last non-whitespace character
5546 * before a comma which is not inside a quoted-string. Or at
5547 * the end of the string.
5553 if (*s == '\\' && *(s + 1))
5562 return gst_rtspsrc_unskip_lws (s, start);
5566 gst_rtsp_decode_quoted_string (gchar * quoted_string)
5570 src = quoted_string + 1;
5571 dst = quoted_string;
5572 while (*src && *src != '"') {
5573 if (*src == '\\' && *(src + 1))
5580 /* Extract the authentication tokens that the server provided for each method
5581 * into an array of structures and give those to the connection object.
5584 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
5585 const gchar * header, gboolean * stale)
5587 GSList *list = NULL, *iter;
5589 gchar *item, *eq, *name_end, *value;
5591 g_return_if_fail (stale != NULL);
5593 gst_rtsp_connection_clear_auth_params (conn);
5596 /* Parse a header whose content is described by RFC2616 as
5597 * "#something", where "something" does not itself contain commas,
5598 * except as part of quoted-strings, into a list of allocated strings.
5600 header = gst_rtspsrc_skip_commas (header);
5602 end = gst_rtspsrc_skip_item (header);
5603 list = g_slist_prepend (list, g_strndup (header, end - header));
5604 header = gst_rtspsrc_skip_commas (end);
5609 list = g_slist_reverse (list);
5610 for (iter = list; iter; iter = iter->next) {
5613 eq = strchr (item, '=');
5615 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
5616 if (name_end == item) {
5617 /* That's no good... */
5624 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
5626 gst_rtsp_decode_quoted_string (value);
5630 if (value && strcmp (item, "stale") == 0 && strcmp (value, "TRUE") == 0)
5632 gst_rtsp_connection_set_auth_param (conn, item, value);
5636 g_slist_free (list);
5639 /* Parse a WWW-Authenticate Response header and determine the
5640 * available authentication methods
5642 * This code should also cope with the fact that each WWW-Authenticate
5643 * header can contain multiple challenge methods + tokens
5645 * At the moment, for Basic auth, we just do a minimal check and don't
5646 * even parse out the realm */
5648 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
5649 GstRTSPConnection * conn, gboolean * stale)
5653 g_return_if_fail (hdr != NULL);
5654 g_return_if_fail (methods != NULL);
5655 g_return_if_fail (stale != NULL);
5657 /* Skip whitespace at the start of the string */
5658 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
5660 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
5661 *methods |= GST_RTSP_AUTH_BASIC;
5662 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
5663 *methods |= GST_RTSP_AUTH_DIGEST;
5664 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
5669 * gst_rtspsrc_setup_auth:
5670 * @src: the rtsp source
5672 * Configure a username and password and auth method on the
5673 * connection object based on a response we received from the
5676 * Currently, this requires that a username and password were supplied
5677 * in the uri. In the future, they may be requested on demand by sending
5678 * a message up the bus.
5680 * Returns: TRUE if authentication information could be set up correctly.
5683 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5687 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5688 GstRTSPAuthMethod method;
5689 GstRTSPResult auth_result;
5691 GstRTSPConnection *conn;
5693 gboolean stale = FALSE;
5695 conn = src->conninfo.connection;
5697 /* Identify the available auth methods and see if any are supported */
5698 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
5699 &hdr, 0) == GST_RTSP_OK) {
5700 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
5703 if (avail_methods == GST_RTSP_AUTH_NONE)
5704 goto no_auth_available;
5706 /* For digest auth, if the response indicates that the session
5707 * data are stale, we just update them in the connection object and
5708 * return TRUE to retry the request */
5710 src->tried_url_auth = FALSE;
5712 url = gst_rtsp_connection_get_url (conn);
5714 /* Do we have username and password available? */
5715 if (url != NULL && !src->tried_url_auth && url->user != NULL
5716 && url->passwd != NULL) {
5719 src->tried_url_auth = TRUE;
5720 GST_DEBUG_OBJECT (src,
5721 "Attempting authentication using credentials from the URL");
5723 user = src->user_id;
5724 pass = src->user_pw;
5725 GST_DEBUG_OBJECT (src,
5726 "Attempting authentication using credentials from the properties");
5729 /* FIXME: If the url didn't contain username and password or we tried them
5730 * already, request a username and passwd from the application via some kind
5731 * of credentials request message */
5733 /* If we don't have a username and passwd at this point, bail out. */
5734 if (user == NULL || pass == NULL)
5737 /* Try to configure for each available authentication method, strongest to
5739 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5740 /* Check if this method is available on the server */
5741 if ((method & avail_methods) == 0)
5744 /* Pass the credentials to the connection to try on the next request */
5745 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5746 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5747 * ignore it and end up retrying later */
5748 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5749 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5750 gst_rtsp_auth_method_to_string (method));
5755 if (method == GST_RTSP_AUTH_NONE)
5756 goto no_auth_available;
5762 /* Output an error indicating that we couldn't connect because there were
5763 * no supported authentication protocols */
5764 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5765 ("No supported authentication protocol was found"));
5770 /* We don't fire an error message, we just return FALSE and let the
5771 * normal NOT_AUTHORIZED error be propagated */
5776 static GstRTSPResult
5777 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5778 GstRTSPMessage * request, GstRTSPMessage * response,
5779 GstRTSPStatusCode * code)
5782 GstRTSPStatusCode thecode;
5783 gchar *content_base = NULL;
5787 if (!src->short_header)
5788 gst_rtsp_ext_list_before_send (src->extensions, request);
5790 GST_DEBUG_OBJECT (src, "sending message");
5793 gst_rtsp_message_dump (request);
5795 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
5799 gst_rtsp_connection_reset_timeout (conn);
5802 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
5807 gst_rtsp_message_dump (response);
5809 switch (response->type) {
5810 case GST_RTSP_MESSAGE_REQUEST:
5811 res = gst_rtspsrc_handle_request (src, conn, response);
5812 if (res == GST_RTSP_EEOF)
5815 goto handle_request_failed;
5817 case GST_RTSP_MESSAGE_RESPONSE:
5818 /* ok, a response is good */
5819 GST_DEBUG_OBJECT (src, "received response message");
5821 case GST_RTSP_MESSAGE_DATA:
5822 /* get next response */
5823 GST_DEBUG_OBJECT (src, "handle data response message");
5824 gst_rtspsrc_handle_data (src, response);
5827 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5832 thecode = response->type_data.response.code;
5834 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5836 /* if the caller wanted the result code, we store it. */
5840 /* If the request didn't succeed, bail out before doing any more */
5841 if (thecode != GST_RTSP_STS_OK)
5844 /* store new content base if any */
5845 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5848 g_free (src->content_base);
5849 src->content_base = g_strdup (content_base);
5851 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5858 gchar *str = gst_rtsp_strresult (res);
5860 if (res != GST_RTSP_EINTR) {
5861 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5862 ("Could not send message. (%s)", str));
5864 GST_WARNING_OBJECT (src, "send interrupted");
5873 GST_WARNING_OBJECT (src, "server closed connection");
5874 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5876 /* if reconnect succeeds, try again */
5878 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5882 /* only try once after reconnect, then fallthrough and error out */
5885 gchar *str = gst_rtsp_strresult (res);
5887 if (res != GST_RTSP_EINTR) {
5888 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5889 ("Could not receive message. (%s)", str));
5891 GST_WARNING_OBJECT (src, "receive interrupted");
5899 handle_request_failed:
5901 /* ERROR was posted */
5902 gst_rtsp_message_unset (response);
5907 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5908 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5909 ("The server closed the connection."));
5910 gst_rtsp_message_unset (response);
5917 * @src: the rtsp source
5918 * @conn: the connection to send on
5919 * @request: must point to a valid request
5920 * @response: must point to an empty #GstRTSPMessage
5921 * @code: an optional code result
5923 * send @request and retrieve the response in @response. optionally @code can be
5924 * non-NULL in which case it will contain the status code of the response.
5926 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5927 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5929 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5930 * @response message) if the response code was not 200 (OK).
5932 * If the attempt results in an authentication failure, then this will attempt
5933 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5936 * Returns: #GST_RTSP_OK if the processing was successful.
5938 static GstRTSPResult
5939 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5940 GstRTSPMessage * request, GstRTSPMessage * response,
5941 GstRTSPStatusCode * code)
5943 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5944 GstRTSPResult res = GST_RTSP_ERROR;
5947 GstRTSPMethod method = GST_RTSP_INVALID;
5953 /* make sure we don't loop forever */
5957 /* save method so we can disable it when the server complains */
5958 method = request->type_data.request.method;
5961 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5965 case GST_RTSP_STS_UNAUTHORIZED:
5966 case GST_RTSP_STS_NOT_FOUND:
5967 if (gst_rtspsrc_setup_auth (src, response)) {
5968 /* Try the request/response again after configuring the auth info
5976 } while (retry == TRUE);
5978 /* If the user requested the code, let them handle errors, otherwise
5979 * post an error below */
5982 else if (int_code != GST_RTSP_STS_OK)
5983 goto error_response;
5990 GST_DEBUG_OBJECT (src, "got error %d", res);
5995 res = GST_RTSP_ERROR;
5997 switch (response->type_data.response.code) {
5998 case GST_RTSP_STS_NOT_FOUND:
5999 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
6000 response->type_data.response.reason));
6002 case GST_RTSP_STS_UNAUTHORIZED:
6003 GST_ELEMENT_ERROR (src, RESOURCE, NOT_AUTHORIZED, (NULL), ("%s",
6004 response->type_data.response.reason));
6006 case GST_RTSP_STS_MOVED_PERMANENTLY:
6007 case GST_RTSP_STS_MOVE_TEMPORARILY:
6009 gchar *new_location;
6010 GstRTSPLowerTrans transports;
6012 GST_DEBUG_OBJECT (src, "got redirection");
6013 /* if we don't have a Location Header, we must error */
6014 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
6015 &new_location, 0) < 0)
6018 /* When we receive a redirect result, we go back to the INIT state after
6019 * parsing the new URI. The caller should do the needed steps to issue
6020 * a new setup when it detects this state change. */
6021 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
6023 /* save current transports */
6024 if (src->conninfo.url)
6025 transports = src->conninfo.url->transports;
6027 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
6029 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
6031 /* set old transports */
6032 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
6033 src->conninfo.url->transports = transports;
6035 src->need_redirect = TRUE;
6036 src->state = GST_RTSP_STATE_INIT;
6040 case GST_RTSP_STS_NOT_ACCEPTABLE:
6041 case GST_RTSP_STS_NOT_IMPLEMENTED:
6042 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
6043 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
6044 gst_rtsp_method_as_text (method));
6045 src->methods &= ~method;
6049 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6050 ("Got error response: %d (%s).", response->type_data.response.code,
6051 response->type_data.response.reason));
6054 /* if we return ERROR we should unset the response ourselves */
6055 if (res == GST_RTSP_ERROR)
6056 gst_rtsp_message_unset (response);
6062 static GstRTSPResult
6063 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
6064 GstRTSPMessage * response, GstRTSPSrc * src)
6066 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
6071 /* parse the response and collect all the supported methods. We need this
6072 * information so that we don't try to send an unsupported request to the
6076 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
6078 GstRTSPHeaderField field;
6082 /* reset supported methods */
6085 /* Try Allow Header first */
6086 field = GST_RTSP_HDR_ALLOW;
6089 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6090 if (indx == 0 && !respoptions) {
6091 /* if no Allow header was found then try the Public header... */
6092 field = GST_RTSP_HDR_PUBLIC;
6093 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6098 src->methods |= gst_rtsp_options_from_text (respoptions);
6103 if (src->methods == 0) {
6104 /* neither Allow nor Public are required, assume the server supports
6105 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
6107 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
6108 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
6110 /* always assume PLAY, FIXME, extensions should be able to override
6112 src->methods |= GST_RTSP_PLAY;
6113 /* also assume it will support Range */
6114 src->seekable = TRUE;
6116 /* we need describe and setup */
6117 if (!(src->methods & GST_RTSP_DESCRIBE))
6119 if (!(src->methods & GST_RTSP_SETUP))
6127 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6128 ("Server does not support DESCRIBE."));
6133 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6134 ("Server does not support SETUP."));
6139 /* masks to be kept in sync with the hardcoded protocol order of preference
6141 static const guint protocol_masks[] = {
6142 GST_RTSP_LOWER_TRANS_UDP,
6143 GST_RTSP_LOWER_TRANS_UDP_MCAST,
6144 GST_RTSP_LOWER_TRANS_TCP,
6148 static GstRTSPResult
6149 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
6150 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
6154 gboolean add_udp_str;
6159 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
6164 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
6166 /* extension listed transports, use those */
6167 if (*transports != NULL)
6170 /* it's the default */
6171 add_udp_str = FALSE;
6173 /* the default RTSP transports */
6174 result = g_string_new ("RTP");
6177 case GST_RTSP_PROFILE_AVP:
6178 g_string_append (result, "/AVP");
6180 case GST_RTSP_PROFILE_SAVP:
6181 g_string_append (result, "/SAVP");
6183 case GST_RTSP_PROFILE_AVPF:
6184 g_string_append (result, "/AVPF");
6186 case GST_RTSP_PROFILE_SAVPF:
6187 g_string_append (result, "/SAVPF");
6193 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
6194 GST_DEBUG_OBJECT (src, "adding UDP unicast");
6196 g_string_append (result, "/UDP");
6197 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
6198 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
6199 GST_DEBUG_OBJECT (src, "adding UDP multicast");
6200 /* we don't have to allocate any UDP ports yet, if the selected transport
6201 * turns out to be multicast we can create them and join the multicast
6202 * group indicated in the transport reply */
6204 g_string_append (result, "/UDP");
6205 g_string_append (result, ";multicast");
6206 if (src->next_port_num != 0) {
6207 if (src->client_port_range.max > 0 &&
6208 src->next_port_num >= src->client_port_range.max)
6211 g_string_append_printf (result, ";client_port=%d-%d",
6212 src->next_port_num, src->next_port_num + 1);
6214 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
6215 GST_DEBUG_OBJECT (src, "adding TCP");
6217 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
6219 *transports = g_string_free (result, FALSE);
6221 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
6228 GST_ERROR ("extension gave error %d", res);
6233 GST_ERROR ("no more ports available");
6234 return GST_RTSP_ERROR;
6238 static GstRTSPResult
6239 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
6240 gint orig_rtpport, gint orig_rtcpport)
6243 gint nr_udp, nr_int;
6245 gint rtpport = 0, rtcpport = 0;
6248 src = stream->parent;
6250 /* find number of placeholders first */
6251 if (strstr (*transports, "%%i2"))
6253 else if (strstr (*transports, "%%i1"))
6258 if (strstr (*transports, "%%u2"))
6260 else if (strstr (*transports, "%%u1"))
6265 if (nr_udp == 0 && nr_int == 0)
6269 if (!orig_rtpport || !orig_rtcpport) {
6270 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
6273 rtpport = orig_rtpport;
6274 rtcpport = orig_rtcpport;
6278 str = g_string_new ("");
6280 while ((next = strstr (p, "%%"))) {
6281 g_string_append_len (str, p, next - p);
6282 if (next[2] == 'u') {
6284 g_string_append_printf (str, "%d", rtpport);
6285 else if (next[3] == '2')
6286 g_string_append_printf (str, "%d", rtcpport);
6288 if (next[2] == 'i') {
6290 g_string_append_printf (str, "%d", src->free_channel);
6291 else if (next[3] == '2')
6292 g_string_append_printf (str, "%d", src->free_channel + 1);
6297 /* append final part */
6298 g_string_append (str, p);
6300 g_free (*transports);
6301 *transports = g_string_free (str, FALSE);
6309 GST_ERROR ("failed to allocate udp ports");
6310 return GST_RTSP_ERROR;
6315 enc_key_length_from_cipher_name (const gchar * cipher)
6317 if (g_strcmp0 (cipher, "aes-128-icm") == 0)
6318 return AES_128_KEY_LEN;
6319 else if (g_strcmp0 (cipher, "aes-256-icm") == 0)
6320 return AES_256_KEY_LEN;
6322 GST_ERROR ("encryption algorithm '%s' not supported", cipher);
6328 auth_key_length_from_auth_name (const gchar * auth)
6330 if (g_strcmp0 (auth, "hmac-sha1-32") == 0)
6331 return HMAC_32_KEY_LEN;
6332 else if (g_strcmp0 (auth, "hmac-sha1-80") == 0)
6333 return HMAC_80_KEY_LEN;
6335 GST_ERROR ("authentication algorithm '%s' not supported", auth);
6341 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
6343 GstCaps *caps = NULL;
6345 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
6349 GST_DEBUG_OBJECT (src, "SRTP parameters received");
6355 default_srtcp_params (void)
6362 guint data_size = GST_ROUND_UP_4 (KEY_SIZE);
6364 /* create a random key */
6365 key_data = g_malloc (data_size);
6366 for (i = 0; i < data_size; i += 4)
6367 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
6369 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
6371 caps = gst_caps_new_simple ("application/x-srtp",
6372 "srtp-key", GST_TYPE_BUFFER, buf,
6373 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
6374 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
6376 gst_buffer_unref (buf);
6382 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
6385 gchar *result, *base64;
6388 GstMIKEYMessage *msg;
6389 GstMIKEYPayload *payload, *pkd;
6395 const gchar *srtcpcipher, *srtcpauth;
6397 stream->srtcpparams = signal_get_srtcp_params (src, stream);
6398 if (stream->srtcpparams == NULL)
6399 stream->srtcpparams = default_srtcp_params ();
6401 s = gst_caps_get_structure (stream->srtcpparams, 0);
6403 srtcpcipher = gst_structure_get_string (s, "srtcp-cipher");
6404 srtcpauth = gst_structure_get_string (s, "srtcp-auth");
6405 val = gst_structure_get_value (s, "srtp-key");
6407 if (srtcpcipher == NULL || srtcpauth == NULL || val == NULL) {
6408 GST_ERROR_OBJECT (src, "could not find the right SRTP parameters in caps");
6412 srtpkey = gst_value_get_buffer (val);
6414 msg = gst_mikey_message_new ();
6415 /* unencrypted MIKEY message, we send this over TLS so this is allowed */
6416 gst_mikey_message_set_info (msg, GST_MIKEY_VERSION, GST_MIKEY_TYPE_PSK_INIT,
6417 FALSE, GST_MIKEY_PRF_MIKEY_1, g_random_int (), GST_MIKEY_MAP_TYPE_SRTP);
6418 /* add policy '0' for our SSRC */
6419 gst_mikey_message_add_cs_srtp (msg, 0, stream->send_ssrc, 0);
6420 /* timestamp is now */
6421 gst_mikey_message_add_t_now_ntp_utc (msg);
6422 /* add some random data */
6423 gst_mikey_message_add_rand_len (msg, 16);
6425 /* the policy '0' is SRTP */
6426 payload = gst_mikey_payload_new (GST_MIKEY_PT_SP);
6427 gst_mikey_payload_sp_set (payload, 0, GST_MIKEY_SEC_PROTO_SRTP);
6429 /* only AES-CM is supported */
6431 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_ALG, 1, &byte);
6432 /* encryption key length */
6433 byte = enc_key_length_from_cipher_name (srtcpcipher);
6434 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_KEY_LEN, 1,
6436 /* only HMAC-SHA1 */
6437 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_ALG, 1,
6439 /* authentication key length */
6440 byte = auth_key_length_from_auth_name (srtcpauth);
6441 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_KEY_LEN, 1,
6443 /* we enable encryption on RTP and RTCP */
6444 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_ENC, 1,
6446 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTCP_ENC, 1,
6448 /* we enable authentication on RTP and RTCP */
6449 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_AUTH, 1,
6451 gst_mikey_message_add_payload (msg, payload);
6453 /* make unencrypted KEMAC */
6454 payload = gst_mikey_payload_new (GST_MIKEY_PT_KEMAC);
6455 gst_mikey_payload_kemac_set (payload, GST_MIKEY_ENC_NULL, GST_MIKEY_MAC_NULL);
6456 /* add the key in KEMAC */
6457 pkd = gst_mikey_payload_new (GST_MIKEY_PT_KEY_DATA);
6458 gst_buffer_map (srtpkey, &info, GST_MAP_READ);
6459 gst_mikey_payload_key_data_set_key (pkd, GST_MIKEY_KD_TEK, info.size,
6461 gst_buffer_unmap (srtpkey, &info);
6462 gst_mikey_payload_kemac_add_sub (payload, pkd);
6463 gst_mikey_message_add_payload (msg, payload);
6465 /* now serialize this to bytes */
6466 bytes = gst_mikey_message_to_bytes (msg, NULL, NULL);
6467 gst_mikey_message_unref (msg);
6468 /* and make it into base64 */
6469 data = g_bytes_get_data (bytes, &size);
6470 base64 = g_base64_encode (data, size);
6471 g_bytes_unref (bytes);
6473 result = g_strdup_printf ("prot=mikey;uri=\"%s\";data=\"%s\"",
6474 stream->conninfo.location, base64);
6481 /* Perform the SETUP request for all the streams.
6483 * We ask the server for a specific transport, which initially includes all the
6484 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
6485 * two local UDP ports that we send to the server.
6487 * Once the server replied with a transport, we configure the other streams
6488 * with the same transport.
6490 * This function will also configure the stream for the selected transport,
6491 * which basically means creating the pipeline.
6493 static GstRTSPResult
6494 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
6497 GstRTSPResult res = GST_RTSP_ERROR;
6498 GstRTSPMessage request = { 0 };
6499 GstRTSPMessage response = { 0 };
6500 GstRTSPStream *stream = NULL;
6501 GstRTSPLowerTrans protocols;
6502 GstRTSPStatusCode code;
6503 gboolean unsupported_real = FALSE;
6504 gint rtpport, rtcpport;
6508 if (src->conninfo.connection) {
6509 url = gst_rtsp_connection_get_url (src->conninfo.connection);
6510 /* we initially allow all configured lower transports. based on the URL
6511 * transports and the replies from the server we narrow them down. */
6512 protocols = url->transports & src->cur_protocols;
6515 protocols = src->cur_protocols;
6521 /* reset some state */
6522 src->free_channel = 0;
6523 src->interleaved = FALSE;
6524 src->need_activate = FALSE;
6525 /* keep track of next port number, 0 is random */
6526 src->next_port_num = src->client_port_range.min;
6527 rtpport = rtcpport = 0;
6529 if (G_UNLIKELY (src->streams == NULL))
6532 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6533 GstRTSPConnection *conn;
6540 stream = (GstRTSPStream *) walk->data;
6542 caps = stream_get_caps_for_pt (stream, stream->default_pt);
6544 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
6548 if (stream->skipped) {
6549 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
6553 /* see if we need to configure this stream */
6554 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
6555 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
6560 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
6561 stream->id, caps, &selected);
6563 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
6567 /* merge/overwrite global caps */
6572 s = gst_caps_get_structure (caps, 0);
6574 num = gst_structure_n_fields (src->props);
6575 for (j = 0; j < num; j++) {
6579 name = gst_structure_nth_field_name (src->props, j);
6580 val = gst_structure_get_value (src->props, name);
6581 gst_structure_set_value (s, name, val);
6583 GST_DEBUG_OBJECT (src, "copied %s", name);
6587 /* skip setup if we have no URL for it */
6588 if (stream->conninfo.location == NULL) {
6589 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
6593 if (src->conninfo.connection == NULL) {
6594 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
6595 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
6598 conn = stream->conninfo.connection;
6600 conn = src->conninfo.connection;
6602 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
6603 stream->conninfo.location);
6605 /* if we have a multicast connection, only suggest multicast from now on */
6606 if (stream->is_multicast)
6607 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
6610 /* first selectable protocol */
6611 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6613 if (!protocol_masks[mask])
6617 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
6618 protocol_masks[mask]);
6619 /* create a string with first transport in line */
6621 res = gst_rtspsrc_create_transports_string (src,
6622 protocols & protocol_masks[mask], stream->profile, &transports);
6623 if (res < 0 || transports == NULL)
6624 goto setup_transport_failed;
6626 if (strlen (transports) == 0) {
6627 g_free (transports);
6628 GST_DEBUG_OBJECT (src, "no transports found");
6633 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
6635 /* replace placeholders with real values, this function will optionally
6636 * allocate UDP ports and other info needed to execute the setup request */
6637 res = gst_rtspsrc_prepare_transports (stream, &transports,
6638 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
6640 g_free (transports);
6641 goto setup_transport_failed;
6644 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
6646 /* create SETUP request */
6648 gst_rtspsrc_init_request (src, &request, GST_RTSP_SETUP,
6649 stream->conninfo.location);
6651 g_free (transports);
6652 goto create_request_failed;
6655 /* select transport */
6656 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
6659 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
6660 stream->profile == GST_RTSP_PROFILE_SAVPF) {
6661 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
6662 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
6665 /* if the user wants a non default RTP packet size we add the blocksize
6667 if (src->rtp_blocksize > 0) {
6668 hval = g_strdup_printf ("%d", src->rtp_blocksize);
6669 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
6673 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
6676 /* handle the code ourselves */
6677 res = gst_rtspsrc_send (src, conn, &request, &response, &code);
6682 case GST_RTSP_STS_OK:
6684 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
6685 gst_rtsp_message_unset (&request);
6686 gst_rtsp_message_unset (&response);
6687 /* cleanup of leftover transport */
6688 gst_rtspsrc_stream_free_udp (stream);
6689 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
6690 * we might be in this case */
6691 if (stream->container && rtpport && rtcpport && !retry) {
6692 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
6697 /* this transport did not go down well, but we may have others to try
6698 * that we did not send yet, try those and only give up then
6699 * but not without checking for lost cause/extension so we can
6700 * post a nicer/more useful error message later */
6701 if (!unsupported_real)
6702 unsupported_real = stream->is_real;
6703 /* select next available protocol, give up on this stream if none */
6705 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6707 if (!protocol_masks[mask] || unsupported_real)
6712 /* cleanup of leftover transport and move to the next stream */
6713 gst_rtspsrc_stream_free_udp (stream);
6714 goto response_error;
6717 /* parse response transport */
6719 gchar *resptrans = NULL;
6720 GstRTSPTransport transport = { 0 };
6722 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
6725 gst_rtspsrc_stream_free_udp (stream);
6729 /* parse transport, go to next stream on parse error */
6730 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6731 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6735 /* update allowed transports for other streams. once the transport of
6736 * one stream has been determined, we make sure that all other streams
6737 * are configured in the same way */
6738 switch (transport.lower_transport) {
6739 case GST_RTSP_LOWER_TRANS_TCP:
6740 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6741 protocols = GST_RTSP_LOWER_TRANS_TCP;
6742 src->interleaved = TRUE;
6743 /* update free channels */
6745 MAX (transport.interleaved.min, src->free_channel);
6747 MAX (transport.interleaved.max, src->free_channel);
6748 src->free_channel++;
6750 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6751 /* only allow multicast for other streams */
6752 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6753 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6754 /* if the server selected our ports, increment our counters so that
6755 * we select a new port later */
6756 if (src->next_port_num == transport.port.min &&
6757 src->next_port_num + 1 == transport.port.max) {
6758 src->next_port_num += 2;
6761 case GST_RTSP_LOWER_TRANS_UDP:
6762 /* only allow unicast for other streams */
6763 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6764 protocols = GST_RTSP_LOWER_TRANS_UDP;
6767 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6768 transport.lower_transport);
6772 if (!src->interleaved || !retry) {
6773 /* now configure the stream with the selected transport */
6774 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6775 GST_DEBUG_OBJECT (src,
6776 "could not configure stream %p transport, skipping stream",
6779 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
6780 /* retain the first allocated UDP port pair */
6781 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
6782 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
6785 /* we need to activate at least one streams when we detect activity */
6786 src->need_activate = TRUE;
6788 /* stream is setup now */
6789 stream->setup = TRUE;
6794 GstRTSPStream *sskip;
6796 skip = g_list_next (skip);
6800 sskip = (GstRTSPStream *) skip->data;
6802 /* skip all streams with the same control url */
6803 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
6804 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
6805 sskip, sskip->conninfo.location);
6806 sskip->skipped = TRUE;
6811 /* clean up our transport struct */
6812 gst_rtsp_transport_init (&transport);
6813 /* clean up used RTSP messages */
6814 gst_rtsp_message_unset (&request);
6815 gst_rtsp_message_unset (&response);
6819 /* store the transport protocol that was configured */
6820 src->cur_protocols = protocols;
6822 gst_rtsp_ext_list_stream_select (src->extensions, url);
6824 /* if there is nothing to activate, error out */
6825 if (!src->need_activate)
6826 goto nothing_to_activate;
6833 /* no transport possible, post an error and stop */
6834 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6835 ("Could not connect to server, no protocols left"));
6836 return GST_RTSP_ERROR;
6840 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6841 ("SDP contains no streams"));
6842 return GST_RTSP_ERROR;
6844 create_request_failed:
6846 gchar *str = gst_rtsp_strresult (res);
6848 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6849 ("Could not create request. (%s)", str));
6853 setup_transport_failed:
6855 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6856 ("Could not setup transport."));
6857 res = GST_RTSP_ERROR;
6862 const gchar *str = gst_rtsp_status_as_text (code);
6864 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6865 ("Error (%d): %s", code, GST_STR_NULL (str)));
6866 res = GST_RTSP_ERROR;
6871 gchar *str = gst_rtsp_strresult (res);
6873 if (res != GST_RTSP_EINTR) {
6874 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6875 ("Could not send message. (%s)", str));
6877 GST_WARNING_OBJECT (src, "send interrupted");
6884 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6885 ("Server did not select transport."));
6886 res = GST_RTSP_ERROR;
6889 nothing_to_activate:
6891 /* none of the available error codes is really right .. */
6892 if (unsupported_real) {
6893 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6894 (_("No supported stream was found. You might need to install a "
6895 "GStreamer RTSP extension plugin for Real media streams.")),
6898 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6899 (_("No supported stream was found. You might need to allow "
6900 "more transport protocols or may otherwise be missing "
6901 "the right GStreamer RTSP extension plugin.")), (NULL));
6903 return GST_RTSP_ERROR;
6907 gst_rtsp_message_unset (&request);
6908 gst_rtsp_message_unset (&response);
6914 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
6915 GstSegment * segment)
6918 GstRTSPTimeRange *therange;
6921 gst_rtsp_range_free (src->range);
6923 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
6924 GST_DEBUG_OBJECT (src, "parsed range %s", range);
6925 src->range = therange;
6927 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6929 gst_segment_init (segment, GST_FORMAT_TIME);
6933 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6934 therange->min.type, therange->min.seconds, therange->max.type,
6935 therange->max.seconds);
6937 if (therange->min.type == GST_RTSP_TIME_NOW)
6939 else if (therange->min.type == GST_RTSP_TIME_END)
6942 seconds = therange->min.seconds * GST_SECOND;
6944 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6945 GST_TIME_ARGS (seconds));
6947 /* we need to start playback without clipping from the position reported by
6949 segment->start = seconds;
6950 segment->position = seconds;
6952 if (therange->max.type == GST_RTSP_TIME_NOW)
6954 else if (therange->max.type == GST_RTSP_TIME_END)
6957 seconds = therange->max.seconds * GST_SECOND;
6959 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6960 GST_TIME_ARGS (seconds));
6962 /* live (WMS) server might send overflowed large max as its idea of infinity,
6963 * compensate to prevent problems later on */
6964 if (seconds != -1 && seconds < 0) {
6966 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6969 /* live (WMS) might send min == max, which is not worth recording */
6970 if (segment->duration == -1 && seconds == segment->start)
6973 /* don't change duration with unknown value, we might have a valid value
6974 * there that we want to keep. */
6976 segment->duration = seconds;
6981 /* Parse clock profived by the server with following syntax:
6983 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6986 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6988 gboolean res = FALSE;
6990 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6991 gchar **fields = NULL, **parts = NULL;
6992 gchar *remote_ip, *str;
6994 GstClockTime base_time;
6997 fields = g_strsplit (gstclock, " ", 0);
6999 /* wrapped clock, not very interesting for now */
7000 if (fields[1] == NULL)
7003 /* remote IP address and port */
7004 if ((str = fields[2]) == NULL)
7007 parts = g_strsplit (str, ":", 0);
7009 if ((remote_ip = parts[0]) == NULL)
7012 if ((str = parts[1]) == NULL)
7020 if ((str = fields[3]) == NULL)
7023 base_time = g_ascii_strtoull (str, NULL, 10);
7026 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
7029 if (src->provided_clock)
7030 gst_object_unref (src->provided_clock);
7031 src->provided_clock = netclock;
7033 gst_element_post_message (GST_ELEMENT_CAST (src),
7034 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
7035 src->provided_clock, TRUE));
7039 g_strfreev (fields);
7045 /* must be called with the RTSP state lock */
7046 static GstRTSPResult
7047 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
7053 /* prepare global stream caps properties */
7055 gst_structure_remove_all_fields (src->props);
7057 src->props = gst_structure_new_empty ("RTSPProperties");
7060 gst_sdp_message_dump (sdp);
7062 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
7064 /* let the app inspect and change the SDP */
7065 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
7067 gst_segment_init (&src->segment, GST_FORMAT_TIME);
7069 /* parse range for duration reporting. */
7074 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
7078 /* keep track of the range and configure it in the segment */
7079 if (gst_rtspsrc_parse_range (src, range, &src->segment))
7083 /* parse clock information. This is GStreamer specific, a server can tell the
7084 * client what clock it is using and wrap that in a network clock. The
7085 * advantage of that is that we can slave to it. */
7087 const gchar *gstclock;
7090 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
7091 if (gstclock == NULL)
7094 /* parse the clock and expose it in the provide_clock method */
7095 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
7099 /* try to find a global control attribute. Note that a '*' means that we should
7100 * do aggregate control with the current url (so we don't do anything and
7101 * leave the current connection as is) */
7103 const gchar *control;
7106 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
7107 if (control == NULL)
7110 /* only take fully qualified urls */
7111 if (g_str_has_prefix (control, "rtsp://"))
7115 g_free (src->conninfo.location);
7116 src->conninfo.location = g_strdup (control);
7117 /* make a connection for this, if there was a connection already, nothing
7119 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
7120 GST_ERROR_OBJECT (src, "could not connect");
7123 /* we need to keep the control url separate from the connection url because
7124 * the rules for constructing the media control url need it */
7125 g_free (src->control);
7126 src->control = g_strdup (control);
7129 /* create streams */
7130 n_streams = gst_sdp_message_medias_len (sdp);
7131 for (i = 0; i < n_streams; i++) {
7132 gst_rtspsrc_create_stream (src, sdp, i);
7135 src->state = GST_RTSP_STATE_INIT;
7138 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
7141 /* reset our state */
7142 src->need_range = TRUE;
7145 src->state = GST_RTSP_STATE_READY;
7152 GST_ERROR_OBJECT (src, "setup failed");
7153 gst_rtspsrc_cleanup (src);
7158 static GstRTSPResult
7159 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
7163 GstRTSPMessage request = { 0 };
7164 GstRTSPMessage response = { 0 };
7167 gchar *respcont = NULL;
7170 src->need_redirect = FALSE;
7172 /* can't continue without a valid url */
7173 if (G_UNLIKELY (src->conninfo.url == NULL)) {
7174 res = GST_RTSP_EINVAL;
7177 src->tried_url_auth = FALSE;
7179 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
7180 goto connect_failed;
7182 /* create OPTIONS */
7183 GST_DEBUG_OBJECT (src, "create options...");
7185 gst_rtspsrc_init_request (src, &request, GST_RTSP_OPTIONS,
7186 src->conninfo.url_str);
7188 goto create_request_failed;
7191 GST_DEBUG_OBJECT (src, "send options...");
7194 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
7197 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
7202 if (!gst_rtspsrc_parse_methods (src, &response))
7205 /* create DESCRIBE */
7206 GST_DEBUG_OBJECT (src, "create describe...");
7208 gst_rtspsrc_init_request (src, &request, GST_RTSP_DESCRIBE,
7209 src->conninfo.url_str);
7211 goto create_request_failed;
7213 /* we only accept SDP for now */
7214 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
7218 GST_DEBUG_OBJECT (src, "send describe...");
7221 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
7224 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
7228 /* we only perform redirect for the describe, currently */
7229 if (src->need_redirect) {
7230 /* close connection, we don't have to send a TEARDOWN yet, ignore the
7232 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7234 gst_rtsp_message_unset (&request);
7235 gst_rtsp_message_unset (&response);
7241 /* it could be that the DESCRIBE method was not implemented */
7242 if (!(src->methods & GST_RTSP_DESCRIBE))
7245 /* check if reply is SDP */
7246 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
7248 /* could not be set but since the request returned OK, we assume it
7249 * was SDP, else check it. */
7251 if (g_ascii_strcasecmp (respcont, "application/sdp") != 0)
7252 goto wrong_content_type;
7255 /* get message body and parse as SDP */
7256 gst_rtsp_message_get_body (&response, &data, &size);
7257 if (data == NULL || size == 0)
7260 GST_DEBUG_OBJECT (src, "parse SDP...");
7261 gst_sdp_message_new (sdp);
7262 gst_sdp_message_parse_buffer (data, size, *sdp);
7264 /* clean up any messages */
7265 gst_rtsp_message_unset (&request);
7266 gst_rtsp_message_unset (&response);
7273 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
7274 ("No valid RTSP URL was provided"));
7279 gchar *str = gst_rtsp_strresult (res);
7281 if (res != GST_RTSP_EINTR) {
7282 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
7283 ("Failed to connect. (%s)", str));
7285 GST_WARNING_OBJECT (src, "connect interrupted");
7290 create_request_failed:
7292 gchar *str = gst_rtsp_strresult (res);
7294 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7295 ("Could not create request. (%s)", str));
7301 /* Don't post a message - the rtsp_send method will have
7302 * taken care of it because we passed NULL for the response code */
7307 /* error was posted */
7308 res = GST_RTSP_ERROR;
7313 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7314 ("Server does not support SDP, got %s.", respcont));
7315 res = GST_RTSP_ERROR;
7320 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7321 ("Server can not provide an SDP."));
7322 res = GST_RTSP_ERROR;
7327 if (src->conninfo.connection) {
7328 GST_DEBUG_OBJECT (src, "free connection");
7329 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7331 gst_rtsp_message_unset (&request);
7332 gst_rtsp_message_unset (&response);
7337 static GstRTSPResult
7338 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
7343 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
7345 if (src->sdp == NULL) {
7346 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
7350 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
7355 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
7362 GST_WARNING_OBJECT (src, "can't get sdp");
7363 src->open_error = TRUE;
7368 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
7369 src->open_error = TRUE;
7374 static GstRTSPResult
7375 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
7377 GstRTSPMessage request = { 0 };
7378 GstRTSPMessage response = { 0 };
7379 GstRTSPResult res = GST_RTSP_OK;
7381 const gchar *control;
7383 GST_DEBUG_OBJECT (src, "TEARDOWN...");
7385 gst_rtspsrc_set_state (src, GST_STATE_READY);
7387 if (src->state < GST_RTSP_STATE_READY) {
7388 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
7395 /* construct a control url */
7396 control = get_aggregate_control (src);
7398 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
7401 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7402 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7403 const gchar *setup_url;
7404 GstRTSPConnInfo *info;
7406 /* try aggregate control first but do non-aggregate control otherwise */
7408 setup_url = control;
7409 else if ((setup_url = stream->conninfo.location) == NULL)
7412 if (src->conninfo.connection) {
7413 info = &src->conninfo;
7414 } else if (stream->conninfo.connection) {
7415 info = &stream->conninfo;
7419 if (!info->connected)
7424 gst_rtspsrc_init_request (src, &request, GST_RTSP_TEARDOWN, setup_url);
7426 goto create_request_failed;
7429 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
7432 gst_rtspsrc_send (src, info->connection, &request, &response,
7436 /* FIXME, parse result? */
7437 gst_rtsp_message_unset (&request);
7438 gst_rtsp_message_unset (&response);
7441 /* early exit when we did aggregate control */
7447 /* close connections */
7448 GST_DEBUG_OBJECT (src, "closing connection...");
7449 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7450 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7451 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7452 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
7456 gst_rtspsrc_cleanup (src);
7458 src->state = GST_RTSP_STATE_INVALID;
7461 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
7466 create_request_failed:
7468 gchar *str = gst_rtsp_strresult (res);
7470 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7471 ("Could not create request. (%s)", str));
7477 gchar *str = gst_rtsp_strresult (res);
7479 gst_rtsp_message_unset (&request);
7480 if (res != GST_RTSP_EINTR) {
7481 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7482 ("Could not send message. (%s)", str));
7484 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
7491 GST_DEBUG_OBJECT (src,
7492 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
7497 /* RTP-Info is of the format:
7499 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
7501 * rtptime corresponds to the timestamp for the NPT time given in the header
7502 * seqbase corresponds to the next sequence number we received. This number
7503 * indicates the first seqnum after the seek and should be used to discard
7504 * packets that are from before the seek.
7507 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
7512 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
7514 infos = g_strsplit (rtpinfo, ",", 0);
7515 for (i = 0; infos[i]; i++) {
7517 GstRTSPStream *stream;
7521 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
7523 /* init values, types of seqbase and timebase are bigger than needed so we
7524 * can store -1 as uninitialized values */
7529 /* parse url, find stream for url.
7530 * parse seq and rtptime. The seq number should be configured in the rtp
7531 * depayloader or session manager to detect gaps. Same for the rtptime, it
7532 * should be used to create an initial time newsegment. */
7533 fields = g_strsplit (infos[i], ";", 0);
7534 for (j = 0; fields[j]; j++) {
7535 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
7536 /* remove leading whitespace */
7537 fields[j] = g_strchug (fields[j]);
7538 if (g_str_has_prefix (fields[j], "url=")) {
7539 /* get the url and the stream */
7541 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
7542 } else if (g_str_has_prefix (fields[j], "seq=")) {
7543 seqbase = atoi (fields[j] + 4);
7544 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
7545 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
7548 g_strfreev (fields);
7549 /* now we need to store the values for the caps of the stream */
7550 if (stream != NULL) {
7551 GST_DEBUG_OBJECT (src,
7552 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
7553 stream, seqbase, timebase);
7555 /* we have a stream, configure detected params */
7556 stream->seqbase = seqbase;
7557 stream->timebase = timebase;
7566 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
7571 interval = strtoul (rtcp, NULL, 10);
7572 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
7577 interval *= GST_MSECOND;
7579 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7580 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7582 /* already (optionally) retrieved this when configuring manager */
7583 if (stream->session) {
7584 GObject *rtpsession = stream->session;
7586 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
7588 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
7592 /* now it happens that (Xenon) server sending this may also provide bogus
7593 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
7594 * and just use RTP-Info to sync */
7596 GObjectClass *klass;
7598 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
7599 if (g_object_class_find_property (klass, "rtcp-sync")) {
7600 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
7601 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
7607 gst_rtspsrc_get_float (const gchar * dstr)
7609 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7611 /* canonicalise floating point string so we can handle float strings
7612 * in the form "24.930" or "24,930" irrespective of the current locale */
7613 g_strlcpy (s, dstr, sizeof (s));
7614 g_strdelimit (s, ",", '.');
7615 return g_ascii_strtod (s, NULL);
7619 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
7621 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7623 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
7624 g_strlcpy (val_str, "now", sizeof (val_str));
7626 if (segment->position == 0) {
7627 g_strlcpy (val_str, "0", sizeof (val_str));
7629 g_ascii_dtostr (val_str, sizeof (val_str),
7630 ((gdouble) segment->position) / GST_SECOND);
7633 return g_strdup_printf ("npt=%s-", val_str);
7637 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
7641 stream->timebase = -1;
7642 stream->seqbase = -1;
7644 len = stream->ptmap->len;
7645 for (i = 0; i < len; i++) {
7646 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
7649 if (item->caps == NULL)
7652 item->caps = gst_caps_make_writable (item->caps);
7653 s = gst_caps_get_structure (item->caps, 0);
7654 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
7658 static GstRTSPResult
7659 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
7661 GstRTSPResult res = GST_RTSP_OK;
7663 if (src->state < GST_RTSP_STATE_READY) {
7664 res = GST_RTSP_ERROR;
7665 if (src->open_error) {
7666 GST_DEBUG_OBJECT (src, "the stream was in error");
7670 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
7672 if ((res = gst_rtspsrc_open (src, async)) < 0) {
7673 GST_DEBUG_OBJECT (src, "failed to open stream");
7682 static GstRTSPResult
7683 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
7685 GstRTSPMessage request = { 0 };
7686 GstRTSPMessage response = { 0 };
7687 GstRTSPResult res = GST_RTSP_OK;
7691 const gchar *control;
7693 GST_DEBUG_OBJECT (src, "PLAY...");
7695 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7698 if (!(src->methods & GST_RTSP_PLAY))
7701 if (src->state == GST_RTSP_STATE_PLAYING)
7704 if (!src->conninfo.connection || !src->conninfo.connected)
7707 /* send some dummy packets before we activate the receive in the
7709 gst_rtspsrc_send_dummy_packets (src);
7711 /* require new SR packets */
7713 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
7715 /* construct a control url */
7716 control = get_aggregate_control (src);
7718 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7719 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7720 const gchar *setup_url;
7721 GstRTSPConnection *conn;
7723 /* try aggregate control first but do non-aggregate control otherwise */
7725 setup_url = control;
7726 else if ((setup_url = stream->conninfo.location) == NULL)
7729 if (src->conninfo.connection) {
7730 conn = src->conninfo.connection;
7731 } else if (stream->conninfo.connection) {
7732 conn = stream->conninfo.connection;
7738 res = gst_rtspsrc_init_request (src, &request, GST_RTSP_PLAY, setup_url);
7740 goto create_request_failed;
7742 if (src->need_range) {
7743 hval = gen_range_header (src, segment);
7745 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
7747 /* store the newsegment event so it can be sent from the streaming thread. */
7748 src->need_segment = TRUE;
7751 if (segment->rate != 1.0) {
7752 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
7754 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
7756 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
7758 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
7762 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
7764 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7767 /* seek may have silently failed as it is not supported */
7768 if (!(src->methods & GST_RTSP_PLAY)) {
7769 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
7770 /* obviously it is supported as we made it here */
7771 src->methods |= GST_RTSP_PLAY;
7772 src->seekable = FALSE;
7773 /* but there is nothing to parse in the response,
7774 * so convey we have no idea and not to expect anything particular */
7775 clear_rtp_base (src, stream);
7779 /* need to do for all streams */
7780 for (run = src->streams; run; run = g_list_next (run))
7781 clear_rtp_base (src, (GstRTSPStream *) run->data);
7783 /* NOTE the above also disables npt based eos detection */
7784 /* and below forces position to 0,
7785 * which is visible feedback we lost the plot */
7786 segment->start = segment->position = src->last_pos;
7789 gst_rtsp_message_unset (&request);
7791 /* parse RTP npt field. This is the current position in the stream (Normal
7792 * Play Time) and should be put in the NEWSEGMENT position field. */
7793 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
7795 gst_rtspsrc_parse_range (src, hval, segment);
7797 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
7798 segment->rate = 1.0;
7800 /* parse Speed header. This is the intended playback rate of the stream
7801 * and should be put in the NEWSEGMENT rate field. */
7802 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
7803 0) == GST_RTSP_OK) {
7804 segment->rate = gst_rtspsrc_get_float (hval);
7805 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
7806 &hval, 0) == GST_RTSP_OK) {
7807 segment->rate = gst_rtspsrc_get_float (hval);
7810 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
7811 * for the RTP packets. If this is not present, we assume all starts from 0...
7812 * This is info for the RTP session manager that we pass to it in caps. */
7814 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
7815 &hval, hval_idx++) == GST_RTSP_OK)
7816 gst_rtspsrc_parse_rtpinfo (src, hval);
7818 /* some servers indicate RTCP parameters in PLAY response,
7819 * rather than properly in SDP */
7820 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
7821 &hval, 0) == GST_RTSP_OK)
7822 gst_rtspsrc_handle_rtcp_interval (src, hval);
7824 gst_rtsp_message_unset (&response);
7826 /* early exit when we did aggregate control */
7830 /* configure the caps of the streams after we parsed all headers. Only reset
7831 * the manager object when we set a new Range header (we did a seek) */
7832 gst_rtspsrc_configure_caps (src, segment, src->need_range);
7834 /* set to PLAYING after we have configured the caps, otherwise we
7835 * might end up calling request_key (with SRTP) while caps are still
7836 * being configured. */
7837 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
7839 /* set again when needed */
7840 src->need_range = FALSE;
7842 src->running = TRUE;
7843 src->base_time = -1;
7844 src->state = GST_RTSP_STATE_PLAYING;
7847 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
7848 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7849 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7850 stream->discont = TRUE;
7855 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
7862 GST_DEBUG_OBJECT (src, "failed to open stream");
7867 GST_DEBUG_OBJECT (src, "PLAY is not supported");
7872 GST_DEBUG_OBJECT (src, "we were already PLAYING");
7875 create_request_failed:
7877 gchar *str = gst_rtsp_strresult (res);
7879 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7880 ("Could not create request. (%s)", str));
7886 gchar *str = gst_rtsp_strresult (res);
7888 gst_rtsp_message_unset (&request);
7889 if (res != GST_RTSP_EINTR) {
7890 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7891 ("Could not send message. (%s)", str));
7893 GST_WARNING_OBJECT (src, "PLAY interrupted");
7900 static GstRTSPResult
7901 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
7903 GstRTSPResult res = GST_RTSP_OK;
7904 GstRTSPMessage request = { 0 };
7905 GstRTSPMessage response = { 0 };
7907 const gchar *control;
7909 GST_DEBUG_OBJECT (src, "PAUSE...");
7911 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7914 if (!(src->methods & GST_RTSP_PAUSE))
7917 if (src->state == GST_RTSP_STATE_READY)
7920 if (!src->conninfo.connection || !src->conninfo.connected)
7923 /* construct a control url */
7924 control = get_aggregate_control (src);
7926 /* loop over the streams. We might exit the loop early when we could do an
7927 * aggregate control */
7928 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7929 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7930 GstRTSPConnection *conn;
7931 const gchar *setup_url;
7933 /* try aggregate control first but do non-aggregate control otherwise */
7935 setup_url = control;
7936 else if ((setup_url = stream->conninfo.location) == NULL)
7939 if (src->conninfo.connection) {
7940 conn = src->conninfo.connection;
7941 } else if (stream->conninfo.connection) {
7942 conn = stream->conninfo.connection;
7948 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7949 ("Sending PAUSE request"));
7952 gst_rtspsrc_init_request (src, &request, GST_RTSP_PAUSE,
7954 goto create_request_failed;
7956 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7959 gst_rtsp_message_unset (&request);
7960 gst_rtsp_message_unset (&response);
7962 /* exit early when we did agregate control */
7967 /* change element states now */
7968 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7971 src->state = GST_RTSP_STATE_READY;
7975 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7982 GST_DEBUG_OBJECT (src, "failed to open stream");
7987 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7992 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7995 create_request_failed:
7997 gchar *str = gst_rtsp_strresult (res);
7999 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
8000 ("Could not create request. (%s)", str));
8006 gchar *str = gst_rtsp_strresult (res);
8008 gst_rtsp_message_unset (&request);
8009 if (res != GST_RTSP_EINTR) {
8010 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
8011 ("Could not send message. (%s)", str));
8013 GST_WARNING_OBJECT (src, "PAUSE interrupted");
8021 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
8023 GstRTSPSrc *rtspsrc;
8025 rtspsrc = GST_RTSPSRC (bin);
8027 switch (GST_MESSAGE_TYPE (message)) {
8028 case GST_MESSAGE_EOS:
8029 gst_message_unref (message);
8031 case GST_MESSAGE_ELEMENT:
8033 const GstStructure *s = gst_message_get_structure (message);
8035 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
8036 gboolean ignore_timeout;
8038 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
8040 GST_OBJECT_LOCK (rtspsrc);
8041 ignore_timeout = rtspsrc->ignore_timeout;
8042 rtspsrc->ignore_timeout = TRUE;
8043 GST_OBJECT_UNLOCK (rtspsrc);
8045 /* we only act on the first udp timeout message, others are irrelevant
8046 * and can be ignored. */
8047 if (!ignore_timeout)
8048 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
8050 gst_message_unref (message);
8053 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8056 case GST_MESSAGE_ERROR:
8059 GstRTSPStream *stream;
8062 udpsrc = GST_MESSAGE_SRC (message);
8064 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
8065 GST_ELEMENT_NAME (udpsrc));
8067 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
8071 /* we ignore the RTCP udpsrc */
8072 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
8075 /* if we get error messages from the udp sources, that's not a problem as
8076 * long as not all of them error out. We also don't really know what the
8077 * problem is, the message does not give enough detail... */
8078 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
8079 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
8080 if (ret != GST_FLOW_OK)
8084 gst_message_unref (message);
8088 /* fatal but not our message, forward */
8089 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8094 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8100 /* the thread where everything happens */
8102 gst_rtspsrc_thread (GstRTSPSrc * src)
8106 GST_OBJECT_LOCK (src);
8107 cmd = src->pending_cmd;
8108 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
8109 || cmd == CMD_LOOP || cmd == CMD_OPEN)
8110 src->pending_cmd = CMD_LOOP;
8112 src->pending_cmd = CMD_WAIT;
8113 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
8115 /* we got the message command, so ensure communication is possible again */
8116 gst_rtspsrc_connection_flush (src, FALSE);
8118 src->busy_cmd = cmd;
8119 GST_OBJECT_UNLOCK (src);
8123 gst_rtspsrc_open (src, TRUE);
8126 gst_rtspsrc_play (src, &src->segment, TRUE);
8129 gst_rtspsrc_pause (src, TRUE);
8132 gst_rtspsrc_close (src, TRUE, FALSE);
8135 gst_rtspsrc_loop (src);
8138 gst_rtspsrc_reconnect (src, FALSE);
8144 GST_OBJECT_LOCK (src);
8145 /* and go back to sleep */
8146 if (src->pending_cmd == CMD_WAIT) {
8148 gst_task_pause (src->task);
8151 src->busy_cmd = CMD_WAIT;
8152 GST_OBJECT_UNLOCK (src);
8156 gst_rtspsrc_start (GstRTSPSrc * src)
8158 GST_DEBUG_OBJECT (src, "starting");
8160 GST_OBJECT_LOCK (src);
8162 src->pending_cmd = CMD_WAIT;
8164 if (src->task == NULL) {
8165 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
8166 if (src->task == NULL)
8169 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
8171 GST_OBJECT_UNLOCK (src);
8178 GST_OBJECT_UNLOCK (src);
8179 GST_ERROR_OBJECT (src, "failed to create task");
8185 gst_rtspsrc_stop (GstRTSPSrc * src)
8189 GST_DEBUG_OBJECT (src, "stopping");
8191 /* also cancels pending task */
8192 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
8194 GST_OBJECT_LOCK (src);
8195 if ((task = src->task)) {
8197 GST_OBJECT_UNLOCK (src);
8199 gst_task_stop (task);
8201 /* make sure it is not running */
8202 GST_RTSP_STREAM_LOCK (src);
8203 GST_RTSP_STREAM_UNLOCK (src);
8205 /* now wait for the task to finish */
8206 gst_task_join (task);
8208 /* and free the task */
8209 gst_object_unref (GST_OBJECT (task));
8211 GST_OBJECT_LOCK (src);
8213 GST_OBJECT_UNLOCK (src);
8215 /* ensure synchronously all is closed and clean */
8216 gst_rtspsrc_close (src, FALSE, TRUE);
8221 static GstStateChangeReturn
8222 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
8224 GstRTSPSrc *rtspsrc;
8225 GstStateChangeReturn ret;
8227 rtspsrc = GST_RTSPSRC (element);
8229 switch (transition) {
8230 case GST_STATE_CHANGE_NULL_TO_READY:
8231 if (!gst_rtspsrc_start (rtspsrc))
8234 case GST_STATE_CHANGE_READY_TO_PAUSED:
8235 /* init some state */
8236 rtspsrc->cur_protocols = rtspsrc->protocols;
8237 /* first attempt, don't ignore timeouts */
8238 rtspsrc->ignore_timeout = FALSE;
8239 rtspsrc->open_error = FALSE;
8240 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
8242 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8243 set_manager_buffer_mode (rtspsrc);
8245 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8246 /* unblock the tcp tasks and make the loop waiting */
8247 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
8248 /* make sure it is waiting before we send PAUSE or PLAY below */
8249 GST_RTSP_STREAM_LOCK (rtspsrc);
8250 GST_RTSP_STREAM_UNLOCK (rtspsrc);
8253 case GST_STATE_CHANGE_PAUSED_TO_READY:
8259 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
8260 if (ret == GST_STATE_CHANGE_FAILURE)
8263 switch (transition) {
8264 case GST_STATE_CHANGE_NULL_TO_READY:
8265 ret = GST_STATE_CHANGE_SUCCESS;
8267 case GST_STATE_CHANGE_READY_TO_PAUSED:
8268 ret = GST_STATE_CHANGE_NO_PREROLL;
8270 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8271 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
8272 ret = GST_STATE_CHANGE_SUCCESS;
8274 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8275 /* send pause request and keep the idle task around */
8276 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
8277 ret = GST_STATE_CHANGE_NO_PREROLL;
8279 case GST_STATE_CHANGE_PAUSED_TO_READY:
8280 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
8281 ret = GST_STATE_CHANGE_SUCCESS;
8283 case GST_STATE_CHANGE_READY_TO_NULL:
8284 gst_rtspsrc_stop (rtspsrc);
8285 ret = GST_STATE_CHANGE_SUCCESS;
8296 GST_DEBUG_OBJECT (rtspsrc, "start failed");
8297 return GST_STATE_CHANGE_FAILURE;
8302 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
8305 GstRTSPSrc *rtspsrc;
8307 rtspsrc = GST_RTSPSRC (element);
8309 if (GST_EVENT_IS_DOWNSTREAM (event)) {
8310 res = gst_rtspsrc_push_event (rtspsrc, event);
8312 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
8319 /*** GSTURIHANDLER INTERFACE *************************************************/
8322 gst_rtspsrc_uri_get_type (GType type)
8327 static const gchar *const *
8328 gst_rtspsrc_uri_get_protocols (GType type)
8330 static const gchar *protocols[] =
8331 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
8332 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
8339 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
8341 GstRTSPSrc *src = GST_RTSPSRC (handler);
8343 /* FIXME: make thread-safe */
8344 return g_strdup (src->conninfo.location);
8348 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
8354 GstRTSPUrl *newurl = NULL;
8355 GstSDPMessage *sdp = NULL;
8357 src = GST_RTSPSRC (handler);
8359 /* same URI, we're fine */
8360 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
8363 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
8364 sres = gst_sdp_message_new (&sdp);
8368 GST_DEBUG_OBJECT (src, "parsing SDP message");
8369 sres = gst_sdp_message_parse_uri (uri, sdp);
8374 GST_DEBUG_OBJECT (src, "parsing URI");
8375 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
8379 /* if worked, free previous and store new url object along with the original
8381 GST_DEBUG_OBJECT (src, "configuring URI");
8382 g_free (src->conninfo.location);
8383 src->conninfo.location = g_strdup (uri);
8384 gst_rtsp_url_free (src->conninfo.url);
8385 src->conninfo.url = newurl;
8386 g_free (src->conninfo.url_str);
8388 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
8390 src->conninfo.url_str = NULL;
8393 gst_sdp_message_free (src->sdp);
8395 src->from_sdp = sdp != NULL;
8397 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
8398 GST_DEBUG_OBJECT (src, "request uri is: %s",
8399 GST_STR_NULL (src->conninfo.url_str));
8406 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
8411 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
8412 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8413 "Could not create SDP");
8418 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
8419 GST_STR_NULL (uri));
8420 gst_sdp_message_free (sdp);
8421 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8427 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
8428 GST_STR_NULL (uri), res);
8429 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8430 "Invalid RTSP URI");
8436 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
8438 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
8440 iface->get_type = gst_rtspsrc_uri_get_type;
8441 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
8442 iface->get_uri = gst_rtspsrc_uri_get_uri;
8443 iface->set_uri = gst_rtspsrc_uri_set_uri;