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);
909 gst_bin_set_suppressed_flags (GST_BIN (src),
910 GST_ELEMENT_FLAG_SOURCE | GST_ELEMENT_FLAG_SINK);
914 gst_rtspsrc_finalize (GObject * object)
918 rtspsrc = GST_RTSPSRC (object);
920 gst_rtsp_ext_list_free (rtspsrc->extensions);
921 g_free (rtspsrc->conninfo.location);
922 gst_rtsp_url_free (rtspsrc->conninfo.url);
923 g_free (rtspsrc->conninfo.url_str);
924 g_free (rtspsrc->user_id);
925 g_free (rtspsrc->user_pw);
926 g_free (rtspsrc->multi_iface);
927 g_free (rtspsrc->user_agent);
930 gst_sdp_message_free (rtspsrc->sdp);
933 if (rtspsrc->provided_clock)
934 gst_object_unref (rtspsrc->provided_clock);
937 gst_structure_free (rtspsrc->sdes);
939 if (rtspsrc->tls_database)
940 g_object_unref (rtspsrc->tls_database);
942 if (rtspsrc->tls_interaction)
943 g_object_unref (rtspsrc->tls_interaction);
946 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
947 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
949 G_OBJECT_CLASS (parent_class)->finalize (object);
953 gst_rtspsrc_provide_clock (GstElement * element)
955 GstRTSPSrc *src = GST_RTSPSRC (element);
958 if ((clock = src->provided_clock) != NULL)
959 gst_object_ref (clock);
964 /* a proxy string of the format [user:passwd@]host[:port] */
966 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
970 g_free (rtsp->proxy_user);
971 rtsp->proxy_user = NULL;
972 g_free (rtsp->proxy_passwd);
973 rtsp->proxy_passwd = NULL;
974 g_free (rtsp->proxy_host);
975 rtsp->proxy_host = NULL;
976 rtsp->proxy_port = 0;
983 /* we allow http:// in front but ignore it */
984 if (g_str_has_prefix (p, "http://"))
987 at = strchr (p, '@');
989 /* look for user:passwd */
990 col = strchr (proxy, ':');
991 if (col == NULL || col > at)
994 rtsp->proxy_user = g_strndup (p, col - p);
996 rtsp->proxy_passwd = g_strndup (col, at - col);
1001 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
1002 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
1003 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
1004 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
1005 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
1006 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
1007 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
1010 col = strchr (p, ':');
1013 /* everything before the colon is the hostname */
1014 rtsp->proxy_host = g_strndup (p, col - p);
1016 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
1018 rtsp->proxy_host = g_strdup (p);
1019 rtsp->proxy_port = 8080;
1025 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
1027 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
1028 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
1031 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
1033 rtspsrc->ptcp_timeout = NULL;
1037 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
1040 GstRTSPSrc *rtspsrc;
1042 rtspsrc = GST_RTSPSRC (object);
1046 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
1047 g_value_get_string (value), NULL);
1049 case PROP_PROTOCOLS:
1050 rtspsrc->protocols = g_value_get_flags (value);
1053 rtspsrc->debug = g_value_get_boolean (value);
1056 rtspsrc->retry = g_value_get_uint (value);
1059 rtspsrc->udp_timeout = g_value_get_uint64 (value);
1061 case PROP_TCP_TIMEOUT:
1062 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
1065 rtspsrc->latency = g_value_get_uint (value);
1067 case PROP_DROP_ON_LATENCY:
1068 rtspsrc->drop_on_latency = g_value_get_boolean (value);
1070 case PROP_CONNECTION_SPEED:
1071 rtspsrc->connection_speed = g_value_get_uint64 (value);
1073 case PROP_NAT_METHOD:
1074 rtspsrc->nat_method = g_value_get_enum (value);
1077 rtspsrc->do_rtcp = g_value_get_boolean (value);
1079 case PROP_DO_RTSP_KEEP_ALIVE:
1080 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
1083 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
1086 g_free (rtspsrc->prop_proxy_id);
1087 rtspsrc->prop_proxy_id = g_value_dup_string (value);
1090 g_free (rtspsrc->prop_proxy_pw);
1091 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
1093 case PROP_RTP_BLOCKSIZE:
1094 rtspsrc->rtp_blocksize = g_value_get_uint (value);
1097 g_free (rtspsrc->user_id);
1098 rtspsrc->user_id = g_value_dup_string (value);
1101 g_free (rtspsrc->user_pw);
1102 rtspsrc->user_pw = g_value_dup_string (value);
1104 case PROP_BUFFER_MODE:
1105 rtspsrc->buffer_mode = g_value_get_enum (value);
1107 case PROP_PORT_RANGE:
1111 str = g_value_get_string (value);
1112 if (sscanf (str, "%u-%u", &rtspsrc->client_port_range.min,
1113 &rtspsrc->client_port_range.max) != 2) {
1114 rtspsrc->client_port_range.min = 0;
1115 rtspsrc->client_port_range.max = 0;
1119 case PROP_UDP_BUFFER_SIZE:
1120 rtspsrc->udp_buffer_size = g_value_get_int (value);
1122 case PROP_SHORT_HEADER:
1123 rtspsrc->short_header = g_value_get_boolean (value);
1125 case PROP_PROBATION:
1126 rtspsrc->probation = g_value_get_uint (value);
1128 case PROP_UDP_RECONNECT:
1129 rtspsrc->udp_reconnect = g_value_get_boolean (value);
1131 case PROP_MULTICAST_IFACE:
1132 g_free (rtspsrc->multi_iface);
1134 if (g_value_get_string (value) == NULL)
1135 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1137 rtspsrc->multi_iface = g_value_dup_string (value);
1140 rtspsrc->ntp_sync = g_value_get_boolean (value);
1142 case PROP_USE_PIPELINE_CLOCK:
1143 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
1146 rtspsrc->sdes = g_value_dup_boxed (value);
1148 case PROP_TLS_VALIDATION_FLAGS:
1149 rtspsrc->tls_validation_flags = g_value_get_flags (value);
1151 case PROP_TLS_DATABASE:
1152 g_clear_object (&rtspsrc->tls_database);
1153 rtspsrc->tls_database = g_value_dup_object (value);
1155 case PROP_TLS_INTERACTION:
1156 g_clear_object (&rtspsrc->tls_interaction);
1157 rtspsrc->tls_interaction = g_value_dup_object (value);
1159 case PROP_DO_RETRANSMISSION:
1160 rtspsrc->do_retransmission = g_value_get_boolean (value);
1162 case PROP_NTP_TIME_SOURCE:
1163 rtspsrc->ntp_time_source = g_value_get_enum (value);
1165 case PROP_USER_AGENT:
1166 g_free (rtspsrc->user_agent);
1167 rtspsrc->user_agent = g_value_dup_string (value);
1169 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1170 rtspsrc->max_rtcp_rtp_time_diff = g_value_get_int (value);
1172 case PROP_RFC7273_SYNC:
1173 rtspsrc->rfc7273_sync = g_value_get_boolean (value);
1176 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1182 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1185 GstRTSPSrc *rtspsrc;
1187 rtspsrc = GST_RTSPSRC (object);
1191 g_value_set_string (value, rtspsrc->conninfo.location);
1193 case PROP_PROTOCOLS:
1194 g_value_set_flags (value, rtspsrc->protocols);
1197 g_value_set_boolean (value, rtspsrc->debug);
1200 g_value_set_uint (value, rtspsrc->retry);
1203 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1205 case PROP_TCP_TIMEOUT:
1209 timeout = ((guint64) rtspsrc->tcp_timeout.tv_sec) * G_USEC_PER_SEC +
1210 rtspsrc->tcp_timeout.tv_usec;
1211 g_value_set_uint64 (value, timeout);
1215 g_value_set_uint (value, rtspsrc->latency);
1217 case PROP_DROP_ON_LATENCY:
1218 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1220 case PROP_CONNECTION_SPEED:
1221 g_value_set_uint64 (value, rtspsrc->connection_speed);
1223 case PROP_NAT_METHOD:
1224 g_value_set_enum (value, rtspsrc->nat_method);
1227 g_value_set_boolean (value, rtspsrc->do_rtcp);
1229 case PROP_DO_RTSP_KEEP_ALIVE:
1230 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1236 if (rtspsrc->proxy_host) {
1238 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1242 g_value_take_string (value, str);
1246 g_value_set_string (value, rtspsrc->prop_proxy_id);
1249 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1251 case PROP_RTP_BLOCKSIZE:
1252 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1255 g_value_set_string (value, rtspsrc->user_id);
1258 g_value_set_string (value, rtspsrc->user_pw);
1260 case PROP_BUFFER_MODE:
1261 g_value_set_enum (value, rtspsrc->buffer_mode);
1263 case PROP_PORT_RANGE:
1267 if (rtspsrc->client_port_range.min != 0) {
1268 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1269 rtspsrc->client_port_range.max);
1273 g_value_take_string (value, str);
1276 case PROP_UDP_BUFFER_SIZE:
1277 g_value_set_int (value, rtspsrc->udp_buffer_size);
1279 case PROP_SHORT_HEADER:
1280 g_value_set_boolean (value, rtspsrc->short_header);
1282 case PROP_PROBATION:
1283 g_value_set_uint (value, rtspsrc->probation);
1285 case PROP_UDP_RECONNECT:
1286 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1288 case PROP_MULTICAST_IFACE:
1289 g_value_set_string (value, rtspsrc->multi_iface);
1292 g_value_set_boolean (value, rtspsrc->ntp_sync);
1294 case PROP_USE_PIPELINE_CLOCK:
1295 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1298 g_value_set_boxed (value, rtspsrc->sdes);
1300 case PROP_TLS_VALIDATION_FLAGS:
1301 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1303 case PROP_TLS_DATABASE:
1304 g_value_set_object (value, rtspsrc->tls_database);
1306 case PROP_TLS_INTERACTION:
1307 g_value_set_object (value, rtspsrc->tls_interaction);
1309 case PROP_DO_RETRANSMISSION:
1310 g_value_set_boolean (value, rtspsrc->do_retransmission);
1312 case PROP_NTP_TIME_SOURCE:
1313 g_value_set_enum (value, rtspsrc->ntp_time_source);
1315 case PROP_USER_AGENT:
1316 g_value_set_string (value, rtspsrc->user_agent);
1318 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1319 g_value_set_int (value, rtspsrc->max_rtcp_rtp_time_diff);
1321 case PROP_RFC7273_SYNC:
1322 g_value_set_boolean (value, rtspsrc->rfc7273_sync);
1325 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1331 find_stream_by_id (GstRTSPStream * stream, gint * id)
1333 if (stream->id == *id)
1340 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1342 /* ignore unconfigured channels here (e.g., those that
1343 * were explicitly skipped during SETUP) */
1344 if ((stream->channelpad[0] != NULL) &&
1345 (stream->channel[0] == *channel || stream->channel[1] == *channel))
1352 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1354 GstElement *src = (GstElement *) a;
1356 if (stream->udpsrc[0] == src)
1358 if (stream->udpsrc[1] == src)
1365 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1367 if (stream->conninfo.location) {
1368 /* check qualified setup_url */
1369 if (!strcmp (stream->conninfo.location, (gchar *) a))
1372 if (stream->control_url) {
1373 /* check original control_url */
1374 if (!strcmp (stream->control_url, (gchar *) a))
1377 /* check if qualified setup_url ends with string */
1378 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1385 static GstRTSPStream *
1386 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1390 /* find and get stream */
1391 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1392 return (GstRTSPStream *) lstream->data;
1397 static const GstSDPBandwidth *
1398 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1399 const GstSDPMedia * media, const gchar * type)
1403 /* first look in the media specific section */
1404 len = gst_sdp_media_bandwidths_len (media);
1405 for (i = 0; i < len; i++) {
1406 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1408 if (strcmp (bw->bwtype, type) == 0)
1411 /* then look in the message specific section */
1412 len = gst_sdp_message_bandwidths_len (sdp);
1413 for (i = 0; i < len; i++) {
1414 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1416 if (strcmp (bw->bwtype, type) == 0)
1423 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1424 const GstSDPMedia * media, GstRTSPStream * stream)
1426 const GstSDPBandwidth *bw;
1428 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1429 stream->as_bandwidth = bw->bandwidth;
1431 stream->as_bandwidth = -1;
1433 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1434 stream->rr_bandwidth = bw->bandwidth;
1436 stream->rr_bandwidth = -1;
1438 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1439 stream->rs_bandwidth = bw->bandwidth;
1441 stream->rs_bandwidth = -1;
1445 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1446 const GstSDPConnection * conn)
1448 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1451 if (conn->addrtype == NULL)
1454 /* check for IPV6 */
1455 if (strcmp (conn->addrtype, "IP4") == 0)
1456 stream->is_ipv6 = FALSE;
1457 else if (strcmp (conn->addrtype, "IP6") == 0)
1458 stream->is_ipv6 = TRUE;
1463 g_free (stream->destination);
1464 stream->destination = g_strdup (conn->address);
1466 /* check for multicast */
1467 stream->is_multicast =
1468 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1470 stream->ttl = conn->ttl;
1473 /* Go over the connections for a stream.
1474 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1476 * - If we are dealing with a localhost address, we disable multicast
1479 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1480 const GstSDPMedia * media, GstRTSPStream * stream)
1482 const GstSDPConnection *conn;
1485 /* first look in the media specific section */
1486 len = gst_sdp_media_connections_len (media);
1487 for (i = 0; i < len; i++) {
1488 conn = gst_sdp_media_get_connection (media, i);
1490 gst_rtspsrc_do_stream_connection (src, stream, conn);
1492 /* then look in the message specific section */
1493 if ((conn = gst_sdp_message_get_connection (sdp))) {
1494 gst_rtspsrc_do_stream_connection (src, stream, conn);
1498 /* m=<media> <UDP port> RTP/AVP <payload>
1501 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
1502 const GstSDPMedia * media, GstRTSPStream * stream)
1506 GstCaps *global_caps;
1509 proto = gst_sdp_media_get_proto (media);
1513 if (g_str_equal (proto, "RTP/AVP"))
1514 stream->profile = GST_RTSP_PROFILE_AVP;
1515 else if (g_str_equal (proto, "RTP/SAVP"))
1516 stream->profile = GST_RTSP_PROFILE_SAVP;
1517 else if (g_str_equal (proto, "RTP/AVPF"))
1518 stream->profile = GST_RTSP_PROFILE_AVPF;
1519 else if (g_str_equal (proto, "RTP/SAVPF"))
1520 stream->profile = GST_RTSP_PROFILE_SAVPF;
1524 /* Parse global SDP attributes once */
1525 global_caps = gst_caps_new_empty_simple ("application/x-unknown");
1526 GST_DEBUG ("mapping sdp session level attributes to caps");
1527 gst_sdp_message_attributes_to_caps (sdp, global_caps);
1528 GST_DEBUG ("mapping sdp media level attributes to caps");
1529 gst_sdp_media_attributes_to_caps (media, global_caps);
1531 /* Keep a copy of the SDP key management */
1532 gst_sdp_media_parse_keymgmt (media, &stream->mikey);
1533 if (stream->mikey == NULL)
1534 gst_sdp_message_parse_keymgmt (sdp, &stream->mikey);
1536 len = gst_sdp_media_formats_len (media);
1537 for (i = 0; i < len; i++) {
1539 GstCaps *caps, *outcaps;
1544 pt = atoi (gst_sdp_media_get_format (media, i));
1546 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
1549 caps = gst_sdp_media_get_caps_from_media (media, pt);
1551 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
1555 /* do some tweaks */
1556 s = gst_caps_get_structure (caps, 0);
1557 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
1558 stream->is_real = (strstr (enc, "-REAL") != NULL);
1559 if (strcmp (enc, "X-ASF-PF") == 0)
1560 stream->container = TRUE;
1563 /* Merge in global caps */
1564 /* Intersect will merge in missing fields to the current caps */
1565 outcaps = gst_caps_intersect (caps, global_caps);
1566 gst_caps_unref (caps);
1568 /* the first pt will be the default */
1569 if (stream->ptmap->len == 0)
1570 stream->default_pt = pt;
1573 item.caps = outcaps;
1575 g_array_append_val (stream->ptmap, item);
1578 gst_caps_unref (global_caps);
1583 GST_ERROR_OBJECT (src, "can't find proto in media");
1588 GST_ERROR_OBJECT (src, "unknown proto in media %s", proto);
1593 static const gchar *
1594 get_aggregate_control (GstRTSPSrc * src)
1599 base = src->control;
1600 else if (src->content_base)
1601 base = src->content_base;
1602 else if (src->conninfo.url_str)
1603 base = src->conninfo.url_str;
1611 clear_ptmap_item (PtMapItem * item)
1614 gst_caps_unref (item->caps);
1617 static GstRTSPStream *
1618 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx,
1621 GstRTSPStream *stream;
1622 const gchar *control_url;
1623 const GstSDPMedia *media;
1625 /* get media, should not return NULL */
1626 media = gst_sdp_message_get_media (sdp, idx);
1630 stream = g_new0 (GstRTSPStream, 1);
1631 stream->parent = src;
1632 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1634 stream->last_ret = GST_FLOW_NOT_LINKED;
1635 stream->added = FALSE;
1636 stream->setup = FALSE;
1637 stream->skipped = FALSE;
1639 stream->eos = FALSE;
1640 stream->discont = TRUE;
1641 stream->seqbase = -1;
1642 stream->timebase = -1;
1643 stream->send_ssrc = g_random_int ();
1644 stream->profile = GST_RTSP_PROFILE_AVP;
1645 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1646 stream->mikey = NULL;
1647 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
1649 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1650 * session manager to scale RTCP. */
1651 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1653 /* collect connection info */
1654 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1656 /* make the payload type map */
1657 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1659 /* collect port number */
1660 stream->port = gst_sdp_media_get_port (media);
1662 /* get control url to construct the setup url. The setup url is used to
1663 * configure the transport of the stream and is used to identity the stream in
1664 * the RTP-Info header field returned from PLAY. */
1665 control_url = gst_sdp_media_get_attribute_val (media, "control");
1666 if (control_url == NULL)
1667 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1669 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1670 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1671 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1672 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1674 /* RFC 2326, C.3: missing control_url permitted in case of a single stream */
1675 if (control_url == NULL && n_streams == 1) {
1679 if (control_url != NULL) {
1680 stream->control_url = g_strdup (control_url);
1681 /* Build a fully qualified url using the content_base if any or by prefixing
1682 * the original request.
1683 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1684 * likely build a URL that the server will fail to understand, this is ok,
1685 * we will fail then. */
1686 if (g_str_has_prefix (control_url, "rtsp://"))
1687 stream->conninfo.location = g_strdup (control_url);
1692 if (g_strcmp0 (control_url, "*") == 0)
1695 base = get_aggregate_control (src);
1697 /* check if the base ends or control starts with / */
1698 has_slash = g_str_has_prefix (control_url, "/");
1699 has_slash = has_slash || g_str_has_suffix (base, "/");
1701 /* concatenate the two strings, insert / when not present */
1702 stream->conninfo.location =
1703 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1706 GST_DEBUG_OBJECT (src, " setup: %s",
1707 GST_STR_NULL (stream->conninfo.location));
1709 /* we keep track of all streams */
1710 src->streams = g_list_append (src->streams, stream);
1718 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1722 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1724 g_array_free (stream->ptmap, TRUE);
1726 g_free (stream->destination);
1727 g_free (stream->control_url);
1728 g_free (stream->conninfo.location);
1730 for (i = 0; i < 2; i++) {
1731 if (stream->udpsrc[i]) {
1732 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1733 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1734 gst_object_unref (stream->udpsrc[i]);
1736 if (stream->channelpad[i])
1737 gst_object_unref (stream->channelpad[i]);
1739 if (stream->udpsink[i]) {
1740 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1741 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1742 gst_object_unref (stream->udpsink[i]);
1745 if (stream->fakesrc) {
1746 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1747 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1748 gst_object_unref (stream->fakesrc);
1750 if (stream->srcpad) {
1751 gst_pad_set_active (stream->srcpad, FALSE);
1753 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1755 if (stream->srtpenc)
1756 gst_object_unref (stream->srtpenc);
1757 if (stream->srtpdec)
1758 gst_object_unref (stream->srtpdec);
1759 if (stream->srtcpparams)
1760 gst_caps_unref (stream->srtcpparams);
1762 gst_mikey_message_unref (stream->mikey);
1763 if (stream->rtcppad)
1764 gst_object_unref (stream->rtcppad);
1765 if (stream->session)
1766 g_object_unref (stream->session);
1767 if (stream->rtx_pt_map)
1768 gst_structure_free (stream->rtx_pt_map);
1773 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1777 GST_DEBUG_OBJECT (src, "cleanup");
1779 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1780 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1782 gst_rtspsrc_stream_free (src, stream);
1784 g_list_free (src->streams);
1785 src->streams = NULL;
1787 if (src->manager_sig_id) {
1788 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1789 src->manager_sig_id = 0;
1791 gst_element_set_state (src->manager, GST_STATE_NULL);
1792 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1793 src->manager = NULL;
1796 gst_structure_free (src->props);
1799 g_free (src->content_base);
1800 src->content_base = NULL;
1802 g_free (src->control);
1803 src->control = NULL;
1806 gst_rtsp_range_free (src->range);
1809 /* don't clear the SDP when it was used in the url */
1810 if (src->sdp && !src->from_sdp) {
1811 gst_sdp_message_free (src->sdp);
1815 src->need_segment = FALSE;
1817 if (src->provided_clock) {
1818 gst_object_unref (src->provided_clock);
1819 src->provided_clock = NULL;
1824 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1825 gint * rtpport, gint * rtcpport)
1828 GstStateChangeReturn ret;
1829 GstElement *udpsrc0, *udpsrc1;
1830 gint tmp_rtp, tmp_rtcp;
1834 src = stream->parent;
1840 /* Start at next port */
1841 tmp_rtp = src->next_port_num;
1843 if (stream->is_ipv6)
1844 host = "udp://[::0]";
1846 host = "udp://0.0.0.0";
1848 /* try to allocate 2 UDP ports, the RTP port should be an even
1849 * number and the RTCP port should be the next (uneven) port */
1852 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1853 tmp_rtp >= src->client_port_range.max)
1856 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1857 if (udpsrc0 == NULL)
1858 goto no_udp_protocol;
1859 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1861 if (src->udp_buffer_size != 0)
1862 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
1865 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
1866 if (ret == GST_STATE_CHANGE_FAILURE) {
1868 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
1871 if (++count > src->retry)
1874 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1875 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1876 gst_object_unref (udpsrc0);
1879 GST_DEBUG_OBJECT (src, "retry %d", count);
1882 goto no_udp_protocol;
1885 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
1886 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
1888 /* check if port is even */
1889 if ((tmp_rtp & 0x01) != 0) {
1890 /* port not even, close and allocate another */
1891 if (++count > src->retry)
1894 GST_DEBUG_OBJECT (src, "RTP port not even");
1896 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1897 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1898 gst_object_unref (udpsrc0);
1901 GST_DEBUG_OBJECT (src, "retry %d", count);
1906 /* allocate port+1 for RTCP now */
1907 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1908 if (udpsrc1 == NULL)
1909 goto no_udp_rtcp_protocol;
1912 tmp_rtcp = tmp_rtp + 1;
1913 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
1916 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
1918 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
1919 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
1920 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
1921 if (ret == GST_STATE_CHANGE_FAILURE) {
1922 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
1924 if (++count > src->retry)
1927 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1928 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1929 gst_object_unref (udpsrc0);
1932 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
1933 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1934 gst_object_unref (udpsrc1);
1938 GST_DEBUG_OBJECT (src, "retry %d", count);
1942 /* all fine, do port check */
1943 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
1944 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
1946 /* this should not happen... */
1947 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
1950 /* we keep these elements, we configure all in configure_transport when the
1951 * server told us to really use the UDP ports. */
1952 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
1953 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
1954 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
1955 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
1957 /* keep track of next available port number when we have a range
1959 if (src->next_port_num != 0)
1960 src->next_port_num = tmp_rtcp + 1;
1967 GST_DEBUG_OBJECT (src, "could not get UDP source");
1972 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
1976 no_udp_rtcp_protocol:
1978 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
1983 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
1984 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
1990 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1991 gst_object_unref (udpsrc0);
1994 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1995 gst_object_unref (udpsrc1);
2002 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2007 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2009 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2010 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2013 for (i = 0; i < 2; i++) {
2014 if (stream->udpsrc[i])
2015 gst_element_set_state (stream->udpsrc[i], state);
2021 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2028 event = gst_event_new_flush_start ();
2029 GST_DEBUG_OBJECT (src, "start flush");
2031 state = GST_STATE_PAUSED;
2033 event = gst_event_new_flush_stop (FALSE);
2034 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2037 state = GST_STATE_PLAYING;
2039 state = GST_STATE_PAUSED;
2041 gst_rtspsrc_push_event (src, event);
2042 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2043 gst_rtspsrc_set_state (src, state);
2046 static GstRTSPResult
2047 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2048 GstRTSPMessage * message, GTimeVal * timeout)
2053 ret = gst_rtsp_connection_send (conn, message, timeout);
2055 ret = GST_RTSP_ERROR;
2060 static GstRTSPResult
2061 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2062 GstRTSPMessage * message, GTimeVal * timeout)
2067 ret = gst_rtsp_connection_receive (conn, message, timeout);
2069 ret = GST_RTSP_ERROR;
2075 gst_rtspsrc_get_position (GstRTSPSrc * src)
2080 query = gst_query_new_position (GST_FORMAT_TIME);
2081 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2082 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2083 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2087 if (stream->srcpad) {
2088 if (gst_pad_query (stream->srcpad, query)) {
2089 gst_query_parse_position (query, &fmt, &pos);
2090 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2091 GST_TIME_ARGS (pos));
2092 src->last_pos = pos;
2102 gst_query_unref (query);
2106 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2111 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2113 gboolean flush, skip;
2116 GstSegment seeksegment = { 0, };
2120 GST_DEBUG_OBJECT (src, "doing seek with event");
2122 gst_event_parse_seek (event, &rate, &format, &flags,
2123 &cur_type, &cur, &stop_type, &stop);
2125 /* no negative rates yet */
2129 /* we need TIME format */
2130 if (format != src->segment.format)
2133 GST_DEBUG_OBJECT (src, "doing seek without event");
2135 cur_type = GST_SEEK_TYPE_SET;
2136 stop_type = GST_SEEK_TYPE_SET;
2139 /* get flush flag */
2140 flush = flags & GST_SEEK_FLAG_FLUSH;
2141 skip = flags & GST_SEEK_FLAG_SKIP;
2143 /* now we need to make sure the streaming thread is stopped. We do this by
2144 * either sending a FLUSH_START event downstream which will cause the
2145 * streaming thread to stop with a WRONG_STATE.
2146 * For a non-flushing seek we simply pause the task, which will happen as soon
2147 * as it completes one iteration (and thus might block when the sink is
2148 * blocking in preroll). */
2150 GST_DEBUG_OBJECT (src, "starting flush");
2151 gst_rtspsrc_flush (src, TRUE, FALSE);
2154 gst_task_pause (src->task);
2158 /* we should now be able to grab the streaming thread because we stopped it
2159 * with the above flush/pause code */
2160 GST_RTSP_STREAM_LOCK (src);
2162 GST_DEBUG_OBJECT (src, "stopped streaming");
2164 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2165 gst_rtspsrc_connection_flush (src, FALSE);
2167 /* copy segment, we need this because we still need the old
2168 * segment when we close the current segment. */
2169 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2171 /* configure the seek parameters in the seeksegment. We will then have the
2172 * right values in the segment to perform the seek */
2174 GST_DEBUG_OBJECT (src, "configuring seek");
2175 gst_segment_do_seek (&seeksegment, rate, format, flags,
2176 cur_type, cur, stop_type, stop, &update);
2179 /* figure out the last position we need to play. If it's configured (stop !=
2180 * -1), use that, else we play until the total duration of the file */
2181 if ((stop = seeksegment.stop) == -1)
2182 stop = seeksegment.duration;
2184 /* if we were playing, pause first */
2185 playing = (src->state == GST_RTSP_STATE_PLAYING);
2187 /* obtain current position in case seek fails */
2188 gst_rtspsrc_get_position (src);
2189 gst_rtspsrc_pause (src, FALSE);
2193 src->state = GST_RTSP_STATE_SEEKING;
2195 /* PLAY will add the range header now. */
2196 src->need_range = TRUE;
2198 /* prepare for streaming again */
2200 /* if we started flush, we stop now */
2201 GST_DEBUG_OBJECT (src, "stopping flush");
2202 gst_rtspsrc_flush (src, FALSE, playing);
2205 /* now we did the seek and can activate the new segment values */
2206 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2208 /* if we're doing a segment seek, post a SEGMENT_START message */
2209 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2210 gst_element_post_message (GST_ELEMENT_CAST (src),
2211 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2212 src->segment.format, src->segment.position));
2215 /* now create the newsegment */
2216 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2217 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2220 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2221 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2222 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2223 stream->discont = TRUE;
2226 /* and continue playing if needed */
2227 GST_OBJECT_LOCK (src);
2228 playing = (GST_STATE_PENDING (src) == GST_STATE_VOID_PENDING
2229 && GST_STATE (src) == GST_STATE_PLAYING)
2230 || (GST_STATE_PENDING (src) == GST_STATE_PLAYING);
2231 GST_OBJECT_UNLOCK (src);
2233 gst_rtspsrc_play (src, &seeksegment, FALSE);
2235 GST_RTSP_STREAM_UNLOCK (src);
2242 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2247 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2253 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2257 gboolean res = TRUE;
2260 src = GST_RTSPSRC_CAST (parent);
2262 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2263 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2265 switch (GST_EVENT_TYPE (event)) {
2266 case GST_EVENT_SEEK:
2267 res = gst_rtspsrc_perform_seek (src, event);
2271 case GST_EVENT_NAVIGATION:
2272 case GST_EVENT_LATENCY:
2280 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2281 res = gst_pad_send_event (target, event);
2282 gst_object_unref (target);
2284 gst_event_unref (event);
2287 gst_event_unref (event);
2293 /* this is the final event function we receive on the internal source pad when
2294 * we deal with TCP connections */
2296 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2301 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2303 switch (GST_EVENT_TYPE (event)) {
2304 case GST_EVENT_SEEK:
2306 case GST_EVENT_NAVIGATION:
2307 case GST_EVENT_LATENCY:
2309 gst_event_unref (event);
2316 /* this is the final query function we receive on the internal source pad when
2317 * we deal with TCP connections */
2319 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2323 gboolean res = TRUE;
2325 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2327 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2328 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2330 switch (GST_QUERY_TYPE (query)) {
2331 case GST_QUERY_POSITION:
2336 case GST_QUERY_DURATION:
2340 gst_query_parse_duration (query, &format, NULL);
2343 case GST_FORMAT_TIME:
2344 gst_query_set_duration (query, format, src->segment.duration);
2352 case GST_QUERY_LATENCY:
2354 /* we are live with a min latency of 0 and unlimited max latency, this
2355 * result will be updated by the session manager if there is any. */
2356 gst_query_set_latency (query, TRUE, 0, -1);
2366 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2368 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2372 gboolean res = FALSE;
2374 src = GST_RTSPSRC_CAST (parent);
2376 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2377 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2379 switch (GST_QUERY_TYPE (query)) {
2380 case GST_QUERY_DURATION:
2384 gst_query_parse_duration (query, &format, NULL);
2387 case GST_FORMAT_TIME:
2388 gst_query_set_duration (query, format, src->segment.duration);
2396 case GST_QUERY_SEEKING:
2400 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2401 if (format == GST_FORMAT_TIME) {
2403 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2405 /* seeking without duration is unlikely */
2406 seekable = seekable && src->seekable && src->segment.duration &&
2407 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2409 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0,
2410 src->segment.duration);
2419 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2421 gst_query_set_uri (query, uri);
2429 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2431 /* forward the query to the proxy target pad */
2433 res = gst_pad_query (target, query);
2434 gst_object_unref (target);
2443 /* callback for RTCP messages to be sent to the server when operating in TCP
2445 static GstFlowReturn
2446 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2449 GstRTSPStream *stream;
2450 GstFlowReturn res = GST_FLOW_OK;
2455 GstRTSPMessage message = { 0 };
2456 GstRTSPConnection *conn;
2458 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2459 src = stream->parent;
2461 gst_buffer_map (buffer, &map, GST_MAP_READ);
2465 gst_rtsp_message_init_data (&message, stream->channel[1]);
2467 /* lend the body data to the message */
2468 gst_rtsp_message_take_body (&message, data, size);
2470 if (stream->conninfo.connection)
2471 conn = stream->conninfo.connection;
2473 conn = src->conninfo.connection;
2475 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2476 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2477 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2479 /* and steal it away again because we will free it when unreffing the
2481 gst_rtsp_message_steal_body (&message, &data, &size);
2482 gst_rtsp_message_unset (&message);
2484 gst_buffer_unmap (buffer, &map);
2485 gst_buffer_unref (buffer);
2490 static GstPadProbeReturn
2491 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2493 GstRTSPSrc *src = user_data;
2495 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2496 GST_DEBUG_PAD_NAME (pad));
2498 /* activate the streams */
2499 GST_OBJECT_LOCK (src);
2500 if (!src->need_activate)
2503 src->need_activate = FALSE;
2504 GST_OBJECT_UNLOCK (src);
2506 gst_rtspsrc_activate_streams (src);
2508 return GST_PAD_PROBE_OK;
2512 GST_OBJECT_UNLOCK (src);
2513 return GST_PAD_PROBE_OK;
2518 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
2520 GstPad *gpad = GST_PAD_CAST (user_data);
2522 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
2523 gst_pad_store_sticky_event (gpad, *event);
2528 /* this callback is called when the session manager generated a new src pad with
2529 * payloaded RTP packets. We simply ghost the pad here. */
2531 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2534 GstPadTemplate *template;
2537 GstRTSPStream *stream;
2540 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2542 GST_RTSP_STATE_LOCK (src);
2544 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2545 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2546 goto unknown_stream;
2548 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2550 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2552 goto unknown_stream;
2555 stream->ssrc = ssrc;
2557 /* we'll add it later see below */
2558 stream->added = TRUE;
2560 /* check if we added all streams */
2562 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2563 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2565 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
2566 ostream, ostream->container, ostream->added, ostream->setup);
2568 /* if we find a stream for which we did a setup that is not added, we
2569 * need to wait some more */
2570 if (ostream->setup && !ostream->added) {
2575 GST_RTSP_STATE_UNLOCK (src);
2577 /* create a new pad we will use to stream to */
2578 template = gst_static_pad_template_get (&rtptemplate);
2579 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2580 gst_object_unref (template);
2583 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2584 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2585 gst_pad_set_active (stream->srcpad, TRUE);
2586 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
2587 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2590 GST_DEBUG_OBJECT (src, "We added all streams");
2591 /* when we get here, all stream are added and we can fire the no-more-pads
2593 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2601 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2602 GST_RTSP_STATE_UNLOCK (src);
2609 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
2613 len = stream->ptmap->len;
2614 for (i = 0; i < len; i++) {
2615 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2623 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2625 GstRTSPStream *stream;
2628 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2630 GST_RTSP_STATE_LOCK (src);
2631 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2633 goto unknown_stream;
2635 if ((caps = stream_get_caps_for_pt (stream, pt)))
2636 gst_caps_ref (caps);
2637 GST_RTSP_STATE_UNLOCK (src);
2643 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2644 GST_RTSP_STATE_UNLOCK (src);
2650 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2652 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2658 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2664 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2670 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2672 GstRTSPSrc *src = stream->parent;
2675 g_object_get (source, "ssrc", &ssrc, NULL);
2677 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2678 ssrc, stream->ssrc, stream->id);
2680 if (ssrc == stream->ssrc)
2681 gst_rtspsrc_do_stream_eos (src, stream);
2685 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2687 GstRTSPSrc *src = stream->parent;
2690 g_object_get (source, "ssrc", &ssrc, NULL);
2692 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2693 ssrc, stream->ssrc, stream->id);
2695 if (ssrc == stream->ssrc)
2696 gst_rtspsrc_do_stream_eos (src, stream);
2700 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2702 GstRTSPStream *stream;
2704 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2706 /* get stream for session */
2707 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2709 gst_rtspsrc_do_stream_eos (src, stream);
2714 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2716 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2721 set_manager_buffer_mode (GstRTSPSrc * src)
2723 GObjectClass *klass;
2725 if (src->manager == NULL)
2728 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2730 if (!g_object_class_find_property (klass, "buffer-mode"))
2733 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2734 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2739 GST_DEBUG_OBJECT (src,
2740 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
2742 if (src->provided_clock) {
2743 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
2745 if (clock == src->provided_clock) {
2746 GST_DEBUG_OBJECT (src, "selected synced");
2747 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
2750 gst_object_unref (clock);
2755 /* Otherwise fall-through and use another buffer mode */
2757 gst_object_unref (clock);
2760 GST_DEBUG_OBJECT (src, "auto buffering mode");
2761 if (src->use_buffering) {
2762 GST_DEBUG_OBJECT (src, "selected buffer");
2763 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
2765 GST_DEBUG_OBJECT (src, "selected slave");
2766 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2771 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2775 GstMIKEYMessage *msg = stream->mikey;
2777 GST_DEBUG ("request key SSRC %u", ssrc);
2779 caps = gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
2780 caps = gst_caps_make_writable (caps);
2782 /* parse crypto sessions and look for the SSRC rollover counter */
2783 msg = stream->mikey;
2784 for (i = 0; msg && i < gst_mikey_message_get_n_cs (msg); i++) {
2785 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
2787 if (ssrc == map->ssrc) {
2788 gst_caps_set_simple (caps, "roc", G_TYPE_UINT, map->roc, NULL);
2797 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2799 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
2800 if (stream->id != session)
2803 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
2804 stream->profile != GST_RTSP_PROFILE_SAVPF)
2807 if (stream->srtpdec == NULL) {
2810 name = g_strdup_printf ("srtpdec_%u", session);
2811 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
2814 g_signal_connect (stream->srtpdec, "request-key",
2815 (GCallback) request_key, stream);
2817 return gst_object_ref (stream->srtpdec);
2821 request_rtcp_encoder (GstElement * rtpbin, guint session,
2822 GstRTSPStream * stream)
2827 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
2828 if (stream->id != session)
2831 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
2832 stream->profile != GST_RTSP_PROFILE_SAVPF)
2835 if (stream->srtpenc == NULL) {
2838 name = g_strdup_printf ("srtpenc_%u", session);
2839 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
2842 /* get RTCP crypto parameters from caps */
2843 s = gst_caps_get_structure (stream->srtcpparams, 0);
2847 GType ciphertype, authtype;
2848 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
2850 ciphertype = g_type_from_name ("GstSrtpCipherType");
2851 authtype = g_type_from_name ("GstSrtpAuthType");
2852 g_value_init (&rtcp_cipher, ciphertype);
2853 g_value_init (&rtcp_auth, authtype);
2855 str = gst_structure_get_string (s, "srtcp-cipher");
2856 gst_value_deserialize (&rtcp_cipher, str);
2857 str = gst_structure_get_string (s, "srtcp-auth");
2858 gst_value_deserialize (&rtcp_auth, str);
2859 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
2861 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-cipher",
2863 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-auth",
2865 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
2867 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
2869 g_object_set (stream->srtpenc, "key", buf, NULL);
2871 g_value_unset (&rtcp_cipher);
2872 g_value_unset (&rtcp_auth);
2873 gst_buffer_unref (buf);
2876 name = g_strdup_printf ("rtcp_sink_%d", session);
2877 pad = gst_element_get_request_pad (stream->srtpenc, name);
2879 gst_object_unref (pad);
2881 return gst_object_ref (stream->srtpenc);
2885 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
2887 GstElement *rtx, *bin;
2890 GstRTSPStream *stream;
2892 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
2894 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
2898 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
2899 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
2900 bin = gst_bin_new (NULL);
2901 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
2902 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
2903 gst_bin_add (GST_BIN (bin), rtx);
2905 pad = gst_element_get_static_pad (rtx, "src");
2906 name = g_strdup_printf ("src_%u", sessid);
2907 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2909 gst_object_unref (pad);
2911 pad = gst_element_get_static_pad (rtx, "sink");
2912 name = g_strdup_printf ("sink_%u", sessid);
2913 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2915 gst_object_unref (pad);
2921 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
2925 gboolean do_retransmission = FALSE;
2927 if (transport->trans != GST_RTSP_TRANS_RTP)
2929 if (transport->profile != GST_RTSP_PROFILE_AVPF &&
2930 transport->profile != GST_RTSP_PROFILE_SAVPF)
2933 signal_id = g_signal_lookup ("request-aux-receiver",
2934 G_OBJECT_TYPE (src->manager));
2935 /* there's already something connected */
2936 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
2937 NULL, NULL, NULL) != 0) {
2938 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
2939 "\"request-aux-receiver\" signal is "
2940 "already used by the application");
2944 /* build the retransmission payload type map */
2945 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2946 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2947 gboolean do_retransmission_stream = FALSE;
2950 if (stream->rtx_pt_map)
2951 gst_structure_free (stream->rtx_pt_map);
2952 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
2954 for (i = 0; i < stream->ptmap->len; i++) {
2955 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2956 GstStructure *s = gst_caps_get_structure (item->caps, 0);
2957 const gchar *encoding;
2959 /* we only care about RTX streams */
2960 if ((encoding = gst_structure_get_string (s, "encoding-name"))
2961 && g_strcmp0 (encoding, "RTX") == 0) {
2962 const gchar *stream_pt_s;
2965 if (gst_structure_get_int (s, "payload", &rtx_pt)
2966 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
2969 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
2971 do_retransmission_stream = TRUE;
2977 if (do_retransmission_stream) {
2978 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
2979 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
2980 do_retransmission = TRUE;
2982 GST_DEBUG_OBJECT (src, "no retransmission payload map for stream "
2983 "id %i", stream->id);
2984 gst_structure_free (stream->rtx_pt_map);
2985 stream->rtx_pt_map = NULL;
2989 if (do_retransmission) {
2990 GST_DEBUG_OBJECT (src, "Enabling retransmissions");
2992 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
2994 /* enable RFC4588 retransmission handling by setting rtprtxreceive
2995 * as the "aux" element of rtpbin */
2996 g_signal_connect (src->manager, "request-aux-receiver",
2997 (GCallback) request_aux_receiver, src);
2999 GST_DEBUG_OBJECT (src,
3000 "Not enabling retransmissions as no stream had a retransmission payload map");
3004 /* try to get and configure a manager */
3006 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3007 GstRTSPTransport * transport)
3009 const gchar *manager;
3011 GstStateChangeReturn ret;
3013 /* find a manager */
3014 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3018 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3020 /* configure the manager */
3021 if (src->manager == NULL) {
3022 GObjectClass *klass;
3024 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3026 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3030 goto use_no_manager;
3032 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3033 goto manager_failed;
3036 /* we manage this element */
3037 gst_element_set_locked_state (src->manager, TRUE);
3038 gst_bin_add (GST_BIN_CAST (src), src->manager);
3040 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3041 if (ret == GST_STATE_CHANGE_FAILURE)
3042 goto start_manager_failure;
3044 g_object_set (src->manager, "latency", src->latency, NULL);
3046 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3048 if (g_object_class_find_property (klass, "ntp-sync")) {
3049 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3052 if (g_object_class_find_property (klass, "rfc7273-sync")) {
3053 g_object_set (src->manager, "rfc7273-sync", src->rfc7273_sync, NULL);
3056 if (src->use_pipeline_clock) {
3057 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3058 g_object_set (src->manager, "use-pipeline-clock", TRUE, NULL);
3061 if (g_object_class_find_property (klass, "ntp-time-source")) {
3062 g_object_set (src->manager, "ntp-time-source", src->ntp_time_source,
3067 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3068 g_object_set (src->manager, "sdes", src->sdes, NULL);
3071 if (g_object_class_find_property (klass, "drop-on-latency")) {
3072 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3076 if (g_object_class_find_property (klass, "max-rtcp-rtp-time-diff")) {
3077 g_object_set (src->manager, "max-rtcp-rtp-time-diff",
3078 src->max_rtcp_rtp_time_diff, NULL);
3081 /* buffer mode pauses are handled by adding offsets to buffer times,
3082 * but some depayloaders may have a hard time syncing output times
3083 * with such input times, e.g. container ones, most notably ASF */
3084 /* TODO alternatives are having an event that indicates these shifts,
3085 * or having rtsp extensions provide suggestion on buffer mode */
3086 /* valid duration implies not likely live pipeline,
3087 * so slaving in jitterbuffer does not make much sense
3088 * (and might mess things up due to bursts) */
3089 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3090 src->segment.duration && stream->container) {
3091 src->use_buffering = TRUE;
3093 src->use_buffering = FALSE;
3096 set_manager_buffer_mode (src);
3098 /* connect to signals */
3099 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3101 src->manager_sig_id =
3102 g_signal_connect (src->manager, "pad-added",
3103 (GCallback) new_manager_pad, src);
3104 src->manager_ptmap_id =
3105 g_signal_connect (src->manager, "request-pt-map",
3106 (GCallback) request_pt_map, src);
3108 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3111 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3114 if (src->do_retransmission)
3115 add_retransmission (src, transport);
3117 g_signal_connect (src->manager, "request-rtp-decoder",
3118 (GCallback) request_rtp_decoder, stream);
3119 g_signal_connect (src->manager, "request-rtcp-decoder",
3120 (GCallback) request_rtp_decoder, stream);
3121 g_signal_connect (src->manager, "request-rtcp-encoder",
3122 (GCallback) request_rtcp_encoder, stream);
3124 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3125 * into a separate RTP session. */
3126 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3127 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3129 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3130 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3133 /* now configure the bandwidth in the manager */
3134 if (g_signal_lookup ("get-internal-session",
3135 G_OBJECT_TYPE (src->manager)) != 0) {
3136 GObject *rtpsession;
3138 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3141 GstRTPProfile rtp_profile;
3143 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3145 stream->session = rtpsession;
3147 if (stream->as_bandwidth != -1) {
3148 GST_INFO_OBJECT (src, "setting AS: %f",
3149 (gdouble) (stream->as_bandwidth * 1000));
3150 g_object_set (rtpsession, "bandwidth",
3151 (gdouble) (stream->as_bandwidth * 1000), NULL);
3153 if (stream->rr_bandwidth != -1) {
3154 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3155 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3158 if (stream->rs_bandwidth != -1) {
3159 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3160 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3164 switch (stream->profile) {
3165 case GST_RTSP_PROFILE_AVPF:
3166 rtp_profile = GST_RTP_PROFILE_AVPF;
3168 case GST_RTSP_PROFILE_SAVP:
3169 rtp_profile = GST_RTP_PROFILE_SAVP;
3171 case GST_RTSP_PROFILE_SAVPF:
3172 rtp_profile = GST_RTP_PROFILE_SAVPF;
3174 case GST_RTSP_PROFILE_AVP:
3176 rtp_profile = GST_RTP_PROFILE_AVP;
3180 g_object_set (rtpsession, "rtp-profile", rtp_profile, NULL);
3182 g_object_set (rtpsession, "probation", src->probation, NULL);
3184 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3186 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3188 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3190 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3192 g_signal_connect (rtpsession, "on-ssrc-active",
3193 (GCallback) on_ssrc_active, stream);
3204 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3209 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3212 start_manager_failure:
3214 GST_DEBUG_OBJECT (src, "could not start session manager");
3219 /* free the UDP sources allocated when negotiating a transport.
3220 * This function is called when the server negotiated to a transport where the
3221 * UDP sources are not needed anymore, such as TCP or multicast. */
3223 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3227 for (i = 0; i < 2; i++) {
3228 if (stream->udpsrc[i]) {
3229 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3230 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3231 gst_object_unref (stream->udpsrc[i]);
3232 stream->udpsrc[i] = NULL;
3237 /* for TCP, create pads to send and receive data to and from the manager and to
3238 * intercept various events and queries
3241 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3242 GstRTSPTransport * transport, GstPad ** outpad)
3245 GstPadTemplate *template;
3246 GstPad *pad0, *pad1;
3248 /* configure for interleaved delivery, nothing needs to be done
3249 * here, the loop function will call the chain functions of the
3250 * session manager. */
3251 stream->channel[0] = transport->interleaved.min;
3252 stream->channel[1] = transport->interleaved.max;
3253 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3254 stream->channel[0], stream->channel[1]);
3256 /* we can remove the allocated UDP ports now */
3257 gst_rtspsrc_stream_free_udp (stream);
3259 /* no session manager, send data to srcpad directly */
3260 if (!stream->channelpad[0]) {
3261 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3263 /* create a new pad we will use to stream to */
3264 name = g_strdup_printf ("stream_%u", stream->id);
3265 template = gst_static_pad_template_get (&rtptemplate);
3266 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3267 gst_object_unref (template);
3270 /* set caps and activate */
3271 gst_pad_use_fixed_caps (stream->channelpad[0]);
3272 gst_pad_set_active (stream->channelpad[0], TRUE);
3274 *outpad = gst_object_ref (stream->channelpad[0]);
3276 GST_DEBUG_OBJECT (src, "using manager source pad");
3278 template = gst_static_pad_template_get (&anysrctemplate);
3280 /* allocate pads for sending the channel data into the manager */
3281 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3282 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3283 gst_object_unref (stream->channelpad[0]);
3284 stream->channelpad[0] = pad0;
3285 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3286 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3287 gst_pad_set_element_private (pad0, src);
3288 gst_pad_set_active (pad0, TRUE);
3290 if (stream->channelpad[1]) {
3291 /* if we have a sinkpad for the other channel, create a pad and link to the
3293 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3294 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3295 gst_pad_link_full (pad1, stream->channelpad[1],
3296 GST_PAD_LINK_CHECK_NOTHING);
3297 gst_object_unref (stream->channelpad[1]);
3298 stream->channelpad[1] = pad1;
3299 gst_pad_set_active (pad1, TRUE);
3301 gst_object_unref (template);
3303 /* setup RTCP transport back to the server if we have to. */
3304 if (src->manager && src->do_rtcp) {
3307 template = gst_static_pad_template_get (&anysinktemplate);
3309 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3310 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3311 gst_pad_set_element_private (stream->rtcppad, stream);
3312 gst_pad_set_active (stream->rtcppad, TRUE);
3314 /* get session RTCP pad */
3315 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3316 pad = gst_element_get_request_pad (src->manager, name);
3321 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3322 gst_object_unref (pad);
3325 gst_object_unref (template);
3331 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3332 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3333 gint * max, guint * ttl)
3335 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3337 if (!(*destination = transport->destination))
3338 *destination = stream->destination;
3341 /* transport first */
3342 *min = transport->port.min;
3343 *max = transport->port.max;
3344 if (*min == -1 && *max == -1) {
3345 /* then try from SDP */
3346 if (stream->port != 0) {
3347 *min = stream->port;
3348 *max = stream->port + 1;
3354 if (!(*ttl = transport->ttl))
3359 /* first take the source, then the endpoint to figure out where to send
3361 if (!(*destination = transport->source)) {
3362 if (src->conninfo.connection)
3363 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3364 else if (stream->conninfo.connection)
3366 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3370 /* for unicast we only expect the ports here */
3371 *min = transport->server_port.min;
3372 *max = transport->server_port.max;
3377 /* For multicast create UDP sources and join the multicast group. */
3379 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3380 GstRTSPTransport * transport, GstPad ** outpad)
3383 const gchar *destination;
3386 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3388 /* we can remove the allocated UDP ports now */
3389 gst_rtspsrc_stream_free_udp (stream);
3391 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3394 /* we need a destination now */
3395 if (destination == NULL)
3396 goto no_destination;
3398 /* we really need ports now or we won't be able to receive anything at all */
3399 if (min == -1 && max == -1)
3402 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3403 destination, min, max);
3405 /* creating UDP source for RTP */
3407 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3409 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3411 if (stream->udpsrc[0] == NULL)
3414 /* take ownership */
3415 gst_object_ref_sink (stream->udpsrc[0]);
3417 if (src->udp_buffer_size != 0)
3418 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3419 src->udp_buffer_size, NULL);
3421 if (src->multi_iface != NULL)
3422 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3423 src->multi_iface, NULL);
3426 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3427 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3430 /* creating another UDP source for RTCP */
3434 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3436 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3438 if (stream->udpsrc[1] == NULL)
3441 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3442 stream->profile == GST_RTSP_PROFILE_SAVPF)
3443 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3445 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3446 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3447 gst_caps_unref (caps);
3449 /* take ownership */
3450 gst_object_ref_sink (stream->udpsrc[1]);
3452 if (src->multi_iface != NULL)
3453 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3454 src->multi_iface, NULL);
3456 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
3463 GST_DEBUG_OBJECT (src, "no UDP source element found");
3468 GST_DEBUG_OBJECT (src, "no destination found");
3473 GST_DEBUG_OBJECT (src, "no ports found");
3478 /* configure the remainder of the UDP ports */
3480 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3481 GstRTSPTransport * transport, GstPad ** outpad)
3483 /* we manage the UDP elements now. For unicast, the UDP sources where
3484 * allocated in the stream when we suggested a transport. */
3485 if (stream->udpsrc[0]) {
3488 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3489 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3491 GST_DEBUG_OBJECT (src, "setting up UDP source");
3493 /* configure a timeout on the UDP port. When the timeout message is
3494 * posted, we assume UDP transport is not possible. We reconnect using TCP
3496 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3497 src->udp_timeout * 1000, NULL);
3499 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
3500 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3502 /* get output pad of the UDP source. */
3503 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3505 /* save it so we can unblock */
3506 stream->blockedpad = *outpad;
3508 /* configure pad block on the pad. As soon as there is dataflow on the
3509 * UDP source, we know that UDP is not blocked by a firewall and we can
3510 * configure all the streams to let the application autoplug decoders. */
3512 gst_pad_add_probe (stream->blockedpad,
3513 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3514 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
3516 if (stream->channelpad[0]) {
3517 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3518 /* configure for UDP delivery, we need to connect the UDP pads to
3519 * the session plugin. */
3520 gst_pad_link_full (*outpad, stream->channelpad[0],
3521 GST_PAD_LINK_CHECK_NOTHING);
3522 gst_object_unref (*outpad);
3524 /* we connected to pad-added signal to get pads from the manager */
3526 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3531 if (stream->udpsrc[1]) {
3534 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3535 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3537 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3538 stream->profile == GST_RTSP_PROFILE_SAVPF)
3539 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3541 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3542 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3543 gst_caps_unref (caps);
3545 if (stream->channelpad[1]) {
3548 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3550 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3551 gst_pad_link_full (pad, stream->channelpad[1],
3552 GST_PAD_LINK_CHECK_NOTHING);
3553 gst_object_unref (pad);
3555 /* leave unlinked */
3561 /* configure the UDP sink back to the server for status reports */
3563 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3564 GstRTSPStream * stream, GstRTSPTransport * transport)
3567 gint rtp_port, rtcp_port;
3568 gboolean do_rtp, do_rtcp;
3569 const gchar *destination;
3574 /* get transport info */
3575 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3576 &rtp_port, &rtcp_port, &ttl);
3578 /* see what we need to do */
3579 do_rtp = (rtp_port != -1);
3580 /* it's possible that the server does not want us to send RTCP in which case
3582 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3584 /* we need a destination when we have RTP or RTCP ports */
3585 if (destination == NULL && (do_rtp || do_rtcp))
3586 goto no_destination;
3588 /* try to construct the fakesrc to the RTP port of the server to open up any
3591 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3594 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3595 stream->udpsink[0] =
3596 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3598 if (stream->udpsink[0] == NULL)
3599 goto no_sink_element;
3601 /* don't join multicast group, we will have the source socket do that */
3602 /* no sync or async state changes needed */
3603 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3604 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3606 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3608 if (stream->udpsrc[0]) {
3609 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3610 * so that NAT firewalls will open a hole for us */
3611 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3615 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3616 /* configure socket and make sure udpsink does not close it when shutting
3617 * down, it belongs to udpsrc after all. */
3618 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3619 "close-socket", FALSE, NULL);
3620 g_object_unref (socket);
3623 /* the source for the dummy packets to open up NAT */
3624 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3625 if (stream->fakesrc == NULL)
3626 goto no_fakesrc_element;
3628 /* random data in 5 buffers, a size of 200 bytes should be fine */
3629 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3630 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3632 /* keep everything locked */
3633 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3634 gst_element_set_locked_state (stream->fakesrc, TRUE);
3636 gst_object_ref (stream->udpsink[0]);
3637 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3638 gst_object_ref (stream->fakesrc);
3639 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3641 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3642 "sink", GST_PAD_LINK_CHECK_NOTHING);
3645 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3648 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3649 stream->udpsink[1] =
3650 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3652 if (stream->udpsink[1] == NULL)
3653 goto no_sink_element;
3655 /* don't join multicast group, we will have the source socket do that */
3656 /* no sync or async state changes needed */
3657 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3658 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3660 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3662 if (stream->udpsrc[1]) {
3663 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3664 * because some servers check the port number of where it sends RTCP to identify
3665 * the RTCP packets it receives */
3666 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3670 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3671 /* configure socket and make sure udpsink does not close it when shutting
3672 * down, it belongs to udpsrc after all. */
3673 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3674 "close-socket", FALSE, NULL);
3675 g_object_unref (socket);
3678 /* we keep this playing always */
3679 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3680 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3682 gst_object_ref (stream->udpsink[1]);
3683 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3685 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3687 /* get session RTCP pad */
3688 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3689 pad = gst_element_get_request_pad (src->manager, name);
3694 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3695 gst_object_unref (pad);
3704 GST_ERROR_OBJECT (src, "no destination address specified");
3709 GST_ERROR_OBJECT (src, "no UDP sink element found");
3714 GST_ERROR_OBJECT (src, "no fakesrc element found");
3719 GST_ERROR_OBJECT (src, "failed to create socket");
3724 /* sets up all elements needed for streaming over the specified transport.
3725 * Does not yet expose the element pads, this will be done when there is actuall
3726 * dataflow detected, which might never happen when UDP is blocked in a
3727 * firewall, for example.
3730 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3731 GstRTSPTransport * transport)
3734 GstPad *outpad = NULL;
3735 GstPadTemplate *template;
3737 const gchar *media_type;
3740 src = stream->parent;
3742 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3744 /* get the proper media type for this stream now */
3745 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
3746 goto unknown_transport;
3748 goto unknown_transport;
3750 /* configure the final media type */
3751 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
3753 len = stream->ptmap->len;
3754 for (i = 0; i < len; i++) {
3756 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3758 if (item->caps == NULL)
3761 s = gst_caps_get_structure (item->caps, 0);
3762 gst_structure_set_name (s, media_type);
3763 /* set ssrc if known */
3764 if (transport->ssrc)
3765 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
3768 /* try to get and configure a manager, channelpad[0-1] will be configured with
3769 * the pads for the manager, or NULL when no manager is needed. */
3770 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3773 switch (transport->lower_transport) {
3774 case GST_RTSP_LOWER_TRANS_TCP:
3775 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3776 goto transport_failed;
3778 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3779 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3780 goto transport_failed;
3781 /* fallthrough, the rest is the same for UDP and MCAST */
3782 case GST_RTSP_LOWER_TRANS_UDP:
3783 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3784 goto transport_failed;
3785 /* configure udpsinks back to the server for RTCP messages and for the
3786 * dummy RTP messages to open NAT. */
3787 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3788 goto transport_failed;
3791 goto unknown_transport;
3795 GST_DEBUG_OBJECT (src, "creating ghostpad");
3797 gst_pad_use_fixed_caps (outpad);
3799 /* create ghostpad, don't add just yet, this will be done when we activate
3801 name = g_strdup_printf ("stream_%u", stream->id);
3802 template = gst_static_pad_template_get (&rtptemplate);
3803 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3804 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3805 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3806 gst_object_unref (template);
3809 gst_object_unref (outpad);
3811 /* mark pad as ok */
3812 stream->last_ret = GST_FLOW_OK;
3819 GST_DEBUG_OBJECT (src, "failed to configure transport");
3824 GST_DEBUG_OBJECT (src, "unknown transport");
3829 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3834 /* send a couple of dummy random packets on the receiver RTP port to the server,
3835 * this should make a firewall think we initiated the data transfer and
3836 * hopefully allow packets to go from the sender port to our RTP receiver port */
3838 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3842 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3845 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3846 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3848 if (stream->fakesrc && stream->udpsink[0]) {
3849 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3850 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3851 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3852 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3853 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3859 /* Adds the source pads of all configured streams to the element.
3860 * This code is performed when we detected dataflow.
3862 * We detect dataflow from either the _loop function or with pad probes on the
3866 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3870 GST_DEBUG_OBJECT (src, "activating streams");
3872 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3873 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3875 if (stream->udpsrc[0]) {
3876 /* remove timeout, we are streaming now and timeouts will be handled by
3877 * the session manager and jitter buffer */
3878 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3880 if (stream->srcpad) {
3881 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3882 gst_pad_set_active (stream->srcpad, TRUE);
3884 /* if we don't have a session manager, set the caps now. If we have a
3885 * session, we will get a notification of the pad and the caps. */
3886 if (!src->manager) {
3889 caps = stream_get_caps_for_pt (stream, stream->default_pt);
3890 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3891 gst_pad_set_caps (stream->srcpad, caps);
3894 if (!stream->added) {
3895 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3896 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3897 stream->added = TRUE;
3902 /* unblock all pads */
3903 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3904 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3906 if (stream->blockid) {
3907 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3908 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3909 stream->blockid = 0;
3917 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
3918 gboolean reset_manager)
3921 guint64 start, stop;
3922 gdouble play_speed, play_scale;
3924 GST_DEBUG_OBJECT (src, "configuring stream caps");
3926 start = segment->position;
3927 stop = segment->duration;
3928 play_speed = segment->rate;
3929 play_scale = segment->applied_rate;
3931 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3932 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3938 len = stream->ptmap->len;
3939 for (j = 0; j < len; j++) {
3941 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
3943 if (item->caps == NULL)
3946 caps = gst_caps_make_writable (item->caps);
3948 if (stream->timebase != -1)
3949 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3950 (guint) stream->timebase, NULL);
3951 if (stream->seqbase != -1)
3952 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3953 (guint) stream->seqbase, NULL);
3954 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3956 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3957 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3958 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3961 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
3964 if (item->pt == stream->default_pt) {
3965 if (stream->udpsrc[0])
3966 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3967 stream->need_caps = TRUE;
3971 if (reset_manager && src->manager) {
3972 GST_DEBUG_OBJECT (src, "clear session");
3973 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3977 static GstFlowReturn
3978 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3983 /* store the value */
3984 stream->last_ret = ret;
3986 /* if it's success we can return the value right away */
3987 if (ret == GST_FLOW_OK)
3990 /* any other error that is not-linked can be returned right
3992 if (ret != GST_FLOW_NOT_LINKED)
3995 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3996 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3997 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3999 ret = ostream->last_ret;
4000 /* some other return value (must be SUCCESS but we can return
4001 * other values as well) */
4002 if (ret != GST_FLOW_NOT_LINKED)
4005 /* if we get here, all other pads were unlinked and we return
4006 * NOT_LINKED then */
4012 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4015 gboolean res = TRUE;
4017 /* only streams that have a connection to the outside world */
4021 if (stream->udpsrc[0]) {
4022 gst_event_ref (event);
4023 res = gst_element_send_event (stream->udpsrc[0], event);
4024 } else if (stream->channelpad[0]) {
4025 gst_event_ref (event);
4026 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4027 res = gst_pad_push_event (stream->channelpad[0], event);
4029 res = gst_pad_send_event (stream->channelpad[0], event);
4032 if (stream->udpsrc[1]) {
4033 gst_event_ref (event);
4034 res &= gst_element_send_event (stream->udpsrc[1], event);
4035 } else if (stream->channelpad[1]) {
4036 gst_event_ref (event);
4037 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4038 res &= gst_pad_push_event (stream->channelpad[1], event);
4040 res &= gst_pad_send_event (stream->channelpad[1], event);
4044 gst_event_unref (event);
4050 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4053 gboolean res = TRUE;
4055 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4056 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4058 gst_event_ref (event);
4059 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4061 gst_event_unref (event);
4066 static GstRTSPResult
4067 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4071 GstRTSPMessage response;
4072 gboolean retry = FALSE;
4073 memset (&response, 0, sizeof (response));
4074 gst_rtsp_message_init (&response);
4076 if (info->connection == NULL) {
4077 if (info->url == NULL) {
4078 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4079 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4082 /* create connection */
4083 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4084 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4085 goto could_not_create;
4088 gst_rtspsrc_setup_auth (src, &response);
4091 g_free (info->url_str);
4092 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4094 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4096 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4097 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4098 src->tls_validation_flags))
4099 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4101 if (src->tls_database)
4102 gst_rtsp_connection_set_tls_database (info->connection,
4105 if (src->tls_interaction)
4106 gst_rtsp_connection_set_tls_interaction (info->connection,
4107 src->tls_interaction);
4110 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4111 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4113 if (src->proxy_host) {
4114 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4116 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4121 if (!info->connected) {
4124 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4125 ("Connecting to %s", info->location));
4126 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4127 res = gst_rtsp_connection_connect_with_response (info->connection,
4128 src->ptcp_timeout, &response);
4130 if (response.type == GST_RTSP_MESSAGE_HTTP_RESPONSE &&
4131 response.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4132 gst_rtsp_conninfo_close (src, info, TRUE);
4136 retry = FALSE; // we should not retry more than once
4141 if (res == GST_RTSP_OK)
4142 info->connected = TRUE;
4144 goto could_not_connect;
4146 } while (!info->connected && retry);
4147 gst_rtsp_message_unset (&response);
4153 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4154 gst_rtsp_message_unset (&response);
4159 gchar *str = gst_rtsp_strresult (res);
4160 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4162 gst_rtsp_message_unset (&response);
4167 gchar *str = gst_rtsp_strresult (res);
4168 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4170 gst_rtsp_message_unset (&response);
4175 static GstRTSPResult
4176 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4179 GST_RTSP_STATE_LOCK (src);
4180 if (info->connected) {
4181 GST_DEBUG_OBJECT (src, "closing connection...");
4182 gst_rtsp_connection_close (info->connection);
4183 info->connected = FALSE;
4185 if (free && info->connection) {
4186 /* free connection */
4187 GST_DEBUG_OBJECT (src, "freeing connection...");
4188 gst_rtsp_connection_free (info->connection);
4189 info->connection = NULL;
4190 info->flushing = FALSE;
4192 GST_RTSP_STATE_UNLOCK (src);
4196 static GstRTSPResult
4197 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4202 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4203 gst_rtsp_conninfo_close (src, info, FALSE);
4204 res = gst_rtsp_conninfo_connect (src, info, async);
4210 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4214 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4215 GST_RTSP_STATE_LOCK (src);
4216 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4217 GST_DEBUG_OBJECT (src, "connection flush");
4218 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4219 src->conninfo.flushing = flush;
4221 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4222 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4223 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4224 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4225 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4226 stream->conninfo.flushing = flush;
4229 GST_RTSP_STATE_UNLOCK (src);
4232 static GstRTSPResult
4233 gst_rtspsrc_init_request (GstRTSPSrc * src, GstRTSPMessage * msg,
4234 GstRTSPMethod method, const gchar * uri)
4238 res = gst_rtsp_message_init_request (msg, method, uri);
4242 /* set user-agent */
4243 if (src->user_agent)
4244 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, src->user_agent);
4249 /* FIXME, handle server request, reply with OK, for now */
4250 static GstRTSPResult
4251 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
4252 GstRTSPMessage * request)
4254 GstRTSPMessage response = { 0 };
4257 GST_DEBUG_OBJECT (src, "got server request message");
4260 gst_rtsp_message_dump (request);
4262 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4264 if (res == GST_RTSP_ENOTIMPL) {
4265 /* default implementation, send OK */
4266 GST_DEBUG_OBJECT (src, "prepare OK reply");
4268 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4273 /* let app parse and reply */
4274 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4275 0, request, &response);
4278 gst_rtsp_message_dump (&response);
4280 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
4284 gst_rtsp_message_unset (&response);
4285 } else if (res == GST_RTSP_EEOF)
4293 gst_rtsp_message_unset (&response);
4298 /* send server keep-alive */
4299 static GstRTSPResult
4300 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4302 GstRTSPMessage request = { 0 };
4304 GstRTSPMethod method;
4305 const gchar *control;
4307 if (src->do_rtsp_keep_alive == FALSE) {
4308 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4309 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4313 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4315 /* find a method to use for keep-alive */
4316 if (src->methods & GST_RTSP_GET_PARAMETER)
4317 method = GST_RTSP_GET_PARAMETER;
4319 method = GST_RTSP_OPTIONS;
4321 control = get_aggregate_control (src);
4322 if (control == NULL)
4325 res = gst_rtspsrc_init_request (src, &request, method, control);
4330 gst_rtsp_message_dump (&request);
4333 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
4338 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4339 gst_rtsp_message_unset (&request);
4346 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4351 gchar *str = gst_rtsp_strresult (res);
4353 gst_rtsp_message_unset (&request);
4354 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4355 ("Could not send keep-alive. (%s)", str));
4361 static GstFlowReturn
4362 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4364 GstFlowReturn ret = GST_FLOW_OK;
4366 GstRTSPStream *stream;
4367 GstPad *outpad = NULL;
4373 channel = message->type_data.data.channel;
4375 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4377 goto unknown_stream;
4379 if (channel == stream->channel[0]) {
4380 outpad = stream->channelpad[0];
4382 } else if (channel == stream->channel[1]) {
4383 outpad = stream->channelpad[1];
4389 /* take a look at the body to figure out what we have */
4390 gst_rtsp_message_get_body (message, &data, &size);
4392 goto invalid_length;
4394 /* channels are not correct on some servers, do extra check */
4395 if (data[1] >= 200 && data[1] <= 204) {
4396 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4397 outpad = stream->channelpad[1];
4401 /* we have no clue what this is, just ignore then. */
4403 goto unknown_stream;
4405 /* take the message body for further processing */
4406 gst_rtsp_message_steal_body (message, &data, &size);
4408 /* strip the trailing \0 */
4411 buf = gst_buffer_new ();
4412 gst_buffer_append_memory (buf,
4413 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4415 /* don't need message anymore */
4416 gst_rtsp_message_unset (message);
4418 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4421 if (src->need_activate) {
4427 guint group_id = gst_util_group_id_next ();
4429 /* generate an SHA256 sum of the URI */
4430 cs = g_checksum_new (G_CHECKSUM_SHA256);
4431 uri = src->conninfo.location;
4432 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4434 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4435 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4439 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4440 event = gst_event_new_stream_start (stream_id);
4441 gst_event_set_group_id (event, group_id);
4444 gst_rtspsrc_stream_push_event (src, ostream, event);
4446 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
4447 /* only streams that have a connection to the outside world */
4448 if (ostream->setup) {
4449 if (ostream->udpsrc[0]) {
4450 gst_element_send_event (ostream->udpsrc[0],
4451 gst_event_new_caps (caps));
4452 } else if (ostream->channelpad[0]) {
4453 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
4454 gst_pad_push_event (ostream->channelpad[0],
4455 gst_event_new_caps (caps));
4457 gst_pad_send_event (ostream->channelpad[0],
4458 gst_event_new_caps (caps));
4460 ostream->need_caps = FALSE;
4462 if (ostream->profile == GST_RTSP_PROFILE_SAVP ||
4463 ostream->profile == GST_RTSP_PROFILE_SAVPF)
4464 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4466 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4468 if (ostream->udpsrc[1]) {
4469 gst_element_send_event (ostream->udpsrc[1],
4470 gst_event_new_caps (caps));
4471 } else if (ostream->channelpad[1]) {
4472 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
4473 gst_pad_push_event (ostream->channelpad[1],
4474 gst_event_new_caps (caps));
4476 gst_pad_send_event (ostream->channelpad[1],
4477 gst_event_new_caps (caps));
4480 gst_caps_unref (caps);
4484 g_checksum_free (cs);
4486 gst_rtspsrc_activate_streams (src);
4487 src->need_activate = FALSE;
4488 src->need_segment = TRUE;
4491 if (src->base_time == -1) {
4492 /* Take current running_time. This timestamp will be put on
4493 * the first buffer of each stream because we are a live source and so we
4494 * timestamp with the running_time. When we are dealing with TCP, we also
4495 * only timestamp the first buffer (using the DISCONT flag) because a server
4496 * typically bursts data, for which we don't want to compensate by speeding
4497 * up the media. The other timestamps will be interpollated from this one
4498 * using the RTP timestamps. */
4499 GST_OBJECT_LOCK (src);
4500 if (GST_ELEMENT_CLOCK (src)) {
4502 GstClockTime base_time;
4504 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4505 base_time = GST_ELEMENT_CAST (src)->base_time;
4507 src->base_time = now - base_time;
4509 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4510 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4512 GST_OBJECT_UNLOCK (src);
4515 /* If needed send a new segment, don't forget we are live and buffer are
4516 * timestamped with running time */
4517 if (src->need_segment) {
4519 src->need_segment = FALSE;
4520 gst_segment_init (&segment, GST_FORMAT_TIME);
4521 gst_rtspsrc_push_event (src, gst_event_new_segment (&segment));
4524 if (stream->need_caps) {
4527 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt))) {
4528 /* only streams that have a connection to the outside world */
4529 if (stream->setup) {
4530 /* Only need to update the TCP caps here, UDP is already handled */
4531 if (stream->channelpad[0]) {
4532 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4533 gst_pad_push_event (stream->channelpad[0],
4534 gst_event_new_caps (caps));
4536 gst_pad_send_event (stream->channelpad[0],
4537 gst_event_new_caps (caps));
4539 stream->need_caps = FALSE;
4543 stream->need_caps = FALSE;
4546 if (stream->discont && !is_rtcp) {
4547 /* mark first RTP buffer as discont */
4548 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4549 stream->discont = FALSE;
4550 /* first buffer gets the timestamp, other buffers are not timestamped and
4551 * their presentation time will be interpollated from the rtp timestamps. */
4552 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4553 GST_TIME_ARGS (src->base_time));
4555 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4558 /* chain to the peer pad */
4559 if (GST_PAD_IS_SINK (outpad))
4560 ret = gst_pad_chain (outpad, buf);
4562 ret = gst_pad_push (outpad, buf);
4565 /* combine all stream flows for the data transport */
4566 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4573 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4574 gst_rtsp_message_unset (message);
4579 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4580 ("Short message received, ignoring."));
4581 gst_rtsp_message_unset (message);
4586 static GstFlowReturn
4587 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4589 GstRTSPMessage message = { 0 };
4591 GstFlowReturn ret = GST_FLOW_OK;
4592 GTimeVal tv_timeout;
4595 /* get the next timeout interval */
4596 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4598 /* see if the timeout period expired */
4599 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4600 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4601 /* send keep-alive, only act on interrupt, a warning will be posted for
4603 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4605 /* get new timeout */
4606 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4609 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4610 tv_timeout.tv_sec, tv_timeout.tv_usec);
4612 /* protect the connection with the connection lock so that we can see when
4613 * we are finished doing server communication */
4615 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4616 &message, src->ptcp_timeout);
4620 GST_DEBUG_OBJECT (src, "we received a server message");
4622 case GST_RTSP_EINTR:
4623 /* we got interrupted this means we need to stop */
4625 case GST_RTSP_ETIMEOUT:
4626 /* no reply, send keep alive */
4627 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4628 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4632 /* go EOS when the server closed the connection */
4638 switch (message.type) {
4639 case GST_RTSP_MESSAGE_REQUEST:
4640 /* server sends us a request message, handle it */
4642 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4644 if (res == GST_RTSP_EEOF)
4647 goto handle_request_failed;
4649 case GST_RTSP_MESSAGE_RESPONSE:
4650 /* we ignore response messages */
4651 GST_DEBUG_OBJECT (src, "ignoring response message");
4653 gst_rtsp_message_dump (&message);
4655 case GST_RTSP_MESSAGE_DATA:
4656 GST_DEBUG_OBJECT (src, "got data message");
4657 ret = gst_rtspsrc_handle_data (src, &message);
4658 if (ret != GST_FLOW_OK)
4659 goto handle_data_failed;
4662 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4667 g_assert_not_reached ();
4672 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4673 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4674 ("The server closed the connection."));
4675 src->conninfo.connected = FALSE;
4676 gst_rtsp_message_unset (&message);
4677 return GST_FLOW_EOS;
4681 gst_rtsp_message_unset (&message);
4682 GST_DEBUG_OBJECT (src, "got interrupted");
4683 return GST_FLOW_FLUSHING;
4687 gchar *str = gst_rtsp_strresult (res);
4689 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4690 ("Could not receive message. (%s)", str));
4693 gst_rtsp_message_unset (&message);
4694 return GST_FLOW_ERROR;
4696 handle_request_failed:
4698 gchar *str = gst_rtsp_strresult (res);
4700 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4701 ("Could not handle server message. (%s)", str));
4703 gst_rtsp_message_unset (&message);
4704 return GST_FLOW_ERROR;
4708 GST_DEBUG_OBJECT (src, "could no handle data message");
4713 static GstFlowReturn
4714 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4717 GstRTSPMessage message = { 0 };
4721 GTimeVal tv_timeout;
4723 /* get the next timeout interval */
4724 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4726 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4727 (gint) tv_timeout.tv_sec);
4729 gst_rtsp_message_unset (&message);
4731 /* we should continue reading the TCP socket because the server might
4732 * send us requests. When the session timeout expires, we need to send a
4733 * keep-alive request to keep the session open. */
4734 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4735 &message, &tv_timeout);
4739 GST_DEBUG_OBJECT (src, "we received a server message");
4741 case GST_RTSP_EINTR:
4742 /* we got interrupted, see what we have to do */
4744 case GST_RTSP_ETIMEOUT:
4745 /* send keep-alive, ignore the result, a warning will be posted. */
4746 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4747 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4751 /* server closed the connection. not very fatal for UDP, reconnect and
4752 * see what happens. */
4753 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4754 ("The server closed the connection."));
4755 if (src->udp_reconnect) {
4757 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4764 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4766 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4767 ("Unhandled return value %d.", res));
4771 switch (message.type) {
4772 case GST_RTSP_MESSAGE_REQUEST:
4773 /* server sends us a request message, handle it */
4775 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4777 if (res == GST_RTSP_EEOF)
4780 goto handle_request_failed;
4782 case GST_RTSP_MESSAGE_RESPONSE:
4783 /* we ignore response and data messages */
4784 GST_DEBUG_OBJECT (src, "ignoring response message");
4786 gst_rtsp_message_dump (&message);
4787 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4788 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4789 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4790 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4791 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4798 case GST_RTSP_MESSAGE_DATA:
4799 /* we ignore response and data messages */
4800 GST_DEBUG_OBJECT (src, "ignoring data message");
4803 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4808 g_assert_not_reached ();
4810 /* we get here when the connection got interrupted */
4813 gst_rtsp_message_unset (&message);
4814 GST_DEBUG_OBJECT (src, "got interrupted");
4815 return GST_FLOW_FLUSHING;
4819 gchar *str = gst_rtsp_strresult (res);
4822 src->conninfo.connected = FALSE;
4823 if (res != GST_RTSP_EINTR) {
4824 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4825 ("Could not connect to server. (%s)", str));
4827 ret = GST_FLOW_ERROR;
4829 ret = GST_FLOW_FLUSHING;
4835 gchar *str = gst_rtsp_strresult (res);
4837 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4838 ("Could not receive message. (%s)", str));
4840 return GST_FLOW_ERROR;
4842 handle_request_failed:
4844 gchar *str = gst_rtsp_strresult (res);
4847 gst_rtsp_message_unset (&message);
4848 if (res != GST_RTSP_EINTR) {
4849 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4850 ("Could not handle server message. (%s)", str));
4852 ret = GST_FLOW_ERROR;
4854 ret = GST_FLOW_FLUSHING;
4860 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4861 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4862 ("The server closed the connection."));
4863 src->conninfo.connected = FALSE;
4864 gst_rtsp_message_unset (&message);
4865 return GST_FLOW_EOS;
4869 static GstRTSPResult
4870 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4872 GstRTSPResult res = GST_RTSP_OK;
4875 GST_DEBUG_OBJECT (src, "doing reconnect");
4877 GST_OBJECT_LOCK (src);
4878 /* only restart when the pads were not yet activated, else we were
4879 * streaming over UDP */
4880 restart = src->need_activate;
4881 GST_OBJECT_UNLOCK (src);
4883 /* no need to restart, we're done */
4887 /* we can try only TCP now */
4888 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
4890 /* close and cleanup our state */
4891 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
4894 /* see if we have TCP left to try. Also don't try TCP when we were configured
4896 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
4899 /* We post a warning message now to inform the user
4900 * that nothing happened. It's most likely a firewall thing. */
4901 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4902 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4903 "firewall is blocking it. Retrying using a tcp connection.",
4904 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
4906 /* open new connection using tcp */
4907 if (gst_rtspsrc_open (src, async) < 0)
4910 /* start playback */
4911 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
4920 src->cur_protocols = 0;
4921 /* no transport possible, post an error and stop */
4922 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4923 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4924 "firewall is blocking it. No other protocols to try.",
4925 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
4926 return GST_RTSP_ERROR;
4930 GST_DEBUG_OBJECT (src, "open failed");
4935 GST_DEBUG_OBJECT (src, "play failed");
4941 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
4945 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
4948 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
4951 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
4954 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
4962 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
4966 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
4969 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
4972 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
4975 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4983 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4987 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4990 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4993 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4996 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
5004 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
5008 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
5011 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
5014 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
5017 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
5025 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
5027 if (ret == GST_RTSP_OK)
5028 gst_rtspsrc_loop_complete_cmd (src, cmd);
5029 else if (ret == GST_RTSP_EINTR)
5030 gst_rtspsrc_loop_cancel_cmd (src, cmd);
5032 gst_rtspsrc_loop_error_cmd (src, cmd);
5036 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
5039 gboolean flushed = FALSE;
5041 /* start new request */
5042 gst_rtspsrc_loop_start_cmd (src, cmd);
5044 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
5046 GST_OBJECT_LOCK (src);
5047 old = src->pending_cmd;
5048 if (old == CMD_RECONNECT) {
5049 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
5050 cmd = CMD_RECONNECT;
5051 } else if (old == CMD_CLOSE) {
5052 /* our CMD_CLOSE might have interrutped CMD_LOOP. gst_rtspsrc_loop
5053 * will send a CMD_WAIT which would cancel our pending CMD_CLOSE (if
5054 * still pending). We just avoid it here by making sure CMD_CLOSE is
5055 * still the pending command. */
5056 GST_DEBUG_OBJECT (src, "ignore, we were closing");
5058 } else if (old != CMD_WAIT) {
5059 src->pending_cmd = CMD_WAIT;
5060 GST_OBJECT_UNLOCK (src);
5061 /* cancel previous request */
5062 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
5063 gst_rtspsrc_loop_cancel_cmd (src, old);
5064 GST_OBJECT_LOCK (src);
5066 src->pending_cmd = cmd;
5067 /* interrupt if allowed */
5068 if (src->busy_cmd & mask) {
5069 GST_DEBUG_OBJECT (src, "connection flush busy %s",
5070 cmd_to_string (src->busy_cmd));
5071 gst_rtspsrc_connection_flush (src, TRUE);
5074 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
5075 cmd_to_string (src->busy_cmd));
5078 gst_task_start (src->task);
5079 GST_OBJECT_UNLOCK (src);
5085 gst_rtspsrc_loop (GstRTSPSrc * src)
5089 if (!src->conninfo.connection || !src->conninfo.connected)
5092 if (src->interleaved)
5093 ret = gst_rtspsrc_loop_interleaved (src);
5095 ret = gst_rtspsrc_loop_udp (src);
5097 if (ret != GST_FLOW_OK)
5105 GST_WARNING_OBJECT (src, "we are not connected");
5106 ret = GST_FLOW_FLUSHING;
5111 const gchar *reason = gst_flow_get_name (ret);
5113 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
5114 src->running = FALSE;
5115 if (ret == GST_FLOW_EOS) {
5116 /* perform EOS logic */
5117 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5118 gst_element_post_message (GST_ELEMENT_CAST (src),
5119 gst_message_new_segment_done (GST_OBJECT_CAST (src),
5120 src->segment.format, src->segment.position));
5121 gst_rtspsrc_push_event (src,
5122 gst_event_new_segment_done (src->segment.format,
5123 src->segment.position));
5125 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5127 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
5128 /* for fatal errors we post an error message, post the error before the
5129 * EOS so the app knows about the error first. */
5130 GST_ELEMENT_FLOW_ERROR (src, ret);
5131 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5133 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
5138 #ifndef GST_DISABLE_GST_DEBUG
5139 static const gchar *
5140 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
5144 while (method != 0) {
5161 /* Parse a WWW-Authenticate Response header and determine the
5162 * available authentication methods
5164 * This code should also cope with the fact that each WWW-Authenticate
5165 * header can contain multiple challenge methods + tokens
5167 * At the moment, for Basic auth, we just do a minimal check and don't
5168 * even parse out the realm */
5170 gst_rtspsrc_parse_auth_hdr (GstRTSPMessage * response,
5171 GstRTSPAuthMethod * methods, GstRTSPConnection * conn, gboolean * stale)
5173 GstRTSPAuthCredential **credentials, **credential;
5175 g_return_if_fail (response != NULL);
5176 g_return_if_fail (methods != NULL);
5177 g_return_if_fail (stale != NULL);
5180 gst_rtsp_message_parse_auth_credentials (response,
5181 GST_RTSP_HDR_WWW_AUTHENTICATE);
5185 credential = credentials;
5186 while (*credential) {
5187 if ((*credential)->scheme == GST_RTSP_AUTH_BASIC) {
5188 *methods |= GST_RTSP_AUTH_BASIC;
5189 } else if ((*credential)->scheme == GST_RTSP_AUTH_DIGEST) {
5190 GstRTSPAuthParam **param = (*credential)->params;
5192 *methods |= GST_RTSP_AUTH_DIGEST;
5194 gst_rtsp_connection_clear_auth_params (conn);
5198 if (strcmp ((*param)->name, "stale") == 0
5199 && g_ascii_strcasecmp ((*param)->value, "TRUE") == 0)
5201 gst_rtsp_connection_set_auth_param (conn, (*param)->name,
5210 gst_rtsp_auth_credentials_free (credentials);
5214 * gst_rtspsrc_setup_auth:
5215 * @src: the rtsp source
5217 * Configure a username and password and auth method on the
5218 * connection object based on a response we received from the
5221 * Currently, this requires that a username and password were supplied
5222 * in the uri. In the future, they may be requested on demand by sending
5223 * a message up the bus.
5225 * Returns: TRUE if authentication information could be set up correctly.
5228 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5232 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5233 GstRTSPAuthMethod method;
5234 GstRTSPResult auth_result;
5236 GstRTSPConnection *conn;
5237 gboolean stale = FALSE;
5239 conn = src->conninfo.connection;
5241 /* Identify the available auth methods and see if any are supported */
5242 gst_rtspsrc_parse_auth_hdr (response, &avail_methods, conn, &stale);
5244 if (avail_methods == GST_RTSP_AUTH_NONE)
5245 goto no_auth_available;
5247 /* For digest auth, if the response indicates that the session
5248 * data are stale, we just update them in the connection object and
5249 * return TRUE to retry the request */
5251 src->tried_url_auth = FALSE;
5253 url = gst_rtsp_connection_get_url (conn);
5255 /* Do we have username and password available? */
5256 if (url != NULL && !src->tried_url_auth && url->user != NULL
5257 && url->passwd != NULL) {
5260 src->tried_url_auth = TRUE;
5261 GST_DEBUG_OBJECT (src,
5262 "Attempting authentication using credentials from the URL");
5264 user = src->user_id;
5265 pass = src->user_pw;
5266 GST_DEBUG_OBJECT (src,
5267 "Attempting authentication using credentials from the properties");
5270 /* FIXME: If the url didn't contain username and password or we tried them
5271 * already, request a username and passwd from the application via some kind
5272 * of credentials request message */
5274 /* If we don't have a username and passwd at this point, bail out. */
5275 if (user == NULL || pass == NULL)
5278 /* Try to configure for each available authentication method, strongest to
5280 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5281 /* Check if this method is available on the server */
5282 if ((method & avail_methods) == 0)
5285 /* Pass the credentials to the connection to try on the next request */
5286 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5287 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5288 * ignore it and end up retrying later */
5289 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5290 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5291 gst_rtsp_auth_method_to_string (method));
5296 if (method == GST_RTSP_AUTH_NONE)
5297 goto no_auth_available;
5303 /* Output an error indicating that we couldn't connect because there were
5304 * no supported authentication protocols */
5305 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5306 ("No supported authentication protocol was found"));
5311 /* We don't fire an error message, we just return FALSE and let the
5312 * normal NOT_AUTHORIZED error be propagated */
5317 static GstRTSPResult
5318 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5319 GstRTSPMessage * request, GstRTSPMessage * response,
5320 GstRTSPStatusCode * code)
5323 GstRTSPStatusCode thecode;
5324 gchar *content_base = NULL;
5328 if (!src->short_header)
5329 gst_rtsp_ext_list_before_send (src->extensions, request);
5331 GST_DEBUG_OBJECT (src, "sending message");
5334 gst_rtsp_message_dump (request);
5336 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
5340 gst_rtsp_connection_reset_timeout (conn);
5343 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
5348 gst_rtsp_message_dump (response);
5350 switch (response->type) {
5351 case GST_RTSP_MESSAGE_REQUEST:
5352 res = gst_rtspsrc_handle_request (src, conn, response);
5353 if (res == GST_RTSP_EEOF)
5356 goto handle_request_failed;
5358 case GST_RTSP_MESSAGE_RESPONSE:
5359 /* ok, a response is good */
5360 GST_DEBUG_OBJECT (src, "received response message");
5362 case GST_RTSP_MESSAGE_DATA:
5363 /* get next response */
5364 GST_DEBUG_OBJECT (src, "handle data response message");
5365 gst_rtspsrc_handle_data (src, response);
5368 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5373 thecode = response->type_data.response.code;
5375 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5377 /* if the caller wanted the result code, we store it. */
5381 /* If the request didn't succeed, bail out before doing any more */
5382 if (thecode != GST_RTSP_STS_OK)
5385 /* store new content base if any */
5386 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5389 g_free (src->content_base);
5390 src->content_base = g_strdup (content_base);
5392 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5399 gchar *str = gst_rtsp_strresult (res);
5401 if (res != GST_RTSP_EINTR) {
5402 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5403 ("Could not send message. (%s)", str));
5405 GST_WARNING_OBJECT (src, "send interrupted");
5414 GST_WARNING_OBJECT (src, "server closed connection");
5415 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5417 /* if reconnect succeeds, try again */
5419 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5423 /* only try once after reconnect, then fallthrough and error out */
5426 gchar *str = gst_rtsp_strresult (res);
5428 if (res != GST_RTSP_EINTR) {
5429 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5430 ("Could not receive message. (%s)", str));
5432 GST_WARNING_OBJECT (src, "receive interrupted");
5440 handle_request_failed:
5442 /* ERROR was posted */
5443 gst_rtsp_message_unset (response);
5448 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5449 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5450 ("The server closed the connection."));
5451 gst_rtsp_message_unset (response);
5458 * @src: the rtsp source
5459 * @conn: the connection to send on
5460 * @request: must point to a valid request
5461 * @response: must point to an empty #GstRTSPMessage
5462 * @code: an optional code result
5464 * send @request and retrieve the response in @response. optionally @code can be
5465 * non-NULL in which case it will contain the status code of the response.
5467 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5468 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5470 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5471 * @response message) if the response code was not 200 (OK).
5473 * If the attempt results in an authentication failure, then this will attempt
5474 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5477 * Returns: #GST_RTSP_OK if the processing was successful.
5479 static GstRTSPResult
5480 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5481 GstRTSPMessage * request, GstRTSPMessage * response,
5482 GstRTSPStatusCode * code)
5484 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5485 GstRTSPResult res = GST_RTSP_ERROR;
5488 GstRTSPMethod method = GST_RTSP_INVALID;
5494 /* make sure we don't loop forever */
5498 /* save method so we can disable it when the server complains */
5499 method = request->type_data.request.method;
5502 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5506 case GST_RTSP_STS_UNAUTHORIZED:
5507 case GST_RTSP_STS_NOT_FOUND:
5508 if (gst_rtspsrc_setup_auth (src, response)) {
5509 /* Try the request/response again after configuring the auth info
5517 } while (retry == TRUE);
5519 /* If the user requested the code, let them handle errors, otherwise
5520 * post an error below */
5523 else if (int_code != GST_RTSP_STS_OK)
5524 goto error_response;
5531 GST_DEBUG_OBJECT (src, "got error %d", res);
5536 res = GST_RTSP_ERROR;
5538 switch (response->type_data.response.code) {
5539 case GST_RTSP_STS_NOT_FOUND:
5540 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5541 response->type_data.response.reason));
5543 case GST_RTSP_STS_UNAUTHORIZED:
5544 GST_ELEMENT_ERROR (src, RESOURCE, NOT_AUTHORIZED, (NULL), ("%s",
5545 response->type_data.response.reason));
5547 case GST_RTSP_STS_MOVED_PERMANENTLY:
5548 case GST_RTSP_STS_MOVE_TEMPORARILY:
5550 gchar *new_location;
5551 GstRTSPLowerTrans transports;
5553 GST_DEBUG_OBJECT (src, "got redirection");
5554 /* if we don't have a Location Header, we must error */
5555 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5556 &new_location, 0) < 0)
5559 /* When we receive a redirect result, we go back to the INIT state after
5560 * parsing the new URI. The caller should do the needed steps to issue
5561 * a new setup when it detects this state change. */
5562 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5564 /* save current transports */
5565 if (src->conninfo.url)
5566 transports = src->conninfo.url->transports;
5568 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5570 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5572 /* set old transports */
5573 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5574 src->conninfo.url->transports = transports;
5576 src->need_redirect = TRUE;
5580 case GST_RTSP_STS_NOT_ACCEPTABLE:
5581 case GST_RTSP_STS_NOT_IMPLEMENTED:
5582 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5583 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5584 gst_rtsp_method_as_text (method));
5585 src->methods &= ~method;
5589 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5590 ("Got error response: %d (%s).", response->type_data.response.code,
5591 response->type_data.response.reason));
5594 /* if we return ERROR we should unset the response ourselves */
5595 if (res == GST_RTSP_ERROR)
5596 gst_rtsp_message_unset (response);
5602 static GstRTSPResult
5603 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5604 GstRTSPMessage * response, GstRTSPSrc * src)
5606 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5611 /* parse the response and collect all the supported methods. We need this
5612 * information so that we don't try to send an unsupported request to the
5616 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5618 GstRTSPHeaderField field;
5622 /* reset supported methods */
5625 /* Try Allow Header first */
5626 field = GST_RTSP_HDR_ALLOW;
5629 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5630 if (indx == 0 && !respoptions) {
5631 /* if no Allow header was found then try the Public header... */
5632 field = GST_RTSP_HDR_PUBLIC;
5633 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5638 src->methods |= gst_rtsp_options_from_text (respoptions);
5643 if (src->methods == 0) {
5644 /* neither Allow nor Public are required, assume the server supports
5645 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5647 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5648 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5650 /* always assume PLAY, FIXME, extensions should be able to override
5652 src->methods |= GST_RTSP_PLAY;
5653 /* also assume it will support Range */
5654 src->seekable = TRUE;
5656 /* we need describe and setup */
5657 if (!(src->methods & GST_RTSP_DESCRIBE))
5659 if (!(src->methods & GST_RTSP_SETUP))
5667 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5668 ("Server does not support DESCRIBE."));
5673 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5674 ("Server does not support SETUP."));
5679 /* masks to be kept in sync with the hardcoded protocol order of preference
5681 static const guint protocol_masks[] = {
5682 GST_RTSP_LOWER_TRANS_UDP,
5683 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5684 GST_RTSP_LOWER_TRANS_TCP,
5688 static GstRTSPResult
5689 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5690 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
5694 gboolean add_udp_str;
5699 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5704 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5706 /* extension listed transports, use those */
5707 if (*transports != NULL)
5710 /* it's the default */
5711 add_udp_str = FALSE;
5713 /* the default RTSP transports */
5714 result = g_string_new ("RTP");
5717 case GST_RTSP_PROFILE_AVP:
5718 g_string_append (result, "/AVP");
5720 case GST_RTSP_PROFILE_SAVP:
5721 g_string_append (result, "/SAVP");
5723 case GST_RTSP_PROFILE_AVPF:
5724 g_string_append (result, "/AVPF");
5726 case GST_RTSP_PROFILE_SAVPF:
5727 g_string_append (result, "/SAVPF");
5733 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5734 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5736 g_string_append (result, "/UDP");
5737 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5738 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5739 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5740 /* we don't have to allocate any UDP ports yet, if the selected transport
5741 * turns out to be multicast we can create them and join the multicast
5742 * group indicated in the transport reply */
5744 g_string_append (result, "/UDP");
5745 g_string_append (result, ";multicast");
5746 if (src->next_port_num != 0) {
5747 if (src->client_port_range.max > 0 &&
5748 src->next_port_num >= src->client_port_range.max)
5751 g_string_append_printf (result, ";client_port=%d-%d",
5752 src->next_port_num, src->next_port_num + 1);
5754 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
5755 GST_DEBUG_OBJECT (src, "adding TCP");
5757 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
5759 *transports = g_string_free (result, FALSE);
5761 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5768 GST_ERROR ("extension gave error %d", res);
5773 GST_ERROR ("no more ports available");
5774 return GST_RTSP_ERROR;
5778 static GstRTSPResult
5779 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5780 gint orig_rtpport, gint orig_rtcpport)
5783 gint nr_udp, nr_int;
5785 gint rtpport = 0, rtcpport = 0;
5788 src = stream->parent;
5790 /* find number of placeholders first */
5791 if (strstr (*transports, "%%i2"))
5793 else if (strstr (*transports, "%%i1"))
5798 if (strstr (*transports, "%%u2"))
5800 else if (strstr (*transports, "%%u1"))
5805 if (nr_udp == 0 && nr_int == 0)
5809 if (!orig_rtpport || !orig_rtcpport) {
5810 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5813 rtpport = orig_rtpport;
5814 rtcpport = orig_rtcpport;
5818 str = g_string_new ("");
5820 while ((next = strstr (p, "%%"))) {
5821 g_string_append_len (str, p, next - p);
5822 if (next[2] == 'u') {
5824 g_string_append_printf (str, "%d", rtpport);
5825 else if (next[3] == '2')
5826 g_string_append_printf (str, "%d", rtcpport);
5828 if (next[2] == 'i') {
5830 g_string_append_printf (str, "%d", src->free_channel);
5831 else if (next[3] == '2')
5832 g_string_append_printf (str, "%d", src->free_channel + 1);
5837 /* append final part */
5838 g_string_append (str, p);
5840 g_free (*transports);
5841 *transports = g_string_free (str, FALSE);
5849 GST_ERROR ("failed to allocate udp ports");
5850 return GST_RTSP_ERROR;
5855 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
5857 GstCaps *caps = NULL;
5859 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
5863 GST_DEBUG_OBJECT (src, "SRTP parameters received");
5869 default_srtcp_params (void)
5876 guint data_size = GST_ROUND_UP_4 (KEY_SIZE);
5878 /* create a random key */
5879 key_data = g_malloc (data_size);
5880 for (i = 0; i < data_size; i += 4)
5881 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
5883 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
5885 caps = gst_caps_new_simple ("application/x-srtcp",
5886 "srtp-key", GST_TYPE_BUFFER, buf,
5887 "srtp-cipher", G_TYPE_STRING, "aes-128-icm",
5888 "srtp-auth", G_TYPE_STRING, "hmac-sha1-80",
5889 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
5890 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
5892 gst_buffer_unref (buf);
5898 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
5900 gchar *base64, *result = NULL;
5901 GstMIKEYMessage *mikey_msg;
5903 stream->srtcpparams = signal_get_srtcp_params (src, stream);
5904 if (stream->srtcpparams == NULL)
5905 stream->srtcpparams = default_srtcp_params ();
5907 mikey_msg = gst_mikey_message_new_from_caps (stream->srtcpparams);
5909 /* add policy '0' for our SSRC */
5910 gst_mikey_message_add_cs_srtp (mikey_msg, 0, stream->send_ssrc, 0);
5912 base64 = gst_mikey_message_base64_encode (mikey_msg);
5913 gst_mikey_message_unref (mikey_msg);
5916 result = gst_sdp_make_keymgmt (stream->conninfo.location, base64);
5924 /* Perform the SETUP request for all the streams.
5926 * We ask the server for a specific transport, which initially includes all the
5927 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
5928 * two local UDP ports that we send to the server.
5930 * Once the server replied with a transport, we configure the other streams
5931 * with the same transport.
5933 * This function will also configure the stream for the selected transport,
5934 * which basically means creating the pipeline.
5936 static GstRTSPResult
5937 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
5940 GstRTSPResult res = GST_RTSP_ERROR;
5941 GstRTSPMessage request = { 0 };
5942 GstRTSPMessage response = { 0 };
5943 GstRTSPStream *stream = NULL;
5944 GstRTSPLowerTrans protocols;
5945 GstRTSPStatusCode code;
5946 gboolean unsupported_real = FALSE;
5947 gint rtpport, rtcpport;
5951 if (src->conninfo.connection) {
5952 url = gst_rtsp_connection_get_url (src->conninfo.connection);
5953 /* we initially allow all configured lower transports. based on the URL
5954 * transports and the replies from the server we narrow them down. */
5955 protocols = url->transports & src->cur_protocols;
5958 protocols = src->cur_protocols;
5964 /* reset some state */
5965 src->free_channel = 0;
5966 src->interleaved = FALSE;
5967 src->need_activate = FALSE;
5968 /* keep track of next port number, 0 is random */
5969 src->next_port_num = src->client_port_range.min;
5970 rtpport = rtcpport = 0;
5972 if (G_UNLIKELY (src->streams == NULL))
5975 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5976 GstRTSPConnection *conn;
5983 stream = (GstRTSPStream *) walk->data;
5985 caps = stream_get_caps_for_pt (stream, stream->default_pt);
5987 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
5991 if (stream->skipped) {
5992 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
5996 /* see if we need to configure this stream */
5997 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
5998 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
6003 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
6004 stream->id, caps, &selected);
6006 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
6010 /* merge/overwrite global caps */
6015 s = gst_caps_get_structure (caps, 0);
6017 num = gst_structure_n_fields (src->props);
6018 for (j = 0; j < num; j++) {
6022 name = gst_structure_nth_field_name (src->props, j);
6023 val = gst_structure_get_value (src->props, name);
6024 gst_structure_set_value (s, name, val);
6026 GST_DEBUG_OBJECT (src, "copied %s", name);
6030 /* skip setup if we have no URL for it */
6031 if (stream->conninfo.location == NULL) {
6032 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
6036 if (src->conninfo.connection == NULL) {
6037 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
6038 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
6041 conn = stream->conninfo.connection;
6043 conn = src->conninfo.connection;
6045 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
6046 stream->conninfo.location);
6048 /* if we have a multicast connection, only suggest multicast from now on */
6049 if (stream->is_multicast)
6050 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
6053 /* first selectable protocol */
6054 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6056 if (!protocol_masks[mask])
6060 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
6061 protocol_masks[mask]);
6062 /* create a string with first transport in line */
6064 res = gst_rtspsrc_create_transports_string (src,
6065 protocols & protocol_masks[mask], stream->profile, &transports);
6066 if (res < 0 || transports == NULL)
6067 goto setup_transport_failed;
6069 if (strlen (transports) == 0) {
6070 g_free (transports);
6071 GST_DEBUG_OBJECT (src, "no transports found");
6076 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
6078 /* replace placeholders with real values, this function will optionally
6079 * allocate UDP ports and other info needed to execute the setup request */
6080 res = gst_rtspsrc_prepare_transports (stream, &transports,
6081 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
6083 g_free (transports);
6084 goto setup_transport_failed;
6087 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
6089 /* create SETUP request */
6091 gst_rtspsrc_init_request (src, &request, GST_RTSP_SETUP,
6092 stream->conninfo.location);
6094 g_free (transports);
6095 goto create_request_failed;
6098 /* select transport */
6099 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
6102 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
6103 stream->profile == GST_RTSP_PROFILE_SAVPF) {
6104 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
6105 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
6108 /* if the user wants a non default RTP packet size we add the blocksize
6110 if (src->rtp_blocksize > 0) {
6111 hval = g_strdup_printf ("%d", src->rtp_blocksize);
6112 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
6116 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
6119 /* handle the code ourselves */
6120 res = gst_rtspsrc_send (src, conn, &request, &response, &code);
6125 case GST_RTSP_STS_OK:
6127 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
6128 gst_rtsp_message_unset (&request);
6129 gst_rtsp_message_unset (&response);
6130 /* cleanup of leftover transport */
6131 gst_rtspsrc_stream_free_udp (stream);
6132 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
6133 * we might be in this case */
6134 if (stream->container && rtpport && rtcpport && !retry) {
6135 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
6140 /* this transport did not go down well, but we may have others to try
6141 * that we did not send yet, try those and only give up then
6142 * but not without checking for lost cause/extension so we can
6143 * post a nicer/more useful error message later */
6144 if (!unsupported_real)
6145 unsupported_real = stream->is_real;
6146 /* select next available protocol, give up on this stream if none */
6148 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6150 if (!protocol_masks[mask] || unsupported_real)
6155 /* cleanup of leftover transport and move to the next stream */
6156 gst_rtspsrc_stream_free_udp (stream);
6157 goto response_error;
6160 /* parse response transport */
6162 gchar *resptrans = NULL;
6163 GstRTSPTransport transport = { 0 };
6165 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
6168 gst_rtspsrc_stream_free_udp (stream);
6172 /* parse transport, go to next stream on parse error */
6173 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6174 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6178 /* update allowed transports for other streams. once the transport of
6179 * one stream has been determined, we make sure that all other streams
6180 * are configured in the same way */
6181 switch (transport.lower_transport) {
6182 case GST_RTSP_LOWER_TRANS_TCP:
6183 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6184 protocols = GST_RTSP_LOWER_TRANS_TCP;
6185 src->interleaved = TRUE;
6186 /* update free channels */
6188 MAX (transport.interleaved.min, src->free_channel);
6190 MAX (transport.interleaved.max, src->free_channel);
6191 src->free_channel++;
6193 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6194 /* only allow multicast for other streams */
6195 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6196 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6197 /* if the server selected our ports, increment our counters so that
6198 * we select a new port later */
6199 if (src->next_port_num == transport.port.min &&
6200 src->next_port_num + 1 == transport.port.max) {
6201 src->next_port_num += 2;
6204 case GST_RTSP_LOWER_TRANS_UDP:
6205 /* only allow unicast for other streams */
6206 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6207 protocols = GST_RTSP_LOWER_TRANS_UDP;
6210 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6211 transport.lower_transport);
6215 if (!src->interleaved || !retry) {
6216 /* now configure the stream with the selected transport */
6217 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6218 GST_DEBUG_OBJECT (src,
6219 "could not configure stream %p transport, skipping stream",
6222 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
6223 /* retain the first allocated UDP port pair */
6224 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
6225 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
6228 /* we need to activate at least one streams when we detect activity */
6229 src->need_activate = TRUE;
6231 /* stream is setup now */
6232 stream->setup = TRUE;
6237 GstRTSPStream *sskip;
6239 skip = g_list_next (skip);
6243 sskip = (GstRTSPStream *) skip->data;
6245 /* skip all streams with the same control url */
6246 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
6247 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
6248 sskip, sskip->conninfo.location);
6249 sskip->skipped = TRUE;
6254 /* clean up our transport struct */
6255 gst_rtsp_transport_init (&transport);
6256 /* clean up used RTSP messages */
6257 gst_rtsp_message_unset (&request);
6258 gst_rtsp_message_unset (&response);
6262 /* store the transport protocol that was configured */
6263 src->cur_protocols = protocols;
6265 gst_rtsp_ext_list_stream_select (src->extensions, url);
6267 /* if there is nothing to activate, error out */
6268 if (!src->need_activate)
6269 goto nothing_to_activate;
6276 /* no transport possible, post an error and stop */
6277 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6278 ("Could not connect to server, no protocols left"));
6279 return GST_RTSP_ERROR;
6283 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6284 ("SDP contains no streams"));
6285 return GST_RTSP_ERROR;
6287 create_request_failed:
6289 gchar *str = gst_rtsp_strresult (res);
6291 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6292 ("Could not create request. (%s)", str));
6296 setup_transport_failed:
6298 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6299 ("Could not setup transport."));
6300 res = GST_RTSP_ERROR;
6305 const gchar *str = gst_rtsp_status_as_text (code);
6307 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6308 ("Error (%d): %s", code, GST_STR_NULL (str)));
6309 res = GST_RTSP_ERROR;
6314 gchar *str = gst_rtsp_strresult (res);
6316 if (res != GST_RTSP_EINTR) {
6317 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6318 ("Could not send message. (%s)", str));
6320 GST_WARNING_OBJECT (src, "send interrupted");
6327 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6328 ("Server did not select transport."));
6329 res = GST_RTSP_ERROR;
6332 nothing_to_activate:
6334 /* none of the available error codes is really right .. */
6335 if (unsupported_real) {
6336 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6337 (_("No supported stream was found. You might need to install a "
6338 "GStreamer RTSP extension plugin for Real media streams.")),
6341 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6342 (_("No supported stream was found. You might need to allow "
6343 "more transport protocols or may otherwise be missing "
6344 "the right GStreamer RTSP extension plugin.")), (NULL));
6346 return GST_RTSP_ERROR;
6350 gst_rtsp_message_unset (&request);
6351 gst_rtsp_message_unset (&response);
6357 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
6358 GstSegment * segment)
6361 GstRTSPTimeRange *therange;
6364 gst_rtsp_range_free (src->range);
6366 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
6367 GST_DEBUG_OBJECT (src, "parsed range %s", range);
6368 src->range = therange;
6370 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6372 gst_segment_init (segment, GST_FORMAT_TIME);
6376 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6377 therange->min.type, therange->min.seconds, therange->max.type,
6378 therange->max.seconds);
6380 if (therange->min.type == GST_RTSP_TIME_NOW)
6382 else if (therange->min.type == GST_RTSP_TIME_END)
6385 seconds = therange->min.seconds * GST_SECOND;
6387 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6388 GST_TIME_ARGS (seconds));
6390 /* we need to start playback without clipping from the position reported by
6392 segment->start = seconds;
6393 segment->position = seconds;
6395 if (therange->max.type == GST_RTSP_TIME_NOW)
6397 else if (therange->max.type == GST_RTSP_TIME_END)
6400 seconds = therange->max.seconds * GST_SECOND;
6402 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6403 GST_TIME_ARGS (seconds));
6405 /* live (WMS) server might send overflowed large max as its idea of infinity,
6406 * compensate to prevent problems later on */
6407 if (seconds != -1 && seconds < 0) {
6409 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6412 /* live (WMS) might send min == max, which is not worth recording */
6413 if (segment->duration == -1 && seconds == segment->start)
6416 /* don't change duration with unknown value, we might have a valid value
6417 * there that we want to keep. */
6419 segment->duration = seconds;
6424 /* Parse clock profived by the server with following syntax:
6426 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6429 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6431 gboolean res = FALSE;
6433 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6434 gchar **fields = NULL, **parts = NULL;
6435 gchar *remote_ip, *str;
6437 GstClockTime base_time;
6440 fields = g_strsplit (gstclock, " ", 0);
6442 /* wrapped clock, not very interesting for now */
6443 if (fields[1] == NULL)
6446 /* remote IP address and port */
6447 if ((str = fields[2]) == NULL)
6450 parts = g_strsplit (str, ":", 0);
6452 if ((remote_ip = parts[0]) == NULL)
6455 if ((str = parts[1]) == NULL)
6463 if ((str = fields[3]) == NULL)
6466 base_time = g_ascii_strtoull (str, NULL, 10);
6469 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
6472 if (src->provided_clock)
6473 gst_object_unref (src->provided_clock);
6474 src->provided_clock = netclock;
6476 gst_element_post_message (GST_ELEMENT_CAST (src),
6477 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
6478 src->provided_clock, TRUE));
6482 g_strfreev (fields);
6488 /* must be called with the RTSP state lock */
6489 static GstRTSPResult
6490 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
6496 /* prepare global stream caps properties */
6498 gst_structure_remove_all_fields (src->props);
6500 src->props = gst_structure_new_empty ("RTSPProperties");
6503 gst_sdp_message_dump (sdp);
6505 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
6507 /* let the app inspect and change the SDP */
6508 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6510 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6512 /* parse range for duration reporting. */
6517 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6521 /* keep track of the range and configure it in the segment */
6522 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6526 /* parse clock information. This is GStreamer specific, a server can tell the
6527 * client what clock it is using and wrap that in a network clock. The
6528 * advantage of that is that we can slave to it. */
6530 const gchar *gstclock;
6533 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6534 if (gstclock == NULL)
6537 /* parse the clock and expose it in the provide_clock method */
6538 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6542 /* try to find a global control attribute. Note that a '*' means that we should
6543 * do aggregate control with the current url (so we don't do anything and
6544 * leave the current connection as is) */
6546 const gchar *control;
6549 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6550 if (control == NULL)
6553 /* only take fully qualified urls */
6554 if (g_str_has_prefix (control, "rtsp://"))
6558 g_free (src->conninfo.location);
6559 src->conninfo.location = g_strdup (control);
6560 /* make a connection for this, if there was a connection already, nothing
6562 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6563 GST_ERROR_OBJECT (src, "could not connect");
6566 /* we need to keep the control url separate from the connection url because
6567 * the rules for constructing the media control url need it */
6568 g_free (src->control);
6569 src->control = g_strdup (control);
6572 /* create streams */
6573 n_streams = gst_sdp_message_medias_len (sdp);
6574 for (i = 0; i < n_streams; i++) {
6575 gst_rtspsrc_create_stream (src, sdp, i, n_streams);
6578 src->state = GST_RTSP_STATE_INIT;
6581 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6584 /* reset our state */
6585 src->need_range = TRUE;
6588 src->state = GST_RTSP_STATE_READY;
6595 GST_ERROR_OBJECT (src, "setup failed");
6596 gst_rtspsrc_cleanup (src);
6601 static GstRTSPResult
6602 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6606 GstRTSPMessage request = { 0 };
6607 GstRTSPMessage response = { 0 };
6610 gchar *respcont = NULL;
6613 src->need_redirect = FALSE;
6615 /* can't continue without a valid url */
6616 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6617 res = GST_RTSP_EINVAL;
6620 src->tried_url_auth = FALSE;
6622 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6623 goto connect_failed;
6625 /* create OPTIONS */
6626 GST_DEBUG_OBJECT (src, "create options...");
6628 gst_rtspsrc_init_request (src, &request, GST_RTSP_OPTIONS,
6629 src->conninfo.url_str);
6631 goto create_request_failed;
6634 GST_DEBUG_OBJECT (src, "send options...");
6637 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6640 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6645 if (!gst_rtspsrc_parse_methods (src, &response))
6648 /* create DESCRIBE */
6649 GST_DEBUG_OBJECT (src, "create describe...");
6651 gst_rtspsrc_init_request (src, &request, GST_RTSP_DESCRIBE,
6652 src->conninfo.url_str);
6654 goto create_request_failed;
6656 /* we only accept SDP for now */
6657 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
6661 GST_DEBUG_OBJECT (src, "send describe...");
6664 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
6667 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6671 /* we only perform redirect for describe and play, currently */
6672 if (src->need_redirect) {
6673 /* close connection, we don't have to send a TEARDOWN yet, ignore the
6675 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6677 gst_rtsp_message_unset (&request);
6678 gst_rtsp_message_unset (&response);
6684 /* it could be that the DESCRIBE method was not implemented */
6685 if (!(src->methods & GST_RTSP_DESCRIBE))
6688 /* check if reply is SDP */
6689 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
6691 /* could not be set but since the request returned OK, we assume it
6692 * was SDP, else check it. */
6694 const gchar *props = strchr (respcont, ';');
6697 gchar *mimetype = g_strndup (respcont, props - respcont);
6699 mimetype = g_strstrip (mimetype);
6700 if (g_ascii_strcasecmp (mimetype, "application/sdp") != 0) {
6702 goto wrong_content_type;
6705 /* TODO: Check for charset property and do conversions of all messages if
6706 * needed. Some servers actually send that property */
6709 } else if (g_ascii_strcasecmp (respcont, "application/sdp") != 0) {
6710 goto wrong_content_type;
6714 /* get message body and parse as SDP */
6715 gst_rtsp_message_get_body (&response, &data, &size);
6716 if (data == NULL || size == 0)
6719 GST_DEBUG_OBJECT (src, "parse SDP...");
6720 gst_sdp_message_new (sdp);
6721 gst_sdp_message_parse_buffer (data, size, *sdp);
6723 /* clean up any messages */
6724 gst_rtsp_message_unset (&request);
6725 gst_rtsp_message_unset (&response);
6732 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
6733 ("No valid RTSP URL was provided"));
6738 gchar *str = gst_rtsp_strresult (res);
6740 if (res != GST_RTSP_EINTR) {
6741 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6742 ("Failed to connect. (%s)", str));
6744 GST_WARNING_OBJECT (src, "connect interrupted");
6749 create_request_failed:
6751 gchar *str = gst_rtsp_strresult (res);
6753 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6754 ("Could not create request. (%s)", str));
6760 /* Don't post a message - the rtsp_send method will have
6761 * taken care of it because we passed NULL for the response code */
6766 /* error was posted */
6767 res = GST_RTSP_ERROR;
6772 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6773 ("Server does not support SDP, got %s.", respcont));
6774 res = GST_RTSP_ERROR;
6779 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6780 ("Server can not provide an SDP."));
6781 res = GST_RTSP_ERROR;
6786 if (src->conninfo.connection) {
6787 GST_DEBUG_OBJECT (src, "free connection");
6788 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6790 gst_rtsp_message_unset (&request);
6791 gst_rtsp_message_unset (&response);
6796 static GstRTSPResult
6797 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
6802 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
6804 if (src->sdp == NULL) {
6805 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
6809 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
6814 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
6821 GST_WARNING_OBJECT (src, "can't get sdp");
6822 src->open_error = TRUE;
6827 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
6828 src->open_error = TRUE;
6833 static GstRTSPResult
6834 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
6836 GstRTSPMessage request = { 0 };
6837 GstRTSPMessage response = { 0 };
6838 GstRTSPResult res = GST_RTSP_OK;
6840 const gchar *control;
6842 GST_DEBUG_OBJECT (src, "TEARDOWN...");
6844 gst_rtspsrc_set_state (src, GST_STATE_READY);
6846 if (src->state < GST_RTSP_STATE_READY) {
6847 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
6854 /* construct a control url */
6855 control = get_aggregate_control (src);
6857 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
6860 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6861 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6862 const gchar *setup_url;
6863 GstRTSPConnInfo *info;
6865 /* try aggregate control first but do non-aggregate control otherwise */
6867 setup_url = control;
6868 else if ((setup_url = stream->conninfo.location) == NULL)
6871 if (src->conninfo.connection) {
6872 info = &src->conninfo;
6873 } else if (stream->conninfo.connection) {
6874 info = &stream->conninfo;
6878 if (!info->connected)
6883 gst_rtspsrc_init_request (src, &request, GST_RTSP_TEARDOWN, setup_url);
6885 goto create_request_failed;
6888 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
6891 gst_rtspsrc_send (src, info->connection, &request, &response,
6895 /* FIXME, parse result? */
6896 gst_rtsp_message_unset (&request);
6897 gst_rtsp_message_unset (&response);
6900 /* early exit when we did aggregate control */
6906 /* close connections */
6907 GST_DEBUG_OBJECT (src, "closing connection...");
6908 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6909 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6910 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6911 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
6915 gst_rtspsrc_cleanup (src);
6917 src->state = GST_RTSP_STATE_INVALID;
6920 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
6925 create_request_failed:
6927 gchar *str = gst_rtsp_strresult (res);
6929 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6930 ("Could not create request. (%s)", str));
6936 gchar *str = gst_rtsp_strresult (res);
6938 gst_rtsp_message_unset (&request);
6939 if (res != GST_RTSP_EINTR) {
6940 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6941 ("Could not send message. (%s)", str));
6943 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
6950 GST_DEBUG_OBJECT (src,
6951 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
6956 /* RTP-Info is of the format:
6958 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
6960 * rtptime corresponds to the timestamp for the NPT time given in the header
6961 * seqbase corresponds to the next sequence number we received. This number
6962 * indicates the first seqnum after the seek and should be used to discard
6963 * packets that are from before the seek.
6966 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
6971 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
6973 infos = g_strsplit (rtpinfo, ",", 0);
6974 for (i = 0; infos[i]; i++) {
6976 GstRTSPStream *stream;
6980 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
6982 /* init values, types of seqbase and timebase are bigger than needed so we
6983 * can store -1 as uninitialized values */
6988 /* parse url, find stream for url.
6989 * parse seq and rtptime. The seq number should be configured in the rtp
6990 * depayloader or session manager to detect gaps. Same for the rtptime, it
6991 * should be used to create an initial time newsegment. */
6992 fields = g_strsplit (infos[i], ";", 0);
6993 for (j = 0; fields[j]; j++) {
6994 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
6995 /* remove leading whitespace */
6996 fields[j] = g_strchug (fields[j]);
6997 if (g_str_has_prefix (fields[j], "url=")) {
6998 /* get the url and the stream */
7000 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
7001 } else if (g_str_has_prefix (fields[j], "seq=")) {
7002 seqbase = atoi (fields[j] + 4);
7003 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
7004 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
7007 g_strfreev (fields);
7008 /* now we need to store the values for the caps of the stream */
7009 if (stream != NULL) {
7010 GST_DEBUG_OBJECT (src,
7011 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
7012 stream, seqbase, timebase);
7014 /* we have a stream, configure detected params */
7015 stream->seqbase = seqbase;
7016 stream->timebase = timebase;
7025 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
7030 interval = strtoul (rtcp, NULL, 10);
7031 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
7036 interval *= GST_MSECOND;
7038 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7039 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7041 /* already (optionally) retrieved this when configuring manager */
7042 if (stream->session) {
7043 GObject *rtpsession = stream->session;
7045 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
7047 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
7051 /* now it happens that (Xenon) server sending this may also provide bogus
7052 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
7053 * and just use RTP-Info to sync */
7055 GObjectClass *klass;
7057 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
7058 if (g_object_class_find_property (klass, "rtcp-sync")) {
7059 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
7060 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
7066 gst_rtspsrc_get_float (const gchar * dstr)
7068 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7070 /* canonicalise floating point string so we can handle float strings
7071 * in the form "24.930" or "24,930" irrespective of the current locale */
7072 g_strlcpy (s, dstr, sizeof (s));
7073 g_strdelimit (s, ",", '.');
7074 return g_ascii_strtod (s, NULL);
7078 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
7080 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7082 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
7083 g_strlcpy (val_str, "now", sizeof (val_str));
7085 if (segment->position == 0) {
7086 g_strlcpy (val_str, "0", sizeof (val_str));
7088 g_ascii_dtostr (val_str, sizeof (val_str),
7089 ((gdouble) segment->position) / GST_SECOND);
7092 return g_strdup_printf ("npt=%s-", val_str);
7096 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
7100 stream->timebase = -1;
7101 stream->seqbase = -1;
7103 len = stream->ptmap->len;
7104 for (i = 0; i < len; i++) {
7105 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
7108 if (item->caps == NULL)
7111 item->caps = gst_caps_make_writable (item->caps);
7112 s = gst_caps_get_structure (item->caps, 0);
7113 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
7114 if (item->pt == stream->default_pt && stream->udpsrc[0])
7115 g_object_set (stream->udpsrc[0], "caps", item->caps, NULL);
7117 stream->need_caps = TRUE;
7120 static GstRTSPResult
7121 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
7123 GstRTSPResult res = GST_RTSP_OK;
7125 if (src->state < GST_RTSP_STATE_READY) {
7126 res = GST_RTSP_ERROR;
7127 if (src->open_error) {
7128 GST_DEBUG_OBJECT (src, "the stream was in error");
7132 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
7134 if ((res = gst_rtspsrc_open (src, async)) < 0) {
7135 GST_DEBUG_OBJECT (src, "failed to open stream");
7144 static GstRTSPResult
7145 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
7147 GstRTSPMessage request = { 0 };
7148 GstRTSPMessage response = { 0 };
7149 GstRTSPResult res = GST_RTSP_OK;
7153 const gchar *control;
7155 GST_DEBUG_OBJECT (src, "PLAY...");
7158 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7161 if (!(src->methods & GST_RTSP_PLAY))
7164 if (src->state == GST_RTSP_STATE_PLAYING)
7167 if (!src->conninfo.connection || !src->conninfo.connected)
7170 /* send some dummy packets before we activate the receive in the
7172 gst_rtspsrc_send_dummy_packets (src);
7174 /* require new SR packets */
7176 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
7178 /* construct a control url */
7179 control = get_aggregate_control (src);
7181 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7182 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7183 const gchar *setup_url;
7184 GstRTSPConnection *conn;
7186 /* try aggregate control first but do non-aggregate control otherwise */
7188 setup_url = control;
7189 else if ((setup_url = stream->conninfo.location) == NULL)
7192 if (src->conninfo.connection) {
7193 conn = src->conninfo.connection;
7194 } else if (stream->conninfo.connection) {
7195 conn = stream->conninfo.connection;
7201 res = gst_rtspsrc_init_request (src, &request, GST_RTSP_PLAY, setup_url);
7203 goto create_request_failed;
7205 if (src->need_range) {
7206 hval = gen_range_header (src, segment);
7208 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
7210 /* store the newsegment event so it can be sent from the streaming thread. */
7211 src->need_segment = TRUE;
7214 if (segment->rate != 1.0) {
7215 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
7217 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
7219 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
7221 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
7225 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
7227 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7230 if (src->need_redirect) {
7231 GST_DEBUG_OBJECT (src,
7232 "redirect: tearing down and restarting with new url");
7233 /* teardown and restart with new url */
7234 gst_rtspsrc_close (src, TRUE, FALSE);
7235 /* reset protocols to force re-negotiation with redirected url */
7236 src->cur_protocols = src->protocols;
7237 gst_rtsp_message_unset (&request);
7238 gst_rtsp_message_unset (&response);
7242 /* seek may have silently failed as it is not supported */
7243 if (!(src->methods & GST_RTSP_PLAY)) {
7244 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
7245 /* obviously it is supported as we made it here */
7246 src->methods |= GST_RTSP_PLAY;
7247 src->seekable = FALSE;
7248 /* but there is nothing to parse in the response,
7249 * so convey we have no idea and not to expect anything particular */
7250 clear_rtp_base (src, stream);
7254 /* need to do for all streams */
7255 for (run = src->streams; run; run = g_list_next (run))
7256 clear_rtp_base (src, (GstRTSPStream *) run->data);
7258 /* NOTE the above also disables npt based eos detection */
7259 /* and below forces position to 0,
7260 * which is visible feedback we lost the plot */
7261 segment->start = segment->position = src->last_pos;
7264 gst_rtsp_message_unset (&request);
7266 /* parse RTP npt field. This is the current position in the stream (Normal
7267 * Play Time) and should be put in the NEWSEGMENT position field. */
7268 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
7270 gst_rtspsrc_parse_range (src, hval, segment);
7272 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
7273 segment->rate = 1.0;
7275 /* parse Speed header. This is the intended playback rate of the stream
7276 * and should be put in the NEWSEGMENT rate field. */
7277 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
7278 0) == GST_RTSP_OK) {
7279 segment->rate = gst_rtspsrc_get_float (hval);
7280 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
7281 &hval, 0) == GST_RTSP_OK) {
7282 segment->rate = gst_rtspsrc_get_float (hval);
7285 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
7286 * for the RTP packets. If this is not present, we assume all starts from 0...
7287 * This is info for the RTP session manager that we pass to it in caps. */
7289 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
7290 &hval, hval_idx++) == GST_RTSP_OK)
7291 gst_rtspsrc_parse_rtpinfo (src, hval);
7293 /* some servers indicate RTCP parameters in PLAY response,
7294 * rather than properly in SDP */
7295 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
7296 &hval, 0) == GST_RTSP_OK)
7297 gst_rtspsrc_handle_rtcp_interval (src, hval);
7299 gst_rtsp_message_unset (&response);
7301 /* early exit when we did aggregate control */
7305 /* configure the caps of the streams after we parsed all headers. Only reset
7306 * the manager object when we set a new Range header (we did a seek) */
7307 gst_rtspsrc_configure_caps (src, segment, src->need_range);
7309 /* set to PLAYING after we have configured the caps, otherwise we
7310 * might end up calling request_key (with SRTP) while caps are still
7311 * being configured. */
7312 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
7314 /* set again when needed */
7315 src->need_range = FALSE;
7317 src->running = TRUE;
7318 src->base_time = -1;
7319 src->state = GST_RTSP_STATE_PLAYING;
7322 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
7323 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7324 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7325 stream->discont = TRUE;
7330 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
7337 GST_DEBUG_OBJECT (src, "failed to open stream");
7342 GST_DEBUG_OBJECT (src, "PLAY is not supported");
7347 GST_DEBUG_OBJECT (src, "we were already PLAYING");
7350 create_request_failed:
7352 gchar *str = gst_rtsp_strresult (res);
7354 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7355 ("Could not create request. (%s)", str));
7361 gchar *str = gst_rtsp_strresult (res);
7363 gst_rtsp_message_unset (&request);
7364 if (res != GST_RTSP_EINTR) {
7365 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7366 ("Could not send message. (%s)", str));
7368 GST_WARNING_OBJECT (src, "PLAY interrupted");
7375 static GstRTSPResult
7376 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
7378 GstRTSPResult res = GST_RTSP_OK;
7379 GstRTSPMessage request = { 0 };
7380 GstRTSPMessage response = { 0 };
7382 const gchar *control;
7384 GST_DEBUG_OBJECT (src, "PAUSE...");
7386 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7389 if (!(src->methods & GST_RTSP_PAUSE))
7392 if (src->state == GST_RTSP_STATE_READY)
7395 if (!src->conninfo.connection || !src->conninfo.connected)
7398 /* construct a control url */
7399 control = get_aggregate_control (src);
7401 /* loop over the streams. We might exit the loop early when we could do an
7402 * aggregate control */
7403 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7404 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7405 GstRTSPConnection *conn;
7406 const gchar *setup_url;
7408 /* try aggregate control first but do non-aggregate control otherwise */
7410 setup_url = control;
7411 else if ((setup_url = stream->conninfo.location) == NULL)
7414 if (src->conninfo.connection) {
7415 conn = src->conninfo.connection;
7416 } else if (stream->conninfo.connection) {
7417 conn = stream->conninfo.connection;
7423 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7424 ("Sending PAUSE request"));
7427 gst_rtspsrc_init_request (src, &request, GST_RTSP_PAUSE,
7429 goto create_request_failed;
7431 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7434 gst_rtsp_message_unset (&request);
7435 gst_rtsp_message_unset (&response);
7437 /* exit early when we did agregate control */
7442 /* change element states now */
7443 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7446 src->state = GST_RTSP_STATE_READY;
7450 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7457 GST_DEBUG_OBJECT (src, "failed to open stream");
7462 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7467 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7470 create_request_failed:
7472 gchar *str = gst_rtsp_strresult (res);
7474 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7475 ("Could not create request. (%s)", str));
7481 gchar *str = gst_rtsp_strresult (res);
7483 gst_rtsp_message_unset (&request);
7484 if (res != GST_RTSP_EINTR) {
7485 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7486 ("Could not send message. (%s)", str));
7488 GST_WARNING_OBJECT (src, "PAUSE interrupted");
7496 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
7498 GstRTSPSrc *rtspsrc;
7500 rtspsrc = GST_RTSPSRC (bin);
7502 switch (GST_MESSAGE_TYPE (message)) {
7503 case GST_MESSAGE_EOS:
7504 gst_message_unref (message);
7506 case GST_MESSAGE_ELEMENT:
7508 const GstStructure *s = gst_message_get_structure (message);
7510 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
7511 gboolean ignore_timeout;
7513 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
7515 GST_OBJECT_LOCK (rtspsrc);
7516 ignore_timeout = rtspsrc->ignore_timeout;
7517 rtspsrc->ignore_timeout = TRUE;
7518 GST_OBJECT_UNLOCK (rtspsrc);
7520 /* we only act on the first udp timeout message, others are irrelevant
7521 * and can be ignored. */
7522 if (!ignore_timeout)
7523 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
7525 gst_message_unref (message);
7528 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7531 case GST_MESSAGE_ERROR:
7534 GstRTSPStream *stream;
7537 udpsrc = GST_MESSAGE_SRC (message);
7539 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7540 GST_ELEMENT_NAME (udpsrc));
7542 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7546 /* we ignore the RTCP udpsrc */
7547 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7550 /* if we get error messages from the udp sources, that's not a problem as
7551 * long as not all of them error out. We also don't really know what the
7552 * problem is, the message does not give enough detail... */
7553 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7554 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7555 if (ret != GST_FLOW_OK)
7559 gst_message_unref (message);
7563 /* fatal but not our message, forward */
7564 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7569 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7575 /* the thread where everything happens */
7577 gst_rtspsrc_thread (GstRTSPSrc * src)
7581 GST_OBJECT_LOCK (src);
7582 cmd = src->pending_cmd;
7583 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7584 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7585 src->pending_cmd = CMD_LOOP;
7587 src->pending_cmd = CMD_WAIT;
7588 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
7590 /* we got the message command, so ensure communication is possible again */
7591 gst_rtspsrc_connection_flush (src, FALSE);
7593 src->busy_cmd = cmd;
7594 GST_OBJECT_UNLOCK (src);
7598 gst_rtspsrc_open (src, TRUE);
7601 gst_rtspsrc_play (src, &src->segment, TRUE);
7604 gst_rtspsrc_pause (src, TRUE);
7607 gst_rtspsrc_close (src, TRUE, FALSE);
7610 gst_rtspsrc_loop (src);
7613 gst_rtspsrc_reconnect (src, FALSE);
7619 GST_OBJECT_LOCK (src);
7620 /* and go back to sleep */
7621 if (src->pending_cmd == CMD_WAIT) {
7623 gst_task_pause (src->task);
7626 src->busy_cmd = CMD_WAIT;
7627 GST_OBJECT_UNLOCK (src);
7631 gst_rtspsrc_start (GstRTSPSrc * src)
7633 GST_DEBUG_OBJECT (src, "starting");
7635 GST_OBJECT_LOCK (src);
7637 src->pending_cmd = CMD_WAIT;
7639 if (src->task == NULL) {
7640 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7641 if (src->task == NULL)
7644 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7646 GST_OBJECT_UNLOCK (src);
7653 GST_OBJECT_UNLOCK (src);
7654 GST_ERROR_OBJECT (src, "failed to create task");
7660 gst_rtspsrc_stop (GstRTSPSrc * src)
7664 GST_DEBUG_OBJECT (src, "stopping");
7666 /* also cancels pending task */
7667 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7669 GST_OBJECT_LOCK (src);
7670 if ((task = src->task)) {
7672 GST_OBJECT_UNLOCK (src);
7674 gst_task_stop (task);
7676 /* make sure it is not running */
7677 GST_RTSP_STREAM_LOCK (src);
7678 GST_RTSP_STREAM_UNLOCK (src);
7680 /* now wait for the task to finish */
7681 gst_task_join (task);
7683 /* and free the task */
7684 gst_object_unref (GST_OBJECT (task));
7686 GST_OBJECT_LOCK (src);
7688 GST_OBJECT_UNLOCK (src);
7690 /* ensure synchronously all is closed and clean */
7691 gst_rtspsrc_close (src, FALSE, TRUE);
7696 static GstStateChangeReturn
7697 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
7699 GstRTSPSrc *rtspsrc;
7700 GstStateChangeReturn ret;
7702 rtspsrc = GST_RTSPSRC (element);
7704 switch (transition) {
7705 case GST_STATE_CHANGE_NULL_TO_READY:
7706 if (!gst_rtspsrc_start (rtspsrc))
7709 case GST_STATE_CHANGE_READY_TO_PAUSED:
7710 /* init some state */
7711 rtspsrc->cur_protocols = rtspsrc->protocols;
7712 /* first attempt, don't ignore timeouts */
7713 rtspsrc->ignore_timeout = FALSE;
7714 rtspsrc->open_error = FALSE;
7715 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
7717 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7718 set_manager_buffer_mode (rtspsrc);
7720 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7721 /* unblock the tcp tasks and make the loop waiting */
7722 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
7723 /* make sure it is waiting before we send PAUSE or PLAY below */
7724 GST_RTSP_STREAM_LOCK (rtspsrc);
7725 GST_RTSP_STREAM_UNLOCK (rtspsrc);
7728 case GST_STATE_CHANGE_PAUSED_TO_READY:
7734 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
7735 if (ret == GST_STATE_CHANGE_FAILURE)
7738 switch (transition) {
7739 case GST_STATE_CHANGE_NULL_TO_READY:
7740 ret = GST_STATE_CHANGE_SUCCESS;
7742 case GST_STATE_CHANGE_READY_TO_PAUSED:
7743 ret = GST_STATE_CHANGE_NO_PREROLL;
7745 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7746 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
7747 ret = GST_STATE_CHANGE_SUCCESS;
7749 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7750 /* send pause request and keep the idle task around */
7751 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
7752 ret = GST_STATE_CHANGE_NO_PREROLL;
7754 case GST_STATE_CHANGE_PAUSED_TO_READY:
7755 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_ALL);
7756 ret = GST_STATE_CHANGE_SUCCESS;
7758 case GST_STATE_CHANGE_READY_TO_NULL:
7759 gst_rtspsrc_stop (rtspsrc);
7760 ret = GST_STATE_CHANGE_SUCCESS;
7763 /* Otherwise it's success, we don't want to return spurious
7764 * NO_PREROLL or ASYNC from internal elements as we care for
7765 * state changes ourselves here
7767 * This is to catch PAUSED->PAUSED and PLAYING->PLAYING transitions.
7769 if (GST_STATE_TRANSITION_NEXT (transition) == GST_STATE_PAUSED)
7770 ret = GST_STATE_CHANGE_NO_PREROLL;
7772 ret = GST_STATE_CHANGE_SUCCESS;
7781 GST_DEBUG_OBJECT (rtspsrc, "start failed");
7782 return GST_STATE_CHANGE_FAILURE;
7787 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
7790 GstRTSPSrc *rtspsrc;
7792 rtspsrc = GST_RTSPSRC (element);
7794 if (GST_EVENT_IS_DOWNSTREAM (event)) {
7795 res = gst_rtspsrc_push_event (rtspsrc, event);
7797 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
7804 /*** GSTURIHANDLER INTERFACE *************************************************/
7807 gst_rtspsrc_uri_get_type (GType type)
7812 static const gchar *const *
7813 gst_rtspsrc_uri_get_protocols (GType type)
7815 static const gchar *protocols[] =
7816 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
7817 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
7824 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
7826 GstRTSPSrc *src = GST_RTSPSRC (handler);
7828 /* FIXME: make thread-safe */
7829 return g_strdup (src->conninfo.location);
7833 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
7839 GstRTSPUrl *newurl = NULL;
7840 GstSDPMessage *sdp = NULL;
7842 src = GST_RTSPSRC (handler);
7844 /* same URI, we're fine */
7845 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
7848 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
7849 sres = gst_sdp_message_new (&sdp);
7853 GST_DEBUG_OBJECT (src, "parsing SDP message");
7854 sres = gst_sdp_message_parse_uri (uri, sdp);
7859 GST_DEBUG_OBJECT (src, "parsing URI");
7860 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
7864 /* if worked, free previous and store new url object along with the original
7866 GST_DEBUG_OBJECT (src, "configuring URI");
7867 g_free (src->conninfo.location);
7868 src->conninfo.location = g_strdup (uri);
7869 gst_rtsp_url_free (src->conninfo.url);
7870 src->conninfo.url = newurl;
7871 g_free (src->conninfo.url_str);
7873 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
7875 src->conninfo.url_str = NULL;
7878 gst_sdp_message_free (src->sdp);
7880 src->from_sdp = sdp != NULL;
7882 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
7883 GST_DEBUG_OBJECT (src, "request uri is: %s",
7884 GST_STR_NULL (src->conninfo.url_str));
7891 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
7896 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
7897 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7898 "Could not create SDP");
7903 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
7904 GST_STR_NULL (uri));
7905 gst_sdp_message_free (sdp);
7906 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7912 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
7913 GST_STR_NULL (uri), res);
7914 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7915 "Invalid RTSP URI");
7921 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
7923 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
7925 iface->get_type = gst_rtspsrc_uri_get_type;
7926 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
7927 iface->get_uri = gst_rtspsrc_uri_get_uri;
7928 iface->set_uri = gst_rtspsrc_uri_set_uri;