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,
127 SIGNAL_ACCEPT_CERTIFICATE,
132 enum _GstRtspSrcRtcpSyncMode
139 enum _GstRtspSrcBufferMode
148 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
150 gst_rtsp_src_buffer_mode_get_type (void)
152 static GType buffer_mode_type = 0;
153 static const GEnumValue buffer_modes[] = {
154 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
155 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
156 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
157 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
158 {BUFFER_MODE_SYNCED, "Synchronized sender and receiver clocks", "synced"},
162 if (!buffer_mode_type) {
164 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
166 return buffer_mode_type;
169 enum _GstRtspSrcNtpTimeSource
172 NTP_TIME_SOURCE_UNIX,
173 NTP_TIME_SOURCE_RUNNING_TIME,
174 NTP_TIME_SOURCE_CLOCK_TIME
177 #define DEBUG_RTSP(__self,msg) gst_rtspsrc_print_rtsp_message (__self, msg)
178 #define DEBUG_SDP(__self,msg) gst_rtspsrc_print_sdp_message (__self, msg)
180 #define GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE (gst_rtsp_src_ntp_time_source_get_type())
182 gst_rtsp_src_ntp_time_source_get_type (void)
184 static GType ntp_time_source_type = 0;
185 static const GEnumValue ntp_time_source_values[] = {
186 {NTP_TIME_SOURCE_NTP, "NTP time based on realtime clock", "ntp"},
187 {NTP_TIME_SOURCE_UNIX, "UNIX time based on realtime clock", "unix"},
188 {NTP_TIME_SOURCE_RUNNING_TIME,
189 "Running time based on pipeline clock",
191 {NTP_TIME_SOURCE_CLOCK_TIME, "Pipeline clock time", "clock-time"},
195 if (!ntp_time_source_type) {
196 ntp_time_source_type =
197 g_enum_register_static ("GstRTSPSrcNtpTimeSource",
198 ntp_time_source_values);
200 return ntp_time_source_type;
203 #define DEFAULT_LOCATION NULL
204 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
205 #define DEFAULT_DEBUG FALSE
206 #define DEFAULT_RETRY 20
207 #define DEFAULT_TIMEOUT 5000000
208 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
209 #define DEFAULT_TCP_TIMEOUT 20000000
210 #define DEFAULT_LATENCY_MS 2000
211 #define DEFAULT_DROP_ON_LATENCY FALSE
212 #define DEFAULT_CONNECTION_SPEED 0
213 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
214 #define DEFAULT_DO_RTCP TRUE
215 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
216 #define DEFAULT_PROXY NULL
217 #define DEFAULT_RTP_BLOCKSIZE 0
218 #define DEFAULT_USER_ID NULL
219 #define DEFAULT_USER_PW NULL
220 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
221 #define DEFAULT_PORT_RANGE NULL
222 #define DEFAULT_SHORT_HEADER FALSE
223 #define DEFAULT_PROBATION 2
224 #define DEFAULT_UDP_RECONNECT TRUE
225 #define DEFAULT_MULTICAST_IFACE NULL
226 #define DEFAULT_NTP_SYNC FALSE
227 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
228 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
229 #define DEFAULT_TLS_DATABASE NULL
230 #define DEFAULT_TLS_INTERACTION NULL
231 #define DEFAULT_DO_RETRANSMISSION TRUE
232 #define DEFAULT_NTP_TIME_SOURCE NTP_TIME_SOURCE_NTP
233 #define DEFAULT_USER_AGENT "GStreamer/" PACKAGE_VERSION
234 #define DEFAULT_MAX_RTCP_RTP_TIME_DIFF 1000
235 #define DEFAULT_RFC7273_SYNC FALSE
236 #define DEFAULT_MAX_TS_OFFSET_ADJUSTMENT G_GUINT64_CONSTANT(0)
237 #define DEFAULT_MAX_TS_OFFSET G_GINT64_CONSTANT(3000000000)
238 #define DEFAULT_VERSION GST_RTSP_VERSION_1_0
250 PROP_DROP_ON_LATENCY,
251 PROP_CONNECTION_SPEED,
254 PROP_DO_RTSP_KEEP_ALIVE,
263 PROP_UDP_BUFFER_SIZE,
267 PROP_MULTICAST_IFACE,
269 PROP_USE_PIPELINE_CLOCK,
271 PROP_TLS_VALIDATION_FLAGS,
273 PROP_TLS_INTERACTION,
274 PROP_DO_RETRANSMISSION,
275 PROP_NTP_TIME_SOURCE,
277 PROP_MAX_RTCP_RTP_TIME_DIFF,
279 PROP_MAX_TS_OFFSET_ADJUSTMENT,
281 PROP_DEFAULT_VERSION,
284 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
286 gst_rtsp_nat_method_get_type (void)
288 static GType rtsp_nat_method_type = 0;
289 static const GEnumValue rtsp_nat_method[] = {
290 {GST_RTSP_NAT_NONE, "None", "none"},
291 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
295 if (!rtsp_nat_method_type) {
296 rtsp_nat_method_type =
297 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
299 return rtsp_nat_method_type;
302 #define RTSP_SRC_RESPONSE_ERROR(src, response_msg, err_cat, err_code, error_message) \
304 GST_ELEMENT_ERROR_WITH_DETAILS((src), err_cat, err_code, ("%s", error_message), \
305 ("%s (%d)", (response_msg)->type_data.response.reason, (response_msg)->type_data.response.code), \
306 ("rtsp-status-code", G_TYPE_UINT, (response_msg)->type_data.response.code, \
307 "rtsp-status-reason", G_TYPE_STRING, GST_STR_NULL((response_msg)->type_data.response.reason), NULL)); \
310 static void gst_rtspsrc_finalize (GObject * object);
312 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
313 const GValue * value, GParamSpec * pspec);
314 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
315 GValue * value, GParamSpec * pspec);
317 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
319 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
320 gpointer iface_data);
322 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
323 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
325 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
326 GstStateChange transition);
327 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
328 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
330 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
331 GstRTSPMessage * response);
333 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
335 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
336 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
338 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
339 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
340 gboolean async, const gchar * seek_style);
341 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
342 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
343 gboolean only_close);
345 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
346 const gchar * uri, GError ** error);
347 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
349 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
350 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
351 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
352 GstRTSPStream * stream, GstEvent * event);
353 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
354 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
355 static GstRTSPResult gst_rtsp_conninfo_close (GstRTSPSrc * src,
356 GstRTSPConnInfo * info, gboolean free);
358 gst_rtspsrc_print_rtsp_message (GstRTSPSrc * src, const GstRTSPMessage * msg);
360 gst_rtspsrc_print_sdp_message (GstRTSPSrc * src, const GstSDPMessage * msg);
368 /* commands we send to out loop to notify it of events */
369 #define CMD_OPEN (1 << 0)
370 #define CMD_PLAY (1 << 1)
371 #define CMD_PAUSE (1 << 2)
372 #define CMD_CLOSE (1 << 3)
373 #define CMD_WAIT (1 << 4)
374 #define CMD_RECONNECT (1 << 5)
375 #define CMD_LOOP (1 << 6)
377 /* mask for all commands */
378 #define CMD_ALL ((CMD_LOOP << 1) - 1)
380 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
382 gchar *__txt = _gst_element_error_printf text; \
383 gst_element_post_message (GST_ELEMENT_CAST (el), \
384 gst_message_new_progress (GST_OBJECT_CAST (el), \
385 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
389 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
391 #define gst_rtspsrc_parent_class parent_class
392 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
393 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
395 #ifndef GST_DISABLE_GST_DEBUG
396 static inline const char *
397 cmd_to_string (guint cmd)
421 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
423 GST_DEBUG_OBJECT (src, "default handler");
428 select_stream_accum (GSignalInvocationHint * ihint,
429 GValue * return_accu, const GValue * handler_return, gpointer data)
433 myboolean = g_value_get_boolean (handler_return);
434 GST_DEBUG ("accum %d", myboolean);
435 g_value_set_boolean (return_accu, myboolean);
437 /* stop emission if FALSE */
442 default_before_send (GstRTSPSrc * src, GstRTSPMessage * msg)
444 GST_DEBUG_OBJECT (src, "default handler");
449 before_send_accum (GSignalInvocationHint * ihint,
450 GValue * return_accu, const GValue * handler_return, gpointer data)
454 myboolean = g_value_get_boolean (handler_return);
455 g_value_set_boolean (return_accu, myboolean);
457 /* prevent send if FALSE */
462 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
464 GObjectClass *gobject_class;
465 GstElementClass *gstelement_class;
466 GstBinClass *gstbin_class;
468 gobject_class = (GObjectClass *) klass;
469 gstelement_class = (GstElementClass *) klass;
470 gstbin_class = (GstBinClass *) klass;
472 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
474 gobject_class->set_property = gst_rtspsrc_set_property;
475 gobject_class->get_property = gst_rtspsrc_get_property;
477 gobject_class->finalize = gst_rtspsrc_finalize;
479 g_object_class_install_property (gobject_class, PROP_LOCATION,
480 g_param_spec_string ("location", "RTSP Location",
481 "Location of the RTSP url to read",
482 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
484 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
485 g_param_spec_flags ("protocols", "Protocols",
486 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
487 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
489 g_object_class_install_property (gobject_class, PROP_DEBUG,
490 g_param_spec_boolean ("debug", "Debug",
491 "Dump request and response messages to stdout"
492 "(DEPRECATED: Printed all RTSP message to gstreamer log as 'log' level)",
494 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
496 g_object_class_install_property (gobject_class, PROP_RETRY,
497 g_param_spec_uint ("retry", "Retry",
498 "Max number of retries when allocating RTP ports.",
499 0, G_MAXUINT16, DEFAULT_RETRY,
500 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
502 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
503 g_param_spec_uint64 ("timeout", "Timeout",
504 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
505 0, G_MAXUINT64, DEFAULT_TIMEOUT,
506 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
508 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
509 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
510 "Fail after timeout microseconds on TCP connections (0 = disabled)",
511 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
512 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
514 g_object_class_install_property (gobject_class, PROP_LATENCY,
515 g_param_spec_uint ("latency", "Buffer latency in ms",
516 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
517 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
519 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
520 g_param_spec_boolean ("drop-on-latency",
521 "Drop buffers when maximum latency is reached",
522 "Tells the jitterbuffer to never exceed the given latency in size",
523 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
525 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
526 g_param_spec_uint64 ("connection-speed", "Connection Speed",
527 "Network connection speed in kbps (0 = unknown)",
528 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
529 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
531 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
532 g_param_spec_enum ("nat-method", "NAT Method",
533 "Method to use for traversing firewalls and NAT",
534 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
535 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
538 * GstRTSPSrc:do-rtcp:
540 * Enable RTCP support. Some old server don't like RTCP and then this property
541 * needs to be set to FALSE.
543 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
544 g_param_spec_boolean ("do-rtcp", "Do RTCP",
545 "Send RTCP packets, disable for old incompatible server.",
546 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
549 * GstRTSPSrc:do-rtsp-keep-alive:
551 * Enable RTSP keep alive support. Some old server don't like RTSP
552 * keep alive and then this property needs to be set to FALSE.
554 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
555 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
556 "Send RTSP keep alive packets, disable for old incompatible server.",
557 DEFAULT_DO_RTSP_KEEP_ALIVE,
558 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
563 * Set the proxy parameters. This has to be a string of the format
564 * [http://][user:passwd@]host[:port].
566 g_object_class_install_property (gobject_class, PROP_PROXY,
567 g_param_spec_string ("proxy", "Proxy",
568 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
569 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
571 * GstRTSPSrc:proxy-id:
573 * Sets the proxy URI user id for authentication. If the URI set via the
574 * "proxy" property contains a user-id already, that will take precedence.
578 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
579 g_param_spec_string ("proxy-id", "proxy-id",
580 "HTTP proxy URI user id for authentication", "",
581 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
583 * GstRTSPSrc:proxy-pw:
585 * Sets the proxy URI password for authentication. If the URI set via the
586 * "proxy" property contains a password already, that will take precedence.
590 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
591 g_param_spec_string ("proxy-pw", "proxy-pw",
592 "HTTP proxy URI user password for authentication", "",
593 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
596 * GstRTSPSrc:rtp-blocksize:
598 * RTP package size to suggest to server.
600 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
601 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
602 "RTP package size to suggest to server (0 = disabled)",
603 0, 65536, DEFAULT_RTP_BLOCKSIZE,
604 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
606 g_object_class_install_property (gobject_class,
608 g_param_spec_string ("user-id", "user-id",
609 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
610 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
611 g_object_class_install_property (gobject_class, PROP_USER_PW,
612 g_param_spec_string ("user-pw", "user-pw",
613 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
614 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
617 * GstRTSPSrc:buffer-mode:
619 * Control the buffering and timestamping mode used by the jitterbuffer.
621 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
622 g_param_spec_enum ("buffer-mode", "Buffer Mode",
623 "Control the buffering algorithm in use",
624 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
625 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
628 * GstRTSPSrc:port-range:
630 * Configure the client port numbers that can be used to recieve RTP and
633 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
634 g_param_spec_string ("port-range", "Port range",
635 "Client port range that can be used to receive RTP and RTCP data, "
636 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
637 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
640 * GstRTSPSrc:udp-buffer-size:
642 * Size of the kernel UDP receive buffer in bytes.
644 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
645 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
646 "Size of the kernel UDP receive buffer in bytes, 0=default",
647 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
648 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
651 * GstRTSPSrc:short-header:
653 * Only send the basic RTSP headers for broken encoders.
655 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
656 g_param_spec_boolean ("short-header", "Short Header",
657 "Only send the basic RTSP headers for broken encoders",
658 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
660 g_object_class_install_property (gobject_class, PROP_PROBATION,
661 g_param_spec_uint ("probation", "Number of probations",
662 "Consecutive packet sequence numbers to accept the source",
663 0, G_MAXUINT, DEFAULT_PROBATION,
664 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
666 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
667 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
668 "Reconnect to the server if RTSP connection is closed when doing UDP",
669 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
671 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
672 g_param_spec_string ("multicast-iface", "Multicast Interface",
673 "The network interface on which to join the multicast group",
674 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
676 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
677 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
678 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
679 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
681 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
682 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
683 "Use the pipeline running-time to set the NTP time in the RTCP SR messages"
684 "(DEPRECATED: Use ntp-time-source property)",
685 DEFAULT_USE_PIPELINE_CLOCK,
686 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
688 g_object_class_install_property (gobject_class, PROP_SDES,
689 g_param_spec_boxed ("sdes", "SDES",
690 "The SDES items of this session",
691 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
694 * GstRTSPSrc::tls-validation-flags:
696 * TLS certificate validation flags used to validate server
701 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
702 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
703 "TLS certificate validation flags used to validate the server certificate",
704 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
705 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
708 * GstRTSPSrc::tls-database:
710 * TLS database with anchor certificate authorities used to validate
711 * the server certificate.
715 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
716 g_param_spec_object ("tls-database", "TLS database",
717 "TLS database with anchor certificate authorities used to validate the server certificate",
718 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
721 * GstRTSPSrc::tls-interaction:
723 * A #GTlsInteraction object to be used when the connection or certificate
724 * database need to interact with the user. This will be used to prompt the
725 * user for passwords where necessary.
729 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
730 g_param_spec_object ("tls-interaction", "TLS interaction",
731 "A GTlsInteraction object to promt the user for password or certificate",
732 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
735 * GstRTSPSrc::do-retransmission:
737 * Attempt to ask the server to retransmit lost packets according to RFC4588.
739 * Note: currently only works with SSRC-multiplexed retransmission streams
743 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
744 g_param_spec_boolean ("do-retransmission", "Retransmission",
745 "Ask the server to retransmit lost packets",
746 DEFAULT_DO_RETRANSMISSION,
747 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
750 * GstRTSPSrc::ntp-time-source:
752 * allows to select the time source that should be used
753 * for the NTP time in RTCP packets
757 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
758 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
759 "NTP time source for RTCP packets",
760 GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE, DEFAULT_NTP_TIME_SOURCE,
761 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
764 * GstRTSPSrc::user-agent:
766 * The string to set in the User-Agent header.
770 g_object_class_install_property (gobject_class, PROP_USER_AGENT,
771 g_param_spec_string ("user-agent", "User Agent",
772 "The User-Agent string to send to the server",
773 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
775 g_object_class_install_property (gobject_class, PROP_MAX_RTCP_RTP_TIME_DIFF,
776 g_param_spec_int ("max-rtcp-rtp-time-diff", "Max RTCP RTP Time Diff",
777 "Maximum amount of time in ms that the RTP time in RTCP SRs "
778 "is allowed to be ahead (-1 disabled)", -1, G_MAXINT,
779 DEFAULT_MAX_RTCP_RTP_TIME_DIFF,
780 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
782 g_object_class_install_property (gobject_class, PROP_RFC7273_SYNC,
783 g_param_spec_boolean ("rfc7273-sync", "Sync on RFC7273 clock",
784 "Synchronize received streams to the RFC7273 clock "
785 "(requires clock and offset to be provided)", DEFAULT_RFC7273_SYNC,
786 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
789 * GstRTSPSrc:default-rtsp-version:
791 * The preferred RTSP version to use while negotiating the version with the server.
795 g_object_class_install_property (gobject_class, PROP_DEFAULT_VERSION,
796 g_param_spec_enum ("default-rtsp-version",
797 "The RTSP version to try first",
798 "The RTSP version that should be tried first when negotiating version.",
799 GST_TYPE_RTSP_VERSION, DEFAULT_VERSION,
800 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
803 * GstRTSPSrc:max-ts-offset-adjustment:
805 * Syncing time stamps to NTP time adds a time offset. This parameter
806 * specifies the maximum number of nanoseconds per frame that this time offset
807 * may be adjusted with. This is used to avoid sudden large changes to time
810 g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET_ADJUSTMENT,
811 g_param_spec_uint64 ("max-ts-offset-adjustment",
812 "Max Timestamp Offset Adjustment",
813 "The maximum number of nanoseconds per frame that time stamp offsets "
814 "may be adjusted (0 = no limit).", 0, G_MAXUINT64,
815 DEFAULT_MAX_TS_OFFSET_ADJUSTMENT, G_PARAM_READWRITE |
816 G_PARAM_STATIC_STRINGS));
819 * GstRtpBin:max-ts-offset:
821 * Used to set an upper limit of how large a time offset may be. This
822 * is used to protect against unrealistic values as a result of either
823 * client,server or clock issues.
825 g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET,
826 g_param_spec_int64 ("max-ts-offset", "Max TS Offset",
827 "The maximum absolute value of the time offset in (nanoseconds). "
828 "Note, if the ntp-sync parameter is set the default value is "
829 "changed to 0 (no limit)", 0, G_MAXINT64, DEFAULT_MAX_TS_OFFSET,
830 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
833 * GstRTSPSrc::handle-request:
834 * @rtspsrc: a #GstRTSPSrc
835 * @request: a #GstRTSPMessage
836 * @response: a #GstRTSPMessage
838 * Handle a server request in @request and prepare @response.
840 * This signal is called from the streaming thread, you should therefore not
841 * do any state changes on @rtspsrc because this might deadlock. If you want
842 * to modify the state as a result of this signal, post a
843 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
848 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
849 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
850 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
851 G_TYPE_POINTER, G_TYPE_POINTER);
854 * GstRTSPSrc::on-sdp:
855 * @rtspsrc: a #GstRTSPSrc
856 * @sdp: a #GstSDPMessage
858 * Emited when the client has retrieved the SDP and before it configures the
859 * streams in the SDP. @sdp can be inspected and modified.
861 * This signal is called from the streaming thread, you should therefore not
862 * do any state changes on @rtspsrc because this might deadlock. If you want
863 * to modify the state as a result of this signal, post a
864 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
869 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
870 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
871 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
872 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
875 * GstRTSPSrc::select-stream:
876 * @rtspsrc: a #GstRTSPSrc
877 * @num: the stream number
878 * @caps: the stream caps
880 * Emited before the client decides to configure the stream @num with
883 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
888 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
889 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
890 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
891 (GCallback) default_select_stream, select_stream_accum, NULL,
892 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
895 * GstRTSPSrc::new-manager:
896 * @rtspsrc: a #GstRTSPSrc
897 * @manager: a #GstElement
899 * Emited after a new manager (like rtpbin) was created and the default
900 * properties were configured.
904 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
905 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
906 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
907 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
910 * GstRTSPSrc::request-rtcp-key:
911 * @rtspsrc: a #GstRTSPSrc
912 * @num: the stream number
914 * Signal emited to get the crypto parameters relevant to the RTCP
915 * stream. User should provide the key and the RTCP encryption ciphers
916 * and authentication, and return them wrapped in a GstCaps.
920 gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY] =
921 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
922 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
925 * GstRTSPSrc::accept-certificate:
926 * @rtspsrc: a #GstRTSPSrc
927 * @peer_cert: the peer's #GTlsCertificate
928 * @errors: the problems with @peer_cert
929 * @user_data: user data set when the signal handler was connected.
931 * This will directly map to #GTlsConnection 's "accept-certificate"
932 * signal and be performed after the default checks of #GstRTSPConnection
933 * (checking against the #GTlsDatabase with the given #GTlsCertificateFlags)
934 * have failed. If no #GTlsDatabase is set on this connection, only this
935 * signal will be emitted.
939 gst_rtspsrc_signals[SIGNAL_ACCEPT_CERTIFICATE] =
940 g_signal_new ("accept-certificate", G_TYPE_FROM_CLASS (klass),
941 G_SIGNAL_RUN_LAST, 0, g_signal_accumulator_true_handled, NULL, NULL,
942 G_TYPE_BOOLEAN, 3, G_TYPE_TLS_CONNECTION, G_TYPE_TLS_CERTIFICATE,
943 G_TYPE_TLS_CERTIFICATE_FLAGS);
946 * GstRTSPSrc::before-send
947 * @rtspsrc: a #GstRTSPSrc
948 * @num: the stream number
950 * Emitted before each RTSP request is sent, in order to allow
951 * the application to modify send parameters or to skip the message entirely.
952 * This can be used, for example, to work with ONVIF Profile G servers,
953 * which need a different/additional range, rate-control, and intra/x
956 * Returns: %TRUE when the command should be sent, %FALSE when the
957 * command should be dropped.
961 gst_rtspsrc_signals[SIGNAL_BEFORE_SEND] =
962 g_signal_new_class_handler ("before-send", G_TYPE_FROM_CLASS (klass),
963 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
964 (GCallback) default_before_send, before_send_accum, NULL,
965 g_cclosure_marshal_generic, G_TYPE_BOOLEAN,
966 1, GST_TYPE_RTSP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
968 gstelement_class->send_event = gst_rtspsrc_send_event;
969 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
970 gstelement_class->change_state = gst_rtspsrc_change_state;
972 gst_element_class_add_static_pad_template (gstelement_class, &rtptemplate);
974 gst_element_class_set_static_metadata (gstelement_class,
975 "RTSP packet receiver", "Source/Network",
976 "Receive data over the network via RTSP (RFC 2326)",
977 "Wim Taymans <wim@fluendo.com>, "
978 "Thijs Vermeir <thijs.vermeir@barco.com>, "
979 "Lutz Mueller <lutz@topfrose.de>");
981 gstbin_class->handle_message = gst_rtspsrc_handle_message;
983 gst_rtsp_ext_list_init ();
987 gst_rtspsrc_init (GstRTSPSrc * src)
989 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
990 src->protocols = DEFAULT_PROTOCOLS;
991 src->debug = DEFAULT_DEBUG;
992 src->retry = DEFAULT_RETRY;
993 src->udp_timeout = DEFAULT_TIMEOUT;
994 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
995 src->latency = DEFAULT_LATENCY_MS;
996 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
997 src->connection_speed = DEFAULT_CONNECTION_SPEED;
998 src->nat_method = DEFAULT_NAT_METHOD;
999 src->do_rtcp = DEFAULT_DO_RTCP;
1000 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
1001 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
1002 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
1003 src->user_id = g_strdup (DEFAULT_USER_ID);
1004 src->user_pw = g_strdup (DEFAULT_USER_PW);
1005 src->buffer_mode = DEFAULT_BUFFER_MODE;
1006 src->client_port_range.min = 0;
1007 src->client_port_range.max = 0;
1008 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
1009 src->short_header = DEFAULT_SHORT_HEADER;
1010 src->probation = DEFAULT_PROBATION;
1011 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
1012 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1013 src->ntp_sync = DEFAULT_NTP_SYNC;
1014 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
1016 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
1017 src->tls_database = DEFAULT_TLS_DATABASE;
1018 src->tls_interaction = DEFAULT_TLS_INTERACTION;
1019 src->do_retransmission = DEFAULT_DO_RETRANSMISSION;
1020 src->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
1021 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
1022 src->max_rtcp_rtp_time_diff = DEFAULT_MAX_RTCP_RTP_TIME_DIFF;
1023 src->rfc7273_sync = DEFAULT_RFC7273_SYNC;
1024 src->max_ts_offset_adjustment = DEFAULT_MAX_TS_OFFSET_ADJUSTMENT;
1025 src->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
1026 src->max_ts_offset_is_set = FALSE;
1027 src->default_version = DEFAULT_VERSION;
1028 src->version = GST_RTSP_VERSION_INVALID;
1030 /* get a list of all extensions */
1031 src->extensions = gst_rtsp_ext_list_get ();
1033 /* connect to send signal */
1034 gst_rtsp_ext_list_connect (src->extensions, "send",
1035 (GCallback) gst_rtspsrc_send_cb, src);
1037 /* protects the streaming thread in interleaved mode or the polling
1038 * thread in UDP mode. */
1039 g_rec_mutex_init (&src->stream_rec_lock);
1041 /* protects our state changes from multiple invocations */
1042 g_rec_mutex_init (&src->state_rec_lock);
1044 src->state = GST_RTSP_STATE_INVALID;
1046 g_mutex_init (&src->conninfo.send_lock);
1047 g_mutex_init (&src->conninfo.recv_lock);
1049 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
1050 gst_bin_set_suppressed_flags (GST_BIN (src),
1051 GST_ELEMENT_FLAG_SOURCE | GST_ELEMENT_FLAG_SINK);
1055 gst_rtspsrc_finalize (GObject * object)
1057 GstRTSPSrc *rtspsrc;
1059 rtspsrc = GST_RTSPSRC (object);
1061 gst_rtsp_ext_list_free (rtspsrc->extensions);
1062 g_free (rtspsrc->conninfo.location);
1063 gst_rtsp_url_free (rtspsrc->conninfo.url);
1064 g_free (rtspsrc->conninfo.url_str);
1065 g_free (rtspsrc->user_id);
1066 g_free (rtspsrc->user_pw);
1067 g_free (rtspsrc->multi_iface);
1068 g_free (rtspsrc->user_agent);
1071 gst_sdp_message_free (rtspsrc->sdp);
1072 rtspsrc->sdp = NULL;
1074 if (rtspsrc->provided_clock)
1075 gst_object_unref (rtspsrc->provided_clock);
1078 gst_structure_free (rtspsrc->sdes);
1080 if (rtspsrc->tls_database)
1081 g_object_unref (rtspsrc->tls_database);
1083 if (rtspsrc->tls_interaction)
1084 g_object_unref (rtspsrc->tls_interaction);
1087 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
1088 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
1090 g_mutex_clear (&rtspsrc->conninfo.send_lock);
1091 g_mutex_clear (&rtspsrc->conninfo.recv_lock);
1093 G_OBJECT_CLASS (parent_class)->finalize (object);
1097 gst_rtspsrc_provide_clock (GstElement * element)
1099 GstRTSPSrc *src = GST_RTSPSRC (element);
1102 if ((clock = src->provided_clock) != NULL)
1103 return gst_object_ref (clock);
1105 return GST_ELEMENT_CLASS (parent_class)->provide_clock (element);
1108 /* a proxy string of the format [user:passwd@]host[:port] */
1110 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
1112 gchar *p, *at, *col;
1114 g_free (rtsp->proxy_user);
1115 rtsp->proxy_user = NULL;
1116 g_free (rtsp->proxy_passwd);
1117 rtsp->proxy_passwd = NULL;
1118 g_free (rtsp->proxy_host);
1119 rtsp->proxy_host = NULL;
1120 rtsp->proxy_port = 0;
1122 p = (gchar *) proxy;
1127 /* we allow http:// in front but ignore it */
1128 if (g_str_has_prefix (p, "http://"))
1131 at = strchr (p, '@');
1133 /* look for user:passwd */
1134 col = strchr (proxy, ':');
1135 if (col == NULL || col > at)
1138 rtsp->proxy_user = g_strndup (p, col - p);
1140 rtsp->proxy_passwd = g_strndup (col, at - col);
1145 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
1146 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
1147 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
1148 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
1149 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
1150 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
1151 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
1154 col = strchr (p, ':');
1157 /* everything before the colon is the hostname */
1158 rtsp->proxy_host = g_strndup (p, col - p);
1160 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
1162 rtsp->proxy_host = g_strdup (p);
1163 rtsp->proxy_port = 8080;
1169 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
1171 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
1172 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
1175 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
1177 rtspsrc->ptcp_timeout = NULL;
1181 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
1184 GstRTSPSrc *rtspsrc;
1186 rtspsrc = GST_RTSPSRC (object);
1190 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
1191 g_value_get_string (value), NULL);
1193 case PROP_PROTOCOLS:
1194 rtspsrc->protocols = g_value_get_flags (value);
1197 rtspsrc->debug = g_value_get_boolean (value);
1200 rtspsrc->retry = g_value_get_uint (value);
1203 rtspsrc->udp_timeout = g_value_get_uint64 (value);
1205 case PROP_TCP_TIMEOUT:
1206 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
1209 rtspsrc->latency = g_value_get_uint (value);
1211 case PROP_DROP_ON_LATENCY:
1212 rtspsrc->drop_on_latency = g_value_get_boolean (value);
1214 case PROP_CONNECTION_SPEED:
1215 rtspsrc->connection_speed = g_value_get_uint64 (value);
1217 case PROP_NAT_METHOD:
1218 rtspsrc->nat_method = g_value_get_enum (value);
1221 rtspsrc->do_rtcp = g_value_get_boolean (value);
1223 case PROP_DO_RTSP_KEEP_ALIVE:
1224 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
1227 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
1230 g_free (rtspsrc->prop_proxy_id);
1231 rtspsrc->prop_proxy_id = g_value_dup_string (value);
1234 g_free (rtspsrc->prop_proxy_pw);
1235 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
1237 case PROP_RTP_BLOCKSIZE:
1238 rtspsrc->rtp_blocksize = g_value_get_uint (value);
1241 g_free (rtspsrc->user_id);
1242 rtspsrc->user_id = g_value_dup_string (value);
1245 g_free (rtspsrc->user_pw);
1246 rtspsrc->user_pw = g_value_dup_string (value);
1248 case PROP_BUFFER_MODE:
1249 rtspsrc->buffer_mode = g_value_get_enum (value);
1251 case PROP_PORT_RANGE:
1255 str = g_value_get_string (value);
1256 if (sscanf (str, "%u-%u", &rtspsrc->client_port_range.min,
1257 &rtspsrc->client_port_range.max) != 2) {
1258 rtspsrc->client_port_range.min = 0;
1259 rtspsrc->client_port_range.max = 0;
1263 case PROP_UDP_BUFFER_SIZE:
1264 rtspsrc->udp_buffer_size = g_value_get_int (value);
1266 case PROP_SHORT_HEADER:
1267 rtspsrc->short_header = g_value_get_boolean (value);
1269 case PROP_PROBATION:
1270 rtspsrc->probation = g_value_get_uint (value);
1272 case PROP_UDP_RECONNECT:
1273 rtspsrc->udp_reconnect = g_value_get_boolean (value);
1275 case PROP_MULTICAST_IFACE:
1276 g_free (rtspsrc->multi_iface);
1278 if (g_value_get_string (value) == NULL)
1279 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1281 rtspsrc->multi_iface = g_value_dup_string (value);
1284 rtspsrc->ntp_sync = g_value_get_boolean (value);
1285 /* The default value of max_ts_offset depends on ntp_sync. If user
1286 * hasn't set it then change default value */
1287 if (!rtspsrc->max_ts_offset_is_set) {
1288 if (rtspsrc->ntp_sync) {
1289 rtspsrc->max_ts_offset = 0;
1291 rtspsrc->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
1295 case PROP_USE_PIPELINE_CLOCK:
1296 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
1299 rtspsrc->sdes = g_value_dup_boxed (value);
1301 case PROP_TLS_VALIDATION_FLAGS:
1302 rtspsrc->tls_validation_flags = g_value_get_flags (value);
1304 case PROP_TLS_DATABASE:
1305 g_clear_object (&rtspsrc->tls_database);
1306 rtspsrc->tls_database = g_value_dup_object (value);
1308 case PROP_TLS_INTERACTION:
1309 g_clear_object (&rtspsrc->tls_interaction);
1310 rtspsrc->tls_interaction = g_value_dup_object (value);
1312 case PROP_DO_RETRANSMISSION:
1313 rtspsrc->do_retransmission = g_value_get_boolean (value);
1315 case PROP_NTP_TIME_SOURCE:
1316 rtspsrc->ntp_time_source = g_value_get_enum (value);
1318 case PROP_USER_AGENT:
1319 g_free (rtspsrc->user_agent);
1320 rtspsrc->user_agent = g_value_dup_string (value);
1322 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1323 rtspsrc->max_rtcp_rtp_time_diff = g_value_get_int (value);
1325 case PROP_RFC7273_SYNC:
1326 rtspsrc->rfc7273_sync = g_value_get_boolean (value);
1328 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
1329 rtspsrc->max_ts_offset_adjustment = g_value_get_uint64 (value);
1331 case PROP_MAX_TS_OFFSET:
1332 rtspsrc->max_ts_offset = g_value_get_int64 (value);
1333 rtspsrc->max_ts_offset_is_set = TRUE;
1335 case PROP_DEFAULT_VERSION:
1336 rtspsrc->default_version = g_value_get_enum (value);
1339 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1345 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1348 GstRTSPSrc *rtspsrc;
1350 rtspsrc = GST_RTSPSRC (object);
1354 g_value_set_string (value, rtspsrc->conninfo.location);
1356 case PROP_PROTOCOLS:
1357 g_value_set_flags (value, rtspsrc->protocols);
1360 g_value_set_boolean (value, rtspsrc->debug);
1363 g_value_set_uint (value, rtspsrc->retry);
1366 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1368 case PROP_TCP_TIMEOUT:
1372 timeout = ((guint64) rtspsrc->tcp_timeout.tv_sec) * G_USEC_PER_SEC +
1373 rtspsrc->tcp_timeout.tv_usec;
1374 g_value_set_uint64 (value, timeout);
1378 g_value_set_uint (value, rtspsrc->latency);
1380 case PROP_DROP_ON_LATENCY:
1381 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1383 case PROP_CONNECTION_SPEED:
1384 g_value_set_uint64 (value, rtspsrc->connection_speed);
1386 case PROP_NAT_METHOD:
1387 g_value_set_enum (value, rtspsrc->nat_method);
1390 g_value_set_boolean (value, rtspsrc->do_rtcp);
1392 case PROP_DO_RTSP_KEEP_ALIVE:
1393 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1399 if (rtspsrc->proxy_host) {
1401 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1405 g_value_take_string (value, str);
1409 g_value_set_string (value, rtspsrc->prop_proxy_id);
1412 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1414 case PROP_RTP_BLOCKSIZE:
1415 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1418 g_value_set_string (value, rtspsrc->user_id);
1421 g_value_set_string (value, rtspsrc->user_pw);
1423 case PROP_BUFFER_MODE:
1424 g_value_set_enum (value, rtspsrc->buffer_mode);
1426 case PROP_PORT_RANGE:
1430 if (rtspsrc->client_port_range.min != 0) {
1431 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1432 rtspsrc->client_port_range.max);
1436 g_value_take_string (value, str);
1439 case PROP_UDP_BUFFER_SIZE:
1440 g_value_set_int (value, rtspsrc->udp_buffer_size);
1442 case PROP_SHORT_HEADER:
1443 g_value_set_boolean (value, rtspsrc->short_header);
1445 case PROP_PROBATION:
1446 g_value_set_uint (value, rtspsrc->probation);
1448 case PROP_UDP_RECONNECT:
1449 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1451 case PROP_MULTICAST_IFACE:
1452 g_value_set_string (value, rtspsrc->multi_iface);
1455 g_value_set_boolean (value, rtspsrc->ntp_sync);
1457 case PROP_USE_PIPELINE_CLOCK:
1458 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1461 g_value_set_boxed (value, rtspsrc->sdes);
1463 case PROP_TLS_VALIDATION_FLAGS:
1464 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1466 case PROP_TLS_DATABASE:
1467 g_value_set_object (value, rtspsrc->tls_database);
1469 case PROP_TLS_INTERACTION:
1470 g_value_set_object (value, rtspsrc->tls_interaction);
1472 case PROP_DO_RETRANSMISSION:
1473 g_value_set_boolean (value, rtspsrc->do_retransmission);
1475 case PROP_NTP_TIME_SOURCE:
1476 g_value_set_enum (value, rtspsrc->ntp_time_source);
1478 case PROP_USER_AGENT:
1479 g_value_set_string (value, rtspsrc->user_agent);
1481 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1482 g_value_set_int (value, rtspsrc->max_rtcp_rtp_time_diff);
1484 case PROP_RFC7273_SYNC:
1485 g_value_set_boolean (value, rtspsrc->rfc7273_sync);
1487 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
1488 g_value_set_uint64 (value, rtspsrc->max_ts_offset_adjustment);
1490 case PROP_MAX_TS_OFFSET:
1491 g_value_set_int64 (value, rtspsrc->max_ts_offset);
1493 case PROP_DEFAULT_VERSION:
1494 g_value_set_enum (value, rtspsrc->default_version);
1497 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1503 find_stream_by_id (GstRTSPStream * stream, gint * id)
1505 if (stream->id == *id)
1512 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1514 /* ignore unconfigured channels here (e.g., those that
1515 * were explicitly skipped during SETUP) */
1516 if ((stream->channelpad[0] != NULL) &&
1517 (stream->channel[0] == *channel || stream->channel[1] == *channel))
1524 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1526 GstElement *src = (GstElement *) a;
1528 if (stream->udpsrc[0] == src)
1530 if (stream->udpsrc[1] == src)
1537 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1539 if (stream->conninfo.location) {
1540 /* check qualified setup_url */
1541 if (!strcmp (stream->conninfo.location, (gchar *) a))
1544 if (stream->control_url) {
1545 /* check original control_url */
1546 if (!strcmp (stream->control_url, (gchar *) a))
1549 /* check if qualified setup_url ends with string */
1550 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1557 static GstRTSPStream *
1558 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1562 /* find and get stream */
1563 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1564 return (GstRTSPStream *) lstream->data;
1569 static const GstSDPBandwidth *
1570 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1571 const GstSDPMedia * media, const gchar * type)
1575 /* first look in the media specific section */
1576 len = gst_sdp_media_bandwidths_len (media);
1577 for (i = 0; i < len; i++) {
1578 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1580 if (strcmp (bw->bwtype, type) == 0)
1583 /* then look in the message specific section */
1584 len = gst_sdp_message_bandwidths_len (sdp);
1585 for (i = 0; i < len; i++) {
1586 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1588 if (strcmp (bw->bwtype, type) == 0)
1595 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1596 const GstSDPMedia * media, GstRTSPStream * stream)
1598 const GstSDPBandwidth *bw;
1600 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1601 stream->as_bandwidth = bw->bandwidth;
1603 stream->as_bandwidth = -1;
1605 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1606 stream->rr_bandwidth = bw->bandwidth;
1608 stream->rr_bandwidth = -1;
1610 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1611 stream->rs_bandwidth = bw->bandwidth;
1613 stream->rs_bandwidth = -1;
1617 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1618 const GstSDPConnection * conn)
1620 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1623 if (conn->addrtype == NULL)
1626 /* check for IPV6 */
1627 if (strcmp (conn->addrtype, "IP4") == 0)
1628 stream->is_ipv6 = FALSE;
1629 else if (strcmp (conn->addrtype, "IP6") == 0)
1630 stream->is_ipv6 = TRUE;
1635 g_free (stream->destination);
1636 stream->destination = g_strdup (conn->address);
1638 /* check for multicast */
1639 stream->is_multicast =
1640 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1642 stream->ttl = conn->ttl;
1645 /* Go over the connections for a stream.
1646 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1648 * - If we are dealing with a localhost address, we disable multicast
1651 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1652 const GstSDPMedia * media, GstRTSPStream * stream)
1654 const GstSDPConnection *conn;
1657 /* first look in the media specific section */
1658 len = gst_sdp_media_connections_len (media);
1659 for (i = 0; i < len; i++) {
1660 conn = gst_sdp_media_get_connection (media, i);
1662 gst_rtspsrc_do_stream_connection (src, stream, conn);
1664 /* then look in the message specific section */
1665 if ((conn = gst_sdp_message_get_connection (sdp))) {
1666 gst_rtspsrc_do_stream_connection (src, stream, conn);
1671 make_stream_id (GstRTSPStream * stream, const GstSDPMedia * media)
1674 g_strdup_printf ("%s:%d:%d:%s:%d", media->media, media->port,
1675 media->num_ports, media->proto, stream->default_pt);
1677 g_strcanon (stream_id, G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS, ':');
1682 /* m=<media> <UDP port> RTP/AVP <payload>
1685 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
1686 const GstSDPMedia * media, GstRTSPStream * stream)
1690 GstCaps *global_caps;
1693 proto = gst_sdp_media_get_proto (media);
1697 if (g_str_equal (proto, "RTP/AVP"))
1698 stream->profile = GST_RTSP_PROFILE_AVP;
1699 else if (g_str_equal (proto, "RTP/SAVP"))
1700 stream->profile = GST_RTSP_PROFILE_SAVP;
1701 else if (g_str_equal (proto, "RTP/AVPF"))
1702 stream->profile = GST_RTSP_PROFILE_AVPF;
1703 else if (g_str_equal (proto, "RTP/SAVPF"))
1704 stream->profile = GST_RTSP_PROFILE_SAVPF;
1708 if (gst_sdp_media_get_attribute_val (media, "recvonly") != NULL)
1709 goto recvonly_media;
1711 /* Parse global SDP attributes once */
1712 global_caps = gst_caps_new_empty_simple ("application/x-unknown");
1713 GST_DEBUG ("mapping sdp session level attributes to caps");
1714 gst_sdp_message_attributes_to_caps (sdp, global_caps);
1715 GST_DEBUG ("mapping sdp media level attributes to caps");
1716 gst_sdp_media_attributes_to_caps (media, global_caps);
1718 /* Keep a copy of the SDP key management */
1719 gst_sdp_media_parse_keymgmt (media, &stream->mikey);
1720 if (stream->mikey == NULL)
1721 gst_sdp_message_parse_keymgmt (sdp, &stream->mikey);
1723 len = gst_sdp_media_formats_len (media);
1724 for (i = 0; i < len; i++) {
1726 GstCaps *caps, *outcaps;
1731 pt = atoi (gst_sdp_media_get_format (media, i));
1733 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
1736 caps = gst_sdp_media_get_caps_from_media (media, pt);
1738 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
1742 /* do some tweaks */
1743 s = gst_caps_get_structure (caps, 0);
1744 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
1745 stream->is_real = (strstr (enc, "-REAL") != NULL);
1746 if (strcmp (enc, "X-ASF-PF") == 0)
1747 stream->container = TRUE;
1750 /* Merge in global caps */
1751 /* Intersect will merge in missing fields to the current caps */
1752 outcaps = gst_caps_intersect (caps, global_caps);
1753 gst_caps_unref (caps);
1755 /* the first pt will be the default */
1756 if (stream->ptmap->len == 0)
1757 stream->default_pt = pt;
1760 item.caps = outcaps;
1762 g_array_append_val (stream->ptmap, item);
1765 stream->stream_id = make_stream_id (stream, media);
1767 gst_caps_unref (global_caps);
1772 GST_ERROR_OBJECT (src, "can't find proto in media");
1777 GST_ERROR_OBJECT (src, "unknown proto in media: '%s'", proto);
1782 GST_DEBUG_OBJECT (src, "recvonly media ignored");
1787 static const gchar *
1788 get_aggregate_control (GstRTSPSrc * src)
1793 base = src->control;
1794 else if (src->content_base)
1795 base = src->content_base;
1796 else if (src->conninfo.url_str)
1797 base = src->conninfo.url_str;
1805 clear_ptmap_item (PtMapItem * item)
1808 gst_caps_unref (item->caps);
1811 static GstRTSPStream *
1812 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx,
1815 GstRTSPStream *stream;
1816 const gchar *control_url;
1817 const GstSDPMedia *media;
1819 /* get media, should not return NULL */
1820 media = gst_sdp_message_get_media (sdp, idx);
1824 stream = g_new0 (GstRTSPStream, 1);
1825 stream->parent = src;
1826 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1828 stream->last_ret = GST_FLOW_NOT_LINKED;
1829 stream->added = FALSE;
1830 stream->setup = FALSE;
1831 stream->skipped = FALSE;
1833 stream->eos = FALSE;
1834 stream->discont = TRUE;
1835 stream->seqbase = -1;
1836 stream->timebase = -1;
1837 stream->send_ssrc = g_random_int ();
1838 stream->profile = GST_RTSP_PROFILE_AVP;
1839 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1840 stream->mikey = NULL;
1841 stream->stream_id = NULL;
1842 g_mutex_init (&stream->conninfo.send_lock);
1843 g_mutex_init (&stream->conninfo.recv_lock);
1844 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
1846 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1847 * session manager to scale RTCP. */
1848 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1850 /* collect connection info */
1851 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1853 /* make the payload type map */
1854 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1856 /* collect port number */
1857 stream->port = gst_sdp_media_get_port (media);
1859 /* get control url to construct the setup url. The setup url is used to
1860 * configure the transport of the stream and is used to identity the stream in
1861 * the RTP-Info header field returned from PLAY. */
1862 control_url = gst_sdp_media_get_attribute_val (media, "control");
1863 if (control_url == NULL)
1864 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1866 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1867 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1868 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1869 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1871 /* RFC 2326, C.3: missing control_url permitted in case of a single stream */
1872 if (control_url == NULL && n_streams == 1) {
1876 if (control_url != NULL) {
1877 stream->control_url = g_strdup (control_url);
1878 /* Build a fully qualified url using the content_base if any or by prefixing
1879 * the original request.
1880 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1881 * likely build a URL that the server will fail to understand, this is ok,
1882 * we will fail then. */
1883 if (g_str_has_prefix (control_url, "rtsp://"))
1884 stream->conninfo.location = g_strdup (control_url);
1889 if (g_strcmp0 (control_url, "*") == 0)
1892 base = get_aggregate_control (src);
1894 /* check if the base ends or control starts with / */
1895 has_slash = g_str_has_prefix (control_url, "/");
1896 has_slash = has_slash || g_str_has_suffix (base, "/");
1898 /* concatenate the two strings, insert / when not present */
1899 stream->conninfo.location =
1900 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1903 GST_DEBUG_OBJECT (src, " setup: %s",
1904 GST_STR_NULL (stream->conninfo.location));
1906 /* we keep track of all streams */
1907 src->streams = g_list_append (src->streams, stream);
1915 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1919 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1921 g_array_free (stream->ptmap, TRUE);
1923 g_free (stream->destination);
1924 g_free (stream->control_url);
1925 g_free (stream->conninfo.location);
1926 g_free (stream->stream_id);
1928 for (i = 0; i < 2; i++) {
1929 if (stream->udpsrc[i]) {
1930 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1931 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1932 gst_object_unref (stream->udpsrc[i]);
1934 if (stream->channelpad[i])
1935 gst_object_unref (stream->channelpad[i]);
1937 if (stream->udpsink[i]) {
1938 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1939 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1940 gst_object_unref (stream->udpsink[i]);
1943 if (stream->fakesrc) {
1944 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1945 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1946 gst_object_unref (stream->fakesrc);
1948 if (stream->srcpad) {
1949 gst_pad_set_active (stream->srcpad, FALSE);
1951 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1953 if (stream->srtpenc)
1954 gst_object_unref (stream->srtpenc);
1955 if (stream->srtpdec)
1956 gst_object_unref (stream->srtpdec);
1957 if (stream->srtcpparams)
1958 gst_caps_unref (stream->srtcpparams);
1960 gst_mikey_message_unref (stream->mikey);
1961 if (stream->rtcppad)
1962 gst_object_unref (stream->rtcppad);
1963 if (stream->session)
1964 g_object_unref (stream->session);
1965 if (stream->rtx_pt_map)
1966 gst_structure_free (stream->rtx_pt_map);
1968 g_mutex_clear (&stream->conninfo.send_lock);
1969 g_mutex_clear (&stream->conninfo.recv_lock);
1975 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1979 GST_DEBUG_OBJECT (src, "cleanup");
1981 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1982 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1984 gst_rtspsrc_stream_free (src, stream);
1986 g_list_free (src->streams);
1987 src->streams = NULL;
1989 if (src->manager_sig_id) {
1990 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1991 src->manager_sig_id = 0;
1993 gst_element_set_state (src->manager, GST_STATE_NULL);
1994 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1995 src->manager = NULL;
1998 gst_structure_free (src->props);
2001 g_free (src->content_base);
2002 src->content_base = NULL;
2004 g_free (src->control);
2005 src->control = NULL;
2008 gst_rtsp_range_free (src->range);
2011 /* don't clear the SDP when it was used in the url */
2012 if (src->sdp && !src->from_sdp) {
2013 gst_sdp_message_free (src->sdp);
2017 src->need_segment = FALSE;
2019 if (src->provided_clock) {
2020 gst_object_unref (src->provided_clock);
2021 src->provided_clock = NULL;
2026 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
2027 gint * rtpport, gint * rtcpport)
2030 GstStateChangeReturn ret;
2031 GstElement *udpsrc0, *udpsrc1;
2032 gint tmp_rtp, tmp_rtcp;
2036 src = stream->parent;
2042 /* Start at next port */
2043 tmp_rtp = src->next_port_num;
2045 if (stream->is_ipv6)
2046 host = "udp://[::0]";
2048 host = "udp://0.0.0.0";
2050 /* try to allocate 2 UDP ports, the RTP port should be an even
2051 * number and the RTCP port should be the next (uneven) port */
2054 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
2055 tmp_rtp >= src->client_port_range.max)
2058 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2059 if (udpsrc0 == NULL)
2060 goto no_udp_protocol;
2061 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
2063 if (src->udp_buffer_size != 0)
2064 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
2067 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
2068 if (ret == GST_STATE_CHANGE_FAILURE) {
2070 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
2073 if (++count > src->retry)
2076 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2077 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2078 gst_object_unref (udpsrc0);
2081 GST_DEBUG_OBJECT (src, "retry %d", count);
2084 goto no_udp_protocol;
2087 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
2088 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
2090 /* check if port is even */
2091 if ((tmp_rtp & 0x01) != 0) {
2092 /* port not even, close and allocate another */
2093 if (++count > src->retry)
2096 GST_DEBUG_OBJECT (src, "RTP port not even");
2098 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2099 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2100 gst_object_unref (udpsrc0);
2103 GST_DEBUG_OBJECT (src, "retry %d", count);
2108 /* allocate port+1 for RTCP now */
2109 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2110 if (udpsrc1 == NULL)
2111 goto no_udp_rtcp_protocol;
2114 tmp_rtcp = tmp_rtp + 1;
2115 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
2118 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
2120 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
2121 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
2122 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
2123 if (ret == GST_STATE_CHANGE_FAILURE) {
2124 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
2126 if (++count > src->retry)
2129 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2130 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2131 gst_object_unref (udpsrc0);
2134 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2135 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2136 gst_object_unref (udpsrc1);
2140 GST_DEBUG_OBJECT (src, "retry %d", count);
2144 /* all fine, do port check */
2145 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2146 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2148 /* this should not happen... */
2149 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2152 /* we keep these elements, we configure all in configure_transport when the
2153 * server told us to really use the UDP ports. */
2154 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2155 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2156 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2157 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2159 /* keep track of next available port number when we have a range
2161 if (src->next_port_num != 0)
2162 src->next_port_num = tmp_rtcp + 1;
2169 GST_DEBUG_OBJECT (src, "could not get UDP source");
2174 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2178 no_udp_rtcp_protocol:
2180 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2185 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2186 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2192 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2193 gst_object_unref (udpsrc0);
2196 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2197 gst_object_unref (udpsrc1);
2204 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2209 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2211 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2212 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2215 for (i = 0; i < 2; i++) {
2216 if (stream->udpsrc[i])
2217 gst_element_set_state (stream->udpsrc[i], state);
2223 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2230 event = gst_event_new_flush_start ();
2231 GST_DEBUG_OBJECT (src, "start flush");
2233 state = GST_STATE_PAUSED;
2235 event = gst_event_new_flush_stop (FALSE);
2236 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2239 state = GST_STATE_PLAYING;
2241 state = GST_STATE_PAUSED;
2243 gst_rtspsrc_push_event (src, event);
2244 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2245 gst_rtspsrc_set_state (src, state);
2248 static GstRTSPResult
2249 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2250 GstRTSPMessage * message, GTimeVal * timeout)
2254 if (conninfo->connection) {
2255 g_mutex_lock (&conninfo->send_lock);
2256 ret = gst_rtsp_connection_send (conninfo->connection, message, timeout);
2257 g_mutex_unlock (&conninfo->send_lock);
2259 ret = GST_RTSP_ERROR;
2265 static GstRTSPResult
2266 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2267 GstRTSPMessage * message, GTimeVal * timeout)
2271 if (conninfo->connection) {
2272 g_mutex_lock (&conninfo->recv_lock);
2273 ret = gst_rtsp_connection_receive (conninfo->connection, message, timeout);
2274 g_mutex_unlock (&conninfo->recv_lock);
2276 ret = GST_RTSP_ERROR;
2283 gst_rtspsrc_get_position (GstRTSPSrc * src)
2288 query = gst_query_new_position (GST_FORMAT_TIME);
2289 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2290 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2291 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2295 if (stream->srcpad) {
2296 if (gst_pad_query (stream->srcpad, query)) {
2297 gst_query_parse_position (query, &fmt, &pos);
2298 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2299 GST_TIME_ARGS (pos));
2300 src->last_pos = pos;
2310 gst_query_unref (query);
2314 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2319 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2321 gboolean flush, skip;
2324 GstSegment seeksegment = { 0, };
2326 const gchar *seek_style = NULL;
2329 GST_DEBUG_OBJECT (src, "doing seek with event %" GST_PTR_FORMAT, event);
2331 gst_event_parse_seek (event, &rate, &format, &flags,
2332 &cur_type, &cur, &stop_type, &stop);
2334 /* no negative rates yet */
2338 /* we need TIME format */
2339 if (format != src->segment.format)
2342 /* Check if we are not at all seekable */
2343 if (src->seekable == -1.0)
2346 /* Additional seeking-to-beginning-only check */
2347 if (src->seekable == 0.0 && cur != 0)
2350 GST_DEBUG_OBJECT (src, "doing seek without event");
2352 cur_type = GST_SEEK_TYPE_SET;
2353 stop_type = GST_SEEK_TYPE_SET;
2356 /* get flush flag */
2357 flush = flags & GST_SEEK_FLAG_FLUSH;
2358 skip = flags & GST_SEEK_FLAG_SKIP;
2360 /* now we need to make sure the streaming thread is stopped. We do this by
2361 * either sending a FLUSH_START event downstream which will cause the
2362 * streaming thread to stop with a WRONG_STATE.
2363 * For a non-flushing seek we simply pause the task, which will happen as soon
2364 * as it completes one iteration (and thus might block when the sink is
2365 * blocking in preroll). */
2367 GST_DEBUG_OBJECT (src, "starting flush");
2368 gst_rtspsrc_flush (src, TRUE, FALSE);
2371 gst_task_pause (src->task);
2375 /* we should now be able to grab the streaming thread because we stopped it
2376 * with the above flush/pause code */
2377 GST_RTSP_STREAM_LOCK (src);
2379 GST_DEBUG_OBJECT (src, "stopped streaming");
2381 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2382 gst_rtspsrc_connection_flush (src, FALSE);
2384 /* copy segment, we need this because we still need the old
2385 * segment when we close the current segment. */
2386 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2388 /* configure the seek parameters in the seeksegment. We will then have the
2389 * right values in the segment to perform the seek */
2391 GST_DEBUG_OBJECT (src, "configuring seek");
2392 gst_segment_do_seek (&seeksegment, rate, format, flags,
2393 cur_type, cur, stop_type, stop, &update);
2396 /* figure out the last position we need to play. If it's configured (stop !=
2397 * -1), use that, else we play until the total duration of the file */
2398 if ((stop = seeksegment.stop) == -1)
2399 stop = seeksegment.duration;
2401 /* if we were playing, pause first */
2402 playing = (src->state == GST_RTSP_STATE_PLAYING);
2404 /* obtain current position in case seek fails */
2405 gst_rtspsrc_get_position (src);
2406 gst_rtspsrc_pause (src, FALSE);
2410 src->state = GST_RTSP_STATE_SEEKING;
2412 /* PLAY will add the range header now. */
2413 src->need_range = TRUE;
2415 /* prepare for streaming again */
2417 /* if we started flush, we stop now */
2418 GST_DEBUG_OBJECT (src, "stopping flush");
2419 gst_rtspsrc_flush (src, FALSE, playing);
2422 /* now we did the seek and can activate the new segment values */
2423 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2425 /* if we're doing a segment seek, post a SEGMENT_START message */
2426 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2427 gst_element_post_message (GST_ELEMENT_CAST (src),
2428 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2429 src->segment.format, src->segment.position));
2432 /* now create the newsegment */
2433 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2434 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2437 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2438 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2439 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2440 stream->discont = TRUE;
2443 /* and continue playing if needed */
2444 GST_OBJECT_LOCK (src);
2445 playing = (GST_STATE_PENDING (src) == GST_STATE_VOID_PENDING
2446 && GST_STATE (src) == GST_STATE_PLAYING)
2447 || (GST_STATE_PENDING (src) == GST_STATE_PLAYING);
2448 GST_OBJECT_UNLOCK (src);
2450 if (src->version >= GST_RTSP_VERSION_2_0) {
2451 if (flags & GST_SEEK_FLAG_ACCURATE)
2453 else if (flags & GST_SEEK_FLAG_KEY_UNIT)
2454 seek_style = "CoRAP";
2455 else if (flags & GST_SEEK_FLAG_KEY_UNIT
2456 && flags & GST_SEEK_FLAG_SNAP_BEFORE)
2457 seek_style = "First-Prior";
2458 else if (flags & GST_SEEK_FLAG_KEY_UNIT && flags & GST_SEEK_FLAG_SNAP_AFTER)
2459 seek_style = "Next";
2463 gst_rtspsrc_play (src, &seeksegment, FALSE, seek_style);
2465 GST_RTSP_STREAM_UNLOCK (src);
2472 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2477 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2482 GST_DEBUG_OBJECT (src, "stream is not seekable");
2488 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2492 gboolean res = TRUE;
2495 src = GST_RTSPSRC_CAST (parent);
2497 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2498 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2500 switch (GST_EVENT_TYPE (event)) {
2501 case GST_EVENT_SEEK:
2502 res = gst_rtspsrc_perform_seek (src, event);
2506 case GST_EVENT_NAVIGATION:
2507 case GST_EVENT_LATENCY:
2515 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2516 res = gst_pad_send_event (target, event);
2517 gst_object_unref (target);
2519 gst_event_unref (event);
2522 gst_event_unref (event);
2529 gst_rtspsrc_handle_src_sink_event (GstPad * pad, GstObject * parent,
2532 GstRTSPStream *stream;
2534 stream = gst_pad_get_element_private (pad);
2536 switch (GST_EVENT_TYPE (event)) {
2537 case GST_EVENT_STREAM_START:{
2538 const gchar *upstream_id;
2541 gst_event_parse_stream_start (event, &upstream_id);
2542 stream_id = g_strdup_printf ("%s/%s", upstream_id, stream->stream_id);
2544 gst_event_unref (event);
2545 event = gst_event_new_stream_start (stream_id);
2553 return gst_pad_push_event (stream->srcpad, event);
2556 /* this is the final event function we receive on the internal source pad when
2557 * we deal with TCP connections */
2559 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2564 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2566 switch (GST_EVENT_TYPE (event)) {
2567 case GST_EVENT_SEEK:
2569 case GST_EVENT_NAVIGATION:
2570 case GST_EVENT_LATENCY:
2572 gst_event_unref (event);
2579 /* this is the final query function we receive on the internal source pad when
2580 * we deal with TCP connections */
2582 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2586 gboolean res = TRUE;
2588 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2590 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2591 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2593 switch (GST_QUERY_TYPE (query)) {
2594 case GST_QUERY_POSITION:
2599 case GST_QUERY_DURATION:
2603 gst_query_parse_duration (query, &format, NULL);
2606 case GST_FORMAT_TIME:
2607 gst_query_set_duration (query, format, src->segment.duration);
2615 case GST_QUERY_LATENCY:
2617 /* we are live with a min latency of 0 and unlimited max latency, this
2618 * result will be updated by the session manager if there is any. */
2619 gst_query_set_latency (query, TRUE, 0, -1);
2629 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2631 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2635 gboolean res = FALSE;
2637 src = GST_RTSPSRC_CAST (parent);
2639 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2640 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2642 switch (GST_QUERY_TYPE (query)) {
2643 case GST_QUERY_DURATION:
2647 gst_query_parse_duration (query, &format, NULL);
2650 case GST_FORMAT_TIME:
2651 gst_query_set_duration (query, format, src->segment.duration);
2659 case GST_QUERY_SEEKING:
2663 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2664 if (format == GST_FORMAT_TIME) {
2666 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2667 GstClockTime start = 0, duration = src->segment.duration;
2669 /* seeking without duration is unlikely */
2670 seekable = seekable && src->seekable >= 0.0 && src->segment.duration &&
2671 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2674 if (src->seekable > 0.0) {
2675 start = src->last_pos - src->seekable * GST_SECOND;
2677 /* src->seekable == 0 means that we can only seek to 0 */
2683 GST_LOG_OBJECT (src, "seekable : %d", seekable);
2685 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, start,
2695 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2697 gst_query_set_uri (query, uri);
2705 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2707 /* forward the query to the proxy target pad */
2709 res = gst_pad_query (target, query);
2710 gst_object_unref (target);
2719 /* callback for RTCP messages to be sent to the server when operating in TCP
2721 static GstFlowReturn
2722 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2725 GstRTSPStream *stream;
2726 GstFlowReturn res = GST_FLOW_OK;
2731 GstRTSPMessage message = { 0 };
2732 GstRTSPConnInfo *conninfo;
2734 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2735 src = stream->parent;
2737 gst_buffer_map (buffer, &map, GST_MAP_READ);
2741 gst_rtsp_message_init_data (&message, stream->channel[1]);
2743 /* lend the body data to the message */
2744 gst_rtsp_message_take_body (&message, data, size);
2746 if (stream->conninfo.connection)
2747 conninfo = &stream->conninfo;
2749 conninfo = &src->conninfo;
2751 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2752 ret = gst_rtspsrc_connection_send (src, conninfo, &message, NULL);
2753 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2755 /* and steal it away again because we will free it when unreffing the
2757 gst_rtsp_message_steal_body (&message, &data, &size);
2758 gst_rtsp_message_unset (&message);
2760 gst_buffer_unmap (buffer, &map);
2761 gst_buffer_unref (buffer);
2766 static GstPadProbeReturn
2767 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2769 GstRTSPSrc *src = user_data;
2771 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2772 GST_DEBUG_PAD_NAME (pad));
2774 /* activate the streams */
2775 GST_OBJECT_LOCK (src);
2776 if (!src->need_activate)
2779 src->need_activate = FALSE;
2780 GST_OBJECT_UNLOCK (src);
2782 gst_rtspsrc_activate_streams (src);
2784 return GST_PAD_PROBE_OK;
2788 GST_OBJECT_UNLOCK (src);
2789 return GST_PAD_PROBE_OK;
2794 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
2796 GstPad *gpad = GST_PAD_CAST (user_data);
2798 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
2799 gst_pad_store_sticky_event (gpad, *event);
2804 /* this callback is called when the session manager generated a new src pad with
2805 * payloaded RTP packets. We simply ghost the pad here. */
2807 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2810 GstPadTemplate *template;
2813 GstRTSPStream *stream;
2815 GstPad *internal_src;
2817 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2819 GST_RTSP_STATE_LOCK (src);
2821 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2822 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2823 goto unknown_stream;
2825 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2827 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2829 goto unknown_stream;
2832 stream->ssrc = ssrc;
2834 /* we'll add it later see below */
2835 stream->added = TRUE;
2837 /* check if we added all streams */
2839 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2840 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2842 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
2843 ostream, ostream->container, ostream->added, ostream->setup);
2845 /* if we find a stream for which we did a setup that is not added, we
2846 * need to wait some more */
2847 if (ostream->setup && !ostream->added) {
2852 GST_RTSP_STATE_UNLOCK (src);
2854 /* create a new pad we will use to stream to */
2855 template = gst_static_pad_template_get (&rtptemplate);
2856 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2857 gst_object_unref (template);
2860 /* We intercept and modify the stream start event */
2862 GST_PAD (gst_proxy_pad_get_internal (GST_PROXY_PAD (stream->srcpad)));
2863 gst_pad_set_element_private (internal_src, stream);
2864 gst_pad_set_event_function (internal_src, gst_rtspsrc_handle_src_sink_event);
2865 gst_object_unref (internal_src);
2867 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2868 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2869 gst_pad_set_active (stream->srcpad, TRUE);
2870 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
2871 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2874 GST_DEBUG_OBJECT (src, "We added all streams");
2875 /* when we get here, all stream are added and we can fire the no-more-pads
2877 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2885 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2886 GST_RTSP_STATE_UNLOCK (src);
2893 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
2897 len = stream->ptmap->len;
2898 for (i = 0; i < len; i++) {
2899 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2907 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2909 GstRTSPStream *stream;
2912 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2914 GST_RTSP_STATE_LOCK (src);
2915 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2917 goto unknown_stream;
2919 if ((caps = stream_get_caps_for_pt (stream, pt)))
2920 gst_caps_ref (caps);
2921 GST_RTSP_STATE_UNLOCK (src);
2927 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2928 GST_RTSP_STATE_UNLOCK (src);
2934 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2936 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2942 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2948 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2954 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2956 GstRTSPSrc *src = stream->parent;
2959 g_object_get (source, "ssrc", &ssrc, NULL);
2961 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2962 ssrc, stream->ssrc, stream->id);
2964 if (ssrc == stream->ssrc)
2965 gst_rtspsrc_do_stream_eos (src, stream);
2969 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2971 GstRTSPSrc *src = stream->parent;
2974 g_object_get (source, "ssrc", &ssrc, NULL);
2976 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2977 ssrc, stream->ssrc, stream->id);
2979 if (ssrc == stream->ssrc)
2980 gst_rtspsrc_do_stream_eos (src, stream);
2984 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2986 GstRTSPStream *stream;
2988 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2990 /* get stream for session */
2991 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2993 gst_rtspsrc_do_stream_eos (src, stream);
2998 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
3000 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
3005 set_manager_buffer_mode (GstRTSPSrc * src)
3007 GObjectClass *klass;
3009 if (src->manager == NULL)
3012 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3014 if (!g_object_class_find_property (klass, "buffer-mode"))
3017 if (src->buffer_mode != BUFFER_MODE_AUTO) {
3018 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
3023 GST_DEBUG_OBJECT (src,
3024 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
3026 if (src->provided_clock) {
3027 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
3029 if (clock == src->provided_clock) {
3030 GST_DEBUG_OBJECT (src, "selected synced");
3031 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
3034 gst_object_unref (clock);
3039 /* Otherwise fall-through and use another buffer mode */
3041 gst_object_unref (clock);
3044 GST_DEBUG_OBJECT (src, "auto buffering mode");
3045 if (src->use_buffering) {
3046 GST_DEBUG_OBJECT (src, "selected buffer");
3047 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
3049 GST_DEBUG_OBJECT (src, "selected slave");
3050 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
3055 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
3059 GstMIKEYMessage *msg = stream->mikey;
3061 GST_DEBUG ("request key SSRC %u", ssrc);
3063 caps = gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
3064 caps = gst_caps_make_writable (caps);
3066 /* parse crypto sessions and look for the SSRC rollover counter */
3067 msg = stream->mikey;
3068 for (i = 0; msg && i < gst_mikey_message_get_n_cs (msg); i++) {
3069 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
3071 if (ssrc == map->ssrc) {
3072 gst_caps_set_simple (caps, "roc", G_TYPE_UINT, map->roc, NULL);
3081 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
3083 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3084 if (stream->id != session)
3087 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3088 stream->profile != GST_RTSP_PROFILE_SAVPF)
3091 if (stream->srtpdec == NULL) {
3094 name = g_strdup_printf ("srtpdec_%u", session);
3095 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
3098 if (stream->srtpdec == NULL) {
3099 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
3100 ("no srtpdec element present!"));
3103 g_signal_connect (stream->srtpdec, "request-key",
3104 (GCallback) request_key, stream);
3106 return gst_object_ref (stream->srtpdec);
3110 request_rtcp_encoder (GstElement * rtpbin, guint session,
3111 GstRTSPStream * stream)
3116 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3117 if (stream->id != session)
3120 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3121 stream->profile != GST_RTSP_PROFILE_SAVPF)
3124 if (stream->srtpenc == NULL) {
3127 name = g_strdup_printf ("srtpenc_%u", session);
3128 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
3131 if (stream->srtpenc == NULL) {
3132 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
3133 ("no srtpenc element present!"));
3137 /* get RTCP crypto parameters from caps */
3138 s = gst_caps_get_structure (stream->srtcpparams, 0);
3142 GType ciphertype, authtype;
3143 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
3145 ciphertype = g_type_from_name ("GstSrtpCipherType");
3146 authtype = g_type_from_name ("GstSrtpAuthType");
3147 g_value_init (&rtcp_cipher, ciphertype);
3148 g_value_init (&rtcp_auth, authtype);
3150 str = gst_structure_get_string (s, "srtcp-cipher");
3151 gst_value_deserialize (&rtcp_cipher, str);
3152 str = gst_structure_get_string (s, "srtcp-auth");
3153 gst_value_deserialize (&rtcp_auth, str);
3154 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
3156 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-cipher",
3158 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-auth",
3160 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
3162 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
3164 g_object_set (stream->srtpenc, "key", buf, NULL);
3166 g_value_unset (&rtcp_cipher);
3167 g_value_unset (&rtcp_auth);
3168 gst_buffer_unref (buf);
3171 name = g_strdup_printf ("rtcp_sink_%d", session);
3172 pad = gst_element_get_request_pad (stream->srtpenc, name);
3174 gst_object_unref (pad);
3176 return gst_object_ref (stream->srtpenc);
3180 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
3182 GstElement *rtx, *bin;
3185 GstRTSPStream *stream;
3187 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
3189 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
3193 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
3194 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
3195 bin = gst_bin_new (NULL);
3196 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
3197 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
3198 gst_bin_add (GST_BIN (bin), rtx);
3200 pad = gst_element_get_static_pad (rtx, "src");
3201 name = g_strdup_printf ("src_%u", sessid);
3202 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3204 gst_object_unref (pad);
3206 pad = gst_element_get_static_pad (rtx, "sink");
3207 name = g_strdup_printf ("sink_%u", sessid);
3208 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3210 gst_object_unref (pad);
3216 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
3220 gboolean do_retransmission = FALSE;
3222 if (transport->trans != GST_RTSP_TRANS_RTP)
3224 if (transport->profile != GST_RTSP_PROFILE_AVPF &&
3225 transport->profile != GST_RTSP_PROFILE_SAVPF)
3228 signal_id = g_signal_lookup ("request-aux-receiver",
3229 G_OBJECT_TYPE (src->manager));
3230 /* there's already something connected */
3231 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
3232 NULL, NULL, NULL) != 0) {
3233 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
3234 "\"request-aux-receiver\" signal is "
3235 "already used by the application");
3239 /* build the retransmission payload type map */
3240 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3241 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3242 gboolean do_retransmission_stream = FALSE;
3245 if (stream->rtx_pt_map)
3246 gst_structure_free (stream->rtx_pt_map);
3247 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
3249 for (i = 0; i < stream->ptmap->len; i++) {
3250 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3251 GstStructure *s = gst_caps_get_structure (item->caps, 0);
3252 const gchar *encoding;
3254 /* we only care about RTX streams */
3255 if ((encoding = gst_structure_get_string (s, "encoding-name"))
3256 && g_strcmp0 (encoding, "RTX") == 0) {
3257 const gchar *stream_pt_s;
3260 if (gst_structure_get_int (s, "payload", &rtx_pt)
3261 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
3264 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
3266 do_retransmission_stream = TRUE;
3272 if (do_retransmission_stream) {
3273 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
3274 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
3275 do_retransmission = TRUE;
3277 GST_DEBUG_OBJECT (src, "no retransmission payload map for stream "
3278 "id %i", stream->id);
3279 gst_structure_free (stream->rtx_pt_map);
3280 stream->rtx_pt_map = NULL;
3284 if (do_retransmission) {
3285 GST_DEBUG_OBJECT (src, "Enabling retransmissions");
3287 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
3289 /* enable RFC4588 retransmission handling by setting rtprtxreceive
3290 * as the "aux" element of rtpbin */
3291 g_signal_connect (src->manager, "request-aux-receiver",
3292 (GCallback) request_aux_receiver, src);
3294 GST_DEBUG_OBJECT (src,
3295 "Not enabling retransmissions as no stream had a retransmission payload map");
3299 /* try to get and configure a manager */
3301 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3302 GstRTSPTransport * transport)
3304 const gchar *manager;
3306 GstStateChangeReturn ret;
3308 /* find a manager */
3309 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3313 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3315 /* configure the manager */
3316 if (src->manager == NULL) {
3317 GObjectClass *klass;
3319 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3321 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3325 goto use_no_manager;
3327 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3328 goto manager_failed;
3331 /* we manage this element */
3332 gst_element_set_locked_state (src->manager, TRUE);
3333 gst_bin_add (GST_BIN_CAST (src), src->manager);
3335 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3336 if (ret == GST_STATE_CHANGE_FAILURE)
3337 goto start_manager_failure;
3339 g_object_set (src->manager, "latency", src->latency, NULL);
3341 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3343 if (g_object_class_find_property (klass, "ntp-sync")) {
3344 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3347 if (g_object_class_find_property (klass, "rfc7273-sync")) {
3348 g_object_set (src->manager, "rfc7273-sync", src->rfc7273_sync, NULL);
3351 if (src->use_pipeline_clock) {
3352 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3353 g_object_set (src->manager, "use-pipeline-clock", TRUE, NULL);
3356 if (g_object_class_find_property (klass, "ntp-time-source")) {
3357 g_object_set (src->manager, "ntp-time-source", src->ntp_time_source,
3362 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3363 g_object_set (src->manager, "sdes", src->sdes, NULL);
3366 if (g_object_class_find_property (klass, "drop-on-latency")) {
3367 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3371 if (g_object_class_find_property (klass, "max-rtcp-rtp-time-diff")) {
3372 g_object_set (src->manager, "max-rtcp-rtp-time-diff",
3373 src->max_rtcp_rtp_time_diff, NULL);
3376 if (g_object_class_find_property (klass, "max-ts-offset-adjustment")) {
3377 g_object_set (src->manager, "max-ts-offset-adjustment",
3378 src->max_ts_offset_adjustment, NULL);
3381 if (g_object_class_find_property (klass, "max-ts-offset")) {
3382 gint64 max_ts_offset;
3384 /* setting max-ts-offset in the manager has side effects so only do it
3385 * if the value differs */
3386 g_object_get (src->manager, "max-ts-offset", &max_ts_offset, NULL);
3387 if (max_ts_offset != src->max_ts_offset) {
3388 g_object_set (src->manager, "max-ts-offset", src->max_ts_offset,
3393 /* buffer mode pauses are handled by adding offsets to buffer times,
3394 * but some depayloaders may have a hard time syncing output times
3395 * with such input times, e.g. container ones, most notably ASF */
3396 /* TODO alternatives are having an event that indicates these shifts,
3397 * or having rtsp extensions provide suggestion on buffer mode */
3398 /* valid duration implies not likely live pipeline,
3399 * so slaving in jitterbuffer does not make much sense
3400 * (and might mess things up due to bursts) */
3401 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3402 src->segment.duration && stream->container) {
3403 src->use_buffering = TRUE;
3405 src->use_buffering = FALSE;
3408 set_manager_buffer_mode (src);
3410 /* connect to signals */
3411 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3413 src->manager_sig_id =
3414 g_signal_connect (src->manager, "pad-added",
3415 (GCallback) new_manager_pad, src);
3416 src->manager_ptmap_id =
3417 g_signal_connect (src->manager, "request-pt-map",
3418 (GCallback) request_pt_map, src);
3420 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3423 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3426 if (src->do_retransmission)
3427 add_retransmission (src, transport);
3429 g_signal_connect (src->manager, "request-rtp-decoder",
3430 (GCallback) request_rtp_decoder, stream);
3431 g_signal_connect (src->manager, "request-rtcp-decoder",
3432 (GCallback) request_rtp_decoder, stream);
3433 g_signal_connect (src->manager, "request-rtcp-encoder",
3434 (GCallback) request_rtcp_encoder, stream);
3436 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3437 * into a separate RTP session. */
3438 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3439 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3441 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3442 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3445 /* now configure the bandwidth in the manager */
3446 if (g_signal_lookup ("get-internal-session",
3447 G_OBJECT_TYPE (src->manager)) != 0) {
3448 GObject *rtpsession;
3450 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3453 GstRTPProfile rtp_profile;
3455 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3457 stream->session = rtpsession;
3459 if (stream->as_bandwidth != -1) {
3460 GST_INFO_OBJECT (src, "setting AS: %f",
3461 (gdouble) (stream->as_bandwidth * 1000));
3462 g_object_set (rtpsession, "bandwidth",
3463 (gdouble) (stream->as_bandwidth * 1000), NULL);
3465 if (stream->rr_bandwidth != -1) {
3466 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3467 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3470 if (stream->rs_bandwidth != -1) {
3471 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3472 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3476 switch (stream->profile) {
3477 case GST_RTSP_PROFILE_AVPF:
3478 rtp_profile = GST_RTP_PROFILE_AVPF;
3480 case GST_RTSP_PROFILE_SAVP:
3481 rtp_profile = GST_RTP_PROFILE_SAVP;
3483 case GST_RTSP_PROFILE_SAVPF:
3484 rtp_profile = GST_RTP_PROFILE_SAVPF;
3486 case GST_RTSP_PROFILE_AVP:
3488 rtp_profile = GST_RTP_PROFILE_AVP;
3492 g_object_set (rtpsession, "rtp-profile", rtp_profile, NULL);
3494 g_object_set (rtpsession, "probation", src->probation, NULL);
3496 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3498 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3500 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3502 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3504 g_signal_connect (rtpsession, "on-ssrc-active",
3505 (GCallback) on_ssrc_active, stream);
3516 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3521 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3524 start_manager_failure:
3526 GST_DEBUG_OBJECT (src, "could not start session manager");
3531 /* free the UDP sources allocated when negotiating a transport.
3532 * This function is called when the server negotiated to a transport where the
3533 * UDP sources are not needed anymore, such as TCP or multicast. */
3535 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3539 for (i = 0; i < 2; i++) {
3540 if (stream->udpsrc[i]) {
3541 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3542 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3543 gst_object_unref (stream->udpsrc[i]);
3544 stream->udpsrc[i] = NULL;
3549 /* for TCP, create pads to send and receive data to and from the manager and to
3550 * intercept various events and queries
3553 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3554 GstRTSPTransport * transport, GstPad ** outpad)
3557 GstPadTemplate *template;
3558 GstPad *pad0, *pad1;
3560 /* configure for interleaved delivery, nothing needs to be done
3561 * here, the loop function will call the chain functions of the
3562 * session manager. */
3563 stream->channel[0] = transport->interleaved.min;
3564 stream->channel[1] = transport->interleaved.max;
3565 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3566 stream->channel[0], stream->channel[1]);
3568 /* we can remove the allocated UDP ports now */
3569 gst_rtspsrc_stream_free_udp (stream);
3571 /* no session manager, send data to srcpad directly */
3572 if (!stream->channelpad[0]) {
3573 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3575 /* create a new pad we will use to stream to */
3576 name = g_strdup_printf ("stream_%u", stream->id);
3577 template = gst_static_pad_template_get (&rtptemplate);
3578 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3579 gst_object_unref (template);
3582 /* set caps and activate */
3583 gst_pad_use_fixed_caps (stream->channelpad[0]);
3584 gst_pad_set_active (stream->channelpad[0], TRUE);
3586 *outpad = gst_object_ref (stream->channelpad[0]);
3588 GST_DEBUG_OBJECT (src, "using manager source pad");
3590 template = gst_static_pad_template_get (&anysrctemplate);
3592 /* allocate pads for sending the channel data into the manager */
3593 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3594 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3595 gst_object_unref (stream->channelpad[0]);
3596 stream->channelpad[0] = pad0;
3597 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3598 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3599 gst_pad_set_element_private (pad0, src);
3600 gst_pad_set_active (pad0, TRUE);
3602 if (stream->channelpad[1]) {
3603 /* if we have a sinkpad for the other channel, create a pad and link to the
3605 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3606 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3607 gst_pad_link_full (pad1, stream->channelpad[1],
3608 GST_PAD_LINK_CHECK_NOTHING);
3609 gst_object_unref (stream->channelpad[1]);
3610 stream->channelpad[1] = pad1;
3611 gst_pad_set_active (pad1, TRUE);
3613 gst_object_unref (template);
3615 /* setup RTCP transport back to the server if we have to. */
3616 if (src->manager && src->do_rtcp) {
3619 template = gst_static_pad_template_get (&anysinktemplate);
3621 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3622 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3623 gst_pad_set_element_private (stream->rtcppad, stream);
3624 gst_pad_set_active (stream->rtcppad, TRUE);
3626 /* get session RTCP pad */
3627 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3628 pad = gst_element_get_request_pad (src->manager, name);
3633 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3634 gst_object_unref (pad);
3637 gst_object_unref (template);
3643 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3644 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3645 gint * max, guint * ttl)
3647 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3649 if (!(*destination = transport->destination))
3650 *destination = stream->destination;
3653 /* transport first */
3654 *min = transport->port.min;
3655 *max = transport->port.max;
3656 if (*min == -1 && *max == -1) {
3657 /* then try from SDP */
3658 if (stream->port != 0) {
3659 *min = stream->port;
3660 *max = stream->port + 1;
3666 if (!(*ttl = transport->ttl))
3671 /* first take the source, then the endpoint to figure out where to send
3673 if (!(*destination = transport->source)) {
3674 if (src->conninfo.connection)
3675 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3676 else if (stream->conninfo.connection)
3678 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3682 /* for unicast we only expect the ports here */
3683 *min = transport->server_port.min;
3684 *max = transport->server_port.max;
3689 /* For multicast create UDP sources and join the multicast group. */
3691 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3692 GstRTSPTransport * transport, GstPad ** outpad)
3695 const gchar *destination;
3698 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3700 /* we can remove the allocated UDP ports now */
3701 gst_rtspsrc_stream_free_udp (stream);
3703 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3706 /* we need a destination now */
3707 if (destination == NULL)
3708 goto no_destination;
3710 /* we really need ports now or we won't be able to receive anything at all */
3711 if (min == -1 && max == -1)
3714 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3715 destination, min, max);
3717 /* creating UDP source for RTP */
3719 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3721 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3723 if (stream->udpsrc[0] == NULL)
3726 /* take ownership */
3727 gst_object_ref_sink (stream->udpsrc[0]);
3729 if (src->udp_buffer_size != 0)
3730 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3731 src->udp_buffer_size, NULL);
3733 if (src->multi_iface != NULL)
3734 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3735 src->multi_iface, NULL);
3738 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3739 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3742 /* creating another UDP source for RTCP */
3746 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3748 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3750 if (stream->udpsrc[1] == NULL)
3753 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3754 stream->profile == GST_RTSP_PROFILE_SAVPF)
3755 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3757 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3758 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3759 gst_caps_unref (caps);
3761 /* take ownership */
3762 gst_object_ref_sink (stream->udpsrc[1]);
3764 if (src->multi_iface != NULL)
3765 g_object_set (G_OBJECT (stream->udpsrc[1]), "multicast-iface",
3766 src->multi_iface, NULL);
3768 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
3775 GST_DEBUG_OBJECT (src, "no UDP source element found");
3780 GST_DEBUG_OBJECT (src, "no destination found");
3785 GST_DEBUG_OBJECT (src, "no ports found");
3790 /* configure the remainder of the UDP ports */
3792 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3793 GstRTSPTransport * transport, GstPad ** outpad)
3795 /* we manage the UDP elements now. For unicast, the UDP sources where
3796 * allocated in the stream when we suggested a transport. */
3797 if (stream->udpsrc[0]) {
3800 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3801 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3803 GST_DEBUG_OBJECT (src, "setting up UDP source");
3805 /* configure a timeout on the UDP port. When the timeout message is
3806 * posted, we assume UDP transport is not possible. We reconnect using TCP
3808 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3809 src->udp_timeout * 1000, NULL);
3811 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
3812 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3814 /* get output pad of the UDP source. */
3815 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3817 /* save it so we can unblock */
3818 stream->blockedpad = *outpad;
3820 /* configure pad block on the pad. As soon as there is dataflow on the
3821 * UDP source, we know that UDP is not blocked by a firewall and we can
3822 * configure all the streams to let the application autoplug decoders. */
3824 gst_pad_add_probe (stream->blockedpad,
3825 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3826 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
3828 if (stream->channelpad[0]) {
3829 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3830 /* configure for UDP delivery, we need to connect the UDP pads to
3831 * the session plugin. */
3832 gst_pad_link_full (*outpad, stream->channelpad[0],
3833 GST_PAD_LINK_CHECK_NOTHING);
3834 gst_object_unref (*outpad);
3836 /* we connected to pad-added signal to get pads from the manager */
3838 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3843 if (stream->udpsrc[1]) {
3846 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3847 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3849 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3850 stream->profile == GST_RTSP_PROFILE_SAVPF)
3851 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3853 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3854 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3855 gst_caps_unref (caps);
3857 if (stream->channelpad[1]) {
3860 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3862 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3863 gst_pad_link_full (pad, stream->channelpad[1],
3864 GST_PAD_LINK_CHECK_NOTHING);
3865 gst_object_unref (pad);
3867 /* leave unlinked */
3873 /* configure the UDP sink back to the server for status reports */
3875 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3876 GstRTSPStream * stream, GstRTSPTransport * transport)
3879 gint rtp_port, rtcp_port;
3880 gboolean do_rtp, do_rtcp;
3881 const gchar *destination;
3886 /* get transport info */
3887 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3888 &rtp_port, &rtcp_port, &ttl);
3890 /* see what we need to do */
3891 do_rtp = (rtp_port != -1);
3892 /* it's possible that the server does not want us to send RTCP in which case
3894 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3896 /* we need a destination when we have RTP or RTCP ports */
3897 if (destination == NULL && (do_rtp || do_rtcp))
3898 goto no_destination;
3900 /* try to construct the fakesrc to the RTP port of the server to open up any
3903 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3906 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3907 stream->udpsink[0] =
3908 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3910 if (stream->udpsink[0] == NULL)
3911 goto no_sink_element;
3913 /* don't join multicast group, we will have the source socket do that */
3914 /* no sync or async state changes needed */
3915 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3916 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3918 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3920 if (stream->udpsrc[0]) {
3921 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3922 * so that NAT firewalls will open a hole for us */
3923 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3927 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3928 /* configure socket and make sure udpsink does not close it when shutting
3929 * down, it belongs to udpsrc after all. */
3930 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3931 "close-socket", FALSE, NULL);
3932 g_object_unref (socket);
3935 /* the source for the dummy packets to open up NAT */
3936 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3937 if (stream->fakesrc == NULL)
3938 goto no_fakesrc_element;
3940 /* random data in 5 buffers, a size of 200 bytes should be fine */
3941 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3942 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3944 /* keep everything locked */
3945 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3946 gst_element_set_locked_state (stream->fakesrc, TRUE);
3948 gst_object_ref (stream->udpsink[0]);
3949 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3950 gst_object_ref (stream->fakesrc);
3951 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3953 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3954 "sink", GST_PAD_LINK_CHECK_NOTHING);
3957 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3960 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3961 stream->udpsink[1] =
3962 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3964 if (stream->udpsink[1] == NULL)
3965 goto no_sink_element;
3967 /* don't join multicast group, we will have the source socket do that */
3968 /* no sync or async state changes needed */
3969 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3970 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3972 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3974 if (stream->udpsrc[1]) {
3975 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3976 * because some servers check the port number of where it sends RTCP to identify
3977 * the RTCP packets it receives */
3978 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3982 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3983 /* configure socket and make sure udpsink does not close it when shutting
3984 * down, it belongs to udpsrc after all. */
3985 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3986 "close-socket", FALSE, NULL);
3987 g_object_unref (socket);
3990 /* we keep this playing always */
3991 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3992 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3994 gst_object_ref (stream->udpsink[1]);
3995 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3997 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3999 /* get session RTCP pad */
4000 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
4001 pad = gst_element_get_request_pad (src->manager, name);
4006 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
4007 gst_object_unref (pad);
4016 GST_ERROR_OBJECT (src, "no destination address specified");
4021 GST_ERROR_OBJECT (src, "no UDP sink element found");
4026 GST_ERROR_OBJECT (src, "no fakesrc element found");
4031 GST_ERROR_OBJECT (src, "failed to create socket");
4036 /* sets up all elements needed for streaming over the specified transport.
4037 * Does not yet expose the element pads, this will be done when there is actuall
4038 * dataflow detected, which might never happen when UDP is blocked in a
4039 * firewall, for example.
4042 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
4043 GstRTSPTransport * transport)
4046 GstPad *outpad = NULL;
4047 GstPadTemplate *template;
4049 const gchar *media_type;
4052 src = stream->parent;
4054 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
4056 /* get the proper media type for this stream now */
4057 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
4058 goto unknown_transport;
4060 goto unknown_transport;
4062 /* configure the final media type */
4063 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
4065 len = stream->ptmap->len;
4066 for (i = 0; i < len; i++) {
4068 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
4070 if (item->caps == NULL)
4073 s = gst_caps_get_structure (item->caps, 0);
4074 gst_structure_set_name (s, media_type);
4075 /* set ssrc if known */
4076 if (transport->ssrc)
4077 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
4080 /* try to get and configure a manager, channelpad[0-1] will be configured with
4081 * the pads for the manager, or NULL when no manager is needed. */
4082 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
4085 switch (transport->lower_transport) {
4086 case GST_RTSP_LOWER_TRANS_TCP:
4087 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
4088 goto transport_failed;
4090 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
4091 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
4092 goto transport_failed;
4093 /* fallthrough, the rest is the same for UDP and MCAST */
4094 case GST_RTSP_LOWER_TRANS_UDP:
4095 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
4096 goto transport_failed;
4097 /* configure udpsinks back to the server for RTCP messages and for the
4098 * dummy RTP messages to open NAT. */
4099 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
4100 goto transport_failed;
4103 goto unknown_transport;
4107 GST_DEBUG_OBJECT (src, "creating ghostpad");
4109 gst_pad_use_fixed_caps (outpad);
4111 /* create ghostpad, don't add just yet, this will be done when we activate
4113 name = g_strdup_printf ("stream_%u", stream->id);
4114 template = gst_static_pad_template_get (&rtptemplate);
4115 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
4116 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
4117 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
4118 gst_object_unref (template);
4121 gst_object_unref (outpad);
4123 /* mark pad as ok */
4124 stream->last_ret = GST_FLOW_OK;
4131 GST_DEBUG_OBJECT (src, "failed to configure transport");
4136 GST_DEBUG_OBJECT (src, "unknown transport");
4141 GST_DEBUG_OBJECT (src, "cannot get a session manager");
4146 /* send a couple of dummy random packets on the receiver RTP port to the server,
4147 * this should make a firewall think we initiated the data transfer and
4148 * hopefully allow packets to go from the sender port to our RTP receiver port */
4150 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
4154 if (src->nat_method != GST_RTSP_NAT_DUMMY)
4157 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4158 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4160 if (stream->fakesrc && stream->udpsink[0]) {
4161 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
4162 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
4163 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
4164 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
4165 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
4171 /* Adds the source pads of all configured streams to the element.
4172 * This code is performed when we detected dataflow.
4174 * We detect dataflow from either the _loop function or with pad probes on the
4178 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
4182 GST_DEBUG_OBJECT (src, "activating streams");
4184 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4185 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4187 if (stream->udpsrc[0]) {
4188 /* remove timeout, we are streaming now and timeouts will be handled by
4189 * the session manager and jitter buffer */
4190 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
4192 if (stream->srcpad) {
4193 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
4194 gst_pad_set_active (stream->srcpad, TRUE);
4196 /* if we don't have a session manager, set the caps now. If we have a
4197 * session, we will get a notification of the pad and the caps. */
4198 if (!src->manager) {
4201 caps = stream_get_caps_for_pt (stream, stream->default_pt);
4202 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
4203 gst_pad_set_caps (stream->srcpad, caps);
4206 if (!stream->added) {
4207 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
4208 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
4209 stream->added = TRUE;
4214 /* unblock all pads */
4215 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4216 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4218 if (stream->blockid) {
4219 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
4220 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
4221 stream->blockid = 0;
4229 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
4230 gboolean reset_manager)
4233 guint64 start, stop;
4234 gdouble play_speed, play_scale;
4236 GST_DEBUG_OBJECT (src, "configuring stream caps");
4238 start = segment->position;
4239 stop = segment->duration;
4240 play_speed = segment->rate;
4241 play_scale = segment->applied_rate;
4243 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4244 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4250 len = stream->ptmap->len;
4251 for (j = 0; j < len; j++) {
4253 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
4255 if (item->caps == NULL)
4258 caps = gst_caps_make_writable (item->caps);
4260 if (stream->timebase != -1)
4261 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
4262 (guint) stream->timebase, NULL);
4263 if (stream->seqbase != -1)
4264 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
4265 (guint) stream->seqbase, NULL);
4266 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
4268 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
4269 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
4270 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
4273 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
4276 if (item->pt == stream->default_pt) {
4277 if (stream->udpsrc[0])
4278 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4279 stream->need_caps = TRUE;
4283 if (reset_manager && src->manager) {
4284 GST_DEBUG_OBJECT (src, "clear session");
4285 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
4289 static GstFlowReturn
4290 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
4295 /* store the value */
4296 stream->last_ret = ret;
4298 /* if it's success we can return the value right away */
4299 if (ret == GST_FLOW_OK)
4302 /* any other error that is not-linked can be returned right
4304 if (ret != GST_FLOW_NOT_LINKED)
4307 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4308 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4309 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4311 ret = ostream->last_ret;
4312 /* some other return value (must be SUCCESS but we can return
4313 * other values as well) */
4314 if (ret != GST_FLOW_NOT_LINKED)
4317 /* if we get here, all other pads were unlinked and we return
4318 * NOT_LINKED then */
4324 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4327 gboolean res = TRUE;
4329 /* only streams that have a connection to the outside world */
4333 if (stream->udpsrc[0]) {
4334 gst_event_ref (event);
4335 res = gst_element_send_event (stream->udpsrc[0], event);
4336 } else if (stream->channelpad[0]) {
4337 gst_event_ref (event);
4338 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4339 res = gst_pad_push_event (stream->channelpad[0], event);
4341 res = gst_pad_send_event (stream->channelpad[0], event);
4344 if (stream->udpsrc[1]) {
4345 gst_event_ref (event);
4346 res &= gst_element_send_event (stream->udpsrc[1], event);
4347 } else if (stream->channelpad[1]) {
4348 gst_event_ref (event);
4349 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4350 res &= gst_pad_push_event (stream->channelpad[1], event);
4352 res &= gst_pad_send_event (stream->channelpad[1], event);
4356 gst_event_unref (event);
4362 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4365 gboolean res = TRUE;
4367 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4368 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4370 gst_event_ref (event);
4371 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4373 gst_event_unref (event);
4379 accept_certificate_cb (GTlsConnection * conn, GTlsCertificate * peer_cert,
4380 GTlsCertificateFlags errors, gpointer user_data)
4382 GstRTSPSrc *src = user_data;
4383 gboolean accept = FALSE;
4385 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ACCEPT_CERTIFICATE], 0, conn,
4386 peer_cert, errors, &accept);
4391 static GstRTSPResult
4392 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4396 GstRTSPMessage response;
4397 gboolean retry = FALSE;
4398 memset (&response, 0, sizeof (response));
4399 gst_rtsp_message_init (&response);
4401 if (info->connection == NULL) {
4402 if (info->url == NULL) {
4403 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4404 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4407 /* create connection */
4408 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4409 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4410 goto could_not_create;
4413 gst_rtspsrc_setup_auth (src, &response);
4416 g_free (info->url_str);
4417 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4419 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4421 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4422 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4423 src->tls_validation_flags))
4424 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4426 if (src->tls_database)
4427 gst_rtsp_connection_set_tls_database (info->connection,
4430 if (src->tls_interaction)
4431 gst_rtsp_connection_set_tls_interaction (info->connection,
4432 src->tls_interaction);
4433 gst_rtsp_connection_set_accept_certificate_func (info->connection,
4434 accept_certificate_cb, src, NULL);
4437 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4438 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4440 if (src->proxy_host) {
4441 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4443 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4448 if (!info->connected) {
4451 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4452 ("Connecting to %s", info->location));
4453 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4454 res = gst_rtsp_connection_connect_with_response (info->connection,
4455 src->ptcp_timeout, &response);
4457 if (response.type == GST_RTSP_MESSAGE_HTTP_RESPONSE &&
4458 response.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4459 gst_rtsp_conninfo_close (src, info, TRUE);
4463 retry = FALSE; // we should not retry more than once
4468 if (res == GST_RTSP_OK)
4469 info->connected = TRUE;
4471 goto could_not_connect;
4473 } while (!info->connected && retry);
4475 gst_rtsp_message_unset (&response);
4481 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4482 gst_rtsp_message_unset (&response);
4487 gchar *str = gst_rtsp_strresult (res);
4488 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4490 gst_rtsp_message_unset (&response);
4495 gchar *str = gst_rtsp_strresult (res);
4496 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4498 gst_rtsp_message_unset (&response);
4503 static GstRTSPResult
4504 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4507 GST_RTSP_STATE_LOCK (src);
4508 if (info->connected) {
4509 GST_DEBUG_OBJECT (src, "closing connection...");
4510 gst_rtsp_connection_close (info->connection);
4511 info->connected = FALSE;
4513 if (free && info->connection) {
4514 /* free connection */
4515 GST_DEBUG_OBJECT (src, "freeing connection...");
4516 gst_rtsp_connection_free (info->connection);
4517 info->connection = NULL;
4518 info->flushing = FALSE;
4520 GST_RTSP_STATE_UNLOCK (src);
4524 static GstRTSPResult
4525 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4530 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4531 gst_rtsp_conninfo_close (src, info, FALSE);
4532 res = gst_rtsp_conninfo_connect (src, info, async);
4538 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4542 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4543 GST_RTSP_STATE_LOCK (src);
4544 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4545 GST_DEBUG_OBJECT (src, "connection flush");
4546 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4547 src->conninfo.flushing = flush;
4549 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4550 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4551 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4552 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4553 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4554 stream->conninfo.flushing = flush;
4557 GST_RTSP_STATE_UNLOCK (src);
4560 static GstRTSPResult
4561 gst_rtspsrc_init_request (GstRTSPSrc * src, GstRTSPMessage * msg,
4562 GstRTSPMethod method, const gchar * uri)
4566 res = gst_rtsp_message_init_request (msg, method, uri);
4570 /* set user-agent */
4571 if (src->user_agent)
4572 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, src->user_agent);
4577 /* FIXME, handle server request, reply with OK, for now */
4578 static GstRTSPResult
4579 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
4580 GstRTSPMessage * request)
4582 GstRTSPMessage response = { 0 };
4585 GST_DEBUG_OBJECT (src, "got server request message");
4587 DEBUG_RTSP (src, request);
4589 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4591 if (res == GST_RTSP_ENOTIMPL) {
4592 /* default implementation, send OK */
4593 GST_DEBUG_OBJECT (src, "prepare OK reply");
4595 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4600 /* let app parse and reply */
4601 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4602 0, request, &response);
4604 DEBUG_RTSP (src, &response);
4606 res = gst_rtspsrc_connection_send (src, conninfo, &response, NULL);
4610 gst_rtsp_message_unset (&response);
4611 } else if (res == GST_RTSP_EEOF)
4619 gst_rtsp_message_unset (&response);
4624 /* send server keep-alive */
4625 static GstRTSPResult
4626 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4628 GstRTSPMessage request = { 0 };
4630 GstRTSPMethod method;
4631 const gchar *control;
4633 if (src->do_rtsp_keep_alive == FALSE) {
4634 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4635 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4639 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4641 /* find a method to use for keep-alive */
4642 if (src->methods & GST_RTSP_GET_PARAMETER)
4643 method = GST_RTSP_GET_PARAMETER;
4645 method = GST_RTSP_OPTIONS;
4647 control = get_aggregate_control (src);
4648 if (control == NULL)
4651 res = gst_rtspsrc_init_request (src, &request, method, control);
4655 request.type_data.request.version = src->version;
4657 res = gst_rtspsrc_connection_send (src, &src->conninfo, &request, NULL);
4661 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4662 gst_rtsp_message_unset (&request);
4669 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4674 gchar *str = gst_rtsp_strresult (res);
4676 gst_rtsp_message_unset (&request);
4677 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4678 ("Could not send keep-alive. (%s)", str));
4684 static GstFlowReturn
4685 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4687 GstFlowReturn ret = GST_FLOW_OK;
4689 GstRTSPStream *stream;
4690 GstPad *outpad = NULL;
4696 channel = message->type_data.data.channel;
4698 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4700 goto unknown_stream;
4702 if (channel == stream->channel[0]) {
4703 outpad = stream->channelpad[0];
4705 } else if (channel == stream->channel[1]) {
4706 outpad = stream->channelpad[1];
4712 /* take a look at the body to figure out what we have */
4713 gst_rtsp_message_get_body (message, &data, &size);
4715 goto invalid_length;
4717 /* channels are not correct on some servers, do extra check */
4718 if (data[1] >= 200 && data[1] <= 204) {
4719 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4720 outpad = stream->channelpad[1];
4724 /* we have no clue what this is, just ignore then. */
4726 goto unknown_stream;
4728 /* take the message body for further processing */
4729 gst_rtsp_message_steal_body (message, &data, &size);
4731 /* strip the trailing \0 */
4734 buf = gst_buffer_new ();
4735 gst_buffer_append_memory (buf,
4736 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4738 /* don't need message anymore */
4739 gst_rtsp_message_unset (message);
4741 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4744 if (src->need_activate) {
4750 guint group_id = gst_util_group_id_next ();
4752 /* generate an SHA256 sum of the URI */
4753 cs = g_checksum_new (G_CHECKSUM_SHA256);
4754 uri = src->conninfo.location;
4755 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4757 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4758 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4762 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4763 event = gst_event_new_stream_start (stream_id);
4764 gst_event_set_group_id (event, group_id);
4767 gst_rtspsrc_stream_push_event (src, ostream, event);
4769 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
4770 /* only streams that have a connection to the outside world */
4771 if (ostream->setup) {
4772 if (ostream->udpsrc[0]) {
4773 gst_element_send_event (ostream->udpsrc[0],
4774 gst_event_new_caps (caps));
4775 } else if (ostream->channelpad[0]) {
4776 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
4777 gst_pad_push_event (ostream->channelpad[0],
4778 gst_event_new_caps (caps));
4780 gst_pad_send_event (ostream->channelpad[0],
4781 gst_event_new_caps (caps));
4783 ostream->need_caps = FALSE;
4785 if (ostream->profile == GST_RTSP_PROFILE_SAVP ||
4786 ostream->profile == GST_RTSP_PROFILE_SAVPF)
4787 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4789 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4791 if (ostream->udpsrc[1]) {
4792 gst_element_send_event (ostream->udpsrc[1],
4793 gst_event_new_caps (caps));
4794 } else if (ostream->channelpad[1]) {
4795 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
4796 gst_pad_push_event (ostream->channelpad[1],
4797 gst_event_new_caps (caps));
4799 gst_pad_send_event (ostream->channelpad[1],
4800 gst_event_new_caps (caps));
4803 gst_caps_unref (caps);
4807 g_checksum_free (cs);
4809 gst_rtspsrc_activate_streams (src);
4810 src->need_activate = FALSE;
4811 src->need_segment = TRUE;
4814 if (src->base_time == -1) {
4815 /* Take current running_time. This timestamp will be put on
4816 * the first buffer of each stream because we are a live source and so we
4817 * timestamp with the running_time. When we are dealing with TCP, we also
4818 * only timestamp the first buffer (using the DISCONT flag) because a server
4819 * typically bursts data, for which we don't want to compensate by speeding
4820 * up the media. The other timestamps will be interpollated from this one
4821 * using the RTP timestamps. */
4822 GST_OBJECT_LOCK (src);
4823 if (GST_ELEMENT_CLOCK (src)) {
4825 GstClockTime base_time;
4827 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4828 base_time = GST_ELEMENT_CAST (src)->base_time;
4830 src->base_time = now - base_time;
4832 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4833 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4835 GST_OBJECT_UNLOCK (src);
4838 /* If needed send a new segment, don't forget we are live and buffer are
4839 * timestamped with running time */
4840 if (src->need_segment) {
4842 src->need_segment = FALSE;
4843 gst_segment_init (&segment, GST_FORMAT_TIME);
4844 gst_rtspsrc_push_event (src, gst_event_new_segment (&segment));
4847 if (stream->need_caps) {
4850 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt))) {
4851 /* only streams that have a connection to the outside world */
4852 if (stream->setup) {
4853 /* Only need to update the TCP caps here, UDP is already handled */
4854 if (stream->channelpad[0]) {
4855 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4856 gst_pad_push_event (stream->channelpad[0],
4857 gst_event_new_caps (caps));
4859 gst_pad_send_event (stream->channelpad[0],
4860 gst_event_new_caps (caps));
4862 stream->need_caps = FALSE;
4866 stream->need_caps = FALSE;
4869 if (stream->discont && !is_rtcp) {
4870 /* mark first RTP buffer as discont */
4871 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4872 stream->discont = FALSE;
4873 /* first buffer gets the timestamp, other buffers are not timestamped and
4874 * their presentation time will be interpollated from the rtp timestamps. */
4875 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4876 GST_TIME_ARGS (src->base_time));
4878 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4881 /* chain to the peer pad */
4882 if (GST_PAD_IS_SINK (outpad))
4883 ret = gst_pad_chain (outpad, buf);
4885 ret = gst_pad_push (outpad, buf);
4888 /* combine all stream flows for the data transport */
4889 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4896 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4897 gst_rtsp_message_unset (message);
4902 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4903 ("Short message received, ignoring."));
4904 gst_rtsp_message_unset (message);
4909 static GstFlowReturn
4910 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4912 GstRTSPMessage message = { 0 };
4914 GstFlowReturn ret = GST_FLOW_OK;
4915 GTimeVal tv_timeout;
4918 /* get the next timeout interval */
4919 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4921 /* see if the timeout period expired */
4922 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4923 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4924 /* send keep-alive, only act on interrupt, a warning will be posted for
4926 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4928 /* get new timeout */
4929 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4932 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4933 tv_timeout.tv_sec, tv_timeout.tv_usec);
4935 /* protect the connection with the connection lock so that we can see when
4936 * we are finished doing server communication */
4938 gst_rtspsrc_connection_receive (src, &src->conninfo,
4939 &message, src->ptcp_timeout);
4943 GST_DEBUG_OBJECT (src, "we received a server message");
4945 case GST_RTSP_EINTR:
4946 /* we got interrupted this means we need to stop */
4948 case GST_RTSP_ETIMEOUT:
4949 /* no reply, send keep alive */
4950 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4951 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4955 /* go EOS when the server closed the connection */
4961 switch (message.type) {
4962 case GST_RTSP_MESSAGE_REQUEST:
4963 /* server sends us a request message, handle it */
4964 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
4965 if (res == GST_RTSP_EEOF)
4968 goto handle_request_failed;
4970 case GST_RTSP_MESSAGE_RESPONSE:
4971 /* we ignore response messages */
4972 GST_DEBUG_OBJECT (src, "ignoring response message");
4973 DEBUG_RTSP (src, &message);
4975 case GST_RTSP_MESSAGE_DATA:
4976 GST_DEBUG_OBJECT (src, "got data message");
4977 ret = gst_rtspsrc_handle_data (src, &message);
4978 if (ret != GST_FLOW_OK)
4979 goto handle_data_failed;
4982 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4987 g_assert_not_reached ();
4992 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4993 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4994 ("The server closed the connection."));
4995 src->conninfo.connected = FALSE;
4996 gst_rtsp_message_unset (&message);
4997 return GST_FLOW_EOS;
5001 gst_rtsp_message_unset (&message);
5002 GST_DEBUG_OBJECT (src, "got interrupted");
5003 return GST_FLOW_FLUSHING;
5007 gchar *str = gst_rtsp_strresult (res);
5009 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5010 ("Could not receive message. (%s)", str));
5013 gst_rtsp_message_unset (&message);
5014 return GST_FLOW_ERROR;
5016 handle_request_failed:
5018 gchar *str = gst_rtsp_strresult (res);
5020 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5021 ("Could not handle server message. (%s)", str));
5023 gst_rtsp_message_unset (&message);
5024 return GST_FLOW_ERROR;
5028 GST_DEBUG_OBJECT (src, "could no handle data message");
5033 static GstFlowReturn
5034 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
5037 GstRTSPMessage message = { 0 };
5041 GTimeVal tv_timeout;
5043 /* get the next timeout interval */
5044 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
5046 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
5047 (gint) tv_timeout.tv_sec);
5049 gst_rtsp_message_unset (&message);
5051 /* we should continue reading the TCP socket because the server might
5052 * send us requests. When the session timeout expires, we need to send a
5053 * keep-alive request to keep the session open. */
5054 res = gst_rtspsrc_connection_receive (src, &src->conninfo,
5055 &message, &tv_timeout);
5059 GST_DEBUG_OBJECT (src, "we received a server message");
5061 case GST_RTSP_EINTR:
5062 /* we got interrupted, see what we have to do */
5064 case GST_RTSP_ETIMEOUT:
5065 /* send keep-alive, ignore the result, a warning will be posted. */
5066 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
5067 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5071 /* server closed the connection. not very fatal for UDP, reconnect and
5072 * see what happens. */
5073 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5074 ("The server closed the connection."));
5075 if (src->udp_reconnect) {
5077 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
5084 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
5086 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5087 ("Unhandled return value %d.", res));
5091 switch (message.type) {
5092 case GST_RTSP_MESSAGE_REQUEST:
5093 /* server sends us a request message, handle it */
5094 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
5095 if (res == GST_RTSP_EEOF)
5098 goto handle_request_failed;
5100 case GST_RTSP_MESSAGE_RESPONSE:
5101 /* we ignore response and data messages */
5102 GST_DEBUG_OBJECT (src, "ignoring response message");
5103 DEBUG_RTSP (src, &message);
5104 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
5105 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
5106 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
5107 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
5108 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5115 case GST_RTSP_MESSAGE_DATA:
5116 /* we ignore response and data messages */
5117 GST_DEBUG_OBJECT (src, "ignoring data message");
5120 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5125 g_assert_not_reached ();
5127 /* we get here when the connection got interrupted */
5130 gst_rtsp_message_unset (&message);
5131 GST_DEBUG_OBJECT (src, "got interrupted");
5132 return GST_FLOW_FLUSHING;
5136 gchar *str = gst_rtsp_strresult (res);
5139 src->conninfo.connected = FALSE;
5140 if (res != GST_RTSP_EINTR) {
5141 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
5142 ("Could not connect to server. (%s)", str));
5144 ret = GST_FLOW_ERROR;
5146 ret = GST_FLOW_FLUSHING;
5152 gchar *str = gst_rtsp_strresult (res);
5154 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5155 ("Could not receive message. (%s)", str));
5157 return GST_FLOW_ERROR;
5159 handle_request_failed:
5161 gchar *str = gst_rtsp_strresult (res);
5164 gst_rtsp_message_unset (&message);
5165 if (res != GST_RTSP_EINTR) {
5166 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5167 ("Could not handle server message. (%s)", str));
5169 ret = GST_FLOW_ERROR;
5171 ret = GST_FLOW_FLUSHING;
5177 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5178 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5179 ("The server closed the connection."));
5180 src->conninfo.connected = FALSE;
5181 gst_rtsp_message_unset (&message);
5182 return GST_FLOW_EOS;
5186 static GstRTSPResult
5187 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
5189 GstRTSPResult res = GST_RTSP_OK;
5192 GST_DEBUG_OBJECT (src, "doing reconnect");
5194 GST_OBJECT_LOCK (src);
5195 /* only restart when the pads were not yet activated, else we were
5196 * streaming over UDP */
5197 restart = src->need_activate;
5198 GST_OBJECT_UNLOCK (src);
5200 /* no need to restart, we're done */
5204 /* we can try only TCP now */
5205 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
5207 /* close and cleanup our state */
5208 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
5211 /* see if we have TCP left to try. Also don't try TCP when we were configured
5213 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
5216 /* We post a warning message now to inform the user
5217 * that nothing happened. It's most likely a firewall thing. */
5218 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5219 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5220 "firewall is blocking it. Retrying using a tcp connection.",
5221 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
5223 /* open new connection using tcp */
5224 if (gst_rtspsrc_open (src, async) < 0)
5227 /* start playback */
5228 if (gst_rtspsrc_play (src, &src->segment, async, NULL) < 0)
5237 src->cur_protocols = 0;
5238 /* no transport possible, post an error and stop */
5239 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5240 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5241 "firewall is blocking it. No other protocols to try.",
5242 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
5243 return GST_RTSP_ERROR;
5247 GST_DEBUG_OBJECT (src, "open failed");
5252 GST_DEBUG_OBJECT (src, "play failed");
5258 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
5262 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
5265 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
5268 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
5271 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
5279 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
5283 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
5286 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
5289 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
5292 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
5300 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
5304 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
5307 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
5310 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
5313 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
5321 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
5325 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
5328 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
5331 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
5334 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
5342 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
5344 if (ret == GST_RTSP_OK)
5345 gst_rtspsrc_loop_complete_cmd (src, cmd);
5346 else if (ret == GST_RTSP_EINTR)
5347 gst_rtspsrc_loop_cancel_cmd (src, cmd);
5349 gst_rtspsrc_loop_error_cmd (src, cmd);
5353 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
5356 gboolean flushed = FALSE;
5358 /* start new request */
5359 gst_rtspsrc_loop_start_cmd (src, cmd);
5361 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
5363 GST_OBJECT_LOCK (src);
5364 old = src->pending_cmd;
5365 if (old == CMD_RECONNECT) {
5366 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
5367 cmd = CMD_RECONNECT;
5368 } else if (old == CMD_CLOSE) {
5369 /* our CMD_CLOSE might have interrutped CMD_LOOP. gst_rtspsrc_loop
5370 * will send a CMD_WAIT which would cancel our pending CMD_CLOSE (if
5371 * still pending). We just avoid it here by making sure CMD_CLOSE is
5372 * still the pending command. */
5373 GST_DEBUG_OBJECT (src, "ignore, we were closing");
5375 } else if (old != CMD_WAIT) {
5376 src->pending_cmd = CMD_WAIT;
5377 GST_OBJECT_UNLOCK (src);
5378 /* cancel previous request */
5379 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
5380 gst_rtspsrc_loop_cancel_cmd (src, old);
5381 GST_OBJECT_LOCK (src);
5383 src->pending_cmd = cmd;
5384 /* interrupt if allowed */
5385 if (src->busy_cmd & mask) {
5386 GST_DEBUG_OBJECT (src, "connection flush busy %s",
5387 cmd_to_string (src->busy_cmd));
5388 gst_rtspsrc_connection_flush (src, TRUE);
5391 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
5392 cmd_to_string (src->busy_cmd));
5395 gst_task_start (src->task);
5396 GST_OBJECT_UNLOCK (src);
5402 gst_rtspsrc_loop (GstRTSPSrc * src)
5406 if (!src->conninfo.connection || !src->conninfo.connected)
5409 if (src->interleaved)
5410 ret = gst_rtspsrc_loop_interleaved (src);
5412 ret = gst_rtspsrc_loop_udp (src);
5414 if (ret != GST_FLOW_OK)
5422 GST_WARNING_OBJECT (src, "we are not connected");
5423 ret = GST_FLOW_FLUSHING;
5428 const gchar *reason = gst_flow_get_name (ret);
5430 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
5431 src->running = FALSE;
5432 if (ret == GST_FLOW_EOS) {
5433 /* perform EOS logic */
5434 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5435 gst_element_post_message (GST_ELEMENT_CAST (src),
5436 gst_message_new_segment_done (GST_OBJECT_CAST (src),
5437 src->segment.format, src->segment.position));
5438 gst_rtspsrc_push_event (src,
5439 gst_event_new_segment_done (src->segment.format,
5440 src->segment.position));
5442 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5444 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
5445 /* for fatal errors we post an error message, post the error before the
5446 * EOS so the app knows about the error first. */
5447 GST_ELEMENT_FLOW_ERROR (src, ret);
5448 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5450 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
5455 #ifndef GST_DISABLE_GST_DEBUG
5456 static const gchar *
5457 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
5461 while (method != 0) {
5478 /* Parse a WWW-Authenticate Response header and determine the
5479 * available authentication methods
5481 * This code should also cope with the fact that each WWW-Authenticate
5482 * header can contain multiple challenge methods + tokens
5484 * At the moment, for Basic auth, we just do a minimal check and don't
5485 * even parse out the realm */
5487 gst_rtspsrc_parse_auth_hdr (GstRTSPMessage * response,
5488 GstRTSPAuthMethod * methods, GstRTSPConnection * conn, gboolean * stale)
5490 GstRTSPAuthCredential **credentials, **credential;
5492 g_return_if_fail (response != NULL);
5493 g_return_if_fail (methods != NULL);
5494 g_return_if_fail (stale != NULL);
5497 gst_rtsp_message_parse_auth_credentials (response,
5498 GST_RTSP_HDR_WWW_AUTHENTICATE);
5502 credential = credentials;
5503 while (*credential) {
5504 if ((*credential)->scheme == GST_RTSP_AUTH_BASIC) {
5505 *methods |= GST_RTSP_AUTH_BASIC;
5506 } else if ((*credential)->scheme == GST_RTSP_AUTH_DIGEST) {
5507 GstRTSPAuthParam **param = (*credential)->params;
5509 *methods |= GST_RTSP_AUTH_DIGEST;
5511 gst_rtsp_connection_clear_auth_params (conn);
5515 if (strcmp ((*param)->name, "stale") == 0
5516 && g_ascii_strcasecmp ((*param)->value, "TRUE") == 0)
5518 gst_rtsp_connection_set_auth_param (conn, (*param)->name,
5527 gst_rtsp_auth_credentials_free (credentials);
5531 * gst_rtspsrc_setup_auth:
5532 * @src: the rtsp source
5534 * Configure a username and password and auth method on the
5535 * connection object based on a response we received from the
5538 * Currently, this requires that a username and password were supplied
5539 * in the uri. In the future, they may be requested on demand by sending
5540 * a message up the bus.
5542 * Returns: TRUE if authentication information could be set up correctly.
5545 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5549 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5550 GstRTSPAuthMethod method;
5551 GstRTSPResult auth_result;
5553 GstRTSPConnection *conn;
5554 gboolean stale = FALSE;
5556 conn = src->conninfo.connection;
5558 /* Identify the available auth methods and see if any are supported */
5559 gst_rtspsrc_parse_auth_hdr (response, &avail_methods, conn, &stale);
5561 if (avail_methods == GST_RTSP_AUTH_NONE)
5562 goto no_auth_available;
5564 /* For digest auth, if the response indicates that the session
5565 * data are stale, we just update them in the connection object and
5566 * return TRUE to retry the request */
5568 src->tried_url_auth = FALSE;
5570 url = gst_rtsp_connection_get_url (conn);
5572 /* Do we have username and password available? */
5573 if (url != NULL && !src->tried_url_auth && url->user != NULL
5574 && url->passwd != NULL) {
5577 src->tried_url_auth = TRUE;
5578 GST_DEBUG_OBJECT (src,
5579 "Attempting authentication using credentials from the URL");
5581 user = src->user_id;
5582 pass = src->user_pw;
5583 GST_DEBUG_OBJECT (src,
5584 "Attempting authentication using credentials from the properties");
5587 /* FIXME: If the url didn't contain username and password or we tried them
5588 * already, request a username and passwd from the application via some kind
5589 * of credentials request message */
5591 /* If we don't have a username and passwd at this point, bail out. */
5592 if (user == NULL || pass == NULL)
5595 /* Try to configure for each available authentication method, strongest to
5597 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5598 /* Check if this method is available on the server */
5599 if ((method & avail_methods) == 0)
5602 /* Pass the credentials to the connection to try on the next request */
5603 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5604 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5605 * ignore it and end up retrying later */
5606 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5607 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5608 gst_rtsp_auth_method_to_string (method));
5613 if (method == GST_RTSP_AUTH_NONE)
5614 goto no_auth_available;
5620 /* Output an error indicating that we couldn't connect because there were
5621 * no supported authentication protocols */
5622 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5623 ("No supported authentication protocol was found"));
5628 /* We don't fire an error message, we just return FALSE and let the
5629 * normal NOT_AUTHORIZED error be propagated */
5634 static GstRTSPResult
5635 gst_rtsp_src_receive_response (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
5636 GstRTSPMessage * response, GstRTSPStatusCode * code)
5638 GstRTSPStatusCode thecode;
5639 gchar *content_base = NULL;
5640 GstRTSPResult res = gst_rtspsrc_connection_receive (src, conninfo,
5641 response, src->ptcp_timeout);
5646 DEBUG_RTSP (src, response);
5648 switch (response->type) {
5649 case GST_RTSP_MESSAGE_REQUEST:
5650 res = gst_rtspsrc_handle_request (src, conninfo, response);
5651 if (res == GST_RTSP_EEOF)
5654 goto handle_request_failed;
5656 /* Not a response, receive next message */
5657 return gst_rtsp_src_receive_response (src, conninfo, response, code);
5658 case GST_RTSP_MESSAGE_RESPONSE:
5659 /* ok, a response is good */
5660 GST_DEBUG_OBJECT (src, "received response message");
5662 case GST_RTSP_MESSAGE_DATA:
5663 /* get next response */
5664 GST_DEBUG_OBJECT (src, "handle data response message");
5665 gst_rtspsrc_handle_data (src, response);
5667 /* Not a response, receive next message */
5668 return gst_rtsp_src_receive_response (src, conninfo, response, code);
5670 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5673 /* Not a response, receive next message */
5674 return gst_rtsp_src_receive_response (src, conninfo, response, code);
5677 thecode = response->type_data.response.code;
5679 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5681 /* if the caller wanted the result code, we store it. */
5685 /* If the request didn't succeed, bail out before doing any more */
5686 if (thecode != GST_RTSP_STS_OK)
5689 /* store new content base if any */
5690 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5693 g_free (src->content_base);
5694 src->content_base = g_strdup (content_base);
5704 return GST_RTSP_EEOF;
5707 gchar *str = gst_rtsp_strresult (res);
5709 if (res != GST_RTSP_EINTR) {
5710 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5711 ("Could not receive message. (%s)", str));
5713 GST_WARNING_OBJECT (src, "receive interrupted");
5721 handle_request_failed:
5723 /* ERROR was posted */
5724 gst_rtsp_message_unset (response);
5729 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5730 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5731 ("The server closed the connection."));
5732 gst_rtsp_message_unset (response);
5738 static GstRTSPResult
5739 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
5740 GstRTSPMessage * request, GstRTSPMessage * response,
5741 GstRTSPStatusCode * code)
5745 gboolean allow_send = TRUE;
5748 if (!src->short_header)
5749 gst_rtsp_ext_list_before_send (src->extensions, request);
5751 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_BEFORE_SEND], 0,
5752 request, &allow_send);
5754 GST_DEBUG_OBJECT (src, "skipping message, disabled by signal");
5758 GST_DEBUG_OBJECT (src, "sending message");
5760 DEBUG_RTSP (src, request);
5762 res = gst_rtspsrc_connection_send (src, conninfo, request, src->ptcp_timeout);
5766 gst_rtsp_connection_reset_timeout (conninfo->connection);
5770 res = gst_rtsp_src_receive_response (src, conninfo, response, code);
5771 if (res == GST_RTSP_EEOF) {
5772 GST_WARNING_OBJECT (src, "server closed connection");
5773 /* only try once after reconnect, then fallthrough and error out */
5774 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5776 /* if reconnect succeeds, try again */
5777 if ((res = gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) == 0)
5781 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5787 gchar *str = gst_rtsp_strresult (res);
5789 if (res != GST_RTSP_EINTR) {
5790 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5791 ("Could not send message. (%s)", str));
5793 GST_WARNING_OBJECT (src, "send interrupted");
5802 * @src: the rtsp source
5803 * @conninfo: the connection information to send on
5804 * @request: must point to a valid request
5805 * @response: must point to an empty #GstRTSPMessage
5806 * @code: an optional code result
5807 * @versions: List of versions to try, setting it back onto the @request message
5808 * if not set, `src->version` will be used as RTSP version.
5810 * send @request and retrieve the response in @response. optionally @code can be
5811 * non-NULL in which case it will contain the status code of the response.
5813 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5814 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5816 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5817 * @response message) if the response code was not 200 (OK).
5819 * If the attempt results in an authentication failure, then this will attempt
5820 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5823 * Returns: #GST_RTSP_OK if the processing was successful.
5825 static GstRTSPResult
5826 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
5827 GstRTSPMessage * request, GstRTSPMessage * response,
5828 GstRTSPStatusCode * code, GstRTSPVersion * versions)
5830 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5831 GstRTSPResult res = GST_RTSP_ERROR;
5834 GstRTSPMethod method = GST_RTSP_INVALID;
5835 gint version_retry = 0;
5841 /* make sure we don't loop forever */
5845 /* save method so we can disable it when the server complains */
5846 method = request->type_data.request.method;
5849 request->type_data.request.version = src->version;
5852 gst_rtspsrc_try_send (src, conninfo, request, response,
5857 case GST_RTSP_STS_UNAUTHORIZED:
5858 case GST_RTSP_STS_NOT_FOUND:
5859 if (gst_rtspsrc_setup_auth (src, response)) {
5860 /* Try the request/response again after configuring the auth info
5865 case GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED:
5866 GST_INFO_OBJECT (src, "Version %s not supported by the server",
5867 versions ? gst_rtsp_version_as_text (versions[version_retry]) :
5869 if (versions && versions[version_retry] != GST_RTSP_VERSION_INVALID) {
5870 GST_INFO_OBJECT (src, "Unsupported version %s => trying %s",
5871 gst_rtsp_version_as_text (request->type_data.request.version),
5872 gst_rtsp_version_as_text (versions[version_retry]));
5873 request->type_data.request.version = versions[version_retry];
5882 } while (retry == TRUE);
5884 /* If the user requested the code, let them handle errors, otherwise
5885 * post an error below */
5888 else if (int_code != GST_RTSP_STS_OK)
5889 goto error_response;
5896 GST_DEBUG_OBJECT (src, "got error %d", res);
5901 res = GST_RTSP_ERROR;
5903 switch (response->type_data.response.code) {
5904 case GST_RTSP_STS_NOT_FOUND:
5905 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_FOUND,
5908 case GST_RTSP_STS_UNAUTHORIZED:
5909 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_AUTHORIZED,
5912 case GST_RTSP_STS_MOVED_PERMANENTLY:
5913 case GST_RTSP_STS_MOVE_TEMPORARILY:
5915 gchar *new_location;
5916 GstRTSPLowerTrans transports;
5918 GST_DEBUG_OBJECT (src, "got redirection");
5919 /* if we don't have a Location Header, we must error */
5920 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5921 &new_location, 0) < 0)
5924 /* When we receive a redirect result, we go back to the INIT state after
5925 * parsing the new URI. The caller should do the needed steps to issue
5926 * a new setup when it detects this state change. */
5927 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5929 /* save current transports */
5930 if (src->conninfo.url)
5931 transports = src->conninfo.url->transports;
5933 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5935 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5937 /* set old transports */
5938 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5939 src->conninfo.url->transports = transports;
5941 src->need_redirect = TRUE;
5945 case GST_RTSP_STS_NOT_ACCEPTABLE:
5946 case GST_RTSP_STS_NOT_IMPLEMENTED:
5947 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5948 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5949 gst_rtsp_method_as_text (method));
5950 src->methods &= ~method;
5954 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, READ,
5958 /* if we return ERROR we should unset the response ourselves */
5959 if (res == GST_RTSP_ERROR)
5960 gst_rtsp_message_unset (response);
5966 static GstRTSPResult
5967 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5968 GstRTSPMessage * response, GstRTSPSrc * src)
5970 return gst_rtspsrc_send (src, &src->conninfo, request, response, NULL, NULL);
5974 /* parse the response and collect all the supported methods. We need this
5975 * information so that we don't try to send an unsupported request to the
5979 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5981 GstRTSPHeaderField field;
5985 /* reset supported methods */
5988 /* Try Allow Header first */
5989 field = GST_RTSP_HDR_ALLOW;
5992 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5996 src->methods |= gst_rtsp_options_from_text (respoptions);
6002 field = GST_RTSP_HDR_PUBLIC;
6005 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6009 src->methods |= gst_rtsp_options_from_text (respoptions);
6014 if (src->methods == 0) {
6015 /* neither Allow nor Public are required, assume the server supports
6016 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
6018 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
6019 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
6021 /* always assume PLAY, FIXME, extensions should be able to override
6023 src->methods |= GST_RTSP_PLAY;
6024 /* also assume it will support Range */
6025 src->seekable = G_MAXFLOAT;
6027 /* we need describe and setup */
6028 if (!(src->methods & GST_RTSP_DESCRIBE))
6030 if (!(src->methods & GST_RTSP_SETUP))
6038 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6039 ("Server does not support DESCRIBE."));
6044 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6045 ("Server does not support SETUP."));
6050 /* masks to be kept in sync with the hardcoded protocol order of preference
6052 static const guint protocol_masks[] = {
6053 GST_RTSP_LOWER_TRANS_UDP,
6054 GST_RTSP_LOWER_TRANS_UDP_MCAST,
6055 GST_RTSP_LOWER_TRANS_TCP,
6059 static GstRTSPResult
6060 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
6061 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
6065 gboolean add_udp_str;
6070 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
6075 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
6077 /* extension listed transports, use those */
6078 if (*transports != NULL)
6081 /* it's the default */
6082 add_udp_str = FALSE;
6084 /* the default RTSP transports */
6085 result = g_string_new ("RTP");
6088 case GST_RTSP_PROFILE_AVP:
6089 g_string_append (result, "/AVP");
6091 case GST_RTSP_PROFILE_SAVP:
6092 g_string_append (result, "/SAVP");
6094 case GST_RTSP_PROFILE_AVPF:
6095 g_string_append (result, "/AVPF");
6097 case GST_RTSP_PROFILE_SAVPF:
6098 g_string_append (result, "/SAVPF");
6104 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
6105 GST_DEBUG_OBJECT (src, "adding UDP unicast");
6107 g_string_append (result, "/UDP");
6108 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
6109 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
6110 GST_DEBUG_OBJECT (src, "adding UDP multicast");
6111 /* we don't have to allocate any UDP ports yet, if the selected transport
6112 * turns out to be multicast we can create them and join the multicast
6113 * group indicated in the transport reply */
6115 g_string_append (result, "/UDP");
6116 g_string_append (result, ";multicast");
6117 if (src->next_port_num != 0) {
6118 if (src->client_port_range.max > 0 &&
6119 src->next_port_num >= src->client_port_range.max)
6122 g_string_append_printf (result, ";client_port=%d-%d",
6123 src->next_port_num, src->next_port_num + 1);
6125 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
6126 GST_DEBUG_OBJECT (src, "adding TCP");
6128 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
6130 *transports = g_string_free (result, FALSE);
6132 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
6139 GST_ERROR ("extension gave error %d", res);
6144 GST_ERROR ("no more ports available");
6145 return GST_RTSP_ERROR;
6149 static GstRTSPResult
6150 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
6151 gint orig_rtpport, gint orig_rtcpport)
6154 gint nr_udp, nr_int;
6156 gint rtpport = 0, rtcpport = 0;
6159 src = stream->parent;
6161 /* find number of placeholders first */
6162 if (strstr (*transports, "%%i2"))
6164 else if (strstr (*transports, "%%i1"))
6169 if (strstr (*transports, "%%u2"))
6171 else if (strstr (*transports, "%%u1"))
6176 if (nr_udp == 0 && nr_int == 0)
6180 if (!orig_rtpport || !orig_rtcpport) {
6181 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
6184 rtpport = orig_rtpport;
6185 rtcpport = orig_rtcpport;
6189 str = g_string_new ("");
6191 while ((next = strstr (p, "%%"))) {
6192 g_string_append_len (str, p, next - p);
6193 if (next[2] == 'u') {
6195 g_string_append_printf (str, "%d", rtpport);
6196 else if (next[3] == '2')
6197 g_string_append_printf (str, "%d", rtcpport);
6199 if (next[2] == 'i') {
6201 g_string_append_printf (str, "%d", src->free_channel);
6202 else if (next[3] == '2')
6203 g_string_append_printf (str, "%d", src->free_channel + 1);
6209 if (src->version >= GST_RTSP_VERSION_2_0)
6210 src->free_channel += 2;
6212 /* append final part */
6213 g_string_append (str, p);
6215 g_free (*transports);
6216 *transports = g_string_free (str, FALSE);
6224 GST_ERROR ("failed to allocate udp ports");
6225 return GST_RTSP_ERROR;
6230 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
6232 GstCaps *caps = NULL;
6234 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
6238 GST_DEBUG_OBJECT (src, "SRTP parameters received");
6244 default_srtcp_params (void)
6251 guint data_size = GST_ROUND_UP_4 (KEY_SIZE);
6253 /* create a random key */
6254 key_data = g_malloc (data_size);
6255 for (i = 0; i < data_size; i += 4)
6256 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
6258 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
6260 caps = gst_caps_new_simple ("application/x-srtcp",
6261 "srtp-key", GST_TYPE_BUFFER, buf,
6262 "srtp-cipher", G_TYPE_STRING, "aes-128-icm",
6263 "srtp-auth", G_TYPE_STRING, "hmac-sha1-80",
6264 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
6265 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
6267 gst_buffer_unref (buf);
6273 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
6275 gchar *base64, *result = NULL;
6276 GstMIKEYMessage *mikey_msg;
6278 stream->srtcpparams = signal_get_srtcp_params (src, stream);
6279 if (stream->srtcpparams == NULL)
6280 stream->srtcpparams = default_srtcp_params ();
6282 mikey_msg = gst_mikey_message_new_from_caps (stream->srtcpparams);
6284 /* add policy '0' for our SSRC */
6285 gst_mikey_message_add_cs_srtp (mikey_msg, 0, stream->send_ssrc, 0);
6287 base64 = gst_mikey_message_base64_encode (mikey_msg);
6288 gst_mikey_message_unref (mikey_msg);
6291 result = gst_sdp_make_keymgmt (stream->conninfo.location, base64);
6299 static GstRTSPResult
6300 gst_rtsp_src_setup_stream_from_response (GstRTSPSrc * src,
6301 GstRTSPStream * stream, GstRTSPMessage * response,
6302 GstRTSPLowerTrans * protocols, gint retry, gint * rtpport, gint * rtcpport)
6304 gchar *resptrans = NULL;
6305 GstRTSPTransport transport = { 0 };
6307 gst_rtsp_message_get_header (response, GST_RTSP_HDR_TRANSPORT, &resptrans, 0);
6309 gst_rtspsrc_stream_free_udp (stream);
6313 /* parse transport, go to next stream on parse error */
6314 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6315 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6316 return GST_RTSP_ELAST;
6319 /* update allowed transports for other streams. once the transport of
6320 * one stream has been determined, we make sure that all other streams
6321 * are configured in the same way */
6322 switch (transport.lower_transport) {
6323 case GST_RTSP_LOWER_TRANS_TCP:
6324 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6326 *protocols = GST_RTSP_LOWER_TRANS_TCP;
6327 src->interleaved = TRUE;
6328 if (src->version < GST_RTSP_VERSION_2_0) {
6329 /* update free channels */
6330 src->free_channel = MAX (transport.interleaved.min, src->free_channel);
6331 src->free_channel = MAX (transport.interleaved.max, src->free_channel);
6332 src->free_channel++;
6335 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6336 /* only allow multicast for other streams */
6337 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6339 *protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6340 /* if the server selected our ports, increment our counters so that
6341 * we select a new port later */
6342 if (src->next_port_num == transport.port.min &&
6343 src->next_port_num + 1 == transport.port.max) {
6344 src->next_port_num += 2;
6347 case GST_RTSP_LOWER_TRANS_UDP:
6348 /* only allow unicast for other streams */
6349 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6351 *protocols = GST_RTSP_LOWER_TRANS_UDP;
6354 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6355 transport.lower_transport);
6359 if (!src->interleaved || !retry) {
6360 /* now configure the stream with the selected transport */
6361 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6362 GST_DEBUG_OBJECT (src,
6363 "could not configure stream %p transport, skipping stream", stream);
6365 } else if (stream->udpsrc[0] && stream->udpsrc[1] && rtpport && rtcpport) {
6366 /* retain the first allocated UDP port pair */
6367 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", rtpport, NULL);
6368 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", rtcpport, NULL);
6371 /* we need to activate at least one stream when we detect activity */
6372 src->need_activate = TRUE;
6374 /* stream is setup now */
6375 stream->setup = TRUE;
6376 stream->waiting_setup_response = FALSE;
6378 if (src->version >= GST_RTSP_VERSION_2_0) {
6379 gchar *prop, *media_properties;
6383 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_MEDIA_PROPERTIES,
6384 &media_properties, 0) != GST_RTSP_OK) {
6385 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6386 ("Error: No MEDIA_PROPERTY header in a SETUP request in RTSP 2.0"
6387 " - this header is mandatory."));
6389 gst_rtsp_message_unset (response);
6390 return GST_RTSP_ERROR;
6393 props = g_strsplit (media_properties, ",", -2);
6394 for (i = 0; props[i]; i++) {
6397 while (*prop == ' ')
6400 if (strstr (prop, "Random-Access")) {
6401 gchar **random_seekable_val = g_strsplit (prop, "=", 2);
6403 if (!random_seekable_val[1])
6404 src->seekable = G_MAXFLOAT;
6406 src->seekable = g_ascii_strtod (random_seekable_val[1], NULL);
6408 g_strfreev (random_seekable_val);
6409 } else if (!g_strcmp0 (prop, "No-Seeking")) {
6410 src->seekable = -1.0;
6411 } else if (!g_strcmp0 (prop, "Beginning-Only")) {
6412 src->seekable = 0.0;
6420 /* clean up our transport struct */
6421 gst_rtsp_transport_init (&transport);
6422 /* clean up used RTSP messages */
6423 gst_rtsp_message_unset (response);
6429 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6430 ("Server did not select transport."));
6432 gst_rtsp_message_unset (response);
6433 return GST_RTSP_ERROR;
6437 static GstRTSPResult
6438 gst_rtspsrc_setup_streams_end (GstRTSPSrc * src, gboolean async)
6441 GstRTSPConnInfo *conninfo;
6443 g_assert (src->version >= GST_RTSP_VERSION_2_0);
6445 conninfo = &src->conninfo;
6446 for (tmp = src->streams; tmp; tmp = tmp->next) {
6447 GstRTSPStream *stream = (GstRTSPStream *) tmp->data;
6448 GstRTSPMessage response = { 0, };
6450 if (!stream->waiting_setup_response)
6453 if (!src->conninfo.connection)
6454 conninfo = &((GstRTSPStream *) tmp->data)->conninfo;
6456 gst_rtsp_src_receive_response (src, conninfo, &response, NULL);
6458 gst_rtsp_src_setup_stream_from_response (src, stream,
6459 &response, NULL, 0, NULL, NULL);
6465 /* Perform the SETUP request for all the streams.
6467 * We ask the server for a specific transport, which initially includes all the
6468 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
6469 * two local UDP ports that we send to the server.
6471 * Once the server replied with a transport, we configure the other streams
6472 * with the same transport.
6474 * In case setup request are not pipelined, this function will also configure the
6475 * stream for the selected transport, * which basically means creating the pipeline.
6476 * Otherwise, the first stream is setup right away from the reply and a
6477 * CMD_FINALIZE_SETUP command is set for the stream pipelines to happen on the
6478 * remaining streams from the RTSP thread.
6480 static GstRTSPResult
6481 gst_rtspsrc_setup_streams_start (GstRTSPSrc * src, gboolean async)
6484 GstRTSPResult res = GST_RTSP_ERROR;
6485 GstRTSPMessage request = { 0 };
6486 GstRTSPMessage response = { 0 };
6487 GstRTSPStream *stream = NULL;
6488 GstRTSPLowerTrans protocols;
6489 GstRTSPStatusCode code;
6490 gboolean unsupported_real = FALSE;
6491 gint rtpport, rtcpport;
6494 gchar *pipelined_request_id = NULL;
6496 if (src->conninfo.connection) {
6497 url = gst_rtsp_connection_get_url (src->conninfo.connection);
6498 /* we initially allow all configured lower transports. based on the URL
6499 * transports and the replies from the server we narrow them down. */
6500 protocols = url->transports & src->cur_protocols;
6503 protocols = src->cur_protocols;
6509 /* reset some state */
6510 src->free_channel = 0;
6511 src->interleaved = FALSE;
6512 src->need_activate = FALSE;
6513 /* keep track of next port number, 0 is random */
6514 src->next_port_num = src->client_port_range.min;
6515 rtpport = rtcpport = 0;
6517 if (G_UNLIKELY (src->streams == NULL))
6520 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6521 GstRTSPConnInfo *conninfo;
6528 stream = (GstRTSPStream *) walk->data;
6530 caps = stream_get_caps_for_pt (stream, stream->default_pt);
6532 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
6536 if (stream->skipped) {
6537 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
6541 /* see if we need to configure this stream */
6542 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
6543 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
6548 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
6549 stream->id, caps, &selected);
6551 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
6555 /* merge/overwrite global caps */
6560 s = gst_caps_get_structure (caps, 0);
6562 num = gst_structure_n_fields (src->props);
6563 for (j = 0; j < num; j++) {
6567 name = gst_structure_nth_field_name (src->props, j);
6568 val = gst_structure_get_value (src->props, name);
6569 gst_structure_set_value (s, name, val);
6571 GST_DEBUG_OBJECT (src, "copied %s", name);
6575 /* skip setup if we have no URL for it */
6576 if (stream->conninfo.location == NULL) {
6577 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
6581 if (src->conninfo.connection == NULL) {
6582 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
6583 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
6586 conninfo = &stream->conninfo;
6588 conninfo = &src->conninfo;
6590 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
6591 stream->conninfo.location);
6593 /* if we have a multicast connection, only suggest multicast from now on */
6594 if (stream->is_multicast)
6595 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
6598 /* first selectable protocol */
6599 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6601 if (!protocol_masks[mask])
6605 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
6606 protocol_masks[mask]);
6607 /* create a string with first transport in line */
6609 res = gst_rtspsrc_create_transports_string (src,
6610 protocols & protocol_masks[mask], stream->profile, &transports);
6611 if (res < 0 || transports == NULL)
6612 goto setup_transport_failed;
6614 if (strlen (transports) == 0) {
6615 g_free (transports);
6616 GST_DEBUG_OBJECT (src, "no transports found");
6621 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
6623 /* replace placeholders with real values, this function will optionally
6624 * allocate UDP ports and other info needed to execute the setup request */
6625 res = gst_rtspsrc_prepare_transports (stream, &transports,
6626 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
6628 g_free (transports);
6629 goto setup_transport_failed;
6632 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
6633 /* create SETUP request */
6635 gst_rtspsrc_init_request (src, &request, GST_RTSP_SETUP,
6636 stream->conninfo.location);
6638 g_free (transports);
6639 goto create_request_failed;
6642 if (src->version >= GST_RTSP_VERSION_2_0) {
6643 if (!pipelined_request_id)
6644 pipelined_request_id = g_strdup_printf ("%d",
6645 g_random_int_range (0, G_MAXINT32));
6647 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_PIPELINED_REQUESTS,
6648 pipelined_request_id);
6649 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT_RANGES,
6650 "npt, clock, smpte, clock");
6653 /* select transport */
6654 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
6657 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
6658 stream->profile == GST_RTSP_PROFILE_SAVPF) {
6659 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
6660 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
6663 /* if the user wants a non default RTP packet size we add the blocksize
6665 if (src->rtp_blocksize > 0) {
6666 hval = g_strdup_printf ("%d", src->rtp_blocksize);
6667 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
6671 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
6674 /* handle the code ourselves */
6676 gst_rtspsrc_send (src, conninfo, &request,
6677 pipelined_request_id ? NULL : &response, &code, NULL);
6682 case GST_RTSP_STS_OK:
6684 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
6685 gst_rtsp_message_unset (&request);
6686 gst_rtsp_message_unset (&response);
6687 /* cleanup of leftover transport */
6688 gst_rtspsrc_stream_free_udp (stream);
6689 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
6690 * we might be in this case */
6691 if (stream->container && rtpport && rtcpport && !retry) {
6692 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
6697 /* this transport did not go down well, but we may have others to try
6698 * that we did not send yet, try those and only give up then
6699 * but not without checking for lost cause/extension so we can
6700 * post a nicer/more useful error message later */
6701 if (!unsupported_real)
6702 unsupported_real = stream->is_real;
6703 /* select next available protocol, give up on this stream if none */
6705 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6707 if (!protocol_masks[mask] || unsupported_real)
6712 /* cleanup of leftover transport and move to the next stream */
6713 gst_rtspsrc_stream_free_udp (stream);
6714 goto response_error;
6718 if (!pipelined_request_id) {
6719 /* parse response transport */
6720 res = gst_rtsp_src_setup_stream_from_response (src, stream,
6721 &response, &protocols, retry, &rtpport, &rtcpport);
6723 case GST_RTSP_ERROR:
6725 case GST_RTSP_ELAST:
6731 stream->waiting_setup_response = TRUE;
6732 /* we need to activate at least one stream when we detect activity */
6733 src->need_activate = TRUE;
6740 GstRTSPStream *sskip;
6742 skip = g_list_next (skip);
6746 sskip = (GstRTSPStream *) skip->data;
6748 /* skip all streams with the same control url */
6749 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
6750 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
6751 sskip, sskip->conninfo.location);
6752 sskip->skipped = TRUE;
6756 gst_rtsp_message_unset (&request);
6759 if (pipelined_request_id) {
6760 gst_rtspsrc_setup_streams_end (src, TRUE);
6763 /* store the transport protocol that was configured */
6764 src->cur_protocols = protocols;
6766 gst_rtsp_ext_list_stream_select (src->extensions, url);
6768 if (pipelined_request_id)
6769 g_free (pipelined_request_id);
6771 /* if there is nothing to activate, error out */
6772 if (!src->need_activate)
6773 goto nothing_to_activate;
6780 /* no transport possible, post an error and stop */
6781 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6782 ("Could not connect to server, no protocols left"));
6783 return GST_RTSP_ERROR;
6787 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6788 ("SDP contains no streams"));
6789 return GST_RTSP_ERROR;
6791 create_request_failed:
6793 gchar *str = gst_rtsp_strresult (res);
6795 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6796 ("Could not create request. (%s)", str));
6800 setup_transport_failed:
6802 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6803 ("Could not setup transport."));
6804 res = GST_RTSP_ERROR;
6809 const gchar *str = gst_rtsp_status_as_text (code);
6811 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6812 ("Error (%d): %s", code, GST_STR_NULL (str)));
6813 res = GST_RTSP_ERROR;
6818 gchar *str = gst_rtsp_strresult (res);
6820 if (res != GST_RTSP_EINTR) {
6821 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6822 ("Could not send message. (%s)", str));
6824 GST_WARNING_OBJECT (src, "send interrupted");
6829 nothing_to_activate:
6831 /* none of the available error codes is really right .. */
6832 if (unsupported_real) {
6833 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6834 (_("No supported stream was found. You might need to install a "
6835 "GStreamer RTSP extension plugin for Real media streams.")),
6838 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6839 (_("No supported stream was found. You might need to allow "
6840 "more transport protocols or may otherwise be missing "
6841 "the right GStreamer RTSP extension plugin.")), (NULL));
6843 return GST_RTSP_ERROR;
6847 if (pipelined_request_id)
6848 g_free (pipelined_request_id);
6849 gst_rtsp_message_unset (&request);
6850 gst_rtsp_message_unset (&response);
6856 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
6857 GstSegment * segment)
6860 GstRTSPTimeRange *therange;
6863 gst_rtsp_range_free (src->range);
6865 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
6866 GST_DEBUG_OBJECT (src, "parsed range %s", range);
6867 src->range = therange;
6869 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6871 gst_segment_init (segment, GST_FORMAT_TIME);
6875 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6876 therange->min.type, therange->min.seconds, therange->max.type,
6877 therange->max.seconds);
6879 if (therange->min.type == GST_RTSP_TIME_NOW)
6881 else if (therange->min.type == GST_RTSP_TIME_END)
6884 seconds = therange->min.seconds * GST_SECOND;
6886 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6887 GST_TIME_ARGS (seconds));
6889 /* we need to start playback without clipping from the position reported by
6891 segment->start = seconds;
6892 segment->position = seconds;
6894 if (therange->max.type == GST_RTSP_TIME_NOW)
6896 else if (therange->max.type == GST_RTSP_TIME_END)
6899 seconds = therange->max.seconds * GST_SECOND;
6901 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6902 GST_TIME_ARGS (seconds));
6904 /* live (WMS) server might send overflowed large max as its idea of infinity,
6905 * compensate to prevent problems later on */
6906 if (seconds != -1 && seconds < 0) {
6908 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6911 /* live (WMS) might send min == max, which is not worth recording */
6912 if (segment->duration == -1 && seconds == segment->start)
6915 /* don't change duration with unknown value, we might have a valid value
6916 * there that we want to keep. */
6918 segment->duration = seconds;
6923 /* Parse clock profived by the server with following syntax:
6925 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6928 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6930 gboolean res = FALSE;
6932 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6933 gchar **fields = NULL, **parts = NULL;
6934 gchar *remote_ip, *str;
6936 GstClockTime base_time;
6939 fields = g_strsplit (gstclock, " ", 0);
6941 /* wrapped clock, not very interesting for now */
6942 if (fields[1] == NULL)
6945 /* remote IP address and port */
6946 if ((str = fields[2]) == NULL)
6949 parts = g_strsplit (str, ":", 0);
6951 if ((remote_ip = parts[0]) == NULL)
6954 if ((str = parts[1]) == NULL)
6962 if ((str = fields[3]) == NULL)
6965 base_time = g_ascii_strtoull (str, NULL, 10);
6968 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
6971 if (src->provided_clock)
6972 gst_object_unref (src->provided_clock);
6973 src->provided_clock = netclock;
6975 gst_element_post_message (GST_ELEMENT_CAST (src),
6976 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
6977 src->provided_clock, TRUE));
6981 g_strfreev (fields);
6987 /* must be called with the RTSP state lock */
6988 static GstRTSPResult
6989 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
6995 /* prepare global stream caps properties */
6997 gst_structure_remove_all_fields (src->props);
6999 src->props = gst_structure_new_empty ("RTSPProperties");
7001 DEBUG_SDP (src, sdp);
7003 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
7005 /* let the app inspect and change the SDP */
7006 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
7008 gst_segment_init (&src->segment, GST_FORMAT_TIME);
7010 /* parse range for duration reporting. */
7015 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
7019 /* keep track of the range and configure it in the segment */
7020 if (gst_rtspsrc_parse_range (src, range, &src->segment))
7024 /* parse clock information. This is GStreamer specific, a server can tell the
7025 * client what clock it is using and wrap that in a network clock. The
7026 * advantage of that is that we can slave to it. */
7028 const gchar *gstclock;
7031 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
7032 if (gstclock == NULL)
7035 /* parse the clock and expose it in the provide_clock method */
7036 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
7040 /* try to find a global control attribute. Note that a '*' means that we should
7041 * do aggregate control with the current url (so we don't do anything and
7042 * leave the current connection as is) */
7044 const gchar *control;
7047 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
7048 if (control == NULL)
7051 /* only take fully qualified urls */
7052 if (g_str_has_prefix (control, "rtsp://"))
7056 g_free (src->conninfo.location);
7057 src->conninfo.location = g_strdup (control);
7058 /* make a connection for this, if there was a connection already, nothing
7060 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
7061 GST_ERROR_OBJECT (src, "could not connect");
7064 /* we need to keep the control url separate from the connection url because
7065 * the rules for constructing the media control url need it */
7066 g_free (src->control);
7067 src->control = g_strdup (control);
7070 /* create streams */
7071 n_streams = gst_sdp_message_medias_len (sdp);
7072 for (i = 0; i < n_streams; i++) {
7073 gst_rtspsrc_create_stream (src, sdp, i, n_streams);
7076 src->state = GST_RTSP_STATE_INIT;
7079 if ((res = gst_rtspsrc_setup_streams_start (src, async)) < 0)
7082 /* reset our state */
7083 src->need_range = TRUE;
7086 src->state = GST_RTSP_STATE_READY;
7093 GST_ERROR_OBJECT (src, "setup failed");
7094 gst_rtspsrc_cleanup (src);
7099 static GstRTSPResult
7100 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
7104 GstRTSPMessage request = { 0 };
7105 GstRTSPMessage response = { 0 };
7108 gchar *respcont = NULL;
7109 GstRTSPVersion versions[] =
7110 { GST_RTSP_VERSION_2_0, GST_RTSP_VERSION_INVALID };
7112 src->version = src->default_version;
7113 if (src->default_version == GST_RTSP_VERSION_2_0) {
7114 versions[0] = GST_RTSP_VERSION_1_0;
7118 src->need_redirect = FALSE;
7120 /* can't continue without a valid url */
7121 if (G_UNLIKELY (src->conninfo.url == NULL)) {
7122 res = GST_RTSP_EINVAL;
7125 src->tried_url_auth = FALSE;
7127 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
7128 goto connect_failed;
7130 /* create OPTIONS */
7131 GST_DEBUG_OBJECT (src, "create options... (%s)", async ? "async" : "sync");
7133 gst_rtspsrc_init_request (src, &request, GST_RTSP_OPTIONS,
7134 src->conninfo.url_str);
7136 goto create_request_failed;
7139 request.type_data.request.version = src->version;
7140 GST_DEBUG_OBJECT (src, "send options...");
7143 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
7146 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
7147 NULL, versions)) < 0) {
7151 src->version = request.type_data.request.version;
7152 GST_INFO_OBJECT (src, "Now using version: %s",
7153 gst_rtsp_version_as_text (src->version));
7156 if (!gst_rtspsrc_parse_methods (src, &response))
7159 /* create DESCRIBE */
7160 GST_DEBUG_OBJECT (src, "create describe...");
7162 gst_rtspsrc_init_request (src, &request, GST_RTSP_DESCRIBE,
7163 src->conninfo.url_str);
7165 goto create_request_failed;
7167 /* we only accept SDP for now */
7168 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
7172 GST_DEBUG_OBJECT (src, "send describe...");
7175 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
7178 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
7182 /* we only perform redirect for describe and play, currently */
7183 if (src->need_redirect) {
7184 /* close connection, we don't have to send a TEARDOWN yet, ignore the
7186 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7188 gst_rtsp_message_unset (&request);
7189 gst_rtsp_message_unset (&response);
7195 /* it could be that the DESCRIBE method was not implemented */
7196 if (!(src->methods & GST_RTSP_DESCRIBE))
7199 /* check if reply is SDP */
7200 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
7202 /* could not be set but since the request returned OK, we assume it
7203 * was SDP, else check it. */
7205 const gchar *props = strchr (respcont, ';');
7208 gchar *mimetype = g_strndup (respcont, props - respcont);
7210 mimetype = g_strstrip (mimetype);
7211 if (g_ascii_strcasecmp (mimetype, "application/sdp") != 0) {
7213 goto wrong_content_type;
7216 /* TODO: Check for charset property and do conversions of all messages if
7217 * needed. Some servers actually send that property */
7220 } else if (g_ascii_strcasecmp (respcont, "application/sdp") != 0) {
7221 goto wrong_content_type;
7225 /* get message body and parse as SDP */
7226 gst_rtsp_message_get_body (&response, &data, &size);
7227 if (data == NULL || size == 0)
7230 GST_DEBUG_OBJECT (src, "parse SDP...");
7231 gst_sdp_message_new (sdp);
7232 gst_sdp_message_parse_buffer (data, size, *sdp);
7234 /* clean up any messages */
7235 gst_rtsp_message_unset (&request);
7236 gst_rtsp_message_unset (&response);
7243 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
7244 ("No valid RTSP URL was provided"));
7249 gchar *str = gst_rtsp_strresult (res);
7251 if (res != GST_RTSP_EINTR) {
7252 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
7253 ("Failed to connect. (%s)", str));
7255 GST_WARNING_OBJECT (src, "connect interrupted");
7260 create_request_failed:
7262 gchar *str = gst_rtsp_strresult (res);
7264 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7265 ("Could not create request. (%s)", str));
7271 /* Don't post a message - the rtsp_send method will have
7272 * taken care of it because we passed NULL for the response code */
7277 /* error was posted */
7278 res = GST_RTSP_ERROR;
7283 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7284 ("Server does not support SDP, got %s.", respcont));
7285 res = GST_RTSP_ERROR;
7290 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7291 ("Server can not provide an SDP."));
7292 res = GST_RTSP_ERROR;
7297 if (src->conninfo.connection) {
7298 GST_DEBUG_OBJECT (src, "free connection");
7299 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7301 gst_rtsp_message_unset (&request);
7302 gst_rtsp_message_unset (&response);
7307 static GstRTSPResult
7308 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
7313 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
7315 if (src->sdp == NULL) {
7316 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
7320 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
7325 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
7332 GST_WARNING_OBJECT (src, "can't get sdp");
7333 src->open_error = TRUE;
7338 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
7339 src->open_error = TRUE;
7344 static GstRTSPResult
7345 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
7347 GstRTSPMessage request = { 0 };
7348 GstRTSPMessage response = { 0 };
7349 GstRTSPResult res = GST_RTSP_OK;
7351 const gchar *control;
7353 GST_DEBUG_OBJECT (src, "TEARDOWN...");
7355 gst_rtspsrc_set_state (src, GST_STATE_READY);
7357 if (src->state < GST_RTSP_STATE_READY) {
7358 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
7365 /* construct a control url */
7366 control = get_aggregate_control (src);
7368 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
7371 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7372 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7373 const gchar *setup_url;
7374 GstRTSPConnInfo *info;
7376 /* try aggregate control first but do non-aggregate control otherwise */
7378 setup_url = control;
7379 else if ((setup_url = stream->conninfo.location) == NULL)
7382 if (src->conninfo.connection) {
7383 info = &src->conninfo;
7384 } else if (stream->conninfo.connection) {
7385 info = &stream->conninfo;
7389 if (!info->connected)
7394 gst_rtspsrc_init_request (src, &request, GST_RTSP_TEARDOWN, setup_url);
7396 goto create_request_failed;
7399 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
7402 gst_rtspsrc_send (src, info, &request, &response, NULL, NULL)) < 0)
7405 /* FIXME, parse result? */
7406 gst_rtsp_message_unset (&request);
7407 gst_rtsp_message_unset (&response);
7410 /* early exit when we did aggregate control */
7416 /* close connections */
7417 GST_DEBUG_OBJECT (src, "closing connection...");
7418 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7419 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7420 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7421 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
7425 gst_rtspsrc_cleanup (src);
7427 src->state = GST_RTSP_STATE_INVALID;
7430 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
7435 create_request_failed:
7437 gchar *str = gst_rtsp_strresult (res);
7439 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7440 ("Could not create request. (%s)", str));
7446 gchar *str = gst_rtsp_strresult (res);
7448 gst_rtsp_message_unset (&request);
7449 if (res != GST_RTSP_EINTR) {
7450 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7451 ("Could not send message. (%s)", str));
7453 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
7460 GST_DEBUG_OBJECT (src,
7461 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
7466 /* RTP-Info is of the format:
7468 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
7470 * rtptime corresponds to the timestamp for the NPT time given in the header
7471 * seqbase corresponds to the next sequence number we received. This number
7472 * indicates the first seqnum after the seek and should be used to discard
7473 * packets that are from before the seek.
7476 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
7481 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
7483 infos = g_strsplit (rtpinfo, ",", 0);
7484 for (i = 0; infos[i]; i++) {
7486 GstRTSPStream *stream;
7490 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
7492 /* init values, types of seqbase and timebase are bigger than needed so we
7493 * can store -1 as uninitialized values */
7498 /* parse url, find stream for url.
7499 * parse seq and rtptime. The seq number should be configured in the rtp
7500 * depayloader or session manager to detect gaps. Same for the rtptime, it
7501 * should be used to create an initial time newsegment. */
7502 fields = g_strsplit (infos[i], ";", 0);
7503 for (j = 0; fields[j]; j++) {
7504 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
7505 /* remove leading whitespace */
7506 fields[j] = g_strchug (fields[j]);
7507 if (g_str_has_prefix (fields[j], "url=")) {
7508 /* get the url and the stream */
7510 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
7511 } else if (g_str_has_prefix (fields[j], "seq=")) {
7512 seqbase = atoi (fields[j] + 4);
7513 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
7514 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
7517 g_strfreev (fields);
7518 /* now we need to store the values for the caps of the stream */
7519 if (stream != NULL) {
7520 GST_DEBUG_OBJECT (src,
7521 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
7522 stream, seqbase, timebase);
7524 /* we have a stream, configure detected params */
7525 stream->seqbase = seqbase;
7526 stream->timebase = timebase;
7535 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
7540 interval = strtoul (rtcp, NULL, 10);
7541 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
7546 interval *= GST_MSECOND;
7548 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7549 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7551 /* already (optionally) retrieved this when configuring manager */
7552 if (stream->session) {
7553 GObject *rtpsession = stream->session;
7555 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
7557 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
7561 /* now it happens that (Xenon) server sending this may also provide bogus
7562 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
7563 * and just use RTP-Info to sync */
7565 GObjectClass *klass;
7567 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
7568 if (g_object_class_find_property (klass, "rtcp-sync")) {
7569 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
7570 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
7576 gst_rtspsrc_get_float (const gchar * dstr)
7578 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7580 /* canonicalise floating point string so we can handle float strings
7581 * in the form "24.930" or "24,930" irrespective of the current locale */
7582 g_strlcpy (s, dstr, sizeof (s));
7583 g_strdelimit (s, ",", '.');
7584 return g_ascii_strtod (s, NULL);
7588 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
7590 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7592 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
7593 g_strlcpy (val_str, "now", sizeof (val_str));
7595 if (segment->position == 0) {
7596 g_strlcpy (val_str, "0", sizeof (val_str));
7598 g_ascii_dtostr (val_str, sizeof (val_str),
7599 ((gdouble) segment->position) / GST_SECOND);
7602 return g_strdup_printf ("npt=%s-", val_str);
7606 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
7610 stream->timebase = -1;
7611 stream->seqbase = -1;
7613 len = stream->ptmap->len;
7614 for (i = 0; i < len; i++) {
7615 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
7618 if (item->caps == NULL)
7621 item->caps = gst_caps_make_writable (item->caps);
7622 s = gst_caps_get_structure (item->caps, 0);
7623 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
7624 if (item->pt == stream->default_pt && stream->udpsrc[0])
7625 g_object_set (stream->udpsrc[0], "caps", item->caps, NULL);
7627 stream->need_caps = TRUE;
7630 static GstRTSPResult
7631 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
7633 GstRTSPResult res = GST_RTSP_OK;
7635 if (src->state < GST_RTSP_STATE_READY) {
7636 res = GST_RTSP_ERROR;
7637 if (src->open_error) {
7638 GST_DEBUG_OBJECT (src, "the stream was in error");
7642 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
7644 if ((res = gst_rtspsrc_open (src, async)) < 0) {
7645 GST_DEBUG_OBJECT (src, "failed to open stream");
7654 static GstRTSPResult
7655 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async,
7656 const gchar * seek_style)
7658 GstRTSPMessage request = { 0 };
7659 GstRTSPMessage response = { 0 };
7660 GstRTSPResult res = GST_RTSP_OK;
7664 const gchar *control;
7666 GST_DEBUG_OBJECT (src, "PLAY...");
7669 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7672 if (!(src->methods & GST_RTSP_PLAY))
7675 if (src->state == GST_RTSP_STATE_PLAYING)
7678 if (!src->conninfo.connection || !src->conninfo.connected)
7681 /* send some dummy packets before we activate the receive in the
7683 gst_rtspsrc_send_dummy_packets (src);
7685 /* require new SR packets */
7687 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
7689 /* construct a control url */
7690 control = get_aggregate_control (src);
7692 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7693 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7694 const gchar *setup_url;
7695 GstRTSPConnInfo *conninfo;
7697 /* try aggregate control first but do non-aggregate control otherwise */
7699 setup_url = control;
7700 else if ((setup_url = stream->conninfo.location) == NULL)
7703 if (src->conninfo.connection) {
7704 conninfo = &src->conninfo;
7705 } else if (stream->conninfo.connection) {
7706 conninfo = &stream->conninfo;
7712 res = gst_rtspsrc_init_request (src, &request, GST_RTSP_PLAY, setup_url);
7714 goto create_request_failed;
7716 if (src->need_range && src->seekable >= 0.0) {
7717 hval = gen_range_header (src, segment);
7719 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
7721 /* store the newsegment event so it can be sent from the streaming thread. */
7722 src->need_segment = TRUE;
7725 if (segment->rate != 1.0) {
7726 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
7728 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
7730 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
7732 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
7736 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SEEK_STYLE,
7740 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
7743 gst_rtspsrc_send (src, conninfo, &request, &response, NULL, NULL))
7747 if (src->need_redirect) {
7748 GST_DEBUG_OBJECT (src,
7749 "redirect: tearing down and restarting with new url");
7750 /* teardown and restart with new url */
7751 gst_rtspsrc_close (src, TRUE, FALSE);
7752 /* reset protocols to force re-negotiation with redirected url */
7753 src->cur_protocols = src->protocols;
7754 gst_rtsp_message_unset (&request);
7755 gst_rtsp_message_unset (&response);
7759 /* seek may have silently failed as it is not supported */
7760 if (!(src->methods & GST_RTSP_PLAY)) {
7761 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
7763 if (src->version >= GST_RTSP_VERSION_2_0 && src->seekable >= 0.0) {
7764 GST_WARNING_OBJECT (src, "Server declared stream as seekable but"
7765 " playing with range failed... Ignoring information.");
7767 /* obviously it is supported as we made it here */
7768 src->methods |= GST_RTSP_PLAY;
7769 src->seekable = -1.0;
7770 /* but there is nothing to parse in the response,
7771 * so convey we have no idea and not to expect anything particular */
7772 clear_rtp_base (src, stream);
7776 /* need to do for all streams */
7777 for (run = src->streams; run; run = g_list_next (run))
7778 clear_rtp_base (src, (GstRTSPStream *) run->data);
7780 /* NOTE the above also disables npt based eos detection */
7781 /* and below forces position to 0,
7782 * which is visible feedback we lost the plot */
7783 segment->start = segment->position = src->last_pos;
7786 gst_rtsp_message_unset (&request);
7788 /* parse RTP npt field. This is the current position in the stream (Normal
7789 * Play Time) and should be put in the NEWSEGMENT position field. */
7790 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
7792 gst_rtspsrc_parse_range (src, hval, segment);
7794 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
7795 segment->rate = 1.0;
7797 /* parse Speed header. This is the intended playback rate of the stream
7798 * and should be put in the NEWSEGMENT rate field. */
7799 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
7800 0) == GST_RTSP_OK) {
7801 segment->rate = gst_rtspsrc_get_float (hval);
7802 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
7803 &hval, 0) == GST_RTSP_OK) {
7804 segment->rate = gst_rtspsrc_get_float (hval);
7807 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
7808 * for the RTP packets. If this is not present, we assume all starts from 0...
7809 * This is info for the RTP session manager that we pass to it in caps. */
7811 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
7812 &hval, hval_idx++) == GST_RTSP_OK)
7813 gst_rtspsrc_parse_rtpinfo (src, hval);
7815 /* some servers indicate RTCP parameters in PLAY response,
7816 * rather than properly in SDP */
7817 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
7818 &hval, 0) == GST_RTSP_OK)
7819 gst_rtspsrc_handle_rtcp_interval (src, hval);
7821 gst_rtsp_message_unset (&response);
7823 /* early exit when we did aggregate control */
7827 /* configure the caps of the streams after we parsed all headers. Only reset
7828 * the manager object when we set a new Range header (we did a seek) */
7829 gst_rtspsrc_configure_caps (src, segment, src->need_range);
7831 /* set to PLAYING after we have configured the caps, otherwise we
7832 * might end up calling request_key (with SRTP) while caps are still
7833 * being configured. */
7834 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
7836 /* set again when needed */
7837 src->need_range = FALSE;
7839 src->running = TRUE;
7840 src->base_time = -1;
7841 src->state = GST_RTSP_STATE_PLAYING;
7844 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
7845 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7846 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7847 stream->discont = TRUE;
7852 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
7859 GST_DEBUG_OBJECT (src, "failed to open stream");
7864 GST_DEBUG_OBJECT (src, "PLAY is not supported");
7869 GST_DEBUG_OBJECT (src, "we were already PLAYING");
7872 create_request_failed:
7874 gchar *str = gst_rtsp_strresult (res);
7876 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7877 ("Could not create request. (%s)", str));
7883 gchar *str = gst_rtsp_strresult (res);
7885 gst_rtsp_message_unset (&request);
7886 if (res != GST_RTSP_EINTR) {
7887 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7888 ("Could not send message. (%s)", str));
7890 GST_WARNING_OBJECT (src, "PLAY interrupted");
7897 static GstRTSPResult
7898 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
7900 GstRTSPResult res = GST_RTSP_OK;
7901 GstRTSPMessage request = { 0 };
7902 GstRTSPMessage response = { 0 };
7904 const gchar *control;
7906 GST_DEBUG_OBJECT (src, "PAUSE...");
7908 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7911 if (!(src->methods & GST_RTSP_PAUSE))
7914 if (src->state == GST_RTSP_STATE_READY)
7917 if (!src->conninfo.connection || !src->conninfo.connected)
7920 /* construct a control url */
7921 control = get_aggregate_control (src);
7923 /* loop over the streams. We might exit the loop early when we could do an
7924 * aggregate control */
7925 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7926 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7927 GstRTSPConnInfo *conninfo;
7928 const gchar *setup_url;
7930 /* try aggregate control first but do non-aggregate control otherwise */
7932 setup_url = control;
7933 else if ((setup_url = stream->conninfo.location) == NULL)
7936 if (src->conninfo.connection) {
7937 conninfo = &src->conninfo;
7938 } else if (stream->conninfo.connection) {
7939 conninfo = &stream->conninfo;
7945 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7946 ("Sending PAUSE request"));
7949 gst_rtspsrc_init_request (src, &request, GST_RTSP_PAUSE,
7951 goto create_request_failed;
7954 gst_rtspsrc_send (src, conninfo, &request, &response, NULL,
7958 gst_rtsp_message_unset (&request);
7959 gst_rtsp_message_unset (&response);
7961 /* exit early when we did agregate control */
7966 /* change element states now */
7967 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7970 src->state = GST_RTSP_STATE_READY;
7974 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7981 GST_DEBUG_OBJECT (src, "failed to open stream");
7986 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7991 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7994 create_request_failed:
7996 gchar *str = gst_rtsp_strresult (res);
7998 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7999 ("Could not create request. (%s)", str));
8005 gchar *str = gst_rtsp_strresult (res);
8007 gst_rtsp_message_unset (&request);
8008 if (res != GST_RTSP_EINTR) {
8009 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
8010 ("Could not send message. (%s)", str));
8012 GST_WARNING_OBJECT (src, "PAUSE interrupted");
8020 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
8022 GstRTSPSrc *rtspsrc;
8024 rtspsrc = GST_RTSPSRC (bin);
8026 switch (GST_MESSAGE_TYPE (message)) {
8027 case GST_MESSAGE_EOS:
8028 gst_message_unref (message);
8030 case GST_MESSAGE_ELEMENT:
8032 const GstStructure *s = gst_message_get_structure (message);
8034 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
8035 gboolean ignore_timeout;
8037 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
8039 GST_OBJECT_LOCK (rtspsrc);
8040 ignore_timeout = rtspsrc->ignore_timeout;
8041 rtspsrc->ignore_timeout = TRUE;
8042 GST_OBJECT_UNLOCK (rtspsrc);
8044 /* we only act on the first udp timeout message, others are irrelevant
8045 * and can be ignored. */
8046 if (!ignore_timeout)
8047 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
8049 gst_message_unref (message);
8052 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8055 case GST_MESSAGE_ERROR:
8058 GstRTSPStream *stream;
8061 udpsrc = GST_MESSAGE_SRC (message);
8063 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
8064 GST_ELEMENT_NAME (udpsrc));
8066 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
8070 /* we ignore the RTCP udpsrc */
8071 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
8074 /* if we get error messages from the udp sources, that's not a problem as
8075 * long as not all of them error out. We also don't really know what the
8076 * problem is, the message does not give enough detail... */
8077 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
8078 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
8079 if (ret != GST_FLOW_OK)
8083 gst_message_unref (message);
8087 /* fatal but not our message, forward */
8088 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8093 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8099 /* the thread where everything happens */
8101 gst_rtspsrc_thread (GstRTSPSrc * src)
8105 GST_OBJECT_LOCK (src);
8106 cmd = src->pending_cmd;
8107 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
8108 || cmd == CMD_LOOP || cmd == CMD_OPEN)
8109 src->pending_cmd = CMD_LOOP;
8111 src->pending_cmd = CMD_WAIT;
8112 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
8114 /* we got the message command, so ensure communication is possible again */
8115 gst_rtspsrc_connection_flush (src, FALSE);
8117 src->busy_cmd = cmd;
8118 GST_OBJECT_UNLOCK (src);
8122 gst_rtspsrc_open (src, TRUE);
8125 gst_rtspsrc_play (src, &src->segment, TRUE, NULL);
8128 gst_rtspsrc_pause (src, TRUE);
8131 gst_rtspsrc_close (src, TRUE, FALSE);
8134 gst_rtspsrc_loop (src);
8137 gst_rtspsrc_reconnect (src, FALSE);
8143 GST_OBJECT_LOCK (src);
8144 /* and go back to sleep */
8145 if (src->pending_cmd == CMD_WAIT) {
8147 gst_task_pause (src->task);
8150 src->busy_cmd = CMD_WAIT;
8151 GST_OBJECT_UNLOCK (src);
8155 gst_rtspsrc_start (GstRTSPSrc * src)
8157 GST_DEBUG_OBJECT (src, "starting");
8159 GST_OBJECT_LOCK (src);
8161 src->pending_cmd = CMD_WAIT;
8163 if (src->task == NULL) {
8164 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
8165 if (src->task == NULL)
8168 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
8170 GST_OBJECT_UNLOCK (src);
8177 GST_OBJECT_UNLOCK (src);
8178 GST_ERROR_OBJECT (src, "failed to create task");
8184 gst_rtspsrc_stop (GstRTSPSrc * src)
8188 GST_DEBUG_OBJECT (src, "stopping");
8190 /* also cancels pending task */
8191 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
8193 GST_OBJECT_LOCK (src);
8194 if ((task = src->task)) {
8196 GST_OBJECT_UNLOCK (src);
8198 gst_task_stop (task);
8200 /* make sure it is not running */
8201 GST_RTSP_STREAM_LOCK (src);
8202 GST_RTSP_STREAM_UNLOCK (src);
8204 /* now wait for the task to finish */
8205 gst_task_join (task);
8207 /* and free the task */
8208 gst_object_unref (GST_OBJECT (task));
8210 GST_OBJECT_LOCK (src);
8212 GST_OBJECT_UNLOCK (src);
8214 /* ensure synchronously all is closed and clean */
8215 gst_rtspsrc_close (src, FALSE, TRUE);
8220 static GstStateChangeReturn
8221 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
8223 GstRTSPSrc *rtspsrc;
8224 GstStateChangeReturn ret;
8226 rtspsrc = GST_RTSPSRC (element);
8228 switch (transition) {
8229 case GST_STATE_CHANGE_NULL_TO_READY:
8230 if (!gst_rtspsrc_start (rtspsrc))
8233 case GST_STATE_CHANGE_READY_TO_PAUSED:
8234 /* init some state */
8235 rtspsrc->cur_protocols = rtspsrc->protocols;
8236 /* first attempt, don't ignore timeouts */
8237 rtspsrc->ignore_timeout = FALSE;
8238 rtspsrc->open_error = FALSE;
8239 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
8241 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8242 set_manager_buffer_mode (rtspsrc);
8244 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8245 /* unblock the tcp tasks and make the loop waiting */
8246 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
8247 /* make sure it is waiting before we send PAUSE or PLAY below */
8248 GST_RTSP_STREAM_LOCK (rtspsrc);
8249 GST_RTSP_STREAM_UNLOCK (rtspsrc);
8252 case GST_STATE_CHANGE_PAUSED_TO_READY:
8258 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
8259 if (ret == GST_STATE_CHANGE_FAILURE)
8262 switch (transition) {
8263 case GST_STATE_CHANGE_NULL_TO_READY:
8264 ret = GST_STATE_CHANGE_SUCCESS;
8266 case GST_STATE_CHANGE_READY_TO_PAUSED:
8267 ret = GST_STATE_CHANGE_NO_PREROLL;
8269 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8270 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
8271 ret = GST_STATE_CHANGE_SUCCESS;
8273 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8274 /* send pause request and keep the idle task around */
8275 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
8276 ret = GST_STATE_CHANGE_NO_PREROLL;
8278 case GST_STATE_CHANGE_PAUSED_TO_READY:
8279 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_ALL);
8280 ret = GST_STATE_CHANGE_SUCCESS;
8282 case GST_STATE_CHANGE_READY_TO_NULL:
8283 gst_rtspsrc_stop (rtspsrc);
8284 ret = GST_STATE_CHANGE_SUCCESS;
8287 /* Otherwise it's success, we don't want to return spurious
8288 * NO_PREROLL or ASYNC from internal elements as we care for
8289 * state changes ourselves here
8291 * This is to catch PAUSED->PAUSED and PLAYING->PLAYING transitions.
8293 if (GST_STATE_TRANSITION_NEXT (transition) == GST_STATE_PAUSED)
8294 ret = GST_STATE_CHANGE_NO_PREROLL;
8296 ret = GST_STATE_CHANGE_SUCCESS;
8305 GST_DEBUG_OBJECT (rtspsrc, "start failed");
8306 return GST_STATE_CHANGE_FAILURE;
8311 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
8314 GstRTSPSrc *rtspsrc;
8316 rtspsrc = GST_RTSPSRC (element);
8318 if (GST_EVENT_IS_DOWNSTREAM (event)) {
8319 res = gst_rtspsrc_push_event (rtspsrc, event);
8321 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
8328 /*** GSTURIHANDLER INTERFACE *************************************************/
8331 gst_rtspsrc_uri_get_type (GType type)
8336 static const gchar *const *
8337 gst_rtspsrc_uri_get_protocols (GType type)
8339 static const gchar *protocols[] =
8340 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
8341 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
8348 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
8350 GstRTSPSrc *src = GST_RTSPSRC (handler);
8352 /* FIXME: make thread-safe */
8353 return g_strdup (src->conninfo.location);
8357 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
8363 GstRTSPUrl *newurl = NULL;
8364 GstSDPMessage *sdp = NULL;
8366 src = GST_RTSPSRC (handler);
8368 /* same URI, we're fine */
8369 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
8372 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
8373 sres = gst_sdp_message_new (&sdp);
8377 GST_DEBUG_OBJECT (src, "parsing SDP message");
8378 sres = gst_sdp_message_parse_uri (uri, sdp);
8383 GST_DEBUG_OBJECT (src, "parsing URI");
8384 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
8388 /* if worked, free previous and store new url object along with the original
8390 GST_DEBUG_OBJECT (src, "configuring URI");
8391 g_free (src->conninfo.location);
8392 src->conninfo.location = g_strdup (uri);
8393 gst_rtsp_url_free (src->conninfo.url);
8394 src->conninfo.url = newurl;
8395 g_free (src->conninfo.url_str);
8397 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
8399 src->conninfo.url_str = NULL;
8402 gst_sdp_message_free (src->sdp);
8404 src->from_sdp = sdp != NULL;
8406 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
8407 GST_DEBUG_OBJECT (src, "request uri is: %s",
8408 GST_STR_NULL (src->conninfo.url_str));
8415 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
8420 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
8421 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8422 "Could not create SDP");
8427 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
8428 GST_STR_NULL (uri));
8429 gst_sdp_message_free (sdp);
8430 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8436 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
8437 GST_STR_NULL (uri), res);
8438 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8439 "Invalid RTSP URI");
8445 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
8447 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
8449 iface->get_type = gst_rtspsrc_uri_get_type;
8450 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
8451 iface->get_uri = gst_rtspsrc_uri_get_uri;
8452 iface->set_uri = gst_rtspsrc_uri_set_uri;
8455 typedef struct _RTSPKeyValue
8457 GstRTSPHeaderField field;
8459 gchar *custom_key; /* custom header string (field is INVALID then) */
8463 key_value_foreach (GArray * array, GFunc func, gpointer user_data)
8467 g_return_if_fail (array != NULL);
8469 for (i = 0; i < array->len; i++) {
8470 (*func) (&g_array_index (array, RTSPKeyValue, i), user_data);
8475 dump_key_value (gpointer data, gpointer user_data G_GNUC_UNUSED)
8477 RTSPKeyValue *key_value = (RTSPKeyValue *) data;
8478 GstRTSPSrc *src = GST_RTSPSRC (user_data);
8479 const gchar *key_string;
8481 if (key_value->custom_key != NULL)
8482 key_string = key_value->custom_key;
8484 key_string = gst_rtsp_header_as_text (key_value->field);
8486 GST_LOG_OBJECT (src, " key: '%s', value: '%s'", key_string,
8491 gst_rtspsrc_print_rtsp_message (GstRTSPSrc * src, const GstRTSPMessage * msg)
8495 GString *body_string = NULL;
8497 g_return_if_fail (src != NULL);
8498 g_return_if_fail (msg != NULL);
8500 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) < GST_LEVEL_LOG)
8503 GST_LOG_OBJECT (src, "--------------------------------------------");
8504 switch (msg->type) {
8505 case GST_RTSP_MESSAGE_REQUEST:
8506 GST_LOG_OBJECT (src, "RTSP request message %p", msg);
8507 GST_LOG_OBJECT (src, " request line:");
8508 GST_LOG_OBJECT (src, " method: '%s'",
8509 gst_rtsp_method_as_text (msg->type_data.request.method));
8510 GST_LOG_OBJECT (src, " uri: '%s'", msg->type_data.request.uri);
8511 GST_LOG_OBJECT (src, " version: '%s'",
8512 gst_rtsp_version_as_text (msg->type_data.request.version));
8513 GST_LOG_OBJECT (src, " headers:");
8514 key_value_foreach (msg->hdr_fields, dump_key_value, src);
8515 GST_LOG_OBJECT (src, " body:");
8516 gst_rtsp_message_get_body (msg, &data, &size);
8518 body_string = g_string_new_len ((const gchar *) data, size);
8519 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
8520 g_string_free (body_string, TRUE);
8524 case GST_RTSP_MESSAGE_RESPONSE:
8525 GST_LOG_OBJECT (src, "RTSP response message %p", msg);
8526 GST_LOG_OBJECT (src, " status line:");
8527 GST_LOG_OBJECT (src, " code: '%d'", msg->type_data.response.code);
8528 GST_LOG_OBJECT (src, " reason: '%s'", msg->type_data.response.reason);
8529 GST_LOG_OBJECT (src, " version: '%s",
8530 gst_rtsp_version_as_text (msg->type_data.response.version));
8531 GST_LOG_OBJECT (src, " headers:");
8532 key_value_foreach (msg->hdr_fields, dump_key_value, src);
8533 gst_rtsp_message_get_body (msg, &data, &size);
8534 GST_LOG_OBJECT (src, " body: length %d", size);
8536 body_string = g_string_new_len ((const gchar *) data, size);
8537 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
8538 g_string_free (body_string, TRUE);
8542 case GST_RTSP_MESSAGE_HTTP_REQUEST:
8543 GST_LOG_OBJECT (src, "HTTP request message %p", msg);
8544 GST_LOG_OBJECT (src, " request line:");
8545 GST_LOG_OBJECT (src, " method: '%s'",
8546 gst_rtsp_method_as_text (msg->type_data.request.method));
8547 GST_LOG_OBJECT (src, " uri: '%s'", msg->type_data.request.uri);
8548 GST_LOG_OBJECT (src, " version: '%s'",
8549 gst_rtsp_version_as_text (msg->type_data.request.version));
8550 GST_LOG_OBJECT (src, " headers:");
8551 key_value_foreach (msg->hdr_fields, dump_key_value, src);
8552 GST_LOG_OBJECT (src, " body:");
8553 gst_rtsp_message_get_body (msg, &data, &size);
8555 body_string = g_string_new_len ((const gchar *) data, size);
8556 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
8557 g_string_free (body_string, TRUE);
8561 case GST_RTSP_MESSAGE_HTTP_RESPONSE:
8562 GST_LOG_OBJECT (src, "HTTP response message %p", msg);
8563 GST_LOG_OBJECT (src, " status line:");
8564 GST_LOG_OBJECT (src, " code: '%d'", msg->type_data.response.code);
8565 GST_LOG_OBJECT (src, " reason: '%s'", msg->type_data.response.reason);
8566 GST_LOG_OBJECT (src, " version: '%s'",
8567 gst_rtsp_version_as_text (msg->type_data.response.version));
8568 GST_LOG_OBJECT (src, " headers:");
8569 key_value_foreach (msg->hdr_fields, dump_key_value, src);
8570 gst_rtsp_message_get_body (msg, &data, &size);
8571 GST_LOG_OBJECT (src, " body: length %d", size);
8573 body_string = g_string_new_len ((const gchar *) data, size);
8574 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
8575 g_string_free (body_string, TRUE);
8579 case GST_RTSP_MESSAGE_DATA:
8580 GST_LOG_OBJECT (src, "RTSP data message %p", msg);
8581 GST_LOG_OBJECT (src, " channel: '%d'", msg->type_data.data.channel);
8582 GST_LOG_OBJECT (src, " size: '%d'", msg->body_size);
8583 gst_rtsp_message_get_body (msg, &data, &size);
8585 body_string = g_string_new_len ((const gchar *) data, size);
8586 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
8587 g_string_free (body_string, TRUE);
8592 GST_LOG_OBJECT (src, "unsupported message type %d", msg->type);
8595 GST_LOG_OBJECT (src, "--------------------------------------------");
8599 gst_rtspsrc_print_sdp_media (GstRTSPSrc * src, GstSDPMedia * media)
8601 GST_LOG_OBJECT (src, " media: '%s'", GST_STR_NULL (media->media));
8602 GST_LOG_OBJECT (src, " port: '%u'", media->port);
8603 GST_LOG_OBJECT (src, " num_ports: '%u'", media->num_ports);
8604 GST_LOG_OBJECT (src, " proto: '%s'", GST_STR_NULL (media->proto));
8605 if (media->fmts && media->fmts->len > 0) {
8608 GST_LOG_OBJECT (src, " formats:");
8609 for (i = 0; i < media->fmts->len; i++) {
8610 GST_LOG_OBJECT (src, " format '%s'", g_array_index (media->fmts,
8614 GST_LOG_OBJECT (src, " information: '%s'",
8615 GST_STR_NULL (media->information));
8616 if (media->connections && media->connections->len > 0) {
8619 GST_LOG_OBJECT (src, " connections:");
8620 for (i = 0; i < media->connections->len; i++) {
8621 GstSDPConnection *conn =
8622 &g_array_index (media->connections, GstSDPConnection, i);
8624 GST_LOG_OBJECT (src, " nettype: '%s'",
8625 GST_STR_NULL (conn->nettype));
8626 GST_LOG_OBJECT (src, " addrtype: '%s'",
8627 GST_STR_NULL (conn->addrtype));
8628 GST_LOG_OBJECT (src, " address: '%s'",
8629 GST_STR_NULL (conn->address));
8630 GST_LOG_OBJECT (src, " ttl: '%u'", conn->ttl);
8631 GST_LOG_OBJECT (src, " addr_number: '%u'", conn->addr_number);
8634 if (media->bandwidths && media->bandwidths->len > 0) {
8637 GST_LOG_OBJECT (src, " bandwidths:");
8638 for (i = 0; i < media->bandwidths->len; i++) {
8639 GstSDPBandwidth *bw =
8640 &g_array_index (media->bandwidths, GstSDPBandwidth, i);
8642 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (bw->bwtype));
8643 GST_LOG_OBJECT (src, " bandwidth: '%u'", bw->bandwidth);
8646 GST_LOG_OBJECT (src, " key:");
8647 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (media->key.type));
8648 GST_LOG_OBJECT (src, " data: '%s'", GST_STR_NULL (media->key.data));
8649 if (media->attributes && media->attributes->len > 0) {
8652 GST_LOG_OBJECT (src, " attributes:");
8653 for (i = 0; i < media->attributes->len; i++) {
8654 GstSDPAttribute *attr =
8655 &g_array_index (media->attributes, GstSDPAttribute, i);
8657 GST_LOG_OBJECT (src, " attribute '%s' : '%s'", attr->key, attr->value);
8663 gst_rtspsrc_print_sdp_message (GstRTSPSrc * src, const GstSDPMessage * msg)
8665 g_return_if_fail (src != NULL);
8666 g_return_if_fail (msg != NULL);
8668 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) < GST_LEVEL_LOG)
8671 GST_LOG_OBJECT (src, "--------------------------------------------");
8672 GST_LOG_OBJECT (src, "sdp packet %p:", msg);
8673 GST_LOG_OBJECT (src, " version: '%s'", GST_STR_NULL (msg->version));
8674 GST_LOG_OBJECT (src, " origin:");
8675 GST_LOG_OBJECT (src, " username: '%s'",
8676 GST_STR_NULL (msg->origin.username));
8677 GST_LOG_OBJECT (src, " sess_id: '%s'",
8678 GST_STR_NULL (msg->origin.sess_id));
8679 GST_LOG_OBJECT (src, " sess_version: '%s'",
8680 GST_STR_NULL (msg->origin.sess_version));
8681 GST_LOG_OBJECT (src, " nettype: '%s'",
8682 GST_STR_NULL (msg->origin.nettype));
8683 GST_LOG_OBJECT (src, " addrtype: '%s'",
8684 GST_STR_NULL (msg->origin.addrtype));
8685 GST_LOG_OBJECT (src, " addr: '%s'", GST_STR_NULL (msg->origin.addr));
8686 GST_LOG_OBJECT (src, " session_name: '%s'",
8687 GST_STR_NULL (msg->session_name));
8688 GST_LOG_OBJECT (src, " information: '%s'", GST_STR_NULL (msg->information));
8689 GST_LOG_OBJECT (src, " uri: '%s'", GST_STR_NULL (msg->uri));
8691 if (msg->emails && msg->emails->len > 0) {
8694 GST_LOG_OBJECT (src, " emails:");
8695 for (i = 0; i < msg->emails->len; i++) {
8696 GST_LOG_OBJECT (src, " email '%s'", g_array_index (msg->emails, gchar *,
8700 if (msg->phones && msg->phones->len > 0) {
8703 GST_LOG_OBJECT (src, " phones:");
8704 for (i = 0; i < msg->phones->len; i++) {
8705 GST_LOG_OBJECT (src, " phone '%s'", g_array_index (msg->phones, gchar *,
8709 GST_LOG_OBJECT (src, " connection:");
8710 GST_LOG_OBJECT (src, " nettype: '%s'",
8711 GST_STR_NULL (msg->connection.nettype));
8712 GST_LOG_OBJECT (src, " addrtype: '%s'",
8713 GST_STR_NULL (msg->connection.addrtype));
8714 GST_LOG_OBJECT (src, " address: '%s'",
8715 GST_STR_NULL (msg->connection.address));
8716 GST_LOG_OBJECT (src, " ttl: '%u'", msg->connection.ttl);
8717 GST_LOG_OBJECT (src, " addr_number: '%u'", msg->connection.addr_number);
8718 if (msg->bandwidths && msg->bandwidths->len > 0) {
8721 GST_LOG_OBJECT (src, " bandwidths:");
8722 for (i = 0; i < msg->bandwidths->len; i++) {
8723 GstSDPBandwidth *bw =
8724 &g_array_index (msg->bandwidths, GstSDPBandwidth, i);
8726 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (bw->bwtype));
8727 GST_LOG_OBJECT (src, " bandwidth: '%u'", bw->bandwidth);
8730 GST_LOG_OBJECT (src, " key:");
8731 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (msg->key.type));
8732 GST_LOG_OBJECT (src, " data: '%s'", GST_STR_NULL (msg->key.data));
8733 if (msg->attributes && msg->attributes->len > 0) {
8736 GST_LOG_OBJECT (src, " attributes:");
8737 for (i = 0; i < msg->attributes->len; i++) {
8738 GstSDPAttribute *attr =
8739 &g_array_index (msg->attributes, GstSDPAttribute, i);
8741 GST_LOG_OBJECT (src, " attribute '%s' : '%s'", attr->key, attr->value);
8744 if (msg->medias && msg->medias->len > 0) {
8747 GST_LOG_OBJECT (src, " medias:");
8748 for (i = 0; i < msg->medias->len; i++) {
8749 GST_LOG_OBJECT (src, " media %u:", i);
8750 gst_rtspsrc_print_sdp_media (src, &g_array_index (msg->medias,
8754 GST_LOG_OBJECT (src, "--------------------------------------------");