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 DEFAULT_LOCATION NULL
199 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
200 #define DEFAULT_DEBUG FALSE
201 #define DEFAULT_RETRY 20
202 #define DEFAULT_TIMEOUT 5000000
203 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
204 #define DEFAULT_TCP_TIMEOUT 20000000
205 #define DEFAULT_LATENCY_MS 2000
206 #define DEFAULT_DROP_ON_LATENCY FALSE
207 #define DEFAULT_CONNECTION_SPEED 0
208 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
209 #define DEFAULT_DO_RTCP TRUE
210 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
211 #define DEFAULT_PROXY NULL
212 #define DEFAULT_RTP_BLOCKSIZE 0
213 #define DEFAULT_USER_ID NULL
214 #define DEFAULT_USER_PW NULL
215 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
216 #define DEFAULT_PORT_RANGE NULL
217 #define DEFAULT_SHORT_HEADER FALSE
218 #define DEFAULT_PROBATION 2
219 #define DEFAULT_UDP_RECONNECT TRUE
220 #define DEFAULT_MULTICAST_IFACE NULL
221 #define DEFAULT_NTP_SYNC FALSE
222 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
223 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
224 #define DEFAULT_TLS_DATABASE NULL
225 #define DEFAULT_TLS_INTERACTION NULL
226 #define DEFAULT_DO_RETRANSMISSION TRUE
227 #define DEFAULT_NTP_TIME_SOURCE NTP_TIME_SOURCE_NTP
228 #define DEFAULT_USER_AGENT "GStreamer/" PACKAGE_VERSION
229 #define DEFAULT_MAX_RTCP_RTP_TIME_DIFF 1000
230 #define DEFAULT_RFC7273_SYNC FALSE
242 PROP_DROP_ON_LATENCY,
243 PROP_CONNECTION_SPEED,
246 PROP_DO_RTSP_KEEP_ALIVE,
255 PROP_UDP_BUFFER_SIZE,
259 PROP_MULTICAST_IFACE,
261 PROP_USE_PIPELINE_CLOCK,
263 PROP_TLS_VALIDATION_FLAGS,
265 PROP_TLS_INTERACTION,
266 PROP_DO_RETRANSMISSION,
267 PROP_NTP_TIME_SOURCE,
269 PROP_MAX_RTCP_RTP_TIME_DIFF,
273 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
275 gst_rtsp_nat_method_get_type (void)
277 static GType rtsp_nat_method_type = 0;
278 static const GEnumValue rtsp_nat_method[] = {
279 {GST_RTSP_NAT_NONE, "None", "none"},
280 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
284 if (!rtsp_nat_method_type) {
285 rtsp_nat_method_type =
286 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
288 return rtsp_nat_method_type;
291 static void gst_rtspsrc_finalize (GObject * object);
293 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
294 const GValue * value, GParamSpec * pspec);
295 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
296 GValue * value, GParamSpec * pspec);
298 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
300 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
301 gpointer iface_data);
303 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
304 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
306 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
307 GstStateChange transition);
308 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
309 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
311 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
312 GstRTSPMessage * response);
314 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
316 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
317 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
319 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
320 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
322 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
323 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
324 gboolean only_close);
326 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
327 const gchar * uri, GError ** error);
328 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
330 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
331 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
332 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
333 GstRTSPStream * stream, GstEvent * event);
334 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
335 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
336 static GstRTSPResult gst_rtsp_conninfo_close (GstRTSPSrc * src,
337 GstRTSPConnInfo * info, gboolean free);
345 /* commands we send to out loop to notify it of events */
346 #define CMD_OPEN (1 << 0)
347 #define CMD_PLAY (1 << 1)
348 #define CMD_PAUSE (1 << 2)
349 #define CMD_CLOSE (1 << 3)
350 #define CMD_WAIT (1 << 4)
351 #define CMD_RECONNECT (1 << 5)
352 #define CMD_LOOP (1 << 6)
354 /* mask for all commands */
355 #define CMD_ALL ((CMD_LOOP << 1) - 1)
357 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
359 gchar *__txt = _gst_element_error_printf text; \
360 gst_element_post_message (GST_ELEMENT_CAST (el), \
361 gst_message_new_progress (GST_OBJECT_CAST (el), \
362 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
366 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
368 #define gst_rtspsrc_parent_class parent_class
369 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
370 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
372 #ifndef GST_DISABLE_GST_DEBUG
373 static inline const char *
374 cmd_to_string (guint cmd)
398 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
400 GST_DEBUG_OBJECT (src, "default handler");
405 select_stream_accum (GSignalInvocationHint * ihint,
406 GValue * return_accu, const GValue * handler_return, gpointer data)
410 myboolean = g_value_get_boolean (handler_return);
411 GST_DEBUG ("accum %d", myboolean);
412 g_value_set_boolean (return_accu, myboolean);
414 /* stop emission if FALSE */
419 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
421 GObjectClass *gobject_class;
422 GstElementClass *gstelement_class;
423 GstBinClass *gstbin_class;
425 gobject_class = (GObjectClass *) klass;
426 gstelement_class = (GstElementClass *) klass;
427 gstbin_class = (GstBinClass *) klass;
429 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
431 gobject_class->set_property = gst_rtspsrc_set_property;
432 gobject_class->get_property = gst_rtspsrc_get_property;
434 gobject_class->finalize = gst_rtspsrc_finalize;
436 g_object_class_install_property (gobject_class, PROP_LOCATION,
437 g_param_spec_string ("location", "RTSP Location",
438 "Location of the RTSP url to read",
439 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
441 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
442 g_param_spec_flags ("protocols", "Protocols",
443 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
444 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
446 g_object_class_install_property (gobject_class, PROP_DEBUG,
447 g_param_spec_boolean ("debug", "Debug",
448 "Dump request and response messages to stdout",
449 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
451 g_object_class_install_property (gobject_class, PROP_RETRY,
452 g_param_spec_uint ("retry", "Retry",
453 "Max number of retries when allocating RTP ports.",
454 0, G_MAXUINT16, DEFAULT_RETRY,
455 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
457 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
458 g_param_spec_uint64 ("timeout", "Timeout",
459 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
460 0, G_MAXUINT64, DEFAULT_TIMEOUT,
461 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
463 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
464 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
465 "Fail after timeout microseconds on TCP connections (0 = disabled)",
466 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
467 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
469 g_object_class_install_property (gobject_class, PROP_LATENCY,
470 g_param_spec_uint ("latency", "Buffer latency in ms",
471 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
472 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
474 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
475 g_param_spec_boolean ("drop-on-latency",
476 "Drop buffers when maximum latency is reached",
477 "Tells the jitterbuffer to never exceed the given latency in size",
478 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
480 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
481 g_param_spec_uint64 ("connection-speed", "Connection Speed",
482 "Network connection speed in kbps (0 = unknown)",
483 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
484 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
486 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
487 g_param_spec_enum ("nat-method", "NAT Method",
488 "Method to use for traversing firewalls and NAT",
489 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
490 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
493 * GstRTSPSrc:do-rtcp:
495 * Enable RTCP support. Some old server don't like RTCP and then this property
496 * needs to be set to FALSE.
498 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
499 g_param_spec_boolean ("do-rtcp", "Do RTCP",
500 "Send RTCP packets, disable for old incompatible server.",
501 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
504 * GstRTSPSrc:do-rtsp-keep-alive:
506 * Enable RTSP keep alive support. Some old server don't like RTSP
507 * keep alive and then this property needs to be set to FALSE.
509 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
510 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
511 "Send RTSP keep alive packets, disable for old incompatible server.",
512 DEFAULT_DO_RTSP_KEEP_ALIVE,
513 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
518 * Set the proxy parameters. This has to be a string of the format
519 * [http://][user:passwd@]host[:port].
521 g_object_class_install_property (gobject_class, PROP_PROXY,
522 g_param_spec_string ("proxy", "Proxy",
523 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
524 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
526 * GstRTSPSrc:proxy-id:
528 * Sets the proxy URI user id for authentication. If the URI set via the
529 * "proxy" property contains a user-id already, that will take precedence.
533 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
534 g_param_spec_string ("proxy-id", "proxy-id",
535 "HTTP proxy URI user id for authentication", "",
536 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
538 * GstRTSPSrc:proxy-pw:
540 * Sets the proxy URI password for authentication. If the URI set via the
541 * "proxy" property contains a password already, that will take precedence.
545 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
546 g_param_spec_string ("proxy-pw", "proxy-pw",
547 "HTTP proxy URI user password for authentication", "",
548 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
551 * GstRTSPSrc:rtp-blocksize:
553 * RTP package size to suggest to server.
555 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
556 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
557 "RTP package size to suggest to server (0 = disabled)",
558 0, 65536, DEFAULT_RTP_BLOCKSIZE,
559 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
561 g_object_class_install_property (gobject_class,
563 g_param_spec_string ("user-id", "user-id",
564 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
565 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
566 g_object_class_install_property (gobject_class, PROP_USER_PW,
567 g_param_spec_string ("user-pw", "user-pw",
568 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
569 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
572 * GstRTSPSrc:buffer-mode:
574 * Control the buffering and timestamping mode used by the jitterbuffer.
576 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
577 g_param_spec_enum ("buffer-mode", "Buffer Mode",
578 "Control the buffering algorithm in use",
579 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
580 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
583 * GstRTSPSrc:port-range:
585 * Configure the client port numbers that can be used to recieve RTP and
588 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
589 g_param_spec_string ("port-range", "Port range",
590 "Client port range that can be used to receive RTP and RTCP data, "
591 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
592 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
595 * GstRTSPSrc:udp-buffer-size:
597 * Size of the kernel UDP receive buffer in bytes.
599 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
600 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
601 "Size of the kernel UDP receive buffer in bytes, 0=default",
602 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
603 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
606 * GstRTSPSrc:short-header:
608 * Only send the basic RTSP headers for broken encoders.
610 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
611 g_param_spec_boolean ("short-header", "Short Header",
612 "Only send the basic RTSP headers for broken encoders",
613 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
615 g_object_class_install_property (gobject_class, PROP_PROBATION,
616 g_param_spec_uint ("probation", "Number of probations",
617 "Consecutive packet sequence numbers to accept the source",
618 0, G_MAXUINT, DEFAULT_PROBATION,
619 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
621 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
622 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
623 "Reconnect to the server if RTSP connection is closed when doing UDP",
624 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
626 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
627 g_param_spec_string ("multicast-iface", "Multicast Interface",
628 "The network interface on which to join the multicast group",
629 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
631 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
632 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
633 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
634 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
636 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
637 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
638 "Use the pipeline running-time to set the NTP time in the RTCP SR messages"
639 "(DEPRECATED: Use ntp-time-source property)",
640 DEFAULT_USE_PIPELINE_CLOCK,
641 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
643 g_object_class_install_property (gobject_class, PROP_SDES,
644 g_param_spec_boxed ("sdes", "SDES",
645 "The SDES items of this session",
646 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
649 * GstRTSPSrc::tls-validation-flags:
651 * TLS certificate validation flags used to validate server
656 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
657 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
658 "TLS certificate validation flags used to validate the server certificate",
659 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
660 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
663 * GstRTSPSrc::tls-database:
665 * TLS database with anchor certificate authorities used to validate
666 * the server certificate.
670 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
671 g_param_spec_object ("tls-database", "TLS database",
672 "TLS database with anchor certificate authorities used to validate the server certificate",
673 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
676 * GstRTSPSrc::tls-interaction:
678 * A #GTlsInteraction object to be used when the connection or certificate
679 * database need to interact with the user. This will be used to prompt the
680 * user for passwords where necessary.
684 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
685 g_param_spec_object ("tls-interaction", "TLS interaction",
686 "A GTlsInteraction object to promt the user for password or certificate",
687 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
690 * GstRTSPSrc::do-retransmission:
692 * Attempt to ask the server to retransmit lost packets according to RFC4588.
694 * Note: currently only works with SSRC-multiplexed retransmission streams
698 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
699 g_param_spec_boolean ("do-retransmission", "Retransmission",
700 "Ask the server to retransmit lost packets",
701 DEFAULT_DO_RETRANSMISSION,
702 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
705 * GstRTSPSrc::ntp-time-source:
707 * allows to select the time source that should be used
708 * for the NTP time in RTCP packets
712 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
713 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
714 "NTP time source for RTCP packets",
715 GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE, DEFAULT_NTP_TIME_SOURCE,
716 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
719 * GstRTSPSrc::user-agent:
721 * The string to set in the User-Agent header.
725 g_object_class_install_property (gobject_class, PROP_USER_AGENT,
726 g_param_spec_string ("user-agent", "User Agent",
727 "The User-Agent string to send to the server",
728 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
730 g_object_class_install_property (gobject_class, PROP_MAX_RTCP_RTP_TIME_DIFF,
731 g_param_spec_int ("max-rtcp-rtp-time-diff", "Max RTCP RTP Time Diff",
732 "Maximum amount of time in ms that the RTP time in RTCP SRs "
733 "is allowed to be ahead (-1 disabled)", -1, G_MAXINT,
734 DEFAULT_MAX_RTCP_RTP_TIME_DIFF,
735 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
737 g_object_class_install_property (gobject_class, PROP_RFC7273_SYNC,
738 g_param_spec_boolean ("rfc7273-sync", "Sync on RFC7273 clock",
739 "Synchronize received streams to the RFC7273 clock "
740 "(requires clock and offset to be provided)", DEFAULT_RFC7273_SYNC,
741 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
744 * GstRTSPSrc::handle-request:
745 * @rtspsrc: a #GstRTSPSrc
746 * @request: a #GstRTSPMessage
747 * @response: a #GstRTSPMessage
749 * Handle a server request in @request and prepare @response.
751 * This signal is called from the streaming thread, you should therefore not
752 * do any state changes on @rtspsrc because this might deadlock. If you want
753 * to modify the state as a result of this signal, post a
754 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
759 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
760 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
761 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
762 G_TYPE_POINTER, G_TYPE_POINTER);
765 * GstRTSPSrc::on-sdp:
766 * @rtspsrc: a #GstRTSPSrc
767 * @sdp: a #GstSDPMessage
769 * Emited when the client has retrieved the SDP and before it configures the
770 * streams in the SDP. @sdp can be inspected and modified.
772 * This signal is called from the streaming thread, you should therefore not
773 * do any state changes on @rtspsrc because this might deadlock. If you want
774 * to modify the state as a result of this signal, post a
775 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
780 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
781 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
782 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
783 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
786 * GstRTSPSrc::select-stream:
787 * @rtspsrc: a #GstRTSPSrc
788 * @num: the stream number
789 * @caps: the stream caps
791 * Emited before the client decides to configure the stream @num with
794 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
799 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
800 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
801 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
802 (GCallback) default_select_stream, select_stream_accum, NULL,
803 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
806 * GstRTSPSrc::new-manager:
807 * @rtspsrc: a #GstRTSPSrc
808 * @manager: a #GstElement
810 * Emited after a new manager (like rtpbin) was created and the default
811 * properties were configured.
815 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
816 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
817 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
818 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
821 * GstRTSPSrc::request-rtcp-key:
822 * @rtspsrc: a #GstRTSPSrc
823 * @num: the stream number
825 * Signal emited to get the crypto parameters relevant to the RTCP
826 * stream. User should provide the key and the RTCP encryption ciphers
827 * and authentication, and return them wrapped in a GstCaps.
831 gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY] =
832 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
833 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
835 gstelement_class->send_event = gst_rtspsrc_send_event;
836 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
837 gstelement_class->change_state = gst_rtspsrc_change_state;
839 gst_element_class_add_static_pad_template (gstelement_class, &rtptemplate);
841 gst_element_class_set_static_metadata (gstelement_class,
842 "RTSP packet receiver", "Source/Network",
843 "Receive data over the network via RTSP (RFC 2326)",
844 "Wim Taymans <wim@fluendo.com>, "
845 "Thijs Vermeir <thijs.vermeir@barco.com>, "
846 "Lutz Mueller <lutz@topfrose.de>");
848 gstbin_class->handle_message = gst_rtspsrc_handle_message;
850 gst_rtsp_ext_list_init ();
854 gst_rtspsrc_init (GstRTSPSrc * src)
856 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
857 src->protocols = DEFAULT_PROTOCOLS;
858 src->debug = DEFAULT_DEBUG;
859 src->retry = DEFAULT_RETRY;
860 src->udp_timeout = DEFAULT_TIMEOUT;
861 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
862 src->latency = DEFAULT_LATENCY_MS;
863 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
864 src->connection_speed = DEFAULT_CONNECTION_SPEED;
865 src->nat_method = DEFAULT_NAT_METHOD;
866 src->do_rtcp = DEFAULT_DO_RTCP;
867 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
868 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
869 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
870 src->user_id = g_strdup (DEFAULT_USER_ID);
871 src->user_pw = g_strdup (DEFAULT_USER_PW);
872 src->buffer_mode = DEFAULT_BUFFER_MODE;
873 src->client_port_range.min = 0;
874 src->client_port_range.max = 0;
875 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
876 src->short_header = DEFAULT_SHORT_HEADER;
877 src->probation = DEFAULT_PROBATION;
878 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
879 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
880 src->ntp_sync = DEFAULT_NTP_SYNC;
881 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
883 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
884 src->tls_database = DEFAULT_TLS_DATABASE;
885 src->tls_interaction = DEFAULT_TLS_INTERACTION;
886 src->do_retransmission = DEFAULT_DO_RETRANSMISSION;
887 src->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
888 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
889 src->max_rtcp_rtp_time_diff = DEFAULT_MAX_RTCP_RTP_TIME_DIFF;
890 src->rfc7273_sync = DEFAULT_RFC7273_SYNC;
892 /* get a list of all extensions */
893 src->extensions = gst_rtsp_ext_list_get ();
895 /* connect to send signal */
896 gst_rtsp_ext_list_connect (src->extensions, "send",
897 (GCallback) gst_rtspsrc_send_cb, src);
899 /* protects the streaming thread in interleaved mode or the polling
900 * thread in UDP mode. */
901 g_rec_mutex_init (&src->stream_rec_lock);
903 /* protects our state changes from multiple invocations */
904 g_rec_mutex_init (&src->state_rec_lock);
906 src->state = GST_RTSP_STATE_INVALID;
908 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
912 gst_rtspsrc_finalize (GObject * object)
916 rtspsrc = GST_RTSPSRC (object);
918 gst_rtsp_ext_list_free (rtspsrc->extensions);
919 g_free (rtspsrc->conninfo.location);
920 gst_rtsp_url_free (rtspsrc->conninfo.url);
921 g_free (rtspsrc->conninfo.url_str);
922 g_free (rtspsrc->user_id);
923 g_free (rtspsrc->user_pw);
924 g_free (rtspsrc->multi_iface);
925 g_free (rtspsrc->user_agent);
928 gst_sdp_message_free (rtspsrc->sdp);
931 if (rtspsrc->provided_clock)
932 gst_object_unref (rtspsrc->provided_clock);
935 gst_structure_free (rtspsrc->sdes);
937 if (rtspsrc->tls_database)
938 g_object_unref (rtspsrc->tls_database);
940 if (rtspsrc->tls_interaction)
941 g_object_unref (rtspsrc->tls_interaction);
944 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
945 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
947 G_OBJECT_CLASS (parent_class)->finalize (object);
951 gst_rtspsrc_provide_clock (GstElement * element)
953 GstRTSPSrc *src = GST_RTSPSRC (element);
956 if ((clock = src->provided_clock) != NULL)
957 gst_object_ref (clock);
962 /* a proxy string of the format [user:passwd@]host[:port] */
964 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
968 g_free (rtsp->proxy_user);
969 rtsp->proxy_user = NULL;
970 g_free (rtsp->proxy_passwd);
971 rtsp->proxy_passwd = NULL;
972 g_free (rtsp->proxy_host);
973 rtsp->proxy_host = NULL;
974 rtsp->proxy_port = 0;
981 /* we allow http:// in front but ignore it */
982 if (g_str_has_prefix (p, "http://"))
985 at = strchr (p, '@');
987 /* look for user:passwd */
988 col = strchr (proxy, ':');
989 if (col == NULL || col > at)
992 rtsp->proxy_user = g_strndup (p, col - p);
994 rtsp->proxy_passwd = g_strndup (col, at - col);
999 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
1000 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
1001 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
1002 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
1003 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
1004 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
1005 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
1008 col = strchr (p, ':');
1011 /* everything before the colon is the hostname */
1012 rtsp->proxy_host = g_strndup (p, col - p);
1014 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
1016 rtsp->proxy_host = g_strdup (p);
1017 rtsp->proxy_port = 8080;
1023 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
1025 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
1026 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
1029 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
1031 rtspsrc->ptcp_timeout = NULL;
1035 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
1038 GstRTSPSrc *rtspsrc;
1040 rtspsrc = GST_RTSPSRC (object);
1044 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
1045 g_value_get_string (value), NULL);
1047 case PROP_PROTOCOLS:
1048 rtspsrc->protocols = g_value_get_flags (value);
1051 rtspsrc->debug = g_value_get_boolean (value);
1054 rtspsrc->retry = g_value_get_uint (value);
1057 rtspsrc->udp_timeout = g_value_get_uint64 (value);
1059 case PROP_TCP_TIMEOUT:
1060 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
1063 rtspsrc->latency = g_value_get_uint (value);
1065 case PROP_DROP_ON_LATENCY:
1066 rtspsrc->drop_on_latency = g_value_get_boolean (value);
1068 case PROP_CONNECTION_SPEED:
1069 rtspsrc->connection_speed = g_value_get_uint64 (value);
1071 case PROP_NAT_METHOD:
1072 rtspsrc->nat_method = g_value_get_enum (value);
1075 rtspsrc->do_rtcp = g_value_get_boolean (value);
1077 case PROP_DO_RTSP_KEEP_ALIVE:
1078 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
1081 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
1084 g_free (rtspsrc->prop_proxy_id);
1085 rtspsrc->prop_proxy_id = g_value_dup_string (value);
1088 g_free (rtspsrc->prop_proxy_pw);
1089 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
1091 case PROP_RTP_BLOCKSIZE:
1092 rtspsrc->rtp_blocksize = g_value_get_uint (value);
1095 g_free (rtspsrc->user_id);
1096 rtspsrc->user_id = g_value_dup_string (value);
1099 g_free (rtspsrc->user_pw);
1100 rtspsrc->user_pw = g_value_dup_string (value);
1102 case PROP_BUFFER_MODE:
1103 rtspsrc->buffer_mode = g_value_get_enum (value);
1105 case PROP_PORT_RANGE:
1109 str = g_value_get_string (value);
1110 if (sscanf (str, "%u-%u", &rtspsrc->client_port_range.min,
1111 &rtspsrc->client_port_range.max) != 2) {
1112 rtspsrc->client_port_range.min = 0;
1113 rtspsrc->client_port_range.max = 0;
1117 case PROP_UDP_BUFFER_SIZE:
1118 rtspsrc->udp_buffer_size = g_value_get_int (value);
1120 case PROP_SHORT_HEADER:
1121 rtspsrc->short_header = g_value_get_boolean (value);
1123 case PROP_PROBATION:
1124 rtspsrc->probation = g_value_get_uint (value);
1126 case PROP_UDP_RECONNECT:
1127 rtspsrc->udp_reconnect = g_value_get_boolean (value);
1129 case PROP_MULTICAST_IFACE:
1130 g_free (rtspsrc->multi_iface);
1132 if (g_value_get_string (value) == NULL)
1133 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1135 rtspsrc->multi_iface = g_value_dup_string (value);
1138 rtspsrc->ntp_sync = g_value_get_boolean (value);
1140 case PROP_USE_PIPELINE_CLOCK:
1141 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
1144 rtspsrc->sdes = g_value_dup_boxed (value);
1146 case PROP_TLS_VALIDATION_FLAGS:
1147 rtspsrc->tls_validation_flags = g_value_get_flags (value);
1149 case PROP_TLS_DATABASE:
1150 g_clear_object (&rtspsrc->tls_database);
1151 rtspsrc->tls_database = g_value_dup_object (value);
1153 case PROP_TLS_INTERACTION:
1154 g_clear_object (&rtspsrc->tls_interaction);
1155 rtspsrc->tls_interaction = g_value_dup_object (value);
1157 case PROP_DO_RETRANSMISSION:
1158 rtspsrc->do_retransmission = g_value_get_boolean (value);
1160 case PROP_NTP_TIME_SOURCE:
1161 rtspsrc->ntp_time_source = g_value_get_enum (value);
1163 case PROP_USER_AGENT:
1164 g_free (rtspsrc->user_agent);
1165 rtspsrc->user_agent = g_value_dup_string (value);
1167 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1168 rtspsrc->max_rtcp_rtp_time_diff = g_value_get_int (value);
1170 case PROP_RFC7273_SYNC:
1171 rtspsrc->rfc7273_sync = g_value_get_boolean (value);
1174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1180 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1183 GstRTSPSrc *rtspsrc;
1185 rtspsrc = GST_RTSPSRC (object);
1189 g_value_set_string (value, rtspsrc->conninfo.location);
1191 case PROP_PROTOCOLS:
1192 g_value_set_flags (value, rtspsrc->protocols);
1195 g_value_set_boolean (value, rtspsrc->debug);
1198 g_value_set_uint (value, rtspsrc->retry);
1201 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1203 case PROP_TCP_TIMEOUT:
1207 timeout = ((guint64) rtspsrc->tcp_timeout.tv_sec) * G_USEC_PER_SEC +
1208 rtspsrc->tcp_timeout.tv_usec;
1209 g_value_set_uint64 (value, timeout);
1213 g_value_set_uint (value, rtspsrc->latency);
1215 case PROP_DROP_ON_LATENCY:
1216 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1218 case PROP_CONNECTION_SPEED:
1219 g_value_set_uint64 (value, rtspsrc->connection_speed);
1221 case PROP_NAT_METHOD:
1222 g_value_set_enum (value, rtspsrc->nat_method);
1225 g_value_set_boolean (value, rtspsrc->do_rtcp);
1227 case PROP_DO_RTSP_KEEP_ALIVE:
1228 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1234 if (rtspsrc->proxy_host) {
1236 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1240 g_value_take_string (value, str);
1244 g_value_set_string (value, rtspsrc->prop_proxy_id);
1247 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1249 case PROP_RTP_BLOCKSIZE:
1250 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1253 g_value_set_string (value, rtspsrc->user_id);
1256 g_value_set_string (value, rtspsrc->user_pw);
1258 case PROP_BUFFER_MODE:
1259 g_value_set_enum (value, rtspsrc->buffer_mode);
1261 case PROP_PORT_RANGE:
1265 if (rtspsrc->client_port_range.min != 0) {
1266 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1267 rtspsrc->client_port_range.max);
1271 g_value_take_string (value, str);
1274 case PROP_UDP_BUFFER_SIZE:
1275 g_value_set_int (value, rtspsrc->udp_buffer_size);
1277 case PROP_SHORT_HEADER:
1278 g_value_set_boolean (value, rtspsrc->short_header);
1280 case PROP_PROBATION:
1281 g_value_set_uint (value, rtspsrc->probation);
1283 case PROP_UDP_RECONNECT:
1284 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1286 case PROP_MULTICAST_IFACE:
1287 g_value_set_string (value, rtspsrc->multi_iface);
1290 g_value_set_boolean (value, rtspsrc->ntp_sync);
1292 case PROP_USE_PIPELINE_CLOCK:
1293 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1296 g_value_set_boxed (value, rtspsrc->sdes);
1298 case PROP_TLS_VALIDATION_FLAGS:
1299 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1301 case PROP_TLS_DATABASE:
1302 g_value_set_object (value, rtspsrc->tls_database);
1304 case PROP_TLS_INTERACTION:
1305 g_value_set_object (value, rtspsrc->tls_interaction);
1307 case PROP_DO_RETRANSMISSION:
1308 g_value_set_boolean (value, rtspsrc->do_retransmission);
1310 case PROP_NTP_TIME_SOURCE:
1311 g_value_set_enum (value, rtspsrc->ntp_time_source);
1313 case PROP_USER_AGENT:
1314 g_value_set_string (value, rtspsrc->user_agent);
1316 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1317 g_value_set_int (value, rtspsrc->max_rtcp_rtp_time_diff);
1319 case PROP_RFC7273_SYNC:
1320 g_value_set_boolean (value, rtspsrc->rfc7273_sync);
1323 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1329 find_stream_by_id (GstRTSPStream * stream, gint * id)
1331 if (stream->id == *id)
1338 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1340 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1347 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1349 GstElement *src = (GstElement *) a;
1351 if (stream->udpsrc[0] == src)
1353 if (stream->udpsrc[1] == src)
1360 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1362 if (stream->conninfo.location) {
1363 /* check qualified setup_url */
1364 if (!strcmp (stream->conninfo.location, (gchar *) a))
1367 if (stream->control_url) {
1368 /* check original control_url */
1369 if (!strcmp (stream->control_url, (gchar *) a))
1372 /* check if qualified setup_url ends with string */
1373 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1380 static GstRTSPStream *
1381 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1385 /* find and get stream */
1386 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1387 return (GstRTSPStream *) lstream->data;
1392 static const GstSDPBandwidth *
1393 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1394 const GstSDPMedia * media, const gchar * type)
1398 /* first look in the media specific section */
1399 len = gst_sdp_media_bandwidths_len (media);
1400 for (i = 0; i < len; i++) {
1401 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1403 if (strcmp (bw->bwtype, type) == 0)
1406 /* then look in the message specific section */
1407 len = gst_sdp_message_bandwidths_len (sdp);
1408 for (i = 0; i < len; i++) {
1409 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1411 if (strcmp (bw->bwtype, type) == 0)
1418 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1419 const GstSDPMedia * media, GstRTSPStream * stream)
1421 const GstSDPBandwidth *bw;
1423 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1424 stream->as_bandwidth = bw->bandwidth;
1426 stream->as_bandwidth = -1;
1428 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1429 stream->rr_bandwidth = bw->bandwidth;
1431 stream->rr_bandwidth = -1;
1433 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1434 stream->rs_bandwidth = bw->bandwidth;
1436 stream->rs_bandwidth = -1;
1440 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1441 const GstSDPConnection * conn)
1443 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1446 if (conn->addrtype == NULL)
1449 /* check for IPV6 */
1450 if (strcmp (conn->addrtype, "IP4") == 0)
1451 stream->is_ipv6 = FALSE;
1452 else if (strcmp (conn->addrtype, "IP6") == 0)
1453 stream->is_ipv6 = TRUE;
1458 g_free (stream->destination);
1459 stream->destination = g_strdup (conn->address);
1461 /* check for multicast */
1462 stream->is_multicast =
1463 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1465 stream->ttl = conn->ttl;
1468 /* Go over the connections for a stream.
1469 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1471 * - If we are dealing with a localhost address, we disable multicast
1474 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1475 const GstSDPMedia * media, GstRTSPStream * stream)
1477 const GstSDPConnection *conn;
1480 /* first look in the media specific section */
1481 len = gst_sdp_media_connections_len (media);
1482 for (i = 0; i < len; i++) {
1483 conn = gst_sdp_media_get_connection (media, i);
1485 gst_rtspsrc_do_stream_connection (src, stream, conn);
1487 /* then look in the message specific section */
1488 if ((conn = gst_sdp_message_get_connection (sdp))) {
1489 gst_rtspsrc_do_stream_connection (src, stream, conn);
1493 /* m=<media> <UDP port> RTP/AVP <payload>
1496 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
1497 const GstSDPMedia * media, GstRTSPStream * stream)
1501 GstCaps *global_caps;
1504 proto = gst_sdp_media_get_proto (media);
1508 if (g_str_equal (proto, "RTP/AVP"))
1509 stream->profile = GST_RTSP_PROFILE_AVP;
1510 else if (g_str_equal (proto, "RTP/SAVP"))
1511 stream->profile = GST_RTSP_PROFILE_SAVP;
1512 else if (g_str_equal (proto, "RTP/AVPF"))
1513 stream->profile = GST_RTSP_PROFILE_AVPF;
1514 else if (g_str_equal (proto, "RTP/SAVPF"))
1515 stream->profile = GST_RTSP_PROFILE_SAVPF;
1519 /* Parse global SDP attributes once */
1520 global_caps = gst_caps_new_empty_simple ("application/x-unknown");
1521 GST_DEBUG ("mapping sdp session level attributes to caps");
1522 gst_sdp_message_attributes_to_caps (sdp, global_caps);
1523 GST_DEBUG ("mapping sdp media level attributes to caps");
1524 gst_sdp_media_attributes_to_caps (media, global_caps);
1526 /* Keep a copy of the SDP key management */
1527 gst_sdp_media_parse_keymgmt (media, &stream->mikey);
1528 if (stream->mikey == NULL)
1529 gst_sdp_message_parse_keymgmt (sdp, &stream->mikey);
1531 len = gst_sdp_media_formats_len (media);
1532 for (i = 0; i < len; i++) {
1534 GstCaps *caps, *outcaps;
1539 pt = atoi (gst_sdp_media_get_format (media, i));
1541 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
1544 caps = gst_sdp_media_get_caps_from_media (media, pt);
1546 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
1550 /* do some tweaks */
1551 s = gst_caps_get_structure (caps, 0);
1552 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
1553 stream->is_real = (strstr (enc, "-REAL") != NULL);
1554 if (strcmp (enc, "X-ASF-PF") == 0)
1555 stream->container = TRUE;
1558 /* Merge in global caps */
1559 /* Intersect will merge in missing fields to the current caps */
1560 outcaps = gst_caps_intersect (caps, global_caps);
1561 gst_caps_unref (caps);
1563 /* the first pt will be the default */
1564 if (stream->ptmap->len == 0)
1565 stream->default_pt = pt;
1568 item.caps = outcaps;
1570 g_array_append_val (stream->ptmap, item);
1573 gst_caps_unref (global_caps);
1578 GST_ERROR_OBJECT (src, "can't find proto in media");
1583 GST_ERROR_OBJECT (src, "unknown proto in media %s", proto);
1588 static const gchar *
1589 get_aggregate_control (GstRTSPSrc * src)
1594 base = src->control;
1595 else if (src->content_base)
1596 base = src->content_base;
1597 else if (src->conninfo.url_str)
1598 base = src->conninfo.url_str;
1606 clear_ptmap_item (PtMapItem * item)
1609 gst_caps_unref (item->caps);
1612 static GstRTSPStream *
1613 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1615 GstRTSPStream *stream;
1616 const gchar *control_url;
1617 const GstSDPMedia *media;
1619 /* get media, should not return NULL */
1620 media = gst_sdp_message_get_media (sdp, idx);
1624 stream = g_new0 (GstRTSPStream, 1);
1625 stream->parent = src;
1626 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1628 stream->last_ret = GST_FLOW_NOT_LINKED;
1629 stream->added = FALSE;
1630 stream->setup = FALSE;
1631 stream->skipped = FALSE;
1633 stream->eos = FALSE;
1634 stream->discont = TRUE;
1635 stream->seqbase = -1;
1636 stream->timebase = -1;
1637 stream->send_ssrc = g_random_int ();
1638 stream->profile = GST_RTSP_PROFILE_AVP;
1639 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1640 stream->mikey = NULL;
1641 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
1643 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1644 * session manager to scale RTCP. */
1645 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1647 /* collect connection info */
1648 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1650 /* make the payload type map */
1651 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1653 /* collect port number */
1654 stream->port = gst_sdp_media_get_port (media);
1656 /* get control url to construct the setup url. The setup url is used to
1657 * configure the transport of the stream and is used to identity the stream in
1658 * the RTP-Info header field returned from PLAY. */
1659 control_url = gst_sdp_media_get_attribute_val (media, "control");
1660 if (control_url == NULL)
1661 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1663 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1664 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1665 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1666 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1668 if (control_url != NULL) {
1669 stream->control_url = g_strdup (control_url);
1670 /* Build a fully qualified url using the content_base if any or by prefixing
1671 * the original request.
1672 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1673 * likely build a URL that the server will fail to understand, this is ok,
1674 * we will fail then. */
1675 if (g_str_has_prefix (control_url, "rtsp://"))
1676 stream->conninfo.location = g_strdup (control_url);
1681 if (g_strcmp0 (control_url, "*") == 0)
1684 base = get_aggregate_control (src);
1686 /* check if the base ends or control starts with / */
1687 has_slash = g_str_has_prefix (control_url, "/");
1688 has_slash = has_slash || g_str_has_suffix (base, "/");
1690 /* concatenate the two strings, insert / when not present */
1691 stream->conninfo.location =
1692 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1695 GST_DEBUG_OBJECT (src, " setup: %s",
1696 GST_STR_NULL (stream->conninfo.location));
1698 /* we keep track of all streams */
1699 src->streams = g_list_append (src->streams, stream);
1707 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1711 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1713 g_array_free (stream->ptmap, TRUE);
1715 g_free (stream->destination);
1716 g_free (stream->control_url);
1717 g_free (stream->conninfo.location);
1719 for (i = 0; i < 2; i++) {
1720 if (stream->udpsrc[i]) {
1721 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1722 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1723 gst_object_unref (stream->udpsrc[i]);
1725 if (stream->channelpad[i])
1726 gst_object_unref (stream->channelpad[i]);
1728 if (stream->udpsink[i]) {
1729 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1730 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1731 gst_object_unref (stream->udpsink[i]);
1734 if (stream->fakesrc) {
1735 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1736 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1737 gst_object_unref (stream->fakesrc);
1739 if (stream->srcpad) {
1740 gst_pad_set_active (stream->srcpad, FALSE);
1742 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1744 if (stream->srtpenc)
1745 gst_object_unref (stream->srtpenc);
1746 if (stream->srtpdec)
1747 gst_object_unref (stream->srtpdec);
1748 if (stream->srtcpparams)
1749 gst_caps_unref (stream->srtcpparams);
1751 gst_mikey_message_unref (stream->mikey);
1752 if (stream->rtcppad)
1753 gst_object_unref (stream->rtcppad);
1754 if (stream->session)
1755 g_object_unref (stream->session);
1756 if (stream->rtx_pt_map)
1757 gst_structure_free (stream->rtx_pt_map);
1762 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1766 GST_DEBUG_OBJECT (src, "cleanup");
1768 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1769 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1771 gst_rtspsrc_stream_free (src, stream);
1773 g_list_free (src->streams);
1774 src->streams = NULL;
1776 if (src->manager_sig_id) {
1777 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1778 src->manager_sig_id = 0;
1780 gst_element_set_state (src->manager, GST_STATE_NULL);
1781 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1782 src->manager = NULL;
1785 gst_structure_free (src->props);
1788 g_free (src->content_base);
1789 src->content_base = NULL;
1791 g_free (src->control);
1792 src->control = NULL;
1795 gst_rtsp_range_free (src->range);
1798 /* don't clear the SDP when it was used in the url */
1799 if (src->sdp && !src->from_sdp) {
1800 gst_sdp_message_free (src->sdp);
1804 src->need_segment = FALSE;
1806 if (src->provided_clock) {
1807 gst_object_unref (src->provided_clock);
1808 src->provided_clock = NULL;
1813 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1814 gint * rtpport, gint * rtcpport)
1817 GstStateChangeReturn ret;
1818 GstElement *udpsrc0, *udpsrc1;
1819 gint tmp_rtp, tmp_rtcp;
1823 src = stream->parent;
1829 /* Start at next port */
1830 tmp_rtp = src->next_port_num;
1832 if (stream->is_ipv6)
1833 host = "udp://[::0]";
1835 host = "udp://0.0.0.0";
1837 /* try to allocate 2 UDP ports, the RTP port should be an even
1838 * number and the RTCP port should be the next (uneven) port */
1841 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1842 tmp_rtp >= src->client_port_range.max)
1845 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1846 if (udpsrc0 == NULL)
1847 goto no_udp_protocol;
1848 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1850 if (src->udp_buffer_size != 0)
1851 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
1854 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
1855 if (ret == GST_STATE_CHANGE_FAILURE) {
1857 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
1860 if (++count > src->retry)
1863 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1864 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1865 gst_object_unref (udpsrc0);
1868 GST_DEBUG_OBJECT (src, "retry %d", count);
1871 goto no_udp_protocol;
1874 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
1875 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
1877 /* check if port is even */
1878 if ((tmp_rtp & 0x01) != 0) {
1879 /* port not even, close and allocate another */
1880 if (++count > src->retry)
1883 GST_DEBUG_OBJECT (src, "RTP port not even");
1885 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1886 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1887 gst_object_unref (udpsrc0);
1890 GST_DEBUG_OBJECT (src, "retry %d", count);
1895 /* allocate port+1 for RTCP now */
1896 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1897 if (udpsrc1 == NULL)
1898 goto no_udp_rtcp_protocol;
1901 tmp_rtcp = tmp_rtp + 1;
1902 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
1905 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
1907 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
1908 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
1909 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
1910 if (ret == GST_STATE_CHANGE_FAILURE) {
1911 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
1913 if (++count > src->retry)
1916 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1917 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1918 gst_object_unref (udpsrc0);
1921 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
1922 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1923 gst_object_unref (udpsrc1);
1927 GST_DEBUG_OBJECT (src, "retry %d", count);
1931 /* all fine, do port check */
1932 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
1933 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
1935 /* this should not happen... */
1936 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
1939 /* we keep these elements, we configure all in configure_transport when the
1940 * server told us to really use the UDP ports. */
1941 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
1942 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
1943 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
1944 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
1946 /* keep track of next available port number when we have a range
1948 if (src->next_port_num != 0)
1949 src->next_port_num = tmp_rtcp + 1;
1956 GST_DEBUG_OBJECT (src, "could not get UDP source");
1961 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
1965 no_udp_rtcp_protocol:
1967 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
1972 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
1973 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
1979 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1980 gst_object_unref (udpsrc0);
1983 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1984 gst_object_unref (udpsrc1);
1991 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
1996 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
1998 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1999 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2002 for (i = 0; i < 2; i++) {
2003 if (stream->udpsrc[i])
2004 gst_element_set_state (stream->udpsrc[i], state);
2010 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2017 event = gst_event_new_flush_start ();
2018 GST_DEBUG_OBJECT (src, "start flush");
2020 state = GST_STATE_PAUSED;
2022 event = gst_event_new_flush_stop (FALSE);
2023 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2026 state = GST_STATE_PLAYING;
2028 state = GST_STATE_PAUSED;
2030 gst_rtspsrc_push_event (src, event);
2031 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2032 gst_rtspsrc_set_state (src, state);
2035 static GstRTSPResult
2036 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2037 GstRTSPMessage * message, GTimeVal * timeout)
2042 ret = gst_rtsp_connection_send (conn, message, timeout);
2044 ret = GST_RTSP_ERROR;
2049 static GstRTSPResult
2050 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2051 GstRTSPMessage * message, GTimeVal * timeout)
2056 ret = gst_rtsp_connection_receive (conn, message, timeout);
2058 ret = GST_RTSP_ERROR;
2064 gst_rtspsrc_get_position (GstRTSPSrc * src)
2069 query = gst_query_new_position (GST_FORMAT_TIME);
2070 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2071 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2072 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2076 if (stream->srcpad) {
2077 if (gst_pad_query (stream->srcpad, query)) {
2078 gst_query_parse_position (query, &fmt, &pos);
2079 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2080 GST_TIME_ARGS (pos));
2081 src->last_pos = pos;
2091 gst_query_unref (query);
2095 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2100 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2102 gboolean flush, skip;
2105 GstSegment seeksegment = { 0, };
2109 GST_DEBUG_OBJECT (src, "doing seek with event");
2111 gst_event_parse_seek (event, &rate, &format, &flags,
2112 &cur_type, &cur, &stop_type, &stop);
2114 /* no negative rates yet */
2118 /* we need TIME format */
2119 if (format != src->segment.format)
2122 GST_DEBUG_OBJECT (src, "doing seek without event");
2124 cur_type = GST_SEEK_TYPE_SET;
2125 stop_type = GST_SEEK_TYPE_SET;
2128 /* get flush flag */
2129 flush = flags & GST_SEEK_FLAG_FLUSH;
2130 skip = flags & GST_SEEK_FLAG_SKIP;
2132 /* now we need to make sure the streaming thread is stopped. We do this by
2133 * either sending a FLUSH_START event downstream which will cause the
2134 * streaming thread to stop with a WRONG_STATE.
2135 * For a non-flushing seek we simply pause the task, which will happen as soon
2136 * as it completes one iteration (and thus might block when the sink is
2137 * blocking in preroll). */
2139 GST_DEBUG_OBJECT (src, "starting flush");
2140 gst_rtspsrc_flush (src, TRUE, FALSE);
2143 gst_task_pause (src->task);
2147 /* we should now be able to grab the streaming thread because we stopped it
2148 * with the above flush/pause code */
2149 GST_RTSP_STREAM_LOCK (src);
2151 GST_DEBUG_OBJECT (src, "stopped streaming");
2153 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2154 gst_rtspsrc_connection_flush (src, FALSE);
2156 /* copy segment, we need this because we still need the old
2157 * segment when we close the current segment. */
2158 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2160 /* configure the seek parameters in the seeksegment. We will then have the
2161 * right values in the segment to perform the seek */
2163 GST_DEBUG_OBJECT (src, "configuring seek");
2164 gst_segment_do_seek (&seeksegment, rate, format, flags,
2165 cur_type, cur, stop_type, stop, &update);
2168 /* figure out the last position we need to play. If it's configured (stop !=
2169 * -1), use that, else we play until the total duration of the file */
2170 if ((stop = seeksegment.stop) == -1)
2171 stop = seeksegment.duration;
2173 /* if we were playing, pause first */
2174 playing = (src->state == GST_RTSP_STATE_PLAYING);
2176 /* obtain current position in case seek fails */
2177 gst_rtspsrc_get_position (src);
2178 gst_rtspsrc_pause (src, FALSE);
2182 src->state = GST_RTSP_STATE_SEEKING;
2184 /* PLAY will add the range header now. */
2185 src->need_range = TRUE;
2187 /* prepare for streaming again */
2189 /* if we started flush, we stop now */
2190 GST_DEBUG_OBJECT (src, "stopping flush");
2191 gst_rtspsrc_flush (src, FALSE, playing);
2194 /* now we did the seek and can activate the new segment values */
2195 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2197 /* if we're doing a segment seek, post a SEGMENT_START message */
2198 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2199 gst_element_post_message (GST_ELEMENT_CAST (src),
2200 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2201 src->segment.format, src->segment.position));
2204 /* now create the newsegment */
2205 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2206 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2209 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2210 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2211 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2212 stream->discont = TRUE;
2215 /* and continue playing if needed */
2216 GST_OBJECT_LOCK (src);
2217 playing = (GST_STATE_PENDING (src) == GST_STATE_VOID_PENDING
2218 && GST_STATE (src) == GST_STATE_PLAYING)
2219 || (GST_STATE_PENDING (src) == GST_STATE_PLAYING);
2220 GST_OBJECT_UNLOCK (src);
2222 gst_rtspsrc_play (src, &seeksegment, FALSE);
2224 GST_RTSP_STREAM_UNLOCK (src);
2231 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2236 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2242 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2246 gboolean res = TRUE;
2249 src = GST_RTSPSRC_CAST (parent);
2251 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2252 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2254 switch (GST_EVENT_TYPE (event)) {
2255 case GST_EVENT_SEEK:
2256 res = gst_rtspsrc_perform_seek (src, event);
2260 case GST_EVENT_NAVIGATION:
2261 case GST_EVENT_LATENCY:
2269 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2270 res = gst_pad_send_event (target, event);
2271 gst_object_unref (target);
2273 gst_event_unref (event);
2276 gst_event_unref (event);
2282 /* this is the final event function we receive on the internal source pad when
2283 * we deal with TCP connections */
2285 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2290 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2292 switch (GST_EVENT_TYPE (event)) {
2293 case GST_EVENT_SEEK:
2295 case GST_EVENT_NAVIGATION:
2296 case GST_EVENT_LATENCY:
2298 gst_event_unref (event);
2305 /* this is the final query function we receive on the internal source pad when
2306 * we deal with TCP connections */
2308 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2312 gboolean res = TRUE;
2314 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2316 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2317 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2319 switch (GST_QUERY_TYPE (query)) {
2320 case GST_QUERY_POSITION:
2325 case GST_QUERY_DURATION:
2329 gst_query_parse_duration (query, &format, NULL);
2332 case GST_FORMAT_TIME:
2333 gst_query_set_duration (query, format, src->segment.duration);
2341 case GST_QUERY_LATENCY:
2343 /* we are live with a min latency of 0 and unlimited max latency, this
2344 * result will be updated by the session manager if there is any. */
2345 gst_query_set_latency (query, TRUE, 0, -1);
2355 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2357 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2361 gboolean res = FALSE;
2363 src = GST_RTSPSRC_CAST (parent);
2365 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2366 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2368 switch (GST_QUERY_TYPE (query)) {
2369 case GST_QUERY_DURATION:
2373 gst_query_parse_duration (query, &format, NULL);
2376 case GST_FORMAT_TIME:
2377 gst_query_set_duration (query, format, src->segment.duration);
2385 case GST_QUERY_SEEKING:
2389 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2390 if (format == GST_FORMAT_TIME) {
2392 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2394 /* seeking without duration is unlikely */
2395 seekable = seekable && src->seekable && src->segment.duration &&
2396 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2398 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0,
2399 src->segment.duration);
2408 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2410 gst_query_set_uri (query, uri);
2418 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2420 /* forward the query to the proxy target pad */
2422 res = gst_pad_query (target, query);
2423 gst_object_unref (target);
2432 /* callback for RTCP messages to be sent to the server when operating in TCP
2434 static GstFlowReturn
2435 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2438 GstRTSPStream *stream;
2439 GstFlowReturn res = GST_FLOW_OK;
2444 GstRTSPMessage message = { 0 };
2445 GstRTSPConnection *conn;
2447 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2448 src = stream->parent;
2450 gst_buffer_map (buffer, &map, GST_MAP_READ);
2454 gst_rtsp_message_init_data (&message, stream->channel[1]);
2456 /* lend the body data to the message */
2457 gst_rtsp_message_take_body (&message, data, size);
2459 if (stream->conninfo.connection)
2460 conn = stream->conninfo.connection;
2462 conn = src->conninfo.connection;
2464 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2465 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2466 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2468 /* and steal it away again because we will free it when unreffing the
2470 gst_rtsp_message_steal_body (&message, &data, &size);
2471 gst_rtsp_message_unset (&message);
2473 gst_buffer_unmap (buffer, &map);
2474 gst_buffer_unref (buffer);
2479 static GstPadProbeReturn
2480 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2482 GstRTSPSrc *src = user_data;
2484 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2485 GST_DEBUG_PAD_NAME (pad));
2487 /* activate the streams */
2488 GST_OBJECT_LOCK (src);
2489 if (!src->need_activate)
2492 src->need_activate = FALSE;
2493 GST_OBJECT_UNLOCK (src);
2495 gst_rtspsrc_activate_streams (src);
2497 return GST_PAD_PROBE_OK;
2501 GST_OBJECT_UNLOCK (src);
2502 return GST_PAD_PROBE_OK;
2507 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
2509 GstPad *gpad = GST_PAD_CAST (user_data);
2511 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
2512 gst_pad_store_sticky_event (gpad, *event);
2517 /* this callback is called when the session manager generated a new src pad with
2518 * payloaded RTP packets. We simply ghost the pad here. */
2520 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2523 GstPadTemplate *template;
2526 GstRTSPStream *stream;
2529 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2531 GST_RTSP_STATE_LOCK (src);
2533 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2534 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2535 goto unknown_stream;
2537 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2539 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2541 goto unknown_stream;
2544 stream->ssrc = ssrc;
2546 /* we'll add it later see below */
2547 stream->added = TRUE;
2549 /* check if we added all streams */
2551 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2552 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2554 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
2555 ostream, ostream->container, ostream->added, ostream->setup);
2557 /* if we find a stream for which we did a setup that is not added, we
2558 * need to wait some more */
2559 if (ostream->setup && !ostream->added) {
2564 GST_RTSP_STATE_UNLOCK (src);
2566 /* create a new pad we will use to stream to */
2567 template = gst_static_pad_template_get (&rtptemplate);
2568 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2569 gst_object_unref (template);
2572 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2573 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2574 gst_pad_set_active (stream->srcpad, TRUE);
2575 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
2576 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2579 GST_DEBUG_OBJECT (src, "We added all streams");
2580 /* when we get here, all stream are added and we can fire the no-more-pads
2582 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2590 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2591 GST_RTSP_STATE_UNLOCK (src);
2598 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
2602 len = stream->ptmap->len;
2603 for (i = 0; i < len; i++) {
2604 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2612 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2614 GstRTSPStream *stream;
2617 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2619 GST_RTSP_STATE_LOCK (src);
2620 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2622 goto unknown_stream;
2624 if ((caps = stream_get_caps_for_pt (stream, pt)))
2625 gst_caps_ref (caps);
2626 GST_RTSP_STATE_UNLOCK (src);
2632 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2633 GST_RTSP_STATE_UNLOCK (src);
2639 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2641 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2647 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2653 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2659 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2661 GstRTSPSrc *src = stream->parent;
2664 g_object_get (source, "ssrc", &ssrc, NULL);
2666 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2667 ssrc, stream->ssrc, stream->id);
2669 if (ssrc == stream->ssrc)
2670 gst_rtspsrc_do_stream_eos (src, stream);
2674 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2676 GstRTSPSrc *src = stream->parent;
2679 g_object_get (source, "ssrc", &ssrc, NULL);
2681 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2682 ssrc, stream->ssrc, stream->id);
2684 if (ssrc == stream->ssrc)
2685 gst_rtspsrc_do_stream_eos (src, stream);
2689 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2691 GstRTSPStream *stream;
2693 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2695 /* get stream for session */
2696 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2698 gst_rtspsrc_do_stream_eos (src, stream);
2703 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2705 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2710 set_manager_buffer_mode (GstRTSPSrc * src)
2712 GObjectClass *klass;
2714 if (src->manager == NULL)
2717 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2719 if (!g_object_class_find_property (klass, "buffer-mode"))
2722 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2723 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2728 GST_DEBUG_OBJECT (src,
2729 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
2731 if (src->provided_clock) {
2732 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
2734 if (clock == src->provided_clock) {
2735 GST_DEBUG_OBJECT (src, "selected synced");
2736 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
2739 gst_object_unref (clock);
2744 /* Otherwise fall-through and use another buffer mode */
2746 gst_object_unref (clock);
2749 GST_DEBUG_OBJECT (src, "auto buffering mode");
2750 if (src->use_buffering) {
2751 GST_DEBUG_OBJECT (src, "selected buffer");
2752 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
2754 GST_DEBUG_OBJECT (src, "selected slave");
2755 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2760 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2764 GstMIKEYMessage *msg = stream->mikey;
2766 GST_DEBUG ("request key SSRC %u", ssrc);
2768 caps = gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
2769 caps = gst_caps_make_writable (caps);
2771 /* parse crypto sessions and look for the SSRC rollover counter */
2772 msg = stream->mikey;
2773 for (i = 0; msg && i < gst_mikey_message_get_n_cs (msg); i++) {
2774 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
2776 if (ssrc == map->ssrc) {
2777 gst_caps_set_simple (caps, "roc", G_TYPE_UINT, map->roc, NULL);
2786 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2788 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
2789 if (stream->id != session)
2792 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
2793 stream->profile != GST_RTSP_PROFILE_SAVPF)
2796 if (stream->srtpdec == NULL) {
2799 name = g_strdup_printf ("srtpdec_%u", session);
2800 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
2803 g_signal_connect (stream->srtpdec, "request-key",
2804 (GCallback) request_key, stream);
2806 return gst_object_ref (stream->srtpdec);
2810 request_rtcp_encoder (GstElement * rtpbin, guint session,
2811 GstRTSPStream * stream)
2816 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
2817 if (stream->id != session)
2820 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
2821 stream->profile != GST_RTSP_PROFILE_SAVPF)
2824 if (stream->srtpenc == NULL) {
2827 name = g_strdup_printf ("srtpenc_%u", session);
2828 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
2831 /* get RTCP crypto parameters from caps */
2832 s = gst_caps_get_structure (stream->srtcpparams, 0);
2836 GType ciphertype, authtype;
2837 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
2839 ciphertype = g_type_from_name ("GstSrtpCipherType");
2840 authtype = g_type_from_name ("GstSrtpAuthType");
2841 g_value_init (&rtcp_cipher, ciphertype);
2842 g_value_init (&rtcp_auth, authtype);
2844 str = gst_structure_get_string (s, "srtcp-cipher");
2845 gst_value_deserialize (&rtcp_cipher, str);
2846 str = gst_structure_get_string (s, "srtcp-auth");
2847 gst_value_deserialize (&rtcp_auth, str);
2848 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
2850 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-cipher",
2852 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-auth",
2854 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
2856 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
2858 g_object_set (stream->srtpenc, "key", buf, NULL);
2860 g_value_unset (&rtcp_cipher);
2861 g_value_unset (&rtcp_auth);
2862 gst_buffer_unref (buf);
2865 name = g_strdup_printf ("rtcp_sink_%d", session);
2866 pad = gst_element_get_request_pad (stream->srtpenc, name);
2868 gst_object_unref (pad);
2870 return gst_object_ref (stream->srtpenc);
2874 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
2876 GstElement *rtx, *bin;
2879 GstRTSPStream *stream;
2881 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
2883 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
2887 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
2888 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
2889 bin = gst_bin_new (NULL);
2890 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
2891 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
2892 gst_bin_add (GST_BIN (bin), rtx);
2894 pad = gst_element_get_static_pad (rtx, "src");
2895 name = g_strdup_printf ("src_%u", sessid);
2896 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2898 gst_object_unref (pad);
2900 pad = gst_element_get_static_pad (rtx, "sink");
2901 name = g_strdup_printf ("sink_%u", sessid);
2902 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2904 gst_object_unref (pad);
2910 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
2914 gboolean do_retransmission = FALSE;
2916 if (transport->trans != GST_RTSP_TRANS_RTP)
2918 if (transport->profile != GST_RTSP_PROFILE_AVPF &&
2919 transport->profile != GST_RTSP_PROFILE_SAVPF)
2922 signal_id = g_signal_lookup ("request-aux-receiver",
2923 G_OBJECT_TYPE (src->manager));
2924 /* there's already something connected */
2925 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
2926 NULL, NULL, NULL) != 0) {
2927 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
2928 "\"request-aux-receiver\" signal is "
2929 "already used by the application");
2933 /* build the retransmission payload type map */
2934 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2935 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2936 gboolean do_retransmission_stream = FALSE;
2939 if (stream->rtx_pt_map)
2940 gst_structure_free (stream->rtx_pt_map);
2941 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
2943 for (i = 0; i < stream->ptmap->len; i++) {
2944 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2945 GstStructure *s = gst_caps_get_structure (item->caps, 0);
2946 const gchar *encoding;
2948 /* we only care about RTX streams */
2949 if ((encoding = gst_structure_get_string (s, "encoding-name"))
2950 && g_strcmp0 (encoding, "RTX") == 0) {
2951 const gchar *stream_pt_s;
2954 if (gst_structure_get_int (s, "payload", &rtx_pt)
2955 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
2958 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
2960 do_retransmission_stream = TRUE;
2966 if (do_retransmission_stream) {
2967 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
2968 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
2969 do_retransmission = TRUE;
2971 GST_DEBUG_OBJECT (src, "no retransmission payload map for stream "
2972 "id %i", stream->id);
2973 gst_structure_free (stream->rtx_pt_map);
2974 stream->rtx_pt_map = NULL;
2978 if (do_retransmission) {
2979 GST_DEBUG_OBJECT (src, "Enabling retransmissions");
2981 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
2983 /* enable RFC4588 retransmission handling by setting rtprtxreceive
2984 * as the "aux" element of rtpbin */
2985 g_signal_connect (src->manager, "request-aux-receiver",
2986 (GCallback) request_aux_receiver, src);
2988 GST_DEBUG_OBJECT (src,
2989 "Not enabling retransmissions as no stream had a retransmission payload map");
2993 /* try to get and configure a manager */
2995 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
2996 GstRTSPTransport * transport)
2998 const gchar *manager;
3000 GstStateChangeReturn ret;
3002 /* find a manager */
3003 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3007 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3009 /* configure the manager */
3010 if (src->manager == NULL) {
3011 GObjectClass *klass;
3013 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3015 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3019 goto use_no_manager;
3021 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3022 goto manager_failed;
3025 /* we manage this element */
3026 gst_element_set_locked_state (src->manager, TRUE);
3027 gst_bin_add (GST_BIN_CAST (src), src->manager);
3029 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3030 if (ret == GST_STATE_CHANGE_FAILURE)
3031 goto start_manager_failure;
3033 g_object_set (src->manager, "latency", src->latency, NULL);
3035 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3037 if (g_object_class_find_property (klass, "ntp-sync")) {
3038 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3041 if (g_object_class_find_property (klass, "rfc7273-sync")) {
3042 g_object_set (src->manager, "rfc7273-sync", src->rfc7273_sync, NULL);
3045 if (src->use_pipeline_clock) {
3046 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3047 g_object_set (src->manager, "use-pipeline-clock", TRUE, NULL);
3050 if (g_object_class_find_property (klass, "ntp-time-source")) {
3051 g_object_set (src->manager, "ntp-time-source", src->ntp_time_source,
3056 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3057 g_object_set (src->manager, "sdes", src->sdes, NULL);
3060 if (g_object_class_find_property (klass, "drop-on-latency")) {
3061 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3065 if (g_object_class_find_property (klass, "max-rtcp-rtp-time-diff")) {
3066 g_object_set (src->manager, "max-rtcp-rtp-time-diff",
3067 src->max_rtcp_rtp_time_diff, NULL);
3070 /* buffer mode pauses are handled by adding offsets to buffer times,
3071 * but some depayloaders may have a hard time syncing output times
3072 * with such input times, e.g. container ones, most notably ASF */
3073 /* TODO alternatives are having an event that indicates these shifts,
3074 * or having rtsp extensions provide suggestion on buffer mode */
3075 /* valid duration implies not likely live pipeline,
3076 * so slaving in jitterbuffer does not make much sense
3077 * (and might mess things up due to bursts) */
3078 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3079 src->segment.duration && stream->container) {
3080 src->use_buffering = TRUE;
3082 src->use_buffering = FALSE;
3085 set_manager_buffer_mode (src);
3087 /* connect to signals */
3088 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3090 src->manager_sig_id =
3091 g_signal_connect (src->manager, "pad-added",
3092 (GCallback) new_manager_pad, src);
3093 src->manager_ptmap_id =
3094 g_signal_connect (src->manager, "request-pt-map",
3095 (GCallback) request_pt_map, src);
3097 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3100 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3103 if (src->do_retransmission)
3104 add_retransmission (src, transport);
3106 g_signal_connect (src->manager, "request-rtp-decoder",
3107 (GCallback) request_rtp_decoder, stream);
3108 g_signal_connect (src->manager, "request-rtcp-decoder",
3109 (GCallback) request_rtp_decoder, stream);
3110 g_signal_connect (src->manager, "request-rtcp-encoder",
3111 (GCallback) request_rtcp_encoder, stream);
3113 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3114 * into a separate RTP session. */
3115 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3116 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3118 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3119 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3122 /* now configure the bandwidth in the manager */
3123 if (g_signal_lookup ("get-internal-session",
3124 G_OBJECT_TYPE (src->manager)) != 0) {
3125 GObject *rtpsession;
3127 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3130 GstRTPProfile rtp_profile;
3132 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3134 stream->session = rtpsession;
3136 if (stream->as_bandwidth != -1) {
3137 GST_INFO_OBJECT (src, "setting AS: %f",
3138 (gdouble) (stream->as_bandwidth * 1000));
3139 g_object_set (rtpsession, "bandwidth",
3140 (gdouble) (stream->as_bandwidth * 1000), NULL);
3142 if (stream->rr_bandwidth != -1) {
3143 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3144 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3147 if (stream->rs_bandwidth != -1) {
3148 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3149 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3153 switch (stream->profile) {
3154 case GST_RTSP_PROFILE_AVPF:
3155 rtp_profile = GST_RTP_PROFILE_AVPF;
3157 case GST_RTSP_PROFILE_SAVP:
3158 rtp_profile = GST_RTP_PROFILE_SAVP;
3160 case GST_RTSP_PROFILE_SAVPF:
3161 rtp_profile = GST_RTP_PROFILE_SAVPF;
3163 case GST_RTSP_PROFILE_AVP:
3165 rtp_profile = GST_RTP_PROFILE_AVP;
3169 g_object_set (rtpsession, "rtp-profile", rtp_profile, NULL);
3171 g_object_set (rtpsession, "probation", src->probation, NULL);
3173 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3175 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3177 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3179 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3181 g_signal_connect (rtpsession, "on-ssrc-active",
3182 (GCallback) on_ssrc_active, stream);
3193 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3198 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3201 start_manager_failure:
3203 GST_DEBUG_OBJECT (src, "could not start session manager");
3208 /* free the UDP sources allocated when negotiating a transport.
3209 * This function is called when the server negotiated to a transport where the
3210 * UDP sources are not needed anymore, such as TCP or multicast. */
3212 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3216 for (i = 0; i < 2; i++) {
3217 if (stream->udpsrc[i]) {
3218 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3219 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3220 gst_object_unref (stream->udpsrc[i]);
3221 stream->udpsrc[i] = NULL;
3226 /* for TCP, create pads to send and receive data to and from the manager and to
3227 * intercept various events and queries
3230 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3231 GstRTSPTransport * transport, GstPad ** outpad)
3234 GstPadTemplate *template;
3235 GstPad *pad0, *pad1;
3237 /* configure for interleaved delivery, nothing needs to be done
3238 * here, the loop function will call the chain functions of the
3239 * session manager. */
3240 stream->channel[0] = transport->interleaved.min;
3241 stream->channel[1] = transport->interleaved.max;
3242 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3243 stream->channel[0], stream->channel[1]);
3245 /* we can remove the allocated UDP ports now */
3246 gst_rtspsrc_stream_free_udp (stream);
3248 /* no session manager, send data to srcpad directly */
3249 if (!stream->channelpad[0]) {
3250 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3252 /* create a new pad we will use to stream to */
3253 name = g_strdup_printf ("stream_%u", stream->id);
3254 template = gst_static_pad_template_get (&rtptemplate);
3255 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3256 gst_object_unref (template);
3259 /* set caps and activate */
3260 gst_pad_use_fixed_caps (stream->channelpad[0]);
3261 gst_pad_set_active (stream->channelpad[0], TRUE);
3263 *outpad = gst_object_ref (stream->channelpad[0]);
3265 GST_DEBUG_OBJECT (src, "using manager source pad");
3267 template = gst_static_pad_template_get (&anysrctemplate);
3269 /* allocate pads for sending the channel data into the manager */
3270 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3271 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3272 gst_object_unref (stream->channelpad[0]);
3273 stream->channelpad[0] = pad0;
3274 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3275 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3276 gst_pad_set_element_private (pad0, src);
3277 gst_pad_set_active (pad0, TRUE);
3279 if (stream->channelpad[1]) {
3280 /* if we have a sinkpad for the other channel, create a pad and link to the
3282 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3283 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3284 gst_pad_link_full (pad1, stream->channelpad[1],
3285 GST_PAD_LINK_CHECK_NOTHING);
3286 gst_object_unref (stream->channelpad[1]);
3287 stream->channelpad[1] = pad1;
3288 gst_pad_set_active (pad1, TRUE);
3290 gst_object_unref (template);
3292 /* setup RTCP transport back to the server if we have to. */
3293 if (src->manager && src->do_rtcp) {
3296 template = gst_static_pad_template_get (&anysinktemplate);
3298 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3299 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3300 gst_pad_set_element_private (stream->rtcppad, stream);
3301 gst_pad_set_active (stream->rtcppad, TRUE);
3303 /* get session RTCP pad */
3304 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3305 pad = gst_element_get_request_pad (src->manager, name);
3310 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3311 gst_object_unref (pad);
3314 gst_object_unref (template);
3320 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3321 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3322 gint * max, guint * ttl)
3324 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3326 if (!(*destination = transport->destination))
3327 *destination = stream->destination;
3330 /* transport first */
3331 *min = transport->port.min;
3332 *max = transport->port.max;
3333 if (*min == -1 && *max == -1) {
3334 /* then try from SDP */
3335 if (stream->port != 0) {
3336 *min = stream->port;
3337 *max = stream->port + 1;
3343 if (!(*ttl = transport->ttl))
3348 /* first take the source, then the endpoint to figure out where to send
3350 if (!(*destination = transport->source)) {
3351 if (src->conninfo.connection)
3352 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3353 else if (stream->conninfo.connection)
3355 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3359 /* for unicast we only expect the ports here */
3360 *min = transport->server_port.min;
3361 *max = transport->server_port.max;
3366 /* For multicast create UDP sources and join the multicast group. */
3368 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3369 GstRTSPTransport * transport, GstPad ** outpad)
3372 const gchar *destination;
3375 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3377 /* we can remove the allocated UDP ports now */
3378 gst_rtspsrc_stream_free_udp (stream);
3380 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3383 /* we need a destination now */
3384 if (destination == NULL)
3385 goto no_destination;
3387 /* we really need ports now or we won't be able to receive anything at all */
3388 if (min == -1 && max == -1)
3391 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3392 destination, min, max);
3394 /* creating UDP source for RTP */
3396 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3398 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3400 if (stream->udpsrc[0] == NULL)
3403 /* take ownership */
3404 gst_object_ref_sink (stream->udpsrc[0]);
3406 if (src->udp_buffer_size != 0)
3407 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3408 src->udp_buffer_size, NULL);
3410 if (src->multi_iface != NULL)
3411 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3412 src->multi_iface, NULL);
3415 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3416 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3419 /* creating another UDP source for RTCP */
3423 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3425 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3427 if (stream->udpsrc[1] == NULL)
3430 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3431 stream->profile == GST_RTSP_PROFILE_SAVPF)
3432 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3434 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3435 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3436 gst_caps_unref (caps);
3438 /* take ownership */
3439 gst_object_ref_sink (stream->udpsrc[1]);
3441 if (src->multi_iface != NULL)
3442 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3443 src->multi_iface, NULL);
3445 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
3452 GST_DEBUG_OBJECT (src, "no UDP source element found");
3457 GST_DEBUG_OBJECT (src, "no destination found");
3462 GST_DEBUG_OBJECT (src, "no ports found");
3467 /* configure the remainder of the UDP ports */
3469 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3470 GstRTSPTransport * transport, GstPad ** outpad)
3472 /* we manage the UDP elements now. For unicast, the UDP sources where
3473 * allocated in the stream when we suggested a transport. */
3474 if (stream->udpsrc[0]) {
3477 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3478 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3480 GST_DEBUG_OBJECT (src, "setting up UDP source");
3482 /* configure a timeout on the UDP port. When the timeout message is
3483 * posted, we assume UDP transport is not possible. We reconnect using TCP
3485 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3486 src->udp_timeout * 1000, NULL);
3488 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
3489 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3491 /* get output pad of the UDP source. */
3492 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3494 /* save it so we can unblock */
3495 stream->blockedpad = *outpad;
3497 /* configure pad block on the pad. As soon as there is dataflow on the
3498 * UDP source, we know that UDP is not blocked by a firewall and we can
3499 * configure all the streams to let the application autoplug decoders. */
3501 gst_pad_add_probe (stream->blockedpad,
3502 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3503 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
3505 if (stream->channelpad[0]) {
3506 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3507 /* configure for UDP delivery, we need to connect the UDP pads to
3508 * the session plugin. */
3509 gst_pad_link_full (*outpad, stream->channelpad[0],
3510 GST_PAD_LINK_CHECK_NOTHING);
3511 gst_object_unref (*outpad);
3513 /* we connected to pad-added signal to get pads from the manager */
3515 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3520 if (stream->udpsrc[1]) {
3523 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3524 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3526 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3527 stream->profile == GST_RTSP_PROFILE_SAVPF)
3528 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3530 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3531 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3532 gst_caps_unref (caps);
3534 if (stream->channelpad[1]) {
3537 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3539 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3540 gst_pad_link_full (pad, stream->channelpad[1],
3541 GST_PAD_LINK_CHECK_NOTHING);
3542 gst_object_unref (pad);
3544 /* leave unlinked */
3550 /* configure the UDP sink back to the server for status reports */
3552 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3553 GstRTSPStream * stream, GstRTSPTransport * transport)
3556 gint rtp_port, rtcp_port;
3557 gboolean do_rtp, do_rtcp;
3558 const gchar *destination;
3563 /* get transport info */
3564 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3565 &rtp_port, &rtcp_port, &ttl);
3567 /* see what we need to do */
3568 do_rtp = (rtp_port != -1);
3569 /* it's possible that the server does not want us to send RTCP in which case
3571 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3573 /* we need a destination when we have RTP or RTCP ports */
3574 if (destination == NULL && (do_rtp || do_rtcp))
3575 goto no_destination;
3577 /* try to construct the fakesrc to the RTP port of the server to open up any
3580 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3583 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3584 stream->udpsink[0] =
3585 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3587 if (stream->udpsink[0] == NULL)
3588 goto no_sink_element;
3590 /* don't join multicast group, we will have the source socket do that */
3591 /* no sync or async state changes needed */
3592 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3593 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3595 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3597 if (stream->udpsrc[0]) {
3598 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3599 * so that NAT firewalls will open a hole for us */
3600 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3604 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3605 /* configure socket and make sure udpsink does not close it when shutting
3606 * down, it belongs to udpsrc after all. */
3607 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3608 "close-socket", FALSE, NULL);
3609 g_object_unref (socket);
3612 /* the source for the dummy packets to open up NAT */
3613 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3614 if (stream->fakesrc == NULL)
3615 goto no_fakesrc_element;
3617 /* random data in 5 buffers, a size of 200 bytes should be fine */
3618 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3619 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3621 /* we don't want to consider this a sink */
3622 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
3624 /* keep everything locked */
3625 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3626 gst_element_set_locked_state (stream->fakesrc, TRUE);
3628 gst_object_ref (stream->udpsink[0]);
3629 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3630 gst_object_ref (stream->fakesrc);
3631 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3633 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3634 "sink", GST_PAD_LINK_CHECK_NOTHING);
3637 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3640 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3641 stream->udpsink[1] =
3642 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3644 if (stream->udpsink[1] == NULL)
3645 goto no_sink_element;
3647 /* don't join multicast group, we will have the source socket do that */
3648 /* no sync or async state changes needed */
3649 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3650 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3652 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3654 if (stream->udpsrc[1]) {
3655 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3656 * because some servers check the port number of where it sends RTCP to identify
3657 * the RTCP packets it receives */
3658 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3662 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3663 /* configure socket and make sure udpsink does not close it when shutting
3664 * down, it belongs to udpsrc after all. */
3665 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3666 "close-socket", FALSE, NULL);
3667 g_object_unref (socket);
3670 /* we don't want to consider this a sink */
3671 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3673 /* we keep this playing always */
3674 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3675 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3677 gst_object_ref (stream->udpsink[1]);
3678 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3680 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3682 /* get session RTCP pad */
3683 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3684 pad = gst_element_get_request_pad (src->manager, name);
3689 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3690 gst_object_unref (pad);
3699 GST_ERROR_OBJECT (src, "no destination address specified");
3704 GST_ERROR_OBJECT (src, "no UDP sink element found");
3709 GST_ERROR_OBJECT (src, "no fakesrc element found");
3714 GST_ERROR_OBJECT (src, "failed to create socket");
3719 /* sets up all elements needed for streaming over the specified transport.
3720 * Does not yet expose the element pads, this will be done when there is actuall
3721 * dataflow detected, which might never happen when UDP is blocked in a
3722 * firewall, for example.
3725 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3726 GstRTSPTransport * transport)
3729 GstPad *outpad = NULL;
3730 GstPadTemplate *template;
3732 const gchar *media_type;
3735 src = stream->parent;
3737 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3739 /* get the proper media type for this stream now */
3740 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
3741 goto unknown_transport;
3743 goto unknown_transport;
3745 /* configure the final media type */
3746 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
3748 len = stream->ptmap->len;
3749 for (i = 0; i < len; i++) {
3751 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3753 if (item->caps == NULL)
3756 s = gst_caps_get_structure (item->caps, 0);
3757 gst_structure_set_name (s, media_type);
3758 /* set ssrc if known */
3759 if (transport->ssrc)
3760 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
3763 /* try to get and configure a manager, channelpad[0-1] will be configured with
3764 * the pads for the manager, or NULL when no manager is needed. */
3765 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3768 switch (transport->lower_transport) {
3769 case GST_RTSP_LOWER_TRANS_TCP:
3770 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3771 goto transport_failed;
3773 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3774 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3775 goto transport_failed;
3776 /* fallthrough, the rest is the same for UDP and MCAST */
3777 case GST_RTSP_LOWER_TRANS_UDP:
3778 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3779 goto transport_failed;
3780 /* configure udpsinks back to the server for RTCP messages and for the
3781 * dummy RTP messages to open NAT. */
3782 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3783 goto transport_failed;
3786 goto unknown_transport;
3790 GST_DEBUG_OBJECT (src, "creating ghostpad");
3792 gst_pad_use_fixed_caps (outpad);
3794 /* create ghostpad, don't add just yet, this will be done when we activate
3796 name = g_strdup_printf ("stream_%u", stream->id);
3797 template = gst_static_pad_template_get (&rtptemplate);
3798 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3799 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3800 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3801 gst_object_unref (template);
3804 gst_object_unref (outpad);
3806 /* mark pad as ok */
3807 stream->last_ret = GST_FLOW_OK;
3814 GST_DEBUG_OBJECT (src, "failed to configure transport");
3819 GST_DEBUG_OBJECT (src, "unknown transport");
3824 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3829 /* send a couple of dummy random packets on the receiver RTP port to the server,
3830 * this should make a firewall think we initiated the data transfer and
3831 * hopefully allow packets to go from the sender port to our RTP receiver port */
3833 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3837 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3840 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3841 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3843 if (stream->fakesrc && stream->udpsink[0]) {
3844 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3845 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3846 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3847 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3848 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3854 /* Adds the source pads of all configured streams to the element.
3855 * This code is performed when we detected dataflow.
3857 * We detect dataflow from either the _loop function or with pad probes on the
3861 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3865 GST_DEBUG_OBJECT (src, "activating streams");
3867 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3868 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3870 if (stream->udpsrc[0]) {
3871 /* remove timeout, we are streaming now and timeouts will be handled by
3872 * the session manager and jitter buffer */
3873 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3875 if (stream->srcpad) {
3876 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3877 gst_pad_set_active (stream->srcpad, TRUE);
3879 /* if we don't have a session manager, set the caps now. If we have a
3880 * session, we will get a notification of the pad and the caps. */
3881 if (!src->manager) {
3884 caps = stream_get_caps_for_pt (stream, stream->default_pt);
3885 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3886 gst_pad_set_caps (stream->srcpad, caps);
3889 if (!stream->added) {
3890 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3891 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3892 stream->added = TRUE;
3897 /* unblock all pads */
3898 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3899 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3901 if (stream->blockid) {
3902 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3903 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3904 stream->blockid = 0;
3912 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
3913 gboolean reset_manager)
3916 guint64 start, stop;
3917 gdouble play_speed, play_scale;
3919 GST_DEBUG_OBJECT (src, "configuring stream caps");
3921 start = segment->position;
3922 stop = segment->duration;
3923 play_speed = segment->rate;
3924 play_scale = segment->applied_rate;
3926 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3927 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3933 len = stream->ptmap->len;
3934 for (j = 0; j < len; j++) {
3936 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
3938 if (item->caps == NULL)
3941 caps = gst_caps_make_writable (item->caps);
3943 if (stream->timebase != -1)
3944 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3945 (guint) stream->timebase, NULL);
3946 if (stream->seqbase != -1)
3947 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3948 (guint) stream->seqbase, NULL);
3949 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3951 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3952 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3953 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3956 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
3959 if (item->pt == stream->default_pt) {
3960 if (stream->udpsrc[0])
3961 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3962 stream->need_caps = TRUE;
3966 if (reset_manager && src->manager) {
3967 GST_DEBUG_OBJECT (src, "clear session");
3968 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3972 static GstFlowReturn
3973 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3978 /* store the value */
3979 stream->last_ret = ret;
3981 /* if it's success we can return the value right away */
3982 if (ret == GST_FLOW_OK)
3985 /* any other error that is not-linked can be returned right
3987 if (ret != GST_FLOW_NOT_LINKED)
3990 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3991 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3992 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3994 ret = ostream->last_ret;
3995 /* some other return value (must be SUCCESS but we can return
3996 * other values as well) */
3997 if (ret != GST_FLOW_NOT_LINKED)
4000 /* if we get here, all other pads were unlinked and we return
4001 * NOT_LINKED then */
4007 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4010 gboolean res = TRUE;
4012 /* only streams that have a connection to the outside world */
4016 if (stream->udpsrc[0]) {
4017 gst_event_ref (event);
4018 res = gst_element_send_event (stream->udpsrc[0], event);
4019 } else if (stream->channelpad[0]) {
4020 gst_event_ref (event);
4021 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4022 res = gst_pad_push_event (stream->channelpad[0], event);
4024 res = gst_pad_send_event (stream->channelpad[0], event);
4027 if (stream->udpsrc[1]) {
4028 gst_event_ref (event);
4029 res &= gst_element_send_event (stream->udpsrc[1], event);
4030 } else if (stream->channelpad[1]) {
4031 gst_event_ref (event);
4032 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4033 res &= gst_pad_push_event (stream->channelpad[1], event);
4035 res &= gst_pad_send_event (stream->channelpad[1], event);
4039 gst_event_unref (event);
4045 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4048 gboolean res = TRUE;
4050 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4051 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4053 gst_event_ref (event);
4054 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4056 gst_event_unref (event);
4061 static GstRTSPResult
4062 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4066 GstRTSPMessage response;
4067 gboolean retry = FALSE;
4068 memset (&response, 0, sizeof (response));
4069 gst_rtsp_message_init (&response);
4071 if (info->connection == NULL) {
4072 if (info->url == NULL) {
4073 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4074 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4077 /* create connection */
4078 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4079 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4080 goto could_not_create;
4083 gst_rtspsrc_setup_auth (src, &response);
4086 g_free (info->url_str);
4087 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4089 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4091 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4092 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4093 src->tls_validation_flags))
4094 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4096 if (src->tls_database)
4097 gst_rtsp_connection_set_tls_database (info->connection,
4100 if (src->tls_interaction)
4101 gst_rtsp_connection_set_tls_interaction (info->connection,
4102 src->tls_interaction);
4105 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4106 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4108 if (src->proxy_host) {
4109 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4111 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4116 if (!info->connected) {
4119 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4120 ("Connecting to %s", info->location));
4121 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4122 res = gst_rtsp_connection_connect_with_response (info->connection,
4123 src->ptcp_timeout, &response);
4125 if (response.type == GST_RTSP_MESSAGE_HTTP_RESPONSE &&
4126 response.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4127 gst_rtsp_conninfo_close (src, info, TRUE);
4131 retry = FALSE; // we should not retry more than once
4136 if (res == GST_RTSP_OK)
4137 info->connected = TRUE;
4139 goto could_not_connect;
4141 } while (!info->connected && retry);
4142 gst_rtsp_message_unset (&response);
4148 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4149 gst_rtsp_message_unset (&response);
4154 gchar *str = gst_rtsp_strresult (res);
4155 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4157 gst_rtsp_message_unset (&response);
4162 gchar *str = gst_rtsp_strresult (res);
4163 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4165 gst_rtsp_message_unset (&response);
4170 static GstRTSPResult
4171 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4174 GST_RTSP_STATE_LOCK (src);
4175 if (info->connected) {
4176 GST_DEBUG_OBJECT (src, "closing connection...");
4177 gst_rtsp_connection_close (info->connection);
4178 info->connected = FALSE;
4180 if (free && info->connection) {
4181 /* free connection */
4182 GST_DEBUG_OBJECT (src, "freeing connection...");
4183 gst_rtsp_connection_free (info->connection);
4184 info->connection = NULL;
4186 GST_RTSP_STATE_UNLOCK (src);
4190 static GstRTSPResult
4191 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4196 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4197 gst_rtsp_conninfo_close (src, info, FALSE);
4198 res = gst_rtsp_conninfo_connect (src, info, async);
4204 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4208 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4209 GST_RTSP_STATE_LOCK (src);
4210 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4211 GST_DEBUG_OBJECT (src, "connection flush");
4212 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4213 src->conninfo.flushing = flush;
4215 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4216 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4217 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4218 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4219 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4220 stream->conninfo.flushing = flush;
4223 GST_RTSP_STATE_UNLOCK (src);
4226 static GstRTSPResult
4227 gst_rtspsrc_init_request (GstRTSPSrc * src, GstRTSPMessage * msg,
4228 GstRTSPMethod method, const gchar * uri)
4232 res = gst_rtsp_message_init_request (msg, method, uri);
4236 /* set user-agent */
4237 if (src->user_agent)
4238 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, src->user_agent);
4243 /* FIXME, handle server request, reply with OK, for now */
4244 static GstRTSPResult
4245 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
4246 GstRTSPMessage * request)
4248 GstRTSPMessage response = { 0 };
4251 GST_DEBUG_OBJECT (src, "got server request message");
4254 gst_rtsp_message_dump (request);
4256 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4258 if (res == GST_RTSP_ENOTIMPL) {
4259 /* default implementation, send OK */
4260 GST_DEBUG_OBJECT (src, "prepare OK reply");
4262 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4267 /* let app parse and reply */
4268 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4269 0, request, &response);
4272 gst_rtsp_message_dump (&response);
4274 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
4278 gst_rtsp_message_unset (&response);
4279 } else if (res == GST_RTSP_EEOF)
4287 gst_rtsp_message_unset (&response);
4292 /* send server keep-alive */
4293 static GstRTSPResult
4294 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4296 GstRTSPMessage request = { 0 };
4298 GstRTSPMethod method;
4299 const gchar *control;
4301 if (src->do_rtsp_keep_alive == FALSE) {
4302 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4303 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4307 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4309 /* find a method to use for keep-alive */
4310 if (src->methods & GST_RTSP_GET_PARAMETER)
4311 method = GST_RTSP_GET_PARAMETER;
4313 method = GST_RTSP_OPTIONS;
4315 control = get_aggregate_control (src);
4316 if (control == NULL)
4319 res = gst_rtspsrc_init_request (src, &request, method, control);
4324 gst_rtsp_message_dump (&request);
4327 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
4332 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4333 gst_rtsp_message_unset (&request);
4340 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4345 gchar *str = gst_rtsp_strresult (res);
4347 gst_rtsp_message_unset (&request);
4348 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4349 ("Could not send keep-alive. (%s)", str));
4355 static GstFlowReturn
4356 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4358 GstFlowReturn ret = GST_FLOW_OK;
4360 GstRTSPStream *stream;
4361 GstPad *outpad = NULL;
4367 channel = message->type_data.data.channel;
4369 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4371 goto unknown_stream;
4373 if (channel == stream->channel[0]) {
4374 outpad = stream->channelpad[0];
4376 } else if (channel == stream->channel[1]) {
4377 outpad = stream->channelpad[1];
4383 /* take a look at the body to figure out what we have */
4384 gst_rtsp_message_get_body (message, &data, &size);
4386 goto invalid_length;
4388 /* channels are not correct on some servers, do extra check */
4389 if (data[1] >= 200 && data[1] <= 204) {
4390 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4391 outpad = stream->channelpad[1];
4395 /* we have no clue what this is, just ignore then. */
4397 goto unknown_stream;
4399 /* take the message body for further processing */
4400 gst_rtsp_message_steal_body (message, &data, &size);
4402 /* strip the trailing \0 */
4405 buf = gst_buffer_new ();
4406 gst_buffer_append_memory (buf,
4407 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4409 /* don't need message anymore */
4410 gst_rtsp_message_unset (message);
4412 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4415 if (src->need_activate) {
4421 guint group_id = gst_util_group_id_next ();
4423 /* generate an SHA256 sum of the URI */
4424 cs = g_checksum_new (G_CHECKSUM_SHA256);
4425 uri = src->conninfo.location;
4426 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4428 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4429 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4433 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4434 event = gst_event_new_stream_start (stream_id);
4435 gst_event_set_group_id (event, group_id);
4438 gst_rtspsrc_stream_push_event (src, ostream, event);
4440 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
4441 /* only streams that have a connection to the outside world */
4442 if (ostream->setup) {
4443 if (ostream->udpsrc[0]) {
4444 gst_element_send_event (ostream->udpsrc[0],
4445 gst_event_new_caps (caps));
4446 } else if (ostream->channelpad[0]) {
4447 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
4448 gst_pad_push_event (ostream->channelpad[0],
4449 gst_event_new_caps (caps));
4451 gst_pad_send_event (ostream->channelpad[0],
4452 gst_event_new_caps (caps));
4454 ostream->need_caps = FALSE;
4456 if (ostream->profile == GST_RTSP_PROFILE_SAVP ||
4457 ostream->profile == GST_RTSP_PROFILE_SAVPF)
4458 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4460 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4462 if (ostream->udpsrc[1]) {
4463 gst_element_send_event (ostream->udpsrc[1],
4464 gst_event_new_caps (caps));
4465 } else if (ostream->channelpad[1]) {
4466 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
4467 gst_pad_push_event (ostream->channelpad[1],
4468 gst_event_new_caps (caps));
4470 gst_pad_send_event (ostream->channelpad[1],
4471 gst_event_new_caps (caps));
4474 gst_caps_unref (caps);
4478 g_checksum_free (cs);
4480 gst_rtspsrc_activate_streams (src);
4481 src->need_activate = FALSE;
4482 src->need_segment = TRUE;
4485 if (src->base_time == -1) {
4486 /* Take current running_time. This timestamp will be put on
4487 * the first buffer of each stream because we are a live source and so we
4488 * timestamp with the running_time. When we are dealing with TCP, we also
4489 * only timestamp the first buffer (using the DISCONT flag) because a server
4490 * typically bursts data, for which we don't want to compensate by speeding
4491 * up the media. The other timestamps will be interpollated from this one
4492 * using the RTP timestamps. */
4493 GST_OBJECT_LOCK (src);
4494 if (GST_ELEMENT_CLOCK (src)) {
4496 GstClockTime base_time;
4498 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4499 base_time = GST_ELEMENT_CAST (src)->base_time;
4501 src->base_time = now - base_time;
4503 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4504 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4506 GST_OBJECT_UNLOCK (src);
4509 /* If needed send a new segment, don't forget we are live and buffer are
4510 * timestamped with running time */
4511 if (src->need_segment) {
4513 src->need_segment = FALSE;
4514 gst_segment_init (&segment, GST_FORMAT_TIME);
4515 gst_rtspsrc_push_event (src, gst_event_new_segment (&segment));
4518 if (stream->need_caps) {
4521 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt))) {
4522 /* only streams that have a connection to the outside world */
4523 if (stream->setup) {
4524 /* Only need to update the TCP caps here, UDP is already handled */
4525 if (stream->channelpad[0]) {
4526 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4527 gst_pad_push_event (stream->channelpad[0],
4528 gst_event_new_caps (caps));
4530 gst_pad_send_event (stream->channelpad[0],
4531 gst_event_new_caps (caps));
4533 stream->need_caps = FALSE;
4537 stream->need_caps = FALSE;
4540 if (stream->discont && !is_rtcp) {
4541 /* mark first RTP buffer as discont */
4542 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4543 stream->discont = FALSE;
4544 /* first buffer gets the timestamp, other buffers are not timestamped and
4545 * their presentation time will be interpollated from the rtp timestamps. */
4546 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4547 GST_TIME_ARGS (src->base_time));
4549 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4552 /* chain to the peer pad */
4553 if (GST_PAD_IS_SINK (outpad))
4554 ret = gst_pad_chain (outpad, buf);
4556 ret = gst_pad_push (outpad, buf);
4559 /* combine all stream flows for the data transport */
4560 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4567 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4568 gst_rtsp_message_unset (message);
4573 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4574 ("Short message received, ignoring."));
4575 gst_rtsp_message_unset (message);
4580 static GstFlowReturn
4581 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4583 GstRTSPMessage message = { 0 };
4585 GstFlowReturn ret = GST_FLOW_OK;
4586 GTimeVal tv_timeout;
4589 /* get the next timeout interval */
4590 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4592 /* see if the timeout period expired */
4593 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4594 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4595 /* send keep-alive, only act on interrupt, a warning will be posted for
4597 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4599 /* get new timeout */
4600 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4603 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4604 tv_timeout.tv_sec, tv_timeout.tv_usec);
4606 /* protect the connection with the connection lock so that we can see when
4607 * we are finished doing server communication */
4609 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4610 &message, src->ptcp_timeout);
4614 GST_DEBUG_OBJECT (src, "we received a server message");
4616 case GST_RTSP_EINTR:
4617 /* we got interrupted this means we need to stop */
4619 case GST_RTSP_ETIMEOUT:
4620 /* no reply, send keep alive */
4621 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4622 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4626 /* go EOS when the server closed the connection */
4632 switch (message.type) {
4633 case GST_RTSP_MESSAGE_REQUEST:
4634 /* server sends us a request message, handle it */
4636 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4638 if (res == GST_RTSP_EEOF)
4641 goto handle_request_failed;
4643 case GST_RTSP_MESSAGE_RESPONSE:
4644 /* we ignore response messages */
4645 GST_DEBUG_OBJECT (src, "ignoring response message");
4647 gst_rtsp_message_dump (&message);
4649 case GST_RTSP_MESSAGE_DATA:
4650 GST_DEBUG_OBJECT (src, "got data message");
4651 ret = gst_rtspsrc_handle_data (src, &message);
4652 if (ret != GST_FLOW_OK)
4653 goto handle_data_failed;
4656 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4661 g_assert_not_reached ();
4666 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4667 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4668 ("The server closed the connection."));
4669 src->conninfo.connected = FALSE;
4670 gst_rtsp_message_unset (&message);
4671 return GST_FLOW_EOS;
4675 gst_rtsp_message_unset (&message);
4676 GST_DEBUG_OBJECT (src, "got interrupted");
4677 return GST_FLOW_FLUSHING;
4681 gchar *str = gst_rtsp_strresult (res);
4683 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4684 ("Could not receive message. (%s)", str));
4687 gst_rtsp_message_unset (&message);
4688 return GST_FLOW_ERROR;
4690 handle_request_failed:
4692 gchar *str = gst_rtsp_strresult (res);
4694 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4695 ("Could not handle server message. (%s)", str));
4697 gst_rtsp_message_unset (&message);
4698 return GST_FLOW_ERROR;
4702 GST_DEBUG_OBJECT (src, "could no handle data message");
4707 static GstFlowReturn
4708 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4711 GstRTSPMessage message = { 0 };
4715 GTimeVal tv_timeout;
4717 /* get the next timeout interval */
4718 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4720 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4721 (gint) tv_timeout.tv_sec);
4723 gst_rtsp_message_unset (&message);
4725 /* we should continue reading the TCP socket because the server might
4726 * send us requests. When the session timeout expires, we need to send a
4727 * keep-alive request to keep the session open. */
4728 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4729 &message, &tv_timeout);
4733 GST_DEBUG_OBJECT (src, "we received a server message");
4735 case GST_RTSP_EINTR:
4736 /* we got interrupted, see what we have to do */
4738 case GST_RTSP_ETIMEOUT:
4739 /* send keep-alive, ignore the result, a warning will be posted. */
4740 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4741 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4745 /* server closed the connection. not very fatal for UDP, reconnect and
4746 * see what happens. */
4747 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4748 ("The server closed the connection."));
4749 if (src->udp_reconnect) {
4751 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4758 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4760 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4761 ("Unhandled return value %d.", res));
4765 switch (message.type) {
4766 case GST_RTSP_MESSAGE_REQUEST:
4767 /* server sends us a request message, handle it */
4769 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4771 if (res == GST_RTSP_EEOF)
4774 goto handle_request_failed;
4776 case GST_RTSP_MESSAGE_RESPONSE:
4777 /* we ignore response and data messages */
4778 GST_DEBUG_OBJECT (src, "ignoring response message");
4780 gst_rtsp_message_dump (&message);
4781 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4782 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4783 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4784 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4785 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4792 case GST_RTSP_MESSAGE_DATA:
4793 /* we ignore response and data messages */
4794 GST_DEBUG_OBJECT (src, "ignoring data message");
4797 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4802 g_assert_not_reached ();
4804 /* we get here when the connection got interrupted */
4807 gst_rtsp_message_unset (&message);
4808 GST_DEBUG_OBJECT (src, "got interrupted");
4809 return GST_FLOW_FLUSHING;
4813 gchar *str = gst_rtsp_strresult (res);
4816 src->conninfo.connected = FALSE;
4817 if (res != GST_RTSP_EINTR) {
4818 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4819 ("Could not connect to server. (%s)", str));
4821 ret = GST_FLOW_ERROR;
4823 ret = GST_FLOW_FLUSHING;
4829 gchar *str = gst_rtsp_strresult (res);
4831 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4832 ("Could not receive message. (%s)", str));
4834 return GST_FLOW_ERROR;
4836 handle_request_failed:
4838 gchar *str = gst_rtsp_strresult (res);
4841 gst_rtsp_message_unset (&message);
4842 if (res != GST_RTSP_EINTR) {
4843 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4844 ("Could not handle server message. (%s)", str));
4846 ret = GST_FLOW_ERROR;
4848 ret = GST_FLOW_FLUSHING;
4854 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4855 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4856 ("The server closed the connection."));
4857 src->conninfo.connected = FALSE;
4858 gst_rtsp_message_unset (&message);
4859 return GST_FLOW_EOS;
4863 static GstRTSPResult
4864 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4866 GstRTSPResult res = GST_RTSP_OK;
4869 GST_DEBUG_OBJECT (src, "doing reconnect");
4871 GST_OBJECT_LOCK (src);
4872 /* only restart when the pads were not yet activated, else we were
4873 * streaming over UDP */
4874 restart = src->need_activate;
4875 GST_OBJECT_UNLOCK (src);
4877 /* no need to restart, we're done */
4881 /* unless redirect, try tcp */
4882 if (!src->need_redirect)
4883 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
4885 /* close and cleanup our state */
4886 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
4889 /* unless redirect, see if we have TCP left to try. Also don't
4890 * try TCP when we were configured with an SDP. */
4891 if (!src->need_redirect && (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP)
4895 if (!src->need_redirect) {
4896 /* We post a warning message now to inform the user
4897 * that nothing happened. It's most likely a firewall thing. */
4898 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4899 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4900 "firewall is blocking it. Retrying using a tcp connection.",
4901 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
4904 /* unless redirect, open new connection using tcp */
4905 if (gst_rtspsrc_open (src, async) < 0)
4908 /* start playback */
4909 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
4918 src->cur_protocols = 0;
4919 /* no transport possible, post an error and stop */
4920 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4921 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4922 "firewall is blocking it. No other protocols to try.",
4923 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
4924 return GST_RTSP_ERROR;
4928 GST_DEBUG_OBJECT (src, "open failed");
4933 GST_DEBUG_OBJECT (src, "play failed");
4939 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
4943 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
4946 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
4949 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
4952 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
4960 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
4964 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
4967 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
4970 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
4973 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4981 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4985 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4988 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4991 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4994 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
5002 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
5006 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
5009 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
5012 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
5015 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
5023 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
5025 if (ret == GST_RTSP_OK)
5026 gst_rtspsrc_loop_complete_cmd (src, cmd);
5027 else if (ret == GST_RTSP_EINTR)
5028 gst_rtspsrc_loop_cancel_cmd (src, cmd);
5030 gst_rtspsrc_loop_error_cmd (src, cmd);
5034 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
5037 gboolean flushed = FALSE;
5039 /* start new request */
5040 gst_rtspsrc_loop_start_cmd (src, cmd);
5042 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
5044 GST_OBJECT_LOCK (src);
5045 old = src->pending_cmd;
5046 if (old == CMD_RECONNECT) {
5047 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
5048 cmd = CMD_RECONNECT;
5050 if (old != CMD_WAIT) {
5051 src->pending_cmd = CMD_WAIT;
5052 GST_OBJECT_UNLOCK (src);
5053 /* cancel previous request */
5054 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
5055 gst_rtspsrc_loop_cancel_cmd (src, old);
5056 GST_OBJECT_LOCK (src);
5058 src->pending_cmd = cmd;
5059 /* interrupt if allowed */
5060 if (src->busy_cmd & mask) {
5061 GST_DEBUG_OBJECT (src, "connection flush busy %s",
5062 cmd_to_string (src->busy_cmd));
5063 gst_rtspsrc_connection_flush (src, TRUE);
5066 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
5067 cmd_to_string (src->busy_cmd));
5070 gst_task_start (src->task);
5071 GST_OBJECT_UNLOCK (src);
5077 gst_rtspsrc_loop (GstRTSPSrc * src)
5081 if (!src->conninfo.connection || !src->conninfo.connected)
5084 if (src->interleaved)
5085 ret = gst_rtspsrc_loop_interleaved (src);
5087 ret = gst_rtspsrc_loop_udp (src);
5089 if (ret != GST_FLOW_OK)
5097 GST_WARNING_OBJECT (src, "we are not connected");
5098 ret = GST_FLOW_FLUSHING;
5103 const gchar *reason = gst_flow_get_name (ret);
5105 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
5106 src->running = FALSE;
5107 if (ret == GST_FLOW_EOS) {
5108 /* perform EOS logic */
5109 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5110 gst_element_post_message (GST_ELEMENT_CAST (src),
5111 gst_message_new_segment_done (GST_OBJECT_CAST (src),
5112 src->segment.format, src->segment.position));
5113 gst_rtspsrc_push_event (src,
5114 gst_event_new_segment_done (src->segment.format,
5115 src->segment.position));
5117 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5119 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
5120 /* for fatal errors we post an error message, post the error before the
5121 * EOS so the app knows about the error first. */
5122 GST_ELEMENT_ERROR (src, STREAM, FAILED,
5123 ("Internal data flow error."),
5124 ("streaming task paused, reason %s (%d)", reason, ret));
5125 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5127 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
5132 #ifndef GST_DISABLE_GST_DEBUG
5133 static const gchar *
5134 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
5138 while (method != 0) {
5155 static const gchar *
5156 gst_rtspsrc_skip_lws (const gchar * s)
5158 while (g_ascii_isspace (*s))
5163 static const gchar *
5164 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
5166 while (s > start && g_ascii_isspace (*(s - 1)))
5171 static const gchar *
5172 gst_rtspsrc_skip_commas (const gchar * s)
5174 /* The grammar allows for multiple commas */
5175 while (g_ascii_isspace (*s) || *s == ',')
5180 static const gchar *
5181 gst_rtspsrc_skip_item (const gchar * s)
5183 gboolean quoted = FALSE;
5184 const gchar *start = s;
5186 /* A list item ends at the last non-whitespace character
5187 * before a comma which is not inside a quoted-string. Or at
5188 * the end of the string.
5194 if (*s == '\\' && *(s + 1))
5203 return gst_rtspsrc_unskip_lws (s, start);
5207 gst_rtsp_decode_quoted_string (gchar * quoted_string)
5211 src = quoted_string + 1;
5212 dst = quoted_string;
5213 while (*src && *src != '"') {
5214 if (*src == '\\' && *(src + 1))
5221 /* Extract the authentication tokens that the server provided for each method
5222 * into an array of structures and give those to the connection object.
5225 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
5226 const gchar * header, gboolean * stale)
5228 GSList *list = NULL, *iter;
5230 gchar *item, *eq, *name_end, *value;
5232 g_return_if_fail (stale != NULL);
5234 gst_rtsp_connection_clear_auth_params (conn);
5237 /* Parse a header whose content is described by RFC2616 as
5238 * "#something", where "something" does not itself contain commas,
5239 * except as part of quoted-strings, into a list of allocated strings.
5241 header = gst_rtspsrc_skip_commas (header);
5243 end = gst_rtspsrc_skip_item (header);
5244 list = g_slist_prepend (list, g_strndup (header, end - header));
5245 header = gst_rtspsrc_skip_commas (end);
5250 list = g_slist_reverse (list);
5251 for (iter = list; iter; iter = iter->next) {
5254 eq = strchr (item, '=');
5256 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
5257 if (name_end == item) {
5258 /* That's no good... */
5265 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
5267 gst_rtsp_decode_quoted_string (value);
5271 if (value && strcmp (item, "stale") == 0 && strcmp (value, "TRUE") == 0)
5273 gst_rtsp_connection_set_auth_param (conn, item, value);
5277 g_slist_free (list);
5280 /* Parse a WWW-Authenticate Response header and determine the
5281 * available authentication methods
5283 * This code should also cope with the fact that each WWW-Authenticate
5284 * header can contain multiple challenge methods + tokens
5286 * At the moment, for Basic auth, we just do a minimal check and don't
5287 * even parse out the realm */
5289 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
5290 GstRTSPConnection * conn, gboolean * stale)
5294 g_return_if_fail (hdr != NULL);
5295 g_return_if_fail (methods != NULL);
5296 g_return_if_fail (stale != NULL);
5298 /* Skip whitespace at the start of the string */
5299 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
5301 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
5302 *methods |= GST_RTSP_AUTH_BASIC;
5303 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
5304 *methods |= GST_RTSP_AUTH_DIGEST;
5305 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
5310 * gst_rtspsrc_setup_auth:
5311 * @src: the rtsp source
5313 * Configure a username and password and auth method on the
5314 * connection object based on a response we received from the
5317 * Currently, this requires that a username and password were supplied
5318 * in the uri. In the future, they may be requested on demand by sending
5319 * a message up the bus.
5321 * Returns: TRUE if authentication information could be set up correctly.
5324 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5328 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5329 GstRTSPAuthMethod method;
5330 GstRTSPResult auth_result;
5332 GstRTSPConnection *conn;
5334 gboolean stale = FALSE;
5336 conn = src->conninfo.connection;
5338 /* Identify the available auth methods and see if any are supported */
5339 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
5340 &hdr, 0) == GST_RTSP_OK) {
5341 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
5344 if (avail_methods == GST_RTSP_AUTH_NONE)
5345 goto no_auth_available;
5347 /* For digest auth, if the response indicates that the session
5348 * data are stale, we just update them in the connection object and
5349 * return TRUE to retry the request */
5351 src->tried_url_auth = FALSE;
5353 url = gst_rtsp_connection_get_url (conn);
5355 /* Do we have username and password available? */
5356 if (url != NULL && !src->tried_url_auth && url->user != NULL
5357 && url->passwd != NULL) {
5360 src->tried_url_auth = TRUE;
5361 GST_DEBUG_OBJECT (src,
5362 "Attempting authentication using credentials from the URL");
5364 user = src->user_id;
5365 pass = src->user_pw;
5366 GST_DEBUG_OBJECT (src,
5367 "Attempting authentication using credentials from the properties");
5370 /* FIXME: If the url didn't contain username and password or we tried them
5371 * already, request a username and passwd from the application via some kind
5372 * of credentials request message */
5374 /* If we don't have a username and passwd at this point, bail out. */
5375 if (user == NULL || pass == NULL)
5378 /* Try to configure for each available authentication method, strongest to
5380 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5381 /* Check if this method is available on the server */
5382 if ((method & avail_methods) == 0)
5385 /* Pass the credentials to the connection to try on the next request */
5386 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5387 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5388 * ignore it and end up retrying later */
5389 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5390 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5391 gst_rtsp_auth_method_to_string (method));
5396 if (method == GST_RTSP_AUTH_NONE)
5397 goto no_auth_available;
5403 /* Output an error indicating that we couldn't connect because there were
5404 * no supported authentication protocols */
5405 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5406 ("No supported authentication protocol was found"));
5411 /* We don't fire an error message, we just return FALSE and let the
5412 * normal NOT_AUTHORIZED error be propagated */
5417 static GstRTSPResult
5418 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5419 GstRTSPMessage * request, GstRTSPMessage * response,
5420 GstRTSPStatusCode * code)
5423 GstRTSPStatusCode thecode;
5424 gchar *content_base = NULL;
5428 if (!src->short_header)
5429 gst_rtsp_ext_list_before_send (src->extensions, request);
5431 GST_DEBUG_OBJECT (src, "sending message");
5434 gst_rtsp_message_dump (request);
5436 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
5440 gst_rtsp_connection_reset_timeout (conn);
5443 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
5448 gst_rtsp_message_dump (response);
5450 switch (response->type) {
5451 case GST_RTSP_MESSAGE_REQUEST:
5452 res = gst_rtspsrc_handle_request (src, conn, response);
5453 if (res == GST_RTSP_EEOF)
5456 goto handle_request_failed;
5458 case GST_RTSP_MESSAGE_RESPONSE:
5459 /* ok, a response is good */
5460 GST_DEBUG_OBJECT (src, "received response message");
5462 case GST_RTSP_MESSAGE_DATA:
5463 /* get next response */
5464 GST_DEBUG_OBJECT (src, "handle data response message");
5465 gst_rtspsrc_handle_data (src, response);
5468 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5473 thecode = response->type_data.response.code;
5475 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5477 /* if the caller wanted the result code, we store it. */
5481 /* If the request didn't succeed, bail out before doing any more */
5482 if (thecode != GST_RTSP_STS_OK)
5485 /* store new content base if any */
5486 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5489 g_free (src->content_base);
5490 src->content_base = g_strdup (content_base);
5492 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5499 gchar *str = gst_rtsp_strresult (res);
5501 if (res != GST_RTSP_EINTR) {
5502 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5503 ("Could not send message. (%s)", str));
5505 GST_WARNING_OBJECT (src, "send interrupted");
5514 GST_WARNING_OBJECT (src, "server closed connection");
5515 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5517 /* if reconnect succeeds, try again */
5519 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5523 /* only try once after reconnect, then fallthrough and error out */
5526 gchar *str = gst_rtsp_strresult (res);
5528 if (res != GST_RTSP_EINTR) {
5529 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5530 ("Could not receive message. (%s)", str));
5532 GST_WARNING_OBJECT (src, "receive interrupted");
5540 handle_request_failed:
5542 /* ERROR was posted */
5543 gst_rtsp_message_unset (response);
5548 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5549 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5550 ("The server closed the connection."));
5551 gst_rtsp_message_unset (response);
5558 * @src: the rtsp source
5559 * @conn: the connection to send on
5560 * @request: must point to a valid request
5561 * @response: must point to an empty #GstRTSPMessage
5562 * @code: an optional code result
5564 * send @request and retrieve the response in @response. optionally @code can be
5565 * non-NULL in which case it will contain the status code of the response.
5567 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5568 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5570 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5571 * @response message) if the response code was not 200 (OK).
5573 * If the attempt results in an authentication failure, then this will attempt
5574 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5577 * Returns: #GST_RTSP_OK if the processing was successful.
5579 static GstRTSPResult
5580 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5581 GstRTSPMessage * request, GstRTSPMessage * response,
5582 GstRTSPStatusCode * code)
5584 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5585 GstRTSPResult res = GST_RTSP_ERROR;
5588 GstRTSPMethod method = GST_RTSP_INVALID;
5594 /* make sure we don't loop forever */
5598 /* save method so we can disable it when the server complains */
5599 method = request->type_data.request.method;
5602 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5606 case GST_RTSP_STS_UNAUTHORIZED:
5607 case GST_RTSP_STS_NOT_FOUND:
5608 if (gst_rtspsrc_setup_auth (src, response)) {
5609 /* Try the request/response again after configuring the auth info
5617 } while (retry == TRUE);
5619 /* If the user requested the code, let them handle errors, otherwise
5620 * post an error below */
5623 else if (int_code != GST_RTSP_STS_OK)
5624 goto error_response;
5631 GST_DEBUG_OBJECT (src, "got error %d", res);
5636 res = GST_RTSP_ERROR;
5638 switch (response->type_data.response.code) {
5639 case GST_RTSP_STS_NOT_FOUND:
5640 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5641 response->type_data.response.reason));
5643 case GST_RTSP_STS_UNAUTHORIZED:
5644 GST_ELEMENT_ERROR (src, RESOURCE, NOT_AUTHORIZED, (NULL), ("%s",
5645 response->type_data.response.reason));
5647 case GST_RTSP_STS_MOVED_PERMANENTLY:
5648 case GST_RTSP_STS_MOVE_TEMPORARILY:
5650 gchar *new_location;
5651 GstRTSPLowerTrans transports;
5653 GST_DEBUG_OBJECT (src, "got redirection");
5654 /* if we don't have a Location Header, we must error */
5655 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5656 &new_location, 0) < 0)
5659 /* When we receive a redirect result, we go back to the INIT state after
5660 * parsing the new URI. The caller should do the needed steps to issue
5661 * a new setup when it detects this state change. */
5662 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5664 /* save current transports */
5665 if (src->conninfo.url)
5666 transports = src->conninfo.url->transports;
5668 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5670 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5672 /* set old transports */
5673 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5674 src->conninfo.url->transports = transports;
5676 src->need_redirect = TRUE;
5677 src->state = GST_RTSP_STATE_INIT;
5681 case GST_RTSP_STS_NOT_ACCEPTABLE:
5682 case GST_RTSP_STS_NOT_IMPLEMENTED:
5683 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5684 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5685 gst_rtsp_method_as_text (method));
5686 src->methods &= ~method;
5690 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5691 ("Got error response: %d (%s).", response->type_data.response.code,
5692 response->type_data.response.reason));
5695 /* if we return ERROR we should unset the response ourselves */
5696 if (res == GST_RTSP_ERROR)
5697 gst_rtsp_message_unset (response);
5703 static GstRTSPResult
5704 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5705 GstRTSPMessage * response, GstRTSPSrc * src)
5707 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5712 /* parse the response and collect all the supported methods. We need this
5713 * information so that we don't try to send an unsupported request to the
5717 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5719 GstRTSPHeaderField field;
5723 /* reset supported methods */
5726 /* Try Allow Header first */
5727 field = GST_RTSP_HDR_ALLOW;
5730 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5731 if (indx == 0 && !respoptions) {
5732 /* if no Allow header was found then try the Public header... */
5733 field = GST_RTSP_HDR_PUBLIC;
5734 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5739 src->methods |= gst_rtsp_options_from_text (respoptions);
5744 if (src->methods == 0) {
5745 /* neither Allow nor Public are required, assume the server supports
5746 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5748 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5749 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5751 /* always assume PLAY, FIXME, extensions should be able to override
5753 src->methods |= GST_RTSP_PLAY;
5754 /* also assume it will support Range */
5755 src->seekable = TRUE;
5757 /* we need describe and setup */
5758 if (!(src->methods & GST_RTSP_DESCRIBE))
5760 if (!(src->methods & GST_RTSP_SETUP))
5768 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5769 ("Server does not support DESCRIBE."));
5774 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5775 ("Server does not support SETUP."));
5780 /* masks to be kept in sync with the hardcoded protocol order of preference
5782 static const guint protocol_masks[] = {
5783 GST_RTSP_LOWER_TRANS_UDP,
5784 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5785 GST_RTSP_LOWER_TRANS_TCP,
5789 static GstRTSPResult
5790 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5791 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
5795 gboolean add_udp_str;
5800 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5805 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5807 /* extension listed transports, use those */
5808 if (*transports != NULL)
5811 /* it's the default */
5812 add_udp_str = FALSE;
5814 /* the default RTSP transports */
5815 result = g_string_new ("RTP");
5818 case GST_RTSP_PROFILE_AVP:
5819 g_string_append (result, "/AVP");
5821 case GST_RTSP_PROFILE_SAVP:
5822 g_string_append (result, "/SAVP");
5824 case GST_RTSP_PROFILE_AVPF:
5825 g_string_append (result, "/AVPF");
5827 case GST_RTSP_PROFILE_SAVPF:
5828 g_string_append (result, "/SAVPF");
5834 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5835 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5837 g_string_append (result, "/UDP");
5838 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5839 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5840 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5841 /* we don't have to allocate any UDP ports yet, if the selected transport
5842 * turns out to be multicast we can create them and join the multicast
5843 * group indicated in the transport reply */
5845 g_string_append (result, "/UDP");
5846 g_string_append (result, ";multicast");
5847 if (src->next_port_num != 0) {
5848 if (src->client_port_range.max > 0 &&
5849 src->next_port_num >= src->client_port_range.max)
5852 g_string_append_printf (result, ";client_port=%d-%d",
5853 src->next_port_num, src->next_port_num + 1);
5855 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
5856 GST_DEBUG_OBJECT (src, "adding TCP");
5858 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
5860 *transports = g_string_free (result, FALSE);
5862 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5869 GST_ERROR ("extension gave error %d", res);
5874 GST_ERROR ("no more ports available");
5875 return GST_RTSP_ERROR;
5879 static GstRTSPResult
5880 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5881 gint orig_rtpport, gint orig_rtcpport)
5884 gint nr_udp, nr_int;
5886 gint rtpport = 0, rtcpport = 0;
5889 src = stream->parent;
5891 /* find number of placeholders first */
5892 if (strstr (*transports, "%%i2"))
5894 else if (strstr (*transports, "%%i1"))
5899 if (strstr (*transports, "%%u2"))
5901 else if (strstr (*transports, "%%u1"))
5906 if (nr_udp == 0 && nr_int == 0)
5910 if (!orig_rtpport || !orig_rtcpport) {
5911 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5914 rtpport = orig_rtpport;
5915 rtcpport = orig_rtcpport;
5919 str = g_string_new ("");
5921 while ((next = strstr (p, "%%"))) {
5922 g_string_append_len (str, p, next - p);
5923 if (next[2] == 'u') {
5925 g_string_append_printf (str, "%d", rtpport);
5926 else if (next[3] == '2')
5927 g_string_append_printf (str, "%d", rtcpport);
5929 if (next[2] == 'i') {
5931 g_string_append_printf (str, "%d", src->free_channel);
5932 else if (next[3] == '2')
5933 g_string_append_printf (str, "%d", src->free_channel + 1);
5938 /* append final part */
5939 g_string_append (str, p);
5941 g_free (*transports);
5942 *transports = g_string_free (str, FALSE);
5950 GST_ERROR ("failed to allocate udp ports");
5951 return GST_RTSP_ERROR;
5956 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
5958 GstCaps *caps = NULL;
5960 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
5964 GST_DEBUG_OBJECT (src, "SRTP parameters received");
5970 default_srtcp_params (void)
5977 guint data_size = GST_ROUND_UP_4 (KEY_SIZE);
5979 /* create a random key */
5980 key_data = g_malloc (data_size);
5981 for (i = 0; i < data_size; i += 4)
5982 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
5984 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
5986 caps = gst_caps_new_simple ("application/x-srtcp",
5987 "srtp-key", GST_TYPE_BUFFER, buf,
5988 "srtp-cipher", G_TYPE_STRING, "aes-128-icm",
5989 "srtp-auth", G_TYPE_STRING, "hmac-sha1-80",
5990 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
5991 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
5993 gst_buffer_unref (buf);
5999 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
6001 gchar *base64, *result = NULL;
6002 GstMIKEYMessage *mikey_msg;
6004 stream->srtcpparams = signal_get_srtcp_params (src, stream);
6005 if (stream->srtcpparams == NULL)
6006 stream->srtcpparams = default_srtcp_params ();
6008 mikey_msg = gst_mikey_message_new_from_caps (stream->srtcpparams);
6010 /* add policy '0' for our SSRC */
6011 gst_mikey_message_add_cs_srtp (mikey_msg, 0, stream->send_ssrc, 0);
6013 base64 = gst_mikey_message_base64_encode (mikey_msg);
6014 gst_mikey_message_unref (mikey_msg);
6017 result = gst_sdp_make_keymgmt (stream->conninfo.location, base64);
6025 /* Perform the SETUP request for all the streams.
6027 * We ask the server for a specific transport, which initially includes all the
6028 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
6029 * two local UDP ports that we send to the server.
6031 * Once the server replied with a transport, we configure the other streams
6032 * with the same transport.
6034 * This function will also configure the stream for the selected transport,
6035 * which basically means creating the pipeline.
6037 static GstRTSPResult
6038 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
6041 GstRTSPResult res = GST_RTSP_ERROR;
6042 GstRTSPMessage request = { 0 };
6043 GstRTSPMessage response = { 0 };
6044 GstRTSPStream *stream = NULL;
6045 GstRTSPLowerTrans protocols;
6046 GstRTSPStatusCode code;
6047 gboolean unsupported_real = FALSE;
6048 gint rtpport, rtcpport;
6052 if (src->conninfo.connection) {
6053 url = gst_rtsp_connection_get_url (src->conninfo.connection);
6054 /* we initially allow all configured lower transports. based on the URL
6055 * transports and the replies from the server we narrow them down. */
6056 protocols = url->transports & src->cur_protocols;
6059 protocols = src->cur_protocols;
6065 /* reset some state */
6066 src->free_channel = 0;
6067 src->interleaved = FALSE;
6068 src->need_activate = FALSE;
6069 /* keep track of next port number, 0 is random */
6070 src->next_port_num = src->client_port_range.min;
6071 rtpport = rtcpport = 0;
6073 if (G_UNLIKELY (src->streams == NULL))
6076 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6077 GstRTSPConnection *conn;
6084 stream = (GstRTSPStream *) walk->data;
6086 caps = stream_get_caps_for_pt (stream, stream->default_pt);
6088 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
6092 if (stream->skipped) {
6093 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
6097 /* see if we need to configure this stream */
6098 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
6099 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
6104 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
6105 stream->id, caps, &selected);
6107 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
6111 /* merge/overwrite global caps */
6116 s = gst_caps_get_structure (caps, 0);
6118 num = gst_structure_n_fields (src->props);
6119 for (j = 0; j < num; j++) {
6123 name = gst_structure_nth_field_name (src->props, j);
6124 val = gst_structure_get_value (src->props, name);
6125 gst_structure_set_value (s, name, val);
6127 GST_DEBUG_OBJECT (src, "copied %s", name);
6131 /* skip setup if we have no URL for it */
6132 if (stream->conninfo.location == NULL) {
6133 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
6137 if (src->conninfo.connection == NULL) {
6138 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
6139 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
6142 conn = stream->conninfo.connection;
6144 conn = src->conninfo.connection;
6146 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
6147 stream->conninfo.location);
6149 /* if we have a multicast connection, only suggest multicast from now on */
6150 if (stream->is_multicast)
6151 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
6154 /* first selectable protocol */
6155 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6157 if (!protocol_masks[mask])
6161 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
6162 protocol_masks[mask]);
6163 /* create a string with first transport in line */
6165 res = gst_rtspsrc_create_transports_string (src,
6166 protocols & protocol_masks[mask], stream->profile, &transports);
6167 if (res < 0 || transports == NULL)
6168 goto setup_transport_failed;
6170 if (strlen (transports) == 0) {
6171 g_free (transports);
6172 GST_DEBUG_OBJECT (src, "no transports found");
6177 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
6179 /* replace placeholders with real values, this function will optionally
6180 * allocate UDP ports and other info needed to execute the setup request */
6181 res = gst_rtspsrc_prepare_transports (stream, &transports,
6182 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
6184 g_free (transports);
6185 goto setup_transport_failed;
6188 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
6190 /* create SETUP request */
6192 gst_rtspsrc_init_request (src, &request, GST_RTSP_SETUP,
6193 stream->conninfo.location);
6195 g_free (transports);
6196 goto create_request_failed;
6199 /* select transport */
6200 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
6203 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
6204 stream->profile == GST_RTSP_PROFILE_SAVPF) {
6205 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
6206 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
6209 /* if the user wants a non default RTP packet size we add the blocksize
6211 if (src->rtp_blocksize > 0) {
6212 hval = g_strdup_printf ("%d", src->rtp_blocksize);
6213 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
6217 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
6220 /* handle the code ourselves */
6221 res = gst_rtspsrc_send (src, conn, &request, &response, &code);
6226 case GST_RTSP_STS_OK:
6228 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
6229 gst_rtsp_message_unset (&request);
6230 gst_rtsp_message_unset (&response);
6231 /* cleanup of leftover transport */
6232 gst_rtspsrc_stream_free_udp (stream);
6233 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
6234 * we might be in this case */
6235 if (stream->container && rtpport && rtcpport && !retry) {
6236 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
6241 /* this transport did not go down well, but we may have others to try
6242 * that we did not send yet, try those and only give up then
6243 * but not without checking for lost cause/extension so we can
6244 * post a nicer/more useful error message later */
6245 if (!unsupported_real)
6246 unsupported_real = stream->is_real;
6247 /* select next available protocol, give up on this stream if none */
6249 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6251 if (!protocol_masks[mask] || unsupported_real)
6256 /* cleanup of leftover transport and move to the next stream */
6257 gst_rtspsrc_stream_free_udp (stream);
6258 goto response_error;
6261 /* parse response transport */
6263 gchar *resptrans = NULL;
6264 GstRTSPTransport transport = { 0 };
6266 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
6269 gst_rtspsrc_stream_free_udp (stream);
6273 /* parse transport, go to next stream on parse error */
6274 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6275 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6279 /* update allowed transports for other streams. once the transport of
6280 * one stream has been determined, we make sure that all other streams
6281 * are configured in the same way */
6282 switch (transport.lower_transport) {
6283 case GST_RTSP_LOWER_TRANS_TCP:
6284 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6285 protocols = GST_RTSP_LOWER_TRANS_TCP;
6286 src->interleaved = TRUE;
6287 /* update free channels */
6289 MAX (transport.interleaved.min, src->free_channel);
6291 MAX (transport.interleaved.max, src->free_channel);
6292 src->free_channel++;
6294 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6295 /* only allow multicast for other streams */
6296 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6297 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6298 /* if the server selected our ports, increment our counters so that
6299 * we select a new port later */
6300 if (src->next_port_num == transport.port.min &&
6301 src->next_port_num + 1 == transport.port.max) {
6302 src->next_port_num += 2;
6305 case GST_RTSP_LOWER_TRANS_UDP:
6306 /* only allow unicast for other streams */
6307 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6308 protocols = GST_RTSP_LOWER_TRANS_UDP;
6311 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6312 transport.lower_transport);
6316 if (!src->interleaved || !retry) {
6317 /* now configure the stream with the selected transport */
6318 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6319 GST_DEBUG_OBJECT (src,
6320 "could not configure stream %p transport, skipping stream",
6323 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
6324 /* retain the first allocated UDP port pair */
6325 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
6326 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
6329 /* we need to activate at least one streams when we detect activity */
6330 src->need_activate = TRUE;
6332 /* stream is setup now */
6333 stream->setup = TRUE;
6338 GstRTSPStream *sskip;
6340 skip = g_list_next (skip);
6344 sskip = (GstRTSPStream *) skip->data;
6346 /* skip all streams with the same control url */
6347 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
6348 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
6349 sskip, sskip->conninfo.location);
6350 sskip->skipped = TRUE;
6355 /* clean up our transport struct */
6356 gst_rtsp_transport_init (&transport);
6357 /* clean up used RTSP messages */
6358 gst_rtsp_message_unset (&request);
6359 gst_rtsp_message_unset (&response);
6363 /* store the transport protocol that was configured */
6364 src->cur_protocols = protocols;
6366 gst_rtsp_ext_list_stream_select (src->extensions, url);
6368 /* if there is nothing to activate, error out */
6369 if (!src->need_activate)
6370 goto nothing_to_activate;
6377 /* no transport possible, post an error and stop */
6378 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6379 ("Could not connect to server, no protocols left"));
6380 return GST_RTSP_ERROR;
6384 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6385 ("SDP contains no streams"));
6386 return GST_RTSP_ERROR;
6388 create_request_failed:
6390 gchar *str = gst_rtsp_strresult (res);
6392 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6393 ("Could not create request. (%s)", str));
6397 setup_transport_failed:
6399 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6400 ("Could not setup transport."));
6401 res = GST_RTSP_ERROR;
6406 const gchar *str = gst_rtsp_status_as_text (code);
6408 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6409 ("Error (%d): %s", code, GST_STR_NULL (str)));
6410 res = GST_RTSP_ERROR;
6415 gchar *str = gst_rtsp_strresult (res);
6417 if (res != GST_RTSP_EINTR) {
6418 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6419 ("Could not send message. (%s)", str));
6421 GST_WARNING_OBJECT (src, "send interrupted");
6428 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6429 ("Server did not select transport."));
6430 res = GST_RTSP_ERROR;
6433 nothing_to_activate:
6435 /* none of the available error codes is really right .. */
6436 if (unsupported_real) {
6437 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6438 (_("No supported stream was found. You might need to install a "
6439 "GStreamer RTSP extension plugin for Real media streams.")),
6442 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6443 (_("No supported stream was found. You might need to allow "
6444 "more transport protocols or may otherwise be missing "
6445 "the right GStreamer RTSP extension plugin.")), (NULL));
6447 return GST_RTSP_ERROR;
6451 gst_rtsp_message_unset (&request);
6452 gst_rtsp_message_unset (&response);
6458 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
6459 GstSegment * segment)
6462 GstRTSPTimeRange *therange;
6465 gst_rtsp_range_free (src->range);
6467 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
6468 GST_DEBUG_OBJECT (src, "parsed range %s", range);
6469 src->range = therange;
6471 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6473 gst_segment_init (segment, GST_FORMAT_TIME);
6477 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6478 therange->min.type, therange->min.seconds, therange->max.type,
6479 therange->max.seconds);
6481 if (therange->min.type == GST_RTSP_TIME_NOW)
6483 else if (therange->min.type == GST_RTSP_TIME_END)
6486 seconds = therange->min.seconds * GST_SECOND;
6488 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6489 GST_TIME_ARGS (seconds));
6491 /* we need to start playback without clipping from the position reported by
6493 segment->start = seconds;
6494 segment->position = seconds;
6496 if (therange->max.type == GST_RTSP_TIME_NOW)
6498 else if (therange->max.type == GST_RTSP_TIME_END)
6501 seconds = therange->max.seconds * GST_SECOND;
6503 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6504 GST_TIME_ARGS (seconds));
6506 /* live (WMS) server might send overflowed large max as its idea of infinity,
6507 * compensate to prevent problems later on */
6508 if (seconds != -1 && seconds < 0) {
6510 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6513 /* live (WMS) might send min == max, which is not worth recording */
6514 if (segment->duration == -1 && seconds == segment->start)
6517 /* don't change duration with unknown value, we might have a valid value
6518 * there that we want to keep. */
6520 segment->duration = seconds;
6525 /* Parse clock profived by the server with following syntax:
6527 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6530 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6532 gboolean res = FALSE;
6534 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6535 gchar **fields = NULL, **parts = NULL;
6536 gchar *remote_ip, *str;
6538 GstClockTime base_time;
6541 fields = g_strsplit (gstclock, " ", 0);
6543 /* wrapped clock, not very interesting for now */
6544 if (fields[1] == NULL)
6547 /* remote IP address and port */
6548 if ((str = fields[2]) == NULL)
6551 parts = g_strsplit (str, ":", 0);
6553 if ((remote_ip = parts[0]) == NULL)
6556 if ((str = parts[1]) == NULL)
6564 if ((str = fields[3]) == NULL)
6567 base_time = g_ascii_strtoull (str, NULL, 10);
6570 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
6573 if (src->provided_clock)
6574 gst_object_unref (src->provided_clock);
6575 src->provided_clock = netclock;
6577 gst_element_post_message (GST_ELEMENT_CAST (src),
6578 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
6579 src->provided_clock, TRUE));
6583 g_strfreev (fields);
6589 /* must be called with the RTSP state lock */
6590 static GstRTSPResult
6591 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
6597 /* prepare global stream caps properties */
6599 gst_structure_remove_all_fields (src->props);
6601 src->props = gst_structure_new_empty ("RTSPProperties");
6604 gst_sdp_message_dump (sdp);
6606 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
6608 /* let the app inspect and change the SDP */
6609 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6611 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6613 /* parse range for duration reporting. */
6618 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6622 /* keep track of the range and configure it in the segment */
6623 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6627 /* parse clock information. This is GStreamer specific, a server can tell the
6628 * client what clock it is using and wrap that in a network clock. The
6629 * advantage of that is that we can slave to it. */
6631 const gchar *gstclock;
6634 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6635 if (gstclock == NULL)
6638 /* parse the clock and expose it in the provide_clock method */
6639 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6643 /* try to find a global control attribute. Note that a '*' means that we should
6644 * do aggregate control with the current url (so we don't do anything and
6645 * leave the current connection as is) */
6647 const gchar *control;
6650 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6651 if (control == NULL)
6654 /* only take fully qualified urls */
6655 if (g_str_has_prefix (control, "rtsp://"))
6659 g_free (src->conninfo.location);
6660 src->conninfo.location = g_strdup (control);
6661 /* make a connection for this, if there was a connection already, nothing
6663 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6664 GST_ERROR_OBJECT (src, "could not connect");
6667 /* we need to keep the control url separate from the connection url because
6668 * the rules for constructing the media control url need it */
6669 g_free (src->control);
6670 src->control = g_strdup (control);
6673 /* create streams */
6674 n_streams = gst_sdp_message_medias_len (sdp);
6675 for (i = 0; i < n_streams; i++) {
6676 gst_rtspsrc_create_stream (src, sdp, i);
6679 src->state = GST_RTSP_STATE_INIT;
6682 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6685 /* reset our state */
6686 src->need_range = TRUE;
6689 src->state = GST_RTSP_STATE_READY;
6696 GST_ERROR_OBJECT (src, "setup failed");
6697 gst_rtspsrc_cleanup (src);
6702 static GstRTSPResult
6703 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6707 GstRTSPMessage request = { 0 };
6708 GstRTSPMessage response = { 0 };
6711 gchar *respcont = NULL;
6714 src->need_redirect = FALSE;
6716 /* can't continue without a valid url */
6717 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6718 res = GST_RTSP_EINVAL;
6721 src->tried_url_auth = FALSE;
6723 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6724 goto connect_failed;
6726 /* create OPTIONS */
6727 GST_DEBUG_OBJECT (src, "create options...");
6729 gst_rtspsrc_init_request (src, &request, GST_RTSP_OPTIONS,
6730 src->conninfo.url_str);
6732 goto create_request_failed;
6735 GST_DEBUG_OBJECT (src, "send options...");
6738 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6741 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6746 if (!gst_rtspsrc_parse_methods (src, &response))
6749 /* create DESCRIBE */
6750 GST_DEBUG_OBJECT (src, "create describe...");
6752 gst_rtspsrc_init_request (src, &request, GST_RTSP_DESCRIBE,
6753 src->conninfo.url_str);
6755 goto create_request_failed;
6757 /* we only accept SDP for now */
6758 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
6762 GST_DEBUG_OBJECT (src, "send describe...");
6765 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
6768 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6772 /* we only perform redirect for the describe, currently */
6773 if (src->need_redirect) {
6774 /* close connection, we don't have to send a TEARDOWN yet, ignore the
6776 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6778 gst_rtsp_message_unset (&request);
6779 gst_rtsp_message_unset (&response);
6785 /* it could be that the DESCRIBE method was not implemented */
6786 if (!(src->methods & GST_RTSP_DESCRIBE))
6789 /* check if reply is SDP */
6790 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
6792 /* could not be set but since the request returned OK, we assume it
6793 * was SDP, else check it. */
6795 const gchar *props = strchr (respcont, ';');
6798 gchar *mimetype = g_strndup (respcont, props - respcont);
6800 mimetype = g_strstrip (mimetype);
6801 if (g_ascii_strcasecmp (mimetype, "application/sdp") != 0) {
6803 goto wrong_content_type;
6806 /* TODO: Check for charset property and do conversions of all messages if
6807 * needed. Some servers actually send that property */
6810 } else if (g_ascii_strcasecmp (respcont, "application/sdp") != 0) {
6811 goto wrong_content_type;
6815 /* get message body and parse as SDP */
6816 gst_rtsp_message_get_body (&response, &data, &size);
6817 if (data == NULL || size == 0)
6820 GST_DEBUG_OBJECT (src, "parse SDP...");
6821 gst_sdp_message_new (sdp);
6822 gst_sdp_message_parse_buffer (data, size, *sdp);
6824 /* clean up any messages */
6825 gst_rtsp_message_unset (&request);
6826 gst_rtsp_message_unset (&response);
6833 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
6834 ("No valid RTSP URL was provided"));
6839 gchar *str = gst_rtsp_strresult (res);
6841 if (res != GST_RTSP_EINTR) {
6842 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6843 ("Failed to connect. (%s)", str));
6845 GST_WARNING_OBJECT (src, "connect interrupted");
6850 create_request_failed:
6852 gchar *str = gst_rtsp_strresult (res);
6854 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6855 ("Could not create request. (%s)", str));
6861 /* Don't post a message - the rtsp_send method will have
6862 * taken care of it because we passed NULL for the response code */
6867 /* error was posted */
6868 res = GST_RTSP_ERROR;
6873 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6874 ("Server does not support SDP, got %s.", respcont));
6875 res = GST_RTSP_ERROR;
6880 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6881 ("Server can not provide an SDP."));
6882 res = GST_RTSP_ERROR;
6887 if (src->conninfo.connection) {
6888 GST_DEBUG_OBJECT (src, "free connection");
6889 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6891 gst_rtsp_message_unset (&request);
6892 gst_rtsp_message_unset (&response);
6897 static GstRTSPResult
6898 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
6903 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
6905 if (src->sdp == NULL) {
6906 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
6910 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
6915 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
6922 GST_WARNING_OBJECT (src, "can't get sdp");
6923 src->open_error = TRUE;
6928 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
6929 src->open_error = TRUE;
6934 static GstRTSPResult
6935 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
6937 GstRTSPMessage request = { 0 };
6938 GstRTSPMessage response = { 0 };
6939 GstRTSPResult res = GST_RTSP_OK;
6941 const gchar *control;
6943 GST_DEBUG_OBJECT (src, "TEARDOWN...");
6945 gst_rtspsrc_set_state (src, GST_STATE_READY);
6947 if (src->state < GST_RTSP_STATE_READY) {
6948 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
6955 /* construct a control url */
6956 control = get_aggregate_control (src);
6958 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
6961 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6962 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6963 const gchar *setup_url;
6964 GstRTSPConnInfo *info;
6966 /* try aggregate control first but do non-aggregate control otherwise */
6968 setup_url = control;
6969 else if ((setup_url = stream->conninfo.location) == NULL)
6972 if (src->conninfo.connection) {
6973 info = &src->conninfo;
6974 } else if (stream->conninfo.connection) {
6975 info = &stream->conninfo;
6979 if (!info->connected)
6984 gst_rtspsrc_init_request (src, &request, GST_RTSP_TEARDOWN, setup_url);
6986 goto create_request_failed;
6989 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
6992 gst_rtspsrc_send (src, info->connection, &request, &response,
6996 /* FIXME, parse result? */
6997 gst_rtsp_message_unset (&request);
6998 gst_rtsp_message_unset (&response);
7001 /* early exit when we did aggregate control */
7007 /* close connections */
7008 GST_DEBUG_OBJECT (src, "closing connection...");
7009 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7010 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7011 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7012 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
7016 gst_rtspsrc_cleanup (src);
7018 src->state = GST_RTSP_STATE_INVALID;
7021 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
7026 create_request_failed:
7028 gchar *str = gst_rtsp_strresult (res);
7030 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7031 ("Could not create request. (%s)", str));
7037 gchar *str = gst_rtsp_strresult (res);
7039 gst_rtsp_message_unset (&request);
7040 if (res != GST_RTSP_EINTR) {
7041 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7042 ("Could not send message. (%s)", str));
7044 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
7051 GST_DEBUG_OBJECT (src,
7052 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
7057 /* RTP-Info is of the format:
7059 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
7061 * rtptime corresponds to the timestamp for the NPT time given in the header
7062 * seqbase corresponds to the next sequence number we received. This number
7063 * indicates the first seqnum after the seek and should be used to discard
7064 * packets that are from before the seek.
7067 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
7072 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
7074 infos = g_strsplit (rtpinfo, ",", 0);
7075 for (i = 0; infos[i]; i++) {
7077 GstRTSPStream *stream;
7081 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
7083 /* init values, types of seqbase and timebase are bigger than needed so we
7084 * can store -1 as uninitialized values */
7089 /* parse url, find stream for url.
7090 * parse seq and rtptime. The seq number should be configured in the rtp
7091 * depayloader or session manager to detect gaps. Same for the rtptime, it
7092 * should be used to create an initial time newsegment. */
7093 fields = g_strsplit (infos[i], ";", 0);
7094 for (j = 0; fields[j]; j++) {
7095 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
7096 /* remove leading whitespace */
7097 fields[j] = g_strchug (fields[j]);
7098 if (g_str_has_prefix (fields[j], "url=")) {
7099 /* get the url and the stream */
7101 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
7102 } else if (g_str_has_prefix (fields[j], "seq=")) {
7103 seqbase = atoi (fields[j] + 4);
7104 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
7105 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
7108 g_strfreev (fields);
7109 /* now we need to store the values for the caps of the stream */
7110 if (stream != NULL) {
7111 GST_DEBUG_OBJECT (src,
7112 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
7113 stream, seqbase, timebase);
7115 /* we have a stream, configure detected params */
7116 stream->seqbase = seqbase;
7117 stream->timebase = timebase;
7126 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
7131 interval = strtoul (rtcp, NULL, 10);
7132 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
7137 interval *= GST_MSECOND;
7139 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7140 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7142 /* already (optionally) retrieved this when configuring manager */
7143 if (stream->session) {
7144 GObject *rtpsession = stream->session;
7146 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
7148 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
7152 /* now it happens that (Xenon) server sending this may also provide bogus
7153 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
7154 * and just use RTP-Info to sync */
7156 GObjectClass *klass;
7158 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
7159 if (g_object_class_find_property (klass, "rtcp-sync")) {
7160 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
7161 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
7167 gst_rtspsrc_get_float (const gchar * dstr)
7169 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7171 /* canonicalise floating point string so we can handle float strings
7172 * in the form "24.930" or "24,930" irrespective of the current locale */
7173 g_strlcpy (s, dstr, sizeof (s));
7174 g_strdelimit (s, ",", '.');
7175 return g_ascii_strtod (s, NULL);
7179 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
7181 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7183 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
7184 g_strlcpy (val_str, "now", sizeof (val_str));
7186 if (segment->position == 0) {
7187 g_strlcpy (val_str, "0", sizeof (val_str));
7189 g_ascii_dtostr (val_str, sizeof (val_str),
7190 ((gdouble) segment->position) / GST_SECOND);
7193 return g_strdup_printf ("npt=%s-", val_str);
7197 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
7201 stream->timebase = -1;
7202 stream->seqbase = -1;
7204 len = stream->ptmap->len;
7205 for (i = 0; i < len; i++) {
7206 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
7209 if (item->caps == NULL)
7212 item->caps = gst_caps_make_writable (item->caps);
7213 s = gst_caps_get_structure (item->caps, 0);
7214 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
7215 if (item->pt == stream->default_pt && stream->udpsrc[0])
7216 g_object_set (stream->udpsrc[0], "caps", item->caps, NULL);
7218 stream->need_caps = TRUE;
7221 static GstRTSPResult
7222 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
7224 GstRTSPResult res = GST_RTSP_OK;
7226 if (src->state < GST_RTSP_STATE_READY) {
7227 res = GST_RTSP_ERROR;
7228 if (src->open_error) {
7229 GST_DEBUG_OBJECT (src, "the stream was in error");
7233 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
7235 if ((res = gst_rtspsrc_open (src, async)) < 0) {
7236 GST_DEBUG_OBJECT (src, "failed to open stream");
7245 static GstRTSPResult
7246 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
7248 GstRTSPMessage request = { 0 };
7249 GstRTSPMessage response = { 0 };
7250 GstRTSPResult res = GST_RTSP_OK;
7254 const gchar *control;
7256 GST_DEBUG_OBJECT (src, "PLAY...");
7258 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7261 if (!(src->methods & GST_RTSP_PLAY))
7264 if (src->state == GST_RTSP_STATE_PLAYING)
7267 if (!src->conninfo.connection || !src->conninfo.connected)
7270 /* send some dummy packets before we activate the receive in the
7272 gst_rtspsrc_send_dummy_packets (src);
7274 /* require new SR packets */
7276 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
7278 /* construct a control url */
7279 control = get_aggregate_control (src);
7281 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7282 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7283 const gchar *setup_url;
7284 GstRTSPConnection *conn;
7286 /* try aggregate control first but do non-aggregate control otherwise */
7288 setup_url = control;
7289 else if ((setup_url = stream->conninfo.location) == NULL)
7292 if (src->conninfo.connection) {
7293 conn = src->conninfo.connection;
7294 } else if (stream->conninfo.connection) {
7295 conn = stream->conninfo.connection;
7301 res = gst_rtspsrc_init_request (src, &request, GST_RTSP_PLAY, setup_url);
7303 goto create_request_failed;
7305 if (src->need_range) {
7306 hval = gen_range_header (src, segment);
7308 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
7310 /* store the newsegment event so it can be sent from the streaming thread. */
7311 src->need_segment = TRUE;
7314 if (segment->rate != 1.0) {
7315 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
7317 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
7319 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
7321 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
7325 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
7327 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7330 /* seek may have silently failed as it is not supported */
7331 if (!(src->methods & GST_RTSP_PLAY)) {
7332 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
7333 /* obviously it is supported as we made it here */
7334 src->methods |= GST_RTSP_PLAY;
7335 src->seekable = FALSE;
7336 /* but there is nothing to parse in the response,
7337 * so convey we have no idea and not to expect anything particular */
7338 clear_rtp_base (src, stream);
7342 /* need to do for all streams */
7343 for (run = src->streams; run; run = g_list_next (run))
7344 clear_rtp_base (src, (GstRTSPStream *) run->data);
7346 /* NOTE the above also disables npt based eos detection */
7347 /* and below forces position to 0,
7348 * which is visible feedback we lost the plot */
7349 segment->start = segment->position = src->last_pos;
7352 gst_rtsp_message_unset (&request);
7354 /* parse RTP npt field. This is the current position in the stream (Normal
7355 * Play Time) and should be put in the NEWSEGMENT position field. */
7356 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
7358 gst_rtspsrc_parse_range (src, hval, segment);
7360 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
7361 segment->rate = 1.0;
7363 /* parse Speed header. This is the intended playback rate of the stream
7364 * and should be put in the NEWSEGMENT rate field. */
7365 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
7366 0) == GST_RTSP_OK) {
7367 segment->rate = gst_rtspsrc_get_float (hval);
7368 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
7369 &hval, 0) == GST_RTSP_OK) {
7370 segment->rate = gst_rtspsrc_get_float (hval);
7373 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
7374 * for the RTP packets. If this is not present, we assume all starts from 0...
7375 * This is info for the RTP session manager that we pass to it in caps. */
7377 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
7378 &hval, hval_idx++) == GST_RTSP_OK)
7379 gst_rtspsrc_parse_rtpinfo (src, hval);
7381 /* some servers indicate RTCP parameters in PLAY response,
7382 * rather than properly in SDP */
7383 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
7384 &hval, 0) == GST_RTSP_OK)
7385 gst_rtspsrc_handle_rtcp_interval (src, hval);
7387 gst_rtsp_message_unset (&response);
7389 /* early exit when we did aggregate control */
7393 /* configure the caps of the streams after we parsed all headers. Only reset
7394 * the manager object when we set a new Range header (we did a seek) */
7395 gst_rtspsrc_configure_caps (src, segment, src->need_range);
7397 /* set to PLAYING after we have configured the caps, otherwise we
7398 * might end up calling request_key (with SRTP) while caps are still
7399 * being configured. */
7400 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
7402 /* set again when needed */
7403 src->need_range = FALSE;
7405 src->running = TRUE;
7406 src->base_time = -1;
7407 src->state = GST_RTSP_STATE_PLAYING;
7410 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
7411 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7412 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7413 stream->discont = TRUE;
7418 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
7425 GST_DEBUG_OBJECT (src, "failed to open stream");
7430 GST_DEBUG_OBJECT (src, "PLAY is not supported");
7435 GST_DEBUG_OBJECT (src, "we were already PLAYING");
7438 create_request_failed:
7440 gchar *str = gst_rtsp_strresult (res);
7442 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7443 ("Could not create request. (%s)", str));
7449 gchar *str = gst_rtsp_strresult (res);
7451 gst_rtsp_message_unset (&request);
7452 if (res != GST_RTSP_EINTR) {
7453 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7454 ("Could not send message. (%s)", str));
7456 GST_WARNING_OBJECT (src, "PLAY interrupted");
7463 static GstRTSPResult
7464 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
7466 GstRTSPResult res = GST_RTSP_OK;
7467 GstRTSPMessage request = { 0 };
7468 GstRTSPMessage response = { 0 };
7470 const gchar *control;
7472 GST_DEBUG_OBJECT (src, "PAUSE...");
7474 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7477 if (!(src->methods & GST_RTSP_PAUSE))
7480 if (src->state == GST_RTSP_STATE_READY)
7483 if (!src->conninfo.connection || !src->conninfo.connected)
7486 /* construct a control url */
7487 control = get_aggregate_control (src);
7489 /* loop over the streams. We might exit the loop early when we could do an
7490 * aggregate control */
7491 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7492 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7493 GstRTSPConnection *conn;
7494 const gchar *setup_url;
7496 /* try aggregate control first but do non-aggregate control otherwise */
7498 setup_url = control;
7499 else if ((setup_url = stream->conninfo.location) == NULL)
7502 if (src->conninfo.connection) {
7503 conn = src->conninfo.connection;
7504 } else if (stream->conninfo.connection) {
7505 conn = stream->conninfo.connection;
7511 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7512 ("Sending PAUSE request"));
7515 gst_rtspsrc_init_request (src, &request, GST_RTSP_PAUSE,
7517 goto create_request_failed;
7519 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7522 gst_rtsp_message_unset (&request);
7523 gst_rtsp_message_unset (&response);
7525 /* exit early when we did agregate control */
7530 /* change element states now */
7531 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7534 src->state = GST_RTSP_STATE_READY;
7538 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7545 GST_DEBUG_OBJECT (src, "failed to open stream");
7550 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7555 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7558 create_request_failed:
7560 gchar *str = gst_rtsp_strresult (res);
7562 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7563 ("Could not create request. (%s)", str));
7569 gchar *str = gst_rtsp_strresult (res);
7571 gst_rtsp_message_unset (&request);
7572 if (res != GST_RTSP_EINTR) {
7573 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7574 ("Could not send message. (%s)", str));
7576 GST_WARNING_OBJECT (src, "PAUSE interrupted");
7584 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
7586 GstRTSPSrc *rtspsrc;
7588 rtspsrc = GST_RTSPSRC (bin);
7590 switch (GST_MESSAGE_TYPE (message)) {
7591 case GST_MESSAGE_EOS:
7592 gst_message_unref (message);
7594 case GST_MESSAGE_ELEMENT:
7596 const GstStructure *s = gst_message_get_structure (message);
7598 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
7599 gboolean ignore_timeout;
7601 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
7603 GST_OBJECT_LOCK (rtspsrc);
7604 ignore_timeout = rtspsrc->ignore_timeout;
7605 rtspsrc->ignore_timeout = TRUE;
7606 GST_OBJECT_UNLOCK (rtspsrc);
7608 /* we only act on the first udp timeout message, others are irrelevant
7609 * and can be ignored. */
7610 if (!ignore_timeout)
7611 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
7613 gst_message_unref (message);
7616 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7619 case GST_MESSAGE_ERROR:
7622 GstRTSPStream *stream;
7625 udpsrc = GST_MESSAGE_SRC (message);
7627 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7628 GST_ELEMENT_NAME (udpsrc));
7630 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7634 /* we ignore the RTCP udpsrc */
7635 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7638 /* if we get error messages from the udp sources, that's not a problem as
7639 * long as not all of them error out. We also don't really know what the
7640 * problem is, the message does not give enough detail... */
7641 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7642 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7643 if (ret != GST_FLOW_OK)
7647 gst_message_unref (message);
7651 /* fatal but not our message, forward */
7652 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7657 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7663 /* the thread where everything happens */
7665 gst_rtspsrc_thread (GstRTSPSrc * src)
7669 GST_OBJECT_LOCK (src);
7670 cmd = src->pending_cmd;
7671 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7672 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7673 src->pending_cmd = CMD_LOOP;
7675 src->pending_cmd = CMD_WAIT;
7676 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
7678 /* we got the message command, so ensure communication is possible again */
7679 gst_rtspsrc_connection_flush (src, FALSE);
7681 src->busy_cmd = cmd;
7682 GST_OBJECT_UNLOCK (src);
7686 gst_rtspsrc_open (src, TRUE);
7689 gst_rtspsrc_play (src, &src->segment, TRUE);
7692 gst_rtspsrc_pause (src, TRUE);
7695 gst_rtspsrc_close (src, TRUE, FALSE);
7698 gst_rtspsrc_loop (src);
7701 gst_rtspsrc_reconnect (src, FALSE);
7707 GST_OBJECT_LOCK (src);
7708 /* and go back to sleep */
7709 if (src->pending_cmd == CMD_WAIT) {
7711 gst_task_pause (src->task);
7714 src->busy_cmd = CMD_WAIT;
7715 GST_OBJECT_UNLOCK (src);
7719 gst_rtspsrc_start (GstRTSPSrc * src)
7721 GST_DEBUG_OBJECT (src, "starting");
7723 GST_OBJECT_LOCK (src);
7725 src->pending_cmd = CMD_WAIT;
7727 if (src->task == NULL) {
7728 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7729 if (src->task == NULL)
7732 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7734 GST_OBJECT_UNLOCK (src);
7741 GST_OBJECT_UNLOCK (src);
7742 GST_ERROR_OBJECT (src, "failed to create task");
7748 gst_rtspsrc_stop (GstRTSPSrc * src)
7752 GST_DEBUG_OBJECT (src, "stopping");
7754 /* also cancels pending task */
7755 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7757 GST_OBJECT_LOCK (src);
7758 if ((task = src->task)) {
7760 GST_OBJECT_UNLOCK (src);
7762 gst_task_stop (task);
7764 /* make sure it is not running */
7765 GST_RTSP_STREAM_LOCK (src);
7766 GST_RTSP_STREAM_UNLOCK (src);
7768 /* now wait for the task to finish */
7769 gst_task_join (task);
7771 /* and free the task */
7772 gst_object_unref (GST_OBJECT (task));
7774 GST_OBJECT_LOCK (src);
7776 GST_OBJECT_UNLOCK (src);
7778 /* ensure synchronously all is closed and clean */
7779 gst_rtspsrc_close (src, FALSE, TRUE);
7784 static GstStateChangeReturn
7785 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
7787 GstRTSPSrc *rtspsrc;
7788 GstStateChangeReturn ret;
7790 rtspsrc = GST_RTSPSRC (element);
7792 switch (transition) {
7793 case GST_STATE_CHANGE_NULL_TO_READY:
7794 if (!gst_rtspsrc_start (rtspsrc))
7797 case GST_STATE_CHANGE_READY_TO_PAUSED:
7798 /* init some state */
7799 rtspsrc->cur_protocols = rtspsrc->protocols;
7800 /* first attempt, don't ignore timeouts */
7801 rtspsrc->ignore_timeout = FALSE;
7802 rtspsrc->open_error = FALSE;
7803 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
7805 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7806 set_manager_buffer_mode (rtspsrc);
7808 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7809 /* unblock the tcp tasks and make the loop waiting */
7810 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
7811 /* make sure it is waiting before we send PAUSE or PLAY below */
7812 GST_RTSP_STREAM_LOCK (rtspsrc);
7813 GST_RTSP_STREAM_UNLOCK (rtspsrc);
7816 case GST_STATE_CHANGE_PAUSED_TO_READY:
7822 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
7823 if (ret == GST_STATE_CHANGE_FAILURE)
7826 switch (transition) {
7827 case GST_STATE_CHANGE_NULL_TO_READY:
7828 ret = GST_STATE_CHANGE_SUCCESS;
7830 case GST_STATE_CHANGE_READY_TO_PAUSED:
7831 ret = GST_STATE_CHANGE_NO_PREROLL;
7833 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7834 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
7835 ret = GST_STATE_CHANGE_SUCCESS;
7837 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7838 /* send pause request and keep the idle task around */
7839 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
7840 ret = GST_STATE_CHANGE_NO_PREROLL;
7842 case GST_STATE_CHANGE_PAUSED_TO_READY:
7843 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
7844 ret = GST_STATE_CHANGE_SUCCESS;
7846 case GST_STATE_CHANGE_READY_TO_NULL:
7847 gst_rtspsrc_stop (rtspsrc);
7848 ret = GST_STATE_CHANGE_SUCCESS;
7851 /* Otherwise it's success, we don't want to return spurious
7852 * NO_PREROLL or ASYNC from internal elements as we care for
7853 * state changes ourselves here
7855 * This is to catch PAUSED->PAUSED and PLAYING->PLAYING transitions.
7857 if (GST_STATE_TRANSITION_NEXT (transition) == GST_STATE_PAUSED)
7858 ret = GST_STATE_CHANGE_NO_PREROLL;
7860 ret = GST_STATE_CHANGE_SUCCESS;
7869 GST_DEBUG_OBJECT (rtspsrc, "start failed");
7870 return GST_STATE_CHANGE_FAILURE;
7875 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
7878 GstRTSPSrc *rtspsrc;
7880 rtspsrc = GST_RTSPSRC (element);
7882 if (GST_EVENT_IS_DOWNSTREAM (event)) {
7883 res = gst_rtspsrc_push_event (rtspsrc, event);
7885 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
7892 /*** GSTURIHANDLER INTERFACE *************************************************/
7895 gst_rtspsrc_uri_get_type (GType type)
7900 static const gchar *const *
7901 gst_rtspsrc_uri_get_protocols (GType type)
7903 static const gchar *protocols[] =
7904 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
7905 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
7912 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
7914 GstRTSPSrc *src = GST_RTSPSRC (handler);
7916 /* FIXME: make thread-safe */
7917 return g_strdup (src->conninfo.location);
7921 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
7927 GstRTSPUrl *newurl = NULL;
7928 GstSDPMessage *sdp = NULL;
7930 src = GST_RTSPSRC (handler);
7932 /* same URI, we're fine */
7933 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
7936 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
7937 sres = gst_sdp_message_new (&sdp);
7941 GST_DEBUG_OBJECT (src, "parsing SDP message");
7942 sres = gst_sdp_message_parse_uri (uri, sdp);
7947 GST_DEBUG_OBJECT (src, "parsing URI");
7948 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
7952 /* if worked, free previous and store new url object along with the original
7954 GST_DEBUG_OBJECT (src, "configuring URI");
7955 g_free (src->conninfo.location);
7956 src->conninfo.location = g_strdup (uri);
7957 gst_rtsp_url_free (src->conninfo.url);
7958 src->conninfo.url = newurl;
7959 g_free (src->conninfo.url_str);
7961 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
7963 src->conninfo.url_str = NULL;
7966 gst_sdp_message_free (src->sdp);
7968 src->from_sdp = sdp != NULL;
7970 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
7971 GST_DEBUG_OBJECT (src, "request uri is: %s",
7972 GST_STR_NULL (src->conninfo.url_str));
7979 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
7984 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
7985 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7986 "Could not create SDP");
7991 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
7992 GST_STR_NULL (uri));
7993 gst_sdp_message_free (sdp);
7994 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8000 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
8001 GST_STR_NULL (uri), res);
8002 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8003 "Invalid RTSP URI");
8009 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
8011 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
8013 iface->get_type = gst_rtspsrc_uri_get_type;
8014 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
8015 iface->get_uri = gst_rtspsrc_uri_get_uri;
8016 iface->set_uri = gst_rtspsrc_uri_set_uri;