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 #define RTSP_SRC_RESPONSE_ERROR(src, response_msg, err_cat, err_code, error_message) \
293 GST_ELEMENT_ERROR_WITH_DETAILS((src), err_cat, err_code, ("%s", error_message), \
294 ("%s (%d)", (response_msg)->type_data.response.reason, (response_msg)->type_data.response.code), \
295 ("rtsp-status-code", G_TYPE_UINT, (response_msg)->type_data.response.code, \
296 "rtsp-status-reason", G_TYPE_STRING, GST_STR_NULL((response_msg)->type_data.response.reason), NULL)); \
299 static void gst_rtspsrc_finalize (GObject * object);
301 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
302 const GValue * value, GParamSpec * pspec);
303 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
304 GValue * value, GParamSpec * pspec);
306 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
308 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
309 gpointer iface_data);
311 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
312 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
314 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
315 GstStateChange transition);
316 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
317 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
319 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
320 GstRTSPMessage * response);
322 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
324 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
325 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
327 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
328 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
330 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
331 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
332 gboolean only_close);
334 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
335 const gchar * uri, GError ** error);
336 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
338 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
339 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
340 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
341 GstRTSPStream * stream, GstEvent * event);
342 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
343 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
344 static GstRTSPResult gst_rtsp_conninfo_close (GstRTSPSrc * src,
345 GstRTSPConnInfo * info, gboolean free);
353 /* commands we send to out loop to notify it of events */
354 #define CMD_OPEN (1 << 0)
355 #define CMD_PLAY (1 << 1)
356 #define CMD_PAUSE (1 << 2)
357 #define CMD_CLOSE (1 << 3)
358 #define CMD_WAIT (1 << 4)
359 #define CMD_RECONNECT (1 << 5)
360 #define CMD_LOOP (1 << 6)
362 /* mask for all commands */
363 #define CMD_ALL ((CMD_LOOP << 1) - 1)
365 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
367 gchar *__txt = _gst_element_error_printf text; \
368 gst_element_post_message (GST_ELEMENT_CAST (el), \
369 gst_message_new_progress (GST_OBJECT_CAST (el), \
370 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
374 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
376 #define gst_rtspsrc_parent_class parent_class
377 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
378 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
380 #ifndef GST_DISABLE_GST_DEBUG
381 static inline const char *
382 cmd_to_string (guint cmd)
406 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
408 GST_DEBUG_OBJECT (src, "default handler");
413 select_stream_accum (GSignalInvocationHint * ihint,
414 GValue * return_accu, const GValue * handler_return, gpointer data)
418 myboolean = g_value_get_boolean (handler_return);
419 GST_DEBUG ("accum %d", myboolean);
420 g_value_set_boolean (return_accu, myboolean);
422 /* stop emission if FALSE */
427 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
429 GObjectClass *gobject_class;
430 GstElementClass *gstelement_class;
431 GstBinClass *gstbin_class;
433 gobject_class = (GObjectClass *) klass;
434 gstelement_class = (GstElementClass *) klass;
435 gstbin_class = (GstBinClass *) klass;
437 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
439 gobject_class->set_property = gst_rtspsrc_set_property;
440 gobject_class->get_property = gst_rtspsrc_get_property;
442 gobject_class->finalize = gst_rtspsrc_finalize;
444 g_object_class_install_property (gobject_class, PROP_LOCATION,
445 g_param_spec_string ("location", "RTSP Location",
446 "Location of the RTSP url to read",
447 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
449 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
450 g_param_spec_flags ("protocols", "Protocols",
451 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
452 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
454 g_object_class_install_property (gobject_class, PROP_DEBUG,
455 g_param_spec_boolean ("debug", "Debug",
456 "Dump request and response messages to stdout",
457 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
459 g_object_class_install_property (gobject_class, PROP_RETRY,
460 g_param_spec_uint ("retry", "Retry",
461 "Max number of retries when allocating RTP ports.",
462 0, G_MAXUINT16, DEFAULT_RETRY,
463 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
465 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
466 g_param_spec_uint64 ("timeout", "Timeout",
467 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
468 0, G_MAXUINT64, DEFAULT_TIMEOUT,
469 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
471 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
472 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
473 "Fail after timeout microseconds on TCP connections (0 = disabled)",
474 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
475 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
477 g_object_class_install_property (gobject_class, PROP_LATENCY,
478 g_param_spec_uint ("latency", "Buffer latency in ms",
479 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
480 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
482 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
483 g_param_spec_boolean ("drop-on-latency",
484 "Drop buffers when maximum latency is reached",
485 "Tells the jitterbuffer to never exceed the given latency in size",
486 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
488 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
489 g_param_spec_uint64 ("connection-speed", "Connection Speed",
490 "Network connection speed in kbps (0 = unknown)",
491 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
492 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
494 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
495 g_param_spec_enum ("nat-method", "NAT Method",
496 "Method to use for traversing firewalls and NAT",
497 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
498 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
501 * GstRTSPSrc:do-rtcp:
503 * Enable RTCP support. Some old server don't like RTCP and then this property
504 * needs to be set to FALSE.
506 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
507 g_param_spec_boolean ("do-rtcp", "Do RTCP",
508 "Send RTCP packets, disable for old incompatible server.",
509 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
512 * GstRTSPSrc:do-rtsp-keep-alive:
514 * Enable RTSP keep alive support. Some old server don't like RTSP
515 * keep alive and then this property needs to be set to FALSE.
517 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
518 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
519 "Send RTSP keep alive packets, disable for old incompatible server.",
520 DEFAULT_DO_RTSP_KEEP_ALIVE,
521 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
526 * Set the proxy parameters. This has to be a string of the format
527 * [http://][user:passwd@]host[:port].
529 g_object_class_install_property (gobject_class, PROP_PROXY,
530 g_param_spec_string ("proxy", "Proxy",
531 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
532 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
534 * GstRTSPSrc:proxy-id:
536 * Sets the proxy URI user id for authentication. If the URI set via the
537 * "proxy" property contains a user-id already, that will take precedence.
541 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
542 g_param_spec_string ("proxy-id", "proxy-id",
543 "HTTP proxy URI user id for authentication", "",
544 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
546 * GstRTSPSrc:proxy-pw:
548 * Sets the proxy URI password for authentication. If the URI set via the
549 * "proxy" property contains a password already, that will take precedence.
553 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
554 g_param_spec_string ("proxy-pw", "proxy-pw",
555 "HTTP proxy URI user password for authentication", "",
556 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
559 * GstRTSPSrc:rtp-blocksize:
561 * RTP package size to suggest to server.
563 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
564 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
565 "RTP package size to suggest to server (0 = disabled)",
566 0, 65536, DEFAULT_RTP_BLOCKSIZE,
567 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
569 g_object_class_install_property (gobject_class,
571 g_param_spec_string ("user-id", "user-id",
572 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
573 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
574 g_object_class_install_property (gobject_class, PROP_USER_PW,
575 g_param_spec_string ("user-pw", "user-pw",
576 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
577 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
580 * GstRTSPSrc:buffer-mode:
582 * Control the buffering and timestamping mode used by the jitterbuffer.
584 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
585 g_param_spec_enum ("buffer-mode", "Buffer Mode",
586 "Control the buffering algorithm in use",
587 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
588 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
591 * GstRTSPSrc:port-range:
593 * Configure the client port numbers that can be used to recieve RTP and
596 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
597 g_param_spec_string ("port-range", "Port range",
598 "Client port range that can be used to receive RTP and RTCP data, "
599 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
600 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
603 * GstRTSPSrc:udp-buffer-size:
605 * Size of the kernel UDP receive buffer in bytes.
607 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
608 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
609 "Size of the kernel UDP receive buffer in bytes, 0=default",
610 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
611 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
614 * GstRTSPSrc:short-header:
616 * Only send the basic RTSP headers for broken encoders.
618 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
619 g_param_spec_boolean ("short-header", "Short Header",
620 "Only send the basic RTSP headers for broken encoders",
621 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
623 g_object_class_install_property (gobject_class, PROP_PROBATION,
624 g_param_spec_uint ("probation", "Number of probations",
625 "Consecutive packet sequence numbers to accept the source",
626 0, G_MAXUINT, DEFAULT_PROBATION,
627 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
629 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
630 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
631 "Reconnect to the server if RTSP connection is closed when doing UDP",
632 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
634 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
635 g_param_spec_string ("multicast-iface", "Multicast Interface",
636 "The network interface on which to join the multicast group",
637 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
639 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
640 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
641 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
642 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
644 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
645 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
646 "Use the pipeline running-time to set the NTP time in the RTCP SR messages"
647 "(DEPRECATED: Use ntp-time-source property)",
648 DEFAULT_USE_PIPELINE_CLOCK,
649 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
651 g_object_class_install_property (gobject_class, PROP_SDES,
652 g_param_spec_boxed ("sdes", "SDES",
653 "The SDES items of this session",
654 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
657 * GstRTSPSrc::tls-validation-flags:
659 * TLS certificate validation flags used to validate server
664 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
665 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
666 "TLS certificate validation flags used to validate the server certificate",
667 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
668 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
671 * GstRTSPSrc::tls-database:
673 * TLS database with anchor certificate authorities used to validate
674 * the server certificate.
678 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
679 g_param_spec_object ("tls-database", "TLS database",
680 "TLS database with anchor certificate authorities used to validate the server certificate",
681 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
684 * GstRTSPSrc::tls-interaction:
686 * A #GTlsInteraction object to be used when the connection or certificate
687 * database need to interact with the user. This will be used to prompt the
688 * user for passwords where necessary.
692 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
693 g_param_spec_object ("tls-interaction", "TLS interaction",
694 "A GTlsInteraction object to promt the user for password or certificate",
695 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
698 * GstRTSPSrc::do-retransmission:
700 * Attempt to ask the server to retransmit lost packets according to RFC4588.
702 * Note: currently only works with SSRC-multiplexed retransmission streams
706 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
707 g_param_spec_boolean ("do-retransmission", "Retransmission",
708 "Ask the server to retransmit lost packets",
709 DEFAULT_DO_RETRANSMISSION,
710 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
713 * GstRTSPSrc::ntp-time-source:
715 * allows to select the time source that should be used
716 * for the NTP time in RTCP packets
720 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
721 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
722 "NTP time source for RTCP packets",
723 GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE, DEFAULT_NTP_TIME_SOURCE,
724 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
727 * GstRTSPSrc::user-agent:
729 * The string to set in the User-Agent header.
733 g_object_class_install_property (gobject_class, PROP_USER_AGENT,
734 g_param_spec_string ("user-agent", "User Agent",
735 "The User-Agent string to send to the server",
736 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
738 g_object_class_install_property (gobject_class, PROP_MAX_RTCP_RTP_TIME_DIFF,
739 g_param_spec_int ("max-rtcp-rtp-time-diff", "Max RTCP RTP Time Diff",
740 "Maximum amount of time in ms that the RTP time in RTCP SRs "
741 "is allowed to be ahead (-1 disabled)", -1, G_MAXINT,
742 DEFAULT_MAX_RTCP_RTP_TIME_DIFF,
743 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
745 g_object_class_install_property (gobject_class, PROP_RFC7273_SYNC,
746 g_param_spec_boolean ("rfc7273-sync", "Sync on RFC7273 clock",
747 "Synchronize received streams to the RFC7273 clock "
748 "(requires clock and offset to be provided)", DEFAULT_RFC7273_SYNC,
749 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
752 * GstRTSPSrc::handle-request:
753 * @rtspsrc: a #GstRTSPSrc
754 * @request: a #GstRTSPMessage
755 * @response: a #GstRTSPMessage
757 * Handle a server request in @request and prepare @response.
759 * This signal is called from the streaming thread, you should therefore not
760 * do any state changes on @rtspsrc because this might deadlock. If you want
761 * to modify the state as a result of this signal, post a
762 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
767 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
768 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
769 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
770 G_TYPE_POINTER, G_TYPE_POINTER);
773 * GstRTSPSrc::on-sdp:
774 * @rtspsrc: a #GstRTSPSrc
775 * @sdp: a #GstSDPMessage
777 * Emited when the client has retrieved the SDP and before it configures the
778 * streams in the SDP. @sdp can be inspected and modified.
780 * This signal is called from the streaming thread, you should therefore not
781 * do any state changes on @rtspsrc because this might deadlock. If you want
782 * to modify the state as a result of this signal, post a
783 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
788 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
789 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
790 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
791 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
794 * GstRTSPSrc::select-stream:
795 * @rtspsrc: a #GstRTSPSrc
796 * @num: the stream number
797 * @caps: the stream caps
799 * Emited before the client decides to configure the stream @num with
802 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
807 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
808 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
809 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
810 (GCallback) default_select_stream, select_stream_accum, NULL,
811 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
814 * GstRTSPSrc::new-manager:
815 * @rtspsrc: a #GstRTSPSrc
816 * @manager: a #GstElement
818 * Emited after a new manager (like rtpbin) was created and the default
819 * properties were configured.
823 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
824 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
825 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
826 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
829 * GstRTSPSrc::request-rtcp-key:
830 * @rtspsrc: a #GstRTSPSrc
831 * @num: the stream number
833 * Signal emited to get the crypto parameters relevant to the RTCP
834 * stream. User should provide the key and the RTCP encryption ciphers
835 * and authentication, and return them wrapped in a GstCaps.
839 gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY] =
840 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
841 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
843 gstelement_class->send_event = gst_rtspsrc_send_event;
844 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
845 gstelement_class->change_state = gst_rtspsrc_change_state;
847 gst_element_class_add_static_pad_template (gstelement_class, &rtptemplate);
849 gst_element_class_set_static_metadata (gstelement_class,
850 "RTSP packet receiver", "Source/Network",
851 "Receive data over the network via RTSP (RFC 2326)",
852 "Wim Taymans <wim@fluendo.com>, "
853 "Thijs Vermeir <thijs.vermeir@barco.com>, "
854 "Lutz Mueller <lutz@topfrose.de>");
856 gstbin_class->handle_message = gst_rtspsrc_handle_message;
858 gst_rtsp_ext_list_init ();
862 gst_rtspsrc_init (GstRTSPSrc * src)
864 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
865 src->protocols = DEFAULT_PROTOCOLS;
866 src->debug = DEFAULT_DEBUG;
867 src->retry = DEFAULT_RETRY;
868 src->udp_timeout = DEFAULT_TIMEOUT;
869 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
870 src->latency = DEFAULT_LATENCY_MS;
871 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
872 src->connection_speed = DEFAULT_CONNECTION_SPEED;
873 src->nat_method = DEFAULT_NAT_METHOD;
874 src->do_rtcp = DEFAULT_DO_RTCP;
875 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
876 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
877 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
878 src->user_id = g_strdup (DEFAULT_USER_ID);
879 src->user_pw = g_strdup (DEFAULT_USER_PW);
880 src->buffer_mode = DEFAULT_BUFFER_MODE;
881 src->client_port_range.min = 0;
882 src->client_port_range.max = 0;
883 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
884 src->short_header = DEFAULT_SHORT_HEADER;
885 src->probation = DEFAULT_PROBATION;
886 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
887 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
888 src->ntp_sync = DEFAULT_NTP_SYNC;
889 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
891 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
892 src->tls_database = DEFAULT_TLS_DATABASE;
893 src->tls_interaction = DEFAULT_TLS_INTERACTION;
894 src->do_retransmission = DEFAULT_DO_RETRANSMISSION;
895 src->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
896 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
897 src->max_rtcp_rtp_time_diff = DEFAULT_MAX_RTCP_RTP_TIME_DIFF;
898 src->rfc7273_sync = DEFAULT_RFC7273_SYNC;
900 /* get a list of all extensions */
901 src->extensions = gst_rtsp_ext_list_get ();
903 /* connect to send signal */
904 gst_rtsp_ext_list_connect (src->extensions, "send",
905 (GCallback) gst_rtspsrc_send_cb, src);
907 /* protects the streaming thread in interleaved mode or the polling
908 * thread in UDP mode. */
909 g_rec_mutex_init (&src->stream_rec_lock);
911 /* protects our state changes from multiple invocations */
912 g_rec_mutex_init (&src->state_rec_lock);
914 src->state = GST_RTSP_STATE_INVALID;
916 g_mutex_init (&src->conninfo.send_lock);
917 g_mutex_init (&src->conninfo.recv_lock);
919 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
920 gst_bin_set_suppressed_flags (GST_BIN (src),
921 GST_ELEMENT_FLAG_SOURCE | GST_ELEMENT_FLAG_SINK);
925 gst_rtspsrc_finalize (GObject * object)
929 rtspsrc = GST_RTSPSRC (object);
931 gst_rtsp_ext_list_free (rtspsrc->extensions);
932 g_free (rtspsrc->conninfo.location);
933 gst_rtsp_url_free (rtspsrc->conninfo.url);
934 g_free (rtspsrc->conninfo.url_str);
935 g_free (rtspsrc->user_id);
936 g_free (rtspsrc->user_pw);
937 g_free (rtspsrc->multi_iface);
938 g_free (rtspsrc->user_agent);
941 gst_sdp_message_free (rtspsrc->sdp);
944 if (rtspsrc->provided_clock)
945 gst_object_unref (rtspsrc->provided_clock);
948 gst_structure_free (rtspsrc->sdes);
950 if (rtspsrc->tls_database)
951 g_object_unref (rtspsrc->tls_database);
953 if (rtspsrc->tls_interaction)
954 g_object_unref (rtspsrc->tls_interaction);
957 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
958 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
960 g_mutex_clear (&rtspsrc->conninfo.send_lock);
961 g_mutex_clear (&rtspsrc->conninfo.recv_lock);
963 G_OBJECT_CLASS (parent_class)->finalize (object);
967 gst_rtspsrc_provide_clock (GstElement * element)
969 GstRTSPSrc *src = GST_RTSPSRC (element);
972 if ((clock = src->provided_clock) != NULL)
973 return gst_object_ref (clock);
975 return GST_ELEMENT_CLASS (parent_class)->provide_clock (element);
978 /* a proxy string of the format [user:passwd@]host[:port] */
980 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
984 g_free (rtsp->proxy_user);
985 rtsp->proxy_user = NULL;
986 g_free (rtsp->proxy_passwd);
987 rtsp->proxy_passwd = NULL;
988 g_free (rtsp->proxy_host);
989 rtsp->proxy_host = NULL;
990 rtsp->proxy_port = 0;
997 /* we allow http:// in front but ignore it */
998 if (g_str_has_prefix (p, "http://"))
1001 at = strchr (p, '@');
1003 /* look for user:passwd */
1004 col = strchr (proxy, ':');
1005 if (col == NULL || col > at)
1008 rtsp->proxy_user = g_strndup (p, col - p);
1010 rtsp->proxy_passwd = g_strndup (col, at - col);
1015 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
1016 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
1017 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
1018 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
1019 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
1020 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
1021 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
1024 col = strchr (p, ':');
1027 /* everything before the colon is the hostname */
1028 rtsp->proxy_host = g_strndup (p, col - p);
1030 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
1032 rtsp->proxy_host = g_strdup (p);
1033 rtsp->proxy_port = 8080;
1039 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
1041 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
1042 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
1045 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
1047 rtspsrc->ptcp_timeout = NULL;
1051 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
1054 GstRTSPSrc *rtspsrc;
1056 rtspsrc = GST_RTSPSRC (object);
1060 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
1061 g_value_get_string (value), NULL);
1063 case PROP_PROTOCOLS:
1064 rtspsrc->protocols = g_value_get_flags (value);
1067 rtspsrc->debug = g_value_get_boolean (value);
1070 rtspsrc->retry = g_value_get_uint (value);
1073 rtspsrc->udp_timeout = g_value_get_uint64 (value);
1075 case PROP_TCP_TIMEOUT:
1076 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
1079 rtspsrc->latency = g_value_get_uint (value);
1081 case PROP_DROP_ON_LATENCY:
1082 rtspsrc->drop_on_latency = g_value_get_boolean (value);
1084 case PROP_CONNECTION_SPEED:
1085 rtspsrc->connection_speed = g_value_get_uint64 (value);
1087 case PROP_NAT_METHOD:
1088 rtspsrc->nat_method = g_value_get_enum (value);
1091 rtspsrc->do_rtcp = g_value_get_boolean (value);
1093 case PROP_DO_RTSP_KEEP_ALIVE:
1094 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
1097 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
1100 g_free (rtspsrc->prop_proxy_id);
1101 rtspsrc->prop_proxy_id = g_value_dup_string (value);
1104 g_free (rtspsrc->prop_proxy_pw);
1105 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
1107 case PROP_RTP_BLOCKSIZE:
1108 rtspsrc->rtp_blocksize = g_value_get_uint (value);
1111 g_free (rtspsrc->user_id);
1112 rtspsrc->user_id = g_value_dup_string (value);
1115 g_free (rtspsrc->user_pw);
1116 rtspsrc->user_pw = g_value_dup_string (value);
1118 case PROP_BUFFER_MODE:
1119 rtspsrc->buffer_mode = g_value_get_enum (value);
1121 case PROP_PORT_RANGE:
1125 str = g_value_get_string (value);
1126 if (sscanf (str, "%u-%u", &rtspsrc->client_port_range.min,
1127 &rtspsrc->client_port_range.max) != 2) {
1128 rtspsrc->client_port_range.min = 0;
1129 rtspsrc->client_port_range.max = 0;
1133 case PROP_UDP_BUFFER_SIZE:
1134 rtspsrc->udp_buffer_size = g_value_get_int (value);
1136 case PROP_SHORT_HEADER:
1137 rtspsrc->short_header = g_value_get_boolean (value);
1139 case PROP_PROBATION:
1140 rtspsrc->probation = g_value_get_uint (value);
1142 case PROP_UDP_RECONNECT:
1143 rtspsrc->udp_reconnect = g_value_get_boolean (value);
1145 case PROP_MULTICAST_IFACE:
1146 g_free (rtspsrc->multi_iface);
1148 if (g_value_get_string (value) == NULL)
1149 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1151 rtspsrc->multi_iface = g_value_dup_string (value);
1154 rtspsrc->ntp_sync = g_value_get_boolean (value);
1156 case PROP_USE_PIPELINE_CLOCK:
1157 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
1160 rtspsrc->sdes = g_value_dup_boxed (value);
1162 case PROP_TLS_VALIDATION_FLAGS:
1163 rtspsrc->tls_validation_flags = g_value_get_flags (value);
1165 case PROP_TLS_DATABASE:
1166 g_clear_object (&rtspsrc->tls_database);
1167 rtspsrc->tls_database = g_value_dup_object (value);
1169 case PROP_TLS_INTERACTION:
1170 g_clear_object (&rtspsrc->tls_interaction);
1171 rtspsrc->tls_interaction = g_value_dup_object (value);
1173 case PROP_DO_RETRANSMISSION:
1174 rtspsrc->do_retransmission = g_value_get_boolean (value);
1176 case PROP_NTP_TIME_SOURCE:
1177 rtspsrc->ntp_time_source = g_value_get_enum (value);
1179 case PROP_USER_AGENT:
1180 g_free (rtspsrc->user_agent);
1181 rtspsrc->user_agent = g_value_dup_string (value);
1183 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1184 rtspsrc->max_rtcp_rtp_time_diff = g_value_get_int (value);
1186 case PROP_RFC7273_SYNC:
1187 rtspsrc->rfc7273_sync = g_value_get_boolean (value);
1190 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1196 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1199 GstRTSPSrc *rtspsrc;
1201 rtspsrc = GST_RTSPSRC (object);
1205 g_value_set_string (value, rtspsrc->conninfo.location);
1207 case PROP_PROTOCOLS:
1208 g_value_set_flags (value, rtspsrc->protocols);
1211 g_value_set_boolean (value, rtspsrc->debug);
1214 g_value_set_uint (value, rtspsrc->retry);
1217 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1219 case PROP_TCP_TIMEOUT:
1223 timeout = ((guint64) rtspsrc->tcp_timeout.tv_sec) * G_USEC_PER_SEC +
1224 rtspsrc->tcp_timeout.tv_usec;
1225 g_value_set_uint64 (value, timeout);
1229 g_value_set_uint (value, rtspsrc->latency);
1231 case PROP_DROP_ON_LATENCY:
1232 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1234 case PROP_CONNECTION_SPEED:
1235 g_value_set_uint64 (value, rtspsrc->connection_speed);
1237 case PROP_NAT_METHOD:
1238 g_value_set_enum (value, rtspsrc->nat_method);
1241 g_value_set_boolean (value, rtspsrc->do_rtcp);
1243 case PROP_DO_RTSP_KEEP_ALIVE:
1244 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1250 if (rtspsrc->proxy_host) {
1252 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1256 g_value_take_string (value, str);
1260 g_value_set_string (value, rtspsrc->prop_proxy_id);
1263 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1265 case PROP_RTP_BLOCKSIZE:
1266 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1269 g_value_set_string (value, rtspsrc->user_id);
1272 g_value_set_string (value, rtspsrc->user_pw);
1274 case PROP_BUFFER_MODE:
1275 g_value_set_enum (value, rtspsrc->buffer_mode);
1277 case PROP_PORT_RANGE:
1281 if (rtspsrc->client_port_range.min != 0) {
1282 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1283 rtspsrc->client_port_range.max);
1287 g_value_take_string (value, str);
1290 case PROP_UDP_BUFFER_SIZE:
1291 g_value_set_int (value, rtspsrc->udp_buffer_size);
1293 case PROP_SHORT_HEADER:
1294 g_value_set_boolean (value, rtspsrc->short_header);
1296 case PROP_PROBATION:
1297 g_value_set_uint (value, rtspsrc->probation);
1299 case PROP_UDP_RECONNECT:
1300 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1302 case PROP_MULTICAST_IFACE:
1303 g_value_set_string (value, rtspsrc->multi_iface);
1306 g_value_set_boolean (value, rtspsrc->ntp_sync);
1308 case PROP_USE_PIPELINE_CLOCK:
1309 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1312 g_value_set_boxed (value, rtspsrc->sdes);
1314 case PROP_TLS_VALIDATION_FLAGS:
1315 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1317 case PROP_TLS_DATABASE:
1318 g_value_set_object (value, rtspsrc->tls_database);
1320 case PROP_TLS_INTERACTION:
1321 g_value_set_object (value, rtspsrc->tls_interaction);
1323 case PROP_DO_RETRANSMISSION:
1324 g_value_set_boolean (value, rtspsrc->do_retransmission);
1326 case PROP_NTP_TIME_SOURCE:
1327 g_value_set_enum (value, rtspsrc->ntp_time_source);
1329 case PROP_USER_AGENT:
1330 g_value_set_string (value, rtspsrc->user_agent);
1332 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1333 g_value_set_int (value, rtspsrc->max_rtcp_rtp_time_diff);
1335 case PROP_RFC7273_SYNC:
1336 g_value_set_boolean (value, rtspsrc->rfc7273_sync);
1339 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1345 find_stream_by_id (GstRTSPStream * stream, gint * id)
1347 if (stream->id == *id)
1354 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1356 /* ignore unconfigured channels here (e.g., those that
1357 * were explicitly skipped during SETUP) */
1358 if ((stream->channelpad[0] != NULL) &&
1359 (stream->channel[0] == *channel || stream->channel[1] == *channel))
1366 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1368 GstElement *src = (GstElement *) a;
1370 if (stream->udpsrc[0] == src)
1372 if (stream->udpsrc[1] == src)
1379 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1381 if (stream->conninfo.location) {
1382 /* check qualified setup_url */
1383 if (!strcmp (stream->conninfo.location, (gchar *) a))
1386 if (stream->control_url) {
1387 /* check original control_url */
1388 if (!strcmp (stream->control_url, (gchar *) a))
1391 /* check if qualified setup_url ends with string */
1392 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1399 static GstRTSPStream *
1400 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1404 /* find and get stream */
1405 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1406 return (GstRTSPStream *) lstream->data;
1411 static const GstSDPBandwidth *
1412 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1413 const GstSDPMedia * media, const gchar * type)
1417 /* first look in the media specific section */
1418 len = gst_sdp_media_bandwidths_len (media);
1419 for (i = 0; i < len; i++) {
1420 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1422 if (strcmp (bw->bwtype, type) == 0)
1425 /* then look in the message specific section */
1426 len = gst_sdp_message_bandwidths_len (sdp);
1427 for (i = 0; i < len; i++) {
1428 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1430 if (strcmp (bw->bwtype, type) == 0)
1437 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1438 const GstSDPMedia * media, GstRTSPStream * stream)
1440 const GstSDPBandwidth *bw;
1442 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1443 stream->as_bandwidth = bw->bandwidth;
1445 stream->as_bandwidth = -1;
1447 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1448 stream->rr_bandwidth = bw->bandwidth;
1450 stream->rr_bandwidth = -1;
1452 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1453 stream->rs_bandwidth = bw->bandwidth;
1455 stream->rs_bandwidth = -1;
1459 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1460 const GstSDPConnection * conn)
1462 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1465 if (conn->addrtype == NULL)
1468 /* check for IPV6 */
1469 if (strcmp (conn->addrtype, "IP4") == 0)
1470 stream->is_ipv6 = FALSE;
1471 else if (strcmp (conn->addrtype, "IP6") == 0)
1472 stream->is_ipv6 = TRUE;
1477 g_free (stream->destination);
1478 stream->destination = g_strdup (conn->address);
1480 /* check for multicast */
1481 stream->is_multicast =
1482 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1484 stream->ttl = conn->ttl;
1487 /* Go over the connections for a stream.
1488 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1490 * - If we are dealing with a localhost address, we disable multicast
1493 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1494 const GstSDPMedia * media, GstRTSPStream * stream)
1496 const GstSDPConnection *conn;
1499 /* first look in the media specific section */
1500 len = gst_sdp_media_connections_len (media);
1501 for (i = 0; i < len; i++) {
1502 conn = gst_sdp_media_get_connection (media, i);
1504 gst_rtspsrc_do_stream_connection (src, stream, conn);
1506 /* then look in the message specific section */
1507 if ((conn = gst_sdp_message_get_connection (sdp))) {
1508 gst_rtspsrc_do_stream_connection (src, stream, conn);
1513 make_stream_id (GstRTSPStream * stream, const GstSDPMedia * media)
1516 g_strdup_printf ("%s:%d:%d:%s:%d", media->media, media->port,
1517 media->num_ports, media->proto, stream->default_pt);
1519 g_strcanon (stream_id, G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS, ':');
1524 /* m=<media> <UDP port> RTP/AVP <payload>
1527 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
1528 const GstSDPMedia * media, GstRTSPStream * stream)
1532 GstCaps *global_caps;
1535 proto = gst_sdp_media_get_proto (media);
1539 if (g_str_equal (proto, "RTP/AVP"))
1540 stream->profile = GST_RTSP_PROFILE_AVP;
1541 else if (g_str_equal (proto, "RTP/SAVP"))
1542 stream->profile = GST_RTSP_PROFILE_SAVP;
1543 else if (g_str_equal (proto, "RTP/AVPF"))
1544 stream->profile = GST_RTSP_PROFILE_AVPF;
1545 else if (g_str_equal (proto, "RTP/SAVPF"))
1546 stream->profile = GST_RTSP_PROFILE_SAVPF;
1550 /* Parse global SDP attributes once */
1551 global_caps = gst_caps_new_empty_simple ("application/x-unknown");
1552 GST_DEBUG ("mapping sdp session level attributes to caps");
1553 gst_sdp_message_attributes_to_caps (sdp, global_caps);
1554 GST_DEBUG ("mapping sdp media level attributes to caps");
1555 gst_sdp_media_attributes_to_caps (media, global_caps);
1557 /* Keep a copy of the SDP key management */
1558 gst_sdp_media_parse_keymgmt (media, &stream->mikey);
1559 if (stream->mikey == NULL)
1560 gst_sdp_message_parse_keymgmt (sdp, &stream->mikey);
1562 len = gst_sdp_media_formats_len (media);
1563 for (i = 0; i < len; i++) {
1565 GstCaps *caps, *outcaps;
1570 pt = atoi (gst_sdp_media_get_format (media, i));
1572 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
1575 caps = gst_sdp_media_get_caps_from_media (media, pt);
1577 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
1581 /* do some tweaks */
1582 s = gst_caps_get_structure (caps, 0);
1583 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
1584 stream->is_real = (strstr (enc, "-REAL") != NULL);
1585 if (strcmp (enc, "X-ASF-PF") == 0)
1586 stream->container = TRUE;
1589 /* Merge in global caps */
1590 /* Intersect will merge in missing fields to the current caps */
1591 outcaps = gst_caps_intersect (caps, global_caps);
1592 gst_caps_unref (caps);
1594 /* the first pt will be the default */
1595 if (stream->ptmap->len == 0)
1596 stream->default_pt = pt;
1599 item.caps = outcaps;
1601 g_array_append_val (stream->ptmap, item);
1604 stream->stream_id = make_stream_id (stream, media);
1606 gst_caps_unref (global_caps);
1611 GST_ERROR_OBJECT (src, "can't find proto in media");
1616 GST_ERROR_OBJECT (src, "unknown proto in media: '%s'", proto);
1621 static const gchar *
1622 get_aggregate_control (GstRTSPSrc * src)
1627 base = src->control;
1628 else if (src->content_base)
1629 base = src->content_base;
1630 else if (src->conninfo.url_str)
1631 base = src->conninfo.url_str;
1639 clear_ptmap_item (PtMapItem * item)
1642 gst_caps_unref (item->caps);
1645 static GstRTSPStream *
1646 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx,
1649 GstRTSPStream *stream;
1650 const gchar *control_url;
1651 const GstSDPMedia *media;
1653 /* get media, should not return NULL */
1654 media = gst_sdp_message_get_media (sdp, idx);
1658 stream = g_new0 (GstRTSPStream, 1);
1659 stream->parent = src;
1660 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1662 stream->last_ret = GST_FLOW_NOT_LINKED;
1663 stream->added = FALSE;
1664 stream->setup = FALSE;
1665 stream->skipped = FALSE;
1667 stream->eos = FALSE;
1668 stream->discont = TRUE;
1669 stream->seqbase = -1;
1670 stream->timebase = -1;
1671 stream->send_ssrc = g_random_int ();
1672 stream->profile = GST_RTSP_PROFILE_AVP;
1673 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1674 stream->mikey = NULL;
1675 stream->stream_id = NULL;
1676 g_mutex_init (&stream->conninfo.send_lock);
1677 g_mutex_init (&stream->conninfo.recv_lock);
1678 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
1680 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1681 * session manager to scale RTCP. */
1682 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1684 /* collect connection info */
1685 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1687 /* make the payload type map */
1688 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1690 /* collect port number */
1691 stream->port = gst_sdp_media_get_port (media);
1693 /* get control url to construct the setup url. The setup url is used to
1694 * configure the transport of the stream and is used to identity the stream in
1695 * the RTP-Info header field returned from PLAY. */
1696 control_url = gst_sdp_media_get_attribute_val (media, "control");
1697 if (control_url == NULL)
1698 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1700 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1701 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1702 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1703 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1705 /* RFC 2326, C.3: missing control_url permitted in case of a single stream */
1706 if (control_url == NULL && n_streams == 1) {
1710 if (control_url != NULL) {
1711 stream->control_url = g_strdup (control_url);
1712 /* Build a fully qualified url using the content_base if any or by prefixing
1713 * the original request.
1714 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1715 * likely build a URL that the server will fail to understand, this is ok,
1716 * we will fail then. */
1717 if (g_str_has_prefix (control_url, "rtsp://"))
1718 stream->conninfo.location = g_strdup (control_url);
1723 if (g_strcmp0 (control_url, "*") == 0)
1726 base = get_aggregate_control (src);
1728 /* check if the base ends or control starts with / */
1729 has_slash = g_str_has_prefix (control_url, "/");
1730 has_slash = has_slash || g_str_has_suffix (base, "/");
1732 /* concatenate the two strings, insert / when not present */
1733 stream->conninfo.location =
1734 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1737 GST_DEBUG_OBJECT (src, " setup: %s",
1738 GST_STR_NULL (stream->conninfo.location));
1740 /* we keep track of all streams */
1741 src->streams = g_list_append (src->streams, stream);
1749 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1753 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1755 g_array_free (stream->ptmap, TRUE);
1757 g_free (stream->destination);
1758 g_free (stream->control_url);
1759 g_free (stream->conninfo.location);
1760 g_free (stream->stream_id);
1762 for (i = 0; i < 2; i++) {
1763 if (stream->udpsrc[i]) {
1764 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1765 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1766 gst_object_unref (stream->udpsrc[i]);
1768 if (stream->channelpad[i])
1769 gst_object_unref (stream->channelpad[i]);
1771 if (stream->udpsink[i]) {
1772 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1773 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1774 gst_object_unref (stream->udpsink[i]);
1777 if (stream->fakesrc) {
1778 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1779 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1780 gst_object_unref (stream->fakesrc);
1782 if (stream->srcpad) {
1783 gst_pad_set_active (stream->srcpad, FALSE);
1785 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1787 if (stream->srtpenc)
1788 gst_object_unref (stream->srtpenc);
1789 if (stream->srtpdec)
1790 gst_object_unref (stream->srtpdec);
1791 if (stream->srtcpparams)
1792 gst_caps_unref (stream->srtcpparams);
1794 gst_mikey_message_unref (stream->mikey);
1795 if (stream->rtcppad)
1796 gst_object_unref (stream->rtcppad);
1797 if (stream->session)
1798 g_object_unref (stream->session);
1799 if (stream->rtx_pt_map)
1800 gst_structure_free (stream->rtx_pt_map);
1802 g_mutex_clear (&stream->conninfo.send_lock);
1803 g_mutex_clear (&stream->conninfo.recv_lock);
1809 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1813 GST_DEBUG_OBJECT (src, "cleanup");
1815 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1816 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1818 gst_rtspsrc_stream_free (src, stream);
1820 g_list_free (src->streams);
1821 src->streams = NULL;
1823 if (src->manager_sig_id) {
1824 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1825 src->manager_sig_id = 0;
1827 gst_element_set_state (src->manager, GST_STATE_NULL);
1828 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1829 src->manager = NULL;
1832 gst_structure_free (src->props);
1835 g_free (src->content_base);
1836 src->content_base = NULL;
1838 g_free (src->control);
1839 src->control = NULL;
1842 gst_rtsp_range_free (src->range);
1845 /* don't clear the SDP when it was used in the url */
1846 if (src->sdp && !src->from_sdp) {
1847 gst_sdp_message_free (src->sdp);
1851 src->need_segment = FALSE;
1853 if (src->provided_clock) {
1854 gst_object_unref (src->provided_clock);
1855 src->provided_clock = NULL;
1860 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1861 gint * rtpport, gint * rtcpport)
1864 GstStateChangeReturn ret;
1865 GstElement *udpsrc0, *udpsrc1;
1866 gint tmp_rtp, tmp_rtcp;
1870 src = stream->parent;
1876 /* Start at next port */
1877 tmp_rtp = src->next_port_num;
1879 if (stream->is_ipv6)
1880 host = "udp://[::0]";
1882 host = "udp://0.0.0.0";
1884 /* try to allocate 2 UDP ports, the RTP port should be an even
1885 * number and the RTCP port should be the next (uneven) port */
1888 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1889 tmp_rtp >= src->client_port_range.max)
1892 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1893 if (udpsrc0 == NULL)
1894 goto no_udp_protocol;
1895 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1897 if (src->udp_buffer_size != 0)
1898 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
1901 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
1902 if (ret == GST_STATE_CHANGE_FAILURE) {
1904 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
1907 if (++count > src->retry)
1910 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1911 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1912 gst_object_unref (udpsrc0);
1915 GST_DEBUG_OBJECT (src, "retry %d", count);
1918 goto no_udp_protocol;
1921 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
1922 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
1924 /* check if port is even */
1925 if ((tmp_rtp & 0x01) != 0) {
1926 /* port not even, close and allocate another */
1927 if (++count > src->retry)
1930 GST_DEBUG_OBJECT (src, "RTP port not even");
1932 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1933 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1934 gst_object_unref (udpsrc0);
1937 GST_DEBUG_OBJECT (src, "retry %d", count);
1942 /* allocate port+1 for RTCP now */
1943 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1944 if (udpsrc1 == NULL)
1945 goto no_udp_rtcp_protocol;
1948 tmp_rtcp = tmp_rtp + 1;
1949 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
1952 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
1954 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
1955 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
1956 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
1957 if (ret == GST_STATE_CHANGE_FAILURE) {
1958 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
1960 if (++count > src->retry)
1963 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1964 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1965 gst_object_unref (udpsrc0);
1968 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
1969 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1970 gst_object_unref (udpsrc1);
1974 GST_DEBUG_OBJECT (src, "retry %d", count);
1978 /* all fine, do port check */
1979 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
1980 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
1982 /* this should not happen... */
1983 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
1986 /* we keep these elements, we configure all in configure_transport when the
1987 * server told us to really use the UDP ports. */
1988 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
1989 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
1990 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
1991 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
1993 /* keep track of next available port number when we have a range
1995 if (src->next_port_num != 0)
1996 src->next_port_num = tmp_rtcp + 1;
2003 GST_DEBUG_OBJECT (src, "could not get UDP source");
2008 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2012 no_udp_rtcp_protocol:
2014 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2019 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2020 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2026 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2027 gst_object_unref (udpsrc0);
2030 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2031 gst_object_unref (udpsrc1);
2038 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2043 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2045 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2046 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2049 for (i = 0; i < 2; i++) {
2050 if (stream->udpsrc[i])
2051 gst_element_set_state (stream->udpsrc[i], state);
2057 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2064 event = gst_event_new_flush_start ();
2065 GST_DEBUG_OBJECT (src, "start flush");
2067 state = GST_STATE_PAUSED;
2069 event = gst_event_new_flush_stop (FALSE);
2070 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2073 state = GST_STATE_PLAYING;
2075 state = GST_STATE_PAUSED;
2077 gst_rtspsrc_push_event (src, event);
2078 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2079 gst_rtspsrc_set_state (src, state);
2082 static GstRTSPResult
2083 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2084 GstRTSPMessage * message, GTimeVal * timeout)
2088 if (conninfo->connection) {
2089 g_mutex_lock (&conninfo->send_lock);
2090 ret = gst_rtsp_connection_send (conninfo->connection, message, timeout);
2091 g_mutex_unlock (&conninfo->send_lock);
2093 ret = GST_RTSP_ERROR;
2099 static GstRTSPResult
2100 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2101 GstRTSPMessage * message, GTimeVal * timeout)
2105 if (conninfo->connection) {
2106 g_mutex_lock (&conninfo->recv_lock);
2107 ret = gst_rtsp_connection_receive (conninfo->connection, message, timeout);
2108 g_mutex_unlock (&conninfo->recv_lock);
2110 ret = GST_RTSP_ERROR;
2117 gst_rtspsrc_get_position (GstRTSPSrc * src)
2122 query = gst_query_new_position (GST_FORMAT_TIME);
2123 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2124 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2125 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2129 if (stream->srcpad) {
2130 if (gst_pad_query (stream->srcpad, query)) {
2131 gst_query_parse_position (query, &fmt, &pos);
2132 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2133 GST_TIME_ARGS (pos));
2134 src->last_pos = pos;
2144 gst_query_unref (query);
2148 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2153 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2155 gboolean flush, skip;
2158 GstSegment seeksegment = { 0, };
2162 GST_DEBUG_OBJECT (src, "doing seek with event");
2164 gst_event_parse_seek (event, &rate, &format, &flags,
2165 &cur_type, &cur, &stop_type, &stop);
2167 /* no negative rates yet */
2171 /* we need TIME format */
2172 if (format != src->segment.format)
2175 GST_DEBUG_OBJECT (src, "doing seek without event");
2177 cur_type = GST_SEEK_TYPE_SET;
2178 stop_type = GST_SEEK_TYPE_SET;
2181 /* get flush flag */
2182 flush = flags & GST_SEEK_FLAG_FLUSH;
2183 skip = flags & GST_SEEK_FLAG_SKIP;
2185 /* now we need to make sure the streaming thread is stopped. We do this by
2186 * either sending a FLUSH_START event downstream which will cause the
2187 * streaming thread to stop with a WRONG_STATE.
2188 * For a non-flushing seek we simply pause the task, which will happen as soon
2189 * as it completes one iteration (and thus might block when the sink is
2190 * blocking in preroll). */
2192 GST_DEBUG_OBJECT (src, "starting flush");
2193 gst_rtspsrc_flush (src, TRUE, FALSE);
2196 gst_task_pause (src->task);
2200 /* we should now be able to grab the streaming thread because we stopped it
2201 * with the above flush/pause code */
2202 GST_RTSP_STREAM_LOCK (src);
2204 GST_DEBUG_OBJECT (src, "stopped streaming");
2206 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2207 gst_rtspsrc_connection_flush (src, FALSE);
2209 /* copy segment, we need this because we still need the old
2210 * segment when we close the current segment. */
2211 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2213 /* configure the seek parameters in the seeksegment. We will then have the
2214 * right values in the segment to perform the seek */
2216 GST_DEBUG_OBJECT (src, "configuring seek");
2217 gst_segment_do_seek (&seeksegment, rate, format, flags,
2218 cur_type, cur, stop_type, stop, &update);
2221 /* figure out the last position we need to play. If it's configured (stop !=
2222 * -1), use that, else we play until the total duration of the file */
2223 if ((stop = seeksegment.stop) == -1)
2224 stop = seeksegment.duration;
2226 /* if we were playing, pause first */
2227 playing = (src->state == GST_RTSP_STATE_PLAYING);
2229 /* obtain current position in case seek fails */
2230 gst_rtspsrc_get_position (src);
2231 gst_rtspsrc_pause (src, FALSE);
2235 src->state = GST_RTSP_STATE_SEEKING;
2237 /* PLAY will add the range header now. */
2238 src->need_range = TRUE;
2240 /* prepare for streaming again */
2242 /* if we started flush, we stop now */
2243 GST_DEBUG_OBJECT (src, "stopping flush");
2244 gst_rtspsrc_flush (src, FALSE, playing);
2247 /* now we did the seek and can activate the new segment values */
2248 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2250 /* if we're doing a segment seek, post a SEGMENT_START message */
2251 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2252 gst_element_post_message (GST_ELEMENT_CAST (src),
2253 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2254 src->segment.format, src->segment.position));
2257 /* now create the newsegment */
2258 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2259 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2262 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2263 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2264 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2265 stream->discont = TRUE;
2268 /* and continue playing if needed */
2269 GST_OBJECT_LOCK (src);
2270 playing = (GST_STATE_PENDING (src) == GST_STATE_VOID_PENDING
2271 && GST_STATE (src) == GST_STATE_PLAYING)
2272 || (GST_STATE_PENDING (src) == GST_STATE_PLAYING);
2273 GST_OBJECT_UNLOCK (src);
2275 gst_rtspsrc_play (src, &seeksegment, FALSE);
2277 GST_RTSP_STREAM_UNLOCK (src);
2284 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2289 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2295 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2299 gboolean res = TRUE;
2302 src = GST_RTSPSRC_CAST (parent);
2304 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2305 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2307 switch (GST_EVENT_TYPE (event)) {
2308 case GST_EVENT_SEEK:
2309 res = gst_rtspsrc_perform_seek (src, event);
2313 case GST_EVENT_NAVIGATION:
2314 case GST_EVENT_LATENCY:
2322 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2323 res = gst_pad_send_event (target, event);
2324 gst_object_unref (target);
2326 gst_event_unref (event);
2329 gst_event_unref (event);
2336 gst_rtspsrc_handle_src_sink_event (GstPad * pad, GstObject * parent,
2339 GstRTSPStream *stream;
2341 stream = gst_pad_get_element_private (pad);
2343 switch (GST_EVENT_TYPE (event)) {
2344 case GST_EVENT_STREAM_START:{
2345 const gchar *upstream_id;
2348 gst_event_parse_stream_start (event, &upstream_id);
2349 stream_id = g_strdup_printf ("%s/%s", upstream_id, stream->stream_id);
2351 gst_event_unref (event);
2352 event = gst_event_new_stream_start (stream_id);
2359 return gst_pad_push_event (stream->srcpad, event);
2362 /* this is the final event function we receive on the internal source pad when
2363 * we deal with TCP connections */
2365 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2370 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2372 switch (GST_EVENT_TYPE (event)) {
2373 case GST_EVENT_SEEK:
2375 case GST_EVENT_NAVIGATION:
2376 case GST_EVENT_LATENCY:
2378 gst_event_unref (event);
2385 /* this is the final query function we receive on the internal source pad when
2386 * we deal with TCP connections */
2388 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2392 gboolean res = TRUE;
2394 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2396 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2397 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2399 switch (GST_QUERY_TYPE (query)) {
2400 case GST_QUERY_POSITION:
2405 case GST_QUERY_DURATION:
2409 gst_query_parse_duration (query, &format, NULL);
2412 case GST_FORMAT_TIME:
2413 gst_query_set_duration (query, format, src->segment.duration);
2421 case GST_QUERY_LATENCY:
2423 /* we are live with a min latency of 0 and unlimited max latency, this
2424 * result will be updated by the session manager if there is any. */
2425 gst_query_set_latency (query, TRUE, 0, -1);
2435 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2437 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2441 gboolean res = FALSE;
2443 src = GST_RTSPSRC_CAST (parent);
2445 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2446 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2448 switch (GST_QUERY_TYPE (query)) {
2449 case GST_QUERY_DURATION:
2453 gst_query_parse_duration (query, &format, NULL);
2456 case GST_FORMAT_TIME:
2457 gst_query_set_duration (query, format, src->segment.duration);
2465 case GST_QUERY_SEEKING:
2469 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2470 if (format == GST_FORMAT_TIME) {
2472 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2474 /* seeking without duration is unlikely */
2475 seekable = seekable && src->seekable && src->segment.duration &&
2476 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2478 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0,
2479 src->segment.duration);
2488 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2490 gst_query_set_uri (query, uri);
2498 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2500 /* forward the query to the proxy target pad */
2502 res = gst_pad_query (target, query);
2503 gst_object_unref (target);
2512 /* callback for RTCP messages to be sent to the server when operating in TCP
2514 static GstFlowReturn
2515 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2518 GstRTSPStream *stream;
2519 GstFlowReturn res = GST_FLOW_OK;
2524 GstRTSPMessage message = { 0 };
2525 GstRTSPConnInfo *conninfo;
2527 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2528 src = stream->parent;
2530 gst_buffer_map (buffer, &map, GST_MAP_READ);
2534 gst_rtsp_message_init_data (&message, stream->channel[1]);
2536 /* lend the body data to the message */
2537 gst_rtsp_message_take_body (&message, data, size);
2539 if (stream->conninfo.connection)
2540 conninfo = &stream->conninfo;
2542 conninfo = &src->conninfo;
2544 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2545 ret = gst_rtspsrc_connection_send (src, conninfo, &message, NULL);
2546 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2548 /* and steal it away again because we will free it when unreffing the
2550 gst_rtsp_message_steal_body (&message, &data, &size);
2551 gst_rtsp_message_unset (&message);
2553 gst_buffer_unmap (buffer, &map);
2554 gst_buffer_unref (buffer);
2559 static GstPadProbeReturn
2560 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2562 GstRTSPSrc *src = user_data;
2564 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2565 GST_DEBUG_PAD_NAME (pad));
2567 /* activate the streams */
2568 GST_OBJECT_LOCK (src);
2569 if (!src->need_activate)
2572 src->need_activate = FALSE;
2573 GST_OBJECT_UNLOCK (src);
2575 gst_rtspsrc_activate_streams (src);
2577 return GST_PAD_PROBE_OK;
2581 GST_OBJECT_UNLOCK (src);
2582 return GST_PAD_PROBE_OK;
2587 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
2589 GstPad *gpad = GST_PAD_CAST (user_data);
2591 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
2592 gst_pad_store_sticky_event (gpad, *event);
2597 /* this callback is called when the session manager generated a new src pad with
2598 * payloaded RTP packets. We simply ghost the pad here. */
2600 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2603 GstPadTemplate *template;
2606 GstRTSPStream *stream;
2608 GstPad *internal_src;
2610 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2612 GST_RTSP_STATE_LOCK (src);
2614 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2615 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2616 goto unknown_stream;
2618 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2620 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2622 goto unknown_stream;
2625 stream->ssrc = ssrc;
2627 /* we'll add it later see below */
2628 stream->added = TRUE;
2630 /* check if we added all streams */
2632 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2633 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2635 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
2636 ostream, ostream->container, ostream->added, ostream->setup);
2638 /* if we find a stream for which we did a setup that is not added, we
2639 * need to wait some more */
2640 if (ostream->setup && !ostream->added) {
2645 GST_RTSP_STATE_UNLOCK (src);
2647 /* create a new pad we will use to stream to */
2648 template = gst_static_pad_template_get (&rtptemplate);
2649 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2650 gst_object_unref (template);
2653 /* We intercept and modify the stream start event */
2655 GST_PAD (gst_proxy_pad_get_internal (GST_PROXY_PAD (stream->srcpad)));
2656 gst_pad_set_element_private (internal_src, stream);
2657 gst_pad_set_event_function (internal_src, gst_rtspsrc_handle_src_sink_event);
2658 gst_object_unref (internal_src);
2660 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2661 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2662 gst_pad_set_active (stream->srcpad, TRUE);
2663 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
2664 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2667 GST_DEBUG_OBJECT (src, "We added all streams");
2668 /* when we get here, all stream are added and we can fire the no-more-pads
2670 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2678 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2679 GST_RTSP_STATE_UNLOCK (src);
2686 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
2690 len = stream->ptmap->len;
2691 for (i = 0; i < len; i++) {
2692 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2700 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2702 GstRTSPStream *stream;
2705 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2707 GST_RTSP_STATE_LOCK (src);
2708 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2710 goto unknown_stream;
2712 if ((caps = stream_get_caps_for_pt (stream, pt)))
2713 gst_caps_ref (caps);
2714 GST_RTSP_STATE_UNLOCK (src);
2720 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2721 GST_RTSP_STATE_UNLOCK (src);
2727 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2729 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2735 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2741 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2747 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2749 GstRTSPSrc *src = stream->parent;
2752 g_object_get (source, "ssrc", &ssrc, NULL);
2754 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2755 ssrc, stream->ssrc, stream->id);
2757 if (ssrc == stream->ssrc)
2758 gst_rtspsrc_do_stream_eos (src, stream);
2762 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2764 GstRTSPSrc *src = stream->parent;
2767 g_object_get (source, "ssrc", &ssrc, NULL);
2769 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2770 ssrc, stream->ssrc, stream->id);
2772 if (ssrc == stream->ssrc)
2773 gst_rtspsrc_do_stream_eos (src, stream);
2777 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2779 GstRTSPStream *stream;
2781 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2783 /* get stream for session */
2784 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2786 gst_rtspsrc_do_stream_eos (src, stream);
2791 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2793 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2798 set_manager_buffer_mode (GstRTSPSrc * src)
2800 GObjectClass *klass;
2802 if (src->manager == NULL)
2805 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2807 if (!g_object_class_find_property (klass, "buffer-mode"))
2810 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2811 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2816 GST_DEBUG_OBJECT (src,
2817 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
2819 if (src->provided_clock) {
2820 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
2822 if (clock == src->provided_clock) {
2823 GST_DEBUG_OBJECT (src, "selected synced");
2824 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
2827 gst_object_unref (clock);
2832 /* Otherwise fall-through and use another buffer mode */
2834 gst_object_unref (clock);
2837 GST_DEBUG_OBJECT (src, "auto buffering mode");
2838 if (src->use_buffering) {
2839 GST_DEBUG_OBJECT (src, "selected buffer");
2840 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
2842 GST_DEBUG_OBJECT (src, "selected slave");
2843 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2848 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2852 GstMIKEYMessage *msg = stream->mikey;
2854 GST_DEBUG ("request key SSRC %u", ssrc);
2856 caps = gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
2857 caps = gst_caps_make_writable (caps);
2859 /* parse crypto sessions and look for the SSRC rollover counter */
2860 msg = stream->mikey;
2861 for (i = 0; msg && i < gst_mikey_message_get_n_cs (msg); i++) {
2862 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
2864 if (ssrc == map->ssrc) {
2865 gst_caps_set_simple (caps, "roc", G_TYPE_UINT, map->roc, NULL);
2874 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2876 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
2877 if (stream->id != session)
2880 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
2881 stream->profile != GST_RTSP_PROFILE_SAVPF)
2884 if (stream->srtpdec == NULL) {
2887 name = g_strdup_printf ("srtpdec_%u", session);
2888 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
2891 if (stream->srtpdec == NULL) {
2892 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
2893 ("no srtpdec element present!"));
2896 g_signal_connect (stream->srtpdec, "request-key",
2897 (GCallback) request_key, stream);
2899 return gst_object_ref (stream->srtpdec);
2903 request_rtcp_encoder (GstElement * rtpbin, guint session,
2904 GstRTSPStream * stream)
2909 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
2910 if (stream->id != session)
2913 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
2914 stream->profile != GST_RTSP_PROFILE_SAVPF)
2917 if (stream->srtpenc == NULL) {
2920 name = g_strdup_printf ("srtpenc_%u", session);
2921 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
2924 if (stream->srtpenc == NULL) {
2925 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
2926 ("no srtpenc element present!"));
2930 /* get RTCP crypto parameters from caps */
2931 s = gst_caps_get_structure (stream->srtcpparams, 0);
2935 GType ciphertype, authtype;
2936 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
2938 ciphertype = g_type_from_name ("GstSrtpCipherType");
2939 authtype = g_type_from_name ("GstSrtpAuthType");
2940 g_value_init (&rtcp_cipher, ciphertype);
2941 g_value_init (&rtcp_auth, authtype);
2943 str = gst_structure_get_string (s, "srtcp-cipher");
2944 gst_value_deserialize (&rtcp_cipher, str);
2945 str = gst_structure_get_string (s, "srtcp-auth");
2946 gst_value_deserialize (&rtcp_auth, str);
2947 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
2949 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-cipher",
2951 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-auth",
2953 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
2955 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
2957 g_object_set (stream->srtpenc, "key", buf, NULL);
2959 g_value_unset (&rtcp_cipher);
2960 g_value_unset (&rtcp_auth);
2961 gst_buffer_unref (buf);
2964 name = g_strdup_printf ("rtcp_sink_%d", session);
2965 pad = gst_element_get_request_pad (stream->srtpenc, name);
2967 gst_object_unref (pad);
2969 return gst_object_ref (stream->srtpenc);
2973 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
2975 GstElement *rtx, *bin;
2978 GstRTSPStream *stream;
2980 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
2982 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
2986 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
2987 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
2988 bin = gst_bin_new (NULL);
2989 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
2990 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
2991 gst_bin_add (GST_BIN (bin), rtx);
2993 pad = gst_element_get_static_pad (rtx, "src");
2994 name = g_strdup_printf ("src_%u", sessid);
2995 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
2997 gst_object_unref (pad);
2999 pad = gst_element_get_static_pad (rtx, "sink");
3000 name = g_strdup_printf ("sink_%u", sessid);
3001 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3003 gst_object_unref (pad);
3009 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
3013 gboolean do_retransmission = FALSE;
3015 if (transport->trans != GST_RTSP_TRANS_RTP)
3017 if (transport->profile != GST_RTSP_PROFILE_AVPF &&
3018 transport->profile != GST_RTSP_PROFILE_SAVPF)
3021 signal_id = g_signal_lookup ("request-aux-receiver",
3022 G_OBJECT_TYPE (src->manager));
3023 /* there's already something connected */
3024 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
3025 NULL, NULL, NULL) != 0) {
3026 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
3027 "\"request-aux-receiver\" signal is "
3028 "already used by the application");
3032 /* build the retransmission payload type map */
3033 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3034 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3035 gboolean do_retransmission_stream = FALSE;
3038 if (stream->rtx_pt_map)
3039 gst_structure_free (stream->rtx_pt_map);
3040 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
3042 for (i = 0; i < stream->ptmap->len; i++) {
3043 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3044 GstStructure *s = gst_caps_get_structure (item->caps, 0);
3045 const gchar *encoding;
3047 /* we only care about RTX streams */
3048 if ((encoding = gst_structure_get_string (s, "encoding-name"))
3049 && g_strcmp0 (encoding, "RTX") == 0) {
3050 const gchar *stream_pt_s;
3053 if (gst_structure_get_int (s, "payload", &rtx_pt)
3054 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
3057 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
3059 do_retransmission_stream = TRUE;
3065 if (do_retransmission_stream) {
3066 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
3067 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
3068 do_retransmission = TRUE;
3070 GST_DEBUG_OBJECT (src, "no retransmission payload map for stream "
3071 "id %i", stream->id);
3072 gst_structure_free (stream->rtx_pt_map);
3073 stream->rtx_pt_map = NULL;
3077 if (do_retransmission) {
3078 GST_DEBUG_OBJECT (src, "Enabling retransmissions");
3080 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
3082 /* enable RFC4588 retransmission handling by setting rtprtxreceive
3083 * as the "aux" element of rtpbin */
3084 g_signal_connect (src->manager, "request-aux-receiver",
3085 (GCallback) request_aux_receiver, src);
3087 GST_DEBUG_OBJECT (src,
3088 "Not enabling retransmissions as no stream had a retransmission payload map");
3092 /* try to get and configure a manager */
3094 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3095 GstRTSPTransport * transport)
3097 const gchar *manager;
3099 GstStateChangeReturn ret;
3101 /* find a manager */
3102 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3106 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3108 /* configure the manager */
3109 if (src->manager == NULL) {
3110 GObjectClass *klass;
3112 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3114 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3118 goto use_no_manager;
3120 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3121 goto manager_failed;
3124 /* we manage this element */
3125 gst_element_set_locked_state (src->manager, TRUE);
3126 gst_bin_add (GST_BIN_CAST (src), src->manager);
3128 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3129 if (ret == GST_STATE_CHANGE_FAILURE)
3130 goto start_manager_failure;
3132 g_object_set (src->manager, "latency", src->latency, NULL);
3134 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3136 if (g_object_class_find_property (klass, "ntp-sync")) {
3137 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3140 if (g_object_class_find_property (klass, "rfc7273-sync")) {
3141 g_object_set (src->manager, "rfc7273-sync", src->rfc7273_sync, NULL);
3144 if (src->use_pipeline_clock) {
3145 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3146 g_object_set (src->manager, "use-pipeline-clock", TRUE, NULL);
3149 if (g_object_class_find_property (klass, "ntp-time-source")) {
3150 g_object_set (src->manager, "ntp-time-source", src->ntp_time_source,
3155 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3156 g_object_set (src->manager, "sdes", src->sdes, NULL);
3159 if (g_object_class_find_property (klass, "drop-on-latency")) {
3160 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3164 if (g_object_class_find_property (klass, "max-rtcp-rtp-time-diff")) {
3165 g_object_set (src->manager, "max-rtcp-rtp-time-diff",
3166 src->max_rtcp_rtp_time_diff, NULL);
3169 /* buffer mode pauses are handled by adding offsets to buffer times,
3170 * but some depayloaders may have a hard time syncing output times
3171 * with such input times, e.g. container ones, most notably ASF */
3172 /* TODO alternatives are having an event that indicates these shifts,
3173 * or having rtsp extensions provide suggestion on buffer mode */
3174 /* valid duration implies not likely live pipeline,
3175 * so slaving in jitterbuffer does not make much sense
3176 * (and might mess things up due to bursts) */
3177 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3178 src->segment.duration && stream->container) {
3179 src->use_buffering = TRUE;
3181 src->use_buffering = FALSE;
3184 set_manager_buffer_mode (src);
3186 /* connect to signals */
3187 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3189 src->manager_sig_id =
3190 g_signal_connect (src->manager, "pad-added",
3191 (GCallback) new_manager_pad, src);
3192 src->manager_ptmap_id =
3193 g_signal_connect (src->manager, "request-pt-map",
3194 (GCallback) request_pt_map, src);
3196 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3199 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3202 if (src->do_retransmission)
3203 add_retransmission (src, transport);
3205 g_signal_connect (src->manager, "request-rtp-decoder",
3206 (GCallback) request_rtp_decoder, stream);
3207 g_signal_connect (src->manager, "request-rtcp-decoder",
3208 (GCallback) request_rtp_decoder, stream);
3209 g_signal_connect (src->manager, "request-rtcp-encoder",
3210 (GCallback) request_rtcp_encoder, stream);
3212 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3213 * into a separate RTP session. */
3214 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3215 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3217 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3218 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3221 /* now configure the bandwidth in the manager */
3222 if (g_signal_lookup ("get-internal-session",
3223 G_OBJECT_TYPE (src->manager)) != 0) {
3224 GObject *rtpsession;
3226 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3229 GstRTPProfile rtp_profile;
3231 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3233 stream->session = rtpsession;
3235 if (stream->as_bandwidth != -1) {
3236 GST_INFO_OBJECT (src, "setting AS: %f",
3237 (gdouble) (stream->as_bandwidth * 1000));
3238 g_object_set (rtpsession, "bandwidth",
3239 (gdouble) (stream->as_bandwidth * 1000), NULL);
3241 if (stream->rr_bandwidth != -1) {
3242 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3243 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3246 if (stream->rs_bandwidth != -1) {
3247 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3248 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3252 switch (stream->profile) {
3253 case GST_RTSP_PROFILE_AVPF:
3254 rtp_profile = GST_RTP_PROFILE_AVPF;
3256 case GST_RTSP_PROFILE_SAVP:
3257 rtp_profile = GST_RTP_PROFILE_SAVP;
3259 case GST_RTSP_PROFILE_SAVPF:
3260 rtp_profile = GST_RTP_PROFILE_SAVPF;
3262 case GST_RTSP_PROFILE_AVP:
3264 rtp_profile = GST_RTP_PROFILE_AVP;
3268 g_object_set (rtpsession, "rtp-profile", rtp_profile, NULL);
3270 g_object_set (rtpsession, "probation", src->probation, NULL);
3272 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3274 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3276 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3278 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3280 g_signal_connect (rtpsession, "on-ssrc-active",
3281 (GCallback) on_ssrc_active, stream);
3292 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3297 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3300 start_manager_failure:
3302 GST_DEBUG_OBJECT (src, "could not start session manager");
3307 /* free the UDP sources allocated when negotiating a transport.
3308 * This function is called when the server negotiated to a transport where the
3309 * UDP sources are not needed anymore, such as TCP or multicast. */
3311 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3315 for (i = 0; i < 2; i++) {
3316 if (stream->udpsrc[i]) {
3317 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3318 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3319 gst_object_unref (stream->udpsrc[i]);
3320 stream->udpsrc[i] = NULL;
3325 /* for TCP, create pads to send and receive data to and from the manager and to
3326 * intercept various events and queries
3329 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3330 GstRTSPTransport * transport, GstPad ** outpad)
3333 GstPadTemplate *template;
3334 GstPad *pad0, *pad1;
3336 /* configure for interleaved delivery, nothing needs to be done
3337 * here, the loop function will call the chain functions of the
3338 * session manager. */
3339 stream->channel[0] = transport->interleaved.min;
3340 stream->channel[1] = transport->interleaved.max;
3341 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3342 stream->channel[0], stream->channel[1]);
3344 /* we can remove the allocated UDP ports now */
3345 gst_rtspsrc_stream_free_udp (stream);
3347 /* no session manager, send data to srcpad directly */
3348 if (!stream->channelpad[0]) {
3349 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3351 /* create a new pad we will use to stream to */
3352 name = g_strdup_printf ("stream_%u", stream->id);
3353 template = gst_static_pad_template_get (&rtptemplate);
3354 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3355 gst_object_unref (template);
3358 /* set caps and activate */
3359 gst_pad_use_fixed_caps (stream->channelpad[0]);
3360 gst_pad_set_active (stream->channelpad[0], TRUE);
3362 *outpad = gst_object_ref (stream->channelpad[0]);
3364 GST_DEBUG_OBJECT (src, "using manager source pad");
3366 template = gst_static_pad_template_get (&anysrctemplate);
3368 /* allocate pads for sending the channel data into the manager */
3369 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3370 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3371 gst_object_unref (stream->channelpad[0]);
3372 stream->channelpad[0] = pad0;
3373 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3374 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3375 gst_pad_set_element_private (pad0, src);
3376 gst_pad_set_active (pad0, TRUE);
3378 if (stream->channelpad[1]) {
3379 /* if we have a sinkpad for the other channel, create a pad and link to the
3381 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3382 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3383 gst_pad_link_full (pad1, stream->channelpad[1],
3384 GST_PAD_LINK_CHECK_NOTHING);
3385 gst_object_unref (stream->channelpad[1]);
3386 stream->channelpad[1] = pad1;
3387 gst_pad_set_active (pad1, TRUE);
3389 gst_object_unref (template);
3391 /* setup RTCP transport back to the server if we have to. */
3392 if (src->manager && src->do_rtcp) {
3395 template = gst_static_pad_template_get (&anysinktemplate);
3397 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3398 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3399 gst_pad_set_element_private (stream->rtcppad, stream);
3400 gst_pad_set_active (stream->rtcppad, TRUE);
3402 /* get session RTCP pad */
3403 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3404 pad = gst_element_get_request_pad (src->manager, name);
3409 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3410 gst_object_unref (pad);
3413 gst_object_unref (template);
3419 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3420 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3421 gint * max, guint * ttl)
3423 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3425 if (!(*destination = transport->destination))
3426 *destination = stream->destination;
3429 /* transport first */
3430 *min = transport->port.min;
3431 *max = transport->port.max;
3432 if (*min == -1 && *max == -1) {
3433 /* then try from SDP */
3434 if (stream->port != 0) {
3435 *min = stream->port;
3436 *max = stream->port + 1;
3442 if (!(*ttl = transport->ttl))
3447 /* first take the source, then the endpoint to figure out where to send
3449 if (!(*destination = transport->source)) {
3450 if (src->conninfo.connection)
3451 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3452 else if (stream->conninfo.connection)
3454 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3458 /* for unicast we only expect the ports here */
3459 *min = transport->server_port.min;
3460 *max = transport->server_port.max;
3465 /* For multicast create UDP sources and join the multicast group. */
3467 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3468 GstRTSPTransport * transport, GstPad ** outpad)
3471 const gchar *destination;
3474 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3476 /* we can remove the allocated UDP ports now */
3477 gst_rtspsrc_stream_free_udp (stream);
3479 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3482 /* we need a destination now */
3483 if (destination == NULL)
3484 goto no_destination;
3486 /* we really need ports now or we won't be able to receive anything at all */
3487 if (min == -1 && max == -1)
3490 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3491 destination, min, max);
3493 /* creating UDP source for RTP */
3495 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3497 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3499 if (stream->udpsrc[0] == NULL)
3502 /* take ownership */
3503 gst_object_ref_sink (stream->udpsrc[0]);
3505 if (src->udp_buffer_size != 0)
3506 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3507 src->udp_buffer_size, NULL);
3509 if (src->multi_iface != NULL)
3510 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3511 src->multi_iface, NULL);
3514 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3515 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3518 /* creating another UDP source for RTCP */
3522 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3524 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3526 if (stream->udpsrc[1] == NULL)
3529 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3530 stream->profile == GST_RTSP_PROFILE_SAVPF)
3531 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3533 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3534 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3535 gst_caps_unref (caps);
3537 /* take ownership */
3538 gst_object_ref_sink (stream->udpsrc[1]);
3540 if (src->multi_iface != NULL)
3541 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3542 src->multi_iface, NULL);
3544 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
3551 GST_DEBUG_OBJECT (src, "no UDP source element found");
3556 GST_DEBUG_OBJECT (src, "no destination found");
3561 GST_DEBUG_OBJECT (src, "no ports found");
3566 /* configure the remainder of the UDP ports */
3568 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3569 GstRTSPTransport * transport, GstPad ** outpad)
3571 /* we manage the UDP elements now. For unicast, the UDP sources where
3572 * allocated in the stream when we suggested a transport. */
3573 if (stream->udpsrc[0]) {
3576 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3577 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3579 GST_DEBUG_OBJECT (src, "setting up UDP source");
3581 /* configure a timeout on the UDP port. When the timeout message is
3582 * posted, we assume UDP transport is not possible. We reconnect using TCP
3584 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3585 src->udp_timeout * 1000, NULL);
3587 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
3588 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3590 /* get output pad of the UDP source. */
3591 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3593 /* save it so we can unblock */
3594 stream->blockedpad = *outpad;
3596 /* configure pad block on the pad. As soon as there is dataflow on the
3597 * UDP source, we know that UDP is not blocked by a firewall and we can
3598 * configure all the streams to let the application autoplug decoders. */
3600 gst_pad_add_probe (stream->blockedpad,
3601 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3602 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
3604 if (stream->channelpad[0]) {
3605 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3606 /* configure for UDP delivery, we need to connect the UDP pads to
3607 * the session plugin. */
3608 gst_pad_link_full (*outpad, stream->channelpad[0],
3609 GST_PAD_LINK_CHECK_NOTHING);
3610 gst_object_unref (*outpad);
3612 /* we connected to pad-added signal to get pads from the manager */
3614 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3619 if (stream->udpsrc[1]) {
3622 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3623 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3625 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3626 stream->profile == GST_RTSP_PROFILE_SAVPF)
3627 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3629 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3630 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3631 gst_caps_unref (caps);
3633 if (stream->channelpad[1]) {
3636 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3638 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3639 gst_pad_link_full (pad, stream->channelpad[1],
3640 GST_PAD_LINK_CHECK_NOTHING);
3641 gst_object_unref (pad);
3643 /* leave unlinked */
3649 /* configure the UDP sink back to the server for status reports */
3651 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3652 GstRTSPStream * stream, GstRTSPTransport * transport)
3655 gint rtp_port, rtcp_port;
3656 gboolean do_rtp, do_rtcp;
3657 const gchar *destination;
3662 /* get transport info */
3663 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3664 &rtp_port, &rtcp_port, &ttl);
3666 /* see what we need to do */
3667 do_rtp = (rtp_port != -1);
3668 /* it's possible that the server does not want us to send RTCP in which case
3670 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3672 /* we need a destination when we have RTP or RTCP ports */
3673 if (destination == NULL && (do_rtp || do_rtcp))
3674 goto no_destination;
3676 /* try to construct the fakesrc to the RTP port of the server to open up any
3679 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3682 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3683 stream->udpsink[0] =
3684 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3686 if (stream->udpsink[0] == NULL)
3687 goto no_sink_element;
3689 /* don't join multicast group, we will have the source socket do that */
3690 /* no sync or async state changes needed */
3691 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3692 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3694 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3696 if (stream->udpsrc[0]) {
3697 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3698 * so that NAT firewalls will open a hole for us */
3699 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3703 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3704 /* configure socket and make sure udpsink does not close it when shutting
3705 * down, it belongs to udpsrc after all. */
3706 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3707 "close-socket", FALSE, NULL);
3708 g_object_unref (socket);
3711 /* the source for the dummy packets to open up NAT */
3712 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3713 if (stream->fakesrc == NULL)
3714 goto no_fakesrc_element;
3716 /* random data in 5 buffers, a size of 200 bytes should be fine */
3717 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3718 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3720 /* keep everything locked */
3721 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3722 gst_element_set_locked_state (stream->fakesrc, TRUE);
3724 gst_object_ref (stream->udpsink[0]);
3725 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3726 gst_object_ref (stream->fakesrc);
3727 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3729 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3730 "sink", GST_PAD_LINK_CHECK_NOTHING);
3733 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3736 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3737 stream->udpsink[1] =
3738 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3740 if (stream->udpsink[1] == NULL)
3741 goto no_sink_element;
3743 /* don't join multicast group, we will have the source socket do that */
3744 /* no sync or async state changes needed */
3745 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3746 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3748 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3750 if (stream->udpsrc[1]) {
3751 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3752 * because some servers check the port number of where it sends RTCP to identify
3753 * the RTCP packets it receives */
3754 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3758 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3759 /* configure socket and make sure udpsink does not close it when shutting
3760 * down, it belongs to udpsrc after all. */
3761 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3762 "close-socket", FALSE, NULL);
3763 g_object_unref (socket);
3766 /* we keep this playing always */
3767 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3768 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3770 gst_object_ref (stream->udpsink[1]);
3771 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3773 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3775 /* get session RTCP pad */
3776 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3777 pad = gst_element_get_request_pad (src->manager, name);
3782 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3783 gst_object_unref (pad);
3792 GST_ERROR_OBJECT (src, "no destination address specified");
3797 GST_ERROR_OBJECT (src, "no UDP sink element found");
3802 GST_ERROR_OBJECT (src, "no fakesrc element found");
3807 GST_ERROR_OBJECT (src, "failed to create socket");
3812 /* sets up all elements needed for streaming over the specified transport.
3813 * Does not yet expose the element pads, this will be done when there is actuall
3814 * dataflow detected, which might never happen when UDP is blocked in a
3815 * firewall, for example.
3818 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3819 GstRTSPTransport * transport)
3822 GstPad *outpad = NULL;
3823 GstPadTemplate *template;
3825 const gchar *media_type;
3828 src = stream->parent;
3830 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3832 /* get the proper media type for this stream now */
3833 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
3834 goto unknown_transport;
3836 goto unknown_transport;
3838 /* configure the final media type */
3839 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
3841 len = stream->ptmap->len;
3842 for (i = 0; i < len; i++) {
3844 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3846 if (item->caps == NULL)
3849 s = gst_caps_get_structure (item->caps, 0);
3850 gst_structure_set_name (s, media_type);
3851 /* set ssrc if known */
3852 if (transport->ssrc)
3853 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
3856 /* try to get and configure a manager, channelpad[0-1] will be configured with
3857 * the pads for the manager, or NULL when no manager is needed. */
3858 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3861 switch (transport->lower_transport) {
3862 case GST_RTSP_LOWER_TRANS_TCP:
3863 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3864 goto transport_failed;
3866 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3867 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3868 goto transport_failed;
3869 /* fallthrough, the rest is the same for UDP and MCAST */
3870 case GST_RTSP_LOWER_TRANS_UDP:
3871 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3872 goto transport_failed;
3873 /* configure udpsinks back to the server for RTCP messages and for the
3874 * dummy RTP messages to open NAT. */
3875 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3876 goto transport_failed;
3879 goto unknown_transport;
3883 GST_DEBUG_OBJECT (src, "creating ghostpad");
3885 gst_pad_use_fixed_caps (outpad);
3887 /* create ghostpad, don't add just yet, this will be done when we activate
3889 name = g_strdup_printf ("stream_%u", stream->id);
3890 template = gst_static_pad_template_get (&rtptemplate);
3891 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3892 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3893 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3894 gst_object_unref (template);
3897 gst_object_unref (outpad);
3899 /* mark pad as ok */
3900 stream->last_ret = GST_FLOW_OK;
3907 GST_DEBUG_OBJECT (src, "failed to configure transport");
3912 GST_DEBUG_OBJECT (src, "unknown transport");
3917 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3922 /* send a couple of dummy random packets on the receiver RTP port to the server,
3923 * this should make a firewall think we initiated the data transfer and
3924 * hopefully allow packets to go from the sender port to our RTP receiver port */
3926 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3930 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3933 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3934 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3936 if (stream->fakesrc && stream->udpsink[0]) {
3937 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3938 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3939 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3940 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3941 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3947 /* Adds the source pads of all configured streams to the element.
3948 * This code is performed when we detected dataflow.
3950 * We detect dataflow from either the _loop function or with pad probes on the
3954 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3958 GST_DEBUG_OBJECT (src, "activating streams");
3960 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3961 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3963 if (stream->udpsrc[0]) {
3964 /* remove timeout, we are streaming now and timeouts will be handled by
3965 * the session manager and jitter buffer */
3966 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3968 if (stream->srcpad) {
3969 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3970 gst_pad_set_active (stream->srcpad, TRUE);
3972 /* if we don't have a session manager, set the caps now. If we have a
3973 * session, we will get a notification of the pad and the caps. */
3974 if (!src->manager) {
3977 caps = stream_get_caps_for_pt (stream, stream->default_pt);
3978 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3979 gst_pad_set_caps (stream->srcpad, caps);
3982 if (!stream->added) {
3983 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3984 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3985 stream->added = TRUE;
3990 /* unblock all pads */
3991 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3992 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3994 if (stream->blockid) {
3995 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3996 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3997 stream->blockid = 0;
4005 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
4006 gboolean reset_manager)
4009 guint64 start, stop;
4010 gdouble play_speed, play_scale;
4012 GST_DEBUG_OBJECT (src, "configuring stream caps");
4014 start = segment->position;
4015 stop = segment->duration;
4016 play_speed = segment->rate;
4017 play_scale = segment->applied_rate;
4019 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4020 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4026 len = stream->ptmap->len;
4027 for (j = 0; j < len; j++) {
4029 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
4031 if (item->caps == NULL)
4034 caps = gst_caps_make_writable (item->caps);
4036 if (stream->timebase != -1)
4037 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
4038 (guint) stream->timebase, NULL);
4039 if (stream->seqbase != -1)
4040 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
4041 (guint) stream->seqbase, NULL);
4042 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
4044 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
4045 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
4046 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
4049 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
4052 if (item->pt == stream->default_pt) {
4053 if (stream->udpsrc[0])
4054 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4055 stream->need_caps = TRUE;
4059 if (reset_manager && src->manager) {
4060 GST_DEBUG_OBJECT (src, "clear session");
4061 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
4065 static GstFlowReturn
4066 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
4071 /* store the value */
4072 stream->last_ret = ret;
4074 /* if it's success we can return the value right away */
4075 if (ret == GST_FLOW_OK)
4078 /* any other error that is not-linked can be returned right
4080 if (ret != GST_FLOW_NOT_LINKED)
4083 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4084 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4085 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4087 ret = ostream->last_ret;
4088 /* some other return value (must be SUCCESS but we can return
4089 * other values as well) */
4090 if (ret != GST_FLOW_NOT_LINKED)
4093 /* if we get here, all other pads were unlinked and we return
4094 * NOT_LINKED then */
4100 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4103 gboolean res = TRUE;
4105 /* only streams that have a connection to the outside world */
4109 if (stream->udpsrc[0]) {
4110 gst_event_ref (event);
4111 res = gst_element_send_event (stream->udpsrc[0], event);
4112 } else if (stream->channelpad[0]) {
4113 gst_event_ref (event);
4114 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4115 res = gst_pad_push_event (stream->channelpad[0], event);
4117 res = gst_pad_send_event (stream->channelpad[0], event);
4120 if (stream->udpsrc[1]) {
4121 gst_event_ref (event);
4122 res &= gst_element_send_event (stream->udpsrc[1], event);
4123 } else if (stream->channelpad[1]) {
4124 gst_event_ref (event);
4125 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4126 res &= gst_pad_push_event (stream->channelpad[1], event);
4128 res &= gst_pad_send_event (stream->channelpad[1], event);
4132 gst_event_unref (event);
4138 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4141 gboolean res = TRUE;
4143 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4144 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4146 gst_event_ref (event);
4147 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4149 gst_event_unref (event);
4154 static GstRTSPResult
4155 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4159 GstRTSPMessage response;
4160 gboolean retry = FALSE;
4161 memset (&response, 0, sizeof (response));
4162 gst_rtsp_message_init (&response);
4164 if (info->connection == NULL) {
4165 if (info->url == NULL) {
4166 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4167 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4170 /* create connection */
4171 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4172 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4173 goto could_not_create;
4176 gst_rtspsrc_setup_auth (src, &response);
4179 g_free (info->url_str);
4180 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4182 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4184 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4185 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4186 src->tls_validation_flags))
4187 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4189 if (src->tls_database)
4190 gst_rtsp_connection_set_tls_database (info->connection,
4193 if (src->tls_interaction)
4194 gst_rtsp_connection_set_tls_interaction (info->connection,
4195 src->tls_interaction);
4198 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4199 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4201 if (src->proxy_host) {
4202 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4204 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4209 if (!info->connected) {
4212 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4213 ("Connecting to %s", info->location));
4214 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4215 res = gst_rtsp_connection_connect_with_response (info->connection,
4216 src->ptcp_timeout, &response);
4218 if (response.type == GST_RTSP_MESSAGE_HTTP_RESPONSE &&
4219 response.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4220 gst_rtsp_conninfo_close (src, info, TRUE);
4224 retry = FALSE; // we should not retry more than once
4229 if (res == GST_RTSP_OK)
4230 info->connected = TRUE;
4232 goto could_not_connect;
4234 } while (!info->connected && retry);
4236 gst_rtsp_message_unset (&response);
4242 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4243 gst_rtsp_message_unset (&response);
4248 gchar *str = gst_rtsp_strresult (res);
4249 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4251 gst_rtsp_message_unset (&response);
4256 gchar *str = gst_rtsp_strresult (res);
4257 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4259 gst_rtsp_message_unset (&response);
4264 static GstRTSPResult
4265 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4268 GST_RTSP_STATE_LOCK (src);
4269 if (info->connected) {
4270 GST_DEBUG_OBJECT (src, "closing connection...");
4271 gst_rtsp_connection_close (info->connection);
4272 info->connected = FALSE;
4274 if (free && info->connection) {
4275 /* free connection */
4276 GST_DEBUG_OBJECT (src, "freeing connection...");
4277 gst_rtsp_connection_free (info->connection);
4278 info->connection = NULL;
4279 info->flushing = FALSE;
4281 GST_RTSP_STATE_UNLOCK (src);
4285 static GstRTSPResult
4286 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4291 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4292 gst_rtsp_conninfo_close (src, info, FALSE);
4293 res = gst_rtsp_conninfo_connect (src, info, async);
4299 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4303 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4304 GST_RTSP_STATE_LOCK (src);
4305 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4306 GST_DEBUG_OBJECT (src, "connection flush");
4307 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4308 src->conninfo.flushing = flush;
4310 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4311 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4312 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4313 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4314 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4315 stream->conninfo.flushing = flush;
4318 GST_RTSP_STATE_UNLOCK (src);
4321 static GstRTSPResult
4322 gst_rtspsrc_init_request (GstRTSPSrc * src, GstRTSPMessage * msg,
4323 GstRTSPMethod method, const gchar * uri)
4327 res = gst_rtsp_message_init_request (msg, method, uri);
4331 /* set user-agent */
4332 if (src->user_agent)
4333 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, src->user_agent);
4338 /* FIXME, handle server request, reply with OK, for now */
4339 static GstRTSPResult
4340 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
4341 GstRTSPMessage * request)
4343 GstRTSPMessage response = { 0 };
4346 GST_DEBUG_OBJECT (src, "got server request message");
4349 gst_rtsp_message_dump (request);
4351 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4353 if (res == GST_RTSP_ENOTIMPL) {
4354 /* default implementation, send OK */
4355 GST_DEBUG_OBJECT (src, "prepare OK reply");
4357 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4362 /* let app parse and reply */
4363 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4364 0, request, &response);
4367 gst_rtsp_message_dump (&response);
4369 res = gst_rtspsrc_connection_send (src, conninfo, &response, NULL);
4373 gst_rtsp_message_unset (&response);
4374 } else if (res == GST_RTSP_EEOF)
4382 gst_rtsp_message_unset (&response);
4387 /* send server keep-alive */
4388 static GstRTSPResult
4389 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4391 GstRTSPMessage request = { 0 };
4393 GstRTSPMethod method;
4394 const gchar *control;
4396 if (src->do_rtsp_keep_alive == FALSE) {
4397 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4398 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4402 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4404 /* find a method to use for keep-alive */
4405 if (src->methods & GST_RTSP_GET_PARAMETER)
4406 method = GST_RTSP_GET_PARAMETER;
4408 method = GST_RTSP_OPTIONS;
4410 control = get_aggregate_control (src);
4411 if (control == NULL)
4414 res = gst_rtspsrc_init_request (src, &request, method, control);
4419 gst_rtsp_message_dump (&request);
4421 res = gst_rtspsrc_connection_send (src, &src->conninfo, &request, NULL);
4425 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4426 gst_rtsp_message_unset (&request);
4433 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4438 gchar *str = gst_rtsp_strresult (res);
4440 gst_rtsp_message_unset (&request);
4441 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4442 ("Could not send keep-alive. (%s)", str));
4448 static GstFlowReturn
4449 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4451 GstFlowReturn ret = GST_FLOW_OK;
4453 GstRTSPStream *stream;
4454 GstPad *outpad = NULL;
4460 channel = message->type_data.data.channel;
4462 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4464 goto unknown_stream;
4466 if (channel == stream->channel[0]) {
4467 outpad = stream->channelpad[0];
4469 } else if (channel == stream->channel[1]) {
4470 outpad = stream->channelpad[1];
4476 /* take a look at the body to figure out what we have */
4477 gst_rtsp_message_get_body (message, &data, &size);
4479 goto invalid_length;
4481 /* channels are not correct on some servers, do extra check */
4482 if (data[1] >= 200 && data[1] <= 204) {
4483 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4484 outpad = stream->channelpad[1];
4488 /* we have no clue what this is, just ignore then. */
4490 goto unknown_stream;
4492 /* take the message body for further processing */
4493 gst_rtsp_message_steal_body (message, &data, &size);
4495 /* strip the trailing \0 */
4498 buf = gst_buffer_new ();
4499 gst_buffer_append_memory (buf,
4500 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4502 /* don't need message anymore */
4503 gst_rtsp_message_unset (message);
4505 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4508 if (src->need_activate) {
4514 guint group_id = gst_util_group_id_next ();
4516 /* generate an SHA256 sum of the URI */
4517 cs = g_checksum_new (G_CHECKSUM_SHA256);
4518 uri = src->conninfo.location;
4519 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4521 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4522 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4526 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4527 event = gst_event_new_stream_start (stream_id);
4528 gst_event_set_group_id (event, group_id);
4531 gst_rtspsrc_stream_push_event (src, ostream, event);
4533 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
4534 /* only streams that have a connection to the outside world */
4535 if (ostream->setup) {
4536 if (ostream->udpsrc[0]) {
4537 gst_element_send_event (ostream->udpsrc[0],
4538 gst_event_new_caps (caps));
4539 } else if (ostream->channelpad[0]) {
4540 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
4541 gst_pad_push_event (ostream->channelpad[0],
4542 gst_event_new_caps (caps));
4544 gst_pad_send_event (ostream->channelpad[0],
4545 gst_event_new_caps (caps));
4547 ostream->need_caps = FALSE;
4549 if (ostream->profile == GST_RTSP_PROFILE_SAVP ||
4550 ostream->profile == GST_RTSP_PROFILE_SAVPF)
4551 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4553 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4555 if (ostream->udpsrc[1]) {
4556 gst_element_send_event (ostream->udpsrc[1],
4557 gst_event_new_caps (caps));
4558 } else if (ostream->channelpad[1]) {
4559 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
4560 gst_pad_push_event (ostream->channelpad[1],
4561 gst_event_new_caps (caps));
4563 gst_pad_send_event (ostream->channelpad[1],
4564 gst_event_new_caps (caps));
4567 gst_caps_unref (caps);
4571 g_checksum_free (cs);
4573 gst_rtspsrc_activate_streams (src);
4574 src->need_activate = FALSE;
4575 src->need_segment = TRUE;
4578 if (src->base_time == -1) {
4579 /* Take current running_time. This timestamp will be put on
4580 * the first buffer of each stream because we are a live source and so we
4581 * timestamp with the running_time. When we are dealing with TCP, we also
4582 * only timestamp the first buffer (using the DISCONT flag) because a server
4583 * typically bursts data, for which we don't want to compensate by speeding
4584 * up the media. The other timestamps will be interpollated from this one
4585 * using the RTP timestamps. */
4586 GST_OBJECT_LOCK (src);
4587 if (GST_ELEMENT_CLOCK (src)) {
4589 GstClockTime base_time;
4591 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4592 base_time = GST_ELEMENT_CAST (src)->base_time;
4594 src->base_time = now - base_time;
4596 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4597 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4599 GST_OBJECT_UNLOCK (src);
4602 /* If needed send a new segment, don't forget we are live and buffer are
4603 * timestamped with running time */
4604 if (src->need_segment) {
4606 src->need_segment = FALSE;
4607 gst_segment_init (&segment, GST_FORMAT_TIME);
4608 gst_rtspsrc_push_event (src, gst_event_new_segment (&segment));
4611 if (stream->need_caps) {
4614 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt))) {
4615 /* only streams that have a connection to the outside world */
4616 if (stream->setup) {
4617 /* Only need to update the TCP caps here, UDP is already handled */
4618 if (stream->channelpad[0]) {
4619 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4620 gst_pad_push_event (stream->channelpad[0],
4621 gst_event_new_caps (caps));
4623 gst_pad_send_event (stream->channelpad[0],
4624 gst_event_new_caps (caps));
4626 stream->need_caps = FALSE;
4630 stream->need_caps = FALSE;
4633 if (stream->discont && !is_rtcp) {
4634 /* mark first RTP buffer as discont */
4635 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4636 stream->discont = FALSE;
4637 /* first buffer gets the timestamp, other buffers are not timestamped and
4638 * their presentation time will be interpollated from the rtp timestamps. */
4639 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4640 GST_TIME_ARGS (src->base_time));
4642 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4645 /* chain to the peer pad */
4646 if (GST_PAD_IS_SINK (outpad))
4647 ret = gst_pad_chain (outpad, buf);
4649 ret = gst_pad_push (outpad, buf);
4652 /* combine all stream flows for the data transport */
4653 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4660 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4661 gst_rtsp_message_unset (message);
4666 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4667 ("Short message received, ignoring."));
4668 gst_rtsp_message_unset (message);
4673 static GstFlowReturn
4674 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4676 GstRTSPMessage message = { 0 };
4678 GstFlowReturn ret = GST_FLOW_OK;
4679 GTimeVal tv_timeout;
4682 /* get the next timeout interval */
4683 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4685 /* see if the timeout period expired */
4686 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4687 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4688 /* send keep-alive, only act on interrupt, a warning will be posted for
4690 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4692 /* get new timeout */
4693 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4696 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4697 tv_timeout.tv_sec, tv_timeout.tv_usec);
4699 /* protect the connection with the connection lock so that we can see when
4700 * we are finished doing server communication */
4702 gst_rtspsrc_connection_receive (src, &src->conninfo,
4703 &message, src->ptcp_timeout);
4707 GST_DEBUG_OBJECT (src, "we received a server message");
4709 case GST_RTSP_EINTR:
4710 /* we got interrupted this means we need to stop */
4712 case GST_RTSP_ETIMEOUT:
4713 /* no reply, send keep alive */
4714 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4715 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4719 /* go EOS when the server closed the connection */
4725 switch (message.type) {
4726 case GST_RTSP_MESSAGE_REQUEST:
4727 /* server sends us a request message, handle it */
4728 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
4729 if (res == GST_RTSP_EEOF)
4732 goto handle_request_failed;
4734 case GST_RTSP_MESSAGE_RESPONSE:
4735 /* we ignore response messages */
4736 GST_DEBUG_OBJECT (src, "ignoring response message");
4738 gst_rtsp_message_dump (&message);
4740 case GST_RTSP_MESSAGE_DATA:
4741 GST_DEBUG_OBJECT (src, "got data message");
4742 ret = gst_rtspsrc_handle_data (src, &message);
4743 if (ret != GST_FLOW_OK)
4744 goto handle_data_failed;
4747 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4752 g_assert_not_reached ();
4757 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4758 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4759 ("The server closed the connection."));
4760 src->conninfo.connected = FALSE;
4761 gst_rtsp_message_unset (&message);
4762 return GST_FLOW_EOS;
4766 gst_rtsp_message_unset (&message);
4767 GST_DEBUG_OBJECT (src, "got interrupted");
4768 return GST_FLOW_FLUSHING;
4772 gchar *str = gst_rtsp_strresult (res);
4774 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4775 ("Could not receive message. (%s)", str));
4778 gst_rtsp_message_unset (&message);
4779 return GST_FLOW_ERROR;
4781 handle_request_failed:
4783 gchar *str = gst_rtsp_strresult (res);
4785 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4786 ("Could not handle server message. (%s)", str));
4788 gst_rtsp_message_unset (&message);
4789 return GST_FLOW_ERROR;
4793 GST_DEBUG_OBJECT (src, "could no handle data message");
4798 static GstFlowReturn
4799 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4802 GstRTSPMessage message = { 0 };
4806 GTimeVal tv_timeout;
4808 /* get the next timeout interval */
4809 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4811 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4812 (gint) tv_timeout.tv_sec);
4814 gst_rtsp_message_unset (&message);
4816 /* we should continue reading the TCP socket because the server might
4817 * send us requests. When the session timeout expires, we need to send a
4818 * keep-alive request to keep the session open. */
4819 res = gst_rtspsrc_connection_receive (src, &src->conninfo,
4820 &message, &tv_timeout);
4824 GST_DEBUG_OBJECT (src, "we received a server message");
4826 case GST_RTSP_EINTR:
4827 /* we got interrupted, see what we have to do */
4829 case GST_RTSP_ETIMEOUT:
4830 /* send keep-alive, ignore the result, a warning will be posted. */
4831 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4832 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4836 /* server closed the connection. not very fatal for UDP, reconnect and
4837 * see what happens. */
4838 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4839 ("The server closed the connection."));
4840 if (src->udp_reconnect) {
4842 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4849 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4851 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4852 ("Unhandled return value %d.", res));
4856 switch (message.type) {
4857 case GST_RTSP_MESSAGE_REQUEST:
4858 /* server sends us a request message, handle it */
4859 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
4860 if (res == GST_RTSP_EEOF)
4863 goto handle_request_failed;
4865 case GST_RTSP_MESSAGE_RESPONSE:
4866 /* we ignore response and data messages */
4867 GST_DEBUG_OBJECT (src, "ignoring response message");
4869 gst_rtsp_message_dump (&message);
4870 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4871 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4872 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4873 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4874 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4881 case GST_RTSP_MESSAGE_DATA:
4882 /* we ignore response and data messages */
4883 GST_DEBUG_OBJECT (src, "ignoring data message");
4886 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4891 g_assert_not_reached ();
4893 /* we get here when the connection got interrupted */
4896 gst_rtsp_message_unset (&message);
4897 GST_DEBUG_OBJECT (src, "got interrupted");
4898 return GST_FLOW_FLUSHING;
4902 gchar *str = gst_rtsp_strresult (res);
4905 src->conninfo.connected = FALSE;
4906 if (res != GST_RTSP_EINTR) {
4907 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4908 ("Could not connect to server. (%s)", str));
4910 ret = GST_FLOW_ERROR;
4912 ret = GST_FLOW_FLUSHING;
4918 gchar *str = gst_rtsp_strresult (res);
4920 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4921 ("Could not receive message. (%s)", str));
4923 return GST_FLOW_ERROR;
4925 handle_request_failed:
4927 gchar *str = gst_rtsp_strresult (res);
4930 gst_rtsp_message_unset (&message);
4931 if (res != GST_RTSP_EINTR) {
4932 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4933 ("Could not handle server message. (%s)", str));
4935 ret = GST_FLOW_ERROR;
4937 ret = GST_FLOW_FLUSHING;
4943 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4944 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4945 ("The server closed the connection."));
4946 src->conninfo.connected = FALSE;
4947 gst_rtsp_message_unset (&message);
4948 return GST_FLOW_EOS;
4952 static GstRTSPResult
4953 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4955 GstRTSPResult res = GST_RTSP_OK;
4958 GST_DEBUG_OBJECT (src, "doing reconnect");
4960 GST_OBJECT_LOCK (src);
4961 /* only restart when the pads were not yet activated, else we were
4962 * streaming over UDP */
4963 restart = src->need_activate;
4964 GST_OBJECT_UNLOCK (src);
4966 /* no need to restart, we're done */
4970 /* we can try only TCP now */
4971 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
4973 /* close and cleanup our state */
4974 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
4977 /* see if we have TCP left to try. Also don't try TCP when we were configured
4979 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
4982 /* We post a warning message now to inform the user
4983 * that nothing happened. It's most likely a firewall thing. */
4984 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4985 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4986 "firewall is blocking it. Retrying using a tcp connection.",
4987 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
4989 /* open new connection using tcp */
4990 if (gst_rtspsrc_open (src, async) < 0)
4993 /* start playback */
4994 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
5003 src->cur_protocols = 0;
5004 /* no transport possible, post an error and stop */
5005 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5006 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5007 "firewall is blocking it. No other protocols to try.",
5008 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
5009 return GST_RTSP_ERROR;
5013 GST_DEBUG_OBJECT (src, "open failed");
5018 GST_DEBUG_OBJECT (src, "play failed");
5024 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
5028 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
5031 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
5034 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
5037 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
5045 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
5049 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
5052 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
5055 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
5058 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
5066 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
5070 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
5073 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
5076 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
5079 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
5087 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
5091 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
5094 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
5097 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
5100 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
5108 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
5110 if (ret == GST_RTSP_OK)
5111 gst_rtspsrc_loop_complete_cmd (src, cmd);
5112 else if (ret == GST_RTSP_EINTR)
5113 gst_rtspsrc_loop_cancel_cmd (src, cmd);
5115 gst_rtspsrc_loop_error_cmd (src, cmd);
5119 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
5122 gboolean flushed = FALSE;
5124 /* start new request */
5125 gst_rtspsrc_loop_start_cmd (src, cmd);
5127 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
5129 GST_OBJECT_LOCK (src);
5130 old = src->pending_cmd;
5131 if (old == CMD_RECONNECT) {
5132 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
5133 cmd = CMD_RECONNECT;
5134 } else if (old == CMD_CLOSE) {
5135 /* our CMD_CLOSE might have interrutped CMD_LOOP. gst_rtspsrc_loop
5136 * will send a CMD_WAIT which would cancel our pending CMD_CLOSE (if
5137 * still pending). We just avoid it here by making sure CMD_CLOSE is
5138 * still the pending command. */
5139 GST_DEBUG_OBJECT (src, "ignore, we were closing");
5141 } else if (old != CMD_WAIT) {
5142 src->pending_cmd = CMD_WAIT;
5143 GST_OBJECT_UNLOCK (src);
5144 /* cancel previous request */
5145 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
5146 gst_rtspsrc_loop_cancel_cmd (src, old);
5147 GST_OBJECT_LOCK (src);
5149 src->pending_cmd = cmd;
5150 /* interrupt if allowed */
5151 if (src->busy_cmd & mask) {
5152 GST_DEBUG_OBJECT (src, "connection flush busy %s",
5153 cmd_to_string (src->busy_cmd));
5154 gst_rtspsrc_connection_flush (src, TRUE);
5157 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
5158 cmd_to_string (src->busy_cmd));
5161 gst_task_start (src->task);
5162 GST_OBJECT_UNLOCK (src);
5168 gst_rtspsrc_loop (GstRTSPSrc * src)
5172 if (!src->conninfo.connection || !src->conninfo.connected)
5175 if (src->interleaved)
5176 ret = gst_rtspsrc_loop_interleaved (src);
5178 ret = gst_rtspsrc_loop_udp (src);
5180 if (ret != GST_FLOW_OK)
5188 GST_WARNING_OBJECT (src, "we are not connected");
5189 ret = GST_FLOW_FLUSHING;
5194 const gchar *reason = gst_flow_get_name (ret);
5196 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
5197 src->running = FALSE;
5198 if (ret == GST_FLOW_EOS) {
5199 /* perform EOS logic */
5200 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5201 gst_element_post_message (GST_ELEMENT_CAST (src),
5202 gst_message_new_segment_done (GST_OBJECT_CAST (src),
5203 src->segment.format, src->segment.position));
5204 gst_rtspsrc_push_event (src,
5205 gst_event_new_segment_done (src->segment.format,
5206 src->segment.position));
5208 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5210 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
5211 /* for fatal errors we post an error message, post the error before the
5212 * EOS so the app knows about the error first. */
5213 GST_ELEMENT_FLOW_ERROR (src, ret);
5214 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5216 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
5221 #ifndef GST_DISABLE_GST_DEBUG
5222 static const gchar *
5223 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
5227 while (method != 0) {
5244 /* Parse a WWW-Authenticate Response header and determine the
5245 * available authentication methods
5247 * This code should also cope with the fact that each WWW-Authenticate
5248 * header can contain multiple challenge methods + tokens
5250 * At the moment, for Basic auth, we just do a minimal check and don't
5251 * even parse out the realm */
5253 gst_rtspsrc_parse_auth_hdr (GstRTSPMessage * response,
5254 GstRTSPAuthMethod * methods, GstRTSPConnection * conn, gboolean * stale)
5256 GstRTSPAuthCredential **credentials, **credential;
5258 g_return_if_fail (response != NULL);
5259 g_return_if_fail (methods != NULL);
5260 g_return_if_fail (stale != NULL);
5263 gst_rtsp_message_parse_auth_credentials (response,
5264 GST_RTSP_HDR_WWW_AUTHENTICATE);
5268 credential = credentials;
5269 while (*credential) {
5270 if ((*credential)->scheme == GST_RTSP_AUTH_BASIC) {
5271 *methods |= GST_RTSP_AUTH_BASIC;
5272 } else if ((*credential)->scheme == GST_RTSP_AUTH_DIGEST) {
5273 GstRTSPAuthParam **param = (*credential)->params;
5275 *methods |= GST_RTSP_AUTH_DIGEST;
5277 gst_rtsp_connection_clear_auth_params (conn);
5281 if (strcmp ((*param)->name, "stale") == 0
5282 && g_ascii_strcasecmp ((*param)->value, "TRUE") == 0)
5284 gst_rtsp_connection_set_auth_param (conn, (*param)->name,
5293 gst_rtsp_auth_credentials_free (credentials);
5297 * gst_rtspsrc_setup_auth:
5298 * @src: the rtsp source
5300 * Configure a username and password and auth method on the
5301 * connection object based on a response we received from the
5304 * Currently, this requires that a username and password were supplied
5305 * in the uri. In the future, they may be requested on demand by sending
5306 * a message up the bus.
5308 * Returns: TRUE if authentication information could be set up correctly.
5311 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5315 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5316 GstRTSPAuthMethod method;
5317 GstRTSPResult auth_result;
5319 GstRTSPConnection *conn;
5320 gboolean stale = FALSE;
5322 conn = src->conninfo.connection;
5324 /* Identify the available auth methods and see if any are supported */
5325 gst_rtspsrc_parse_auth_hdr (response, &avail_methods, conn, &stale);
5327 if (avail_methods == GST_RTSP_AUTH_NONE)
5328 goto no_auth_available;
5330 /* For digest auth, if the response indicates that the session
5331 * data are stale, we just update them in the connection object and
5332 * return TRUE to retry the request */
5334 src->tried_url_auth = FALSE;
5336 url = gst_rtsp_connection_get_url (conn);
5338 /* Do we have username and password available? */
5339 if (url != NULL && !src->tried_url_auth && url->user != NULL
5340 && url->passwd != NULL) {
5343 src->tried_url_auth = TRUE;
5344 GST_DEBUG_OBJECT (src,
5345 "Attempting authentication using credentials from the URL");
5347 user = src->user_id;
5348 pass = src->user_pw;
5349 GST_DEBUG_OBJECT (src,
5350 "Attempting authentication using credentials from the properties");
5353 /* FIXME: If the url didn't contain username and password or we tried them
5354 * already, request a username and passwd from the application via some kind
5355 * of credentials request message */
5357 /* If we don't have a username and passwd at this point, bail out. */
5358 if (user == NULL || pass == NULL)
5361 /* Try to configure for each available authentication method, strongest to
5363 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5364 /* Check if this method is available on the server */
5365 if ((method & avail_methods) == 0)
5368 /* Pass the credentials to the connection to try on the next request */
5369 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5370 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5371 * ignore it and end up retrying later */
5372 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5373 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5374 gst_rtsp_auth_method_to_string (method));
5379 if (method == GST_RTSP_AUTH_NONE)
5380 goto no_auth_available;
5386 /* Output an error indicating that we couldn't connect because there were
5387 * no supported authentication protocols */
5388 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5389 ("No supported authentication protocol was found"));
5394 /* We don't fire an error message, we just return FALSE and let the
5395 * normal NOT_AUTHORIZED error be propagated */
5400 static GstRTSPResult
5401 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
5402 GstRTSPMessage * request, GstRTSPMessage * response,
5403 GstRTSPStatusCode * code)
5406 GstRTSPStatusCode thecode;
5407 gchar *content_base = NULL;
5411 if (!src->short_header)
5412 gst_rtsp_ext_list_before_send (src->extensions, request);
5414 GST_DEBUG_OBJECT (src, "sending message");
5417 gst_rtsp_message_dump (request);
5419 res = gst_rtspsrc_connection_send (src, conninfo, request, src->ptcp_timeout);
5423 gst_rtsp_connection_reset_timeout (conninfo->connection);
5427 gst_rtspsrc_connection_receive (src, conninfo, response,
5433 gst_rtsp_message_dump (response);
5435 switch (response->type) {
5436 case GST_RTSP_MESSAGE_REQUEST:
5437 res = gst_rtspsrc_handle_request (src, conninfo, response);
5438 if (res == GST_RTSP_EEOF)
5441 goto handle_request_failed;
5443 case GST_RTSP_MESSAGE_RESPONSE:
5444 /* ok, a response is good */
5445 GST_DEBUG_OBJECT (src, "received response message");
5447 case GST_RTSP_MESSAGE_DATA:
5448 /* get next response */
5449 GST_DEBUG_OBJECT (src, "handle data response message");
5450 gst_rtspsrc_handle_data (src, response);
5453 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5458 thecode = response->type_data.response.code;
5460 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5462 /* if the caller wanted the result code, we store it. */
5466 /* If the request didn't succeed, bail out before doing any more */
5467 if (thecode != GST_RTSP_STS_OK)
5470 /* store new content base if any */
5471 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5474 g_free (src->content_base);
5475 src->content_base = g_strdup (content_base);
5477 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5484 gchar *str = gst_rtsp_strresult (res);
5486 if (res != GST_RTSP_EINTR) {
5487 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5488 ("Could not send message. (%s)", str));
5490 GST_WARNING_OBJECT (src, "send interrupted");
5499 GST_WARNING_OBJECT (src, "server closed connection");
5500 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5502 /* if reconnect succeeds, try again */
5504 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5508 /* only try once after reconnect, then fallthrough and error out */
5511 gchar *str = gst_rtsp_strresult (res);
5513 if (res != GST_RTSP_EINTR) {
5514 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5515 ("Could not receive message. (%s)", str));
5517 GST_WARNING_OBJECT (src, "receive interrupted");
5525 handle_request_failed:
5527 /* ERROR was posted */
5528 gst_rtsp_message_unset (response);
5533 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5534 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5535 ("The server closed the connection."));
5536 gst_rtsp_message_unset (response);
5543 * @src: the rtsp source
5544 * @conn: the connection to send on
5545 * @request: must point to a valid request
5546 * @response: must point to an empty #GstRTSPMessage
5547 * @code: an optional code result
5549 * send @request and retrieve the response in @response. optionally @code can be
5550 * non-NULL in which case it will contain the status code of the response.
5552 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5553 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5555 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5556 * @response message) if the response code was not 200 (OK).
5558 * If the attempt results in an authentication failure, then this will attempt
5559 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5562 * Returns: #GST_RTSP_OK if the processing was successful.
5564 static GstRTSPResult
5565 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
5566 GstRTSPMessage * request, GstRTSPMessage * response,
5567 GstRTSPStatusCode * code)
5569 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5570 GstRTSPResult res = GST_RTSP_ERROR;
5573 GstRTSPMethod method = GST_RTSP_INVALID;
5579 /* make sure we don't loop forever */
5583 /* save method so we can disable it when the server complains */
5584 method = request->type_data.request.method;
5587 gst_rtspsrc_try_send (src, conninfo, request, response,
5592 case GST_RTSP_STS_UNAUTHORIZED:
5593 case GST_RTSP_STS_NOT_FOUND:
5594 if (gst_rtspsrc_setup_auth (src, response)) {
5595 /* Try the request/response again after configuring the auth info
5603 } while (retry == TRUE);
5605 /* If the user requested the code, let them handle errors, otherwise
5606 * post an error below */
5609 else if (int_code != GST_RTSP_STS_OK)
5610 goto error_response;
5617 GST_DEBUG_OBJECT (src, "got error %d", res);
5622 res = GST_RTSP_ERROR;
5624 switch (response->type_data.response.code) {
5625 case GST_RTSP_STS_NOT_FOUND:
5626 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_FOUND,
5629 case GST_RTSP_STS_UNAUTHORIZED:
5630 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_AUTHORIZED,
5633 case GST_RTSP_STS_MOVED_PERMANENTLY:
5634 case GST_RTSP_STS_MOVE_TEMPORARILY:
5636 gchar *new_location;
5637 GstRTSPLowerTrans transports;
5639 GST_DEBUG_OBJECT (src, "got redirection");
5640 /* if we don't have a Location Header, we must error */
5641 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5642 &new_location, 0) < 0)
5645 /* When we receive a redirect result, we go back to the INIT state after
5646 * parsing the new URI. The caller should do the needed steps to issue
5647 * a new setup when it detects this state change. */
5648 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5650 /* save current transports */
5651 if (src->conninfo.url)
5652 transports = src->conninfo.url->transports;
5654 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5656 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5658 /* set old transports */
5659 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5660 src->conninfo.url->transports = transports;
5662 src->need_redirect = TRUE;
5666 case GST_RTSP_STS_NOT_ACCEPTABLE:
5667 case GST_RTSP_STS_NOT_IMPLEMENTED:
5668 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5669 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5670 gst_rtsp_method_as_text (method));
5671 src->methods &= ~method;
5675 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, READ,
5679 /* if we return ERROR we should unset the response ourselves */
5680 if (res == GST_RTSP_ERROR)
5681 gst_rtsp_message_unset (response);
5687 static GstRTSPResult
5688 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5689 GstRTSPMessage * response, GstRTSPSrc * src)
5691 return gst_rtspsrc_send (src, &src->conninfo, request, response, NULL);
5695 /* parse the response and collect all the supported methods. We need this
5696 * information so that we don't try to send an unsupported request to the
5700 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5702 GstRTSPHeaderField field;
5706 /* reset supported methods */
5709 /* Try Allow Header first */
5710 field = GST_RTSP_HDR_ALLOW;
5713 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5714 if (indx == 0 && !respoptions) {
5715 /* if no Allow header was found then try the Public header... */
5716 field = GST_RTSP_HDR_PUBLIC;
5717 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5722 src->methods |= gst_rtsp_options_from_text (respoptions);
5727 if (src->methods == 0) {
5728 /* neither Allow nor Public are required, assume the server supports
5729 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5731 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5732 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5734 /* always assume PLAY, FIXME, extensions should be able to override
5736 src->methods |= GST_RTSP_PLAY;
5737 /* also assume it will support Range */
5738 src->seekable = TRUE;
5740 /* we need describe and setup */
5741 if (!(src->methods & GST_RTSP_DESCRIBE))
5743 if (!(src->methods & GST_RTSP_SETUP))
5751 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5752 ("Server does not support DESCRIBE."));
5757 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5758 ("Server does not support SETUP."));
5763 /* masks to be kept in sync with the hardcoded protocol order of preference
5765 static const guint protocol_masks[] = {
5766 GST_RTSP_LOWER_TRANS_UDP,
5767 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5768 GST_RTSP_LOWER_TRANS_TCP,
5772 static GstRTSPResult
5773 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5774 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
5778 gboolean add_udp_str;
5783 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5788 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5790 /* extension listed transports, use those */
5791 if (*transports != NULL)
5794 /* it's the default */
5795 add_udp_str = FALSE;
5797 /* the default RTSP transports */
5798 result = g_string_new ("RTP");
5801 case GST_RTSP_PROFILE_AVP:
5802 g_string_append (result, "/AVP");
5804 case GST_RTSP_PROFILE_SAVP:
5805 g_string_append (result, "/SAVP");
5807 case GST_RTSP_PROFILE_AVPF:
5808 g_string_append (result, "/AVPF");
5810 case GST_RTSP_PROFILE_SAVPF:
5811 g_string_append (result, "/SAVPF");
5817 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5818 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5820 g_string_append (result, "/UDP");
5821 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5822 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5823 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5824 /* we don't have to allocate any UDP ports yet, if the selected transport
5825 * turns out to be multicast we can create them and join the multicast
5826 * group indicated in the transport reply */
5828 g_string_append (result, "/UDP");
5829 g_string_append (result, ";multicast");
5830 if (src->next_port_num != 0) {
5831 if (src->client_port_range.max > 0 &&
5832 src->next_port_num >= src->client_port_range.max)
5835 g_string_append_printf (result, ";client_port=%d-%d",
5836 src->next_port_num, src->next_port_num + 1);
5838 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
5839 GST_DEBUG_OBJECT (src, "adding TCP");
5841 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
5843 *transports = g_string_free (result, FALSE);
5845 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5852 GST_ERROR ("extension gave error %d", res);
5857 GST_ERROR ("no more ports available");
5858 return GST_RTSP_ERROR;
5862 static GstRTSPResult
5863 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5864 gint orig_rtpport, gint orig_rtcpport)
5867 gint nr_udp, nr_int;
5869 gint rtpport = 0, rtcpport = 0;
5872 src = stream->parent;
5874 /* find number of placeholders first */
5875 if (strstr (*transports, "%%i2"))
5877 else if (strstr (*transports, "%%i1"))
5882 if (strstr (*transports, "%%u2"))
5884 else if (strstr (*transports, "%%u1"))
5889 if (nr_udp == 0 && nr_int == 0)
5893 if (!orig_rtpport || !orig_rtcpport) {
5894 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5897 rtpport = orig_rtpport;
5898 rtcpport = orig_rtcpport;
5902 str = g_string_new ("");
5904 while ((next = strstr (p, "%%"))) {
5905 g_string_append_len (str, p, next - p);
5906 if (next[2] == 'u') {
5908 g_string_append_printf (str, "%d", rtpport);
5909 else if (next[3] == '2')
5910 g_string_append_printf (str, "%d", rtcpport);
5912 if (next[2] == 'i') {
5914 g_string_append_printf (str, "%d", src->free_channel);
5915 else if (next[3] == '2')
5916 g_string_append_printf (str, "%d", src->free_channel + 1);
5921 /* append final part */
5922 g_string_append (str, p);
5924 g_free (*transports);
5925 *transports = g_string_free (str, FALSE);
5933 GST_ERROR ("failed to allocate udp ports");
5934 return GST_RTSP_ERROR;
5939 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
5941 GstCaps *caps = NULL;
5943 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
5947 GST_DEBUG_OBJECT (src, "SRTP parameters received");
5953 default_srtcp_params (void)
5960 guint data_size = GST_ROUND_UP_4 (KEY_SIZE);
5962 /* create a random key */
5963 key_data = g_malloc (data_size);
5964 for (i = 0; i < data_size; i += 4)
5965 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
5967 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
5969 caps = gst_caps_new_simple ("application/x-srtcp",
5970 "srtp-key", GST_TYPE_BUFFER, buf,
5971 "srtp-cipher", G_TYPE_STRING, "aes-128-icm",
5972 "srtp-auth", G_TYPE_STRING, "hmac-sha1-80",
5973 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
5974 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
5976 gst_buffer_unref (buf);
5982 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
5984 gchar *base64, *result = NULL;
5985 GstMIKEYMessage *mikey_msg;
5987 stream->srtcpparams = signal_get_srtcp_params (src, stream);
5988 if (stream->srtcpparams == NULL)
5989 stream->srtcpparams = default_srtcp_params ();
5991 mikey_msg = gst_mikey_message_new_from_caps (stream->srtcpparams);
5993 /* add policy '0' for our SSRC */
5994 gst_mikey_message_add_cs_srtp (mikey_msg, 0, stream->send_ssrc, 0);
5996 base64 = gst_mikey_message_base64_encode (mikey_msg);
5997 gst_mikey_message_unref (mikey_msg);
6000 result = gst_sdp_make_keymgmt (stream->conninfo.location, base64);
6008 /* Perform the SETUP request for all the streams.
6010 * We ask the server for a specific transport, which initially includes all the
6011 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
6012 * two local UDP ports that we send to the server.
6014 * Once the server replied with a transport, we configure the other streams
6015 * with the same transport.
6017 * This function will also configure the stream for the selected transport,
6018 * which basically means creating the pipeline.
6020 static GstRTSPResult
6021 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
6024 GstRTSPResult res = GST_RTSP_ERROR;
6025 GstRTSPMessage request = { 0 };
6026 GstRTSPMessage response = { 0 };
6027 GstRTSPStream *stream = NULL;
6028 GstRTSPLowerTrans protocols;
6029 GstRTSPStatusCode code;
6030 gboolean unsupported_real = FALSE;
6031 gint rtpport, rtcpport;
6035 if (src->conninfo.connection) {
6036 url = gst_rtsp_connection_get_url (src->conninfo.connection);
6037 /* we initially allow all configured lower transports. based on the URL
6038 * transports and the replies from the server we narrow them down. */
6039 protocols = url->transports & src->cur_protocols;
6042 protocols = src->cur_protocols;
6048 /* reset some state */
6049 src->free_channel = 0;
6050 src->interleaved = FALSE;
6051 src->need_activate = FALSE;
6052 /* keep track of next port number, 0 is random */
6053 src->next_port_num = src->client_port_range.min;
6054 rtpport = rtcpport = 0;
6056 if (G_UNLIKELY (src->streams == NULL))
6059 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6060 GstRTSPConnInfo *conninfo;
6067 stream = (GstRTSPStream *) walk->data;
6069 caps = stream_get_caps_for_pt (stream, stream->default_pt);
6071 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
6075 if (stream->skipped) {
6076 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
6080 /* see if we need to configure this stream */
6081 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
6082 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
6087 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
6088 stream->id, caps, &selected);
6090 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
6094 /* merge/overwrite global caps */
6099 s = gst_caps_get_structure (caps, 0);
6101 num = gst_structure_n_fields (src->props);
6102 for (j = 0; j < num; j++) {
6106 name = gst_structure_nth_field_name (src->props, j);
6107 val = gst_structure_get_value (src->props, name);
6108 gst_structure_set_value (s, name, val);
6110 GST_DEBUG_OBJECT (src, "copied %s", name);
6114 /* skip setup if we have no URL for it */
6115 if (stream->conninfo.location == NULL) {
6116 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
6120 if (src->conninfo.connection == NULL) {
6121 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
6122 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
6125 conninfo = &stream->conninfo;
6127 conninfo = &src->conninfo;
6129 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
6130 stream->conninfo.location);
6132 /* if we have a multicast connection, only suggest multicast from now on */
6133 if (stream->is_multicast)
6134 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
6137 /* first selectable protocol */
6138 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6140 if (!protocol_masks[mask])
6144 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
6145 protocol_masks[mask]);
6146 /* create a string with first transport in line */
6148 res = gst_rtspsrc_create_transports_string (src,
6149 protocols & protocol_masks[mask], stream->profile, &transports);
6150 if (res < 0 || transports == NULL)
6151 goto setup_transport_failed;
6153 if (strlen (transports) == 0) {
6154 g_free (transports);
6155 GST_DEBUG_OBJECT (src, "no transports found");
6160 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
6162 /* replace placeholders with real values, this function will optionally
6163 * allocate UDP ports and other info needed to execute the setup request */
6164 res = gst_rtspsrc_prepare_transports (stream, &transports,
6165 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
6167 g_free (transports);
6168 goto setup_transport_failed;
6171 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
6173 /* create SETUP request */
6175 gst_rtspsrc_init_request (src, &request, GST_RTSP_SETUP,
6176 stream->conninfo.location);
6178 g_free (transports);
6179 goto create_request_failed;
6182 /* select transport */
6183 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
6186 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
6187 stream->profile == GST_RTSP_PROFILE_SAVPF) {
6188 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
6189 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
6192 /* if the user wants a non default RTP packet size we add the blocksize
6194 if (src->rtp_blocksize > 0) {
6195 hval = g_strdup_printf ("%d", src->rtp_blocksize);
6196 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
6200 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
6203 /* handle the code ourselves */
6204 res = gst_rtspsrc_send (src, conninfo, &request, &response, &code);
6209 case GST_RTSP_STS_OK:
6211 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
6212 gst_rtsp_message_unset (&request);
6213 gst_rtsp_message_unset (&response);
6214 /* cleanup of leftover transport */
6215 gst_rtspsrc_stream_free_udp (stream);
6216 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
6217 * we might be in this case */
6218 if (stream->container && rtpport && rtcpport && !retry) {
6219 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
6224 /* this transport did not go down well, but we may have others to try
6225 * that we did not send yet, try those and only give up then
6226 * but not without checking for lost cause/extension so we can
6227 * post a nicer/more useful error message later */
6228 if (!unsupported_real)
6229 unsupported_real = stream->is_real;
6230 /* select next available protocol, give up on this stream if none */
6232 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6234 if (!protocol_masks[mask] || unsupported_real)
6239 /* cleanup of leftover transport and move to the next stream */
6240 gst_rtspsrc_stream_free_udp (stream);
6241 goto response_error;
6244 /* parse response transport */
6246 gchar *resptrans = NULL;
6247 GstRTSPTransport transport = { 0 };
6249 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
6252 gst_rtspsrc_stream_free_udp (stream);
6256 /* parse transport, go to next stream on parse error */
6257 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6258 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6262 /* update allowed transports for other streams. once the transport of
6263 * one stream has been determined, we make sure that all other streams
6264 * are configured in the same way */
6265 switch (transport.lower_transport) {
6266 case GST_RTSP_LOWER_TRANS_TCP:
6267 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6268 protocols = GST_RTSP_LOWER_TRANS_TCP;
6269 src->interleaved = TRUE;
6270 /* update free channels */
6272 MAX (transport.interleaved.min, src->free_channel);
6274 MAX (transport.interleaved.max, src->free_channel);
6275 src->free_channel++;
6277 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6278 /* only allow multicast for other streams */
6279 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6280 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6281 /* if the server selected our ports, increment our counters so that
6282 * we select a new port later */
6283 if (src->next_port_num == transport.port.min &&
6284 src->next_port_num + 1 == transport.port.max) {
6285 src->next_port_num += 2;
6288 case GST_RTSP_LOWER_TRANS_UDP:
6289 /* only allow unicast for other streams */
6290 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6291 protocols = GST_RTSP_LOWER_TRANS_UDP;
6294 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6295 transport.lower_transport);
6299 if (!src->interleaved || !retry) {
6300 /* now configure the stream with the selected transport */
6301 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6302 GST_DEBUG_OBJECT (src,
6303 "could not configure stream %p transport, skipping stream",
6306 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
6307 /* retain the first allocated UDP port pair */
6308 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
6309 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
6312 /* we need to activate at least one streams when we detect activity */
6313 src->need_activate = TRUE;
6315 /* stream is setup now */
6316 stream->setup = TRUE;
6321 GstRTSPStream *sskip;
6323 skip = g_list_next (skip);
6327 sskip = (GstRTSPStream *) skip->data;
6329 /* skip all streams with the same control url */
6330 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
6331 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
6332 sskip, sskip->conninfo.location);
6333 sskip->skipped = TRUE;
6338 /* clean up our transport struct */
6339 gst_rtsp_transport_init (&transport);
6340 /* clean up used RTSP messages */
6341 gst_rtsp_message_unset (&request);
6342 gst_rtsp_message_unset (&response);
6346 /* store the transport protocol that was configured */
6347 src->cur_protocols = protocols;
6349 gst_rtsp_ext_list_stream_select (src->extensions, url);
6351 /* if there is nothing to activate, error out */
6352 if (!src->need_activate)
6353 goto nothing_to_activate;
6360 /* no transport possible, post an error and stop */
6361 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6362 ("Could not connect to server, no protocols left"));
6363 return GST_RTSP_ERROR;
6367 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6368 ("SDP contains no streams"));
6369 return GST_RTSP_ERROR;
6371 create_request_failed:
6373 gchar *str = gst_rtsp_strresult (res);
6375 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6376 ("Could not create request. (%s)", str));
6380 setup_transport_failed:
6382 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6383 ("Could not setup transport."));
6384 res = GST_RTSP_ERROR;
6389 const gchar *str = gst_rtsp_status_as_text (code);
6391 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6392 ("Error (%d): %s", code, GST_STR_NULL (str)));
6393 res = GST_RTSP_ERROR;
6398 gchar *str = gst_rtsp_strresult (res);
6400 if (res != GST_RTSP_EINTR) {
6401 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6402 ("Could not send message. (%s)", str));
6404 GST_WARNING_OBJECT (src, "send interrupted");
6411 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6412 ("Server did not select transport."));
6413 res = GST_RTSP_ERROR;
6416 nothing_to_activate:
6418 /* none of the available error codes is really right .. */
6419 if (unsupported_real) {
6420 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6421 (_("No supported stream was found. You might need to install a "
6422 "GStreamer RTSP extension plugin for Real media streams.")),
6425 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6426 (_("No supported stream was found. You might need to allow "
6427 "more transport protocols or may otherwise be missing "
6428 "the right GStreamer RTSP extension plugin.")), (NULL));
6430 return GST_RTSP_ERROR;
6434 gst_rtsp_message_unset (&request);
6435 gst_rtsp_message_unset (&response);
6441 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
6442 GstSegment * segment)
6445 GstRTSPTimeRange *therange;
6448 gst_rtsp_range_free (src->range);
6450 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
6451 GST_DEBUG_OBJECT (src, "parsed range %s", range);
6452 src->range = therange;
6454 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6456 gst_segment_init (segment, GST_FORMAT_TIME);
6460 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6461 therange->min.type, therange->min.seconds, therange->max.type,
6462 therange->max.seconds);
6464 if (therange->min.type == GST_RTSP_TIME_NOW)
6466 else if (therange->min.type == GST_RTSP_TIME_END)
6469 seconds = therange->min.seconds * GST_SECOND;
6471 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6472 GST_TIME_ARGS (seconds));
6474 /* we need to start playback without clipping from the position reported by
6476 segment->start = seconds;
6477 segment->position = seconds;
6479 if (therange->max.type == GST_RTSP_TIME_NOW)
6481 else if (therange->max.type == GST_RTSP_TIME_END)
6484 seconds = therange->max.seconds * GST_SECOND;
6486 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6487 GST_TIME_ARGS (seconds));
6489 /* live (WMS) server might send overflowed large max as its idea of infinity,
6490 * compensate to prevent problems later on */
6491 if (seconds != -1 && seconds < 0) {
6493 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6496 /* live (WMS) might send min == max, which is not worth recording */
6497 if (segment->duration == -1 && seconds == segment->start)
6500 /* don't change duration with unknown value, we might have a valid value
6501 * there that we want to keep. */
6503 segment->duration = seconds;
6508 /* Parse clock profived by the server with following syntax:
6510 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6513 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6515 gboolean res = FALSE;
6517 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6518 gchar **fields = NULL, **parts = NULL;
6519 gchar *remote_ip, *str;
6521 GstClockTime base_time;
6524 fields = g_strsplit (gstclock, " ", 0);
6526 /* wrapped clock, not very interesting for now */
6527 if (fields[1] == NULL)
6530 /* remote IP address and port */
6531 if ((str = fields[2]) == NULL)
6534 parts = g_strsplit (str, ":", 0);
6536 if ((remote_ip = parts[0]) == NULL)
6539 if ((str = parts[1]) == NULL)
6547 if ((str = fields[3]) == NULL)
6550 base_time = g_ascii_strtoull (str, NULL, 10);
6553 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
6556 if (src->provided_clock)
6557 gst_object_unref (src->provided_clock);
6558 src->provided_clock = netclock;
6560 gst_element_post_message (GST_ELEMENT_CAST (src),
6561 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
6562 src->provided_clock, TRUE));
6566 g_strfreev (fields);
6572 /* must be called with the RTSP state lock */
6573 static GstRTSPResult
6574 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
6580 /* prepare global stream caps properties */
6582 gst_structure_remove_all_fields (src->props);
6584 src->props = gst_structure_new_empty ("RTSPProperties");
6587 gst_sdp_message_dump (sdp);
6589 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
6591 /* let the app inspect and change the SDP */
6592 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6594 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6596 /* parse range for duration reporting. */
6601 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6605 /* keep track of the range and configure it in the segment */
6606 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6610 /* parse clock information. This is GStreamer specific, a server can tell the
6611 * client what clock it is using and wrap that in a network clock. The
6612 * advantage of that is that we can slave to it. */
6614 const gchar *gstclock;
6617 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6618 if (gstclock == NULL)
6621 /* parse the clock and expose it in the provide_clock method */
6622 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6626 /* try to find a global control attribute. Note that a '*' means that we should
6627 * do aggregate control with the current url (so we don't do anything and
6628 * leave the current connection as is) */
6630 const gchar *control;
6633 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6634 if (control == NULL)
6637 /* only take fully qualified urls */
6638 if (g_str_has_prefix (control, "rtsp://"))
6642 g_free (src->conninfo.location);
6643 src->conninfo.location = g_strdup (control);
6644 /* make a connection for this, if there was a connection already, nothing
6646 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6647 GST_ERROR_OBJECT (src, "could not connect");
6650 /* we need to keep the control url separate from the connection url because
6651 * the rules for constructing the media control url need it */
6652 g_free (src->control);
6653 src->control = g_strdup (control);
6656 /* create streams */
6657 n_streams = gst_sdp_message_medias_len (sdp);
6658 for (i = 0; i < n_streams; i++) {
6659 gst_rtspsrc_create_stream (src, sdp, i, n_streams);
6662 src->state = GST_RTSP_STATE_INIT;
6665 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6668 /* reset our state */
6669 src->need_range = TRUE;
6672 src->state = GST_RTSP_STATE_READY;
6679 GST_ERROR_OBJECT (src, "setup failed");
6680 gst_rtspsrc_cleanup (src);
6685 static GstRTSPResult
6686 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6690 GstRTSPMessage request = { 0 };
6691 GstRTSPMessage response = { 0 };
6694 gchar *respcont = NULL;
6697 src->need_redirect = FALSE;
6699 /* can't continue without a valid url */
6700 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6701 res = GST_RTSP_EINVAL;
6704 src->tried_url_auth = FALSE;
6706 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6707 goto connect_failed;
6709 /* create OPTIONS */
6710 GST_DEBUG_OBJECT (src, "create options...");
6712 gst_rtspsrc_init_request (src, &request, GST_RTSP_OPTIONS,
6713 src->conninfo.url_str);
6715 goto create_request_failed;
6718 GST_DEBUG_OBJECT (src, "send options...");
6721 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6724 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
6729 if (!gst_rtspsrc_parse_methods (src, &response))
6732 /* create DESCRIBE */
6733 GST_DEBUG_OBJECT (src, "create describe...");
6735 gst_rtspsrc_init_request (src, &request, GST_RTSP_DESCRIBE,
6736 src->conninfo.url_str);
6738 goto create_request_failed;
6740 /* we only accept SDP for now */
6741 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
6745 GST_DEBUG_OBJECT (src, "send describe...");
6748 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
6751 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
6755 /* we only perform redirect for describe and play, currently */
6756 if (src->need_redirect) {
6757 /* close connection, we don't have to send a TEARDOWN yet, ignore the
6759 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6761 gst_rtsp_message_unset (&request);
6762 gst_rtsp_message_unset (&response);
6768 /* it could be that the DESCRIBE method was not implemented */
6769 if (!(src->methods & GST_RTSP_DESCRIBE))
6772 /* check if reply is SDP */
6773 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
6775 /* could not be set but since the request returned OK, we assume it
6776 * was SDP, else check it. */
6778 const gchar *props = strchr (respcont, ';');
6781 gchar *mimetype = g_strndup (respcont, props - respcont);
6783 mimetype = g_strstrip (mimetype);
6784 if (g_ascii_strcasecmp (mimetype, "application/sdp") != 0) {
6786 goto wrong_content_type;
6789 /* TODO: Check for charset property and do conversions of all messages if
6790 * needed. Some servers actually send that property */
6793 } else if (g_ascii_strcasecmp (respcont, "application/sdp") != 0) {
6794 goto wrong_content_type;
6798 /* get message body and parse as SDP */
6799 gst_rtsp_message_get_body (&response, &data, &size);
6800 if (data == NULL || size == 0)
6803 GST_DEBUG_OBJECT (src, "parse SDP...");
6804 gst_sdp_message_new (sdp);
6805 gst_sdp_message_parse_buffer (data, size, *sdp);
6807 /* clean up any messages */
6808 gst_rtsp_message_unset (&request);
6809 gst_rtsp_message_unset (&response);
6816 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
6817 ("No valid RTSP URL was provided"));
6822 gchar *str = gst_rtsp_strresult (res);
6824 if (res != GST_RTSP_EINTR) {
6825 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6826 ("Failed to connect. (%s)", str));
6828 GST_WARNING_OBJECT (src, "connect interrupted");
6833 create_request_failed:
6835 gchar *str = gst_rtsp_strresult (res);
6837 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6838 ("Could not create request. (%s)", str));
6844 /* Don't post a message - the rtsp_send method will have
6845 * taken care of it because we passed NULL for the response code */
6850 /* error was posted */
6851 res = GST_RTSP_ERROR;
6856 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6857 ("Server does not support SDP, got %s.", respcont));
6858 res = GST_RTSP_ERROR;
6863 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6864 ("Server can not provide an SDP."));
6865 res = GST_RTSP_ERROR;
6870 if (src->conninfo.connection) {
6871 GST_DEBUG_OBJECT (src, "free connection");
6872 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6874 gst_rtsp_message_unset (&request);
6875 gst_rtsp_message_unset (&response);
6880 static GstRTSPResult
6881 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
6886 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
6888 if (src->sdp == NULL) {
6889 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
6893 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
6898 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
6905 GST_WARNING_OBJECT (src, "can't get sdp");
6906 src->open_error = TRUE;
6911 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
6912 src->open_error = TRUE;
6917 static GstRTSPResult
6918 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
6920 GstRTSPMessage request = { 0 };
6921 GstRTSPMessage response = { 0 };
6922 GstRTSPResult res = GST_RTSP_OK;
6924 const gchar *control;
6926 GST_DEBUG_OBJECT (src, "TEARDOWN...");
6928 gst_rtspsrc_set_state (src, GST_STATE_READY);
6930 if (src->state < GST_RTSP_STATE_READY) {
6931 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
6938 /* construct a control url */
6939 control = get_aggregate_control (src);
6941 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
6944 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6945 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6946 const gchar *setup_url;
6947 GstRTSPConnInfo *info;
6949 /* try aggregate control first but do non-aggregate control otherwise */
6951 setup_url = control;
6952 else if ((setup_url = stream->conninfo.location) == NULL)
6955 if (src->conninfo.connection) {
6956 info = &src->conninfo;
6957 } else if (stream->conninfo.connection) {
6958 info = &stream->conninfo;
6962 if (!info->connected)
6967 gst_rtspsrc_init_request (src, &request, GST_RTSP_TEARDOWN, setup_url);
6969 goto create_request_failed;
6972 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
6974 if ((res = gst_rtspsrc_send (src, info, &request, &response, NULL)) < 0)
6977 /* FIXME, parse result? */
6978 gst_rtsp_message_unset (&request);
6979 gst_rtsp_message_unset (&response);
6982 /* early exit when we did aggregate control */
6988 /* close connections */
6989 GST_DEBUG_OBJECT (src, "closing connection...");
6990 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6991 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6992 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6993 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
6997 gst_rtspsrc_cleanup (src);
6999 src->state = GST_RTSP_STATE_INVALID;
7002 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
7007 create_request_failed:
7009 gchar *str = gst_rtsp_strresult (res);
7011 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7012 ("Could not create request. (%s)", str));
7018 gchar *str = gst_rtsp_strresult (res);
7020 gst_rtsp_message_unset (&request);
7021 if (res != GST_RTSP_EINTR) {
7022 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7023 ("Could not send message. (%s)", str));
7025 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
7032 GST_DEBUG_OBJECT (src,
7033 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
7038 /* RTP-Info is of the format:
7040 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
7042 * rtptime corresponds to the timestamp for the NPT time given in the header
7043 * seqbase corresponds to the next sequence number we received. This number
7044 * indicates the first seqnum after the seek and should be used to discard
7045 * packets that are from before the seek.
7048 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
7053 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
7055 infos = g_strsplit (rtpinfo, ",", 0);
7056 for (i = 0; infos[i]; i++) {
7058 GstRTSPStream *stream;
7062 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
7064 /* init values, types of seqbase and timebase are bigger than needed so we
7065 * can store -1 as uninitialized values */
7070 /* parse url, find stream for url.
7071 * parse seq and rtptime. The seq number should be configured in the rtp
7072 * depayloader or session manager to detect gaps. Same for the rtptime, it
7073 * should be used to create an initial time newsegment. */
7074 fields = g_strsplit (infos[i], ";", 0);
7075 for (j = 0; fields[j]; j++) {
7076 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
7077 /* remove leading whitespace */
7078 fields[j] = g_strchug (fields[j]);
7079 if (g_str_has_prefix (fields[j], "url=")) {
7080 /* get the url and the stream */
7082 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
7083 } else if (g_str_has_prefix (fields[j], "seq=")) {
7084 seqbase = atoi (fields[j] + 4);
7085 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
7086 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
7089 g_strfreev (fields);
7090 /* now we need to store the values for the caps of the stream */
7091 if (stream != NULL) {
7092 GST_DEBUG_OBJECT (src,
7093 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
7094 stream, seqbase, timebase);
7096 /* we have a stream, configure detected params */
7097 stream->seqbase = seqbase;
7098 stream->timebase = timebase;
7107 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
7112 interval = strtoul (rtcp, NULL, 10);
7113 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
7118 interval *= GST_MSECOND;
7120 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7121 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7123 /* already (optionally) retrieved this when configuring manager */
7124 if (stream->session) {
7125 GObject *rtpsession = stream->session;
7127 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
7129 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
7133 /* now it happens that (Xenon) server sending this may also provide bogus
7134 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
7135 * and just use RTP-Info to sync */
7137 GObjectClass *klass;
7139 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
7140 if (g_object_class_find_property (klass, "rtcp-sync")) {
7141 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
7142 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
7148 gst_rtspsrc_get_float (const gchar * dstr)
7150 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7152 /* canonicalise floating point string so we can handle float strings
7153 * in the form "24.930" or "24,930" irrespective of the current locale */
7154 g_strlcpy (s, dstr, sizeof (s));
7155 g_strdelimit (s, ",", '.');
7156 return g_ascii_strtod (s, NULL);
7160 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
7162 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7164 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
7165 g_strlcpy (val_str, "now", sizeof (val_str));
7167 if (segment->position == 0) {
7168 g_strlcpy (val_str, "0", sizeof (val_str));
7170 g_ascii_dtostr (val_str, sizeof (val_str),
7171 ((gdouble) segment->position) / GST_SECOND);
7174 return g_strdup_printf ("npt=%s-", val_str);
7178 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
7182 stream->timebase = -1;
7183 stream->seqbase = -1;
7185 len = stream->ptmap->len;
7186 for (i = 0; i < len; i++) {
7187 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
7190 if (item->caps == NULL)
7193 item->caps = gst_caps_make_writable (item->caps);
7194 s = gst_caps_get_structure (item->caps, 0);
7195 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
7196 if (item->pt == stream->default_pt && stream->udpsrc[0])
7197 g_object_set (stream->udpsrc[0], "caps", item->caps, NULL);
7199 stream->need_caps = TRUE;
7202 static GstRTSPResult
7203 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
7205 GstRTSPResult res = GST_RTSP_OK;
7207 if (src->state < GST_RTSP_STATE_READY) {
7208 res = GST_RTSP_ERROR;
7209 if (src->open_error) {
7210 GST_DEBUG_OBJECT (src, "the stream was in error");
7214 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
7216 if ((res = gst_rtspsrc_open (src, async)) < 0) {
7217 GST_DEBUG_OBJECT (src, "failed to open stream");
7226 static GstRTSPResult
7227 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
7229 GstRTSPMessage request = { 0 };
7230 GstRTSPMessage response = { 0 };
7231 GstRTSPResult res = GST_RTSP_OK;
7235 const gchar *control;
7237 GST_DEBUG_OBJECT (src, "PLAY...");
7240 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7243 if (!(src->methods & GST_RTSP_PLAY))
7246 if (src->state == GST_RTSP_STATE_PLAYING)
7249 if (!src->conninfo.connection || !src->conninfo.connected)
7252 /* send some dummy packets before we activate the receive in the
7254 gst_rtspsrc_send_dummy_packets (src);
7256 /* require new SR packets */
7258 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
7260 /* construct a control url */
7261 control = get_aggregate_control (src);
7263 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7264 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7265 const gchar *setup_url;
7266 GstRTSPConnInfo *conninfo;
7268 /* try aggregate control first but do non-aggregate control otherwise */
7270 setup_url = control;
7271 else if ((setup_url = stream->conninfo.location) == NULL)
7274 if (src->conninfo.connection) {
7275 conninfo = &src->conninfo;
7276 } else if (stream->conninfo.connection) {
7277 conninfo = &stream->conninfo;
7283 res = gst_rtspsrc_init_request (src, &request, GST_RTSP_PLAY, setup_url);
7285 goto create_request_failed;
7287 if (src->need_range) {
7288 hval = gen_range_header (src, segment);
7290 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
7292 /* store the newsegment event so it can be sent from the streaming thread. */
7293 src->need_segment = TRUE;
7296 if (segment->rate != 1.0) {
7297 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
7299 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
7301 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
7303 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
7307 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
7309 if ((res = gst_rtspsrc_send (src, conninfo, &request, &response, NULL)) < 0)
7312 if (src->need_redirect) {
7313 GST_DEBUG_OBJECT (src,
7314 "redirect: tearing down and restarting with new url");
7315 /* teardown and restart with new url */
7316 gst_rtspsrc_close (src, TRUE, FALSE);
7317 /* reset protocols to force re-negotiation with redirected url */
7318 src->cur_protocols = src->protocols;
7319 gst_rtsp_message_unset (&request);
7320 gst_rtsp_message_unset (&response);
7324 /* seek may have silently failed as it is not supported */
7325 if (!(src->methods & GST_RTSP_PLAY)) {
7326 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
7327 /* obviously it is supported as we made it here */
7328 src->methods |= GST_RTSP_PLAY;
7329 src->seekable = FALSE;
7330 /* but there is nothing to parse in the response,
7331 * so convey we have no idea and not to expect anything particular */
7332 clear_rtp_base (src, stream);
7336 /* need to do for all streams */
7337 for (run = src->streams; run; run = g_list_next (run))
7338 clear_rtp_base (src, (GstRTSPStream *) run->data);
7340 /* NOTE the above also disables npt based eos detection */
7341 /* and below forces position to 0,
7342 * which is visible feedback we lost the plot */
7343 segment->start = segment->position = src->last_pos;
7346 gst_rtsp_message_unset (&request);
7348 /* parse RTP npt field. This is the current position in the stream (Normal
7349 * Play Time) and should be put in the NEWSEGMENT position field. */
7350 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
7352 gst_rtspsrc_parse_range (src, hval, segment);
7354 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
7355 segment->rate = 1.0;
7357 /* parse Speed header. This is the intended playback rate of the stream
7358 * and should be put in the NEWSEGMENT rate field. */
7359 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
7360 0) == GST_RTSP_OK) {
7361 segment->rate = gst_rtspsrc_get_float (hval);
7362 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
7363 &hval, 0) == GST_RTSP_OK) {
7364 segment->rate = gst_rtspsrc_get_float (hval);
7367 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
7368 * for the RTP packets. If this is not present, we assume all starts from 0...
7369 * This is info for the RTP session manager that we pass to it in caps. */
7371 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
7372 &hval, hval_idx++) == GST_RTSP_OK)
7373 gst_rtspsrc_parse_rtpinfo (src, hval);
7375 /* some servers indicate RTCP parameters in PLAY response,
7376 * rather than properly in SDP */
7377 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
7378 &hval, 0) == GST_RTSP_OK)
7379 gst_rtspsrc_handle_rtcp_interval (src, hval);
7381 gst_rtsp_message_unset (&response);
7383 /* early exit when we did aggregate control */
7387 /* configure the caps of the streams after we parsed all headers. Only reset
7388 * the manager object when we set a new Range header (we did a seek) */
7389 gst_rtspsrc_configure_caps (src, segment, src->need_range);
7391 /* set to PLAYING after we have configured the caps, otherwise we
7392 * might end up calling request_key (with SRTP) while caps are still
7393 * being configured. */
7394 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
7396 /* set again when needed */
7397 src->need_range = FALSE;
7399 src->running = TRUE;
7400 src->base_time = -1;
7401 src->state = GST_RTSP_STATE_PLAYING;
7404 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
7405 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7406 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7407 stream->discont = TRUE;
7412 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
7419 GST_DEBUG_OBJECT (src, "failed to open stream");
7424 GST_DEBUG_OBJECT (src, "PLAY is not supported");
7429 GST_DEBUG_OBJECT (src, "we were already PLAYING");
7432 create_request_failed:
7434 gchar *str = gst_rtsp_strresult (res);
7436 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7437 ("Could not create request. (%s)", str));
7443 gchar *str = gst_rtsp_strresult (res);
7445 gst_rtsp_message_unset (&request);
7446 if (res != GST_RTSP_EINTR) {
7447 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7448 ("Could not send message. (%s)", str));
7450 GST_WARNING_OBJECT (src, "PLAY interrupted");
7457 static GstRTSPResult
7458 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
7460 GstRTSPResult res = GST_RTSP_OK;
7461 GstRTSPMessage request = { 0 };
7462 GstRTSPMessage response = { 0 };
7464 const gchar *control;
7466 GST_DEBUG_OBJECT (src, "PAUSE...");
7468 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7471 if (!(src->methods & GST_RTSP_PAUSE))
7474 if (src->state == GST_RTSP_STATE_READY)
7477 if (!src->conninfo.connection || !src->conninfo.connected)
7480 /* construct a control url */
7481 control = get_aggregate_control (src);
7483 /* loop over the streams. We might exit the loop early when we could do an
7484 * aggregate control */
7485 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7486 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7487 GstRTSPConnInfo *conninfo;
7488 const gchar *setup_url;
7490 /* try aggregate control first but do non-aggregate control otherwise */
7492 setup_url = control;
7493 else if ((setup_url = stream->conninfo.location) == NULL)
7496 if (src->conninfo.connection) {
7497 conninfo = &src->conninfo;
7498 } else if (stream->conninfo.connection) {
7499 conninfo = &stream->conninfo;
7505 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7506 ("Sending PAUSE request"));
7509 gst_rtspsrc_init_request (src, &request, GST_RTSP_PAUSE,
7511 goto create_request_failed;
7513 if ((res = gst_rtspsrc_send (src, conninfo, &request, &response, NULL)) < 0)
7516 gst_rtsp_message_unset (&request);
7517 gst_rtsp_message_unset (&response);
7519 /* exit early when we did agregate control */
7524 /* change element states now */
7525 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7528 src->state = GST_RTSP_STATE_READY;
7532 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7539 GST_DEBUG_OBJECT (src, "failed to open stream");
7544 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7549 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7552 create_request_failed:
7554 gchar *str = gst_rtsp_strresult (res);
7556 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7557 ("Could not create request. (%s)", str));
7563 gchar *str = gst_rtsp_strresult (res);
7565 gst_rtsp_message_unset (&request);
7566 if (res != GST_RTSP_EINTR) {
7567 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7568 ("Could not send message. (%s)", str));
7570 GST_WARNING_OBJECT (src, "PAUSE interrupted");
7578 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
7580 GstRTSPSrc *rtspsrc;
7582 rtspsrc = GST_RTSPSRC (bin);
7584 switch (GST_MESSAGE_TYPE (message)) {
7585 case GST_MESSAGE_EOS:
7586 gst_message_unref (message);
7588 case GST_MESSAGE_ELEMENT:
7590 const GstStructure *s = gst_message_get_structure (message);
7592 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
7593 gboolean ignore_timeout;
7595 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
7597 GST_OBJECT_LOCK (rtspsrc);
7598 ignore_timeout = rtspsrc->ignore_timeout;
7599 rtspsrc->ignore_timeout = TRUE;
7600 GST_OBJECT_UNLOCK (rtspsrc);
7602 /* we only act on the first udp timeout message, others are irrelevant
7603 * and can be ignored. */
7604 if (!ignore_timeout)
7605 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
7607 gst_message_unref (message);
7610 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7613 case GST_MESSAGE_ERROR:
7616 GstRTSPStream *stream;
7619 udpsrc = GST_MESSAGE_SRC (message);
7621 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7622 GST_ELEMENT_NAME (udpsrc));
7624 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7628 /* we ignore the RTCP udpsrc */
7629 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7632 /* if we get error messages from the udp sources, that's not a problem as
7633 * long as not all of them error out. We also don't really know what the
7634 * problem is, the message does not give enough detail... */
7635 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7636 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7637 if (ret != GST_FLOW_OK)
7641 gst_message_unref (message);
7645 /* fatal but not our message, forward */
7646 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7651 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7657 /* the thread where everything happens */
7659 gst_rtspsrc_thread (GstRTSPSrc * src)
7663 GST_OBJECT_LOCK (src);
7664 cmd = src->pending_cmd;
7665 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7666 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7667 src->pending_cmd = CMD_LOOP;
7669 src->pending_cmd = CMD_WAIT;
7670 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
7672 /* we got the message command, so ensure communication is possible again */
7673 gst_rtspsrc_connection_flush (src, FALSE);
7675 src->busy_cmd = cmd;
7676 GST_OBJECT_UNLOCK (src);
7680 gst_rtspsrc_open (src, TRUE);
7683 gst_rtspsrc_play (src, &src->segment, TRUE);
7686 gst_rtspsrc_pause (src, TRUE);
7689 gst_rtspsrc_close (src, TRUE, FALSE);
7692 gst_rtspsrc_loop (src);
7695 gst_rtspsrc_reconnect (src, FALSE);
7701 GST_OBJECT_LOCK (src);
7702 /* and go back to sleep */
7703 if (src->pending_cmd == CMD_WAIT) {
7705 gst_task_pause (src->task);
7708 src->busy_cmd = CMD_WAIT;
7709 GST_OBJECT_UNLOCK (src);
7713 gst_rtspsrc_start (GstRTSPSrc * src)
7715 GST_DEBUG_OBJECT (src, "starting");
7717 GST_OBJECT_LOCK (src);
7719 src->pending_cmd = CMD_WAIT;
7721 if (src->task == NULL) {
7722 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7723 if (src->task == NULL)
7726 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7728 GST_OBJECT_UNLOCK (src);
7735 GST_OBJECT_UNLOCK (src);
7736 GST_ERROR_OBJECT (src, "failed to create task");
7742 gst_rtspsrc_stop (GstRTSPSrc * src)
7746 GST_DEBUG_OBJECT (src, "stopping");
7748 /* also cancels pending task */
7749 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7751 GST_OBJECT_LOCK (src);
7752 if ((task = src->task)) {
7754 GST_OBJECT_UNLOCK (src);
7756 gst_task_stop (task);
7758 /* make sure it is not running */
7759 GST_RTSP_STREAM_LOCK (src);
7760 GST_RTSP_STREAM_UNLOCK (src);
7762 /* now wait for the task to finish */
7763 gst_task_join (task);
7765 /* and free the task */
7766 gst_object_unref (GST_OBJECT (task));
7768 GST_OBJECT_LOCK (src);
7770 GST_OBJECT_UNLOCK (src);
7772 /* ensure synchronously all is closed and clean */
7773 gst_rtspsrc_close (src, FALSE, TRUE);
7778 static GstStateChangeReturn
7779 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
7781 GstRTSPSrc *rtspsrc;
7782 GstStateChangeReturn ret;
7784 rtspsrc = GST_RTSPSRC (element);
7786 switch (transition) {
7787 case GST_STATE_CHANGE_NULL_TO_READY:
7788 if (!gst_rtspsrc_start (rtspsrc))
7791 case GST_STATE_CHANGE_READY_TO_PAUSED:
7792 /* init some state */
7793 rtspsrc->cur_protocols = rtspsrc->protocols;
7794 /* first attempt, don't ignore timeouts */
7795 rtspsrc->ignore_timeout = FALSE;
7796 rtspsrc->open_error = FALSE;
7797 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
7799 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7800 set_manager_buffer_mode (rtspsrc);
7802 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7803 /* unblock the tcp tasks and make the loop waiting */
7804 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
7805 /* make sure it is waiting before we send PAUSE or PLAY below */
7806 GST_RTSP_STREAM_LOCK (rtspsrc);
7807 GST_RTSP_STREAM_UNLOCK (rtspsrc);
7810 case GST_STATE_CHANGE_PAUSED_TO_READY:
7816 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
7817 if (ret == GST_STATE_CHANGE_FAILURE)
7820 switch (transition) {
7821 case GST_STATE_CHANGE_NULL_TO_READY:
7822 ret = GST_STATE_CHANGE_SUCCESS;
7824 case GST_STATE_CHANGE_READY_TO_PAUSED:
7825 ret = GST_STATE_CHANGE_NO_PREROLL;
7827 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7828 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
7829 ret = GST_STATE_CHANGE_SUCCESS;
7831 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7832 /* send pause request and keep the idle task around */
7833 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
7834 ret = GST_STATE_CHANGE_NO_PREROLL;
7836 case GST_STATE_CHANGE_PAUSED_TO_READY:
7837 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_ALL);
7838 ret = GST_STATE_CHANGE_SUCCESS;
7840 case GST_STATE_CHANGE_READY_TO_NULL:
7841 gst_rtspsrc_stop (rtspsrc);
7842 ret = GST_STATE_CHANGE_SUCCESS;
7845 /* Otherwise it's success, we don't want to return spurious
7846 * NO_PREROLL or ASYNC from internal elements as we care for
7847 * state changes ourselves here
7849 * This is to catch PAUSED->PAUSED and PLAYING->PLAYING transitions.
7851 if (GST_STATE_TRANSITION_NEXT (transition) == GST_STATE_PAUSED)
7852 ret = GST_STATE_CHANGE_NO_PREROLL;
7854 ret = GST_STATE_CHANGE_SUCCESS;
7863 GST_DEBUG_OBJECT (rtspsrc, "start failed");
7864 return GST_STATE_CHANGE_FAILURE;
7869 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
7872 GstRTSPSrc *rtspsrc;
7874 rtspsrc = GST_RTSPSRC (element);
7876 if (GST_EVENT_IS_DOWNSTREAM (event)) {
7877 res = gst_rtspsrc_push_event (rtspsrc, event);
7879 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
7886 /*** GSTURIHANDLER INTERFACE *************************************************/
7889 gst_rtspsrc_uri_get_type (GType type)
7894 static const gchar *const *
7895 gst_rtspsrc_uri_get_protocols (GType type)
7897 static const gchar *protocols[] =
7898 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
7899 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
7906 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
7908 GstRTSPSrc *src = GST_RTSPSRC (handler);
7910 /* FIXME: make thread-safe */
7911 return g_strdup (src->conninfo.location);
7915 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
7921 GstRTSPUrl *newurl = NULL;
7922 GstSDPMessage *sdp = NULL;
7924 src = GST_RTSPSRC (handler);
7926 /* same URI, we're fine */
7927 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
7930 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
7931 sres = gst_sdp_message_new (&sdp);
7935 GST_DEBUG_OBJECT (src, "parsing SDP message");
7936 sres = gst_sdp_message_parse_uri (uri, sdp);
7941 GST_DEBUG_OBJECT (src, "parsing URI");
7942 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
7946 /* if worked, free previous and store new url object along with the original
7948 GST_DEBUG_OBJECT (src, "configuring URI");
7949 g_free (src->conninfo.location);
7950 src->conninfo.location = g_strdup (uri);
7951 gst_rtsp_url_free (src->conninfo.url);
7952 src->conninfo.url = newurl;
7953 g_free (src->conninfo.url_str);
7955 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
7957 src->conninfo.url_str = NULL;
7960 gst_sdp_message_free (src->sdp);
7962 src->from_sdp = sdp != NULL;
7964 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
7965 GST_DEBUG_OBJECT (src, "request uri is: %s",
7966 GST_STR_NULL (src->conninfo.url_str));
7973 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
7978 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
7979 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7980 "Could not create SDP");
7985 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
7986 GST_STR_NULL (uri));
7987 gst_sdp_message_free (sdp);
7988 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7994 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
7995 GST_STR_NULL (uri), res);
7996 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7997 "Invalid RTSP URI");
8003 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
8005 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
8007 iface->get_type = gst_rtspsrc_uri_get_type;
8008 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
8009 iface->get_uri = gst_rtspsrc_uri_get_uri;
8010 iface->set_uri = gst_rtspsrc_uri_set_uri;