2 * Copyright (C) <2005,2006> Wim Taymans <wim at fluendo dot com>
3 * <2006> Lutz Mueller <lutz at topfrose dot de>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 * Unless otherwise indicated, Source Code is licensed under MIT license.
22 * See further explanation attached in License Statement (distributed in the file
25 * Permission is hereby granted, free of charge, to any person obtaining a copy of
26 * this software and associated documentation files (the "Software"), to deal in
27 * the Software without restriction, including without limitation the rights to
28 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
29 * of the Software, and to permit persons to whom the Software is furnished to do
30 * so, subject to the following conditions:
32 * The above copyright notice and this permission notice shall be included in all
33 * copies or substantial portions of the Software.
35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44 * SECTION:element-rtspsrc
46 * Makes a connection to an RTSP server and read the data.
47 * rtspsrc strictly follows RFC 2326 and therefore does not (yet) support
48 * RealMedia/Quicktime/Microsoft extensions.
50 * RTSP supports transport over TCP or UDP in unicast or multicast mode. By
51 * default rtspsrc will negotiate a connection in the following order:
52 * UDP unicast/UDP multicast/TCP. The order cannot be changed but the allowed
53 * protocols can be controlled with the #GstRTSPSrc:protocols property.
55 * rtspsrc currently understands SDP as the format of the session description.
56 * For each stream listed in the SDP a new rtp_stream\%d pad will be created
57 * with caps derived from the SDP media description. This is a caps of mime type
58 * "application/x-rtp" that can be connected to any available RTP depayloader
61 * rtspsrc will internally instantiate an RTP session manager element
62 * that will handle the RTCP messages to and from the server, jitter removal,
63 * packet reordering along with providing a clock for the pipeline.
64 * This feature is implemented using the gstrtpbin element.
66 * rtspsrc acts like a live source and will therefore only generate data in the
70 * <title>Example launch line</title>
72 * gst-launch-1.0 rtspsrc location=rtsp://some.server/url ! fakesink
73 * ]| Establish a connection to an RTSP server and send the raw RTP packets to a
84 #endif /* HAVE_UNISTD_H */
90 #include <gst/net/gstnet.h>
91 #include <gst/sdp/gstsdpmessage.h>
92 #include <gst/sdp/gstmikey.h>
93 #include <gst/rtp/rtp.h>
95 #include "gst/gst-i18n-plugin.h"
97 #include "gstrtspsrc.h"
99 GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
100 #define GST_CAT_DEFAULT (rtspsrc_debug)
102 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream_%u",
105 GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
107 /* templates used internally */
108 static GstStaticPadTemplate anysrctemplate =
109 GST_STATIC_PAD_TEMPLATE ("internalsrc_%u",
112 GST_STATIC_CAPS_ANY);
114 static GstStaticPadTemplate anysinktemplate =
115 GST_STATIC_PAD_TEMPLATE ("internalsink_%u",
118 GST_STATIC_CAPS_ANY);
122 SIGNAL_HANDLE_REQUEST,
124 SIGNAL_SELECT_STREAM,
126 SIGNAL_REQUEST_RTCP_KEY,
130 enum _GstRtspSrcRtcpSyncMode
137 enum _GstRtspSrcBufferMode
146 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
148 gst_rtsp_src_buffer_mode_get_type (void)
150 static GType buffer_mode_type = 0;
151 static const GEnumValue buffer_modes[] = {
152 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
153 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
154 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
155 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
156 {BUFFER_MODE_SYNCED, "Synchronized sender and receiver clocks", "synced"},
160 if (!buffer_mode_type) {
162 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
164 return buffer_mode_type;
167 enum _GstRtspSrcNtpTimeSource
170 NTP_TIME_SOURCE_UNIX,
171 NTP_TIME_SOURCE_RUNNING_TIME,
172 NTP_TIME_SOURCE_CLOCK_TIME
175 #define GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE (gst_rtsp_src_ntp_time_source_get_type())
177 gst_rtsp_src_ntp_time_source_get_type (void)
179 static GType ntp_time_source_type = 0;
180 static const GEnumValue ntp_time_source_values[] = {
181 {NTP_TIME_SOURCE_NTP, "NTP time based on realtime clock", "ntp"},
182 {NTP_TIME_SOURCE_UNIX, "UNIX time based on realtime clock", "unix"},
183 {NTP_TIME_SOURCE_RUNNING_TIME,
184 "Running time based on pipeline clock",
186 {NTP_TIME_SOURCE_CLOCK_TIME, "Pipeline clock time", "clock-time"},
190 if (!ntp_time_source_type) {
191 ntp_time_source_type =
192 g_enum_register_static ("GstRTSPSrcNtpTimeSource",
193 ntp_time_source_values);
195 return ntp_time_source_type;
198 #define DEFAULT_LOCATION NULL
199 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
200 #define DEFAULT_DEBUG FALSE
201 #define DEFAULT_RETRY 20
202 #define DEFAULT_TIMEOUT 5000000
203 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
204 #define DEFAULT_TCP_TIMEOUT 20000000
205 #define DEFAULT_LATENCY_MS 2000
206 #define DEFAULT_DROP_ON_LATENCY FALSE
207 #define DEFAULT_CONNECTION_SPEED 0
208 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
209 #define DEFAULT_DO_RTCP TRUE
210 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
211 #define DEFAULT_PROXY NULL
212 #define DEFAULT_RTP_BLOCKSIZE 0
213 #define DEFAULT_USER_ID NULL
214 #define DEFAULT_USER_PW NULL
215 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
216 #define DEFAULT_PORT_RANGE NULL
217 #define DEFAULT_SHORT_HEADER FALSE
218 #define DEFAULT_PROBATION 2
219 #define DEFAULT_UDP_RECONNECT TRUE
220 #define DEFAULT_MULTICAST_IFACE NULL
221 #define DEFAULT_NTP_SYNC FALSE
222 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
223 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
224 #define DEFAULT_TLS_DATABASE NULL
225 #define DEFAULT_TLS_INTERACTION NULL
226 #define DEFAULT_DO_RETRANSMISSION TRUE
227 #define DEFAULT_NTP_TIME_SOURCE NTP_TIME_SOURCE_NTP
228 #define DEFAULT_USER_AGENT "GStreamer/" PACKAGE_VERSION
229 #define DEFAULT_MAX_RTCP_RTP_TIME_DIFF 1000
230 #define DEFAULT_RFC7273_SYNC FALSE
242 PROP_DROP_ON_LATENCY,
243 PROP_CONNECTION_SPEED,
246 PROP_DO_RTSP_KEEP_ALIVE,
255 PROP_UDP_BUFFER_SIZE,
259 PROP_MULTICAST_IFACE,
261 PROP_USE_PIPELINE_CLOCK,
263 PROP_TLS_VALIDATION_FLAGS,
265 PROP_TLS_INTERACTION,
266 PROP_DO_RETRANSMISSION,
267 PROP_NTP_TIME_SOURCE,
269 PROP_MAX_RTCP_RTP_TIME_DIFF,
273 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
275 gst_rtsp_nat_method_get_type (void)
277 static GType rtsp_nat_method_type = 0;
278 static const GEnumValue rtsp_nat_method[] = {
279 {GST_RTSP_NAT_NONE, "None", "none"},
280 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
284 if (!rtsp_nat_method_type) {
285 rtsp_nat_method_type =
286 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
288 return rtsp_nat_method_type;
291 static void gst_rtspsrc_finalize (GObject * object);
293 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
294 const GValue * value, GParamSpec * pspec);
295 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
296 GValue * value, GParamSpec * pspec);
298 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
300 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
301 gpointer iface_data);
303 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
304 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
306 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
307 GstStateChange transition);
308 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
309 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
311 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
312 GstRTSPMessage * response);
314 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
316 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
317 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
319 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
320 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
322 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
323 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
324 gboolean only_close);
326 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
327 const gchar * uri, GError ** error);
328 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
330 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
331 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
332 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
333 GstRTSPStream * stream, GstEvent * event);
334 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
335 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
336 static GstRTSPResult gst_rtsp_conninfo_close (GstRTSPSrc * src,
337 GstRTSPConnInfo * info, gboolean free);
345 /* commands we send to out loop to notify it of events */
346 #define CMD_OPEN (1 << 0)
347 #define CMD_PLAY (1 << 1)
348 #define CMD_PAUSE (1 << 2)
349 #define CMD_CLOSE (1 << 3)
350 #define CMD_WAIT (1 << 4)
351 #define CMD_RECONNECT (1 << 5)
352 #define CMD_LOOP (1 << 6)
354 /* mask for all commands */
355 #define CMD_ALL ((CMD_LOOP << 1) - 1)
357 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
359 gchar *__txt = _gst_element_error_printf text; \
360 gst_element_post_message (GST_ELEMENT_CAST (el), \
361 gst_message_new_progress (GST_OBJECT_CAST (el), \
362 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
366 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
368 #define gst_rtspsrc_parent_class parent_class
369 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
370 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
372 #ifndef GST_DISABLE_GST_DEBUG
373 static inline const char *
374 cmd_to_string (guint cmd)
398 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
400 GST_DEBUG_OBJECT (src, "default handler");
405 select_stream_accum (GSignalInvocationHint * ihint,
406 GValue * return_accu, const GValue * handler_return, gpointer data)
410 myboolean = g_value_get_boolean (handler_return);
411 GST_DEBUG ("accum %d", myboolean);
412 g_value_set_boolean (return_accu, myboolean);
414 /* stop emission if FALSE */
419 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
421 GObjectClass *gobject_class;
422 GstElementClass *gstelement_class;
423 GstBinClass *gstbin_class;
425 gobject_class = (GObjectClass *) klass;
426 gstelement_class = (GstElementClass *) klass;
427 gstbin_class = (GstBinClass *) klass;
429 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
431 gobject_class->set_property = gst_rtspsrc_set_property;
432 gobject_class->get_property = gst_rtspsrc_get_property;
434 gobject_class->finalize = gst_rtspsrc_finalize;
436 g_object_class_install_property (gobject_class, PROP_LOCATION,
437 g_param_spec_string ("location", "RTSP Location",
438 "Location of the RTSP url to read",
439 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
441 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
442 g_param_spec_flags ("protocols", "Protocols",
443 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
444 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
446 g_object_class_install_property (gobject_class, PROP_DEBUG,
447 g_param_spec_boolean ("debug", "Debug",
448 "Dump request and response messages to stdout",
449 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
451 g_object_class_install_property (gobject_class, PROP_RETRY,
452 g_param_spec_uint ("retry", "Retry",
453 "Max number of retries when allocating RTP ports.",
454 0, G_MAXUINT16, DEFAULT_RETRY,
455 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
457 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
458 g_param_spec_uint64 ("timeout", "Timeout",
459 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
460 0, G_MAXUINT64, DEFAULT_TIMEOUT,
461 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
463 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
464 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
465 "Fail after timeout microseconds on TCP connections (0 = disabled)",
466 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
467 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
469 g_object_class_install_property (gobject_class, PROP_LATENCY,
470 g_param_spec_uint ("latency", "Buffer latency in ms",
471 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
472 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
474 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
475 g_param_spec_boolean ("drop-on-latency",
476 "Drop buffers when maximum latency is reached",
477 "Tells the jitterbuffer to never exceed the given latency in size",
478 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
480 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
481 g_param_spec_uint64 ("connection-speed", "Connection Speed",
482 "Network connection speed in kbps (0 = unknown)",
483 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
484 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
486 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
487 g_param_spec_enum ("nat-method", "NAT Method",
488 "Method to use for traversing firewalls and NAT",
489 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
490 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
493 * GstRTSPSrc:do-rtcp:
495 * Enable RTCP support. Some old server don't like RTCP and then this property
496 * needs to be set to FALSE.
498 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
499 g_param_spec_boolean ("do-rtcp", "Do RTCP",
500 "Send RTCP packets, disable for old incompatible server.",
501 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
504 * GstRTSPSrc:do-rtsp-keep-alive:
506 * Enable RTSP keep alive support. Some old server don't like RTSP
507 * keep alive and then this property needs to be set to FALSE.
509 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
510 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
511 "Send RTSP keep alive packets, disable for old incompatible server.",
512 DEFAULT_DO_RTSP_KEEP_ALIVE,
513 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
518 * Set the proxy parameters. This has to be a string of the format
519 * [http://][user:passwd@]host[:port].
521 g_object_class_install_property (gobject_class, PROP_PROXY,
522 g_param_spec_string ("proxy", "Proxy",
523 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
524 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
526 * GstRTSPSrc:proxy-id:
528 * Sets the proxy URI user id for authentication. If the URI set via the
529 * "proxy" property contains a user-id already, that will take precedence.
533 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
534 g_param_spec_string ("proxy-id", "proxy-id",
535 "HTTP proxy URI user id for authentication", "",
536 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
538 * GstRTSPSrc:proxy-pw:
540 * Sets the proxy URI password for authentication. If the URI set via the
541 * "proxy" property contains a password already, that will take precedence.
545 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
546 g_param_spec_string ("proxy-pw", "proxy-pw",
547 "HTTP proxy URI user password for authentication", "",
548 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
551 * GstRTSPSrc:rtp-blocksize:
553 * RTP package size to suggest to server.
555 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
556 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
557 "RTP package size to suggest to server (0 = disabled)",
558 0, 65536, DEFAULT_RTP_BLOCKSIZE,
559 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
561 g_object_class_install_property (gobject_class,
563 g_param_spec_string ("user-id", "user-id",
564 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
565 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
566 g_object_class_install_property (gobject_class, PROP_USER_PW,
567 g_param_spec_string ("user-pw", "user-pw",
568 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
569 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
572 * GstRTSPSrc:buffer-mode:
574 * Control the buffering and timestamping mode used by the jitterbuffer.
576 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
577 g_param_spec_enum ("buffer-mode", "Buffer Mode",
578 "Control the buffering algorithm in use",
579 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
580 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
583 * GstRTSPSrc:port-range:
585 * Configure the client port numbers that can be used to recieve RTP and
588 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
589 g_param_spec_string ("port-range", "Port range",
590 "Client port range that can be used to receive RTP and RTCP data, "
591 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
592 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
595 * GstRTSPSrc:udp-buffer-size:
597 * Size of the kernel UDP receive buffer in bytes.
599 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
600 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
601 "Size of the kernel UDP receive buffer in bytes, 0=default",
602 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
603 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
606 * GstRTSPSrc:short-header:
608 * Only send the basic RTSP headers for broken encoders.
610 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
611 g_param_spec_boolean ("short-header", "Short Header",
612 "Only send the basic RTSP headers for broken encoders",
613 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
615 g_object_class_install_property (gobject_class, PROP_PROBATION,
616 g_param_spec_uint ("probation", "Number of probations",
617 "Consecutive packet sequence numbers to accept the source",
618 0, G_MAXUINT, DEFAULT_PROBATION,
619 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
621 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
622 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
623 "Reconnect to the server if RTSP connection is closed when doing UDP",
624 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
626 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
627 g_param_spec_string ("multicast-iface", "Multicast Interface",
628 "The network interface on which to join the multicast group",
629 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
631 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
632 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
633 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
634 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
636 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
637 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
638 "Use the pipeline running-time to set the NTP time in the RTCP SR messages"
639 "(DEPRECATED: Use ntp-time-source property)",
640 DEFAULT_USE_PIPELINE_CLOCK,
641 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
643 g_object_class_install_property (gobject_class, PROP_SDES,
644 g_param_spec_boxed ("sdes", "SDES",
645 "The SDES items of this session",
646 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
649 * GstRTSPSrc::tls-validation-flags:
651 * TLS certificate validation flags used to validate server
656 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
657 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
658 "TLS certificate validation flags used to validate the server certificate",
659 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
660 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
663 * GstRTSPSrc::tls-database:
665 * TLS database with anchor certificate authorities used to validate
666 * the server certificate.
670 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
671 g_param_spec_object ("tls-database", "TLS database",
672 "TLS database with anchor certificate authorities used to validate the server certificate",
673 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
676 * GstRTSPSrc::tls-interaction:
678 * A #GTlsInteraction object to be used when the connection or certificate
679 * database need to interact with the user. This will be used to prompt the
680 * user for passwords where necessary.
684 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
685 g_param_spec_object ("tls-interaction", "TLS interaction",
686 "A GTlsInteraction object to promt the user for password or certificate",
687 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
690 * GstRTSPSrc::do-retransmission:
692 * Attempt to ask the server to retransmit lost packets according to RFC4588.
694 * Note: currently only works with SSRC-multiplexed retransmission streams
698 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
699 g_param_spec_boolean ("do-retransmission", "Retransmission",
700 "Ask the server to retransmit lost packets",
701 DEFAULT_DO_RETRANSMISSION,
702 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
705 * GstRTSPSrc::ntp-time-source:
707 * allows to select the time source that should be used
708 * for the NTP time in RTCP packets
712 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
713 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
714 "NTP time source for RTCP packets",
715 GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE, DEFAULT_NTP_TIME_SOURCE,
716 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
719 * GstRTSPSrc::user-agent:
721 * The string to set in the User-Agent header.
725 g_object_class_install_property (gobject_class, PROP_USER_AGENT,
726 g_param_spec_string ("user-agent", "User Agent",
727 "The User-Agent string to send to the server",
728 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
730 g_object_class_install_property (gobject_class, PROP_MAX_RTCP_RTP_TIME_DIFF,
731 g_param_spec_int ("max-rtcp-rtp-time-diff", "Max RTCP RTP Time Diff",
732 "Maximum amount of time in ms that the RTP time in RTCP SRs "
733 "is allowed to be ahead (-1 disabled)", -1, G_MAXINT,
734 DEFAULT_MAX_RTCP_RTP_TIME_DIFF,
735 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
737 g_object_class_install_property (gobject_class, PROP_RFC7273_SYNC,
738 g_param_spec_boolean ("rfc7273-sync", "Sync on RFC7273 clock",
739 "Synchronize received streams to the RFC7273 clock "
740 "(requires clock and offset to be provided)", DEFAULT_RFC7273_SYNC,
741 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
744 * GstRTSPSrc::handle-request:
745 * @rtspsrc: a #GstRTSPSrc
746 * @request: a #GstRTSPMessage
747 * @response: a #GstRTSPMessage
749 * Handle a server request in @request and prepare @response.
751 * This signal is called from the streaming thread, you should therefore not
752 * do any state changes on @rtspsrc because this might deadlock. If you want
753 * to modify the state as a result of this signal, post a
754 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
759 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
760 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
761 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
762 G_TYPE_POINTER, G_TYPE_POINTER);
765 * GstRTSPSrc::on-sdp:
766 * @rtspsrc: a #GstRTSPSrc
767 * @sdp: a #GstSDPMessage
769 * Emited when the client has retrieved the SDP and before it configures the
770 * streams in the SDP. @sdp can be inspected and modified.
772 * This signal is called from the streaming thread, you should therefore not
773 * do any state changes on @rtspsrc because this might deadlock. If you want
774 * to modify the state as a result of this signal, post a
775 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
780 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
781 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
782 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
783 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
786 * GstRTSPSrc::select-stream:
787 * @rtspsrc: a #GstRTSPSrc
788 * @num: the stream number
789 * @caps: the stream caps
791 * Emited before the client decides to configure the stream @num with
794 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
799 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
800 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
801 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
802 (GCallback) default_select_stream, select_stream_accum, NULL,
803 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
806 * GstRTSPSrc::new-manager:
807 * @rtspsrc: a #GstRTSPSrc
808 * @manager: a #GstElement
810 * Emited after a new manager (like rtpbin) was created and the default
811 * properties were configured.
815 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
816 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
817 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
818 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
821 * GstRTSPSrc::request-rtcp-key:
822 * @rtspsrc: a #GstRTSPSrc
823 * @num: the stream number
825 * Signal emited to get the crypto parameters relevant to the RTCP
826 * stream. User should provide the key and the RTCP encryption ciphers
827 * and authentication, and return them wrapped in a GstCaps.
831 gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY] =
832 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
833 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
835 gstelement_class->send_event = gst_rtspsrc_send_event;
836 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
837 gstelement_class->change_state = gst_rtspsrc_change_state;
839 gst_element_class_add_static_pad_template (gstelement_class, &rtptemplate);
841 gst_element_class_set_static_metadata (gstelement_class,
842 "RTSP packet receiver", "Source/Network",
843 "Receive data over the network via RTSP (RFC 2326)",
844 "Wim Taymans <wim@fluendo.com>, "
845 "Thijs Vermeir <thijs.vermeir@barco.com>, "
846 "Lutz Mueller <lutz@topfrose.de>");
848 gstbin_class->handle_message = gst_rtspsrc_handle_message;
850 gst_rtsp_ext_list_init ();
854 gst_rtspsrc_init (GstRTSPSrc * src)
856 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
857 src->protocols = DEFAULT_PROTOCOLS;
858 src->debug = DEFAULT_DEBUG;
859 src->retry = DEFAULT_RETRY;
860 src->udp_timeout = DEFAULT_TIMEOUT;
861 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
862 src->latency = DEFAULT_LATENCY_MS;
863 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
864 src->connection_speed = DEFAULT_CONNECTION_SPEED;
865 src->nat_method = DEFAULT_NAT_METHOD;
866 src->do_rtcp = DEFAULT_DO_RTCP;
867 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
868 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
869 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
870 src->user_id = g_strdup (DEFAULT_USER_ID);
871 src->user_pw = g_strdup (DEFAULT_USER_PW);
872 src->buffer_mode = DEFAULT_BUFFER_MODE;
873 src->client_port_range.min = 0;
874 src->client_port_range.max = 0;
875 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
876 src->short_header = DEFAULT_SHORT_HEADER;
877 src->probation = DEFAULT_PROBATION;
878 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
879 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
880 src->ntp_sync = DEFAULT_NTP_SYNC;
881 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
883 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
884 src->tls_database = DEFAULT_TLS_DATABASE;
885 src->tls_interaction = DEFAULT_TLS_INTERACTION;
886 src->do_retransmission = DEFAULT_DO_RETRANSMISSION;
887 src->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
888 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
889 src->max_rtcp_rtp_time_diff = DEFAULT_MAX_RTCP_RTP_TIME_DIFF;
890 src->rfc7273_sync = DEFAULT_RFC7273_SYNC;
892 /* get a list of all extensions */
893 src->extensions = gst_rtsp_ext_list_get ();
895 /* connect to send signal */
896 gst_rtsp_ext_list_connect (src->extensions, "send",
897 (GCallback) gst_rtspsrc_send_cb, src);
899 /* protects the streaming thread in interleaved mode or the polling
900 * thread in UDP mode. */
901 g_rec_mutex_init (&src->stream_rec_lock);
903 /* protects our state changes from multiple invocations */
904 g_rec_mutex_init (&src->state_rec_lock);
906 src->state = GST_RTSP_STATE_INVALID;
908 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
912 gst_rtspsrc_finalize (GObject * object)
916 rtspsrc = GST_RTSPSRC (object);
918 gst_rtsp_ext_list_free (rtspsrc->extensions);
919 g_free (rtspsrc->conninfo.location);
920 gst_rtsp_url_free (rtspsrc->conninfo.url);
921 g_free (rtspsrc->conninfo.url_str);
922 g_free (rtspsrc->user_id);
923 g_free (rtspsrc->user_pw);
924 g_free (rtspsrc->multi_iface);
925 g_free (rtspsrc->user_agent);
928 gst_sdp_message_free (rtspsrc->sdp);
931 if (rtspsrc->provided_clock)
932 gst_object_unref (rtspsrc->provided_clock);
935 gst_structure_free (rtspsrc->sdes);
937 if (rtspsrc->tls_database)
938 g_object_unref (rtspsrc->tls_database);
940 if (rtspsrc->tls_interaction)
941 g_object_unref (rtspsrc->tls_interaction);
944 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
945 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
947 G_OBJECT_CLASS (parent_class)->finalize (object);
951 gst_rtspsrc_provide_clock (GstElement * element)
953 GstRTSPSrc *src = GST_RTSPSRC (element);
956 if ((clock = src->provided_clock) != NULL)
957 gst_object_ref (clock);
962 /* a proxy string of the format [user:passwd@]host[:port] */
964 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
968 g_free (rtsp->proxy_user);
969 rtsp->proxy_user = NULL;
970 g_free (rtsp->proxy_passwd);
971 rtsp->proxy_passwd = NULL;
972 g_free (rtsp->proxy_host);
973 rtsp->proxy_host = NULL;
974 rtsp->proxy_port = 0;
981 /* we allow http:// in front but ignore it */
982 if (g_str_has_prefix (p, "http://"))
985 at = strchr (p, '@');
987 /* look for user:passwd */
988 col = strchr (proxy, ':');
989 if (col == NULL || col > at)
992 rtsp->proxy_user = g_strndup (p, col - p);
994 rtsp->proxy_passwd = g_strndup (col, at - col);
999 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
1000 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
1001 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
1002 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
1003 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
1004 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
1005 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
1008 col = strchr (p, ':');
1011 /* everything before the colon is the hostname */
1012 rtsp->proxy_host = g_strndup (p, col - p);
1014 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
1016 rtsp->proxy_host = g_strdup (p);
1017 rtsp->proxy_port = 8080;
1023 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
1025 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
1026 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
1029 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
1031 rtspsrc->ptcp_timeout = NULL;
1035 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
1038 GstRTSPSrc *rtspsrc;
1040 rtspsrc = GST_RTSPSRC (object);
1044 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
1045 g_value_get_string (value), NULL);
1047 case PROP_PROTOCOLS:
1048 rtspsrc->protocols = g_value_get_flags (value);
1051 rtspsrc->debug = g_value_get_boolean (value);
1054 rtspsrc->retry = g_value_get_uint (value);
1057 rtspsrc->udp_timeout = g_value_get_uint64 (value);
1059 case PROP_TCP_TIMEOUT:
1060 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
1063 rtspsrc->latency = g_value_get_uint (value);
1065 case PROP_DROP_ON_LATENCY:
1066 rtspsrc->drop_on_latency = g_value_get_boolean (value);
1068 case PROP_CONNECTION_SPEED:
1069 rtspsrc->connection_speed = g_value_get_uint64 (value);
1071 case PROP_NAT_METHOD:
1072 rtspsrc->nat_method = g_value_get_enum (value);
1075 rtspsrc->do_rtcp = g_value_get_boolean (value);
1077 case PROP_DO_RTSP_KEEP_ALIVE:
1078 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
1081 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
1084 g_free (rtspsrc->prop_proxy_id);
1085 rtspsrc->prop_proxy_id = g_value_dup_string (value);
1088 g_free (rtspsrc->prop_proxy_pw);
1089 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
1091 case PROP_RTP_BLOCKSIZE:
1092 rtspsrc->rtp_blocksize = g_value_get_uint (value);
1095 g_free (rtspsrc->user_id);
1096 rtspsrc->user_id = g_value_dup_string (value);
1099 g_free (rtspsrc->user_pw);
1100 rtspsrc->user_pw = g_value_dup_string (value);
1102 case PROP_BUFFER_MODE:
1103 rtspsrc->buffer_mode = g_value_get_enum (value);
1105 case PROP_PORT_RANGE:
1109 str = g_value_get_string (value);
1110 if (sscanf (str, "%u-%u", &rtspsrc->client_port_range.min,
1111 &rtspsrc->client_port_range.max) != 2) {
1112 rtspsrc->client_port_range.min = 0;
1113 rtspsrc->client_port_range.max = 0;
1117 case PROP_UDP_BUFFER_SIZE:
1118 rtspsrc->udp_buffer_size = g_value_get_int (value);
1120 case PROP_SHORT_HEADER:
1121 rtspsrc->short_header = g_value_get_boolean (value);
1123 case PROP_PROBATION:
1124 rtspsrc->probation = g_value_get_uint (value);
1126 case PROP_UDP_RECONNECT:
1127 rtspsrc->udp_reconnect = g_value_get_boolean (value);
1129 case PROP_MULTICAST_IFACE:
1130 g_free (rtspsrc->multi_iface);
1132 if (g_value_get_string (value) == NULL)
1133 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1135 rtspsrc->multi_iface = g_value_dup_string (value);
1138 rtspsrc->ntp_sync = g_value_get_boolean (value);
1140 case PROP_USE_PIPELINE_CLOCK:
1141 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
1144 rtspsrc->sdes = g_value_dup_boxed (value);
1146 case PROP_TLS_VALIDATION_FLAGS:
1147 rtspsrc->tls_validation_flags = g_value_get_flags (value);
1149 case PROP_TLS_DATABASE:
1150 g_clear_object (&rtspsrc->tls_database);
1151 rtspsrc->tls_database = g_value_dup_object (value);
1153 case PROP_TLS_INTERACTION:
1154 g_clear_object (&rtspsrc->tls_interaction);
1155 rtspsrc->tls_interaction = g_value_dup_object (value);
1157 case PROP_DO_RETRANSMISSION:
1158 rtspsrc->do_retransmission = g_value_get_boolean (value);
1160 case PROP_NTP_TIME_SOURCE:
1161 rtspsrc->ntp_time_source = g_value_get_enum (value);
1163 case PROP_USER_AGENT:
1164 g_free (rtspsrc->user_agent);
1165 rtspsrc->user_agent = g_value_dup_string (value);
1167 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1168 rtspsrc->max_rtcp_rtp_time_diff = g_value_get_int (value);
1170 case PROP_RFC7273_SYNC:
1171 rtspsrc->rfc7273_sync = g_value_get_boolean (value);
1174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1180 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1183 GstRTSPSrc *rtspsrc;
1185 rtspsrc = GST_RTSPSRC (object);
1189 g_value_set_string (value, rtspsrc->conninfo.location);
1191 case PROP_PROTOCOLS:
1192 g_value_set_flags (value, rtspsrc->protocols);
1195 g_value_set_boolean (value, rtspsrc->debug);
1198 g_value_set_uint (value, rtspsrc->retry);
1201 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1203 case PROP_TCP_TIMEOUT:
1207 timeout = ((guint64) rtspsrc->tcp_timeout.tv_sec) * G_USEC_PER_SEC +
1208 rtspsrc->tcp_timeout.tv_usec;
1209 g_value_set_uint64 (value, timeout);
1213 g_value_set_uint (value, rtspsrc->latency);
1215 case PROP_DROP_ON_LATENCY:
1216 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1218 case PROP_CONNECTION_SPEED:
1219 g_value_set_uint64 (value, rtspsrc->connection_speed);
1221 case PROP_NAT_METHOD:
1222 g_value_set_enum (value, rtspsrc->nat_method);
1225 g_value_set_boolean (value, rtspsrc->do_rtcp);
1227 case PROP_DO_RTSP_KEEP_ALIVE:
1228 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1234 if (rtspsrc->proxy_host) {
1236 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1240 g_value_take_string (value, str);
1244 g_value_set_string (value, rtspsrc->prop_proxy_id);
1247 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1249 case PROP_RTP_BLOCKSIZE:
1250 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1253 g_value_set_string (value, rtspsrc->user_id);
1256 g_value_set_string (value, rtspsrc->user_pw);
1258 case PROP_BUFFER_MODE:
1259 g_value_set_enum (value, rtspsrc->buffer_mode);
1261 case PROP_PORT_RANGE:
1265 if (rtspsrc->client_port_range.min != 0) {
1266 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1267 rtspsrc->client_port_range.max);
1271 g_value_take_string (value, str);
1274 case PROP_UDP_BUFFER_SIZE:
1275 g_value_set_int (value, rtspsrc->udp_buffer_size);
1277 case PROP_SHORT_HEADER:
1278 g_value_set_boolean (value, rtspsrc->short_header);
1280 case PROP_PROBATION:
1281 g_value_set_uint (value, rtspsrc->probation);
1283 case PROP_UDP_RECONNECT:
1284 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1286 case PROP_MULTICAST_IFACE:
1287 g_value_set_string (value, rtspsrc->multi_iface);
1290 g_value_set_boolean (value, rtspsrc->ntp_sync);
1292 case PROP_USE_PIPELINE_CLOCK:
1293 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1296 g_value_set_boxed (value, rtspsrc->sdes);
1298 case PROP_TLS_VALIDATION_FLAGS:
1299 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1301 case PROP_TLS_DATABASE:
1302 g_value_set_object (value, rtspsrc->tls_database);
1304 case PROP_TLS_INTERACTION:
1305 g_value_set_object (value, rtspsrc->tls_interaction);
1307 case PROP_DO_RETRANSMISSION:
1308 g_value_set_boolean (value, rtspsrc->do_retransmission);
1310 case PROP_NTP_TIME_SOURCE:
1311 g_value_set_enum (value, rtspsrc->ntp_time_source);
1313 case PROP_USER_AGENT:
1314 g_value_set_string (value, rtspsrc->user_agent);
1316 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1317 g_value_set_int (value, rtspsrc->max_rtcp_rtp_time_diff);
1319 case PROP_RFC7273_SYNC:
1320 g_value_set_boolean (value, rtspsrc->rfc7273_sync);
1323 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1329 find_stream_by_id (GstRTSPStream * stream, gint * id)
1331 if (stream->id == *id)
1338 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1340 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1347 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1349 GstElement *src = (GstElement *) a;
1351 if (stream->udpsrc[0] == src)
1353 if (stream->udpsrc[1] == src)
1360 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1362 if (stream->conninfo.location) {
1363 /* check qualified setup_url */
1364 if (!strcmp (stream->conninfo.location, (gchar *) a))
1367 if (stream->control_url) {
1368 /* check original control_url */
1369 if (!strcmp (stream->control_url, (gchar *) a))
1372 /* check if qualified setup_url ends with string */
1373 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1380 static GstRTSPStream *
1381 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1385 /* find and get stream */
1386 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1387 return (GstRTSPStream *) lstream->data;
1392 static const GstSDPBandwidth *
1393 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1394 const GstSDPMedia * media, const gchar * type)
1398 /* first look in the media specific section */
1399 len = gst_sdp_media_bandwidths_len (media);
1400 for (i = 0; i < len; i++) {
1401 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1403 if (strcmp (bw->bwtype, type) == 0)
1406 /* then look in the message specific section */
1407 len = gst_sdp_message_bandwidths_len (sdp);
1408 for (i = 0; i < len; i++) {
1409 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1411 if (strcmp (bw->bwtype, type) == 0)
1418 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1419 const GstSDPMedia * media, GstRTSPStream * stream)
1421 const GstSDPBandwidth *bw;
1423 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1424 stream->as_bandwidth = bw->bandwidth;
1426 stream->as_bandwidth = -1;
1428 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1429 stream->rr_bandwidth = bw->bandwidth;
1431 stream->rr_bandwidth = -1;
1433 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1434 stream->rs_bandwidth = bw->bandwidth;
1436 stream->rs_bandwidth = -1;
1440 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1441 const GstSDPConnection * conn)
1443 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1446 if (conn->addrtype == NULL)
1449 /* check for IPV6 */
1450 if (strcmp (conn->addrtype, "IP4") == 0)
1451 stream->is_ipv6 = FALSE;
1452 else if (strcmp (conn->addrtype, "IP6") == 0)
1453 stream->is_ipv6 = TRUE;
1458 g_free (stream->destination);
1459 stream->destination = g_strdup (conn->address);
1461 /* check for multicast */
1462 stream->is_multicast =
1463 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1465 stream->ttl = conn->ttl;
1468 /* Go over the connections for a stream.
1469 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1471 * - If we are dealing with a localhost address, we disable multicast
1474 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1475 const GstSDPMedia * media, GstRTSPStream * stream)
1477 const GstSDPConnection *conn;
1480 /* first look in the media specific section */
1481 len = gst_sdp_media_connections_len (media);
1482 for (i = 0; i < len; i++) {
1483 conn = gst_sdp_media_get_connection (media, i);
1485 gst_rtspsrc_do_stream_connection (src, stream, conn);
1487 /* then look in the message specific section */
1488 if ((conn = gst_sdp_message_get_connection (sdp))) {
1489 gst_rtspsrc_do_stream_connection (src, stream, conn);
1493 /* m=<media> <UDP port> RTP/AVP <payload>
1496 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
1497 const GstSDPMedia * media, GstRTSPStream * stream)
1501 GstCaps *global_caps;
1504 proto = gst_sdp_media_get_proto (media);
1508 if (g_str_equal (proto, "RTP/AVP"))
1509 stream->profile = GST_RTSP_PROFILE_AVP;
1510 else if (g_str_equal (proto, "RTP/SAVP"))
1511 stream->profile = GST_RTSP_PROFILE_SAVP;
1512 else if (g_str_equal (proto, "RTP/AVPF"))
1513 stream->profile = GST_RTSP_PROFILE_AVPF;
1514 else if (g_str_equal (proto, "RTP/SAVPF"))
1515 stream->profile = GST_RTSP_PROFILE_SAVPF;
1519 /* Parse global SDP attributes once */
1520 global_caps = gst_caps_new_empty_simple ("application/x-unknown");
1521 GST_DEBUG ("mapping sdp session level attributes to caps");
1522 gst_sdp_message_attributes_to_caps (sdp, global_caps);
1523 GST_DEBUG ("mapping sdp media level attributes to caps");
1524 gst_sdp_media_attributes_to_caps (media, global_caps);
1526 /* Keep a copy of the SDP key management */
1527 gst_sdp_media_parse_keymgmt (media, &stream->mikey);
1528 if (stream->mikey == NULL)
1529 gst_sdp_message_parse_keymgmt (sdp, &stream->mikey);
1531 len = gst_sdp_media_formats_len (media);
1532 for (i = 0; i < len; i++) {
1534 GstCaps *caps, *outcaps;
1539 pt = atoi (gst_sdp_media_get_format (media, i));
1541 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
1544 caps = gst_sdp_media_get_caps_from_media (media, pt);
1546 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
1550 /* do some tweaks */
1551 s = gst_caps_get_structure (caps, 0);
1552 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
1553 stream->is_real = (strstr (enc, "-REAL") != NULL);
1554 if (strcmp (enc, "X-ASF-PF") == 0)
1555 stream->container = TRUE;
1558 /* Merge in global caps */
1559 /* Intersect will merge in missing fields to the current caps */
1560 outcaps = gst_caps_intersect (caps, global_caps);
1561 gst_caps_unref (caps);
1563 /* the first pt will be the default */
1564 if (stream->ptmap->len == 0)
1565 stream->default_pt = pt;
1568 item.caps = outcaps;
1570 g_array_append_val (stream->ptmap, item);
1573 gst_caps_unref (global_caps);
1578 GST_ERROR_OBJECT (src, "can't find proto in media");
1583 GST_ERROR_OBJECT (src, "unknown proto in media %s", proto);
1588 static const gchar *
1589 get_aggregate_control (GstRTSPSrc * src)
1594 base = src->control;
1595 else if (src->content_base)
1596 base = src->content_base;
1597 else if (src->conninfo.url_str)
1598 base = src->conninfo.url_str;
1606 clear_ptmap_item (PtMapItem * item)
1609 gst_caps_unref (item->caps);
1612 static GstRTSPStream *
1613 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1615 GstRTSPStream *stream;
1616 const gchar *control_url;
1617 const GstSDPMedia *media;
1619 /* get media, should not return NULL */
1620 media = gst_sdp_message_get_media (sdp, idx);
1624 stream = g_new0 (GstRTSPStream, 1);
1625 stream->parent = src;
1626 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1628 stream->last_ret = GST_FLOW_NOT_LINKED;
1629 stream->added = FALSE;
1630 stream->setup = FALSE;
1631 stream->skipped = FALSE;
1633 stream->eos = FALSE;
1634 stream->discont = TRUE;
1635 stream->seqbase = -1;
1636 stream->timebase = -1;
1637 stream->send_ssrc = g_random_int ();
1638 stream->profile = GST_RTSP_PROFILE_AVP;
1639 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1640 stream->mikey = NULL;
1641 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
1643 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1644 * session manager to scale RTCP. */
1645 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1647 /* collect connection info */
1648 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1650 /* make the payload type map */
1651 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1653 /* collect port number */
1654 stream->port = gst_sdp_media_get_port (media);
1656 /* get control url to construct the setup url. The setup url is used to
1657 * configure the transport of the stream and is used to identity the stream in
1658 * the RTP-Info header field returned from PLAY. */
1659 control_url = gst_sdp_media_get_attribute_val (media, "control");
1660 if (control_url == NULL)
1661 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1663 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1664 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1665 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1666 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1668 if (control_url != NULL) {
1669 stream->control_url = g_strdup (control_url);
1670 /* Build a fully qualified url using the content_base if any or by prefixing
1671 * the original request.
1672 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1673 * likely build a URL that the server will fail to understand, this is ok,
1674 * we will fail then. */
1675 if (g_str_has_prefix (control_url, "rtsp://"))
1676 stream->conninfo.location = g_strdup (control_url);
1681 if (g_strcmp0 (control_url, "*") == 0)
1684 base = get_aggregate_control (src);
1686 /* check if the base ends or control starts with / */
1687 has_slash = g_str_has_prefix (control_url, "/");
1688 has_slash = has_slash || g_str_has_suffix (base, "/");
1690 /* concatenate the two strings, insert / when not present */
1691 stream->conninfo.location =
1692 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1695 GST_DEBUG_OBJECT (src, " setup: %s",
1696 GST_STR_NULL (stream->conninfo.location));
1698 /* we keep track of all streams */
1699 src->streams = g_list_append (src->streams, stream);
1707 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1711 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1713 g_array_free (stream->ptmap, TRUE);
1715 g_free (stream->destination);
1716 g_free (stream->control_url);
1717 g_free (stream->conninfo.location);
1719 for (i = 0; i < 2; i++) {
1720 if (stream->udpsrc[i]) {
1721 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1722 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1723 gst_object_unref (stream->udpsrc[i]);
1725 if (stream->channelpad[i])
1726 gst_object_unref (stream->channelpad[i]);
1728 if (stream->udpsink[i]) {
1729 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1730 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1731 gst_object_unref (stream->udpsink[i]);
1734 if (stream->fakesrc) {
1735 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1736 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1737 gst_object_unref (stream->fakesrc);
1739 if (stream->srcpad) {
1740 gst_pad_set_active (stream->srcpad, FALSE);
1742 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1744 if (stream->srtpenc)
1745 gst_object_unref (stream->srtpenc);
1746 if (stream->srtpdec)
1747 gst_object_unref (stream->srtpdec);
1748 if (stream->srtcpparams)
1749 gst_caps_unref (stream->srtcpparams);
1751 gst_mikey_message_unref (stream->mikey);
1752 if (stream->rtcppad)
1753 gst_object_unref (stream->rtcppad);
1754 if (stream->session)
1755 g_object_unref (stream->session);
1756 if (stream->rtx_pt_map)
1757 gst_structure_free (stream->rtx_pt_map);
1762 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1766 GST_DEBUG_OBJECT (src, "cleanup");
1768 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1769 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1771 gst_rtspsrc_stream_free (src, stream);
1773 g_list_free (src->streams);
1774 src->streams = NULL;
1776 if (src->manager_sig_id) {
1777 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1778 src->manager_sig_id = 0;
1780 gst_element_set_state (src->manager, GST_STATE_NULL);
1781 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1782 src->manager = NULL;
1785 gst_structure_free (src->props);
1788 g_free (src->content_base);
1789 src->content_base = NULL;
1791 g_free (src->control);
1792 src->control = NULL;
1795 gst_rtsp_range_free (src->range);
1798 /* don't clear the SDP when it was used in the url */
1799 if (src->sdp && !src->from_sdp) {
1800 gst_sdp_message_free (src->sdp);
1804 src->need_segment = FALSE;
1806 if (src->provided_clock) {
1807 gst_object_unref (src->provided_clock);
1808 src->provided_clock = NULL;
1813 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1814 gint * rtpport, gint * rtcpport)
1817 GstStateChangeReturn ret;
1818 GstElement *udpsrc0, *udpsrc1;
1819 gint tmp_rtp, tmp_rtcp;
1823 src = stream->parent;
1829 /* Start at next port */
1830 tmp_rtp = src->next_port_num;
1832 if (stream->is_ipv6)
1833 host = "udp://[::0]";
1835 host = "udp://0.0.0.0";
1837 /* try to allocate 2 UDP ports, the RTP port should be an even
1838 * number and the RTCP port should be the next (uneven) port */
1841 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1842 tmp_rtp >= src->client_port_range.max)
1845 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1846 if (udpsrc0 == NULL)
1847 goto no_udp_protocol;
1848 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1850 if (src->udp_buffer_size != 0)
1851 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
1854 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
1855 if (ret == GST_STATE_CHANGE_FAILURE) {
1857 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
1860 if (++count > src->retry)
1863 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1864 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1865 gst_object_unref (udpsrc0);
1868 GST_DEBUG_OBJECT (src, "retry %d", count);
1871 goto no_udp_protocol;
1874 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
1875 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
1877 /* check if port is even */
1878 if ((tmp_rtp & 0x01) != 0) {
1879 /* port not even, close and allocate another */
1880 if (++count > src->retry)
1883 GST_DEBUG_OBJECT (src, "RTP port not even");
1885 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1886 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1887 gst_object_unref (udpsrc0);
1890 GST_DEBUG_OBJECT (src, "retry %d", count);
1895 /* allocate port+1 for RTCP now */
1896 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1897 if (udpsrc1 == NULL)
1898 goto no_udp_rtcp_protocol;
1901 tmp_rtcp = tmp_rtp + 1;
1902 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
1905 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
1907 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
1908 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
1909 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
1910 if (ret == GST_STATE_CHANGE_FAILURE) {
1911 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
1913 if (++count > src->retry)
1916 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1917 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1918 gst_object_unref (udpsrc0);
1921 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
1922 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1923 gst_object_unref (udpsrc1);
1927 GST_DEBUG_OBJECT (src, "retry %d", count);
1931 /* all fine, do port check */
1932 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
1933 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
1935 /* this should not happen... */
1936 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
1939 /* we keep these elements, we configure all in configure_transport when the
1940 * server told us to really use the UDP ports. */
1941 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
1942 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
1943 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
1944 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
1946 /* keep track of next available port number when we have a range
1948 if (src->next_port_num != 0)
1949 src->next_port_num = tmp_rtcp + 1;
1956 GST_DEBUG_OBJECT (src, "could not get UDP source");
1961 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
1965 no_udp_rtcp_protocol:
1967 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
1972 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
1973 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
1979 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1980 gst_object_unref (udpsrc0);
1983 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1984 gst_object_unref (udpsrc1);
1991 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
1996 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
1998 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1999 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2002 for (i = 0; i < 2; i++) {
2003 if (stream->udpsrc[i])
2004 gst_element_set_state (stream->udpsrc[i], state);
2010 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2017 event = gst_event_new_flush_start ();
2018 GST_DEBUG_OBJECT (src, "start flush");
2020 state = GST_STATE_PAUSED;
2022 event = gst_event_new_flush_stop (FALSE);
2023 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2026 state = GST_STATE_PLAYING;
2028 state = GST_STATE_PAUSED;
2030 gst_rtspsrc_push_event (src, event);
2031 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2032 gst_rtspsrc_set_state (src, state);
2035 static GstRTSPResult
2036 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2037 GstRTSPMessage * message, GTimeVal * timeout)
2042 ret = gst_rtsp_connection_send (conn, message, timeout);
2044 ret = GST_RTSP_ERROR;
2049 static GstRTSPResult
2050 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2051 GstRTSPMessage * message, GTimeVal * timeout)
2056 ret = gst_rtsp_connection_receive (conn, message, timeout);
2058 ret = GST_RTSP_ERROR;
2064 gst_rtspsrc_get_position (GstRTSPSrc * src)
2069 query = gst_query_new_position (GST_FORMAT_TIME);
2070 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2071 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2072 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2076 if (stream->srcpad) {
2077 if (gst_pad_query (stream->srcpad, query)) {
2078 gst_query_parse_position (query, &fmt, &pos);
2079 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2080 GST_TIME_ARGS (pos));
2081 src->last_pos = pos;
2091 gst_query_unref (query);
2095 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2100 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2102 gboolean flush, skip;
2105 GstSegment seeksegment = { 0, };
2109 GST_DEBUG_OBJECT (src, "doing seek with event");
2111 gst_event_parse_seek (event, &rate, &format, &flags,
2112 &cur_type, &cur, &stop_type, &stop);
2114 /* no negative rates yet */
2118 /* we need TIME format */
2119 if (format != src->segment.format)
2122 GST_DEBUG_OBJECT (src, "doing seek without event");
2124 cur_type = GST_SEEK_TYPE_SET;
2125 stop_type = GST_SEEK_TYPE_SET;
2128 /* get flush flag */
2129 flush = flags & GST_SEEK_FLAG_FLUSH;
2130 skip = flags & GST_SEEK_FLAG_SKIP;
2132 /* now we need to make sure the streaming thread is stopped. We do this by
2133 * either sending a FLUSH_START event downstream which will cause the
2134 * streaming thread to stop with a WRONG_STATE.
2135 * For a non-flushing seek we simply pause the task, which will happen as soon
2136 * as it completes one iteration (and thus might block when the sink is
2137 * blocking in preroll). */
2139 GST_DEBUG_OBJECT (src, "starting flush");
2140 gst_rtspsrc_flush (src, TRUE, FALSE);
2143 gst_task_pause (src->task);
2147 /* we should now be able to grab the streaming thread because we stopped it
2148 * with the above flush/pause code */
2149 GST_RTSP_STREAM_LOCK (src);
2151 GST_DEBUG_OBJECT (src, "stopped streaming");
2153 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2154 gst_rtspsrc_connection_flush (src, FALSE);
2156 /* copy segment, we need this because we still need the old
2157 * segment when we close the current segment. */
2158 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2160 /* configure the seek parameters in the seeksegment. We will then have the
2161 * right values in the segment to perform the seek */
2163 GST_DEBUG_OBJECT (src, "configuring seek");
2164 gst_segment_do_seek (&seeksegment, rate, format, flags,
2165 cur_type, cur, stop_type, stop, &update);
2168 /* figure out the last position we need to play. If it's configured (stop !=
2169 * -1), use that, else we play until the total duration of the file */
2170 if ((stop = seeksegment.stop) == -1)
2171 stop = seeksegment.duration;
2173 playing = (src->state == GST_RTSP_STATE_PLAYING);
2175 /* if we were playing, pause first */
2177 /* obtain current position in case seek fails */
2178 gst_rtspsrc_get_position (src);
2179 gst_rtspsrc_pause (src, FALSE);
2183 src->state = GST_RTSP_STATE_SEEKING;
2185 /* PLAY will add the range header now. */
2186 src->need_range = TRUE;
2188 /* and continue playing */
2190 gst_rtspsrc_play (src, &seeksegment, FALSE);
2192 /* prepare for streaming again */
2194 /* if we started flush, we stop now */
2195 GST_DEBUG_OBJECT (src, "stopping flush");
2196 gst_rtspsrc_flush (src, FALSE, playing);
2199 /* now we did the seek and can activate the new segment values */
2200 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2202 /* if we're doing a segment seek, post a SEGMENT_START message */
2203 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2204 gst_element_post_message (GST_ELEMENT_CAST (src),
2205 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2206 src->segment.format, src->segment.position));
2209 /* now create the newsegment */
2210 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2211 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2214 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2215 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2216 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2217 stream->discont = TRUE;
2220 GST_RTSP_STREAM_UNLOCK (src);
2227 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2232 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2238 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2242 gboolean res = TRUE;
2245 src = GST_RTSPSRC_CAST (parent);
2247 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2248 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2250 switch (GST_EVENT_TYPE (event)) {
2251 case GST_EVENT_SEEK:
2252 res = gst_rtspsrc_perform_seek (src, event);
2256 case GST_EVENT_NAVIGATION:
2257 case GST_EVENT_LATENCY:
2265 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2266 res = gst_pad_send_event (target, event);
2267 gst_object_unref (target);
2269 gst_event_unref (event);
2272 gst_event_unref (event);
2278 /* this is the final event function we receive on the internal source pad when
2279 * we deal with TCP connections */
2281 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2286 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2288 switch (GST_EVENT_TYPE (event)) {
2289 case GST_EVENT_SEEK:
2291 case GST_EVENT_NAVIGATION:
2292 case GST_EVENT_LATENCY:
2294 gst_event_unref (event);
2301 /* this is the final query function we receive on the internal source pad when
2302 * we deal with TCP connections */
2304 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2308 gboolean res = TRUE;
2310 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2312 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2313 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2315 switch (GST_QUERY_TYPE (query)) {
2316 case GST_QUERY_POSITION:
2321 case GST_QUERY_DURATION:
2325 gst_query_parse_duration (query, &format, NULL);
2328 case GST_FORMAT_TIME:
2329 gst_query_set_duration (query, format, src->segment.duration);
2337 case GST_QUERY_LATENCY:
2339 /* we are live with a min latency of 0 and unlimited max latency, this
2340 * result will be updated by the session manager if there is any. */
2341 gst_query_set_latency (query, TRUE, 0, -1);
2351 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2353 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2357 gboolean res = FALSE;
2359 src = GST_RTSPSRC_CAST (parent);
2361 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2362 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2364 switch (GST_QUERY_TYPE (query)) {
2365 case GST_QUERY_DURATION:
2369 gst_query_parse_duration (query, &format, NULL);
2372 case GST_FORMAT_TIME:
2373 gst_query_set_duration (query, format, src->segment.duration);
2381 case GST_QUERY_SEEKING:
2385 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2386 if (format == GST_FORMAT_TIME) {
2388 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2390 /* seeking without duration is unlikely */
2391 seekable = seekable && src->seekable && src->segment.duration &&
2392 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2394 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0,
2395 src->segment.duration);
2404 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2406 gst_query_set_uri (query, uri);
2414 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2416 /* forward the query to the proxy target pad */
2418 res = gst_pad_query (target, query);
2419 gst_object_unref (target);
2428 /* callback for RTCP messages to be sent to the server when operating in TCP
2430 static GstFlowReturn
2431 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2434 GstRTSPStream *stream;
2435 GstFlowReturn res = GST_FLOW_OK;
2440 GstRTSPMessage message = { 0 };
2441 GstRTSPConnection *conn;
2443 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2444 src = stream->parent;
2446 gst_buffer_map (buffer, &map, GST_MAP_READ);
2450 gst_rtsp_message_init_data (&message, stream->channel[1]);
2452 /* lend the body data to the message */
2453 gst_rtsp_message_take_body (&message, data, size);
2455 if (stream->conninfo.connection)
2456 conn = stream->conninfo.connection;
2458 conn = src->conninfo.connection;
2460 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2461 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2462 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2464 /* and steal it away again because we will free it when unreffing the
2466 gst_rtsp_message_steal_body (&message, &data, &size);
2467 gst_rtsp_message_unset (&message);
2469 gst_buffer_unmap (buffer, &map);
2470 gst_buffer_unref (buffer);
2475 static GstPadProbeReturn
2476 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2478 GstRTSPSrc *src = user_data;
2480 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2481 GST_DEBUG_PAD_NAME (pad));
2483 /* activate the streams */
2484 GST_OBJECT_LOCK (src);
2485 if (!src->need_activate)
2488 src->need_activate = FALSE;
2489 GST_OBJECT_UNLOCK (src);
2491 gst_rtspsrc_activate_streams (src);
2493 return GST_PAD_PROBE_OK;
2497 GST_OBJECT_UNLOCK (src);
2498 return GST_PAD_PROBE_OK;
2503 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
2505 GstPad *gpad = GST_PAD_CAST (user_data);
2507 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
2508 gst_pad_store_sticky_event (gpad, *event);
2513 /* this callback is called when the session manager generated a new src pad with
2514 * payloaded RTP packets. We simply ghost the pad here. */
2516 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2519 GstPadTemplate *template;
2522 GstRTSPStream *stream;
2525 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2527 GST_RTSP_STATE_LOCK (src);
2529 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2530 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2531 goto unknown_stream;
2533 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2535 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2537 goto unknown_stream;
2540 stream->ssrc = ssrc;
2542 /* we'll add it later see below */
2543 stream->added = TRUE;
2545 /* check if we added all streams */
2547 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2548 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2550 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
2551 ostream, ostream->container, ostream->added, ostream->setup);
2553 /* if we find a stream for which we did a setup that is not added, we
2554 * need to wait some more */
2555 if (ostream->setup && !ostream->added) {
2560 GST_RTSP_STATE_UNLOCK (src);
2562 /* create a new pad we will use to stream to */
2563 template = gst_static_pad_template_get (&rtptemplate);
2564 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2565 gst_object_unref (template);
2568 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2569 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2570 gst_pad_set_active (stream->srcpad, TRUE);
2571 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
2572 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2575 GST_DEBUG_OBJECT (src, "We added all streams");
2576 /* when we get here, all stream are added and we can fire the no-more-pads
2578 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2586 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2587 GST_RTSP_STATE_UNLOCK (src);
2594 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
2598 len = stream->ptmap->len;
2599 for (i = 0; i < len; i++) {
2600 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2608 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2610 GstRTSPStream *stream;
2613 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2615 GST_RTSP_STATE_LOCK (src);
2616 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2618 goto unknown_stream;
2620 if ((caps = stream_get_caps_for_pt (stream, pt)))
2621 gst_caps_ref (caps);
2622 GST_RTSP_STATE_UNLOCK (src);
2628 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2629 GST_RTSP_STATE_UNLOCK (src);
2635 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2637 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2643 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2649 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2655 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2657 GstRTSPSrc *src = stream->parent;
2660 g_object_get (source, "ssrc", &ssrc, NULL);
2662 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2663 ssrc, stream->ssrc, stream->id);
2665 if (ssrc == stream->ssrc)
2666 gst_rtspsrc_do_stream_eos (src, stream);
2670 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2672 GstRTSPSrc *src = stream->parent;
2675 g_object_get (source, "ssrc", &ssrc, NULL);
2677 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2678 ssrc, stream->ssrc, stream->id);
2680 if (ssrc == stream->ssrc)
2681 gst_rtspsrc_do_stream_eos (src, stream);
2685 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2687 GstRTSPStream *stream;
2689 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2691 /* get stream for session */
2692 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2694 gst_rtspsrc_do_stream_eos (src, stream);
2699 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2701 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2706 set_manager_buffer_mode (GstRTSPSrc * src)
2708 GObjectClass *klass;
2710 if (src->manager == NULL)
2713 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2715 if (!g_object_class_find_property (klass, "buffer-mode"))
2718 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2719 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2724 GST_DEBUG_OBJECT (src,
2725 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
2727 if (src->provided_clock) {
2728 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
2730 if (clock == src->provided_clock) {
2731 GST_DEBUG_OBJECT (src, "selected synced");
2732 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
2735 gst_object_unref (clock);
2740 /* Otherwise fall-through and use another buffer mode */
2742 gst_object_unref (clock);
2745 GST_DEBUG_OBJECT (src, "auto buffering mode");
2746 if (src->use_buffering) {
2747 GST_DEBUG_OBJECT (src, "selected buffer");
2748 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
2750 GST_DEBUG_OBJECT (src, "selected slave");
2751 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2756 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2760 GstMIKEYMessage *msg = stream->mikey;
2762 GST_DEBUG ("request key SSRC %u", ssrc);
2764 caps = gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
2765 caps = gst_caps_make_writable (caps);
2767 /* parse crypto sessions and look for the SSRC rollover counter */
2768 msg = stream->mikey;
2769 for (i = 0; msg && i < gst_mikey_message_get_n_cs (msg); i++) {
2770 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
2772 if (ssrc == map->ssrc) {
2773 gst_caps_set_simple (caps, "roc", G_TYPE_UINT, map->roc, NULL);
2782 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2784 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
2785 if (stream->id != session)
2788 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
2789 stream->profile != GST_RTSP_PROFILE_SAVPF)
2792 if (stream->srtpdec == NULL) {
2795 name = g_strdup_printf ("srtpdec_%u", session);
2796 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
2799 g_signal_connect (stream->srtpdec, "request-key",
2800 (GCallback) request_key, stream);
2802 return gst_object_ref (stream->srtpdec);
2806 request_rtcp_encoder (GstElement * rtpbin, guint session,
2807 GstRTSPStream * stream)
2812 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
2813 if (stream->id != session)
2816 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
2817 stream->profile != GST_RTSP_PROFILE_SAVPF)
2820 if (stream->srtpenc == NULL) {
2823 name = g_strdup_printf ("srtpenc_%u", session);
2824 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
2827 /* get RTCP crypto parameters from caps */
2828 s = gst_caps_get_structure (stream->srtcpparams, 0);
2832 GType ciphertype, authtype;
2833 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
2835 ciphertype = g_type_from_name ("GstSrtpCipherType");
2836 authtype = g_type_from_name ("GstSrtpAuthType");
2837 g_value_init (&rtcp_cipher, ciphertype);
2838 g_value_init (&rtcp_auth, authtype);
2840 str = gst_structure_get_string (s, "srtcp-cipher");
2841 gst_value_deserialize (&rtcp_cipher, str);
2842 str = gst_structure_get_string (s, "srtcp-auth");
2843 gst_value_deserialize (&rtcp_auth, str);
2844 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
2846 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-cipher",
2848 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-auth",
2850 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
2852 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
2854 g_object_set (stream->srtpenc, "key", buf, NULL);
2856 g_value_unset (&rtcp_cipher);
2857 g_value_unset (&rtcp_auth);
2858 gst_buffer_unref (buf);
2861 name = g_strdup_printf ("rtcp_sink_%d", session);
2862 pad = gst_element_get_request_pad (stream->srtpenc, name);
2864 gst_object_unref (pad);
2866 return gst_object_ref (stream->srtpenc);
2870 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
2872 GstElement *rtx, *bin;
2875 GstRTSPStream *stream;
2877 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
2879 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
2883 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
2884 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
2885 bin = gst_bin_new (NULL);
2886 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
2887 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
2888 gst_bin_add (GST_BIN (bin), rtx);
2890 pad = gst_element_get_static_pad (rtx, "src");
2891 name = g_strdup_printf ("src_%u", sessid);
2892 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2894 gst_object_unref (pad);
2896 pad = gst_element_get_static_pad (rtx, "sink");
2897 name = g_strdup_printf ("sink_%u", sessid);
2898 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2900 gst_object_unref (pad);
2906 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
2910 gboolean do_retransmission = FALSE;
2912 if (transport->trans != GST_RTSP_TRANS_RTP)
2914 if (transport->profile != GST_RTSP_PROFILE_AVPF &&
2915 transport->profile != GST_RTSP_PROFILE_SAVPF)
2918 signal_id = g_signal_lookup ("request-aux-receiver",
2919 G_OBJECT_TYPE (src->manager));
2920 /* there's already something connected */
2921 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
2922 NULL, NULL, NULL) != 0) {
2923 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
2924 "\"request-aux-receiver\" signal is "
2925 "already used by the application");
2929 /* build the retransmission payload type map */
2930 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2931 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2932 gboolean do_retransmission_stream = FALSE;
2935 if (stream->rtx_pt_map)
2936 gst_structure_free (stream->rtx_pt_map);
2937 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
2939 for (i = 0; i < stream->ptmap->len; i++) {
2940 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2941 GstStructure *s = gst_caps_get_structure (item->caps, 0);
2942 const gchar *encoding;
2944 /* we only care about RTX streams */
2945 if ((encoding = gst_structure_get_string (s, "encoding-name"))
2946 && g_strcmp0 (encoding, "RTX") == 0) {
2947 const gchar *stream_pt_s;
2950 if (gst_structure_get_int (s, "payload", &rtx_pt)
2951 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
2954 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
2956 do_retransmission_stream = TRUE;
2962 if (do_retransmission_stream) {
2963 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
2964 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
2965 do_retransmission = TRUE;
2967 GST_DEBUG_OBJECT (src, "no retransmission payload map for stream "
2968 "id %i", stream->id);
2969 gst_structure_free (stream->rtx_pt_map);
2970 stream->rtx_pt_map = NULL;
2974 if (do_retransmission) {
2975 GST_DEBUG_OBJECT (src, "Enabling retransmissions");
2977 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
2979 /* enable RFC4588 retransmission handling by setting rtprtxreceive
2980 * as the "aux" element of rtpbin */
2981 g_signal_connect (src->manager, "request-aux-receiver",
2982 (GCallback) request_aux_receiver, src);
2984 GST_DEBUG_OBJECT (src,
2985 "Not enabling retransmissions as no stream had a retransmission payload map");
2989 /* try to get and configure a manager */
2991 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
2992 GstRTSPTransport * transport)
2994 const gchar *manager;
2996 GstStateChangeReturn ret;
2998 /* find a manager */
2999 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3003 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3005 /* configure the manager */
3006 if (src->manager == NULL) {
3007 GObjectClass *klass;
3009 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3011 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3015 goto use_no_manager;
3017 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3018 goto manager_failed;
3021 /* we manage this element */
3022 gst_element_set_locked_state (src->manager, TRUE);
3023 gst_bin_add (GST_BIN_CAST (src), src->manager);
3025 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3026 if (ret == GST_STATE_CHANGE_FAILURE)
3027 goto start_manager_failure;
3029 g_object_set (src->manager, "latency", src->latency, NULL);
3031 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3033 if (g_object_class_find_property (klass, "ntp-sync")) {
3034 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3037 if (g_object_class_find_property (klass, "rfc7273-sync")) {
3038 g_object_set (src->manager, "rfc7273-sync", src->rfc7273_sync, NULL);
3041 if (src->use_pipeline_clock) {
3042 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3043 g_object_set (src->manager, "use-pipeline-clock", TRUE, NULL);
3046 if (g_object_class_find_property (klass, "ntp-time-source")) {
3047 g_object_set (src->manager, "ntp-time-source", src->ntp_time_source,
3052 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3053 g_object_set (src->manager, "sdes", src->sdes, NULL);
3056 if (g_object_class_find_property (klass, "drop-on-latency")) {
3057 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3061 if (g_object_class_find_property (klass, "max-rtcp-rtp-time-diff")) {
3062 g_object_set (src->manager, "max-rtcp-rtp-time-diff",
3063 src->max_rtcp_rtp_time_diff, NULL);
3066 /* buffer mode pauses are handled by adding offsets to buffer times,
3067 * but some depayloaders may have a hard time syncing output times
3068 * with such input times, e.g. container ones, most notably ASF */
3069 /* TODO alternatives are having an event that indicates these shifts,
3070 * or having rtsp extensions provide suggestion on buffer mode */
3071 /* valid duration implies not likely live pipeline,
3072 * so slaving in jitterbuffer does not make much sense
3073 * (and might mess things up due to bursts) */
3074 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3075 src->segment.duration && stream->container) {
3076 src->use_buffering = TRUE;
3078 src->use_buffering = FALSE;
3081 set_manager_buffer_mode (src);
3083 /* connect to signals */
3084 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3086 src->manager_sig_id =
3087 g_signal_connect (src->manager, "pad-added",
3088 (GCallback) new_manager_pad, src);
3089 src->manager_ptmap_id =
3090 g_signal_connect (src->manager, "request-pt-map",
3091 (GCallback) request_pt_map, src);
3093 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3096 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3099 if (src->do_retransmission)
3100 add_retransmission (src, transport);
3102 g_signal_connect (src->manager, "request-rtp-decoder",
3103 (GCallback) request_rtp_decoder, stream);
3104 g_signal_connect (src->manager, "request-rtcp-decoder",
3105 (GCallback) request_rtp_decoder, stream);
3106 g_signal_connect (src->manager, "request-rtcp-encoder",
3107 (GCallback) request_rtcp_encoder, stream);
3109 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3110 * into a separate RTP session. */
3111 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3112 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3114 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3115 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3118 /* now configure the bandwidth in the manager */
3119 if (g_signal_lookup ("get-internal-session",
3120 G_OBJECT_TYPE (src->manager)) != 0) {
3121 GObject *rtpsession;
3123 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3126 GstRTPProfile rtp_profile;
3128 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3130 stream->session = rtpsession;
3132 if (stream->as_bandwidth != -1) {
3133 GST_INFO_OBJECT (src, "setting AS: %f",
3134 (gdouble) (stream->as_bandwidth * 1000));
3135 g_object_set (rtpsession, "bandwidth",
3136 (gdouble) (stream->as_bandwidth * 1000), NULL);
3138 if (stream->rr_bandwidth != -1) {
3139 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3140 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3143 if (stream->rs_bandwidth != -1) {
3144 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3145 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3149 switch (stream->profile) {
3150 case GST_RTSP_PROFILE_AVPF:
3151 rtp_profile = GST_RTP_PROFILE_AVPF;
3153 case GST_RTSP_PROFILE_SAVP:
3154 rtp_profile = GST_RTP_PROFILE_SAVP;
3156 case GST_RTSP_PROFILE_SAVPF:
3157 rtp_profile = GST_RTP_PROFILE_SAVPF;
3159 case GST_RTSP_PROFILE_AVP:
3161 rtp_profile = GST_RTP_PROFILE_AVP;
3165 g_object_set (rtpsession, "rtp-profile", rtp_profile, NULL);
3167 g_object_set (rtpsession, "probation", src->probation, NULL);
3169 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3171 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3173 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3175 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3177 g_signal_connect (rtpsession, "on-ssrc-active",
3178 (GCallback) on_ssrc_active, stream);
3189 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3194 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3197 start_manager_failure:
3199 GST_DEBUG_OBJECT (src, "could not start session manager");
3204 /* free the UDP sources allocated when negotiating a transport.
3205 * This function is called when the server negotiated to a transport where the
3206 * UDP sources are not needed anymore, such as TCP or multicast. */
3208 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3212 for (i = 0; i < 2; i++) {
3213 if (stream->udpsrc[i]) {
3214 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3215 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3216 gst_object_unref (stream->udpsrc[i]);
3217 stream->udpsrc[i] = NULL;
3222 /* for TCP, create pads to send and receive data to and from the manager and to
3223 * intercept various events and queries
3226 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3227 GstRTSPTransport * transport, GstPad ** outpad)
3230 GstPadTemplate *template;
3231 GstPad *pad0, *pad1;
3233 /* configure for interleaved delivery, nothing needs to be done
3234 * here, the loop function will call the chain functions of the
3235 * session manager. */
3236 stream->channel[0] = transport->interleaved.min;
3237 stream->channel[1] = transport->interleaved.max;
3238 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3239 stream->channel[0], stream->channel[1]);
3241 /* we can remove the allocated UDP ports now */
3242 gst_rtspsrc_stream_free_udp (stream);
3244 /* no session manager, send data to srcpad directly */
3245 if (!stream->channelpad[0]) {
3246 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3248 /* create a new pad we will use to stream to */
3249 name = g_strdup_printf ("stream_%u", stream->id);
3250 template = gst_static_pad_template_get (&rtptemplate);
3251 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3252 gst_object_unref (template);
3255 /* set caps and activate */
3256 gst_pad_use_fixed_caps (stream->channelpad[0]);
3257 gst_pad_set_active (stream->channelpad[0], TRUE);
3259 *outpad = gst_object_ref (stream->channelpad[0]);
3261 GST_DEBUG_OBJECT (src, "using manager source pad");
3263 template = gst_static_pad_template_get (&anysrctemplate);
3265 /* allocate pads for sending the channel data into the manager */
3266 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3267 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3268 gst_object_unref (stream->channelpad[0]);
3269 stream->channelpad[0] = pad0;
3270 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3271 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3272 gst_pad_set_element_private (pad0, src);
3273 gst_pad_set_active (pad0, TRUE);
3275 if (stream->channelpad[1]) {
3276 /* if we have a sinkpad for the other channel, create a pad and link to the
3278 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3279 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3280 gst_pad_link_full (pad1, stream->channelpad[1],
3281 GST_PAD_LINK_CHECK_NOTHING);
3282 gst_object_unref (stream->channelpad[1]);
3283 stream->channelpad[1] = pad1;
3284 gst_pad_set_active (pad1, TRUE);
3286 gst_object_unref (template);
3288 /* setup RTCP transport back to the server if we have to. */
3289 if (src->manager && src->do_rtcp) {
3292 template = gst_static_pad_template_get (&anysinktemplate);
3294 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3295 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3296 gst_pad_set_element_private (stream->rtcppad, stream);
3297 gst_pad_set_active (stream->rtcppad, TRUE);
3299 /* get session RTCP pad */
3300 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3301 pad = gst_element_get_request_pad (src->manager, name);
3306 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3307 gst_object_unref (pad);
3310 gst_object_unref (template);
3316 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3317 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3318 gint * max, guint * ttl)
3320 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3322 if (!(*destination = transport->destination))
3323 *destination = stream->destination;
3326 /* transport first */
3327 *min = transport->port.min;
3328 *max = transport->port.max;
3329 if (*min == -1 && *max == -1) {
3330 /* then try from SDP */
3331 if (stream->port != 0) {
3332 *min = stream->port;
3333 *max = stream->port + 1;
3339 if (!(*ttl = transport->ttl))
3344 /* first take the source, then the endpoint to figure out where to send
3346 if (!(*destination = transport->source)) {
3347 if (src->conninfo.connection)
3348 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3349 else if (stream->conninfo.connection)
3351 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3355 /* for unicast we only expect the ports here */
3356 *min = transport->server_port.min;
3357 *max = transport->server_port.max;
3362 /* For multicast create UDP sources and join the multicast group. */
3364 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3365 GstRTSPTransport * transport, GstPad ** outpad)
3368 const gchar *destination;
3371 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3373 /* we can remove the allocated UDP ports now */
3374 gst_rtspsrc_stream_free_udp (stream);
3376 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3379 /* we need a destination now */
3380 if (destination == NULL)
3381 goto no_destination;
3383 /* we really need ports now or we won't be able to receive anything at all */
3384 if (min == -1 && max == -1)
3387 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3388 destination, min, max);
3390 /* creating UDP source for RTP */
3392 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3394 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3396 if (stream->udpsrc[0] == NULL)
3399 /* take ownership */
3400 gst_object_ref_sink (stream->udpsrc[0]);
3402 if (src->udp_buffer_size != 0)
3403 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3404 src->udp_buffer_size, NULL);
3406 if (src->multi_iface != NULL)
3407 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3408 src->multi_iface, NULL);
3411 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3412 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3415 /* creating another UDP source for RTCP */
3419 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3421 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3423 if (stream->udpsrc[1] == NULL)
3426 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3427 stream->profile == GST_RTSP_PROFILE_SAVPF)
3428 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3430 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3431 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3432 gst_caps_unref (caps);
3434 /* take ownership */
3435 gst_object_ref_sink (stream->udpsrc[1]);
3437 if (src->multi_iface != NULL)
3438 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3439 src->multi_iface, NULL);
3441 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
3448 GST_DEBUG_OBJECT (src, "no UDP source element found");
3453 GST_DEBUG_OBJECT (src, "no destination found");
3458 GST_DEBUG_OBJECT (src, "no ports found");
3463 /* configure the remainder of the UDP ports */
3465 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3466 GstRTSPTransport * transport, GstPad ** outpad)
3468 /* we manage the UDP elements now. For unicast, the UDP sources where
3469 * allocated in the stream when we suggested a transport. */
3470 if (stream->udpsrc[0]) {
3473 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3474 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3476 GST_DEBUG_OBJECT (src, "setting up UDP source");
3478 /* configure a timeout on the UDP port. When the timeout message is
3479 * posted, we assume UDP transport is not possible. We reconnect using TCP
3481 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3482 src->udp_timeout * 1000, NULL);
3484 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
3485 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3487 /* get output pad of the UDP source. */
3488 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3490 /* save it so we can unblock */
3491 stream->blockedpad = *outpad;
3493 /* configure pad block on the pad. As soon as there is dataflow on the
3494 * UDP source, we know that UDP is not blocked by a firewall and we can
3495 * configure all the streams to let the application autoplug decoders. */
3497 gst_pad_add_probe (stream->blockedpad,
3498 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3499 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
3501 if (stream->channelpad[0]) {
3502 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3503 /* configure for UDP delivery, we need to connect the UDP pads to
3504 * the session plugin. */
3505 gst_pad_link_full (*outpad, stream->channelpad[0],
3506 GST_PAD_LINK_CHECK_NOTHING);
3507 gst_object_unref (*outpad);
3509 /* we connected to pad-added signal to get pads from the manager */
3511 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3516 if (stream->udpsrc[1]) {
3519 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3520 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3522 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3523 stream->profile == GST_RTSP_PROFILE_SAVPF)
3524 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3526 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3527 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3528 gst_caps_unref (caps);
3530 if (stream->channelpad[1]) {
3533 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3535 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3536 gst_pad_link_full (pad, stream->channelpad[1],
3537 GST_PAD_LINK_CHECK_NOTHING);
3538 gst_object_unref (pad);
3540 /* leave unlinked */
3546 /* configure the UDP sink back to the server for status reports */
3548 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3549 GstRTSPStream * stream, GstRTSPTransport * transport)
3552 gint rtp_port, rtcp_port;
3553 gboolean do_rtp, do_rtcp;
3554 const gchar *destination;
3559 /* get transport info */
3560 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3561 &rtp_port, &rtcp_port, &ttl);
3563 /* see what we need to do */
3564 do_rtp = (rtp_port != -1);
3565 /* it's possible that the server does not want us to send RTCP in which case
3567 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3569 /* we need a destination when we have RTP or RTCP ports */
3570 if (destination == NULL && (do_rtp || do_rtcp))
3571 goto no_destination;
3573 /* try to construct the fakesrc to the RTP port of the server to open up any
3576 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3579 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3580 stream->udpsink[0] =
3581 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3583 if (stream->udpsink[0] == NULL)
3584 goto no_sink_element;
3586 /* don't join multicast group, we will have the source socket do that */
3587 /* no sync or async state changes needed */
3588 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3589 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3591 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3593 if (stream->udpsrc[0]) {
3594 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3595 * so that NAT firewalls will open a hole for us */
3596 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3600 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3601 /* configure socket and make sure udpsink does not close it when shutting
3602 * down, it belongs to udpsrc after all. */
3603 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3604 "close-socket", FALSE, NULL);
3605 g_object_unref (socket);
3608 /* the source for the dummy packets to open up NAT */
3609 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3610 if (stream->fakesrc == NULL)
3611 goto no_fakesrc_element;
3613 /* random data in 5 buffers, a size of 200 bytes should be fine */
3614 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3615 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3617 /* we don't want to consider this a sink */
3618 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
3620 /* keep everything locked */
3621 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3622 gst_element_set_locked_state (stream->fakesrc, TRUE);
3624 gst_object_ref (stream->udpsink[0]);
3625 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3626 gst_object_ref (stream->fakesrc);
3627 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3629 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3630 "sink", GST_PAD_LINK_CHECK_NOTHING);
3633 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3636 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3637 stream->udpsink[1] =
3638 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3640 if (stream->udpsink[1] == NULL)
3641 goto no_sink_element;
3643 /* don't join multicast group, we will have the source socket do that */
3644 /* no sync or async state changes needed */
3645 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3646 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3648 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3650 if (stream->udpsrc[1]) {
3651 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3652 * because some servers check the port number of where it sends RTCP to identify
3653 * the RTCP packets it receives */
3654 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3658 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3659 /* configure socket and make sure udpsink does not close it when shutting
3660 * down, it belongs to udpsrc after all. */
3661 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3662 "close-socket", FALSE, NULL);
3663 g_object_unref (socket);
3666 /* we don't want to consider this a sink */
3667 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3669 /* we keep this playing always */
3670 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3671 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3673 gst_object_ref (stream->udpsink[1]);
3674 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3676 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3678 /* get session RTCP pad */
3679 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3680 pad = gst_element_get_request_pad (src->manager, name);
3685 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3686 gst_object_unref (pad);
3695 GST_ERROR_OBJECT (src, "no destination address specified");
3700 GST_ERROR_OBJECT (src, "no UDP sink element found");
3705 GST_ERROR_OBJECT (src, "no fakesrc element found");
3710 GST_ERROR_OBJECT (src, "failed to create socket");
3715 /* sets up all elements needed for streaming over the specified transport.
3716 * Does not yet expose the element pads, this will be done when there is actuall
3717 * dataflow detected, which might never happen when UDP is blocked in a
3718 * firewall, for example.
3721 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3722 GstRTSPTransport * transport)
3725 GstPad *outpad = NULL;
3726 GstPadTemplate *template;
3728 const gchar *media_type;
3731 src = stream->parent;
3733 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3735 /* get the proper media type for this stream now */
3736 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
3737 goto unknown_transport;
3739 goto unknown_transport;
3741 /* configure the final media type */
3742 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
3744 len = stream->ptmap->len;
3745 for (i = 0; i < len; i++) {
3747 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3749 if (item->caps == NULL)
3752 s = gst_caps_get_structure (item->caps, 0);
3753 gst_structure_set_name (s, media_type);
3754 /* set ssrc if known */
3755 if (transport->ssrc)
3756 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
3759 /* try to get and configure a manager, channelpad[0-1] will be configured with
3760 * the pads for the manager, or NULL when no manager is needed. */
3761 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3764 switch (transport->lower_transport) {
3765 case GST_RTSP_LOWER_TRANS_TCP:
3766 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3767 goto transport_failed;
3769 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3770 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3771 goto transport_failed;
3772 /* fallthrough, the rest is the same for UDP and MCAST */
3773 case GST_RTSP_LOWER_TRANS_UDP:
3774 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3775 goto transport_failed;
3776 /* configure udpsinks back to the server for RTCP messages and for the
3777 * dummy RTP messages to open NAT. */
3778 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3779 goto transport_failed;
3782 goto unknown_transport;
3786 GST_DEBUG_OBJECT (src, "creating ghostpad");
3788 gst_pad_use_fixed_caps (outpad);
3790 /* create ghostpad, don't add just yet, this will be done when we activate
3792 name = g_strdup_printf ("stream_%u", stream->id);
3793 template = gst_static_pad_template_get (&rtptemplate);
3794 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3795 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3796 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3797 gst_object_unref (template);
3800 gst_object_unref (outpad);
3802 /* mark pad as ok */
3803 stream->last_ret = GST_FLOW_OK;
3810 GST_DEBUG_OBJECT (src, "failed to configure transport");
3815 GST_DEBUG_OBJECT (src, "unknown transport");
3820 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3825 /* send a couple of dummy random packets on the receiver RTP port to the server,
3826 * this should make a firewall think we initiated the data transfer and
3827 * hopefully allow packets to go from the sender port to our RTP receiver port */
3829 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3833 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3836 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3837 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3839 if (stream->fakesrc && stream->udpsink[0]) {
3840 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3841 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3842 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3843 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3844 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3850 /* Adds the source pads of all configured streams to the element.
3851 * This code is performed when we detected dataflow.
3853 * We detect dataflow from either the _loop function or with pad probes on the
3857 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3861 GST_DEBUG_OBJECT (src, "activating streams");
3863 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3864 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3866 if (stream->udpsrc[0]) {
3867 /* remove timeout, we are streaming now and timeouts will be handled by
3868 * the session manager and jitter buffer */
3869 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3871 if (stream->srcpad) {
3872 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3873 gst_pad_set_active (stream->srcpad, TRUE);
3875 /* if we don't have a session manager, set the caps now. If we have a
3876 * session, we will get a notification of the pad and the caps. */
3877 if (!src->manager) {
3880 caps = stream_get_caps_for_pt (stream, stream->default_pt);
3881 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3882 gst_pad_set_caps (stream->srcpad, caps);
3885 if (!stream->added) {
3886 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3887 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3888 stream->added = TRUE;
3893 /* unblock all pads */
3894 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3895 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3897 if (stream->blockid) {
3898 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3899 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3900 stream->blockid = 0;
3908 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
3909 gboolean reset_manager)
3912 guint64 start, stop;
3913 gdouble play_speed, play_scale;
3915 GST_DEBUG_OBJECT (src, "configuring stream caps");
3917 start = segment->position;
3918 stop = segment->duration;
3919 play_speed = segment->rate;
3920 play_scale = segment->applied_rate;
3922 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3923 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3929 len = stream->ptmap->len;
3930 for (j = 0; j < len; j++) {
3932 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
3934 if (item->caps == NULL)
3937 caps = gst_caps_make_writable (item->caps);
3939 if (stream->timebase != -1)
3940 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3941 (guint) stream->timebase, NULL);
3942 if (stream->seqbase != -1)
3943 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3944 (guint) stream->seqbase, NULL);
3945 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3947 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3948 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3949 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3952 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
3955 if (item->pt == stream->default_pt) {
3956 if (stream->udpsrc[0])
3957 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3958 stream->need_caps = TRUE;
3962 if (reset_manager && src->manager) {
3963 GST_DEBUG_OBJECT (src, "clear session");
3964 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3968 static GstFlowReturn
3969 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3974 /* store the value */
3975 stream->last_ret = ret;
3977 /* if it's success we can return the value right away */
3978 if (ret == GST_FLOW_OK)
3981 /* any other error that is not-linked can be returned right
3983 if (ret != GST_FLOW_NOT_LINKED)
3986 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3987 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3988 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3990 ret = ostream->last_ret;
3991 /* some other return value (must be SUCCESS but we can return
3992 * other values as well) */
3993 if (ret != GST_FLOW_NOT_LINKED)
3996 /* if we get here, all other pads were unlinked and we return
3997 * NOT_LINKED then */
4003 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4006 gboolean res = TRUE;
4008 /* only streams that have a connection to the outside world */
4012 if (stream->udpsrc[0]) {
4013 gst_event_ref (event);
4014 res = gst_element_send_event (stream->udpsrc[0], event);
4015 } else if (stream->channelpad[0]) {
4016 gst_event_ref (event);
4017 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4018 res = gst_pad_push_event (stream->channelpad[0], event);
4020 res = gst_pad_send_event (stream->channelpad[0], event);
4023 if (stream->udpsrc[1]) {
4024 gst_event_ref (event);
4025 res &= gst_element_send_event (stream->udpsrc[1], event);
4026 } else if (stream->channelpad[1]) {
4027 gst_event_ref (event);
4028 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4029 res &= gst_pad_push_event (stream->channelpad[1], event);
4031 res &= gst_pad_send_event (stream->channelpad[1], event);
4035 gst_event_unref (event);
4041 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4044 gboolean res = TRUE;
4046 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4047 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4049 gst_event_ref (event);
4050 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4052 gst_event_unref (event);
4057 static GstRTSPResult
4058 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4062 GstRTSPMessage response;
4063 gboolean retry = FALSE;
4064 memset (&response, 0, sizeof (response));
4065 gst_rtsp_message_init (&response);
4067 if (info->connection == NULL) {
4068 if (info->url == NULL) {
4069 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4070 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4073 /* create connection */
4074 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4075 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4076 goto could_not_create;
4079 gst_rtspsrc_setup_auth (src, &response);
4082 g_free (info->url_str);
4083 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4085 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4087 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4088 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4089 src->tls_validation_flags))
4090 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4092 if (src->tls_database)
4093 gst_rtsp_connection_set_tls_database (info->connection,
4096 if (src->tls_interaction)
4097 gst_rtsp_connection_set_tls_interaction (info->connection,
4098 src->tls_interaction);
4101 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4102 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4104 if (src->proxy_host) {
4105 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4107 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4112 if (!info->connected) {
4115 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4116 ("Connecting to %s", info->location));
4117 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4118 res = gst_rtsp_connection_connect_with_response (info->connection,
4119 src->ptcp_timeout, &response);
4121 if (response.type == GST_RTSP_MESSAGE_HTTP_RESPONSE &&
4122 response.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4123 gst_rtsp_conninfo_close (src, info, TRUE);
4127 retry = FALSE; // we should not retry more than once
4132 if (res == GST_RTSP_OK)
4133 info->connected = TRUE;
4135 goto could_not_connect;
4137 } while (!info->connected && retry);
4138 gst_rtsp_message_unset (&response);
4144 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4145 gst_rtsp_message_unset (&response);
4150 gchar *str = gst_rtsp_strresult (res);
4151 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4153 gst_rtsp_message_unset (&response);
4158 gchar *str = gst_rtsp_strresult (res);
4159 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4161 gst_rtsp_message_unset (&response);
4166 static GstRTSPResult
4167 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4170 GST_RTSP_STATE_LOCK (src);
4171 if (info->connected) {
4172 GST_DEBUG_OBJECT (src, "closing connection...");
4173 gst_rtsp_connection_close (info->connection);
4174 info->connected = FALSE;
4176 if (free && info->connection) {
4177 /* free connection */
4178 GST_DEBUG_OBJECT (src, "freeing connection...");
4179 gst_rtsp_connection_free (info->connection);
4180 info->connection = NULL;
4182 GST_RTSP_STATE_UNLOCK (src);
4186 static GstRTSPResult
4187 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4192 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4193 gst_rtsp_conninfo_close (src, info, FALSE);
4194 res = gst_rtsp_conninfo_connect (src, info, async);
4200 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4204 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4205 GST_RTSP_STATE_LOCK (src);
4206 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4207 GST_DEBUG_OBJECT (src, "connection flush");
4208 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4209 src->conninfo.flushing = flush;
4211 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4212 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4213 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4214 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4215 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4216 stream->conninfo.flushing = flush;
4219 GST_RTSP_STATE_UNLOCK (src);
4222 static GstRTSPResult
4223 gst_rtspsrc_init_request (GstRTSPSrc * src, GstRTSPMessage * msg,
4224 GstRTSPMethod method, const gchar * uri)
4228 res = gst_rtsp_message_init_request (msg, method, uri);
4232 /* set user-agent */
4233 if (src->user_agent)
4234 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, src->user_agent);
4239 /* FIXME, handle server request, reply with OK, for now */
4240 static GstRTSPResult
4241 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
4242 GstRTSPMessage * request)
4244 GstRTSPMessage response = { 0 };
4247 GST_DEBUG_OBJECT (src, "got server request message");
4250 gst_rtsp_message_dump (request);
4252 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4254 if (res == GST_RTSP_ENOTIMPL) {
4255 /* default implementation, send OK */
4256 GST_DEBUG_OBJECT (src, "prepare OK reply");
4258 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4263 /* let app parse and reply */
4264 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4265 0, request, &response);
4268 gst_rtsp_message_dump (&response);
4270 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
4274 gst_rtsp_message_unset (&response);
4275 } else if (res == GST_RTSP_EEOF)
4283 gst_rtsp_message_unset (&response);
4288 /* send server keep-alive */
4289 static GstRTSPResult
4290 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4292 GstRTSPMessage request = { 0 };
4294 GstRTSPMethod method;
4295 const gchar *control;
4297 if (src->do_rtsp_keep_alive == FALSE) {
4298 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4299 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4303 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4305 /* find a method to use for keep-alive */
4306 if (src->methods & GST_RTSP_GET_PARAMETER)
4307 method = GST_RTSP_GET_PARAMETER;
4309 method = GST_RTSP_OPTIONS;
4311 control = get_aggregate_control (src);
4312 if (control == NULL)
4315 res = gst_rtspsrc_init_request (src, &request, method, control);
4320 gst_rtsp_message_dump (&request);
4323 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
4328 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4329 gst_rtsp_message_unset (&request);
4336 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4341 gchar *str = gst_rtsp_strresult (res);
4343 gst_rtsp_message_unset (&request);
4344 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4345 ("Could not send keep-alive. (%s)", str));
4351 static GstFlowReturn
4352 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4354 GstFlowReturn ret = GST_FLOW_OK;
4356 GstRTSPStream *stream;
4357 GstPad *outpad = NULL;
4363 channel = message->type_data.data.channel;
4365 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4367 goto unknown_stream;
4369 if (channel == stream->channel[0]) {
4370 outpad = stream->channelpad[0];
4372 } else if (channel == stream->channel[1]) {
4373 outpad = stream->channelpad[1];
4379 /* take a look at the body to figure out what we have */
4380 gst_rtsp_message_get_body (message, &data, &size);
4382 goto invalid_length;
4384 /* channels are not correct on some servers, do extra check */
4385 if (data[1] >= 200 && data[1] <= 204) {
4386 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4387 outpad = stream->channelpad[1];
4391 /* we have no clue what this is, just ignore then. */
4393 goto unknown_stream;
4395 /* take the message body for further processing */
4396 gst_rtsp_message_steal_body (message, &data, &size);
4398 /* strip the trailing \0 */
4401 buf = gst_buffer_new ();
4402 gst_buffer_append_memory (buf,
4403 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4405 /* don't need message anymore */
4406 gst_rtsp_message_unset (message);
4408 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4411 if (src->need_activate) {
4417 guint group_id = gst_util_group_id_next ();
4419 /* generate an SHA256 sum of the URI */
4420 cs = g_checksum_new (G_CHECKSUM_SHA256);
4421 uri = src->conninfo.location;
4422 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4424 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4425 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4429 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4430 event = gst_event_new_stream_start (stream_id);
4431 gst_event_set_group_id (event, group_id);
4434 gst_rtspsrc_stream_push_event (src, ostream, event);
4436 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
4437 /* only streams that have a connection to the outside world */
4438 if (ostream->setup) {
4439 if (ostream->udpsrc[0]) {
4440 gst_element_send_event (ostream->udpsrc[0],
4441 gst_event_new_caps (caps));
4442 } else if (ostream->channelpad[0]) {
4443 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
4444 gst_pad_push_event (ostream->channelpad[0],
4445 gst_event_new_caps (caps));
4447 gst_pad_send_event (ostream->channelpad[0],
4448 gst_event_new_caps (caps));
4450 ostream->need_caps = FALSE;
4452 if (ostream->profile == GST_RTSP_PROFILE_SAVP ||
4453 ostream->profile == GST_RTSP_PROFILE_SAVPF)
4454 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4456 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4458 if (ostream->udpsrc[1]) {
4459 gst_element_send_event (ostream->udpsrc[1],
4460 gst_event_new_caps (caps));
4461 } else if (ostream->channelpad[1]) {
4462 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
4463 gst_pad_push_event (ostream->channelpad[1],
4464 gst_event_new_caps (caps));
4466 gst_pad_send_event (ostream->channelpad[1],
4467 gst_event_new_caps (caps));
4470 gst_caps_unref (caps);
4474 g_checksum_free (cs);
4476 gst_rtspsrc_activate_streams (src);
4477 src->need_activate = FALSE;
4478 src->need_segment = TRUE;
4481 if (src->base_time == -1) {
4482 /* Take current running_time. This timestamp will be put on
4483 * the first buffer of each stream because we are a live source and so we
4484 * timestamp with the running_time. When we are dealing with TCP, we also
4485 * only timestamp the first buffer (using the DISCONT flag) because a server
4486 * typically bursts data, for which we don't want to compensate by speeding
4487 * up the media. The other timestamps will be interpollated from this one
4488 * using the RTP timestamps. */
4489 GST_OBJECT_LOCK (src);
4490 if (GST_ELEMENT_CLOCK (src)) {
4492 GstClockTime base_time;
4494 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4495 base_time = GST_ELEMENT_CAST (src)->base_time;
4497 src->base_time = now - base_time;
4499 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4500 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4502 GST_OBJECT_UNLOCK (src);
4505 /* If needed send a new segment, don't forget we are live and buffer are
4506 * timestamped with running time */
4507 if (src->need_segment) {
4509 src->need_segment = FALSE;
4510 gst_segment_init (&segment, GST_FORMAT_TIME);
4511 gst_rtspsrc_push_event (src, gst_event_new_segment (&segment));
4514 if (stream->need_caps) {
4517 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt))) {
4518 /* only streams that have a connection to the outside world */
4519 if (stream->setup) {
4520 /* Only need to update the TCP caps here, UDP is already handled */
4521 if (stream->channelpad[0]) {
4522 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4523 gst_pad_push_event (stream->channelpad[0],
4524 gst_event_new_caps (caps));
4526 gst_pad_send_event (stream->channelpad[0],
4527 gst_event_new_caps (caps));
4529 stream->need_caps = FALSE;
4533 stream->need_caps = FALSE;
4536 if (stream->discont && !is_rtcp) {
4537 /* mark first RTP buffer as discont */
4538 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4539 stream->discont = FALSE;
4540 /* first buffer gets the timestamp, other buffers are not timestamped and
4541 * their presentation time will be interpollated from the rtp timestamps. */
4542 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4543 GST_TIME_ARGS (src->base_time));
4545 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4548 /* chain to the peer pad */
4549 if (GST_PAD_IS_SINK (outpad))
4550 ret = gst_pad_chain (outpad, buf);
4552 ret = gst_pad_push (outpad, buf);
4555 /* combine all stream flows for the data transport */
4556 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4563 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4564 gst_rtsp_message_unset (message);
4569 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4570 ("Short message received, ignoring."));
4571 gst_rtsp_message_unset (message);
4576 static GstFlowReturn
4577 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4579 GstRTSPMessage message = { 0 };
4581 GstFlowReturn ret = GST_FLOW_OK;
4582 GTimeVal tv_timeout;
4585 /* get the next timeout interval */
4586 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4588 /* see if the timeout period expired */
4589 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4590 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4591 /* send keep-alive, only act on interrupt, a warning will be posted for
4593 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4595 /* get new timeout */
4596 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4599 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4600 tv_timeout.tv_sec, tv_timeout.tv_usec);
4602 /* protect the connection with the connection lock so that we can see when
4603 * we are finished doing server communication */
4605 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4606 &message, src->ptcp_timeout);
4610 GST_DEBUG_OBJECT (src, "we received a server message");
4612 case GST_RTSP_EINTR:
4613 /* we got interrupted this means we need to stop */
4615 case GST_RTSP_ETIMEOUT:
4616 /* no reply, send keep alive */
4617 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4618 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4622 /* go EOS when the server closed the connection */
4628 switch (message.type) {
4629 case GST_RTSP_MESSAGE_REQUEST:
4630 /* server sends us a request message, handle it */
4632 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4634 if (res == GST_RTSP_EEOF)
4637 goto handle_request_failed;
4639 case GST_RTSP_MESSAGE_RESPONSE:
4640 /* we ignore response messages */
4641 GST_DEBUG_OBJECT (src, "ignoring response message");
4643 gst_rtsp_message_dump (&message);
4645 case GST_RTSP_MESSAGE_DATA:
4646 GST_DEBUG_OBJECT (src, "got data message");
4647 ret = gst_rtspsrc_handle_data (src, &message);
4648 if (ret != GST_FLOW_OK)
4649 goto handle_data_failed;
4652 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4657 g_assert_not_reached ();
4662 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4663 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4664 ("The server closed the connection."));
4665 src->conninfo.connected = FALSE;
4666 gst_rtsp_message_unset (&message);
4667 return GST_FLOW_EOS;
4671 gst_rtsp_message_unset (&message);
4672 GST_DEBUG_OBJECT (src, "got interrupted");
4673 return GST_FLOW_FLUSHING;
4677 gchar *str = gst_rtsp_strresult (res);
4679 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4680 ("Could not receive message. (%s)", str));
4683 gst_rtsp_message_unset (&message);
4684 return GST_FLOW_ERROR;
4686 handle_request_failed:
4688 gchar *str = gst_rtsp_strresult (res);
4690 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4691 ("Could not handle server message. (%s)", str));
4693 gst_rtsp_message_unset (&message);
4694 return GST_FLOW_ERROR;
4698 GST_DEBUG_OBJECT (src, "could no handle data message");
4703 static GstFlowReturn
4704 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4707 GstRTSPMessage message = { 0 };
4711 GTimeVal tv_timeout;
4713 /* get the next timeout interval */
4714 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4716 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4717 (gint) tv_timeout.tv_sec);
4719 gst_rtsp_message_unset (&message);
4721 /* we should continue reading the TCP socket because the server might
4722 * send us requests. When the session timeout expires, we need to send a
4723 * keep-alive request to keep the session open. */
4724 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4725 &message, &tv_timeout);
4729 GST_DEBUG_OBJECT (src, "we received a server message");
4731 case GST_RTSP_EINTR:
4732 /* we got interrupted, see what we have to do */
4734 case GST_RTSP_ETIMEOUT:
4735 /* send keep-alive, ignore the result, a warning will be posted. */
4736 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4737 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4741 /* server closed the connection. not very fatal for UDP, reconnect and
4742 * see what happens. */
4743 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4744 ("The server closed the connection."));
4745 if (src->udp_reconnect) {
4747 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4754 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4756 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4757 ("Unhandled return value %d.", res));
4761 switch (message.type) {
4762 case GST_RTSP_MESSAGE_REQUEST:
4763 /* server sends us a request message, handle it */
4765 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4767 if (res == GST_RTSP_EEOF)
4770 goto handle_request_failed;
4772 case GST_RTSP_MESSAGE_RESPONSE:
4773 /* we ignore response and data messages */
4774 GST_DEBUG_OBJECT (src, "ignoring response message");
4776 gst_rtsp_message_dump (&message);
4777 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4778 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4779 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4780 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4781 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4788 case GST_RTSP_MESSAGE_DATA:
4789 /* we ignore response and data messages */
4790 GST_DEBUG_OBJECT (src, "ignoring data message");
4793 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4798 g_assert_not_reached ();
4800 /* we get here when the connection got interrupted */
4803 gst_rtsp_message_unset (&message);
4804 GST_DEBUG_OBJECT (src, "got interrupted");
4805 return GST_FLOW_FLUSHING;
4809 gchar *str = gst_rtsp_strresult (res);
4812 src->conninfo.connected = FALSE;
4813 if (res != GST_RTSP_EINTR) {
4814 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4815 ("Could not connect to server. (%s)", str));
4817 ret = GST_FLOW_ERROR;
4819 ret = GST_FLOW_FLUSHING;
4825 gchar *str = gst_rtsp_strresult (res);
4827 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4828 ("Could not receive message. (%s)", str));
4830 return GST_FLOW_ERROR;
4832 handle_request_failed:
4834 gchar *str = gst_rtsp_strresult (res);
4837 gst_rtsp_message_unset (&message);
4838 if (res != GST_RTSP_EINTR) {
4839 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4840 ("Could not handle server message. (%s)", str));
4842 ret = GST_FLOW_ERROR;
4844 ret = GST_FLOW_FLUSHING;
4850 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4851 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4852 ("The server closed the connection."));
4853 src->conninfo.connected = FALSE;
4854 gst_rtsp_message_unset (&message);
4855 return GST_FLOW_EOS;
4859 static GstRTSPResult
4860 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4862 GstRTSPResult res = GST_RTSP_OK;
4865 GST_DEBUG_OBJECT (src, "doing reconnect");
4867 GST_OBJECT_LOCK (src);
4868 /* only restart when the pads were not yet activated, else we were
4869 * streaming over UDP */
4870 restart = src->need_activate;
4871 GST_OBJECT_UNLOCK (src);
4873 /* no need to restart, we're done */
4877 /* we can try only TCP now */
4878 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
4880 /* close and cleanup our state */
4881 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
4884 /* see if we have TCP left to try. Also don't try TCP when we were configured
4886 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
4889 /* We post a warning message now to inform the user
4890 * that nothing happened. It's most likely a firewall thing. */
4891 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4892 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4893 "firewall is blocking it. Retrying using a TCP connection.",
4894 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4896 /* open new connection using tcp */
4897 if (gst_rtspsrc_open (src, async) < 0)
4900 /* start playback */
4901 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
4910 src->cur_protocols = 0;
4911 /* no transport possible, post an error and stop */
4912 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4913 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4914 "firewall is blocking it. No other protocols to try.",
4915 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4916 return GST_RTSP_ERROR;
4920 GST_DEBUG_OBJECT (src, "open failed");
4925 GST_DEBUG_OBJECT (src, "play failed");
4931 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
4935 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
4938 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
4941 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
4944 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
4952 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
4956 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
4959 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
4962 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
4965 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4973 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4977 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4980 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4983 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4986 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4994 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4998 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
5001 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
5004 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
5007 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
5015 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
5017 if (ret == GST_RTSP_OK)
5018 gst_rtspsrc_loop_complete_cmd (src, cmd);
5019 else if (ret == GST_RTSP_EINTR)
5020 gst_rtspsrc_loop_cancel_cmd (src, cmd);
5022 gst_rtspsrc_loop_error_cmd (src, cmd);
5026 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
5029 gboolean flushed = FALSE;
5031 /* start new request */
5032 gst_rtspsrc_loop_start_cmd (src, cmd);
5034 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
5036 GST_OBJECT_LOCK (src);
5037 old = src->pending_cmd;
5038 if (old == CMD_RECONNECT) {
5039 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
5040 cmd = CMD_RECONNECT;
5042 if (old != CMD_WAIT) {
5043 src->pending_cmd = CMD_WAIT;
5044 GST_OBJECT_UNLOCK (src);
5045 /* cancel previous request */
5046 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
5047 gst_rtspsrc_loop_cancel_cmd (src, old);
5048 GST_OBJECT_LOCK (src);
5050 src->pending_cmd = cmd;
5051 /* interrupt if allowed */
5052 if (src->busy_cmd & mask) {
5053 GST_DEBUG_OBJECT (src, "connection flush busy %s",
5054 cmd_to_string (src->busy_cmd));
5055 gst_rtspsrc_connection_flush (src, TRUE);
5058 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
5059 cmd_to_string (src->busy_cmd));
5062 gst_task_start (src->task);
5063 GST_OBJECT_UNLOCK (src);
5069 gst_rtspsrc_loop (GstRTSPSrc * src)
5073 if (!src->conninfo.connection || !src->conninfo.connected)
5076 if (src->interleaved)
5077 ret = gst_rtspsrc_loop_interleaved (src);
5079 ret = gst_rtspsrc_loop_udp (src);
5081 if (ret != GST_FLOW_OK)
5089 GST_WARNING_OBJECT (src, "we are not connected");
5090 ret = GST_FLOW_FLUSHING;
5095 const gchar *reason = gst_flow_get_name (ret);
5097 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
5098 src->running = FALSE;
5099 if (ret == GST_FLOW_EOS) {
5100 /* perform EOS logic */
5101 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5102 gst_element_post_message (GST_ELEMENT_CAST (src),
5103 gst_message_new_segment_done (GST_OBJECT_CAST (src),
5104 src->segment.format, src->segment.position));
5105 gst_rtspsrc_push_event (src,
5106 gst_event_new_segment_done (src->segment.format,
5107 src->segment.position));
5109 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5111 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
5112 /* for fatal errors we post an error message, post the error before the
5113 * EOS so the app knows about the error first. */
5114 GST_ELEMENT_ERROR (src, STREAM, FAILED,
5115 ("Internal data flow error."),
5116 ("streaming task paused, reason %s (%d)", reason, ret));
5117 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5119 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
5124 #ifndef GST_DISABLE_GST_DEBUG
5125 static const gchar *
5126 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
5130 while (method != 0) {
5147 static const gchar *
5148 gst_rtspsrc_skip_lws (const gchar * s)
5150 while (g_ascii_isspace (*s))
5155 static const gchar *
5156 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
5158 while (s > start && g_ascii_isspace (*(s - 1)))
5163 static const gchar *
5164 gst_rtspsrc_skip_commas (const gchar * s)
5166 /* The grammar allows for multiple commas */
5167 while (g_ascii_isspace (*s) || *s == ',')
5172 static const gchar *
5173 gst_rtspsrc_skip_item (const gchar * s)
5175 gboolean quoted = FALSE;
5176 const gchar *start = s;
5178 /* A list item ends at the last non-whitespace character
5179 * before a comma which is not inside a quoted-string. Or at
5180 * the end of the string.
5186 if (*s == '\\' && *(s + 1))
5195 return gst_rtspsrc_unskip_lws (s, start);
5199 gst_rtsp_decode_quoted_string (gchar * quoted_string)
5203 src = quoted_string + 1;
5204 dst = quoted_string;
5205 while (*src && *src != '"') {
5206 if (*src == '\\' && *(src + 1))
5213 /* Extract the authentication tokens that the server provided for each method
5214 * into an array of structures and give those to the connection object.
5217 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
5218 const gchar * header, gboolean * stale)
5220 GSList *list = NULL, *iter;
5222 gchar *item, *eq, *name_end, *value;
5224 g_return_if_fail (stale != NULL);
5226 gst_rtsp_connection_clear_auth_params (conn);
5229 /* Parse a header whose content is described by RFC2616 as
5230 * "#something", where "something" does not itself contain commas,
5231 * except as part of quoted-strings, into a list of allocated strings.
5233 header = gst_rtspsrc_skip_commas (header);
5235 end = gst_rtspsrc_skip_item (header);
5236 list = g_slist_prepend (list, g_strndup (header, end - header));
5237 header = gst_rtspsrc_skip_commas (end);
5242 list = g_slist_reverse (list);
5243 for (iter = list; iter; iter = iter->next) {
5246 eq = strchr (item, '=');
5248 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
5249 if (name_end == item) {
5250 /* That's no good... */
5257 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
5259 gst_rtsp_decode_quoted_string (value);
5263 if (value && strcmp (item, "stale") == 0 && strcmp (value, "TRUE") == 0)
5265 gst_rtsp_connection_set_auth_param (conn, item, value);
5269 g_slist_free (list);
5272 /* Parse a WWW-Authenticate Response header and determine the
5273 * available authentication methods
5275 * This code should also cope with the fact that each WWW-Authenticate
5276 * header can contain multiple challenge methods + tokens
5278 * At the moment, for Basic auth, we just do a minimal check and don't
5279 * even parse out the realm */
5281 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
5282 GstRTSPConnection * conn, gboolean * stale)
5286 g_return_if_fail (hdr != NULL);
5287 g_return_if_fail (methods != NULL);
5288 g_return_if_fail (stale != NULL);
5290 /* Skip whitespace at the start of the string */
5291 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
5293 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
5294 *methods |= GST_RTSP_AUTH_BASIC;
5295 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
5296 *methods |= GST_RTSP_AUTH_DIGEST;
5297 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
5302 * gst_rtspsrc_setup_auth:
5303 * @src: the rtsp source
5305 * Configure a username and password and auth method on the
5306 * connection object based on a response we received from the
5309 * Currently, this requires that a username and password were supplied
5310 * in the uri. In the future, they may be requested on demand by sending
5311 * a message up the bus.
5313 * Returns: TRUE if authentication information could be set up correctly.
5316 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5320 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5321 GstRTSPAuthMethod method;
5322 GstRTSPResult auth_result;
5324 GstRTSPConnection *conn;
5326 gboolean stale = FALSE;
5328 conn = src->conninfo.connection;
5330 /* Identify the available auth methods and see if any are supported */
5331 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
5332 &hdr, 0) == GST_RTSP_OK) {
5333 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
5336 if (avail_methods == GST_RTSP_AUTH_NONE)
5337 goto no_auth_available;
5339 /* For digest auth, if the response indicates that the session
5340 * data are stale, we just update them in the connection object and
5341 * return TRUE to retry the request */
5343 src->tried_url_auth = FALSE;
5345 url = gst_rtsp_connection_get_url (conn);
5347 /* Do we have username and password available? */
5348 if (url != NULL && !src->tried_url_auth && url->user != NULL
5349 && url->passwd != NULL) {
5352 src->tried_url_auth = TRUE;
5353 GST_DEBUG_OBJECT (src,
5354 "Attempting authentication using credentials from the URL");
5356 user = src->user_id;
5357 pass = src->user_pw;
5358 GST_DEBUG_OBJECT (src,
5359 "Attempting authentication using credentials from the properties");
5362 /* FIXME: If the url didn't contain username and password or we tried them
5363 * already, request a username and passwd from the application via some kind
5364 * of credentials request message */
5366 /* If we don't have a username and passwd at this point, bail out. */
5367 if (user == NULL || pass == NULL)
5370 /* Try to configure for each available authentication method, strongest to
5372 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5373 /* Check if this method is available on the server */
5374 if ((method & avail_methods) == 0)
5377 /* Pass the credentials to the connection to try on the next request */
5378 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5379 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5380 * ignore it and end up retrying later */
5381 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5382 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5383 gst_rtsp_auth_method_to_string (method));
5388 if (method == GST_RTSP_AUTH_NONE)
5389 goto no_auth_available;
5395 /* Output an error indicating that we couldn't connect because there were
5396 * no supported authentication protocols */
5397 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5398 ("No supported authentication protocol was found"));
5403 /* We don't fire an error message, we just return FALSE and let the
5404 * normal NOT_AUTHORIZED error be propagated */
5409 static GstRTSPResult
5410 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5411 GstRTSPMessage * request, GstRTSPMessage * response,
5412 GstRTSPStatusCode * code)
5415 GstRTSPStatusCode thecode;
5416 gchar *content_base = NULL;
5420 if (!src->short_header)
5421 gst_rtsp_ext_list_before_send (src->extensions, request);
5423 GST_DEBUG_OBJECT (src, "sending message");
5426 gst_rtsp_message_dump (request);
5428 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
5432 gst_rtsp_connection_reset_timeout (conn);
5435 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
5440 gst_rtsp_message_dump (response);
5442 switch (response->type) {
5443 case GST_RTSP_MESSAGE_REQUEST:
5444 res = gst_rtspsrc_handle_request (src, conn, response);
5445 if (res == GST_RTSP_EEOF)
5448 goto handle_request_failed;
5450 case GST_RTSP_MESSAGE_RESPONSE:
5451 /* ok, a response is good */
5452 GST_DEBUG_OBJECT (src, "received response message");
5454 case GST_RTSP_MESSAGE_DATA:
5455 /* get next response */
5456 GST_DEBUG_OBJECT (src, "handle data response message");
5457 gst_rtspsrc_handle_data (src, response);
5460 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5465 thecode = response->type_data.response.code;
5467 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5469 /* if the caller wanted the result code, we store it. */
5473 /* If the request didn't succeed, bail out before doing any more */
5474 if (thecode != GST_RTSP_STS_OK)
5477 /* store new content base if any */
5478 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5481 g_free (src->content_base);
5482 src->content_base = g_strdup (content_base);
5484 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5491 gchar *str = gst_rtsp_strresult (res);
5493 if (res != GST_RTSP_EINTR) {
5494 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5495 ("Could not send message. (%s)", str));
5497 GST_WARNING_OBJECT (src, "send interrupted");
5506 GST_WARNING_OBJECT (src, "server closed connection");
5507 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5509 /* if reconnect succeeds, try again */
5511 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5515 /* only try once after reconnect, then fallthrough and error out */
5518 gchar *str = gst_rtsp_strresult (res);
5520 if (res != GST_RTSP_EINTR) {
5521 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5522 ("Could not receive message. (%s)", str));
5524 GST_WARNING_OBJECT (src, "receive interrupted");
5532 handle_request_failed:
5534 /* ERROR was posted */
5535 gst_rtsp_message_unset (response);
5540 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5541 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5542 ("The server closed the connection."));
5543 gst_rtsp_message_unset (response);
5550 * @src: the rtsp source
5551 * @conn: the connection to send on
5552 * @request: must point to a valid request
5553 * @response: must point to an empty #GstRTSPMessage
5554 * @code: an optional code result
5556 * send @request and retrieve the response in @response. optionally @code can be
5557 * non-NULL in which case it will contain the status code of the response.
5559 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5560 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5562 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5563 * @response message) if the response code was not 200 (OK).
5565 * If the attempt results in an authentication failure, then this will attempt
5566 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5569 * Returns: #GST_RTSP_OK if the processing was successful.
5571 static GstRTSPResult
5572 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5573 GstRTSPMessage * request, GstRTSPMessage * response,
5574 GstRTSPStatusCode * code)
5576 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5577 GstRTSPResult res = GST_RTSP_ERROR;
5580 GstRTSPMethod method = GST_RTSP_INVALID;
5586 /* make sure we don't loop forever */
5590 /* save method so we can disable it when the server complains */
5591 method = request->type_data.request.method;
5594 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5598 case GST_RTSP_STS_UNAUTHORIZED:
5599 case GST_RTSP_STS_NOT_FOUND:
5600 if (gst_rtspsrc_setup_auth (src, response)) {
5601 /* Try the request/response again after configuring the auth info
5609 } while (retry == TRUE);
5611 /* If the user requested the code, let them handle errors, otherwise
5612 * post an error below */
5615 else if (int_code != GST_RTSP_STS_OK)
5616 goto error_response;
5623 GST_DEBUG_OBJECT (src, "got error %d", res);
5628 res = GST_RTSP_ERROR;
5630 switch (response->type_data.response.code) {
5631 case GST_RTSP_STS_NOT_FOUND:
5632 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5633 response->type_data.response.reason));
5635 case GST_RTSP_STS_UNAUTHORIZED:
5636 GST_ELEMENT_ERROR (src, RESOURCE, NOT_AUTHORIZED, (NULL), ("%s",
5637 response->type_data.response.reason));
5639 case GST_RTSP_STS_MOVED_PERMANENTLY:
5640 case GST_RTSP_STS_MOVE_TEMPORARILY:
5642 gchar *new_location;
5643 GstRTSPLowerTrans transports;
5645 GST_DEBUG_OBJECT (src, "got redirection");
5646 /* if we don't have a Location Header, we must error */
5647 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5648 &new_location, 0) < 0)
5651 /* When we receive a redirect result, we go back to the INIT state after
5652 * parsing the new URI. The caller should do the needed steps to issue
5653 * a new setup when it detects this state change. */
5654 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5656 /* save current transports */
5657 if (src->conninfo.url)
5658 transports = src->conninfo.url->transports;
5660 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5662 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5664 /* set old transports */
5665 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5666 src->conninfo.url->transports = transports;
5668 src->need_redirect = TRUE;
5669 src->state = GST_RTSP_STATE_INIT;
5673 case GST_RTSP_STS_NOT_ACCEPTABLE:
5674 case GST_RTSP_STS_NOT_IMPLEMENTED:
5675 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5676 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5677 gst_rtsp_method_as_text (method));
5678 src->methods &= ~method;
5682 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5683 ("Got error response: %d (%s).", response->type_data.response.code,
5684 response->type_data.response.reason));
5687 /* if we return ERROR we should unset the response ourselves */
5688 if (res == GST_RTSP_ERROR)
5689 gst_rtsp_message_unset (response);
5695 static GstRTSPResult
5696 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5697 GstRTSPMessage * response, GstRTSPSrc * src)
5699 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5704 /* parse the response and collect all the supported methods. We need this
5705 * information so that we don't try to send an unsupported request to the
5709 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5711 GstRTSPHeaderField field;
5715 /* reset supported methods */
5718 /* Try Allow Header first */
5719 field = GST_RTSP_HDR_ALLOW;
5722 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5723 if (indx == 0 && !respoptions) {
5724 /* if no Allow header was found then try the Public header... */
5725 field = GST_RTSP_HDR_PUBLIC;
5726 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5731 src->methods |= gst_rtsp_options_from_text (respoptions);
5736 if (src->methods == 0) {
5737 /* neither Allow nor Public are required, assume the server supports
5738 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5740 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5741 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5743 /* always assume PLAY, FIXME, extensions should be able to override
5745 src->methods |= GST_RTSP_PLAY;
5746 /* also assume it will support Range */
5747 src->seekable = TRUE;
5749 /* we need describe and setup */
5750 if (!(src->methods & GST_RTSP_DESCRIBE))
5752 if (!(src->methods & GST_RTSP_SETUP))
5760 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5761 ("Server does not support DESCRIBE."));
5766 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5767 ("Server does not support SETUP."));
5772 /* masks to be kept in sync with the hardcoded protocol order of preference
5774 static const guint protocol_masks[] = {
5775 GST_RTSP_LOWER_TRANS_UDP,
5776 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5777 GST_RTSP_LOWER_TRANS_TCP,
5781 static GstRTSPResult
5782 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5783 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
5787 gboolean add_udp_str;
5792 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5797 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5799 /* extension listed transports, use those */
5800 if (*transports != NULL)
5803 /* it's the default */
5804 add_udp_str = FALSE;
5806 /* the default RTSP transports */
5807 result = g_string_new ("RTP");
5810 case GST_RTSP_PROFILE_AVP:
5811 g_string_append (result, "/AVP");
5813 case GST_RTSP_PROFILE_SAVP:
5814 g_string_append (result, "/SAVP");
5816 case GST_RTSP_PROFILE_AVPF:
5817 g_string_append (result, "/AVPF");
5819 case GST_RTSP_PROFILE_SAVPF:
5820 g_string_append (result, "/SAVPF");
5826 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5827 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5829 g_string_append (result, "/UDP");
5830 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5831 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5832 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5833 /* we don't have to allocate any UDP ports yet, if the selected transport
5834 * turns out to be multicast we can create them and join the multicast
5835 * group indicated in the transport reply */
5837 g_string_append (result, "/UDP");
5838 g_string_append (result, ";multicast");
5839 if (src->next_port_num != 0) {
5840 if (src->client_port_range.max > 0 &&
5841 src->next_port_num >= src->client_port_range.max)
5844 g_string_append_printf (result, ";client_port=%d-%d",
5845 src->next_port_num, src->next_port_num + 1);
5847 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
5848 GST_DEBUG_OBJECT (src, "adding TCP");
5850 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
5852 *transports = g_string_free (result, FALSE);
5854 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5861 GST_ERROR ("extension gave error %d", res);
5866 GST_ERROR ("no more ports available");
5867 return GST_RTSP_ERROR;
5871 static GstRTSPResult
5872 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5873 gint orig_rtpport, gint orig_rtcpport)
5876 gint nr_udp, nr_int;
5878 gint rtpport = 0, rtcpport = 0;
5881 src = stream->parent;
5883 /* find number of placeholders first */
5884 if (strstr (*transports, "%%i2"))
5886 else if (strstr (*transports, "%%i1"))
5891 if (strstr (*transports, "%%u2"))
5893 else if (strstr (*transports, "%%u1"))
5898 if (nr_udp == 0 && nr_int == 0)
5902 if (!orig_rtpport || !orig_rtcpport) {
5903 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5906 rtpport = orig_rtpport;
5907 rtcpport = orig_rtcpport;
5911 str = g_string_new ("");
5913 while ((next = strstr (p, "%%"))) {
5914 g_string_append_len (str, p, next - p);
5915 if (next[2] == 'u') {
5917 g_string_append_printf (str, "%d", rtpport);
5918 else if (next[3] == '2')
5919 g_string_append_printf (str, "%d", rtcpport);
5921 if (next[2] == 'i') {
5923 g_string_append_printf (str, "%d", src->free_channel);
5924 else if (next[3] == '2')
5925 g_string_append_printf (str, "%d", src->free_channel + 1);
5930 /* append final part */
5931 g_string_append (str, p);
5933 g_free (*transports);
5934 *transports = g_string_free (str, FALSE);
5942 GST_ERROR ("failed to allocate udp ports");
5943 return GST_RTSP_ERROR;
5948 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
5950 GstCaps *caps = NULL;
5952 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
5956 GST_DEBUG_OBJECT (src, "SRTP parameters received");
5962 default_srtcp_params (void)
5969 guint data_size = GST_ROUND_UP_4 (KEY_SIZE);
5971 /* create a random key */
5972 key_data = g_malloc (data_size);
5973 for (i = 0; i < data_size; i += 4)
5974 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
5976 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
5978 caps = gst_caps_new_simple ("application/x-srtcp",
5979 "srtp-key", GST_TYPE_BUFFER, buf,
5980 "srtp-cipher", G_TYPE_STRING, "aes-128-icm",
5981 "srtp-auth", G_TYPE_STRING, "hmac-sha1-80",
5982 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
5983 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
5985 gst_buffer_unref (buf);
5991 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
5993 gchar *base64, *result = NULL;
5994 GstMIKEYMessage *mikey_msg;
5996 stream->srtcpparams = signal_get_srtcp_params (src, stream);
5997 if (stream->srtcpparams == NULL)
5998 stream->srtcpparams = default_srtcp_params ();
6000 mikey_msg = gst_mikey_message_new_from_caps (stream->srtcpparams);
6002 /* add policy '0' for our SSRC */
6003 gst_mikey_message_add_cs_srtp (mikey_msg, 0, stream->send_ssrc, 0);
6005 base64 = gst_mikey_message_base64_encode (mikey_msg);
6006 gst_mikey_message_unref (mikey_msg);
6009 result = gst_sdp_make_keymgmt (stream->conninfo.location, base64);
6017 /* Perform the SETUP request for all the streams.
6019 * We ask the server for a specific transport, which initially includes all the
6020 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
6021 * two local UDP ports that we send to the server.
6023 * Once the server replied with a transport, we configure the other streams
6024 * with the same transport.
6026 * This function will also configure the stream for the selected transport,
6027 * which basically means creating the pipeline.
6029 static GstRTSPResult
6030 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
6033 GstRTSPResult res = GST_RTSP_ERROR;
6034 GstRTSPMessage request = { 0 };
6035 GstRTSPMessage response = { 0 };
6036 GstRTSPStream *stream = NULL;
6037 GstRTSPLowerTrans protocols;
6038 GstRTSPStatusCode code;
6039 gboolean unsupported_real = FALSE;
6040 gint rtpport, rtcpport;
6044 if (src->conninfo.connection) {
6045 url = gst_rtsp_connection_get_url (src->conninfo.connection);
6046 /* we initially allow all configured lower transports. based on the URL
6047 * transports and the replies from the server we narrow them down. */
6048 protocols = url->transports & src->cur_protocols;
6051 protocols = src->cur_protocols;
6057 /* reset some state */
6058 src->free_channel = 0;
6059 src->interleaved = FALSE;
6060 src->need_activate = FALSE;
6061 /* keep track of next port number, 0 is random */
6062 src->next_port_num = src->client_port_range.min;
6063 rtpport = rtcpport = 0;
6065 if (G_UNLIKELY (src->streams == NULL))
6068 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6069 GstRTSPConnection *conn;
6076 stream = (GstRTSPStream *) walk->data;
6078 caps = stream_get_caps_for_pt (stream, stream->default_pt);
6080 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
6084 if (stream->skipped) {
6085 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
6089 /* see if we need to configure this stream */
6090 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
6091 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
6096 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
6097 stream->id, caps, &selected);
6099 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
6103 /* merge/overwrite global caps */
6108 s = gst_caps_get_structure (caps, 0);
6110 num = gst_structure_n_fields (src->props);
6111 for (j = 0; j < num; j++) {
6115 name = gst_structure_nth_field_name (src->props, j);
6116 val = gst_structure_get_value (src->props, name);
6117 gst_structure_set_value (s, name, val);
6119 GST_DEBUG_OBJECT (src, "copied %s", name);
6123 /* skip setup if we have no URL for it */
6124 if (stream->conninfo.location == NULL) {
6125 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
6129 if (src->conninfo.connection == NULL) {
6130 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
6131 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
6134 conn = stream->conninfo.connection;
6136 conn = src->conninfo.connection;
6138 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
6139 stream->conninfo.location);
6141 /* if we have a multicast connection, only suggest multicast from now on */
6142 if (stream->is_multicast)
6143 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
6146 /* first selectable protocol */
6147 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6149 if (!protocol_masks[mask])
6153 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
6154 protocol_masks[mask]);
6155 /* create a string with first transport in line */
6157 res = gst_rtspsrc_create_transports_string (src,
6158 protocols & protocol_masks[mask], stream->profile, &transports);
6159 if (res < 0 || transports == NULL)
6160 goto setup_transport_failed;
6162 if (strlen (transports) == 0) {
6163 g_free (transports);
6164 GST_DEBUG_OBJECT (src, "no transports found");
6169 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
6171 /* replace placeholders with real values, this function will optionally
6172 * allocate UDP ports and other info needed to execute the setup request */
6173 res = gst_rtspsrc_prepare_transports (stream, &transports,
6174 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
6176 g_free (transports);
6177 goto setup_transport_failed;
6180 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
6182 /* create SETUP request */
6184 gst_rtspsrc_init_request (src, &request, GST_RTSP_SETUP,
6185 stream->conninfo.location);
6187 g_free (transports);
6188 goto create_request_failed;
6191 /* select transport */
6192 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
6195 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
6196 stream->profile == GST_RTSP_PROFILE_SAVPF) {
6197 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
6198 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
6201 /* if the user wants a non default RTP packet size we add the blocksize
6203 if (src->rtp_blocksize > 0) {
6204 hval = g_strdup_printf ("%d", src->rtp_blocksize);
6205 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
6209 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
6212 /* handle the code ourselves */
6213 res = gst_rtspsrc_send (src, conn, &request, &response, &code);
6218 case GST_RTSP_STS_OK:
6220 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
6221 gst_rtsp_message_unset (&request);
6222 gst_rtsp_message_unset (&response);
6223 /* cleanup of leftover transport */
6224 gst_rtspsrc_stream_free_udp (stream);
6225 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
6226 * we might be in this case */
6227 if (stream->container && rtpport && rtcpport && !retry) {
6228 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
6233 /* this transport did not go down well, but we may have others to try
6234 * that we did not send yet, try those and only give up then
6235 * but not without checking for lost cause/extension so we can
6236 * post a nicer/more useful error message later */
6237 if (!unsupported_real)
6238 unsupported_real = stream->is_real;
6239 /* select next available protocol, give up on this stream if none */
6241 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6243 if (!protocol_masks[mask] || unsupported_real)
6248 /* cleanup of leftover transport and move to the next stream */
6249 gst_rtspsrc_stream_free_udp (stream);
6250 goto response_error;
6253 /* parse response transport */
6255 gchar *resptrans = NULL;
6256 GstRTSPTransport transport = { 0 };
6258 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
6261 gst_rtspsrc_stream_free_udp (stream);
6265 /* parse transport, go to next stream on parse error */
6266 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6267 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6271 /* update allowed transports for other streams. once the transport of
6272 * one stream has been determined, we make sure that all other streams
6273 * are configured in the same way */
6274 switch (transport.lower_transport) {
6275 case GST_RTSP_LOWER_TRANS_TCP:
6276 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6277 protocols = GST_RTSP_LOWER_TRANS_TCP;
6278 src->interleaved = TRUE;
6279 /* update free channels */
6281 MAX (transport.interleaved.min, src->free_channel);
6283 MAX (transport.interleaved.max, src->free_channel);
6284 src->free_channel++;
6286 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6287 /* only allow multicast for other streams */
6288 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6289 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6290 /* if the server selected our ports, increment our counters so that
6291 * we select a new port later */
6292 if (src->next_port_num == transport.port.min &&
6293 src->next_port_num + 1 == transport.port.max) {
6294 src->next_port_num += 2;
6297 case GST_RTSP_LOWER_TRANS_UDP:
6298 /* only allow unicast for other streams */
6299 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6300 protocols = GST_RTSP_LOWER_TRANS_UDP;
6303 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6304 transport.lower_transport);
6308 if (!src->interleaved || !retry) {
6309 /* now configure the stream with the selected transport */
6310 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6311 GST_DEBUG_OBJECT (src,
6312 "could not configure stream %p transport, skipping stream",
6315 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
6316 /* retain the first allocated UDP port pair */
6317 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
6318 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
6321 /* we need to activate at least one streams when we detect activity */
6322 src->need_activate = TRUE;
6324 /* stream is setup now */
6325 stream->setup = TRUE;
6330 GstRTSPStream *sskip;
6332 skip = g_list_next (skip);
6336 sskip = (GstRTSPStream *) skip->data;
6338 /* skip all streams with the same control url */
6339 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
6340 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
6341 sskip, sskip->conninfo.location);
6342 sskip->skipped = TRUE;
6347 /* clean up our transport struct */
6348 gst_rtsp_transport_init (&transport);
6349 /* clean up used RTSP messages */
6350 gst_rtsp_message_unset (&request);
6351 gst_rtsp_message_unset (&response);
6355 /* store the transport protocol that was configured */
6356 src->cur_protocols = protocols;
6358 gst_rtsp_ext_list_stream_select (src->extensions, url);
6360 /* if there is nothing to activate, error out */
6361 if (!src->need_activate)
6362 goto nothing_to_activate;
6369 /* no transport possible, post an error and stop */
6370 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6371 ("Could not connect to server, no protocols left"));
6372 return GST_RTSP_ERROR;
6376 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6377 ("SDP contains no streams"));
6378 return GST_RTSP_ERROR;
6380 create_request_failed:
6382 gchar *str = gst_rtsp_strresult (res);
6384 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6385 ("Could not create request. (%s)", str));
6389 setup_transport_failed:
6391 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6392 ("Could not setup transport."));
6393 res = GST_RTSP_ERROR;
6398 const gchar *str = gst_rtsp_status_as_text (code);
6400 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6401 ("Error (%d): %s", code, GST_STR_NULL (str)));
6402 res = GST_RTSP_ERROR;
6407 gchar *str = gst_rtsp_strresult (res);
6409 if (res != GST_RTSP_EINTR) {
6410 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6411 ("Could not send message. (%s)", str));
6413 GST_WARNING_OBJECT (src, "send interrupted");
6420 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6421 ("Server did not select transport."));
6422 res = GST_RTSP_ERROR;
6425 nothing_to_activate:
6427 /* none of the available error codes is really right .. */
6428 if (unsupported_real) {
6429 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6430 (_("No supported stream was found. You might need to install a "
6431 "GStreamer RTSP extension plugin for Real media streams.")),
6434 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6435 (_("No supported stream was found. You might need to allow "
6436 "more transport protocols or may otherwise be missing "
6437 "the right GStreamer RTSP extension plugin.")), (NULL));
6439 return GST_RTSP_ERROR;
6443 gst_rtsp_message_unset (&request);
6444 gst_rtsp_message_unset (&response);
6450 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
6451 GstSegment * segment)
6454 GstRTSPTimeRange *therange;
6457 gst_rtsp_range_free (src->range);
6459 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
6460 GST_DEBUG_OBJECT (src, "parsed range %s", range);
6461 src->range = therange;
6463 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6465 gst_segment_init (segment, GST_FORMAT_TIME);
6469 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6470 therange->min.type, therange->min.seconds, therange->max.type,
6471 therange->max.seconds);
6473 if (therange->min.type == GST_RTSP_TIME_NOW)
6475 else if (therange->min.type == GST_RTSP_TIME_END)
6478 seconds = therange->min.seconds * GST_SECOND;
6480 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6481 GST_TIME_ARGS (seconds));
6483 /* we need to start playback without clipping from the position reported by
6485 segment->start = seconds;
6486 segment->position = seconds;
6488 if (therange->max.type == GST_RTSP_TIME_NOW)
6490 else if (therange->max.type == GST_RTSP_TIME_END)
6493 seconds = therange->max.seconds * GST_SECOND;
6495 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6496 GST_TIME_ARGS (seconds));
6498 /* live (WMS) server might send overflowed large max as its idea of infinity,
6499 * compensate to prevent problems later on */
6500 if (seconds != -1 && seconds < 0) {
6502 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6505 /* live (WMS) might send min == max, which is not worth recording */
6506 if (segment->duration == -1 && seconds == segment->start)
6509 /* don't change duration with unknown value, we might have a valid value
6510 * there that we want to keep. */
6512 segment->duration = seconds;
6517 /* Parse clock profived by the server with following syntax:
6519 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6522 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6524 gboolean res = FALSE;
6526 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6527 gchar **fields = NULL, **parts = NULL;
6528 gchar *remote_ip, *str;
6530 GstClockTime base_time;
6533 fields = g_strsplit (gstclock, " ", 0);
6535 /* wrapped clock, not very interesting for now */
6536 if (fields[1] == NULL)
6539 /* remote IP address and port */
6540 if ((str = fields[2]) == NULL)
6543 parts = g_strsplit (str, ":", 0);
6545 if ((remote_ip = parts[0]) == NULL)
6548 if ((str = parts[1]) == NULL)
6556 if ((str = fields[3]) == NULL)
6559 base_time = g_ascii_strtoull (str, NULL, 10);
6562 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
6565 if (src->provided_clock)
6566 gst_object_unref (src->provided_clock);
6567 src->provided_clock = netclock;
6569 gst_element_post_message (GST_ELEMENT_CAST (src),
6570 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
6571 src->provided_clock, TRUE));
6575 g_strfreev (fields);
6581 /* must be called with the RTSP state lock */
6582 static GstRTSPResult
6583 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
6589 /* prepare global stream caps properties */
6591 gst_structure_remove_all_fields (src->props);
6593 src->props = gst_structure_new_empty ("RTSPProperties");
6596 gst_sdp_message_dump (sdp);
6598 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
6600 /* let the app inspect and change the SDP */
6601 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6603 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6605 /* parse range for duration reporting. */
6610 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6614 /* keep track of the range and configure it in the segment */
6615 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6619 /* parse clock information. This is GStreamer specific, a server can tell the
6620 * client what clock it is using and wrap that in a network clock. The
6621 * advantage of that is that we can slave to it. */
6623 const gchar *gstclock;
6626 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6627 if (gstclock == NULL)
6630 /* parse the clock and expose it in the provide_clock method */
6631 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6635 /* try to find a global control attribute. Note that a '*' means that we should
6636 * do aggregate control with the current url (so we don't do anything and
6637 * leave the current connection as is) */
6639 const gchar *control;
6642 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6643 if (control == NULL)
6646 /* only take fully qualified urls */
6647 if (g_str_has_prefix (control, "rtsp://"))
6651 g_free (src->conninfo.location);
6652 src->conninfo.location = g_strdup (control);
6653 /* make a connection for this, if there was a connection already, nothing
6655 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6656 GST_ERROR_OBJECT (src, "could not connect");
6659 /* we need to keep the control url separate from the connection url because
6660 * the rules for constructing the media control url need it */
6661 g_free (src->control);
6662 src->control = g_strdup (control);
6665 /* create streams */
6666 n_streams = gst_sdp_message_medias_len (sdp);
6667 for (i = 0; i < n_streams; i++) {
6668 gst_rtspsrc_create_stream (src, sdp, i);
6671 src->state = GST_RTSP_STATE_INIT;
6674 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6677 /* reset our state */
6678 src->need_range = TRUE;
6681 src->state = GST_RTSP_STATE_READY;
6688 GST_ERROR_OBJECT (src, "setup failed");
6689 gst_rtspsrc_cleanup (src);
6694 static GstRTSPResult
6695 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6699 GstRTSPMessage request = { 0 };
6700 GstRTSPMessage response = { 0 };
6703 gchar *respcont = NULL;
6706 src->need_redirect = FALSE;
6708 /* can't continue without a valid url */
6709 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6710 res = GST_RTSP_EINVAL;
6713 src->tried_url_auth = FALSE;
6715 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6716 goto connect_failed;
6718 /* create OPTIONS */
6719 GST_DEBUG_OBJECT (src, "create options...");
6721 gst_rtspsrc_init_request (src, &request, GST_RTSP_OPTIONS,
6722 src->conninfo.url_str);
6724 goto create_request_failed;
6727 GST_DEBUG_OBJECT (src, "send options...");
6730 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6733 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6738 if (!gst_rtspsrc_parse_methods (src, &response))
6741 /* create DESCRIBE */
6742 GST_DEBUG_OBJECT (src, "create describe...");
6744 gst_rtspsrc_init_request (src, &request, GST_RTSP_DESCRIBE,
6745 src->conninfo.url_str);
6747 goto create_request_failed;
6749 /* we only accept SDP for now */
6750 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
6754 GST_DEBUG_OBJECT (src, "send describe...");
6757 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
6760 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6764 /* we only perform redirect for the describe, currently */
6765 if (src->need_redirect) {
6766 /* close connection, we don't have to send a TEARDOWN yet, ignore the
6768 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6770 gst_rtsp_message_unset (&request);
6771 gst_rtsp_message_unset (&response);
6777 /* it could be that the DESCRIBE method was not implemented */
6778 if (!(src->methods & GST_RTSP_DESCRIBE))
6781 /* check if reply is SDP */
6782 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
6784 /* could not be set but since the request returned OK, we assume it
6785 * was SDP, else check it. */
6787 if (g_ascii_strcasecmp (respcont, "application/sdp") != 0)
6788 goto wrong_content_type;
6791 /* get message body and parse as SDP */
6792 gst_rtsp_message_get_body (&response, &data, &size);
6793 if (data == NULL || size == 0)
6796 GST_DEBUG_OBJECT (src, "parse SDP...");
6797 gst_sdp_message_new (sdp);
6798 gst_sdp_message_parse_buffer (data, size, *sdp);
6800 /* clean up any messages */
6801 gst_rtsp_message_unset (&request);
6802 gst_rtsp_message_unset (&response);
6809 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
6810 ("No valid RTSP URL was provided"));
6815 gchar *str = gst_rtsp_strresult (res);
6817 if (res != GST_RTSP_EINTR) {
6818 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6819 ("Failed to connect. (%s)", str));
6821 GST_WARNING_OBJECT (src, "connect interrupted");
6826 create_request_failed:
6828 gchar *str = gst_rtsp_strresult (res);
6830 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6831 ("Could not create request. (%s)", str));
6837 /* Don't post a message - the rtsp_send method will have
6838 * taken care of it because we passed NULL for the response code */
6843 /* error was posted */
6844 res = GST_RTSP_ERROR;
6849 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6850 ("Server does not support SDP, got %s.", respcont));
6851 res = GST_RTSP_ERROR;
6856 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6857 ("Server can not provide an SDP."));
6858 res = GST_RTSP_ERROR;
6863 if (src->conninfo.connection) {
6864 GST_DEBUG_OBJECT (src, "free connection");
6865 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6867 gst_rtsp_message_unset (&request);
6868 gst_rtsp_message_unset (&response);
6873 static GstRTSPResult
6874 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
6879 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
6881 if (src->sdp == NULL) {
6882 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
6886 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
6891 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
6898 GST_WARNING_OBJECT (src, "can't get sdp");
6899 src->open_error = TRUE;
6904 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
6905 src->open_error = TRUE;
6910 static GstRTSPResult
6911 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
6913 GstRTSPMessage request = { 0 };
6914 GstRTSPMessage response = { 0 };
6915 GstRTSPResult res = GST_RTSP_OK;
6917 const gchar *control;
6919 GST_DEBUG_OBJECT (src, "TEARDOWN...");
6921 gst_rtspsrc_set_state (src, GST_STATE_READY);
6923 if (src->state < GST_RTSP_STATE_READY) {
6924 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
6931 /* construct a control url */
6932 control = get_aggregate_control (src);
6934 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
6937 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6938 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6939 const gchar *setup_url;
6940 GstRTSPConnInfo *info;
6942 /* try aggregate control first but do non-aggregate control otherwise */
6944 setup_url = control;
6945 else if ((setup_url = stream->conninfo.location) == NULL)
6948 if (src->conninfo.connection) {
6949 info = &src->conninfo;
6950 } else if (stream->conninfo.connection) {
6951 info = &stream->conninfo;
6955 if (!info->connected)
6960 gst_rtspsrc_init_request (src, &request, GST_RTSP_TEARDOWN, setup_url);
6962 goto create_request_failed;
6965 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
6968 gst_rtspsrc_send (src, info->connection, &request, &response,
6972 /* FIXME, parse result? */
6973 gst_rtsp_message_unset (&request);
6974 gst_rtsp_message_unset (&response);
6977 /* early exit when we did aggregate control */
6983 /* close connections */
6984 GST_DEBUG_OBJECT (src, "closing connection...");
6985 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6986 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6987 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6988 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
6992 gst_rtspsrc_cleanup (src);
6994 src->state = GST_RTSP_STATE_INVALID;
6997 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
7002 create_request_failed:
7004 gchar *str = gst_rtsp_strresult (res);
7006 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7007 ("Could not create request. (%s)", str));
7013 gchar *str = gst_rtsp_strresult (res);
7015 gst_rtsp_message_unset (&request);
7016 if (res != GST_RTSP_EINTR) {
7017 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7018 ("Could not send message. (%s)", str));
7020 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
7027 GST_DEBUG_OBJECT (src,
7028 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
7033 /* RTP-Info is of the format:
7035 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
7037 * rtptime corresponds to the timestamp for the NPT time given in the header
7038 * seqbase corresponds to the next sequence number we received. This number
7039 * indicates the first seqnum after the seek and should be used to discard
7040 * packets that are from before the seek.
7043 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
7048 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
7050 infos = g_strsplit (rtpinfo, ",", 0);
7051 for (i = 0; infos[i]; i++) {
7053 GstRTSPStream *stream;
7057 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
7059 /* init values, types of seqbase and timebase are bigger than needed so we
7060 * can store -1 as uninitialized values */
7065 /* parse url, find stream for url.
7066 * parse seq and rtptime. The seq number should be configured in the rtp
7067 * depayloader or session manager to detect gaps. Same for the rtptime, it
7068 * should be used to create an initial time newsegment. */
7069 fields = g_strsplit (infos[i], ";", 0);
7070 for (j = 0; fields[j]; j++) {
7071 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
7072 /* remove leading whitespace */
7073 fields[j] = g_strchug (fields[j]);
7074 if (g_str_has_prefix (fields[j], "url=")) {
7075 /* get the url and the stream */
7077 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
7078 } else if (g_str_has_prefix (fields[j], "seq=")) {
7079 seqbase = atoi (fields[j] + 4);
7080 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
7081 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
7084 g_strfreev (fields);
7085 /* now we need to store the values for the caps of the stream */
7086 if (stream != NULL) {
7087 GST_DEBUG_OBJECT (src,
7088 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
7089 stream, seqbase, timebase);
7091 /* we have a stream, configure detected params */
7092 stream->seqbase = seqbase;
7093 stream->timebase = timebase;
7102 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
7107 interval = strtoul (rtcp, NULL, 10);
7108 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
7113 interval *= GST_MSECOND;
7115 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7116 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7118 /* already (optionally) retrieved this when configuring manager */
7119 if (stream->session) {
7120 GObject *rtpsession = stream->session;
7122 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
7124 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
7128 /* now it happens that (Xenon) server sending this may also provide bogus
7129 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
7130 * and just use RTP-Info to sync */
7132 GObjectClass *klass;
7134 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
7135 if (g_object_class_find_property (klass, "rtcp-sync")) {
7136 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
7137 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
7143 gst_rtspsrc_get_float (const gchar * dstr)
7145 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7147 /* canonicalise floating point string so we can handle float strings
7148 * in the form "24.930" or "24,930" irrespective of the current locale */
7149 g_strlcpy (s, dstr, sizeof (s));
7150 g_strdelimit (s, ",", '.');
7151 return g_ascii_strtod (s, NULL);
7155 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
7157 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7159 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
7160 g_strlcpy (val_str, "now", sizeof (val_str));
7162 if (segment->position == 0) {
7163 g_strlcpy (val_str, "0", sizeof (val_str));
7165 g_ascii_dtostr (val_str, sizeof (val_str),
7166 ((gdouble) segment->position) / GST_SECOND);
7169 return g_strdup_printf ("npt=%s-", val_str);
7173 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
7177 stream->timebase = -1;
7178 stream->seqbase = -1;
7180 len = stream->ptmap->len;
7181 for (i = 0; i < len; i++) {
7182 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
7185 if (item->caps == NULL)
7188 item->caps = gst_caps_make_writable (item->caps);
7189 s = gst_caps_get_structure (item->caps, 0);
7190 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
7191 if (item->pt == stream->default_pt && stream->udpsrc[0])
7192 g_object_set (stream->udpsrc[0], "caps", item->caps, NULL);
7194 stream->need_caps = TRUE;
7197 static GstRTSPResult
7198 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
7200 GstRTSPResult res = GST_RTSP_OK;
7202 if (src->state < GST_RTSP_STATE_READY) {
7203 res = GST_RTSP_ERROR;
7204 if (src->open_error) {
7205 GST_DEBUG_OBJECT (src, "the stream was in error");
7209 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
7211 if ((res = gst_rtspsrc_open (src, async)) < 0) {
7212 GST_DEBUG_OBJECT (src, "failed to open stream");
7221 static GstRTSPResult
7222 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
7224 GstRTSPMessage request = { 0 };
7225 GstRTSPMessage response = { 0 };
7226 GstRTSPResult res = GST_RTSP_OK;
7230 const gchar *control;
7232 GST_DEBUG_OBJECT (src, "PLAY...");
7234 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7237 if (!(src->methods & GST_RTSP_PLAY))
7240 if (src->state == GST_RTSP_STATE_PLAYING)
7243 if (!src->conninfo.connection || !src->conninfo.connected)
7246 /* send some dummy packets before we activate the receive in the
7248 gst_rtspsrc_send_dummy_packets (src);
7250 /* require new SR packets */
7252 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
7254 /* construct a control url */
7255 control = get_aggregate_control (src);
7257 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7258 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7259 const gchar *setup_url;
7260 GstRTSPConnection *conn;
7262 /* try aggregate control first but do non-aggregate control otherwise */
7264 setup_url = control;
7265 else if ((setup_url = stream->conninfo.location) == NULL)
7268 if (src->conninfo.connection) {
7269 conn = src->conninfo.connection;
7270 } else if (stream->conninfo.connection) {
7271 conn = stream->conninfo.connection;
7277 res = gst_rtspsrc_init_request (src, &request, GST_RTSP_PLAY, setup_url);
7279 goto create_request_failed;
7281 if (src->need_range) {
7282 hval = gen_range_header (src, segment);
7284 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
7286 /* store the newsegment event so it can be sent from the streaming thread. */
7287 src->need_segment = TRUE;
7290 if (segment->rate != 1.0) {
7291 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
7293 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
7295 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
7297 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
7301 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
7303 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7306 /* seek may have silently failed as it is not supported */
7307 if (!(src->methods & GST_RTSP_PLAY)) {
7308 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
7309 /* obviously it is supported as we made it here */
7310 src->methods |= GST_RTSP_PLAY;
7311 src->seekable = FALSE;
7312 /* but there is nothing to parse in the response,
7313 * so convey we have no idea and not to expect anything particular */
7314 clear_rtp_base (src, stream);
7318 /* need to do for all streams */
7319 for (run = src->streams; run; run = g_list_next (run))
7320 clear_rtp_base (src, (GstRTSPStream *) run->data);
7322 /* NOTE the above also disables npt based eos detection */
7323 /* and below forces position to 0,
7324 * which is visible feedback we lost the plot */
7325 segment->start = segment->position = src->last_pos;
7328 gst_rtsp_message_unset (&request);
7330 /* parse RTP npt field. This is the current position in the stream (Normal
7331 * Play Time) and should be put in the NEWSEGMENT position field. */
7332 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
7334 gst_rtspsrc_parse_range (src, hval, segment);
7336 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
7337 segment->rate = 1.0;
7339 /* parse Speed header. This is the intended playback rate of the stream
7340 * and should be put in the NEWSEGMENT rate field. */
7341 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
7342 0) == GST_RTSP_OK) {
7343 segment->rate = gst_rtspsrc_get_float (hval);
7344 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
7345 &hval, 0) == GST_RTSP_OK) {
7346 segment->rate = gst_rtspsrc_get_float (hval);
7349 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
7350 * for the RTP packets. If this is not present, we assume all starts from 0...
7351 * This is info for the RTP session manager that we pass to it in caps. */
7353 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
7354 &hval, hval_idx++) == GST_RTSP_OK)
7355 gst_rtspsrc_parse_rtpinfo (src, hval);
7357 /* some servers indicate RTCP parameters in PLAY response,
7358 * rather than properly in SDP */
7359 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
7360 &hval, 0) == GST_RTSP_OK)
7361 gst_rtspsrc_handle_rtcp_interval (src, hval);
7363 gst_rtsp_message_unset (&response);
7365 /* early exit when we did aggregate control */
7369 /* configure the caps of the streams after we parsed all headers. Only reset
7370 * the manager object when we set a new Range header (we did a seek) */
7371 gst_rtspsrc_configure_caps (src, segment, src->need_range);
7373 /* set to PLAYING after we have configured the caps, otherwise we
7374 * might end up calling request_key (with SRTP) while caps are still
7375 * being configured. */
7376 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
7378 /* set again when needed */
7379 src->need_range = FALSE;
7381 src->running = TRUE;
7382 src->base_time = -1;
7383 src->state = GST_RTSP_STATE_PLAYING;
7386 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
7387 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7388 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7389 stream->discont = TRUE;
7394 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
7401 GST_DEBUG_OBJECT (src, "failed to open stream");
7406 GST_DEBUG_OBJECT (src, "PLAY is not supported");
7411 GST_DEBUG_OBJECT (src, "we were already PLAYING");
7414 create_request_failed:
7416 gchar *str = gst_rtsp_strresult (res);
7418 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7419 ("Could not create request. (%s)", str));
7425 gchar *str = gst_rtsp_strresult (res);
7427 gst_rtsp_message_unset (&request);
7428 if (res != GST_RTSP_EINTR) {
7429 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7430 ("Could not send message. (%s)", str));
7432 GST_WARNING_OBJECT (src, "PLAY interrupted");
7439 static GstRTSPResult
7440 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
7442 GstRTSPResult res = GST_RTSP_OK;
7443 GstRTSPMessage request = { 0 };
7444 GstRTSPMessage response = { 0 };
7446 const gchar *control;
7448 GST_DEBUG_OBJECT (src, "PAUSE...");
7450 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7453 if (!(src->methods & GST_RTSP_PAUSE))
7456 if (src->state == GST_RTSP_STATE_READY)
7459 if (!src->conninfo.connection || !src->conninfo.connected)
7462 /* construct a control url */
7463 control = get_aggregate_control (src);
7465 /* loop over the streams. We might exit the loop early when we could do an
7466 * aggregate control */
7467 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7468 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7469 GstRTSPConnection *conn;
7470 const gchar *setup_url;
7472 /* try aggregate control first but do non-aggregate control otherwise */
7474 setup_url = control;
7475 else if ((setup_url = stream->conninfo.location) == NULL)
7478 if (src->conninfo.connection) {
7479 conn = src->conninfo.connection;
7480 } else if (stream->conninfo.connection) {
7481 conn = stream->conninfo.connection;
7487 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7488 ("Sending PAUSE request"));
7491 gst_rtspsrc_init_request (src, &request, GST_RTSP_PAUSE,
7493 goto create_request_failed;
7495 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7498 gst_rtsp_message_unset (&request);
7499 gst_rtsp_message_unset (&response);
7501 /* exit early when we did agregate control */
7506 /* change element states now */
7507 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7510 src->state = GST_RTSP_STATE_READY;
7514 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7521 GST_DEBUG_OBJECT (src, "failed to open stream");
7526 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7531 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7534 create_request_failed:
7536 gchar *str = gst_rtsp_strresult (res);
7538 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7539 ("Could not create request. (%s)", str));
7545 gchar *str = gst_rtsp_strresult (res);
7547 gst_rtsp_message_unset (&request);
7548 if (res != GST_RTSP_EINTR) {
7549 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7550 ("Could not send message. (%s)", str));
7552 GST_WARNING_OBJECT (src, "PAUSE interrupted");
7560 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
7562 GstRTSPSrc *rtspsrc;
7564 rtspsrc = GST_RTSPSRC (bin);
7566 switch (GST_MESSAGE_TYPE (message)) {
7567 case GST_MESSAGE_EOS:
7568 gst_message_unref (message);
7570 case GST_MESSAGE_ELEMENT:
7572 const GstStructure *s = gst_message_get_structure (message);
7574 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
7575 gboolean ignore_timeout;
7577 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
7579 GST_OBJECT_LOCK (rtspsrc);
7580 ignore_timeout = rtspsrc->ignore_timeout;
7581 rtspsrc->ignore_timeout = TRUE;
7582 GST_OBJECT_UNLOCK (rtspsrc);
7584 /* we only act on the first udp timeout message, others are irrelevant
7585 * and can be ignored. */
7586 if (!ignore_timeout)
7587 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
7589 gst_message_unref (message);
7592 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7595 case GST_MESSAGE_ERROR:
7598 GstRTSPStream *stream;
7601 udpsrc = GST_MESSAGE_SRC (message);
7603 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7604 GST_ELEMENT_NAME (udpsrc));
7606 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7610 /* we ignore the RTCP udpsrc */
7611 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7614 /* if we get error messages from the udp sources, that's not a problem as
7615 * long as not all of them error out. We also don't really know what the
7616 * problem is, the message does not give enough detail... */
7617 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7618 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7619 if (ret != GST_FLOW_OK)
7623 gst_message_unref (message);
7627 /* fatal but not our message, forward */
7628 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7633 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7639 /* the thread where everything happens */
7641 gst_rtspsrc_thread (GstRTSPSrc * src)
7645 GST_OBJECT_LOCK (src);
7646 cmd = src->pending_cmd;
7647 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7648 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7649 src->pending_cmd = CMD_LOOP;
7651 src->pending_cmd = CMD_WAIT;
7652 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
7654 /* we got the message command, so ensure communication is possible again */
7655 gst_rtspsrc_connection_flush (src, FALSE);
7657 src->busy_cmd = cmd;
7658 GST_OBJECT_UNLOCK (src);
7662 gst_rtspsrc_open (src, TRUE);
7665 gst_rtspsrc_play (src, &src->segment, TRUE);
7668 gst_rtspsrc_pause (src, TRUE);
7671 gst_rtspsrc_close (src, TRUE, FALSE);
7674 gst_rtspsrc_loop (src);
7677 gst_rtspsrc_reconnect (src, FALSE);
7683 GST_OBJECT_LOCK (src);
7684 /* and go back to sleep */
7685 if (src->pending_cmd == CMD_WAIT) {
7687 gst_task_pause (src->task);
7690 src->busy_cmd = CMD_WAIT;
7691 GST_OBJECT_UNLOCK (src);
7695 gst_rtspsrc_start (GstRTSPSrc * src)
7697 GST_DEBUG_OBJECT (src, "starting");
7699 GST_OBJECT_LOCK (src);
7701 src->pending_cmd = CMD_WAIT;
7703 if (src->task == NULL) {
7704 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7705 if (src->task == NULL)
7708 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7710 GST_OBJECT_UNLOCK (src);
7717 GST_OBJECT_UNLOCK (src);
7718 GST_ERROR_OBJECT (src, "failed to create task");
7724 gst_rtspsrc_stop (GstRTSPSrc * src)
7728 GST_DEBUG_OBJECT (src, "stopping");
7730 /* also cancels pending task */
7731 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7733 GST_OBJECT_LOCK (src);
7734 if ((task = src->task)) {
7736 GST_OBJECT_UNLOCK (src);
7738 gst_task_stop (task);
7740 /* make sure it is not running */
7741 GST_RTSP_STREAM_LOCK (src);
7742 GST_RTSP_STREAM_UNLOCK (src);
7744 /* now wait for the task to finish */
7745 gst_task_join (task);
7747 /* and free the task */
7748 gst_object_unref (GST_OBJECT (task));
7750 GST_OBJECT_LOCK (src);
7752 GST_OBJECT_UNLOCK (src);
7754 /* ensure synchronously all is closed and clean */
7755 gst_rtspsrc_close (src, FALSE, TRUE);
7760 static GstStateChangeReturn
7761 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
7763 GstRTSPSrc *rtspsrc;
7764 GstStateChangeReturn ret;
7766 rtspsrc = GST_RTSPSRC (element);
7768 switch (transition) {
7769 case GST_STATE_CHANGE_NULL_TO_READY:
7770 if (!gst_rtspsrc_start (rtspsrc))
7773 case GST_STATE_CHANGE_READY_TO_PAUSED:
7774 /* init some state */
7775 rtspsrc->cur_protocols = rtspsrc->protocols;
7776 /* first attempt, don't ignore timeouts */
7777 rtspsrc->ignore_timeout = FALSE;
7778 rtspsrc->open_error = FALSE;
7779 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
7781 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7782 set_manager_buffer_mode (rtspsrc);
7784 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7785 /* unblock the tcp tasks and make the loop waiting */
7786 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
7787 /* make sure it is waiting before we send PAUSE or PLAY below */
7788 GST_RTSP_STREAM_LOCK (rtspsrc);
7789 GST_RTSP_STREAM_UNLOCK (rtspsrc);
7792 case GST_STATE_CHANGE_PAUSED_TO_READY:
7798 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
7799 if (ret == GST_STATE_CHANGE_FAILURE)
7802 switch (transition) {
7803 case GST_STATE_CHANGE_NULL_TO_READY:
7804 ret = GST_STATE_CHANGE_SUCCESS;
7806 case GST_STATE_CHANGE_READY_TO_PAUSED:
7807 ret = GST_STATE_CHANGE_NO_PREROLL;
7809 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7810 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
7811 ret = GST_STATE_CHANGE_SUCCESS;
7813 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7814 /* send pause request and keep the idle task around */
7815 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
7816 ret = GST_STATE_CHANGE_NO_PREROLL;
7818 case GST_STATE_CHANGE_PAUSED_TO_READY:
7819 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
7820 ret = GST_STATE_CHANGE_SUCCESS;
7822 case GST_STATE_CHANGE_READY_TO_NULL:
7823 gst_rtspsrc_stop (rtspsrc);
7824 ret = GST_STATE_CHANGE_SUCCESS;
7827 /* Otherwise it's success, we don't want to return spurious
7828 * NO_PREROLL or ASYNC from internal elements as we care for
7829 * state changes ourselves here
7831 * This is to catch PAUSED->PAUSED and PLAYING->PLAYING transitions.
7833 if (GST_STATE_TRANSITION_NEXT (transition) == GST_STATE_PAUSED)
7834 ret = GST_STATE_CHANGE_NO_PREROLL;
7836 ret = GST_STATE_CHANGE_SUCCESS;
7845 GST_DEBUG_OBJECT (rtspsrc, "start failed");
7846 return GST_STATE_CHANGE_FAILURE;
7851 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
7854 GstRTSPSrc *rtspsrc;
7856 rtspsrc = GST_RTSPSRC (element);
7858 if (GST_EVENT_IS_DOWNSTREAM (event)) {
7859 res = gst_rtspsrc_push_event (rtspsrc, event);
7861 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
7868 /*** GSTURIHANDLER INTERFACE *************************************************/
7871 gst_rtspsrc_uri_get_type (GType type)
7876 static const gchar *const *
7877 gst_rtspsrc_uri_get_protocols (GType type)
7879 static const gchar *protocols[] =
7880 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
7881 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
7888 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
7890 GstRTSPSrc *src = GST_RTSPSRC (handler);
7892 /* FIXME: make thread-safe */
7893 return g_strdup (src->conninfo.location);
7897 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
7903 GstRTSPUrl *newurl = NULL;
7904 GstSDPMessage *sdp = NULL;
7906 src = GST_RTSPSRC (handler);
7908 /* same URI, we're fine */
7909 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
7912 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
7913 sres = gst_sdp_message_new (&sdp);
7917 GST_DEBUG_OBJECT (src, "parsing SDP message");
7918 sres = gst_sdp_message_parse_uri (uri, sdp);
7923 GST_DEBUG_OBJECT (src, "parsing URI");
7924 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
7928 /* if worked, free previous and store new url object along with the original
7930 GST_DEBUG_OBJECT (src, "configuring URI");
7931 g_free (src->conninfo.location);
7932 src->conninfo.location = g_strdup (uri);
7933 gst_rtsp_url_free (src->conninfo.url);
7934 src->conninfo.url = newurl;
7935 g_free (src->conninfo.url_str);
7937 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
7939 src->conninfo.url_str = NULL;
7942 gst_sdp_message_free (src->sdp);
7944 src->from_sdp = sdp != NULL;
7946 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
7947 GST_DEBUG_OBJECT (src, "request uri is: %s",
7948 GST_STR_NULL (src->conninfo.url_str));
7955 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
7960 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
7961 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7962 "Could not create SDP");
7967 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
7968 GST_STR_NULL (uri));
7969 gst_sdp_message_free (sdp);
7970 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7976 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
7977 GST_STR_NULL (uri), res);
7978 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7979 "Invalid RTSP URI");
7985 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
7987 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
7989 iface->get_type = gst_rtspsrc_uri_get_type;
7990 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
7991 iface->get_uri = gst_rtspsrc_uri_get_uri;
7992 iface->set_uri = gst_rtspsrc_uri_set_uri;