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,
129 SIGNAL_PUSH_BACKCHANNEL_BUFFER,
133 enum _GstRtspSrcRtcpSyncMode
140 enum _GstRtspSrcBufferMode
149 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
151 gst_rtsp_src_buffer_mode_get_type (void)
153 static GType buffer_mode_type = 0;
154 static const GEnumValue buffer_modes[] = {
155 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
156 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
157 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
158 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
159 {BUFFER_MODE_SYNCED, "Synchronized sender and receiver clocks", "synced"},
163 if (!buffer_mode_type) {
165 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
167 return buffer_mode_type;
170 enum _GstRtspSrcNtpTimeSource
173 NTP_TIME_SOURCE_UNIX,
174 NTP_TIME_SOURCE_RUNNING_TIME,
175 NTP_TIME_SOURCE_CLOCK_TIME
178 #define DEBUG_RTSP(__self,msg) gst_rtspsrc_print_rtsp_message (__self, msg)
179 #define DEBUG_SDP(__self,msg) gst_rtspsrc_print_sdp_message (__self, msg)
181 #define GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE (gst_rtsp_src_ntp_time_source_get_type())
183 gst_rtsp_src_ntp_time_source_get_type (void)
185 static GType ntp_time_source_type = 0;
186 static const GEnumValue ntp_time_source_values[] = {
187 {NTP_TIME_SOURCE_NTP, "NTP time based on realtime clock", "ntp"},
188 {NTP_TIME_SOURCE_UNIX, "UNIX time based on realtime clock", "unix"},
189 {NTP_TIME_SOURCE_RUNNING_TIME,
190 "Running time based on pipeline clock",
192 {NTP_TIME_SOURCE_CLOCK_TIME, "Pipeline clock time", "clock-time"},
196 if (!ntp_time_source_type) {
197 ntp_time_source_type =
198 g_enum_register_static ("GstRTSPSrcNtpTimeSource",
199 ntp_time_source_values);
201 return ntp_time_source_type;
204 enum _GstRtspBackchannel
210 #define GST_TYPE_RTSP_BACKCHANNEL (gst_rtsp_backchannel_get_type())
212 gst_rtsp_backchannel_get_type (void)
214 static GType backchannel_type = 0;
215 static const GEnumValue backchannel_values[] = {
216 {BACKCHANNEL_NONE, "No backchannel", "none"},
217 {BACKCHANNEL_ONVIF, "ONVIF audio backchannel", "onvif"},
221 if (G_UNLIKELY (backchannel_type == 0)) {
223 g_enum_register_static ("GstRTSPBackchannel", backchannel_values);
225 return backchannel_type;
228 #define BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL "www.onvif.org/ver20/backchannel"
230 #define DEFAULT_LOCATION NULL
231 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
232 #define DEFAULT_DEBUG FALSE
233 #define DEFAULT_RETRY 20
234 #define DEFAULT_TIMEOUT 5000000
235 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
236 #define DEFAULT_TCP_TIMEOUT 20000000
237 #define DEFAULT_LATENCY_MS 2000
238 #define DEFAULT_DROP_ON_LATENCY FALSE
239 #define DEFAULT_CONNECTION_SPEED 0
240 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
241 #define DEFAULT_DO_RTCP TRUE
242 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
243 #define DEFAULT_PROXY NULL
244 #define DEFAULT_RTP_BLOCKSIZE 0
245 #define DEFAULT_USER_ID NULL
246 #define DEFAULT_USER_PW NULL
247 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
248 #define DEFAULT_PORT_RANGE NULL
249 #define DEFAULT_SHORT_HEADER FALSE
250 #define DEFAULT_PROBATION 2
251 #define DEFAULT_UDP_RECONNECT TRUE
252 #define DEFAULT_MULTICAST_IFACE NULL
253 #define DEFAULT_NTP_SYNC FALSE
254 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
255 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
256 #define DEFAULT_TLS_DATABASE NULL
257 #define DEFAULT_TLS_INTERACTION NULL
258 #define DEFAULT_DO_RETRANSMISSION TRUE
259 #define DEFAULT_NTP_TIME_SOURCE NTP_TIME_SOURCE_NTP
260 #define DEFAULT_USER_AGENT "GStreamer/" PACKAGE_VERSION
261 #define DEFAULT_MAX_RTCP_RTP_TIME_DIFF 1000
262 #define DEFAULT_RFC7273_SYNC FALSE
263 #define DEFAULT_MAX_TS_OFFSET_ADJUSTMENT G_GUINT64_CONSTANT(0)
264 #define DEFAULT_MAX_TS_OFFSET G_GINT64_CONSTANT(3000000000)
265 #define DEFAULT_VERSION GST_RTSP_VERSION_1_0
266 #define DEFAULT_BACKCHANNEL GST_RTSP_BACKCHANNEL_NONE
278 PROP_DROP_ON_LATENCY,
279 PROP_CONNECTION_SPEED,
282 PROP_DO_RTSP_KEEP_ALIVE,
291 PROP_UDP_BUFFER_SIZE,
295 PROP_MULTICAST_IFACE,
297 PROP_USE_PIPELINE_CLOCK,
299 PROP_TLS_VALIDATION_FLAGS,
301 PROP_TLS_INTERACTION,
302 PROP_DO_RETRANSMISSION,
303 PROP_NTP_TIME_SOURCE,
305 PROP_MAX_RTCP_RTP_TIME_DIFF,
307 PROP_MAX_TS_OFFSET_ADJUSTMENT,
309 PROP_DEFAULT_VERSION,
313 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
315 gst_rtsp_nat_method_get_type (void)
317 static GType rtsp_nat_method_type = 0;
318 static const GEnumValue rtsp_nat_method[] = {
319 {GST_RTSP_NAT_NONE, "None", "none"},
320 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
324 if (!rtsp_nat_method_type) {
325 rtsp_nat_method_type =
326 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
328 return rtsp_nat_method_type;
331 #define RTSP_SRC_RESPONSE_ERROR(src, response_msg, err_cat, err_code, error_message) \
333 GST_ELEMENT_ERROR_WITH_DETAILS((src), err_cat, err_code, ("%s", error_message), \
334 ("%s (%d)", (response_msg)->type_data.response.reason, (response_msg)->type_data.response.code), \
335 ("rtsp-status-code", G_TYPE_UINT, (response_msg)->type_data.response.code, \
336 "rtsp-status-reason", G_TYPE_STRING, GST_STR_NULL((response_msg)->type_data.response.reason), NULL)); \
339 static void gst_rtspsrc_finalize (GObject * object);
341 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
342 const GValue * value, GParamSpec * pspec);
343 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
344 GValue * value, GParamSpec * pspec);
346 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
348 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
349 gpointer iface_data);
351 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
352 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
354 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
355 GstStateChange transition);
356 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
357 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
359 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
360 GstRTSPMessage * response);
362 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
364 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
365 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
367 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
368 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
369 gboolean async, const gchar * seek_style);
370 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
371 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
372 gboolean only_close);
374 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
375 const gchar * uri, GError ** error);
376 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
378 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
379 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
380 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
381 GstRTSPStream * stream, GstEvent * event);
382 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
383 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
384 static GstRTSPResult gst_rtsp_conninfo_close (GstRTSPSrc * src,
385 GstRTSPConnInfo * info, gboolean free);
387 gst_rtspsrc_print_rtsp_message (GstRTSPSrc * src, const GstRTSPMessage * msg);
389 gst_rtspsrc_print_sdp_message (GstRTSPSrc * src, const GstSDPMessage * msg);
391 static GstFlowReturn gst_rtspsrc_push_backchannel_buffer (GstRTSPSrc * src,
392 guint id, GstSample * sample);
400 /* commands we send to out loop to notify it of events */
401 #define CMD_OPEN (1 << 0)
402 #define CMD_PLAY (1 << 1)
403 #define CMD_PAUSE (1 << 2)
404 #define CMD_CLOSE (1 << 3)
405 #define CMD_WAIT (1 << 4)
406 #define CMD_RECONNECT (1 << 5)
407 #define CMD_LOOP (1 << 6)
409 /* mask for all commands */
410 #define CMD_ALL ((CMD_LOOP << 1) - 1)
412 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
414 gchar *__txt = _gst_element_error_printf text; \
415 gst_element_post_message (GST_ELEMENT_CAST (el), \
416 gst_message_new_progress (GST_OBJECT_CAST (el), \
417 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
421 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
423 #define gst_rtspsrc_parent_class parent_class
424 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
425 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
427 #ifndef GST_DISABLE_GST_DEBUG
428 static inline const char *
429 cmd_to_string (guint cmd)
453 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
455 GST_DEBUG_OBJECT (src, "default handler");
460 select_stream_accum (GSignalInvocationHint * ihint,
461 GValue * return_accu, const GValue * handler_return, gpointer data)
465 myboolean = g_value_get_boolean (handler_return);
466 GST_DEBUG ("accum %d", myboolean);
467 g_value_set_boolean (return_accu, myboolean);
469 /* stop emission if FALSE */
474 default_before_send (GstRTSPSrc * src, GstRTSPMessage * msg)
476 GST_DEBUG_OBJECT (src, "default handler");
481 before_send_accum (GSignalInvocationHint * ihint,
482 GValue * return_accu, const GValue * handler_return, gpointer data)
486 myboolean = g_value_get_boolean (handler_return);
487 g_value_set_boolean (return_accu, myboolean);
489 /* prevent send if FALSE */
494 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
496 GObjectClass *gobject_class;
497 GstElementClass *gstelement_class;
498 GstBinClass *gstbin_class;
500 gobject_class = (GObjectClass *) klass;
501 gstelement_class = (GstElementClass *) klass;
502 gstbin_class = (GstBinClass *) klass;
504 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
506 gobject_class->set_property = gst_rtspsrc_set_property;
507 gobject_class->get_property = gst_rtspsrc_get_property;
509 gobject_class->finalize = gst_rtspsrc_finalize;
511 g_object_class_install_property (gobject_class, PROP_LOCATION,
512 g_param_spec_string ("location", "RTSP Location",
513 "Location of the RTSP url to read",
514 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
516 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
517 g_param_spec_flags ("protocols", "Protocols",
518 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
519 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
521 g_object_class_install_property (gobject_class, PROP_DEBUG,
522 g_param_spec_boolean ("debug", "Debug",
523 "Dump request and response messages to stdout"
524 "(DEPRECATED: Printed all RTSP message to gstreamer log as 'log' level)",
526 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
528 g_object_class_install_property (gobject_class, PROP_RETRY,
529 g_param_spec_uint ("retry", "Retry",
530 "Max number of retries when allocating RTP ports.",
531 0, G_MAXUINT16, DEFAULT_RETRY,
532 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
534 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
535 g_param_spec_uint64 ("timeout", "Timeout",
536 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
537 0, G_MAXUINT64, DEFAULT_TIMEOUT,
538 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
540 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
541 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
542 "Fail after timeout microseconds on TCP connections (0 = disabled)",
543 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
544 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
546 g_object_class_install_property (gobject_class, PROP_LATENCY,
547 g_param_spec_uint ("latency", "Buffer latency in ms",
548 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
549 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
551 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
552 g_param_spec_boolean ("drop-on-latency",
553 "Drop buffers when maximum latency is reached",
554 "Tells the jitterbuffer to never exceed the given latency in size",
555 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
557 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
558 g_param_spec_uint64 ("connection-speed", "Connection Speed",
559 "Network connection speed in kbps (0 = unknown)",
560 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
561 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
563 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
564 g_param_spec_enum ("nat-method", "NAT Method",
565 "Method to use for traversing firewalls and NAT",
566 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
567 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
570 * GstRTSPSrc:do-rtcp:
572 * Enable RTCP support. Some old server don't like RTCP and then this property
573 * needs to be set to FALSE.
575 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
576 g_param_spec_boolean ("do-rtcp", "Do RTCP",
577 "Send RTCP packets, disable for old incompatible server.",
578 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
581 * GstRTSPSrc:do-rtsp-keep-alive:
583 * Enable RTSP keep alive support. Some old server don't like RTSP
584 * keep alive and then this property needs to be set to FALSE.
586 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
587 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
588 "Send RTSP keep alive packets, disable for old incompatible server.",
589 DEFAULT_DO_RTSP_KEEP_ALIVE,
590 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
595 * Set the proxy parameters. This has to be a string of the format
596 * [http://][user:passwd@]host[:port].
598 g_object_class_install_property (gobject_class, PROP_PROXY,
599 g_param_spec_string ("proxy", "Proxy",
600 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
601 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
603 * GstRTSPSrc:proxy-id:
605 * Sets the proxy URI user id for authentication. If the URI set via the
606 * "proxy" property contains a user-id already, that will take precedence.
610 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
611 g_param_spec_string ("proxy-id", "proxy-id",
612 "HTTP proxy URI user id for authentication", "",
613 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
615 * GstRTSPSrc:proxy-pw:
617 * Sets the proxy URI password for authentication. If the URI set via the
618 * "proxy" property contains a password already, that will take precedence.
622 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
623 g_param_spec_string ("proxy-pw", "proxy-pw",
624 "HTTP proxy URI user password for authentication", "",
625 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
628 * GstRTSPSrc:rtp-blocksize:
630 * RTP package size to suggest to server.
632 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
633 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
634 "RTP package size to suggest to server (0 = disabled)",
635 0, 65536, DEFAULT_RTP_BLOCKSIZE,
636 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
638 g_object_class_install_property (gobject_class,
640 g_param_spec_string ("user-id", "user-id",
641 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
642 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
643 g_object_class_install_property (gobject_class, PROP_USER_PW,
644 g_param_spec_string ("user-pw", "user-pw",
645 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
646 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
649 * GstRTSPSrc:buffer-mode:
651 * Control the buffering and timestamping mode used by the jitterbuffer.
653 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
654 g_param_spec_enum ("buffer-mode", "Buffer Mode",
655 "Control the buffering algorithm in use",
656 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
657 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
660 * GstRTSPSrc:port-range:
662 * Configure the client port numbers that can be used to recieve RTP and
665 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
666 g_param_spec_string ("port-range", "Port range",
667 "Client port range that can be used to receive RTP and RTCP data, "
668 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
669 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
672 * GstRTSPSrc:udp-buffer-size:
674 * Size of the kernel UDP receive buffer in bytes.
676 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
677 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
678 "Size of the kernel UDP receive buffer in bytes, 0=default",
679 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
680 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
683 * GstRTSPSrc:short-header:
685 * Only send the basic RTSP headers for broken encoders.
687 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
688 g_param_spec_boolean ("short-header", "Short Header",
689 "Only send the basic RTSP headers for broken encoders",
690 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
692 g_object_class_install_property (gobject_class, PROP_PROBATION,
693 g_param_spec_uint ("probation", "Number of probations",
694 "Consecutive packet sequence numbers to accept the source",
695 0, G_MAXUINT, DEFAULT_PROBATION,
696 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
698 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
699 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
700 "Reconnect to the server if RTSP connection is closed when doing UDP",
701 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
703 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
704 g_param_spec_string ("multicast-iface", "Multicast Interface",
705 "The network interface on which to join the multicast group",
706 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
708 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
709 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
710 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
711 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
713 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
714 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
715 "Use the pipeline running-time to set the NTP time in the RTCP SR messages"
716 "(DEPRECATED: Use ntp-time-source property)",
717 DEFAULT_USE_PIPELINE_CLOCK,
718 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
720 g_object_class_install_property (gobject_class, PROP_SDES,
721 g_param_spec_boxed ("sdes", "SDES",
722 "The SDES items of this session",
723 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
726 * GstRTSPSrc::tls-validation-flags:
728 * TLS certificate validation flags used to validate server
733 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
734 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
735 "TLS certificate validation flags used to validate the server certificate",
736 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
737 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
740 * GstRTSPSrc::tls-database:
742 * TLS database with anchor certificate authorities used to validate
743 * the server certificate.
747 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
748 g_param_spec_object ("tls-database", "TLS database",
749 "TLS database with anchor certificate authorities used to validate the server certificate",
750 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
753 * GstRTSPSrc::tls-interaction:
755 * A #GTlsInteraction object to be used when the connection or certificate
756 * database need to interact with the user. This will be used to prompt the
757 * user for passwords where necessary.
761 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
762 g_param_spec_object ("tls-interaction", "TLS interaction",
763 "A GTlsInteraction object to promt the user for password or certificate",
764 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
767 * GstRTSPSrc::do-retransmission:
769 * Attempt to ask the server to retransmit lost packets according to RFC4588.
771 * Note: currently only works with SSRC-multiplexed retransmission streams
775 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
776 g_param_spec_boolean ("do-retransmission", "Retransmission",
777 "Ask the server to retransmit lost packets",
778 DEFAULT_DO_RETRANSMISSION,
779 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
782 * GstRTSPSrc::ntp-time-source:
784 * allows to select the time source that should be used
785 * for the NTP time in RTCP packets
789 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
790 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
791 "NTP time source for RTCP packets",
792 GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE, DEFAULT_NTP_TIME_SOURCE,
793 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
796 * GstRTSPSrc::user-agent:
798 * The string to set in the User-Agent header.
802 g_object_class_install_property (gobject_class, PROP_USER_AGENT,
803 g_param_spec_string ("user-agent", "User Agent",
804 "The User-Agent string to send to the server",
805 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
807 g_object_class_install_property (gobject_class, PROP_MAX_RTCP_RTP_TIME_DIFF,
808 g_param_spec_int ("max-rtcp-rtp-time-diff", "Max RTCP RTP Time Diff",
809 "Maximum amount of time in ms that the RTP time in RTCP SRs "
810 "is allowed to be ahead (-1 disabled)", -1, G_MAXINT,
811 DEFAULT_MAX_RTCP_RTP_TIME_DIFF,
812 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
814 g_object_class_install_property (gobject_class, PROP_RFC7273_SYNC,
815 g_param_spec_boolean ("rfc7273-sync", "Sync on RFC7273 clock",
816 "Synchronize received streams to the RFC7273 clock "
817 "(requires clock and offset to be provided)", DEFAULT_RFC7273_SYNC,
818 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
821 * GstRTSPSrc:default-rtsp-version:
823 * The preferred RTSP version to use while negotiating the version with the server.
827 g_object_class_install_property (gobject_class, PROP_DEFAULT_VERSION,
828 g_param_spec_enum ("default-rtsp-version",
829 "The RTSP version to try first",
830 "The RTSP version that should be tried first when negotiating version.",
831 GST_TYPE_RTSP_VERSION, DEFAULT_VERSION,
832 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
835 * GstRTSPSrc:max-ts-offset-adjustment:
837 * Syncing time stamps to NTP time adds a time offset. This parameter
838 * specifies the maximum number of nanoseconds per frame that this time offset
839 * may be adjusted with. This is used to avoid sudden large changes to time
842 g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET_ADJUSTMENT,
843 g_param_spec_uint64 ("max-ts-offset-adjustment",
844 "Max Timestamp Offset Adjustment",
845 "The maximum number of nanoseconds per frame that time stamp offsets "
846 "may be adjusted (0 = no limit).", 0, G_MAXUINT64,
847 DEFAULT_MAX_TS_OFFSET_ADJUSTMENT, G_PARAM_READWRITE |
848 G_PARAM_STATIC_STRINGS));
851 * GstRTSPSrc:max-ts-offset:
853 * Used to set an upper limit of how large a time offset may be. This
854 * is used to protect against unrealistic values as a result of either
855 * client,server or clock issues.
857 g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET,
858 g_param_spec_int64 ("max-ts-offset", "Max TS Offset",
859 "The maximum absolute value of the time offset in (nanoseconds). "
860 "Note, if the ntp-sync parameter is set the default value is "
861 "changed to 0 (no limit)", 0, G_MAXINT64, DEFAULT_MAX_TS_OFFSET,
862 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
865 * GstRTSPSrc:backchannel
867 * Select a type of backchannel to setup with the RTSP server.
868 * Default value is "none". Allowed values are "none" and "onvif".
872 g_object_class_install_property (gobject_class, PROP_BACKCHANNEL,
873 g_param_spec_enum ("backchannel", "Backchannel type",
874 "The type of backchannel to setup. Default is 'none'.",
875 GST_TYPE_RTSP_BACKCHANNEL, BACKCHANNEL_NONE,
876 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
879 * GstRTSPSrc::handle-request:
880 * @rtspsrc: a #GstRTSPSrc
881 * @request: a #GstRTSPMessage
882 * @response: a #GstRTSPMessage
884 * Handle a server request in @request and prepare @response.
886 * This signal is called from the streaming thread, you should therefore not
887 * do any state changes on @rtspsrc because this might deadlock. If you want
888 * to modify the state as a result of this signal, post a
889 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
894 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
895 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
896 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
897 G_TYPE_POINTER, G_TYPE_POINTER);
900 * GstRTSPSrc::on-sdp:
901 * @rtspsrc: a #GstRTSPSrc
902 * @sdp: a #GstSDPMessage
904 * Emitted when the client has retrieved the SDP and before it configures the
905 * streams in the SDP. @sdp can be inspected and modified.
907 * This signal is called from the streaming thread, you should therefore not
908 * do any state changes on @rtspsrc because this might deadlock. If you want
909 * to modify the state as a result of this signal, post a
910 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
915 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
916 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
917 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
918 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
921 * GstRTSPSrc::select-stream:
922 * @rtspsrc: a #GstRTSPSrc
923 * @num: the stream number
924 * @caps: the stream caps
926 * Emitted before the client decides to configure the stream @num with
929 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
934 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
935 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
936 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
937 (GCallback) default_select_stream, select_stream_accum, NULL,
938 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
941 * GstRTSPSrc::new-manager:
942 * @rtspsrc: a #GstRTSPSrc
943 * @manager: a #GstElement
945 * Emitted after a new manager (like rtpbin) was created and the default
946 * properties were configured.
950 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
951 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
952 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
953 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
956 * GstRTSPSrc::request-rtcp-key:
957 * @rtspsrc: a #GstRTSPSrc
958 * @num: the stream number
960 * Signal emitted to get the crypto parameters relevant to the RTCP
961 * stream. User should provide the key and the RTCP encryption ciphers
962 * and authentication, and return them wrapped in a GstCaps.
966 gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY] =
967 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
968 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
971 * GstRTSPSrc::accept-certificate:
972 * @rtspsrc: a #GstRTSPSrc
973 * @peer_cert: the peer's #GTlsCertificate
974 * @errors: the problems with @peer_cert
975 * @user_data: user data set when the signal handler was connected.
977 * This will directly map to #GTlsConnection 's "accept-certificate"
978 * signal and be performed after the default checks of #GstRTSPConnection
979 * (checking against the #GTlsDatabase with the given #GTlsCertificateFlags)
980 * have failed. If no #GTlsDatabase is set on this connection, only this
981 * signal will be emitted.
985 gst_rtspsrc_signals[SIGNAL_ACCEPT_CERTIFICATE] =
986 g_signal_new ("accept-certificate", G_TYPE_FROM_CLASS (klass),
987 G_SIGNAL_RUN_LAST, 0, g_signal_accumulator_true_handled, NULL, NULL,
988 G_TYPE_BOOLEAN, 3, G_TYPE_TLS_CONNECTION, G_TYPE_TLS_CERTIFICATE,
989 G_TYPE_TLS_CERTIFICATE_FLAGS);
992 * GstRTSPSrc::before-send
993 * @rtspsrc: a #GstRTSPSrc
994 * @num: the stream number
996 * Emitted before each RTSP request is sent, in order to allow
997 * the application to modify send parameters or to skip the message entirely.
998 * This can be used, for example, to work with ONVIF Profile G servers,
999 * which need a different/additional range, rate-control, and intra/x
1002 * Returns: %TRUE when the command should be sent, %FALSE when the
1003 * command should be dropped.
1007 gst_rtspsrc_signals[SIGNAL_BEFORE_SEND] =
1008 g_signal_new_class_handler ("before-send", G_TYPE_FROM_CLASS (klass),
1009 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
1010 (GCallback) default_before_send, before_send_accum, NULL,
1011 g_cclosure_marshal_generic, G_TYPE_BOOLEAN,
1012 1, GST_TYPE_RTSP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
1015 * GstRTSPSrc::push-backchannel-buffer:
1016 * @rtspsrc: a #GstRTSPSrc
1017 * @buffer: RTP buffer to send back
1021 gst_rtspsrc_signals[SIGNAL_PUSH_BACKCHANNEL_BUFFER] =
1022 g_signal_new ("push-backchannel-buffer", G_TYPE_FROM_CLASS (klass),
1023 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, G_STRUCT_OFFSET (GstRTSPSrcClass,
1024 push_backchannel_buffer), NULL, NULL, NULL, GST_TYPE_FLOW_RETURN, 2,
1025 G_TYPE_UINT, GST_TYPE_BUFFER);
1027 gstelement_class->send_event = gst_rtspsrc_send_event;
1028 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
1029 gstelement_class->change_state = gst_rtspsrc_change_state;
1031 gst_element_class_add_static_pad_template (gstelement_class, &rtptemplate);
1033 gst_element_class_set_static_metadata (gstelement_class,
1034 "RTSP packet receiver", "Source/Network",
1035 "Receive data over the network via RTSP (RFC 2326)",
1036 "Wim Taymans <wim@fluendo.com>, "
1037 "Thijs Vermeir <thijs.vermeir@barco.com>, "
1038 "Lutz Mueller <lutz@topfrose.de>");
1040 gstbin_class->handle_message = gst_rtspsrc_handle_message;
1042 klass->push_backchannel_buffer = gst_rtspsrc_push_backchannel_buffer;
1044 gst_rtsp_ext_list_init ();
1048 gst_rtspsrc_init (GstRTSPSrc * src)
1050 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
1051 src->protocols = DEFAULT_PROTOCOLS;
1052 src->debug = DEFAULT_DEBUG;
1053 src->retry = DEFAULT_RETRY;
1054 src->udp_timeout = DEFAULT_TIMEOUT;
1055 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
1056 src->latency = DEFAULT_LATENCY_MS;
1057 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
1058 src->connection_speed = DEFAULT_CONNECTION_SPEED;
1059 src->nat_method = DEFAULT_NAT_METHOD;
1060 src->do_rtcp = DEFAULT_DO_RTCP;
1061 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
1062 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
1063 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
1064 src->user_id = g_strdup (DEFAULT_USER_ID);
1065 src->user_pw = g_strdup (DEFAULT_USER_PW);
1066 src->buffer_mode = DEFAULT_BUFFER_MODE;
1067 src->client_port_range.min = 0;
1068 src->client_port_range.max = 0;
1069 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
1070 src->short_header = DEFAULT_SHORT_HEADER;
1071 src->probation = DEFAULT_PROBATION;
1072 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
1073 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1074 src->ntp_sync = DEFAULT_NTP_SYNC;
1075 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
1077 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
1078 src->tls_database = DEFAULT_TLS_DATABASE;
1079 src->tls_interaction = DEFAULT_TLS_INTERACTION;
1080 src->do_retransmission = DEFAULT_DO_RETRANSMISSION;
1081 src->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
1082 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
1083 src->max_rtcp_rtp_time_diff = DEFAULT_MAX_RTCP_RTP_TIME_DIFF;
1084 src->rfc7273_sync = DEFAULT_RFC7273_SYNC;
1085 src->max_ts_offset_adjustment = DEFAULT_MAX_TS_OFFSET_ADJUSTMENT;
1086 src->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
1087 src->max_ts_offset_is_set = FALSE;
1088 src->default_version = DEFAULT_VERSION;
1089 src->version = GST_RTSP_VERSION_INVALID;
1091 /* get a list of all extensions */
1092 src->extensions = gst_rtsp_ext_list_get ();
1094 /* connect to send signal */
1095 gst_rtsp_ext_list_connect (src->extensions, "send",
1096 (GCallback) gst_rtspsrc_send_cb, src);
1098 /* protects the streaming thread in interleaved mode or the polling
1099 * thread in UDP mode. */
1100 g_rec_mutex_init (&src->stream_rec_lock);
1102 /* protects our state changes from multiple invocations */
1103 g_rec_mutex_init (&src->state_rec_lock);
1105 src->state = GST_RTSP_STATE_INVALID;
1107 g_mutex_init (&src->conninfo.send_lock);
1108 g_mutex_init (&src->conninfo.recv_lock);
1110 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
1111 gst_bin_set_suppressed_flags (GST_BIN (src),
1112 GST_ELEMENT_FLAG_SOURCE | GST_ELEMENT_FLAG_SINK);
1116 gst_rtspsrc_finalize (GObject * object)
1118 GstRTSPSrc *rtspsrc;
1120 rtspsrc = GST_RTSPSRC (object);
1122 gst_rtsp_ext_list_free (rtspsrc->extensions);
1123 g_free (rtspsrc->conninfo.location);
1124 gst_rtsp_url_free (rtspsrc->conninfo.url);
1125 g_free (rtspsrc->conninfo.url_str);
1126 g_free (rtspsrc->user_id);
1127 g_free (rtspsrc->user_pw);
1128 g_free (rtspsrc->multi_iface);
1129 g_free (rtspsrc->user_agent);
1132 gst_sdp_message_free (rtspsrc->sdp);
1133 rtspsrc->sdp = NULL;
1135 if (rtspsrc->provided_clock)
1136 gst_object_unref (rtspsrc->provided_clock);
1139 gst_structure_free (rtspsrc->sdes);
1141 if (rtspsrc->tls_database)
1142 g_object_unref (rtspsrc->tls_database);
1144 if (rtspsrc->tls_interaction)
1145 g_object_unref (rtspsrc->tls_interaction);
1148 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
1149 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
1151 g_mutex_clear (&rtspsrc->conninfo.send_lock);
1152 g_mutex_clear (&rtspsrc->conninfo.recv_lock);
1154 G_OBJECT_CLASS (parent_class)->finalize (object);
1158 gst_rtspsrc_provide_clock (GstElement * element)
1160 GstRTSPSrc *src = GST_RTSPSRC (element);
1163 if ((clock = src->provided_clock) != NULL)
1164 return gst_object_ref (clock);
1166 return GST_ELEMENT_CLASS (parent_class)->provide_clock (element);
1169 /* a proxy string of the format [user:passwd@]host[:port] */
1171 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
1173 gchar *p, *at, *col;
1175 g_free (rtsp->proxy_user);
1176 rtsp->proxy_user = NULL;
1177 g_free (rtsp->proxy_passwd);
1178 rtsp->proxy_passwd = NULL;
1179 g_free (rtsp->proxy_host);
1180 rtsp->proxy_host = NULL;
1181 rtsp->proxy_port = 0;
1183 p = (gchar *) proxy;
1188 /* we allow http:// in front but ignore it */
1189 if (g_str_has_prefix (p, "http://"))
1192 at = strchr (p, '@');
1194 /* look for user:passwd */
1195 col = strchr (proxy, ':');
1196 if (col == NULL || col > at)
1199 rtsp->proxy_user = g_strndup (p, col - p);
1201 rtsp->proxy_passwd = g_strndup (col, at - col);
1206 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
1207 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
1208 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
1209 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
1210 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
1211 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
1212 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
1215 col = strchr (p, ':');
1218 /* everything before the colon is the hostname */
1219 rtsp->proxy_host = g_strndup (p, col - p);
1221 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
1223 rtsp->proxy_host = g_strdup (p);
1224 rtsp->proxy_port = 8080;
1230 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
1232 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
1233 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
1236 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
1238 rtspsrc->ptcp_timeout = NULL;
1242 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
1245 GstRTSPSrc *rtspsrc;
1247 rtspsrc = GST_RTSPSRC (object);
1251 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
1252 g_value_get_string (value), NULL);
1254 case PROP_PROTOCOLS:
1255 rtspsrc->protocols = g_value_get_flags (value);
1258 rtspsrc->debug = g_value_get_boolean (value);
1261 rtspsrc->retry = g_value_get_uint (value);
1264 rtspsrc->udp_timeout = g_value_get_uint64 (value);
1266 case PROP_TCP_TIMEOUT:
1267 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
1270 rtspsrc->latency = g_value_get_uint (value);
1272 case PROP_DROP_ON_LATENCY:
1273 rtspsrc->drop_on_latency = g_value_get_boolean (value);
1275 case PROP_CONNECTION_SPEED:
1276 rtspsrc->connection_speed = g_value_get_uint64 (value);
1278 case PROP_NAT_METHOD:
1279 rtspsrc->nat_method = g_value_get_enum (value);
1282 rtspsrc->do_rtcp = g_value_get_boolean (value);
1284 case PROP_DO_RTSP_KEEP_ALIVE:
1285 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
1288 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
1291 g_free (rtspsrc->prop_proxy_id);
1292 rtspsrc->prop_proxy_id = g_value_dup_string (value);
1295 g_free (rtspsrc->prop_proxy_pw);
1296 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
1298 case PROP_RTP_BLOCKSIZE:
1299 rtspsrc->rtp_blocksize = g_value_get_uint (value);
1302 g_free (rtspsrc->user_id);
1303 rtspsrc->user_id = g_value_dup_string (value);
1306 g_free (rtspsrc->user_pw);
1307 rtspsrc->user_pw = g_value_dup_string (value);
1309 case PROP_BUFFER_MODE:
1310 rtspsrc->buffer_mode = g_value_get_enum (value);
1312 case PROP_PORT_RANGE:
1316 str = g_value_get_string (value);
1317 if (sscanf (str, "%u-%u", &rtspsrc->client_port_range.min,
1318 &rtspsrc->client_port_range.max) != 2) {
1319 rtspsrc->client_port_range.min = 0;
1320 rtspsrc->client_port_range.max = 0;
1324 case PROP_UDP_BUFFER_SIZE:
1325 rtspsrc->udp_buffer_size = g_value_get_int (value);
1327 case PROP_SHORT_HEADER:
1328 rtspsrc->short_header = g_value_get_boolean (value);
1330 case PROP_PROBATION:
1331 rtspsrc->probation = g_value_get_uint (value);
1333 case PROP_UDP_RECONNECT:
1334 rtspsrc->udp_reconnect = g_value_get_boolean (value);
1336 case PROP_MULTICAST_IFACE:
1337 g_free (rtspsrc->multi_iface);
1339 if (g_value_get_string (value) == NULL)
1340 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1342 rtspsrc->multi_iface = g_value_dup_string (value);
1345 rtspsrc->ntp_sync = g_value_get_boolean (value);
1346 /* The default value of max_ts_offset depends on ntp_sync. If user
1347 * hasn't set it then change default value */
1348 if (!rtspsrc->max_ts_offset_is_set) {
1349 if (rtspsrc->ntp_sync) {
1350 rtspsrc->max_ts_offset = 0;
1352 rtspsrc->max_ts_offset = DEFAULT_MAX_TS_OFFSET;
1356 case PROP_USE_PIPELINE_CLOCK:
1357 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
1360 rtspsrc->sdes = g_value_dup_boxed (value);
1362 case PROP_TLS_VALIDATION_FLAGS:
1363 rtspsrc->tls_validation_flags = g_value_get_flags (value);
1365 case PROP_TLS_DATABASE:
1366 g_clear_object (&rtspsrc->tls_database);
1367 rtspsrc->tls_database = g_value_dup_object (value);
1369 case PROP_TLS_INTERACTION:
1370 g_clear_object (&rtspsrc->tls_interaction);
1371 rtspsrc->tls_interaction = g_value_dup_object (value);
1373 case PROP_DO_RETRANSMISSION:
1374 rtspsrc->do_retransmission = g_value_get_boolean (value);
1376 case PROP_NTP_TIME_SOURCE:
1377 rtspsrc->ntp_time_source = g_value_get_enum (value);
1379 case PROP_USER_AGENT:
1380 g_free (rtspsrc->user_agent);
1381 rtspsrc->user_agent = g_value_dup_string (value);
1383 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1384 rtspsrc->max_rtcp_rtp_time_diff = g_value_get_int (value);
1386 case PROP_RFC7273_SYNC:
1387 rtspsrc->rfc7273_sync = g_value_get_boolean (value);
1389 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
1390 rtspsrc->max_ts_offset_adjustment = g_value_get_uint64 (value);
1392 case PROP_MAX_TS_OFFSET:
1393 rtspsrc->max_ts_offset = g_value_get_int64 (value);
1394 rtspsrc->max_ts_offset_is_set = TRUE;
1396 case PROP_DEFAULT_VERSION:
1397 rtspsrc->default_version = g_value_get_enum (value);
1399 case PROP_BACKCHANNEL:
1400 rtspsrc->backchannel = g_value_get_enum (value);
1403 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1409 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1412 GstRTSPSrc *rtspsrc;
1414 rtspsrc = GST_RTSPSRC (object);
1418 g_value_set_string (value, rtspsrc->conninfo.location);
1420 case PROP_PROTOCOLS:
1421 g_value_set_flags (value, rtspsrc->protocols);
1424 g_value_set_boolean (value, rtspsrc->debug);
1427 g_value_set_uint (value, rtspsrc->retry);
1430 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1432 case PROP_TCP_TIMEOUT:
1436 timeout = ((guint64) rtspsrc->tcp_timeout.tv_sec) * G_USEC_PER_SEC +
1437 rtspsrc->tcp_timeout.tv_usec;
1438 g_value_set_uint64 (value, timeout);
1442 g_value_set_uint (value, rtspsrc->latency);
1444 case PROP_DROP_ON_LATENCY:
1445 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1447 case PROP_CONNECTION_SPEED:
1448 g_value_set_uint64 (value, rtspsrc->connection_speed);
1450 case PROP_NAT_METHOD:
1451 g_value_set_enum (value, rtspsrc->nat_method);
1454 g_value_set_boolean (value, rtspsrc->do_rtcp);
1456 case PROP_DO_RTSP_KEEP_ALIVE:
1457 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1463 if (rtspsrc->proxy_host) {
1465 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1469 g_value_take_string (value, str);
1473 g_value_set_string (value, rtspsrc->prop_proxy_id);
1476 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1478 case PROP_RTP_BLOCKSIZE:
1479 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1482 g_value_set_string (value, rtspsrc->user_id);
1485 g_value_set_string (value, rtspsrc->user_pw);
1487 case PROP_BUFFER_MODE:
1488 g_value_set_enum (value, rtspsrc->buffer_mode);
1490 case PROP_PORT_RANGE:
1494 if (rtspsrc->client_port_range.min != 0) {
1495 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1496 rtspsrc->client_port_range.max);
1500 g_value_take_string (value, str);
1503 case PROP_UDP_BUFFER_SIZE:
1504 g_value_set_int (value, rtspsrc->udp_buffer_size);
1506 case PROP_SHORT_HEADER:
1507 g_value_set_boolean (value, rtspsrc->short_header);
1509 case PROP_PROBATION:
1510 g_value_set_uint (value, rtspsrc->probation);
1512 case PROP_UDP_RECONNECT:
1513 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1515 case PROP_MULTICAST_IFACE:
1516 g_value_set_string (value, rtspsrc->multi_iface);
1519 g_value_set_boolean (value, rtspsrc->ntp_sync);
1521 case PROP_USE_PIPELINE_CLOCK:
1522 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1525 g_value_set_boxed (value, rtspsrc->sdes);
1527 case PROP_TLS_VALIDATION_FLAGS:
1528 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1530 case PROP_TLS_DATABASE:
1531 g_value_set_object (value, rtspsrc->tls_database);
1533 case PROP_TLS_INTERACTION:
1534 g_value_set_object (value, rtspsrc->tls_interaction);
1536 case PROP_DO_RETRANSMISSION:
1537 g_value_set_boolean (value, rtspsrc->do_retransmission);
1539 case PROP_NTP_TIME_SOURCE:
1540 g_value_set_enum (value, rtspsrc->ntp_time_source);
1542 case PROP_USER_AGENT:
1543 g_value_set_string (value, rtspsrc->user_agent);
1545 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1546 g_value_set_int (value, rtspsrc->max_rtcp_rtp_time_diff);
1548 case PROP_RFC7273_SYNC:
1549 g_value_set_boolean (value, rtspsrc->rfc7273_sync);
1551 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
1552 g_value_set_uint64 (value, rtspsrc->max_ts_offset_adjustment);
1554 case PROP_MAX_TS_OFFSET:
1555 g_value_set_int64 (value, rtspsrc->max_ts_offset);
1557 case PROP_DEFAULT_VERSION:
1558 g_value_set_enum (value, rtspsrc->default_version);
1560 case PROP_BACKCHANNEL:
1561 g_value_set_enum (value, rtspsrc->backchannel);
1564 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1570 find_stream_by_id (GstRTSPStream * stream, gint * id)
1572 if (stream->id == *id)
1579 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1581 /* ignore unconfigured channels here (e.g., those that
1582 * were explicitly skipped during SETUP) */
1583 if ((stream->channelpad[0] != NULL) &&
1584 (stream->channel[0] == *channel || stream->channel[1] == *channel))
1591 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1593 GstElement *src = (GstElement *) a;
1595 if (stream->udpsrc[0] == src)
1597 if (stream->udpsrc[1] == src)
1604 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1606 if (stream->conninfo.location) {
1607 /* check qualified setup_url */
1608 if (!strcmp (stream->conninfo.location, (gchar *) a))
1611 if (stream->control_url) {
1612 /* check original control_url */
1613 if (!strcmp (stream->control_url, (gchar *) a))
1616 /* check if qualified setup_url ends with string */
1617 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1624 static GstRTSPStream *
1625 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1629 /* find and get stream */
1630 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1631 return (GstRTSPStream *) lstream->data;
1636 static const GstSDPBandwidth *
1637 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1638 const GstSDPMedia * media, const gchar * type)
1642 /* first look in the media specific section */
1643 len = gst_sdp_media_bandwidths_len (media);
1644 for (i = 0; i < len; i++) {
1645 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1647 if (strcmp (bw->bwtype, type) == 0)
1650 /* then look in the message specific section */
1651 len = gst_sdp_message_bandwidths_len (sdp);
1652 for (i = 0; i < len; i++) {
1653 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1655 if (strcmp (bw->bwtype, type) == 0)
1662 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1663 const GstSDPMedia * media, GstRTSPStream * stream)
1665 const GstSDPBandwidth *bw;
1667 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1668 stream->as_bandwidth = bw->bandwidth;
1670 stream->as_bandwidth = -1;
1672 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1673 stream->rr_bandwidth = bw->bandwidth;
1675 stream->rr_bandwidth = -1;
1677 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1678 stream->rs_bandwidth = bw->bandwidth;
1680 stream->rs_bandwidth = -1;
1684 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1685 const GstSDPConnection * conn)
1687 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1690 if (conn->addrtype == NULL)
1693 /* check for IPV6 */
1694 if (strcmp (conn->addrtype, "IP4") == 0)
1695 stream->is_ipv6 = FALSE;
1696 else if (strcmp (conn->addrtype, "IP6") == 0)
1697 stream->is_ipv6 = TRUE;
1702 g_free (stream->destination);
1703 stream->destination = g_strdup (conn->address);
1705 /* check for multicast */
1706 stream->is_multicast =
1707 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1709 stream->ttl = conn->ttl;
1712 /* Go over the connections for a stream.
1713 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1715 * - If we are dealing with a localhost address, we disable multicast
1718 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1719 const GstSDPMedia * media, GstRTSPStream * stream)
1721 const GstSDPConnection *conn;
1724 /* first look in the media specific section */
1725 len = gst_sdp_media_connections_len (media);
1726 for (i = 0; i < len; i++) {
1727 conn = gst_sdp_media_get_connection (media, i);
1729 gst_rtspsrc_do_stream_connection (src, stream, conn);
1731 /* then look in the message specific section */
1732 if ((conn = gst_sdp_message_get_connection (sdp))) {
1733 gst_rtspsrc_do_stream_connection (src, stream, conn);
1738 make_stream_id (GstRTSPStream * stream, const GstSDPMedia * media)
1741 g_strdup_printf ("%s:%d:%d:%s:%d", media->media, media->port,
1742 media->num_ports, media->proto, stream->default_pt);
1744 g_strcanon (stream_id, G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS, ':');
1749 /* m=<media> <UDP port> RTP/AVP <payload>
1752 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
1753 const GstSDPMedia * media, GstRTSPStream * stream)
1757 GstCaps *global_caps;
1760 proto = gst_sdp_media_get_proto (media);
1764 if (g_str_equal (proto, "RTP/AVP"))
1765 stream->profile = GST_RTSP_PROFILE_AVP;
1766 else if (g_str_equal (proto, "RTP/SAVP"))
1767 stream->profile = GST_RTSP_PROFILE_SAVP;
1768 else if (g_str_equal (proto, "RTP/AVPF"))
1769 stream->profile = GST_RTSP_PROFILE_AVPF;
1770 else if (g_str_equal (proto, "RTP/SAVPF"))
1771 stream->profile = GST_RTSP_PROFILE_SAVPF;
1775 if (gst_sdp_media_get_attribute_val (media, "sendonly") != NULL &&
1776 /* We want to setup caps for streams configured as backchannel */
1777 !stream->is_backchannel && src->backchannel != BACKCHANNEL_NONE)
1778 goto sendonly_media;
1780 /* Parse global SDP attributes once */
1781 global_caps = gst_caps_new_empty_simple ("application/x-unknown");
1782 GST_DEBUG ("mapping sdp session level attributes to caps");
1783 gst_sdp_message_attributes_to_caps (sdp, global_caps);
1784 GST_DEBUG ("mapping sdp media level attributes to caps");
1785 gst_sdp_media_attributes_to_caps (media, global_caps);
1787 /* Keep a copy of the SDP key management */
1788 gst_sdp_media_parse_keymgmt (media, &stream->mikey);
1789 if (stream->mikey == NULL)
1790 gst_sdp_message_parse_keymgmt (sdp, &stream->mikey);
1792 len = gst_sdp_media_formats_len (media);
1793 for (i = 0; i < len; i++) {
1795 GstCaps *caps, *outcaps;
1800 pt = atoi (gst_sdp_media_get_format (media, i));
1802 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
1805 caps = gst_sdp_media_get_caps_from_media (media, pt);
1807 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
1811 /* do some tweaks */
1812 s = gst_caps_get_structure (caps, 0);
1813 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
1814 stream->is_real = (strstr (enc, "-REAL") != NULL);
1815 if (strcmp (enc, "X-ASF-PF") == 0)
1816 stream->container = TRUE;
1819 /* Merge in global caps */
1820 /* Intersect will merge in missing fields to the current caps */
1821 outcaps = gst_caps_intersect (caps, global_caps);
1822 gst_caps_unref (caps);
1824 /* the first pt will be the default */
1825 if (stream->ptmap->len == 0)
1826 stream->default_pt = pt;
1829 item.caps = outcaps;
1831 g_array_append_val (stream->ptmap, item);
1834 stream->stream_id = make_stream_id (stream, media);
1836 gst_caps_unref (global_caps);
1841 GST_ERROR_OBJECT (src, "can't find proto in media");
1846 GST_ERROR_OBJECT (src, "unknown proto in media: '%s'", proto);
1851 GST_DEBUG_OBJECT (src, "sendonly media ignored, no backchannel");
1856 static const gchar *
1857 get_aggregate_control (GstRTSPSrc * src)
1862 base = src->control;
1863 else if (src->content_base)
1864 base = src->content_base;
1865 else if (src->conninfo.url_str)
1866 base = src->conninfo.url_str;
1874 clear_ptmap_item (PtMapItem * item)
1877 gst_caps_unref (item->caps);
1880 static GstRTSPStream *
1881 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx,
1884 GstRTSPStream *stream;
1885 const gchar *control_url;
1886 const GstSDPMedia *media;
1888 /* get media, should not return NULL */
1889 media = gst_sdp_message_get_media (sdp, idx);
1893 stream = g_new0 (GstRTSPStream, 1);
1894 stream->parent = src;
1895 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1897 stream->last_ret = GST_FLOW_NOT_LINKED;
1898 stream->added = FALSE;
1899 stream->setup = FALSE;
1900 stream->skipped = FALSE;
1902 stream->eos = FALSE;
1903 stream->discont = TRUE;
1904 stream->seqbase = -1;
1905 stream->timebase = -1;
1906 stream->send_ssrc = g_random_int ();
1907 stream->profile = GST_RTSP_PROFILE_AVP;
1908 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1909 stream->mikey = NULL;
1910 stream->stream_id = NULL;
1911 stream->is_backchannel = FALSE;
1912 g_mutex_init (&stream->conninfo.send_lock);
1913 g_mutex_init (&stream->conninfo.recv_lock);
1914 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
1916 /* stream is sendonly and onvif backchannel is requested */
1917 if (gst_sdp_media_get_attribute_val (media, "sendonly") != NULL &&
1918 src->backchannel != BACKCHANNEL_NONE)
1919 stream->is_backchannel = TRUE;
1921 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1922 * session manager to scale RTCP. */
1923 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1925 /* collect connection info */
1926 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1928 /* make the payload type map */
1929 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1931 /* collect port number */
1932 stream->port = gst_sdp_media_get_port (media);
1934 /* get control url to construct the setup url. The setup url is used to
1935 * configure the transport of the stream and is used to identity the stream in
1936 * the RTP-Info header field returned from PLAY. */
1937 control_url = gst_sdp_media_get_attribute_val (media, "control");
1938 if (control_url == NULL)
1939 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1941 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1942 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1943 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1944 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1946 /* RFC 2326, C.3: missing control_url permitted in case of a single stream */
1947 if (control_url == NULL && n_streams == 1) {
1951 if (control_url != NULL) {
1952 stream->control_url = g_strdup (control_url);
1953 /* Build a fully qualified url using the content_base if any or by prefixing
1954 * the original request.
1955 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1956 * likely build a URL that the server will fail to understand, this is ok,
1957 * we will fail then. */
1958 if (g_str_has_prefix (control_url, "rtsp://"))
1959 stream->conninfo.location = g_strdup (control_url);
1964 if (g_strcmp0 (control_url, "*") == 0)
1967 base = get_aggregate_control (src);
1969 /* check if the base ends or control starts with / */
1970 has_slash = g_str_has_prefix (control_url, "/");
1971 has_slash = has_slash || g_str_has_suffix (base, "/");
1973 /* concatenate the two strings, insert / when not present */
1974 stream->conninfo.location =
1975 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1978 GST_DEBUG_OBJECT (src, " setup: %s",
1979 GST_STR_NULL (stream->conninfo.location));
1981 /* we keep track of all streams */
1982 src->streams = g_list_append (src->streams, stream);
1990 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1994 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1996 g_array_free (stream->ptmap, TRUE);
1998 g_free (stream->destination);
1999 g_free (stream->control_url);
2000 g_free (stream->conninfo.location);
2001 g_free (stream->stream_id);
2003 for (i = 0; i < 2; i++) {
2004 if (stream->udpsrc[i]) {
2005 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
2006 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
2007 gst_object_unref (stream->udpsrc[i]);
2009 if (stream->channelpad[i])
2010 gst_object_unref (stream->channelpad[i]);
2012 if (stream->udpsink[i]) {
2013 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
2014 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
2015 gst_object_unref (stream->udpsink[i]);
2018 if (stream->rtpsrc) {
2019 gst_element_set_state (stream->rtpsrc, GST_STATE_NULL);
2020 gst_bin_remove (GST_BIN_CAST (src), stream->rtpsrc);
2021 gst_object_unref (stream->rtpsrc);
2023 if (stream->srcpad) {
2024 gst_pad_set_active (stream->srcpad, FALSE);
2026 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2028 if (stream->srtpenc)
2029 gst_object_unref (stream->srtpenc);
2030 if (stream->srtpdec)
2031 gst_object_unref (stream->srtpdec);
2032 if (stream->srtcpparams)
2033 gst_caps_unref (stream->srtcpparams);
2035 gst_mikey_message_unref (stream->mikey);
2036 if (stream->rtcppad)
2037 gst_object_unref (stream->rtcppad);
2038 if (stream->session)
2039 g_object_unref (stream->session);
2040 if (stream->rtx_pt_map)
2041 gst_structure_free (stream->rtx_pt_map);
2043 g_mutex_clear (&stream->conninfo.send_lock);
2044 g_mutex_clear (&stream->conninfo.recv_lock);
2050 gst_rtspsrc_cleanup (GstRTSPSrc * src)
2054 GST_DEBUG_OBJECT (src, "cleanup");
2056 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2057 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2059 gst_rtspsrc_stream_free (src, stream);
2061 g_list_free (src->streams);
2062 src->streams = NULL;
2064 if (src->manager_sig_id) {
2065 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
2066 src->manager_sig_id = 0;
2068 gst_element_set_state (src->manager, GST_STATE_NULL);
2069 gst_bin_remove (GST_BIN_CAST (src), src->manager);
2070 src->manager = NULL;
2073 gst_structure_free (src->props);
2076 g_free (src->content_base);
2077 src->content_base = NULL;
2079 g_free (src->control);
2080 src->control = NULL;
2083 gst_rtsp_range_free (src->range);
2086 /* don't clear the SDP when it was used in the url */
2087 if (src->sdp && !src->from_sdp) {
2088 gst_sdp_message_free (src->sdp);
2092 src->need_segment = FALSE;
2094 if (src->provided_clock) {
2095 gst_object_unref (src->provided_clock);
2096 src->provided_clock = NULL;
2101 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
2102 gint * rtpport, gint * rtcpport)
2105 GstStateChangeReturn ret;
2106 GstElement *udpsrc0, *udpsrc1;
2107 gint tmp_rtp, tmp_rtcp;
2111 src = stream->parent;
2117 /* Start at next port */
2118 tmp_rtp = src->next_port_num;
2120 if (stream->is_ipv6)
2121 host = "udp://[::0]";
2123 host = "udp://0.0.0.0";
2125 /* try to allocate 2 UDP ports, the RTP port should be an even
2126 * number and the RTCP port should be the next (uneven) port */
2129 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
2130 tmp_rtp >= src->client_port_range.max)
2133 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2134 if (udpsrc0 == NULL)
2135 goto no_udp_protocol;
2136 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
2138 if (src->udp_buffer_size != 0)
2139 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
2142 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
2143 if (ret == GST_STATE_CHANGE_FAILURE) {
2145 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
2148 if (++count > src->retry)
2151 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2152 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2153 gst_object_unref (udpsrc0);
2156 GST_DEBUG_OBJECT (src, "retry %d", count);
2159 goto no_udp_protocol;
2162 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
2163 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
2165 /* check if port is even */
2166 if ((tmp_rtp & 0x01) != 0) {
2167 /* port not even, close and allocate another */
2168 if (++count > src->retry)
2171 GST_DEBUG_OBJECT (src, "RTP port not even");
2173 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2174 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2175 gst_object_unref (udpsrc0);
2178 GST_DEBUG_OBJECT (src, "retry %d", count);
2183 /* allocate port+1 for RTCP now */
2184 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2185 if (udpsrc1 == NULL)
2186 goto no_udp_rtcp_protocol;
2189 tmp_rtcp = tmp_rtp + 1;
2190 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
2193 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
2195 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
2196 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
2197 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
2198 if (ret == GST_STATE_CHANGE_FAILURE) {
2199 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
2201 if (++count > src->retry)
2204 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2205 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2206 gst_object_unref (udpsrc0);
2209 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2210 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2211 gst_object_unref (udpsrc1);
2215 GST_DEBUG_OBJECT (src, "retry %d", count);
2219 /* all fine, do port check */
2220 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2221 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2223 /* this should not happen... */
2224 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2227 /* we keep these elements, we configure all in configure_transport when the
2228 * server told us to really use the UDP ports. */
2229 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2230 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2231 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2232 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2234 /* keep track of next available port number when we have a range
2236 if (src->next_port_num != 0)
2237 src->next_port_num = tmp_rtcp + 1;
2244 GST_DEBUG_OBJECT (src, "could not get UDP source");
2249 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2253 no_udp_rtcp_protocol:
2255 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2260 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2261 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2267 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2268 gst_object_unref (udpsrc0);
2271 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2272 gst_object_unref (udpsrc1);
2279 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2284 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2286 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2287 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2290 for (i = 0; i < 2; i++) {
2291 if (stream->udpsrc[i])
2292 gst_element_set_state (stream->udpsrc[i], state);
2298 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2305 event = gst_event_new_flush_start ();
2306 GST_DEBUG_OBJECT (src, "start flush");
2308 state = GST_STATE_PAUSED;
2310 event = gst_event_new_flush_stop (FALSE);
2311 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2314 state = GST_STATE_PLAYING;
2316 state = GST_STATE_PAUSED;
2318 gst_rtspsrc_push_event (src, event);
2319 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2320 gst_rtspsrc_set_state (src, state);
2323 static GstRTSPResult
2324 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2325 GstRTSPMessage * message, GTimeVal * timeout)
2329 if (conninfo->connection) {
2330 g_mutex_lock (&conninfo->send_lock);
2331 ret = gst_rtsp_connection_send (conninfo->connection, message, timeout);
2332 g_mutex_unlock (&conninfo->send_lock);
2334 ret = GST_RTSP_ERROR;
2340 static GstRTSPResult
2341 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2342 GstRTSPMessage * message, GTimeVal * timeout)
2346 if (conninfo->connection) {
2347 g_mutex_lock (&conninfo->recv_lock);
2348 ret = gst_rtsp_connection_receive (conninfo->connection, message, timeout);
2349 g_mutex_unlock (&conninfo->recv_lock);
2351 ret = GST_RTSP_ERROR;
2358 gst_rtspsrc_get_position (GstRTSPSrc * src)
2363 query = gst_query_new_position (GST_FORMAT_TIME);
2364 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2365 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2366 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2370 if (stream->srcpad) {
2371 if (gst_pad_query (stream->srcpad, query)) {
2372 gst_query_parse_position (query, &fmt, &pos);
2373 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2374 GST_TIME_ARGS (pos));
2375 src->last_pos = pos;
2385 gst_query_unref (query);
2389 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2394 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2396 gboolean flush, skip;
2399 GstSegment seeksegment = { 0, };
2401 const gchar *seek_style = NULL;
2404 GST_DEBUG_OBJECT (src, "doing seek with event %" GST_PTR_FORMAT, event);
2406 gst_event_parse_seek (event, &rate, &format, &flags,
2407 &cur_type, &cur, &stop_type, &stop);
2409 /* no negative rates yet */
2413 /* we need TIME format */
2414 if (format != src->segment.format)
2417 /* Check if we are not at all seekable */
2418 if (src->seekable == -1.0)
2421 /* Additional seeking-to-beginning-only check */
2422 if (src->seekable == 0.0 && cur != 0)
2425 GST_DEBUG_OBJECT (src, "doing seek without event");
2427 cur_type = GST_SEEK_TYPE_SET;
2428 stop_type = GST_SEEK_TYPE_SET;
2431 if (flags & GST_SEEK_FLAG_SEGMENT)
2432 goto invalid_segment_flag;
2434 /* get flush flag */
2435 flush = flags & GST_SEEK_FLAG_FLUSH;
2436 skip = flags & GST_SEEK_FLAG_SKIP;
2438 /* now we need to make sure the streaming thread is stopped. We do this by
2439 * either sending a FLUSH_START event downstream which will cause the
2440 * streaming thread to stop with a WRONG_STATE.
2441 * For a non-flushing seek we simply pause the task, which will happen as soon
2442 * as it completes one iteration (and thus might block when the sink is
2443 * blocking in preroll). */
2445 GST_DEBUG_OBJECT (src, "starting flush");
2446 gst_rtspsrc_flush (src, TRUE, FALSE);
2449 gst_task_pause (src->task);
2453 /* we should now be able to grab the streaming thread because we stopped it
2454 * with the above flush/pause code */
2455 GST_RTSP_STREAM_LOCK (src);
2457 GST_DEBUG_OBJECT (src, "stopped streaming");
2459 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2460 gst_rtspsrc_connection_flush (src, FALSE);
2462 /* copy segment, we need this because we still need the old
2463 * segment when we close the current segment. */
2464 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2466 /* configure the seek parameters in the seeksegment. We will then have the
2467 * right values in the segment to perform the seek */
2469 GST_DEBUG_OBJECT (src, "configuring seek");
2470 gst_segment_do_seek (&seeksegment, rate, format, flags,
2471 cur_type, cur, stop_type, stop, &update);
2474 /* figure out the last position we need to play. If it's configured (stop !=
2475 * -1), use that, else we play until the total duration of the file */
2476 if ((stop = seeksegment.stop) == -1)
2477 stop = seeksegment.duration;
2479 /* if we were playing, pause first */
2480 playing = (src->state == GST_RTSP_STATE_PLAYING);
2482 /* obtain current position in case seek fails */
2483 gst_rtspsrc_get_position (src);
2484 gst_rtspsrc_pause (src, FALSE);
2488 src->state = GST_RTSP_STATE_SEEKING;
2490 /* PLAY will add the range header now. */
2491 src->need_range = TRUE;
2493 /* prepare for streaming again */
2495 /* if we started flush, we stop now */
2496 GST_DEBUG_OBJECT (src, "stopping flush");
2497 gst_rtspsrc_flush (src, FALSE, playing);
2500 /* now we did the seek and can activate the new segment values */
2501 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2503 /* if we're doing a segment seek, post a SEGMENT_START message */
2504 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2505 gst_element_post_message (GST_ELEMENT_CAST (src),
2506 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2507 src->segment.format, src->segment.position));
2510 /* now create the newsegment */
2511 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2512 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2515 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2516 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2517 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2518 stream->discont = TRUE;
2521 /* and continue playing if needed */
2522 GST_OBJECT_LOCK (src);
2523 playing = (GST_STATE_PENDING (src) == GST_STATE_VOID_PENDING
2524 && GST_STATE (src) == GST_STATE_PLAYING)
2525 || (GST_STATE_PENDING (src) == GST_STATE_PLAYING);
2526 GST_OBJECT_UNLOCK (src);
2528 if (src->version >= GST_RTSP_VERSION_2_0) {
2529 if (flags & GST_SEEK_FLAG_ACCURATE)
2531 else if (flags & GST_SEEK_FLAG_KEY_UNIT)
2532 seek_style = "CoRAP";
2533 else if (flags & GST_SEEK_FLAG_KEY_UNIT
2534 && flags & GST_SEEK_FLAG_SNAP_BEFORE)
2535 seek_style = "First-Prior";
2536 else if (flags & GST_SEEK_FLAG_KEY_UNIT && flags & GST_SEEK_FLAG_SNAP_AFTER)
2537 seek_style = "Next";
2541 gst_rtspsrc_play (src, &seeksegment, FALSE, seek_style);
2543 GST_RTSP_STREAM_UNLOCK (src);
2550 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2555 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2560 GST_DEBUG_OBJECT (src, "stream is not seekable");
2563 invalid_segment_flag:
2565 GST_WARNING_OBJECT (src, "Segment seeks not supported");
2571 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2575 gboolean res = TRUE;
2578 src = GST_RTSPSRC_CAST (parent);
2580 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2581 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2583 switch (GST_EVENT_TYPE (event)) {
2584 case GST_EVENT_SEEK:
2585 res = gst_rtspsrc_perform_seek (src, event);
2589 case GST_EVENT_NAVIGATION:
2590 case GST_EVENT_LATENCY:
2598 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2599 res = gst_pad_send_event (target, event);
2600 gst_object_unref (target);
2602 gst_event_unref (event);
2605 gst_event_unref (event);
2612 gst_rtspsrc_handle_src_sink_event (GstPad * pad, GstObject * parent,
2615 GstRTSPStream *stream;
2617 stream = gst_pad_get_element_private (pad);
2619 switch (GST_EVENT_TYPE (event)) {
2620 case GST_EVENT_STREAM_START:{
2621 const gchar *upstream_id;
2624 gst_event_parse_stream_start (event, &upstream_id);
2625 stream_id = g_strdup_printf ("%s/%s", upstream_id, stream->stream_id);
2627 gst_event_unref (event);
2628 event = gst_event_new_stream_start (stream_id);
2636 return gst_pad_push_event (stream->srcpad, event);
2639 /* this is the final event function we receive on the internal source pad when
2640 * we deal with TCP connections */
2642 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2647 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2649 switch (GST_EVENT_TYPE (event)) {
2650 case GST_EVENT_SEEK:
2652 case GST_EVENT_NAVIGATION:
2653 case GST_EVENT_LATENCY:
2655 gst_event_unref (event);
2662 /* this is the final query function we receive on the internal source pad when
2663 * we deal with TCP connections */
2665 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2669 gboolean res = TRUE;
2671 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2673 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2674 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2676 switch (GST_QUERY_TYPE (query)) {
2677 case GST_QUERY_POSITION:
2682 case GST_QUERY_DURATION:
2686 gst_query_parse_duration (query, &format, NULL);
2689 case GST_FORMAT_TIME:
2690 gst_query_set_duration (query, format, src->segment.duration);
2698 case GST_QUERY_LATENCY:
2700 /* we are live with a min latency of 0 and unlimited max latency, this
2701 * result will be updated by the session manager if there is any. */
2702 gst_query_set_latency (query, TRUE, 0, -1);
2712 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2714 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2718 gboolean res = FALSE;
2720 src = GST_RTSPSRC_CAST (parent);
2722 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2723 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2725 switch (GST_QUERY_TYPE (query)) {
2726 case GST_QUERY_DURATION:
2730 gst_query_parse_duration (query, &format, NULL);
2733 case GST_FORMAT_TIME:
2734 gst_query_set_duration (query, format, src->segment.duration);
2742 case GST_QUERY_SEEKING:
2746 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2747 if (format == GST_FORMAT_TIME) {
2749 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2750 GstClockTime start = 0, duration = src->segment.duration;
2752 /* seeking without duration is unlikely */
2753 seekable = seekable && src->seekable >= 0.0 && src->segment.duration &&
2754 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2757 if (src->seekable > 0.0) {
2758 start = src->last_pos - src->seekable * GST_SECOND;
2760 /* src->seekable == 0 means that we can only seek to 0 */
2766 GST_LOG_OBJECT (src, "seekable : %d", seekable);
2768 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, start,
2778 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2780 gst_query_set_uri (query, uri);
2788 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2790 /* forward the query to the proxy target pad */
2792 res = gst_pad_query (target, query);
2793 gst_object_unref (target);
2802 /* callback for RTCP messages to be sent to the server when operating in TCP
2804 static GstFlowReturn
2805 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2808 GstRTSPStream *stream;
2809 GstFlowReturn res = GST_FLOW_OK;
2814 GstRTSPMessage message = { 0 };
2815 GstRTSPConnInfo *conninfo;
2817 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2818 src = stream->parent;
2820 gst_buffer_map (buffer, &map, GST_MAP_READ);
2824 gst_rtsp_message_init_data (&message, stream->channel[1]);
2826 /* lend the body data to the message */
2827 gst_rtsp_message_take_body (&message, data, size);
2829 if (stream->conninfo.connection)
2830 conninfo = &stream->conninfo;
2832 conninfo = &src->conninfo;
2834 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2835 ret = gst_rtspsrc_connection_send (src, conninfo, &message, NULL);
2836 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2838 /* and steal it away again because we will free it when unreffing the
2840 gst_rtsp_message_steal_body (&message, &data, &size);
2841 gst_rtsp_message_unset (&message);
2843 gst_buffer_unmap (buffer, &map);
2844 gst_buffer_unref (buffer);
2849 static GstFlowReturn
2850 gst_rtspsrc_push_backchannel_buffer (GstRTSPSrc * src, guint id,
2853 GstFlowReturn res = GST_FLOW_OK;
2854 GstRTSPStream *stream;
2856 if (!src->conninfo.connected || src->state != GST_RTSP_STATE_PLAYING)
2859 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2860 if (stream == NULL) {
2861 GST_ERROR_OBJECT (src, "no stream with id %u", id);
2865 if (src->interleaved) {
2871 GstRTSPMessage message = { 0 };
2872 GstRTSPConnInfo *conninfo;
2874 buffer = gst_sample_get_buffer (sample);
2876 gst_buffer_map (buffer, &map, GST_MAP_READ);
2880 gst_rtsp_message_init_data (&message, stream->channel[0]);
2882 /* lend the body data to the message */
2883 gst_rtsp_message_take_body (&message, data, size);
2885 if (stream->conninfo.connection)
2886 conninfo = &stream->conninfo;
2888 conninfo = &src->conninfo;
2890 GST_DEBUG_OBJECT (src, "sending %u bytes backchannel RTP", size);
2891 ret = gst_rtspsrc_connection_send (src, conninfo, &message, NULL);
2892 GST_DEBUG_OBJECT (src, "sent backchannel RTP, %d", ret);
2894 /* and steal it away again because we will free it when unreffing the
2896 gst_rtsp_message_steal_body (&message, &data, &size);
2897 gst_rtsp_message_unset (&message);
2899 gst_buffer_unmap (buffer, &map);
2903 g_signal_emit_by_name (stream->rtpsrc, "push-sample", sample, &res);
2904 GST_DEBUG_OBJECT (src, "sent backchannel RTP sample %p: %s", sample,
2905 gst_flow_get_name (res));
2909 gst_sample_unref (sample);
2914 static GstPadProbeReturn
2915 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2917 GstRTSPSrc *src = user_data;
2919 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2920 GST_DEBUG_PAD_NAME (pad));
2922 /* activate the streams */
2923 GST_OBJECT_LOCK (src);
2924 if (!src->need_activate)
2927 src->need_activate = FALSE;
2928 GST_OBJECT_UNLOCK (src);
2930 gst_rtspsrc_activate_streams (src);
2932 return GST_PAD_PROBE_OK;
2936 GST_OBJECT_UNLOCK (src);
2937 return GST_PAD_PROBE_OK;
2942 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
2944 GstPad *gpad = GST_PAD_CAST (user_data);
2946 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
2947 gst_pad_store_sticky_event (gpad, *event);
2953 add_backchannel_fakesink (GstRTSPSrc * src, GstRTSPStream * stream,
2957 GstElement *fakesink;
2959 fakesink = gst_element_factory_make ("fakesink", NULL);
2960 if (fakesink == NULL) {
2961 GST_ERROR_OBJECT (src, "no fakesink");
2965 sinkpad = gst_element_get_static_pad (fakesink, "sink");
2967 GST_DEBUG_OBJECT (src, "backchannel stream %p, hooking fakesink", stream);
2969 gst_bin_add (GST_BIN_CAST (src), fakesink);
2970 if (gst_pad_link (srcpad, sinkpad) != GST_PAD_LINK_OK) {
2971 GST_WARNING_OBJECT (src, "could not link to fakesink");
2975 gst_object_unref (sinkpad);
2977 gst_element_sync_state_with_parent (fakesink);
2981 /* this callback is called when the session manager generated a new src pad with
2982 * payloaded RTP packets. We simply ghost the pad here. */
2984 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2987 GstPadTemplate *template;
2990 GstRTSPStream *stream;
2992 GstPad *internal_src;
2994 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2996 GST_RTSP_STATE_LOCK (src);
2998 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2999 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
3000 goto unknown_stream;
3002 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
3004 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
3006 goto unknown_stream;
3009 stream->ssrc = ssrc;
3011 /* we'll add it later see below */
3012 stream->added = TRUE;
3014 /* check if we added all streams */
3016 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
3017 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
3019 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
3020 ostream, ostream->container, ostream->added, ostream->setup);
3022 /* if we find a stream for which we did a setup that is not added, we
3023 * need to wait some more */
3024 if (ostream->setup && !ostream->added) {
3029 GST_RTSP_STATE_UNLOCK (src);
3031 /* create a new pad we will use to stream to */
3032 template = gst_static_pad_template_get (&rtptemplate);
3033 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
3034 gst_object_unref (template);
3037 /* We intercept and modify the stream start event */
3039 GST_PAD (gst_proxy_pad_get_internal (GST_PROXY_PAD (stream->srcpad)));
3040 gst_pad_set_element_private (internal_src, stream);
3041 gst_pad_set_event_function (internal_src, gst_rtspsrc_handle_src_sink_event);
3042 gst_object_unref (internal_src);
3044 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3045 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3046 gst_pad_set_active (stream->srcpad, TRUE);
3047 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
3049 /* don't add the srcpad if this is a sendonly stream */
3050 if (stream->is_backchannel)
3051 add_backchannel_fakesink (src, stream, stream->srcpad);
3053 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3056 GST_DEBUG_OBJECT (src, "We added all streams");
3057 /* when we get here, all stream are added and we can fire the no-more-pads
3059 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
3067 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
3068 GST_RTSP_STATE_UNLOCK (src);
3075 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
3079 len = stream->ptmap->len;
3080 for (i = 0; i < len; i++) {
3081 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3089 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
3091 GstRTSPStream *stream;
3094 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
3096 GST_RTSP_STATE_LOCK (src);
3097 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3099 goto unknown_stream;
3101 if ((caps = stream_get_caps_for_pt (stream, pt)))
3102 gst_caps_ref (caps);
3103 GST_RTSP_STATE_UNLOCK (src);
3109 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
3110 GST_RTSP_STATE_UNLOCK (src);
3116 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
3118 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
3124 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
3130 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
3136 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
3138 GstRTSPSrc *src = stream->parent;
3141 g_object_get (source, "ssrc", &ssrc, NULL);
3143 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
3144 ssrc, stream->ssrc, stream->id);
3146 if (ssrc == stream->ssrc)
3147 gst_rtspsrc_do_stream_eos (src, stream);
3151 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
3153 GstRTSPSrc *src = stream->parent;
3156 g_object_get (source, "ssrc", &ssrc, NULL);
3158 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
3159 ssrc, stream->ssrc, stream->id);
3161 if (ssrc == stream->ssrc)
3162 gst_rtspsrc_do_stream_eos (src, stream);
3166 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
3168 GstRTSPStream *stream;
3170 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
3172 /* get stream for session */
3173 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3175 gst_rtspsrc_do_stream_eos (src, stream);
3180 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
3182 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
3187 set_manager_buffer_mode (GstRTSPSrc * src)
3189 GObjectClass *klass;
3191 if (src->manager == NULL)
3194 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3196 if (!g_object_class_find_property (klass, "buffer-mode"))
3199 if (src->buffer_mode != BUFFER_MODE_AUTO) {
3200 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
3205 GST_DEBUG_OBJECT (src,
3206 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
3208 if (src->provided_clock) {
3209 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
3211 if (clock == src->provided_clock) {
3212 GST_DEBUG_OBJECT (src, "selected synced");
3213 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
3216 gst_object_unref (clock);
3221 /* Otherwise fall-through and use another buffer mode */
3223 gst_object_unref (clock);
3226 GST_DEBUG_OBJECT (src, "auto buffering mode");
3227 if (src->use_buffering) {
3228 GST_DEBUG_OBJECT (src, "selected buffer");
3229 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
3231 GST_DEBUG_OBJECT (src, "selected slave");
3232 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
3237 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
3241 GstMIKEYMessage *msg = stream->mikey;
3243 GST_DEBUG ("request key SSRC %u", ssrc);
3245 caps = gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
3246 caps = gst_caps_make_writable (caps);
3248 /* parse crypto sessions and look for the SSRC rollover counter */
3249 msg = stream->mikey;
3250 for (i = 0; msg && i < gst_mikey_message_get_n_cs (msg); i++) {
3251 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
3253 if (ssrc == map->ssrc) {
3254 gst_caps_set_simple (caps, "roc", G_TYPE_UINT, map->roc, NULL);
3263 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
3265 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3266 if (stream->id != session)
3269 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3270 stream->profile != GST_RTSP_PROFILE_SAVPF)
3273 if (stream->srtpdec == NULL) {
3276 name = g_strdup_printf ("srtpdec_%u", session);
3277 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
3280 if (stream->srtpdec == NULL) {
3281 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
3282 ("no srtpdec element present!"));
3285 g_signal_connect (stream->srtpdec, "request-key",
3286 (GCallback) request_key, stream);
3288 return gst_object_ref (stream->srtpdec);
3292 request_rtcp_encoder (GstElement * rtpbin, guint session,
3293 GstRTSPStream * stream)
3298 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3299 if (stream->id != session)
3302 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3303 stream->profile != GST_RTSP_PROFILE_SAVPF)
3306 if (stream->srtpenc == NULL) {
3309 name = g_strdup_printf ("srtpenc_%u", session);
3310 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
3313 if (stream->srtpenc == NULL) {
3314 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
3315 ("no srtpenc element present!"));
3319 /* get RTCP crypto parameters from caps */
3320 s = gst_caps_get_structure (stream->srtcpparams, 0);
3324 GType ciphertype, authtype;
3325 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
3327 ciphertype = g_type_from_name ("GstSrtpCipherType");
3328 authtype = g_type_from_name ("GstSrtpAuthType");
3329 g_value_init (&rtcp_cipher, ciphertype);
3330 g_value_init (&rtcp_auth, authtype);
3332 str = gst_structure_get_string (s, "srtcp-cipher");
3333 gst_value_deserialize (&rtcp_cipher, str);
3334 str = gst_structure_get_string (s, "srtcp-auth");
3335 gst_value_deserialize (&rtcp_auth, str);
3336 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
3338 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-cipher",
3340 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-auth",
3342 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
3344 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
3346 g_object_set (stream->srtpenc, "key", buf, NULL);
3348 g_value_unset (&rtcp_cipher);
3349 g_value_unset (&rtcp_auth);
3350 gst_buffer_unref (buf);
3353 name = g_strdup_printf ("rtcp_sink_%d", session);
3354 pad = gst_element_get_request_pad (stream->srtpenc, name);
3356 gst_object_unref (pad);
3358 return gst_object_ref (stream->srtpenc);
3362 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
3364 GstElement *rtx, *bin;
3367 GstRTSPStream *stream;
3369 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
3371 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
3375 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
3376 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
3377 bin = gst_bin_new (NULL);
3378 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
3379 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
3380 gst_bin_add (GST_BIN (bin), rtx);
3382 pad = gst_element_get_static_pad (rtx, "src");
3383 name = g_strdup_printf ("src_%u", sessid);
3384 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3386 gst_object_unref (pad);
3388 pad = gst_element_get_static_pad (rtx, "sink");
3389 name = g_strdup_printf ("sink_%u", sessid);
3390 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3392 gst_object_unref (pad);
3398 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
3402 gboolean do_retransmission = FALSE;
3404 if (transport->trans != GST_RTSP_TRANS_RTP)
3406 if (transport->profile != GST_RTSP_PROFILE_AVPF &&
3407 transport->profile != GST_RTSP_PROFILE_SAVPF)
3410 signal_id = g_signal_lookup ("request-aux-receiver",
3411 G_OBJECT_TYPE (src->manager));
3412 /* there's already something connected */
3413 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
3414 NULL, NULL, NULL) != 0) {
3415 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
3416 "\"request-aux-receiver\" signal is "
3417 "already used by the application");
3421 /* build the retransmission payload type map */
3422 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3423 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3424 gboolean do_retransmission_stream = FALSE;
3427 if (stream->rtx_pt_map)
3428 gst_structure_free (stream->rtx_pt_map);
3429 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
3431 for (i = 0; i < stream->ptmap->len; i++) {
3432 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3433 GstStructure *s = gst_caps_get_structure (item->caps, 0);
3434 const gchar *encoding;
3436 /* we only care about RTX streams */
3437 if ((encoding = gst_structure_get_string (s, "encoding-name"))
3438 && g_strcmp0 (encoding, "RTX") == 0) {
3439 const gchar *stream_pt_s;
3442 if (gst_structure_get_int (s, "payload", &rtx_pt)
3443 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
3446 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
3448 do_retransmission_stream = TRUE;
3454 if (do_retransmission_stream) {
3455 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
3456 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
3457 do_retransmission = TRUE;
3459 GST_DEBUG_OBJECT (src, "no retransmission payload map for stream "
3460 "id %i", stream->id);
3461 gst_structure_free (stream->rtx_pt_map);
3462 stream->rtx_pt_map = NULL;
3466 if (do_retransmission) {
3467 GST_DEBUG_OBJECT (src, "Enabling retransmissions");
3469 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
3471 /* enable RFC4588 retransmission handling by setting rtprtxreceive
3472 * as the "aux" element of rtpbin */
3473 g_signal_connect (src->manager, "request-aux-receiver",
3474 (GCallback) request_aux_receiver, src);
3476 GST_DEBUG_OBJECT (src,
3477 "Not enabling retransmissions as no stream had a retransmission payload map");
3481 /* try to get and configure a manager */
3483 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3484 GstRTSPTransport * transport)
3486 const gchar *manager;
3488 GstStateChangeReturn ret;
3490 /* find a manager */
3491 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3495 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3497 /* configure the manager */
3498 if (src->manager == NULL) {
3499 GObjectClass *klass;
3501 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3503 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3507 goto use_no_manager;
3509 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3510 goto manager_failed;
3513 /* we manage this element */
3514 gst_element_set_locked_state (src->manager, TRUE);
3515 gst_bin_add (GST_BIN_CAST (src), src->manager);
3517 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3518 if (ret == GST_STATE_CHANGE_FAILURE)
3519 goto start_manager_failure;
3521 g_object_set (src->manager, "latency", src->latency, NULL);
3523 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3525 if (g_object_class_find_property (klass, "ntp-sync")) {
3526 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3529 if (g_object_class_find_property (klass, "rfc7273-sync")) {
3530 g_object_set (src->manager, "rfc7273-sync", src->rfc7273_sync, NULL);
3533 if (src->use_pipeline_clock) {
3534 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3535 g_object_set (src->manager, "use-pipeline-clock", TRUE, NULL);
3538 if (g_object_class_find_property (klass, "ntp-time-source")) {
3539 g_object_set (src->manager, "ntp-time-source", src->ntp_time_source,
3544 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3545 g_object_set (src->manager, "sdes", src->sdes, NULL);
3548 if (g_object_class_find_property (klass, "drop-on-latency")) {
3549 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3553 if (g_object_class_find_property (klass, "max-rtcp-rtp-time-diff")) {
3554 g_object_set (src->manager, "max-rtcp-rtp-time-diff",
3555 src->max_rtcp_rtp_time_diff, NULL);
3558 if (g_object_class_find_property (klass, "max-ts-offset-adjustment")) {
3559 g_object_set (src->manager, "max-ts-offset-adjustment",
3560 src->max_ts_offset_adjustment, NULL);
3563 if (g_object_class_find_property (klass, "max-ts-offset")) {
3564 gint64 max_ts_offset;
3566 /* setting max-ts-offset in the manager has side effects so only do it
3567 * if the value differs */
3568 g_object_get (src->manager, "max-ts-offset", &max_ts_offset, NULL);
3569 if (max_ts_offset != src->max_ts_offset) {
3570 g_object_set (src->manager, "max-ts-offset", src->max_ts_offset,
3575 /* buffer mode pauses are handled by adding offsets to buffer times,
3576 * but some depayloaders may have a hard time syncing output times
3577 * with such input times, e.g. container ones, most notably ASF */
3578 /* TODO alternatives are having an event that indicates these shifts,
3579 * or having rtsp extensions provide suggestion on buffer mode */
3580 /* valid duration implies not likely live pipeline,
3581 * so slaving in jitterbuffer does not make much sense
3582 * (and might mess things up due to bursts) */
3583 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3584 src->segment.duration && stream->container) {
3585 src->use_buffering = TRUE;
3587 src->use_buffering = FALSE;
3590 set_manager_buffer_mode (src);
3592 /* connect to signals */
3593 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3595 src->manager_sig_id =
3596 g_signal_connect (src->manager, "pad-added",
3597 (GCallback) new_manager_pad, src);
3598 src->manager_ptmap_id =
3599 g_signal_connect (src->manager, "request-pt-map",
3600 (GCallback) request_pt_map, src);
3602 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3605 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3608 if (src->do_retransmission)
3609 add_retransmission (src, transport);
3611 g_signal_connect (src->manager, "request-rtp-decoder",
3612 (GCallback) request_rtp_decoder, stream);
3613 g_signal_connect (src->manager, "request-rtcp-decoder",
3614 (GCallback) request_rtp_decoder, stream);
3615 g_signal_connect (src->manager, "request-rtcp-encoder",
3616 (GCallback) request_rtcp_encoder, stream);
3618 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3619 * into a separate RTP session. */
3620 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3621 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3623 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3624 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3627 /* now configure the bandwidth in the manager */
3628 if (g_signal_lookup ("get-internal-session",
3629 G_OBJECT_TYPE (src->manager)) != 0) {
3630 GObject *rtpsession;
3632 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3635 GstRTPProfile rtp_profile;
3637 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3639 stream->session = rtpsession;
3641 if (stream->as_bandwidth != -1) {
3642 GST_INFO_OBJECT (src, "setting AS: %f",
3643 (gdouble) (stream->as_bandwidth * 1000));
3644 g_object_set (rtpsession, "bandwidth",
3645 (gdouble) (stream->as_bandwidth * 1000), NULL);
3647 if (stream->rr_bandwidth != -1) {
3648 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3649 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3652 if (stream->rs_bandwidth != -1) {
3653 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3654 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3658 switch (stream->profile) {
3659 case GST_RTSP_PROFILE_AVPF:
3660 rtp_profile = GST_RTP_PROFILE_AVPF;
3662 case GST_RTSP_PROFILE_SAVP:
3663 rtp_profile = GST_RTP_PROFILE_SAVP;
3665 case GST_RTSP_PROFILE_SAVPF:
3666 rtp_profile = GST_RTP_PROFILE_SAVPF;
3668 case GST_RTSP_PROFILE_AVP:
3670 rtp_profile = GST_RTP_PROFILE_AVP;
3674 g_object_set (rtpsession, "rtp-profile", rtp_profile, NULL);
3676 g_object_set (rtpsession, "probation", src->probation, NULL);
3678 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3680 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3682 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3684 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3686 g_signal_connect (rtpsession, "on-ssrc-active",
3687 (GCallback) on_ssrc_active, stream);
3698 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3703 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3706 start_manager_failure:
3708 GST_DEBUG_OBJECT (src, "could not start session manager");
3713 /* free the UDP sources allocated when negotiating a transport.
3714 * This function is called when the server negotiated to a transport where the
3715 * UDP sources are not needed anymore, such as TCP or multicast. */
3717 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3721 for (i = 0; i < 2; i++) {
3722 if (stream->udpsrc[i]) {
3723 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3724 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3725 gst_object_unref (stream->udpsrc[i]);
3726 stream->udpsrc[i] = NULL;
3731 /* for TCP, create pads to send and receive data to and from the manager and to
3732 * intercept various events and queries
3735 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3736 GstRTSPTransport * transport, GstPad ** outpad)
3739 GstPadTemplate *template;
3740 GstPad *pad0, *pad1;
3742 /* configure for interleaved delivery, nothing needs to be done
3743 * here, the loop function will call the chain functions of the
3744 * session manager. */
3745 stream->channel[0] = transport->interleaved.min;
3746 stream->channel[1] = transport->interleaved.max;
3747 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3748 stream->channel[0], stream->channel[1]);
3750 /* we can remove the allocated UDP ports now */
3751 gst_rtspsrc_stream_free_udp (stream);
3753 /* no session manager, send data to srcpad directly */
3754 if (!stream->channelpad[0]) {
3755 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3757 /* create a new pad we will use to stream to */
3758 name = g_strdup_printf ("stream_%u", stream->id);
3759 template = gst_static_pad_template_get (&rtptemplate);
3760 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3761 gst_object_unref (template);
3764 /* set caps and activate */
3765 gst_pad_use_fixed_caps (stream->channelpad[0]);
3766 gst_pad_set_active (stream->channelpad[0], TRUE);
3768 *outpad = gst_object_ref (stream->channelpad[0]);
3770 GST_DEBUG_OBJECT (src, "using manager source pad");
3772 template = gst_static_pad_template_get (&anysrctemplate);
3774 /* allocate pads for sending the channel data into the manager */
3775 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3776 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3777 gst_object_unref (stream->channelpad[0]);
3778 stream->channelpad[0] = pad0;
3779 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3780 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3781 gst_pad_set_element_private (pad0, src);
3782 gst_pad_set_active (pad0, TRUE);
3784 if (stream->channelpad[1]) {
3785 /* if we have a sinkpad for the other channel, create a pad and link to the
3787 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3788 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3789 gst_pad_link_full (pad1, stream->channelpad[1],
3790 GST_PAD_LINK_CHECK_NOTHING);
3791 gst_object_unref (stream->channelpad[1]);
3792 stream->channelpad[1] = pad1;
3793 gst_pad_set_active (pad1, TRUE);
3795 gst_object_unref (template);
3797 /* setup RTCP transport back to the server if we have to. */
3798 if (src->manager && src->do_rtcp) {
3801 template = gst_static_pad_template_get (&anysinktemplate);
3803 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3804 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3805 gst_pad_set_element_private (stream->rtcppad, stream);
3806 gst_pad_set_active (stream->rtcppad, TRUE);
3808 /* get session RTCP pad */
3809 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3810 pad = gst_element_get_request_pad (src->manager, name);
3815 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3816 gst_object_unref (pad);
3819 gst_object_unref (template);
3825 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3826 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3827 gint * max, guint * ttl)
3829 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3831 if (!(*destination = transport->destination))
3832 *destination = stream->destination;
3835 /* transport first */
3836 *min = transport->port.min;
3837 *max = transport->port.max;
3838 if (*min == -1 && *max == -1) {
3839 /* then try from SDP */
3840 if (stream->port != 0) {
3841 *min = stream->port;
3842 *max = stream->port + 1;
3848 if (!(*ttl = transport->ttl))
3853 /* first take the source, then the endpoint to figure out where to send
3855 if (!(*destination = transport->source)) {
3856 if (src->conninfo.connection)
3857 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3858 else if (stream->conninfo.connection)
3860 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3864 /* for unicast we only expect the ports here */
3865 *min = transport->server_port.min;
3866 *max = transport->server_port.max;
3871 /* For multicast create UDP sources and join the multicast group. */
3873 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3874 GstRTSPTransport * transport, GstPad ** outpad)
3877 const gchar *destination;
3880 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3882 /* we can remove the allocated UDP ports now */
3883 gst_rtspsrc_stream_free_udp (stream);
3885 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3888 /* we need a destination now */
3889 if (destination == NULL)
3890 goto no_destination;
3892 /* we really need ports now or we won't be able to receive anything at all */
3893 if (min == -1 && max == -1)
3896 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3897 destination, min, max);
3899 /* creating UDP source for RTP */
3901 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3903 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3905 if (stream->udpsrc[0] == NULL)
3908 /* take ownership */
3909 gst_object_ref_sink (stream->udpsrc[0]);
3911 if (src->udp_buffer_size != 0)
3912 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3913 src->udp_buffer_size, NULL);
3915 if (src->multi_iface != NULL)
3916 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3917 src->multi_iface, NULL);
3920 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3921 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3924 /* creating another UDP source for RTCP */
3928 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3930 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3932 if (stream->udpsrc[1] == NULL)
3935 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3936 stream->profile == GST_RTSP_PROFILE_SAVPF)
3937 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3939 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3940 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3941 gst_caps_unref (caps);
3943 /* take ownership */
3944 gst_object_ref_sink (stream->udpsrc[1]);
3946 if (src->multi_iface != NULL)
3947 g_object_set (G_OBJECT (stream->udpsrc[1]), "multicast-iface",
3948 src->multi_iface, NULL);
3950 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
3957 GST_DEBUG_OBJECT (src, "no UDP source element found");
3962 GST_DEBUG_OBJECT (src, "no destination found");
3967 GST_DEBUG_OBJECT (src, "no ports found");
3972 /* configure the remainder of the UDP ports */
3974 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3975 GstRTSPTransport * transport, GstPad ** outpad)
3977 /* we manage the UDP elements now. For unicast, the UDP sources where
3978 * allocated in the stream when we suggested a transport. */
3979 if (stream->udpsrc[0]) {
3982 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3983 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3985 GST_DEBUG_OBJECT (src, "setting up UDP source");
3987 /* configure a timeout on the UDP port. When the timeout message is
3988 * posted, we assume UDP transport is not possible. We reconnect using TCP
3990 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3991 src->udp_timeout * 1000, NULL);
3993 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
3994 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3996 /* get output pad of the UDP source. */
3997 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3999 /* save it so we can unblock */
4000 stream->blockedpad = *outpad;
4002 /* configure pad block on the pad. As soon as there is dataflow on the
4003 * UDP source, we know that UDP is not blocked by a firewall and we can
4004 * configure all the streams to let the application autoplug decoders. */
4006 gst_pad_add_probe (stream->blockedpad,
4007 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
4008 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
4010 if (stream->channelpad[0]) {
4011 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
4012 /* configure for UDP delivery, we need to connect the UDP pads to
4013 * the session plugin. */
4014 gst_pad_link_full (*outpad, stream->channelpad[0],
4015 GST_PAD_LINK_CHECK_NOTHING);
4016 gst_object_unref (*outpad);
4018 /* we connected to pad-added signal to get pads from the manager */
4020 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
4025 if (stream->udpsrc[1]) {
4028 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
4029 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
4031 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
4032 stream->profile == GST_RTSP_PROFILE_SAVPF)
4033 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4035 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4036 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
4037 gst_caps_unref (caps);
4039 if (stream->channelpad[1]) {
4042 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
4044 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
4045 gst_pad_link_full (pad, stream->channelpad[1],
4046 GST_PAD_LINK_CHECK_NOTHING);
4047 gst_object_unref (pad);
4049 /* leave unlinked */
4055 /* configure the UDP sink back to the server for status reports */
4057 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
4058 GstRTSPStream * stream, GstRTSPTransport * transport)
4061 gint rtp_port, rtcp_port;
4062 gboolean do_rtp, do_rtcp;
4063 const gchar *destination;
4068 /* get transport info */
4069 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
4070 &rtp_port, &rtcp_port, &ttl);
4072 /* see what we need to do */
4073 do_rtp = (rtp_port != -1);
4074 /* it's possible that the server does not want us to send RTCP in which case
4076 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
4078 /* we need a destination when we have RTP or RTCP ports */
4079 if (destination == NULL && (do_rtp || do_rtcp))
4080 goto no_destination;
4082 /* try to construct the fakesrc to the RTP port of the server to open up any
4083 * NAT firewalls or, if backchannel, construct an appsrc */
4085 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
4088 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
4089 stream->udpsink[0] =
4090 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
4092 if (stream->udpsink[0] == NULL)
4093 goto no_sink_element;
4095 /* don't join multicast group, we will have the source socket do that */
4096 /* no sync or async state changes needed */
4097 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
4098 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
4100 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4102 if (stream->udpsrc[0]) {
4103 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
4104 * so that NAT firewalls will open a hole for us */
4105 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
4109 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
4110 /* configure socket and make sure udpsink does not close it when shutting
4111 * down, it belongs to udpsrc after all. */
4112 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
4113 "close-socket", FALSE, NULL);
4114 g_object_unref (socket);
4117 if (stream->is_backchannel) {
4118 /* appsrc is for the app to shovel data using push-backchannel-buffer */
4119 stream->rtpsrc = gst_element_factory_make ("appsrc", NULL);
4120 if (stream->rtpsrc == NULL)
4121 goto no_appsrc_element;
4123 /* interal use only, don't emit signals */
4124 g_object_set (G_OBJECT (stream->rtpsrc), "emit-signals", TRUE,
4125 "is-live", TRUE, NULL);
4127 /* the source for the dummy packets to open up NAT */
4128 stream->rtpsrc = gst_element_factory_make ("fakesrc", NULL);
4129 if (stream->rtpsrc == NULL)
4130 goto no_fakesrc_element;
4132 /* random data in 5 buffers, a size of 200 bytes should be fine */
4133 g_object_set (G_OBJECT (stream->rtpsrc), "filltype", 3, "num-buffers", 5,
4134 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
4137 /* keep everything locked */
4138 gst_element_set_locked_state (stream->udpsink[0], TRUE);
4139 gst_element_set_locked_state (stream->rtpsrc, TRUE);
4141 gst_object_ref (stream->udpsink[0]);
4142 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
4143 gst_object_ref (stream->rtpsrc);
4144 gst_bin_add (GST_BIN_CAST (src), stream->rtpsrc);
4146 gst_element_link_pads_full (stream->rtpsrc, "src", stream->udpsink[0],
4147 "sink", GST_PAD_LINK_CHECK_NOTHING);
4150 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
4153 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
4154 stream->udpsink[1] =
4155 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
4157 if (stream->udpsink[1] == NULL)
4158 goto no_sink_element;
4160 /* don't join multicast group, we will have the source socket do that */
4161 /* no sync or async state changes needed */
4162 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
4163 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
4165 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4167 if (stream->udpsrc[1]) {
4168 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
4169 * because some servers check the port number of where it sends RTCP to identify
4170 * the RTCP packets it receives */
4171 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
4175 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
4176 /* configure socket and make sure udpsink does not close it when shutting
4177 * down, it belongs to udpsrc after all. */
4178 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
4179 "close-socket", FALSE, NULL);
4180 g_object_unref (socket);
4183 /* we keep this playing always */
4184 gst_element_set_locked_state (stream->udpsink[1], TRUE);
4185 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
4187 gst_object_ref (stream->udpsink[1]);
4188 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
4190 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
4192 /* get session RTCP pad */
4193 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
4194 pad = gst_element_get_request_pad (src->manager, name);
4199 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
4200 gst_object_unref (pad);
4209 GST_ERROR_OBJECT (src, "no destination address specified");
4214 GST_ERROR_OBJECT (src, "no UDP sink element found");
4219 GST_ERROR_OBJECT (src, "no appsrc element found");
4224 GST_ERROR_OBJECT (src, "no fakesrc element found");
4229 GST_ERROR_OBJECT (src, "failed to create socket");
4234 /* sets up all elements needed for streaming over the specified transport.
4235 * Does not yet expose the element pads, this will be done when there is actuall
4236 * dataflow detected, which might never happen when UDP is blocked in a
4237 * firewall, for example.
4240 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
4241 GstRTSPTransport * transport)
4244 GstPad *outpad = NULL;
4245 GstPadTemplate *template;
4247 const gchar *media_type;
4250 src = stream->parent;
4252 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
4254 /* get the proper media type for this stream now */
4255 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
4256 goto unknown_transport;
4258 goto unknown_transport;
4260 /* configure the final media type */
4261 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
4263 len = stream->ptmap->len;
4264 for (i = 0; i < len; i++) {
4266 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
4268 if (item->caps == NULL)
4271 s = gst_caps_get_structure (item->caps, 0);
4272 gst_structure_set_name (s, media_type);
4273 /* set ssrc if known */
4274 if (transport->ssrc)
4275 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
4278 /* try to get and configure a manager, channelpad[0-1] will be configured with
4279 * the pads for the manager, or NULL when no manager is needed. */
4280 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
4283 switch (transport->lower_transport) {
4284 case GST_RTSP_LOWER_TRANS_TCP:
4285 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
4286 goto transport_failed;
4288 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
4289 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
4290 goto transport_failed;
4291 /* fallthrough, the rest is the same for UDP and MCAST */
4292 case GST_RTSP_LOWER_TRANS_UDP:
4293 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
4294 goto transport_failed;
4295 /* configure udpsinks back to the server for RTCP messages, for the
4296 * dummy RTP messages to open NAT, and for the backchannel */
4297 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
4298 goto transport_failed;
4301 goto unknown_transport;
4304 /* using backchannel and no manager, hence no srcpad for this stream */
4305 if (outpad && stream->is_backchannel) {
4306 add_backchannel_fakesink (src, stream, outpad);
4307 gst_object_unref (outpad);
4308 } else if (outpad) {
4309 GST_DEBUG_OBJECT (src, "creating ghostpad for stream %p", stream);
4311 gst_pad_use_fixed_caps (outpad);
4313 /* create ghostpad, don't add just yet, this will be done when we activate
4315 name = g_strdup_printf ("stream_%u", stream->id);
4316 template = gst_static_pad_template_get (&rtptemplate);
4317 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
4318 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
4319 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
4320 gst_object_unref (template);
4323 gst_object_unref (outpad);
4325 /* mark pad as ok */
4326 stream->last_ret = GST_FLOW_OK;
4333 GST_WARNING_OBJECT (src, "failed to configure transport");
4338 GST_WARNING_OBJECT (src, "unknown transport");
4343 GST_WARNING_OBJECT (src, "cannot get a session manager");
4348 /* send a couple of dummy random packets on the receiver RTP port to the server,
4349 * this should make a firewall think we initiated the data transfer and
4350 * hopefully allow packets to go from the sender port to our RTP receiver port */
4352 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
4356 if (src->nat_method != GST_RTSP_NAT_DUMMY)
4359 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4360 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4362 if (!stream->rtpsrc || !stream->udpsink[0])
4365 if (stream->is_backchannel)
4366 GST_DEBUG_OBJECT (src, "starting backchannel stream %p", stream);
4368 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
4370 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
4371 gst_element_set_state (stream->rtpsrc, GST_STATE_NULL);
4372 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
4373 gst_element_set_state (stream->rtpsrc, GST_STATE_PLAYING);
4378 /* Adds the source pads of all configured streams to the element.
4379 * This code is performed when we detected dataflow.
4381 * We detect dataflow from either the _loop function or with pad probes on the
4385 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
4389 GST_DEBUG_OBJECT (src, "activating streams");
4391 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4392 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4394 if (stream->udpsrc[0]) {
4395 /* remove timeout, we are streaming now and timeouts will be handled by
4396 * the session manager and jitter buffer */
4397 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
4399 if (stream->srcpad) {
4400 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
4401 gst_pad_set_active (stream->srcpad, TRUE);
4403 /* if we don't have a session manager, set the caps now. If we have a
4404 * session, we will get a notification of the pad and the caps. */
4405 if (!src->manager) {
4408 caps = stream_get_caps_for_pt (stream, stream->default_pt);
4409 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
4410 gst_pad_set_caps (stream->srcpad, caps);
4413 if (!stream->added) {
4414 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
4415 if (stream->is_backchannel)
4416 add_backchannel_fakesink (src, stream, stream->srcpad);
4418 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
4419 stream->added = TRUE;
4424 /* unblock all pads */
4425 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4426 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4428 if (stream->blockid) {
4429 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
4430 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
4431 stream->blockid = 0;
4439 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
4440 gboolean reset_manager)
4443 guint64 start, stop;
4444 gdouble play_speed, play_scale;
4446 GST_DEBUG_OBJECT (src, "configuring stream caps");
4448 start = segment->position;
4449 stop = segment->duration;
4450 play_speed = segment->rate;
4451 play_scale = segment->applied_rate;
4453 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4454 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4460 len = stream->ptmap->len;
4461 for (j = 0; j < len; j++) {
4463 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
4465 if (item->caps == NULL)
4468 caps = gst_caps_make_writable (item->caps);
4470 if (stream->timebase != -1)
4471 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
4472 (guint) stream->timebase, NULL);
4473 if (stream->seqbase != -1)
4474 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
4475 (guint) stream->seqbase, NULL);
4476 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
4478 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
4479 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
4480 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
4483 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
4486 if (item->pt == stream->default_pt) {
4487 if (stream->udpsrc[0])
4488 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4489 stream->need_caps = TRUE;
4493 if (reset_manager && src->manager) {
4494 GST_DEBUG_OBJECT (src, "clear session");
4495 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
4499 static GstFlowReturn
4500 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
4505 /* store the value */
4506 stream->last_ret = ret;
4508 /* if it's success we can return the value right away */
4509 if (ret == GST_FLOW_OK)
4512 /* any other error that is not-linked can be returned right
4514 if (ret != GST_FLOW_NOT_LINKED)
4517 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4518 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4519 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4521 ret = ostream->last_ret;
4522 /* some other return value (must be SUCCESS but we can return
4523 * other values as well) */
4524 if (ret != GST_FLOW_NOT_LINKED)
4527 /* if we get here, all other pads were unlinked and we return
4528 * NOT_LINKED then */
4534 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4537 gboolean res = TRUE;
4539 /* only streams that have a connection to the outside world */
4543 if (stream->udpsrc[0]) {
4544 gst_event_ref (event);
4545 res = gst_element_send_event (stream->udpsrc[0], event);
4546 } else if (stream->channelpad[0]) {
4547 gst_event_ref (event);
4548 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4549 res = gst_pad_push_event (stream->channelpad[0], event);
4551 res = gst_pad_send_event (stream->channelpad[0], event);
4554 if (stream->udpsrc[1]) {
4555 gst_event_ref (event);
4556 res &= gst_element_send_event (stream->udpsrc[1], event);
4557 } else if (stream->channelpad[1]) {
4558 gst_event_ref (event);
4559 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4560 res &= gst_pad_push_event (stream->channelpad[1], event);
4562 res &= gst_pad_send_event (stream->channelpad[1], event);
4566 gst_event_unref (event);
4572 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4575 gboolean res = TRUE;
4577 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4578 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4580 gst_event_ref (event);
4581 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4583 gst_event_unref (event);
4589 accept_certificate_cb (GTlsConnection * conn, GTlsCertificate * peer_cert,
4590 GTlsCertificateFlags errors, gpointer user_data)
4592 GstRTSPSrc *src = user_data;
4593 gboolean accept = FALSE;
4595 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ACCEPT_CERTIFICATE], 0, conn,
4596 peer_cert, errors, &accept);
4601 static GstRTSPResult
4602 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4606 GstRTSPMessage response;
4607 gboolean retry = FALSE;
4608 memset (&response, 0, sizeof (response));
4609 gst_rtsp_message_init (&response);
4611 if (info->connection == NULL) {
4612 if (info->url == NULL) {
4613 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4614 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4617 /* create connection */
4618 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4619 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4620 goto could_not_create;
4623 gst_rtspsrc_setup_auth (src, &response);
4626 g_free (info->url_str);
4627 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4629 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4631 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4632 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4633 src->tls_validation_flags))
4634 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4636 if (src->tls_database)
4637 gst_rtsp_connection_set_tls_database (info->connection,
4640 if (src->tls_interaction)
4641 gst_rtsp_connection_set_tls_interaction (info->connection,
4642 src->tls_interaction);
4643 gst_rtsp_connection_set_accept_certificate_func (info->connection,
4644 accept_certificate_cb, src, NULL);
4647 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4648 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4650 if (src->proxy_host) {
4651 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4653 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4658 if (!info->connected) {
4661 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4662 ("Connecting to %s", info->location));
4663 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4664 res = gst_rtsp_connection_connect_with_response (info->connection,
4665 src->ptcp_timeout, &response);
4667 if (response.type == GST_RTSP_MESSAGE_HTTP_RESPONSE &&
4668 response.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4669 gst_rtsp_conninfo_close (src, info, TRUE);
4673 retry = FALSE; // we should not retry more than once
4678 if (res == GST_RTSP_OK)
4679 info->connected = TRUE;
4681 goto could_not_connect;
4683 } while (!info->connected && retry);
4685 gst_rtsp_message_unset (&response);
4691 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4692 gst_rtsp_message_unset (&response);
4697 gchar *str = gst_rtsp_strresult (res);
4698 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4700 gst_rtsp_message_unset (&response);
4705 gchar *str = gst_rtsp_strresult (res);
4706 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4708 gst_rtsp_message_unset (&response);
4713 static GstRTSPResult
4714 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4717 GST_RTSP_STATE_LOCK (src);
4718 if (info->connected) {
4719 GST_DEBUG_OBJECT (src, "closing connection...");
4720 gst_rtsp_connection_close (info->connection);
4721 info->connected = FALSE;
4723 if (free && info->connection) {
4724 /* free connection */
4725 GST_DEBUG_OBJECT (src, "freeing connection...");
4726 gst_rtsp_connection_free (info->connection);
4727 info->connection = NULL;
4728 info->flushing = FALSE;
4730 GST_RTSP_STATE_UNLOCK (src);
4734 static GstRTSPResult
4735 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4740 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4741 gst_rtsp_conninfo_close (src, info, FALSE);
4742 res = gst_rtsp_conninfo_connect (src, info, async);
4748 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4752 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4753 GST_RTSP_STATE_LOCK (src);
4754 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4755 GST_DEBUG_OBJECT (src, "connection flush");
4756 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4757 src->conninfo.flushing = flush;
4759 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4760 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4761 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4762 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4763 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4764 stream->conninfo.flushing = flush;
4767 GST_RTSP_STATE_UNLOCK (src);
4770 static GstRTSPResult
4771 gst_rtspsrc_init_request (GstRTSPSrc * src, GstRTSPMessage * msg,
4772 GstRTSPMethod method, const gchar * uri)
4776 res = gst_rtsp_message_init_request (msg, method, uri);
4780 /* set user-agent */
4781 if (src->user_agent)
4782 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, src->user_agent);
4787 /* FIXME, handle server request, reply with OK, for now */
4788 static GstRTSPResult
4789 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
4790 GstRTSPMessage * request)
4792 GstRTSPMessage response = { 0 };
4795 GST_DEBUG_OBJECT (src, "got server request message");
4797 DEBUG_RTSP (src, request);
4799 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4801 if (res == GST_RTSP_ENOTIMPL) {
4802 /* default implementation, send OK */
4803 GST_DEBUG_OBJECT (src, "prepare OK reply");
4805 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4810 /* let app parse and reply */
4811 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4812 0, request, &response);
4814 DEBUG_RTSP (src, &response);
4816 res = gst_rtspsrc_connection_send (src, conninfo, &response, NULL);
4820 gst_rtsp_message_unset (&response);
4821 } else if (res == GST_RTSP_EEOF)
4829 gst_rtsp_message_unset (&response);
4834 /* send server keep-alive */
4835 static GstRTSPResult
4836 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4838 GstRTSPMessage request = { 0 };
4840 GstRTSPMethod method;
4841 const gchar *control;
4843 if (src->do_rtsp_keep_alive == FALSE) {
4844 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4845 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4849 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4851 /* find a method to use for keep-alive */
4852 if (src->methods & GST_RTSP_GET_PARAMETER)
4853 method = GST_RTSP_GET_PARAMETER;
4855 method = GST_RTSP_OPTIONS;
4857 control = get_aggregate_control (src);
4858 if (control == NULL)
4861 res = gst_rtspsrc_init_request (src, &request, method, control);
4865 request.type_data.request.version = src->version;
4867 res = gst_rtspsrc_connection_send (src, &src->conninfo, &request, NULL);
4871 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4872 gst_rtsp_message_unset (&request);
4879 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4884 gchar *str = gst_rtsp_strresult (res);
4886 gst_rtsp_message_unset (&request);
4887 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4888 ("Could not send keep-alive. (%s)", str));
4894 static GstFlowReturn
4895 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4897 GstFlowReturn ret = GST_FLOW_OK;
4899 GstRTSPStream *stream;
4900 GstPad *outpad = NULL;
4906 channel = message->type_data.data.channel;
4908 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4910 goto unknown_stream;
4912 if (channel == stream->channel[0]) {
4913 outpad = stream->channelpad[0];
4915 } else if (channel == stream->channel[1]) {
4916 outpad = stream->channelpad[1];
4922 /* take a look at the body to figure out what we have */
4923 gst_rtsp_message_get_body (message, &data, &size);
4925 goto invalid_length;
4927 /* channels are not correct on some servers, do extra check */
4928 if (data[1] >= 200 && data[1] <= 204) {
4929 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4930 outpad = stream->channelpad[1];
4934 /* we have no clue what this is, just ignore then. */
4936 goto unknown_stream;
4938 /* take the message body for further processing */
4939 gst_rtsp_message_steal_body (message, &data, &size);
4941 /* strip the trailing \0 */
4944 buf = gst_buffer_new ();
4945 gst_buffer_append_memory (buf,
4946 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4948 /* don't need message anymore */
4949 gst_rtsp_message_unset (message);
4951 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4954 if (src->need_activate) {
4960 guint group_id = gst_util_group_id_next ();
4962 /* generate an SHA256 sum of the URI */
4963 cs = g_checksum_new (G_CHECKSUM_SHA256);
4964 uri = src->conninfo.location;
4965 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4967 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4968 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4972 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4973 event = gst_event_new_stream_start (stream_id);
4974 gst_event_set_group_id (event, group_id);
4977 gst_rtspsrc_stream_push_event (src, ostream, event);
4979 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
4980 /* only streams that have a connection to the outside world */
4981 if (ostream->setup) {
4982 if (ostream->udpsrc[0]) {
4983 gst_element_send_event (ostream->udpsrc[0],
4984 gst_event_new_caps (caps));
4985 } else if (ostream->channelpad[0]) {
4986 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
4987 gst_pad_push_event (ostream->channelpad[0],
4988 gst_event_new_caps (caps));
4990 gst_pad_send_event (ostream->channelpad[0],
4991 gst_event_new_caps (caps));
4993 ostream->need_caps = FALSE;
4995 if (ostream->profile == GST_RTSP_PROFILE_SAVP ||
4996 ostream->profile == GST_RTSP_PROFILE_SAVPF)
4997 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4999 caps = gst_caps_new_empty_simple ("application/x-rtcp");
5001 if (ostream->udpsrc[1]) {
5002 gst_element_send_event (ostream->udpsrc[1],
5003 gst_event_new_caps (caps));
5004 } else if (ostream->channelpad[1]) {
5005 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
5006 gst_pad_push_event (ostream->channelpad[1],
5007 gst_event_new_caps (caps));
5009 gst_pad_send_event (ostream->channelpad[1],
5010 gst_event_new_caps (caps));
5013 gst_caps_unref (caps);
5017 g_checksum_free (cs);
5019 gst_rtspsrc_activate_streams (src);
5020 src->need_activate = FALSE;
5021 src->need_segment = TRUE;
5024 if (src->base_time == -1) {
5025 /* Take current running_time. This timestamp will be put on
5026 * the first buffer of each stream because we are a live source and so we
5027 * timestamp with the running_time. When we are dealing with TCP, we also
5028 * only timestamp the first buffer (using the DISCONT flag) because a server
5029 * typically bursts data, for which we don't want to compensate by speeding
5030 * up the media. The other timestamps will be interpollated from this one
5031 * using the RTP timestamps. */
5032 GST_OBJECT_LOCK (src);
5033 if (GST_ELEMENT_CLOCK (src)) {
5035 GstClockTime base_time;
5037 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
5038 base_time = GST_ELEMENT_CAST (src)->base_time;
5040 src->base_time = now - base_time;
5042 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
5043 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
5045 GST_OBJECT_UNLOCK (src);
5048 /* If needed send a new segment, don't forget we are live and buffer are
5049 * timestamped with running time */
5050 if (src->need_segment) {
5052 src->need_segment = FALSE;
5053 gst_segment_init (&segment, GST_FORMAT_TIME);
5054 gst_rtspsrc_push_event (src, gst_event_new_segment (&segment));
5057 if (stream->need_caps) {
5060 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt))) {
5061 /* only streams that have a connection to the outside world */
5062 if (stream->setup) {
5063 /* Only need to update the TCP caps here, UDP is already handled */
5064 if (stream->channelpad[0]) {
5065 if (GST_PAD_IS_SRC (stream->channelpad[0]))
5066 gst_pad_push_event (stream->channelpad[0],
5067 gst_event_new_caps (caps));
5069 gst_pad_send_event (stream->channelpad[0],
5070 gst_event_new_caps (caps));
5072 stream->need_caps = FALSE;
5076 stream->need_caps = FALSE;
5079 if (stream->discont && !is_rtcp) {
5080 /* mark first RTP buffer as discont */
5081 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
5082 stream->discont = FALSE;
5083 /* first buffer gets the timestamp, other buffers are not timestamped and
5084 * their presentation time will be interpollated from the rtp timestamps. */
5085 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
5086 GST_TIME_ARGS (src->base_time));
5088 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
5091 /* chain to the peer pad */
5092 if (GST_PAD_IS_SINK (outpad))
5093 ret = gst_pad_chain (outpad, buf);
5095 ret = gst_pad_push (outpad, buf);
5098 /* combine all stream flows for the data transport */
5099 ret = gst_rtspsrc_combine_flows (src, stream, ret);
5106 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
5107 gst_rtsp_message_unset (message);
5112 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5113 ("Short message received, ignoring."));
5114 gst_rtsp_message_unset (message);
5119 static GstFlowReturn
5120 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
5122 GstRTSPMessage message = { 0 };
5124 GstFlowReturn ret = GST_FLOW_OK;
5125 GTimeVal tv_timeout;
5128 /* get the next timeout interval */
5129 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
5131 /* see if the timeout period expired */
5132 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
5133 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
5134 /* send keep-alive, only act on interrupt, a warning will be posted for
5136 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5138 /* get new timeout */
5139 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
5142 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
5143 tv_timeout.tv_sec, tv_timeout.tv_usec);
5145 /* protect the connection with the connection lock so that we can see when
5146 * we are finished doing server communication */
5148 gst_rtspsrc_connection_receive (src, &src->conninfo,
5149 &message, src->ptcp_timeout);
5153 GST_DEBUG_OBJECT (src, "we received a server message");
5155 case GST_RTSP_EINTR:
5156 /* we got interrupted this means we need to stop */
5158 case GST_RTSP_ETIMEOUT:
5159 /* no reply, send keep alive */
5160 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
5161 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5165 /* go EOS when the server closed the connection */
5171 switch (message.type) {
5172 case GST_RTSP_MESSAGE_REQUEST:
5173 /* server sends us a request message, handle it */
5174 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
5175 if (res == GST_RTSP_EEOF)
5178 goto handle_request_failed;
5180 case GST_RTSP_MESSAGE_RESPONSE:
5181 /* we ignore response messages */
5182 GST_DEBUG_OBJECT (src, "ignoring response message");
5183 DEBUG_RTSP (src, &message);
5185 case GST_RTSP_MESSAGE_DATA:
5186 GST_DEBUG_OBJECT (src, "got data message");
5187 ret = gst_rtspsrc_handle_data (src, &message);
5188 if (ret != GST_FLOW_OK)
5189 goto handle_data_failed;
5192 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5197 g_assert_not_reached ();
5202 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5203 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5204 ("The server closed the connection."));
5205 src->conninfo.connected = FALSE;
5206 gst_rtsp_message_unset (&message);
5207 return GST_FLOW_EOS;
5211 gst_rtsp_message_unset (&message);
5212 GST_DEBUG_OBJECT (src, "got interrupted");
5213 return GST_FLOW_FLUSHING;
5217 gchar *str = gst_rtsp_strresult (res);
5219 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5220 ("Could not receive message. (%s)", str));
5223 gst_rtsp_message_unset (&message);
5224 return GST_FLOW_ERROR;
5226 handle_request_failed:
5228 gchar *str = gst_rtsp_strresult (res);
5230 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5231 ("Could not handle server message. (%s)", str));
5233 gst_rtsp_message_unset (&message);
5234 return GST_FLOW_ERROR;
5238 GST_DEBUG_OBJECT (src, "could no handle data message");
5243 static GstFlowReturn
5244 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
5247 GstRTSPMessage message = { 0 };
5251 GTimeVal tv_timeout;
5253 /* get the next timeout interval */
5254 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
5256 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
5257 (gint) tv_timeout.tv_sec);
5259 gst_rtsp_message_unset (&message);
5261 /* we should continue reading the TCP socket because the server might
5262 * send us requests. When the session timeout expires, we need to send a
5263 * keep-alive request to keep the session open. */
5264 res = gst_rtspsrc_connection_receive (src, &src->conninfo,
5265 &message, &tv_timeout);
5269 GST_DEBUG_OBJECT (src, "we received a server message");
5271 case GST_RTSP_EINTR:
5272 /* we got interrupted, see what we have to do */
5274 case GST_RTSP_ETIMEOUT:
5275 /* send keep-alive, ignore the result, a warning will be posted. */
5276 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
5277 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5281 /* server closed the connection. not very fatal for UDP, reconnect and
5282 * see what happens. */
5283 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5284 ("The server closed the connection."));
5285 if (src->udp_reconnect) {
5287 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
5294 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
5296 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5297 ("Unhandled return value %d.", res));
5301 switch (message.type) {
5302 case GST_RTSP_MESSAGE_REQUEST:
5303 /* server sends us a request message, handle it */
5304 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
5305 if (res == GST_RTSP_EEOF)
5308 goto handle_request_failed;
5310 case GST_RTSP_MESSAGE_RESPONSE:
5311 /* we ignore response and data messages */
5312 GST_DEBUG_OBJECT (src, "ignoring response message");
5313 DEBUG_RTSP (src, &message);
5314 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
5315 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
5316 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
5317 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
5318 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5325 case GST_RTSP_MESSAGE_DATA:
5326 /* we ignore response and data messages */
5327 GST_DEBUG_OBJECT (src, "ignoring data message");
5330 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5335 g_assert_not_reached ();
5337 /* we get here when the connection got interrupted */
5340 gst_rtsp_message_unset (&message);
5341 GST_DEBUG_OBJECT (src, "got interrupted");
5342 return GST_FLOW_FLUSHING;
5346 gchar *str = gst_rtsp_strresult (res);
5349 src->conninfo.connected = FALSE;
5350 if (res != GST_RTSP_EINTR) {
5351 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
5352 ("Could not connect to server. (%s)", str));
5354 ret = GST_FLOW_ERROR;
5356 ret = GST_FLOW_FLUSHING;
5362 gchar *str = gst_rtsp_strresult (res);
5364 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5365 ("Could not receive message. (%s)", str));
5367 return GST_FLOW_ERROR;
5369 handle_request_failed:
5371 gchar *str = gst_rtsp_strresult (res);
5374 gst_rtsp_message_unset (&message);
5375 if (res != GST_RTSP_EINTR) {
5376 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5377 ("Could not handle server message. (%s)", str));
5379 ret = GST_FLOW_ERROR;
5381 ret = GST_FLOW_FLUSHING;
5387 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5388 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5389 ("The server closed the connection."));
5390 src->conninfo.connected = FALSE;
5391 gst_rtsp_message_unset (&message);
5392 return GST_FLOW_EOS;
5396 static GstRTSPResult
5397 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
5399 GstRTSPResult res = GST_RTSP_OK;
5402 GST_DEBUG_OBJECT (src, "doing reconnect");
5404 GST_OBJECT_LOCK (src);
5405 /* only restart when the pads were not yet activated, else we were
5406 * streaming over UDP */
5407 restart = src->need_activate;
5408 GST_OBJECT_UNLOCK (src);
5410 /* no need to restart, we're done */
5414 /* we can try only TCP now */
5415 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
5417 /* close and cleanup our state */
5418 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
5421 /* see if we have TCP left to try. Also don't try TCP when we were configured
5423 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
5426 /* We post a warning message now to inform the user
5427 * that nothing happened. It's most likely a firewall thing. */
5428 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5429 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5430 "firewall is blocking it. Retrying using a tcp connection.",
5431 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
5433 /* open new connection using tcp */
5434 if (gst_rtspsrc_open (src, async) < 0)
5437 /* start playback */
5438 if (gst_rtspsrc_play (src, &src->segment, async, NULL) < 0)
5447 src->cur_protocols = 0;
5448 /* no transport possible, post an error and stop */
5449 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5450 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5451 "firewall is blocking it. No other protocols to try.",
5452 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
5453 return GST_RTSP_ERROR;
5457 GST_DEBUG_OBJECT (src, "open failed");
5462 GST_DEBUG_OBJECT (src, "play failed");
5468 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
5472 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
5475 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
5478 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
5481 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
5489 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
5493 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
5496 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
5499 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
5502 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
5510 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
5514 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
5517 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
5520 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
5523 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
5531 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
5535 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
5538 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
5541 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
5544 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
5552 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
5554 if (ret == GST_RTSP_OK)
5555 gst_rtspsrc_loop_complete_cmd (src, cmd);
5556 else if (ret == GST_RTSP_EINTR)
5557 gst_rtspsrc_loop_cancel_cmd (src, cmd);
5559 gst_rtspsrc_loop_error_cmd (src, cmd);
5563 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
5566 gboolean flushed = FALSE;
5568 /* start new request */
5569 gst_rtspsrc_loop_start_cmd (src, cmd);
5571 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
5573 GST_OBJECT_LOCK (src);
5574 old = src->pending_cmd;
5575 if (old == CMD_RECONNECT) {
5576 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
5577 cmd = CMD_RECONNECT;
5578 } else if (old == CMD_CLOSE) {
5579 /* our CMD_CLOSE might have interrutped CMD_LOOP. gst_rtspsrc_loop
5580 * will send a CMD_WAIT which would cancel our pending CMD_CLOSE (if
5581 * still pending). We just avoid it here by making sure CMD_CLOSE is
5582 * still the pending command. */
5583 GST_DEBUG_OBJECT (src, "ignore, we were closing");
5585 } else if (old != CMD_WAIT) {
5586 src->pending_cmd = CMD_WAIT;
5587 GST_OBJECT_UNLOCK (src);
5588 /* cancel previous request */
5589 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
5590 gst_rtspsrc_loop_cancel_cmd (src, old);
5591 GST_OBJECT_LOCK (src);
5593 src->pending_cmd = cmd;
5594 /* interrupt if allowed */
5595 if (src->busy_cmd & mask) {
5596 GST_DEBUG_OBJECT (src, "connection flush busy %s",
5597 cmd_to_string (src->busy_cmd));
5598 gst_rtspsrc_connection_flush (src, TRUE);
5601 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
5602 cmd_to_string (src->busy_cmd));
5605 gst_task_start (src->task);
5606 GST_OBJECT_UNLOCK (src);
5612 gst_rtspsrc_loop (GstRTSPSrc * src)
5616 if (!src->conninfo.connection || !src->conninfo.connected)
5619 if (src->interleaved)
5620 ret = gst_rtspsrc_loop_interleaved (src);
5622 ret = gst_rtspsrc_loop_udp (src);
5624 if (ret != GST_FLOW_OK)
5632 GST_WARNING_OBJECT (src, "we are not connected");
5633 ret = GST_FLOW_FLUSHING;
5638 const gchar *reason = gst_flow_get_name (ret);
5640 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
5641 src->running = FALSE;
5642 if (ret == GST_FLOW_EOS) {
5643 /* perform EOS logic */
5644 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5645 gst_element_post_message (GST_ELEMENT_CAST (src),
5646 gst_message_new_segment_done (GST_OBJECT_CAST (src),
5647 src->segment.format, src->segment.position));
5648 gst_rtspsrc_push_event (src,
5649 gst_event_new_segment_done (src->segment.format,
5650 src->segment.position));
5652 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5654 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
5655 /* for fatal errors we post an error message, post the error before the
5656 * EOS so the app knows about the error first. */
5657 GST_ELEMENT_FLOW_ERROR (src, ret);
5658 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5660 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
5665 #ifndef GST_DISABLE_GST_DEBUG
5666 static const gchar *
5667 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
5671 while (method != 0) {
5688 /* Parse a WWW-Authenticate Response header and determine the
5689 * available authentication methods
5691 * This code should also cope with the fact that each WWW-Authenticate
5692 * header can contain multiple challenge methods + tokens
5694 * At the moment, for Basic auth, we just do a minimal check and don't
5695 * even parse out the realm */
5697 gst_rtspsrc_parse_auth_hdr (GstRTSPMessage * response,
5698 GstRTSPAuthMethod * methods, GstRTSPConnection * conn, gboolean * stale)
5700 GstRTSPAuthCredential **credentials, **credential;
5702 g_return_if_fail (response != NULL);
5703 g_return_if_fail (methods != NULL);
5704 g_return_if_fail (stale != NULL);
5707 gst_rtsp_message_parse_auth_credentials (response,
5708 GST_RTSP_HDR_WWW_AUTHENTICATE);
5712 credential = credentials;
5713 while (*credential) {
5714 if ((*credential)->scheme == GST_RTSP_AUTH_BASIC) {
5715 *methods |= GST_RTSP_AUTH_BASIC;
5716 } else if ((*credential)->scheme == GST_RTSP_AUTH_DIGEST) {
5717 GstRTSPAuthParam **param = (*credential)->params;
5719 *methods |= GST_RTSP_AUTH_DIGEST;
5721 gst_rtsp_connection_clear_auth_params (conn);
5725 if (strcmp ((*param)->name, "stale") == 0
5726 && g_ascii_strcasecmp ((*param)->value, "TRUE") == 0)
5728 gst_rtsp_connection_set_auth_param (conn, (*param)->name,
5737 gst_rtsp_auth_credentials_free (credentials);
5741 * gst_rtspsrc_setup_auth:
5742 * @src: the rtsp source
5744 * Configure a username and password and auth method on the
5745 * connection object based on a response we received from the
5748 * Currently, this requires that a username and password were supplied
5749 * in the uri. In the future, they may be requested on demand by sending
5750 * a message up the bus.
5752 * Returns: TRUE if authentication information could be set up correctly.
5755 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5759 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5760 GstRTSPAuthMethod method;
5761 GstRTSPResult auth_result;
5763 GstRTSPConnection *conn;
5764 gboolean stale = FALSE;
5766 conn = src->conninfo.connection;
5768 /* Identify the available auth methods and see if any are supported */
5769 gst_rtspsrc_parse_auth_hdr (response, &avail_methods, conn, &stale);
5771 if (avail_methods == GST_RTSP_AUTH_NONE)
5772 goto no_auth_available;
5774 /* For digest auth, if the response indicates that the session
5775 * data are stale, we just update them in the connection object and
5776 * return TRUE to retry the request */
5778 src->tried_url_auth = FALSE;
5780 url = gst_rtsp_connection_get_url (conn);
5782 /* Do we have username and password available? */
5783 if (url != NULL && !src->tried_url_auth && url->user != NULL
5784 && url->passwd != NULL) {
5787 src->tried_url_auth = TRUE;
5788 GST_DEBUG_OBJECT (src,
5789 "Attempting authentication using credentials from the URL");
5791 user = src->user_id;
5792 pass = src->user_pw;
5793 GST_DEBUG_OBJECT (src,
5794 "Attempting authentication using credentials from the properties");
5797 /* FIXME: If the url didn't contain username and password or we tried them
5798 * already, request a username and passwd from the application via some kind
5799 * of credentials request message */
5801 /* If we don't have a username and passwd at this point, bail out. */
5802 if (user == NULL || pass == NULL)
5805 /* Try to configure for each available authentication method, strongest to
5807 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5808 /* Check if this method is available on the server */
5809 if ((method & avail_methods) == 0)
5812 /* Pass the credentials to the connection to try on the next request */
5813 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5814 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5815 * ignore it and end up retrying later */
5816 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5817 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5818 gst_rtsp_auth_method_to_string (method));
5823 if (method == GST_RTSP_AUTH_NONE)
5824 goto no_auth_available;
5830 /* Output an error indicating that we couldn't connect because there were
5831 * no supported authentication protocols */
5832 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5833 ("No supported authentication protocol was found"));
5838 /* We don't fire an error message, we just return FALSE and let the
5839 * normal NOT_AUTHORIZED error be propagated */
5844 static GstRTSPResult
5845 gst_rtsp_src_receive_response (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
5846 GstRTSPMessage * response, GstRTSPStatusCode * code)
5848 GstRTSPStatusCode thecode;
5849 gchar *content_base = NULL;
5850 GstRTSPResult res = gst_rtspsrc_connection_receive (src, conninfo,
5851 response, src->ptcp_timeout);
5856 DEBUG_RTSP (src, response);
5858 switch (response->type) {
5859 case GST_RTSP_MESSAGE_REQUEST:
5860 res = gst_rtspsrc_handle_request (src, conninfo, response);
5861 if (res == GST_RTSP_EEOF)
5864 goto handle_request_failed;
5866 /* Not a response, receive next message */
5867 return gst_rtsp_src_receive_response (src, conninfo, response, code);
5868 case GST_RTSP_MESSAGE_RESPONSE:
5869 /* ok, a response is good */
5870 GST_DEBUG_OBJECT (src, "received response message");
5872 case GST_RTSP_MESSAGE_DATA:
5873 /* get next response */
5874 GST_DEBUG_OBJECT (src, "handle data response message");
5875 gst_rtspsrc_handle_data (src, response);
5877 /* Not a response, receive next message */
5878 return gst_rtsp_src_receive_response (src, conninfo, response, code);
5880 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5883 /* Not a response, receive next message */
5884 return gst_rtsp_src_receive_response (src, conninfo, response, code);
5887 thecode = response->type_data.response.code;
5889 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5891 /* if the caller wanted the result code, we store it. */
5895 /* If the request didn't succeed, bail out before doing any more */
5896 if (thecode != GST_RTSP_STS_OK)
5899 /* store new content base if any */
5900 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5903 g_free (src->content_base);
5904 src->content_base = g_strdup (content_base);
5914 return GST_RTSP_EEOF;
5917 gchar *str = gst_rtsp_strresult (res);
5919 if (res != GST_RTSP_EINTR) {
5920 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5921 ("Could not receive message. (%s)", str));
5923 GST_WARNING_OBJECT (src, "receive interrupted");
5931 handle_request_failed:
5933 /* ERROR was posted */
5934 gst_rtsp_message_unset (response);
5939 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5940 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5941 ("The server closed the connection."));
5942 gst_rtsp_message_unset (response);
5948 static GstRTSPResult
5949 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
5950 GstRTSPMessage * request, GstRTSPMessage * response,
5951 GstRTSPStatusCode * code)
5955 gboolean allow_send = TRUE;
5958 if (!src->short_header)
5959 gst_rtsp_ext_list_before_send (src->extensions, request);
5961 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_BEFORE_SEND], 0,
5962 request, &allow_send);
5964 GST_DEBUG_OBJECT (src, "skipping message, disabled by signal");
5968 GST_DEBUG_OBJECT (src, "sending message");
5970 DEBUG_RTSP (src, request);
5972 res = gst_rtspsrc_connection_send (src, conninfo, request, src->ptcp_timeout);
5976 gst_rtsp_connection_reset_timeout (conninfo->connection);
5980 res = gst_rtsp_src_receive_response (src, conninfo, response, code);
5981 if (res == GST_RTSP_EEOF) {
5982 GST_WARNING_OBJECT (src, "server closed connection");
5983 /* only try once after reconnect, then fallthrough and error out */
5984 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5986 /* if reconnect succeeds, try again */
5987 if ((res = gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) == 0)
5991 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5997 gchar *str = gst_rtsp_strresult (res);
5999 if (res != GST_RTSP_EINTR) {
6000 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6001 ("Could not send message. (%s)", str));
6003 GST_WARNING_OBJECT (src, "send interrupted");
6012 * @src: the rtsp source
6013 * @conninfo: the connection information to send on
6014 * @request: must point to a valid request
6015 * @response: must point to an empty #GstRTSPMessage
6016 * @code: an optional code result
6017 * @versions: List of versions to try, setting it back onto the @request message
6018 * if not set, `src->version` will be used as RTSP version.
6020 * send @request and retrieve the response in @response. optionally @code can be
6021 * non-NULL in which case it will contain the status code of the response.
6023 * If This function returns #GST_RTSP_OK, @response will contain a valid response
6024 * message that should be cleaned with gst_rtsp_message_unset() after usage.
6026 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
6027 * @response message) if the response code was not 200 (OK).
6029 * If the attempt results in an authentication failure, then this will attempt
6030 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
6033 * Returns: #GST_RTSP_OK if the processing was successful.
6035 static GstRTSPResult
6036 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
6037 GstRTSPMessage * request, GstRTSPMessage * response,
6038 GstRTSPStatusCode * code, GstRTSPVersion * versions)
6040 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
6041 GstRTSPResult res = GST_RTSP_ERROR;
6044 GstRTSPMethod method = GST_RTSP_INVALID;
6045 gint version_retry = 0;
6051 /* make sure we don't loop forever */
6055 /* save method so we can disable it when the server complains */
6056 method = request->type_data.request.method;
6059 request->type_data.request.version = src->version;
6062 gst_rtspsrc_try_send (src, conninfo, request, response,
6067 case GST_RTSP_STS_UNAUTHORIZED:
6068 case GST_RTSP_STS_NOT_FOUND:
6069 if (gst_rtspsrc_setup_auth (src, response)) {
6070 /* Try the request/response again after configuring the auth info
6075 case GST_RTSP_STS_RTSP_VERSION_NOT_SUPPORTED:
6076 GST_INFO_OBJECT (src, "Version %s not supported by the server",
6077 versions ? gst_rtsp_version_as_text (versions[version_retry]) :
6079 if (versions && versions[version_retry] != GST_RTSP_VERSION_INVALID) {
6080 GST_INFO_OBJECT (src, "Unsupported version %s => trying %s",
6081 gst_rtsp_version_as_text (request->type_data.request.version),
6082 gst_rtsp_version_as_text (versions[version_retry]));
6083 request->type_data.request.version = versions[version_retry];
6092 } while (retry == TRUE);
6094 /* If the user requested the code, let them handle errors, otherwise
6095 * post an error below */
6098 else if (int_code != GST_RTSP_STS_OK)
6099 goto error_response;
6106 GST_DEBUG_OBJECT (src, "got error %d", res);
6111 res = GST_RTSP_ERROR;
6113 switch (response->type_data.response.code) {
6114 case GST_RTSP_STS_NOT_FOUND:
6115 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_FOUND,
6118 case GST_RTSP_STS_UNAUTHORIZED:
6119 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_AUTHORIZED,
6122 case GST_RTSP_STS_MOVED_PERMANENTLY:
6123 case GST_RTSP_STS_MOVE_TEMPORARILY:
6125 gchar *new_location;
6126 GstRTSPLowerTrans transports;
6128 GST_DEBUG_OBJECT (src, "got redirection");
6129 /* if we don't have a Location Header, we must error */
6130 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
6131 &new_location, 0) < 0)
6134 /* When we receive a redirect result, we go back to the INIT state after
6135 * parsing the new URI. The caller should do the needed steps to issue
6136 * a new setup when it detects this state change. */
6137 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
6139 /* save current transports */
6140 if (src->conninfo.url)
6141 transports = src->conninfo.url->transports;
6143 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
6145 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
6147 /* set old transports */
6148 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
6149 src->conninfo.url->transports = transports;
6151 src->need_redirect = TRUE;
6155 case GST_RTSP_STS_NOT_ACCEPTABLE:
6156 case GST_RTSP_STS_NOT_IMPLEMENTED:
6157 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
6158 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
6159 gst_rtsp_method_as_text (method));
6160 src->methods &= ~method;
6164 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, READ,
6168 /* if we return ERROR we should unset the response ourselves */
6169 if (res == GST_RTSP_ERROR)
6170 gst_rtsp_message_unset (response);
6176 static GstRTSPResult
6177 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
6178 GstRTSPMessage * response, GstRTSPSrc * src)
6180 return gst_rtspsrc_send (src, &src->conninfo, request, response, NULL, NULL);
6184 /* parse the response and collect all the supported methods. We need this
6185 * information so that we don't try to send an unsupported request to the
6189 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
6191 GstRTSPHeaderField field;
6195 /* reset supported methods */
6198 /* Try Allow Header first */
6199 field = GST_RTSP_HDR_ALLOW;
6202 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6206 src->methods |= gst_rtsp_options_from_text (respoptions);
6212 field = GST_RTSP_HDR_PUBLIC;
6215 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6219 src->methods |= gst_rtsp_options_from_text (respoptions);
6224 if (src->methods == 0) {
6225 /* neither Allow nor Public are required, assume the server supports
6226 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
6228 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
6229 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
6231 /* always assume PLAY, FIXME, extensions should be able to override
6233 src->methods |= GST_RTSP_PLAY;
6234 /* also assume it will support Range */
6235 src->seekable = G_MAXFLOAT;
6237 /* we need describe and setup */
6238 if (!(src->methods & GST_RTSP_DESCRIBE))
6240 if (!(src->methods & GST_RTSP_SETUP))
6248 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6249 ("Server does not support DESCRIBE."));
6254 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6255 ("Server does not support SETUP."));
6260 /* masks to be kept in sync with the hardcoded protocol order of preference
6262 static const guint protocol_masks[] = {
6263 GST_RTSP_LOWER_TRANS_UDP,
6264 GST_RTSP_LOWER_TRANS_UDP_MCAST,
6265 GST_RTSP_LOWER_TRANS_TCP,
6269 static GstRTSPResult
6270 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
6271 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
6275 gboolean add_udp_str;
6280 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
6285 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
6287 /* extension listed transports, use those */
6288 if (*transports != NULL)
6291 /* it's the default */
6292 add_udp_str = FALSE;
6294 /* the default RTSP transports */
6295 result = g_string_new ("RTP");
6298 case GST_RTSP_PROFILE_AVP:
6299 g_string_append (result, "/AVP");
6301 case GST_RTSP_PROFILE_SAVP:
6302 g_string_append (result, "/SAVP");
6304 case GST_RTSP_PROFILE_AVPF:
6305 g_string_append (result, "/AVPF");
6307 case GST_RTSP_PROFILE_SAVPF:
6308 g_string_append (result, "/SAVPF");
6314 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
6315 GST_DEBUG_OBJECT (src, "adding UDP unicast");
6317 g_string_append (result, "/UDP");
6318 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
6319 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
6320 GST_DEBUG_OBJECT (src, "adding UDP multicast");
6321 /* we don't have to allocate any UDP ports yet, if the selected transport
6322 * turns out to be multicast we can create them and join the multicast
6323 * group indicated in the transport reply */
6325 g_string_append (result, "/UDP");
6326 g_string_append (result, ";multicast");
6327 if (src->next_port_num != 0) {
6328 if (src->client_port_range.max > 0 &&
6329 src->next_port_num >= src->client_port_range.max)
6332 g_string_append_printf (result, ";client_port=%d-%d",
6333 src->next_port_num, src->next_port_num + 1);
6335 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
6336 GST_DEBUG_OBJECT (src, "adding TCP");
6338 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
6340 *transports = g_string_free (result, FALSE);
6342 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
6349 GST_ERROR ("extension gave error %d", res);
6354 GST_ERROR ("no more ports available");
6355 return GST_RTSP_ERROR;
6359 static GstRTSPResult
6360 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
6361 gint orig_rtpport, gint orig_rtcpport)
6364 gint nr_udp, nr_int;
6366 gint rtpport = 0, rtcpport = 0;
6369 src = stream->parent;
6371 /* find number of placeholders first */
6372 if (strstr (*transports, "%%i2"))
6374 else if (strstr (*transports, "%%i1"))
6379 if (strstr (*transports, "%%u2"))
6381 else if (strstr (*transports, "%%u1"))
6386 if (nr_udp == 0 && nr_int == 0)
6390 if (!orig_rtpport || !orig_rtcpport) {
6391 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
6394 rtpport = orig_rtpport;
6395 rtcpport = orig_rtcpport;
6399 str = g_string_new ("");
6401 while ((next = strstr (p, "%%"))) {
6402 g_string_append_len (str, p, next - p);
6403 if (next[2] == 'u') {
6405 g_string_append_printf (str, "%d", rtpport);
6406 else if (next[3] == '2')
6407 g_string_append_printf (str, "%d", rtcpport);
6409 if (next[2] == 'i') {
6411 g_string_append_printf (str, "%d", src->free_channel);
6412 else if (next[3] == '2')
6413 g_string_append_printf (str, "%d", src->free_channel + 1);
6419 if (src->version >= GST_RTSP_VERSION_2_0)
6420 src->free_channel += 2;
6422 /* append final part */
6423 g_string_append (str, p);
6425 g_free (*transports);
6426 *transports = g_string_free (str, FALSE);
6434 GST_ERROR ("failed to allocate udp ports");
6435 return GST_RTSP_ERROR;
6440 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
6442 GstCaps *caps = NULL;
6444 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
6448 GST_DEBUG_OBJECT (src, "SRTP parameters received");
6454 default_srtcp_params (void)
6461 guint data_size = GST_ROUND_UP_4 (KEY_SIZE);
6463 /* create a random key */
6464 key_data = g_malloc (data_size);
6465 for (i = 0; i < data_size; i += 4)
6466 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
6468 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
6470 caps = gst_caps_new_simple ("application/x-srtcp",
6471 "srtp-key", GST_TYPE_BUFFER, buf,
6472 "srtp-cipher", G_TYPE_STRING, "aes-128-icm",
6473 "srtp-auth", G_TYPE_STRING, "hmac-sha1-80",
6474 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
6475 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
6477 gst_buffer_unref (buf);
6483 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
6485 gchar *base64, *result = NULL;
6486 GstMIKEYMessage *mikey_msg;
6488 stream->srtcpparams = signal_get_srtcp_params (src, stream);
6489 if (stream->srtcpparams == NULL)
6490 stream->srtcpparams = default_srtcp_params ();
6492 mikey_msg = gst_mikey_message_new_from_caps (stream->srtcpparams);
6494 /* add policy '0' for our SSRC */
6495 gst_mikey_message_add_cs_srtp (mikey_msg, 0, stream->send_ssrc, 0);
6497 base64 = gst_mikey_message_base64_encode (mikey_msg);
6498 gst_mikey_message_unref (mikey_msg);
6501 result = gst_sdp_make_keymgmt (stream->conninfo.location, base64);
6509 static GstRTSPResult
6510 gst_rtsp_src_setup_stream_from_response (GstRTSPSrc * src,
6511 GstRTSPStream * stream, GstRTSPMessage * response,
6512 GstRTSPLowerTrans * protocols, gint retry, gint * rtpport, gint * rtcpport)
6514 gchar *resptrans = NULL;
6515 GstRTSPTransport transport = { 0 };
6517 gst_rtsp_message_get_header (response, GST_RTSP_HDR_TRANSPORT, &resptrans, 0);
6519 gst_rtspsrc_stream_free_udp (stream);
6523 /* parse transport, go to next stream on parse error */
6524 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6525 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6526 return GST_RTSP_ELAST;
6529 /* update allowed transports for other streams. once the transport of
6530 * one stream has been determined, we make sure that all other streams
6531 * are configured in the same way */
6532 switch (transport.lower_transport) {
6533 case GST_RTSP_LOWER_TRANS_TCP:
6534 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6536 *protocols = GST_RTSP_LOWER_TRANS_TCP;
6537 src->interleaved = TRUE;
6538 if (src->version < GST_RTSP_VERSION_2_0) {
6539 /* update free channels */
6540 src->free_channel = MAX (transport.interleaved.min, src->free_channel);
6541 src->free_channel = MAX (transport.interleaved.max, src->free_channel);
6542 src->free_channel++;
6545 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6546 /* only allow multicast for other streams */
6547 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6549 *protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6550 /* if the server selected our ports, increment our counters so that
6551 * we select a new port later */
6552 if (src->next_port_num == transport.port.min &&
6553 src->next_port_num + 1 == transport.port.max) {
6554 src->next_port_num += 2;
6557 case GST_RTSP_LOWER_TRANS_UDP:
6558 /* only allow unicast for other streams */
6559 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6561 *protocols = GST_RTSP_LOWER_TRANS_UDP;
6564 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6565 transport.lower_transport);
6569 if (!src->interleaved || !retry) {
6570 /* now configure the stream with the selected transport */
6571 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6572 GST_DEBUG_OBJECT (src,
6573 "could not configure stream %p transport, skipping stream", stream);
6575 } else if (stream->udpsrc[0] && stream->udpsrc[1] && rtpport && rtcpport) {
6576 /* retain the first allocated UDP port pair */
6577 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", rtpport, NULL);
6578 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", rtcpport, NULL);
6581 /* we need to activate at least one stream when we detect activity */
6582 src->need_activate = TRUE;
6584 /* stream is setup now */
6585 stream->setup = TRUE;
6586 stream->waiting_setup_response = FALSE;
6588 if (src->version >= GST_RTSP_VERSION_2_0) {
6589 gchar *prop, *media_properties;
6593 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_MEDIA_PROPERTIES,
6594 &media_properties, 0) != GST_RTSP_OK) {
6595 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6596 ("Error: No MEDIA_PROPERTY header in a SETUP request in RTSP 2.0"
6597 " - this header is mandatory."));
6599 gst_rtsp_message_unset (response);
6600 return GST_RTSP_ERROR;
6603 props = g_strsplit (media_properties, ",", -2);
6604 for (i = 0; props[i]; i++) {
6607 while (*prop == ' ')
6610 if (strstr (prop, "Random-Access")) {
6611 gchar **random_seekable_val = g_strsplit (prop, "=", 2);
6613 if (!random_seekable_val[1])
6614 src->seekable = G_MAXFLOAT;
6616 src->seekable = g_ascii_strtod (random_seekable_val[1], NULL);
6618 g_strfreev (random_seekable_val);
6619 } else if (!g_strcmp0 (prop, "No-Seeking")) {
6620 src->seekable = -1.0;
6621 } else if (!g_strcmp0 (prop, "Beginning-Only")) {
6622 src->seekable = 0.0;
6630 /* clean up our transport struct */
6631 gst_rtsp_transport_init (&transport);
6632 /* clean up used RTSP messages */
6633 gst_rtsp_message_unset (response);
6639 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6640 ("Server did not select transport."));
6642 gst_rtsp_message_unset (response);
6643 return GST_RTSP_ERROR;
6647 static GstRTSPResult
6648 gst_rtspsrc_setup_streams_end (GstRTSPSrc * src, gboolean async)
6651 GstRTSPConnInfo *conninfo;
6653 g_assert (src->version >= GST_RTSP_VERSION_2_0);
6655 conninfo = &src->conninfo;
6656 for (tmp = src->streams; tmp; tmp = tmp->next) {
6657 GstRTSPStream *stream = (GstRTSPStream *) tmp->data;
6658 GstRTSPMessage response = { 0, };
6660 if (!stream->waiting_setup_response)
6663 if (!src->conninfo.connection)
6664 conninfo = &((GstRTSPStream *) tmp->data)->conninfo;
6666 gst_rtsp_src_receive_response (src, conninfo, &response, NULL);
6668 gst_rtsp_src_setup_stream_from_response (src, stream,
6669 &response, NULL, 0, NULL, NULL);
6675 /* Perform the SETUP request for all the streams.
6677 * We ask the server for a specific transport, which initially includes all the
6678 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
6679 * two local UDP ports that we send to the server.
6681 * Once the server replied with a transport, we configure the other streams
6682 * with the same transport.
6684 * In case setup request are not pipelined, this function will also configure the
6685 * stream for the selected transport, * which basically means creating the pipeline.
6686 * Otherwise, the first stream is setup right away from the reply and a
6687 * CMD_FINALIZE_SETUP command is set for the stream pipelines to happen on the
6688 * remaining streams from the RTSP thread.
6690 static GstRTSPResult
6691 gst_rtspsrc_setup_streams_start (GstRTSPSrc * src, gboolean async)
6694 GstRTSPResult res = GST_RTSP_ERROR;
6695 GstRTSPMessage request = { 0 };
6696 GstRTSPMessage response = { 0 };
6697 GstRTSPStream *stream = NULL;
6698 GstRTSPLowerTrans protocols;
6699 GstRTSPStatusCode code;
6700 gboolean unsupported_real = FALSE;
6701 gint rtpport, rtcpport;
6704 gchar *pipelined_request_id = NULL;
6706 if (src->conninfo.connection) {
6707 url = gst_rtsp_connection_get_url (src->conninfo.connection);
6708 /* we initially allow all configured lower transports. based on the URL
6709 * transports and the replies from the server we narrow them down. */
6710 protocols = url->transports & src->cur_protocols;
6713 protocols = src->cur_protocols;
6719 /* reset some state */
6720 src->free_channel = 0;
6721 src->interleaved = FALSE;
6722 src->need_activate = FALSE;
6723 /* keep track of next port number, 0 is random */
6724 src->next_port_num = src->client_port_range.min;
6725 rtpport = rtcpport = 0;
6727 if (G_UNLIKELY (src->streams == NULL))
6730 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6731 GstRTSPConnInfo *conninfo;
6738 stream = (GstRTSPStream *) walk->data;
6740 caps = stream_get_caps_for_pt (stream, stream->default_pt);
6742 GST_WARNING_OBJECT (src, "skipping stream %p, no caps", stream);
6746 if (stream->skipped) {
6747 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
6751 /* see if we need to configure this stream */
6752 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
6753 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
6758 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
6759 stream->id, caps, &selected);
6761 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
6765 /* merge/overwrite global caps */
6770 s = gst_caps_get_structure (caps, 0);
6772 num = gst_structure_n_fields (src->props);
6773 for (j = 0; j < num; j++) {
6777 name = gst_structure_nth_field_name (src->props, j);
6778 val = gst_structure_get_value (src->props, name);
6779 gst_structure_set_value (s, name, val);
6781 GST_DEBUG_OBJECT (src, "copied %s", name);
6785 /* skip setup if we have no URL for it */
6786 if (stream->conninfo.location == NULL) {
6787 GST_WARNING_OBJECT (src, "skipping stream %p, no setup", stream);
6791 if (src->conninfo.connection == NULL) {
6792 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
6793 GST_WARNING_OBJECT (src, "skipping stream %p, failed to connect",
6797 conninfo = &stream->conninfo;
6799 conninfo = &src->conninfo;
6801 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
6802 stream->conninfo.location);
6804 /* if we have a multicast connection, only suggest multicast from now on */
6805 if (stream->is_multicast)
6806 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
6809 /* first selectable protocol */
6810 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6812 if (!protocol_masks[mask])
6816 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
6817 protocol_masks[mask]);
6818 /* create a string with first transport in line */
6820 res = gst_rtspsrc_create_transports_string (src,
6821 protocols & protocol_masks[mask], stream->profile, &transports);
6822 if (res < 0 || transports == NULL)
6823 goto setup_transport_failed;
6825 if (strlen (transports) == 0) {
6826 g_free (transports);
6827 GST_DEBUG_OBJECT (src, "no transports found");
6832 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
6834 /* replace placeholders with real values, this function will optionally
6835 * allocate UDP ports and other info needed to execute the setup request */
6836 res = gst_rtspsrc_prepare_transports (stream, &transports,
6837 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
6839 g_free (transports);
6840 goto setup_transport_failed;
6843 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
6844 /* create SETUP request */
6846 gst_rtspsrc_init_request (src, &request, GST_RTSP_SETUP,
6847 stream->conninfo.location);
6849 g_free (transports);
6850 goto create_request_failed;
6853 if (src->version >= GST_RTSP_VERSION_2_0) {
6854 if (!pipelined_request_id)
6855 pipelined_request_id = g_strdup_printf ("%d",
6856 g_random_int_range (0, G_MAXINT32));
6858 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_PIPELINED_REQUESTS,
6859 pipelined_request_id);
6860 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT_RANGES,
6861 "npt, clock, smpte, clock");
6864 /* select transport */
6865 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
6867 if (stream->is_backchannel && src->backchannel == BACKCHANNEL_ONVIF)
6868 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
6869 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
6872 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
6873 stream->profile == GST_RTSP_PROFILE_SAVPF) {
6874 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
6875 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
6878 /* if the user wants a non default RTP packet size we add the blocksize
6880 if (src->rtp_blocksize > 0) {
6881 hval = g_strdup_printf ("%d", src->rtp_blocksize);
6882 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
6886 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
6889 /* handle the code ourselves */
6891 gst_rtspsrc_send (src, conninfo, &request,
6892 pipelined_request_id ? NULL : &response, &code, NULL);
6897 case GST_RTSP_STS_OK:
6899 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
6900 gst_rtsp_message_unset (&request);
6901 gst_rtsp_message_unset (&response);
6902 /* cleanup of leftover transport */
6903 gst_rtspsrc_stream_free_udp (stream);
6904 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
6905 * we might be in this case */
6906 if (stream->container && rtpport && rtcpport && !retry) {
6907 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
6912 /* this transport did not go down well, but we may have others to try
6913 * that we did not send yet, try those and only give up then
6914 * but not without checking for lost cause/extension so we can
6915 * post a nicer/more useful error message later */
6916 if (!unsupported_real)
6917 unsupported_real = stream->is_real;
6918 /* select next available protocol, give up on this stream if none */
6920 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6922 if (!protocol_masks[mask] || unsupported_real)
6927 /* cleanup of leftover transport and move to the next stream */
6928 gst_rtspsrc_stream_free_udp (stream);
6929 goto response_error;
6933 if (!pipelined_request_id) {
6934 /* parse response transport */
6935 res = gst_rtsp_src_setup_stream_from_response (src, stream,
6936 &response, &protocols, retry, &rtpport, &rtcpport);
6938 case GST_RTSP_ERROR:
6940 case GST_RTSP_ELAST:
6946 stream->waiting_setup_response = TRUE;
6947 /* we need to activate at least one stream when we detect activity */
6948 src->need_activate = TRUE;
6955 GstRTSPStream *sskip;
6957 skip = g_list_next (skip);
6961 sskip = (GstRTSPStream *) skip->data;
6963 /* skip all streams with the same control url */
6964 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
6965 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
6966 sskip, sskip->conninfo.location);
6967 sskip->skipped = TRUE;
6971 gst_rtsp_message_unset (&request);
6974 if (pipelined_request_id) {
6975 gst_rtspsrc_setup_streams_end (src, TRUE);
6978 /* store the transport protocol that was configured */
6979 src->cur_protocols = protocols;
6981 gst_rtsp_ext_list_stream_select (src->extensions, url);
6983 if (pipelined_request_id)
6984 g_free (pipelined_request_id);
6986 /* if there is nothing to activate, error out */
6987 if (!src->need_activate)
6988 goto nothing_to_activate;
6995 /* no transport possible, post an error and stop */
6996 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6997 ("Could not connect to server, no protocols left"));
6998 return GST_RTSP_ERROR;
7002 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7003 ("SDP contains no streams"));
7004 return GST_RTSP_ERROR;
7006 create_request_failed:
7008 gchar *str = gst_rtsp_strresult (res);
7010 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7011 ("Could not create request. (%s)", str));
7015 setup_transport_failed:
7017 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7018 ("Could not setup transport."));
7019 res = GST_RTSP_ERROR;
7024 const gchar *str = gst_rtsp_status_as_text (code);
7026 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7027 ("Error (%d): %s", code, GST_STR_NULL (str)));
7028 res = GST_RTSP_ERROR;
7033 gchar *str = gst_rtsp_strresult (res);
7035 if (res != GST_RTSP_EINTR) {
7036 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7037 ("Could not send message. (%s)", str));
7039 GST_WARNING_OBJECT (src, "send interrupted");
7044 nothing_to_activate:
7046 /* none of the available error codes is really right .. */
7047 if (unsupported_real) {
7048 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
7049 (_("No supported stream was found. You might need to install a "
7050 "GStreamer RTSP extension plugin for Real media streams.")),
7053 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
7054 (_("No supported stream was found. You might need to allow "
7055 "more transport protocols or may otherwise be missing "
7056 "the right GStreamer RTSP extension plugin.")), (NULL));
7058 return GST_RTSP_ERROR;
7062 if (pipelined_request_id)
7063 g_free (pipelined_request_id);
7064 gst_rtsp_message_unset (&request);
7065 gst_rtsp_message_unset (&response);
7071 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
7072 GstSegment * segment)
7075 GstRTSPTimeRange *therange;
7078 gst_rtsp_range_free (src->range);
7080 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
7081 GST_DEBUG_OBJECT (src, "parsed range %s", range);
7082 src->range = therange;
7084 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
7086 gst_segment_init (segment, GST_FORMAT_TIME);
7090 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
7091 therange->min.type, therange->min.seconds, therange->max.type,
7092 therange->max.seconds);
7094 if (therange->min.type == GST_RTSP_TIME_NOW)
7096 else if (therange->min.type == GST_RTSP_TIME_END)
7099 seconds = therange->min.seconds * GST_SECOND;
7101 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
7102 GST_TIME_ARGS (seconds));
7104 /* we need to start playback without clipping from the position reported by
7106 segment->start = seconds;
7107 segment->position = seconds;
7109 if (therange->max.type == GST_RTSP_TIME_NOW)
7111 else if (therange->max.type == GST_RTSP_TIME_END)
7114 seconds = therange->max.seconds * GST_SECOND;
7116 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
7117 GST_TIME_ARGS (seconds));
7119 /* live (WMS) server might send overflowed large max as its idea of infinity,
7120 * compensate to prevent problems later on */
7121 if (seconds != -1 && seconds < 0) {
7123 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
7126 /* live (WMS) might send min == max, which is not worth recording */
7127 if (segment->duration == -1 && seconds == segment->start)
7130 /* don't change duration with unknown value, we might have a valid value
7131 * there that we want to keep. */
7133 segment->duration = seconds;
7138 /* Parse clock profived by the server with following syntax:
7140 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
7143 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
7145 gboolean res = FALSE;
7147 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
7148 gchar **fields = NULL, **parts = NULL;
7149 gchar *remote_ip, *str;
7151 GstClockTime base_time;
7154 fields = g_strsplit (gstclock, " ", 0);
7156 /* wrapped clock, not very interesting for now */
7157 if (fields[1] == NULL)
7160 /* remote IP address and port */
7161 if ((str = fields[2]) == NULL)
7164 parts = g_strsplit (str, ":", 0);
7166 if ((remote_ip = parts[0]) == NULL)
7169 if ((str = parts[1]) == NULL)
7177 if ((str = fields[3]) == NULL)
7180 base_time = g_ascii_strtoull (str, NULL, 10);
7183 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
7186 if (src->provided_clock)
7187 gst_object_unref (src->provided_clock);
7188 src->provided_clock = netclock;
7190 gst_element_post_message (GST_ELEMENT_CAST (src),
7191 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
7192 src->provided_clock, TRUE));
7196 g_strfreev (fields);
7202 /* must be called with the RTSP state lock */
7203 static GstRTSPResult
7204 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
7210 /* prepare global stream caps properties */
7212 gst_structure_remove_all_fields (src->props);
7214 src->props = gst_structure_new_empty ("RTSPProperties");
7216 DEBUG_SDP (src, sdp);
7218 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
7220 /* let the app inspect and change the SDP */
7221 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
7223 gst_segment_init (&src->segment, GST_FORMAT_TIME);
7225 /* parse range for duration reporting. */
7230 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
7234 /* keep track of the range and configure it in the segment */
7235 if (gst_rtspsrc_parse_range (src, range, &src->segment))
7239 /* parse clock information. This is GStreamer specific, a server can tell the
7240 * client what clock it is using and wrap that in a network clock. The
7241 * advantage of that is that we can slave to it. */
7243 const gchar *gstclock;
7246 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
7247 if (gstclock == NULL)
7250 /* parse the clock and expose it in the provide_clock method */
7251 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
7255 /* try to find a global control attribute. Note that a '*' means that we should
7256 * do aggregate control with the current url (so we don't do anything and
7257 * leave the current connection as is) */
7259 const gchar *control;
7262 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
7263 if (control == NULL)
7266 /* only take fully qualified urls */
7267 if (g_str_has_prefix (control, "rtsp://"))
7271 g_free (src->conninfo.location);
7272 src->conninfo.location = g_strdup (control);
7273 /* make a connection for this, if there was a connection already, nothing
7275 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
7276 GST_ERROR_OBJECT (src, "could not connect");
7279 /* we need to keep the control url separate from the connection url because
7280 * the rules for constructing the media control url need it */
7281 g_free (src->control);
7282 src->control = g_strdup (control);
7285 /* create streams */
7286 n_streams = gst_sdp_message_medias_len (sdp);
7287 for (i = 0; i < n_streams; i++) {
7288 gst_rtspsrc_create_stream (src, sdp, i, n_streams);
7291 src->state = GST_RTSP_STATE_INIT;
7294 if ((res = gst_rtspsrc_setup_streams_start (src, async)) < 0)
7297 /* reset our state */
7298 src->need_range = TRUE;
7301 src->state = GST_RTSP_STATE_READY;
7308 GST_ERROR_OBJECT (src, "setup failed");
7309 gst_rtspsrc_cleanup (src);
7314 static GstRTSPResult
7315 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
7319 GstRTSPMessage request = { 0 };
7320 GstRTSPMessage response = { 0 };
7323 gchar *respcont = NULL;
7324 GstRTSPVersion versions[] =
7325 { GST_RTSP_VERSION_2_0, GST_RTSP_VERSION_INVALID };
7327 src->version = src->default_version;
7328 if (src->default_version == GST_RTSP_VERSION_2_0) {
7329 versions[0] = GST_RTSP_VERSION_1_0;
7333 src->need_redirect = FALSE;
7335 /* can't continue without a valid url */
7336 if (G_UNLIKELY (src->conninfo.url == NULL)) {
7337 res = GST_RTSP_EINVAL;
7340 src->tried_url_auth = FALSE;
7342 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
7343 goto connect_failed;
7345 /* create OPTIONS */
7346 GST_DEBUG_OBJECT (src, "create options... (%s)", async ? "async" : "sync");
7348 gst_rtspsrc_init_request (src, &request, GST_RTSP_OPTIONS,
7349 src->conninfo.url_str);
7351 goto create_request_failed;
7354 request.type_data.request.version = src->version;
7355 GST_DEBUG_OBJECT (src, "send options...");
7358 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
7361 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
7362 NULL, versions)) < 0) {
7366 src->version = request.type_data.request.version;
7367 GST_INFO_OBJECT (src, "Now using version: %s",
7368 gst_rtsp_version_as_text (src->version));
7371 if (!gst_rtspsrc_parse_methods (src, &response))
7374 /* create DESCRIBE */
7375 GST_DEBUG_OBJECT (src, "create describe...");
7377 gst_rtspsrc_init_request (src, &request, GST_RTSP_DESCRIBE,
7378 src->conninfo.url_str);
7380 goto create_request_failed;
7382 /* we only accept SDP for now */
7383 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
7386 if (src->backchannel == BACKCHANNEL_ONVIF)
7387 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
7388 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
7389 /* TODO: Handle the case when backchannel is unsupported and goto restart */
7392 GST_DEBUG_OBJECT (src, "send describe...");
7395 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
7398 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
7402 /* we only perform redirect for describe and play, currently */
7403 if (src->need_redirect) {
7404 /* close connection, we don't have to send a TEARDOWN yet, ignore the
7406 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7408 gst_rtsp_message_unset (&request);
7409 gst_rtsp_message_unset (&response);
7415 /* it could be that the DESCRIBE method was not implemented */
7416 if (!(src->methods & GST_RTSP_DESCRIBE))
7419 /* check if reply is SDP */
7420 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
7422 /* could not be set but since the request returned OK, we assume it
7423 * was SDP, else check it. */
7425 const gchar *props = strchr (respcont, ';');
7428 gchar *mimetype = g_strndup (respcont, props - respcont);
7430 mimetype = g_strstrip (mimetype);
7431 if (g_ascii_strcasecmp (mimetype, "application/sdp") != 0) {
7433 goto wrong_content_type;
7436 /* TODO: Check for charset property and do conversions of all messages if
7437 * needed. Some servers actually send that property */
7440 } else if (g_ascii_strcasecmp (respcont, "application/sdp") != 0) {
7441 goto wrong_content_type;
7445 /* get message body and parse as SDP */
7446 gst_rtsp_message_get_body (&response, &data, &size);
7447 if (data == NULL || size == 0)
7450 GST_DEBUG_OBJECT (src, "parse SDP...");
7451 gst_sdp_message_new (sdp);
7452 gst_sdp_message_parse_buffer (data, size, *sdp);
7454 /* clean up any messages */
7455 gst_rtsp_message_unset (&request);
7456 gst_rtsp_message_unset (&response);
7463 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
7464 ("No valid RTSP URL was provided"));
7469 gchar *str = gst_rtsp_strresult (res);
7471 if (res != GST_RTSP_EINTR) {
7472 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
7473 ("Failed to connect. (%s)", str));
7475 GST_WARNING_OBJECT (src, "connect interrupted");
7480 create_request_failed:
7482 gchar *str = gst_rtsp_strresult (res);
7484 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7485 ("Could not create request. (%s)", str));
7491 /* Don't post a message - the rtsp_send method will have
7492 * taken care of it because we passed NULL for the response code */
7497 /* error was posted */
7498 res = GST_RTSP_ERROR;
7503 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7504 ("Server does not support SDP, got %s.", respcont));
7505 res = GST_RTSP_ERROR;
7510 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7511 ("Server can not provide an SDP."));
7512 res = GST_RTSP_ERROR;
7517 if (src->conninfo.connection) {
7518 GST_DEBUG_OBJECT (src, "free connection");
7519 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7521 gst_rtsp_message_unset (&request);
7522 gst_rtsp_message_unset (&response);
7527 static GstRTSPResult
7528 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
7533 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
7535 if (src->sdp == NULL) {
7536 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
7540 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
7545 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
7552 GST_WARNING_OBJECT (src, "can't get sdp");
7553 src->open_error = TRUE;
7558 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
7559 src->open_error = TRUE;
7564 static GstRTSPResult
7565 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
7567 GstRTSPMessage request = { 0 };
7568 GstRTSPMessage response = { 0 };
7569 GstRTSPResult res = GST_RTSP_OK;
7571 const gchar *control;
7573 GST_DEBUG_OBJECT (src, "TEARDOWN...");
7575 gst_rtspsrc_set_state (src, GST_STATE_READY);
7577 if (src->state < GST_RTSP_STATE_READY) {
7578 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
7585 /* construct a control url */
7586 control = get_aggregate_control (src);
7588 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
7591 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7592 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7593 const gchar *setup_url;
7594 GstRTSPConnInfo *info;
7596 /* try aggregate control first but do non-aggregate control otherwise */
7598 setup_url = control;
7599 else if ((setup_url = stream->conninfo.location) == NULL)
7602 if (src->conninfo.connection) {
7603 info = &src->conninfo;
7604 } else if (stream->conninfo.connection) {
7605 info = &stream->conninfo;
7609 if (!info->connected)
7614 gst_rtspsrc_init_request (src, &request, GST_RTSP_TEARDOWN, setup_url);
7616 goto create_request_failed;
7618 if (stream->is_backchannel && src->backchannel == BACKCHANNEL_ONVIF)
7619 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
7620 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
7623 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
7626 gst_rtspsrc_send (src, info, &request, &response, NULL, NULL)) < 0)
7629 /* FIXME, parse result? */
7630 gst_rtsp_message_unset (&request);
7631 gst_rtsp_message_unset (&response);
7634 /* early exit when we did aggregate control */
7640 /* close connections */
7641 GST_DEBUG_OBJECT (src, "closing connection...");
7642 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7643 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7644 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7645 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
7649 gst_rtspsrc_cleanup (src);
7651 src->state = GST_RTSP_STATE_INVALID;
7654 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
7659 create_request_failed:
7661 gchar *str = gst_rtsp_strresult (res);
7663 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7664 ("Could not create request. (%s)", str));
7670 gchar *str = gst_rtsp_strresult (res);
7672 gst_rtsp_message_unset (&request);
7673 if (res != GST_RTSP_EINTR) {
7674 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7675 ("Could not send message. (%s)", str));
7677 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
7684 GST_DEBUG_OBJECT (src,
7685 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
7690 /* RTP-Info is of the format:
7692 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
7694 * rtptime corresponds to the timestamp for the NPT time given in the header
7695 * seqbase corresponds to the next sequence number we received. This number
7696 * indicates the first seqnum after the seek and should be used to discard
7697 * packets that are from before the seek.
7700 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
7705 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
7707 infos = g_strsplit (rtpinfo, ",", 0);
7708 for (i = 0; infos[i]; i++) {
7710 GstRTSPStream *stream;
7714 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
7716 /* init values, types of seqbase and timebase are bigger than needed so we
7717 * can store -1 as uninitialized values */
7722 /* parse url, find stream for url.
7723 * parse seq and rtptime. The seq number should be configured in the rtp
7724 * depayloader or session manager to detect gaps. Same for the rtptime, it
7725 * should be used to create an initial time newsegment. */
7726 fields = g_strsplit (infos[i], ";", 0);
7727 for (j = 0; fields[j]; j++) {
7728 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
7729 /* remove leading whitespace */
7730 fields[j] = g_strchug (fields[j]);
7731 if (g_str_has_prefix (fields[j], "url=")) {
7732 /* get the url and the stream */
7734 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
7735 } else if (g_str_has_prefix (fields[j], "seq=")) {
7736 seqbase = atoi (fields[j] + 4);
7737 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
7738 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
7741 g_strfreev (fields);
7742 /* now we need to store the values for the caps of the stream */
7743 if (stream != NULL) {
7744 GST_DEBUG_OBJECT (src,
7745 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
7746 stream, seqbase, timebase);
7748 /* we have a stream, configure detected params */
7749 stream->seqbase = seqbase;
7750 stream->timebase = timebase;
7759 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
7764 interval = strtoul (rtcp, NULL, 10);
7765 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
7770 interval *= GST_MSECOND;
7772 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7773 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7775 /* already (optionally) retrieved this when configuring manager */
7776 if (stream->session) {
7777 GObject *rtpsession = stream->session;
7779 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
7781 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
7785 /* now it happens that (Xenon) server sending this may also provide bogus
7786 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
7787 * and just use RTP-Info to sync */
7789 GObjectClass *klass;
7791 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
7792 if (g_object_class_find_property (klass, "rtcp-sync")) {
7793 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
7794 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
7800 gst_rtspsrc_get_float (const gchar * dstr)
7802 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7804 /* canonicalise floating point string so we can handle float strings
7805 * in the form "24.930" or "24,930" irrespective of the current locale */
7806 g_strlcpy (s, dstr, sizeof (s));
7807 g_strdelimit (s, ",", '.');
7808 return g_ascii_strtod (s, NULL);
7812 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
7814 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7816 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
7817 g_strlcpy (val_str, "now", sizeof (val_str));
7819 if (segment->position == 0) {
7820 g_strlcpy (val_str, "0", sizeof (val_str));
7822 g_ascii_dtostr (val_str, sizeof (val_str),
7823 ((gdouble) segment->position) / GST_SECOND);
7826 return g_strdup_printf ("npt=%s-", val_str);
7830 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
7834 stream->timebase = -1;
7835 stream->seqbase = -1;
7837 len = stream->ptmap->len;
7838 for (i = 0; i < len; i++) {
7839 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
7842 if (item->caps == NULL)
7845 item->caps = gst_caps_make_writable (item->caps);
7846 s = gst_caps_get_structure (item->caps, 0);
7847 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
7848 if (item->pt == stream->default_pt && stream->udpsrc[0])
7849 g_object_set (stream->udpsrc[0], "caps", item->caps, NULL);
7851 stream->need_caps = TRUE;
7854 static GstRTSPResult
7855 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
7857 GstRTSPResult res = GST_RTSP_OK;
7859 if (src->state < GST_RTSP_STATE_READY) {
7860 res = GST_RTSP_ERROR;
7861 if (src->open_error) {
7862 GST_DEBUG_OBJECT (src, "the stream was in error");
7866 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
7868 if ((res = gst_rtspsrc_open (src, async)) < 0) {
7869 GST_DEBUG_OBJECT (src, "failed to open stream");
7878 static GstRTSPResult
7879 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async,
7880 const gchar * seek_style)
7882 GstRTSPMessage request = { 0 };
7883 GstRTSPMessage response = { 0 };
7884 GstRTSPResult res = GST_RTSP_OK;
7888 const gchar *control;
7890 GST_DEBUG_OBJECT (src, "PLAY...");
7893 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7896 if (!(src->methods & GST_RTSP_PLAY))
7899 if (src->state == GST_RTSP_STATE_PLAYING)
7902 if (!src->conninfo.connection || !src->conninfo.connected)
7905 /* send some dummy packets before we activate the receive in the
7907 gst_rtspsrc_send_dummy_packets (src);
7909 /* require new SR packets */
7911 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
7913 /* construct a control url */
7914 control = get_aggregate_control (src);
7916 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7917 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7918 const gchar *setup_url;
7919 GstRTSPConnInfo *conninfo;
7921 /* try aggregate control first but do non-aggregate control otherwise */
7923 setup_url = control;
7924 else if ((setup_url = stream->conninfo.location) == NULL)
7927 if (src->conninfo.connection) {
7928 conninfo = &src->conninfo;
7929 } else if (stream->conninfo.connection) {
7930 conninfo = &stream->conninfo;
7936 res = gst_rtspsrc_init_request (src, &request, GST_RTSP_PLAY, setup_url);
7938 goto create_request_failed;
7940 if (src->need_range && src->seekable >= 0.0) {
7941 hval = gen_range_header (src, segment);
7943 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
7945 /* store the newsegment event so it can be sent from the streaming thread. */
7946 src->need_segment = TRUE;
7949 if (segment->rate != 1.0) {
7950 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
7952 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
7954 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
7956 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
7960 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SEEK_STYLE,
7963 /* when we have an ONVIF audio backchannel, the PLAY request must have the
7964 * Require: header when doing either aggregate or non-aggregate control */
7965 if (src->backchannel == BACKCHANNEL_ONVIF &&
7966 (control || stream->is_backchannel))
7967 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
7968 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
7971 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
7974 gst_rtspsrc_send (src, conninfo, &request, &response, NULL, NULL))
7978 if (src->need_redirect) {
7979 GST_DEBUG_OBJECT (src,
7980 "redirect: tearing down and restarting with new url");
7981 /* teardown and restart with new url */
7982 gst_rtspsrc_close (src, TRUE, FALSE);
7983 /* reset protocols to force re-negotiation with redirected url */
7984 src->cur_protocols = src->protocols;
7985 gst_rtsp_message_unset (&request);
7986 gst_rtsp_message_unset (&response);
7990 /* seek may have silently failed as it is not supported */
7991 if (!(src->methods & GST_RTSP_PLAY)) {
7992 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
7994 if (src->version >= GST_RTSP_VERSION_2_0 && src->seekable >= 0.0) {
7995 GST_WARNING_OBJECT (src, "Server declared stream as seekable but"
7996 " playing with range failed... Ignoring information.");
7998 /* obviously it is supported as we made it here */
7999 src->methods |= GST_RTSP_PLAY;
8000 src->seekable = -1.0;
8001 /* but there is nothing to parse in the response,
8002 * so convey we have no idea and not to expect anything particular */
8003 clear_rtp_base (src, stream);
8007 /* need to do for all streams */
8008 for (run = src->streams; run; run = g_list_next (run))
8009 clear_rtp_base (src, (GstRTSPStream *) run->data);
8011 /* NOTE the above also disables npt based eos detection */
8012 /* and below forces position to 0,
8013 * which is visible feedback we lost the plot */
8014 segment->start = segment->position = src->last_pos;
8017 gst_rtsp_message_unset (&request);
8019 /* parse RTP npt field. This is the current position in the stream (Normal
8020 * Play Time) and should be put in the NEWSEGMENT position field. */
8021 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
8023 gst_rtspsrc_parse_range (src, hval, segment);
8025 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
8026 segment->rate = 1.0;
8028 /* parse Speed header. This is the intended playback rate of the stream
8029 * and should be put in the NEWSEGMENT rate field. */
8030 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
8031 0) == GST_RTSP_OK) {
8032 segment->rate = gst_rtspsrc_get_float (hval);
8033 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
8034 &hval, 0) == GST_RTSP_OK) {
8035 segment->rate = gst_rtspsrc_get_float (hval);
8038 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
8039 * for the RTP packets. If this is not present, we assume all starts from 0...
8040 * This is info for the RTP session manager that we pass to it in caps. */
8042 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
8043 &hval, hval_idx++) == GST_RTSP_OK)
8044 gst_rtspsrc_parse_rtpinfo (src, hval);
8046 /* some servers indicate RTCP parameters in PLAY response,
8047 * rather than properly in SDP */
8048 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
8049 &hval, 0) == GST_RTSP_OK)
8050 gst_rtspsrc_handle_rtcp_interval (src, hval);
8052 gst_rtsp_message_unset (&response);
8054 /* early exit when we did aggregate control */
8058 /* configure the caps of the streams after we parsed all headers. Only reset
8059 * the manager object when we set a new Range header (we did a seek) */
8060 gst_rtspsrc_configure_caps (src, segment, src->need_range);
8062 /* set to PLAYING after we have configured the caps, otherwise we
8063 * might end up calling request_key (with SRTP) while caps are still
8064 * being configured. */
8065 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
8067 /* set again when needed */
8068 src->need_range = FALSE;
8070 src->running = TRUE;
8071 src->base_time = -1;
8072 src->state = GST_RTSP_STATE_PLAYING;
8075 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
8076 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8077 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8078 stream->discont = TRUE;
8083 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
8090 GST_WARNING_OBJECT (src, "failed to open stream");
8095 GST_WARNING_OBJECT (src, "PLAY is not supported");
8100 GST_WARNING_OBJECT (src, "we were already PLAYING");
8103 create_request_failed:
8105 gchar *str = gst_rtsp_strresult (res);
8107 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
8108 ("Could not create request. (%s)", str));
8114 gchar *str = gst_rtsp_strresult (res);
8116 gst_rtsp_message_unset (&request);
8117 if (res != GST_RTSP_EINTR) {
8118 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
8119 ("Could not send message. (%s)", str));
8121 GST_WARNING_OBJECT (src, "PLAY interrupted");
8128 static GstRTSPResult
8129 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
8131 GstRTSPResult res = GST_RTSP_OK;
8132 GstRTSPMessage request = { 0 };
8133 GstRTSPMessage response = { 0 };
8135 const gchar *control;
8137 GST_DEBUG_OBJECT (src, "PAUSE...");
8139 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
8142 if (!(src->methods & GST_RTSP_PAUSE))
8145 if (src->state == GST_RTSP_STATE_READY)
8148 if (!src->conninfo.connection || !src->conninfo.connected)
8151 /* construct a control url */
8152 control = get_aggregate_control (src);
8154 /* loop over the streams. We might exit the loop early when we could do an
8155 * aggregate control */
8156 for (walk = src->streams; walk; walk = g_list_next (walk)) {
8157 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
8158 GstRTSPConnInfo *conninfo;
8159 const gchar *setup_url;
8161 /* try aggregate control first but do non-aggregate control otherwise */
8163 setup_url = control;
8164 else if ((setup_url = stream->conninfo.location) == NULL)
8167 if (src->conninfo.connection) {
8168 conninfo = &src->conninfo;
8169 } else if (stream->conninfo.connection) {
8170 conninfo = &stream->conninfo;
8176 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
8177 ("Sending PAUSE request"));
8180 gst_rtspsrc_init_request (src, &request, GST_RTSP_PAUSE,
8182 goto create_request_failed;
8184 /* when we have an ONVIF audio backchannel, the PAUSE request must have the
8185 * Require: header when doing either aggregate or non-aggregate control */
8186 if (src->backchannel == BACKCHANNEL_ONVIF &&
8187 (control || stream->is_backchannel))
8188 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_REQUIRE,
8189 BACKCHANNEL_ONVIF_HDR_REQUIRE_VAL);
8192 gst_rtspsrc_send (src, conninfo, &request, &response, NULL,
8196 gst_rtsp_message_unset (&request);
8197 gst_rtsp_message_unset (&response);
8199 /* exit early when we did agregate control */
8204 /* change element states now */
8205 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
8208 src->state = GST_RTSP_STATE_READY;
8212 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
8219 GST_DEBUG_OBJECT (src, "failed to open stream");
8224 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
8229 GST_DEBUG_OBJECT (src, "we were already PAUSED");
8232 create_request_failed:
8234 gchar *str = gst_rtsp_strresult (res);
8236 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
8237 ("Could not create request. (%s)", str));
8243 gchar *str = gst_rtsp_strresult (res);
8245 gst_rtsp_message_unset (&request);
8246 if (res != GST_RTSP_EINTR) {
8247 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
8248 ("Could not send message. (%s)", str));
8250 GST_WARNING_OBJECT (src, "PAUSE interrupted");
8258 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
8260 GstRTSPSrc *rtspsrc;
8262 rtspsrc = GST_RTSPSRC (bin);
8264 switch (GST_MESSAGE_TYPE (message)) {
8265 case GST_MESSAGE_EOS:
8266 gst_message_unref (message);
8268 case GST_MESSAGE_ELEMENT:
8270 const GstStructure *s = gst_message_get_structure (message);
8272 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
8273 gboolean ignore_timeout;
8275 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
8277 GST_OBJECT_LOCK (rtspsrc);
8278 ignore_timeout = rtspsrc->ignore_timeout;
8279 rtspsrc->ignore_timeout = TRUE;
8280 GST_OBJECT_UNLOCK (rtspsrc);
8282 /* we only act on the first udp timeout message, others are irrelevant
8283 * and can be ignored. */
8284 if (!ignore_timeout)
8285 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
8287 gst_message_unref (message);
8290 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8293 case GST_MESSAGE_ERROR:
8296 GstRTSPStream *stream;
8299 udpsrc = GST_MESSAGE_SRC (message);
8301 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
8302 GST_ELEMENT_NAME (udpsrc));
8304 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
8308 /* we ignore the RTCP udpsrc */
8309 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
8312 /* if we get error messages from the udp sources, that's not a problem as
8313 * long as not all of them error out. We also don't really know what the
8314 * problem is, the message does not give enough detail... */
8315 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
8316 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
8317 if (ret != GST_FLOW_OK)
8321 gst_message_unref (message);
8325 /* fatal but not our message, forward */
8326 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8331 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8337 /* the thread where everything happens */
8339 gst_rtspsrc_thread (GstRTSPSrc * src)
8343 GST_OBJECT_LOCK (src);
8344 cmd = src->pending_cmd;
8345 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
8346 || cmd == CMD_LOOP || cmd == CMD_OPEN)
8347 src->pending_cmd = CMD_LOOP;
8349 src->pending_cmd = CMD_WAIT;
8350 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
8352 /* we got the message command, so ensure communication is possible again */
8353 gst_rtspsrc_connection_flush (src, FALSE);
8355 src->busy_cmd = cmd;
8356 GST_OBJECT_UNLOCK (src);
8360 gst_rtspsrc_open (src, TRUE);
8363 gst_rtspsrc_play (src, &src->segment, TRUE, NULL);
8366 gst_rtspsrc_pause (src, TRUE);
8369 gst_rtspsrc_close (src, TRUE, FALSE);
8372 gst_rtspsrc_loop (src);
8375 gst_rtspsrc_reconnect (src, FALSE);
8381 GST_OBJECT_LOCK (src);
8382 /* and go back to sleep */
8383 if (src->pending_cmd == CMD_WAIT) {
8385 gst_task_pause (src->task);
8388 src->busy_cmd = CMD_WAIT;
8389 GST_OBJECT_UNLOCK (src);
8393 gst_rtspsrc_start (GstRTSPSrc * src)
8395 GST_DEBUG_OBJECT (src, "starting");
8397 GST_OBJECT_LOCK (src);
8399 src->pending_cmd = CMD_WAIT;
8401 if (src->task == NULL) {
8402 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
8403 if (src->task == NULL)
8406 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
8408 GST_OBJECT_UNLOCK (src);
8415 GST_OBJECT_UNLOCK (src);
8416 GST_ERROR_OBJECT (src, "failed to create task");
8422 gst_rtspsrc_stop (GstRTSPSrc * src)
8426 GST_DEBUG_OBJECT (src, "stopping");
8428 /* also cancels pending task */
8429 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
8431 GST_OBJECT_LOCK (src);
8432 if ((task = src->task)) {
8434 GST_OBJECT_UNLOCK (src);
8436 gst_task_stop (task);
8438 /* make sure it is not running */
8439 GST_RTSP_STREAM_LOCK (src);
8440 GST_RTSP_STREAM_UNLOCK (src);
8442 /* now wait for the task to finish */
8443 gst_task_join (task);
8445 /* and free the task */
8446 gst_object_unref (GST_OBJECT (task));
8448 GST_OBJECT_LOCK (src);
8450 GST_OBJECT_UNLOCK (src);
8452 /* ensure synchronously all is closed and clean */
8453 gst_rtspsrc_close (src, FALSE, TRUE);
8458 static GstStateChangeReturn
8459 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
8461 GstRTSPSrc *rtspsrc;
8462 GstStateChangeReturn ret;
8464 rtspsrc = GST_RTSPSRC (element);
8466 switch (transition) {
8467 case GST_STATE_CHANGE_NULL_TO_READY:
8468 if (!gst_rtspsrc_start (rtspsrc))
8471 case GST_STATE_CHANGE_READY_TO_PAUSED:
8472 /* init some state */
8473 rtspsrc->cur_protocols = rtspsrc->protocols;
8474 /* first attempt, don't ignore timeouts */
8475 rtspsrc->ignore_timeout = FALSE;
8476 rtspsrc->open_error = FALSE;
8477 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
8479 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8480 set_manager_buffer_mode (rtspsrc);
8482 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8483 /* unblock the tcp tasks and make the loop waiting */
8484 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
8485 /* make sure it is waiting before we send PAUSE or PLAY below */
8486 GST_RTSP_STREAM_LOCK (rtspsrc);
8487 GST_RTSP_STREAM_UNLOCK (rtspsrc);
8490 case GST_STATE_CHANGE_PAUSED_TO_READY:
8496 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
8497 if (ret == GST_STATE_CHANGE_FAILURE)
8500 switch (transition) {
8501 case GST_STATE_CHANGE_NULL_TO_READY:
8502 ret = GST_STATE_CHANGE_SUCCESS;
8504 case GST_STATE_CHANGE_READY_TO_PAUSED:
8505 ret = GST_STATE_CHANGE_NO_PREROLL;
8507 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8508 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
8509 ret = GST_STATE_CHANGE_SUCCESS;
8511 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8512 /* send pause request and keep the idle task around */
8513 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
8514 ret = GST_STATE_CHANGE_NO_PREROLL;
8516 case GST_STATE_CHANGE_PAUSED_TO_READY:
8517 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_ALL);
8518 ret = GST_STATE_CHANGE_SUCCESS;
8520 case GST_STATE_CHANGE_READY_TO_NULL:
8521 gst_rtspsrc_stop (rtspsrc);
8522 ret = GST_STATE_CHANGE_SUCCESS;
8525 /* Otherwise it's success, we don't want to return spurious
8526 * NO_PREROLL or ASYNC from internal elements as we care for
8527 * state changes ourselves here
8529 * This is to catch PAUSED->PAUSED and PLAYING->PLAYING transitions.
8531 if (GST_STATE_TRANSITION_NEXT (transition) == GST_STATE_PAUSED)
8532 ret = GST_STATE_CHANGE_NO_PREROLL;
8534 ret = GST_STATE_CHANGE_SUCCESS;
8543 GST_DEBUG_OBJECT (rtspsrc, "start failed");
8544 return GST_STATE_CHANGE_FAILURE;
8549 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
8552 GstRTSPSrc *rtspsrc;
8554 rtspsrc = GST_RTSPSRC (element);
8556 if (GST_EVENT_IS_DOWNSTREAM (event)) {
8557 res = gst_rtspsrc_push_event (rtspsrc, event);
8559 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
8566 /*** GSTURIHANDLER INTERFACE *************************************************/
8569 gst_rtspsrc_uri_get_type (GType type)
8574 static const gchar *const *
8575 gst_rtspsrc_uri_get_protocols (GType type)
8577 static const gchar *protocols[] =
8578 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
8579 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
8586 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
8588 GstRTSPSrc *src = GST_RTSPSRC (handler);
8590 /* FIXME: make thread-safe */
8591 return g_strdup (src->conninfo.location);
8595 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
8601 GstRTSPUrl *newurl = NULL;
8602 GstSDPMessage *sdp = NULL;
8604 src = GST_RTSPSRC (handler);
8606 /* same URI, we're fine */
8607 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
8610 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
8611 sres = gst_sdp_message_new (&sdp);
8615 GST_DEBUG_OBJECT (src, "parsing SDP message");
8616 sres = gst_sdp_message_parse_uri (uri, sdp);
8621 GST_DEBUG_OBJECT (src, "parsing URI");
8622 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
8626 /* if worked, free previous and store new url object along with the original
8628 GST_DEBUG_OBJECT (src, "configuring URI");
8629 g_free (src->conninfo.location);
8630 src->conninfo.location = g_strdup (uri);
8631 gst_rtsp_url_free (src->conninfo.url);
8632 src->conninfo.url = newurl;
8633 g_free (src->conninfo.url_str);
8635 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
8637 src->conninfo.url_str = NULL;
8640 gst_sdp_message_free (src->sdp);
8642 src->from_sdp = sdp != NULL;
8644 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
8645 GST_DEBUG_OBJECT (src, "request uri is: %s",
8646 GST_STR_NULL (src->conninfo.url_str));
8653 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
8658 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
8659 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8660 "Could not create SDP");
8665 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
8666 GST_STR_NULL (uri));
8667 gst_sdp_message_free (sdp);
8668 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8674 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
8675 GST_STR_NULL (uri), res);
8676 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8677 "Invalid RTSP URI");
8683 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
8685 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
8687 iface->get_type = gst_rtspsrc_uri_get_type;
8688 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
8689 iface->get_uri = gst_rtspsrc_uri_get_uri;
8690 iface->set_uri = gst_rtspsrc_uri_set_uri;
8693 typedef struct _RTSPKeyValue
8695 GstRTSPHeaderField field;
8697 gchar *custom_key; /* custom header string (field is INVALID then) */
8701 key_value_foreach (GArray * array, GFunc func, gpointer user_data)
8705 g_return_if_fail (array != NULL);
8707 for (i = 0; i < array->len; i++) {
8708 (*func) (&g_array_index (array, RTSPKeyValue, i), user_data);
8713 dump_key_value (gpointer data, gpointer user_data G_GNUC_UNUSED)
8715 RTSPKeyValue *key_value = (RTSPKeyValue *) data;
8716 GstRTSPSrc *src = GST_RTSPSRC (user_data);
8717 const gchar *key_string;
8719 if (key_value->custom_key != NULL)
8720 key_string = key_value->custom_key;
8722 key_string = gst_rtsp_header_as_text (key_value->field);
8724 GST_LOG_OBJECT (src, " key: '%s', value: '%s'", key_string,
8729 gst_rtspsrc_print_rtsp_message (GstRTSPSrc * src, const GstRTSPMessage * msg)
8733 GString *body_string = NULL;
8735 g_return_if_fail (src != NULL);
8736 g_return_if_fail (msg != NULL);
8738 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) < GST_LEVEL_LOG)
8741 GST_LOG_OBJECT (src, "--------------------------------------------");
8742 switch (msg->type) {
8743 case GST_RTSP_MESSAGE_REQUEST:
8744 GST_LOG_OBJECT (src, "RTSP request message %p", msg);
8745 GST_LOG_OBJECT (src, " request line:");
8746 GST_LOG_OBJECT (src, " method: '%s'",
8747 gst_rtsp_method_as_text (msg->type_data.request.method));
8748 GST_LOG_OBJECT (src, " uri: '%s'", msg->type_data.request.uri);
8749 GST_LOG_OBJECT (src, " version: '%s'",
8750 gst_rtsp_version_as_text (msg->type_data.request.version));
8751 GST_LOG_OBJECT (src, " headers:");
8752 key_value_foreach (msg->hdr_fields, dump_key_value, src);
8753 GST_LOG_OBJECT (src, " body:");
8754 gst_rtsp_message_get_body (msg, &data, &size);
8756 body_string = g_string_new_len ((const gchar *) data, size);
8757 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
8758 g_string_free (body_string, TRUE);
8762 case GST_RTSP_MESSAGE_RESPONSE:
8763 GST_LOG_OBJECT (src, "RTSP response message %p", msg);
8764 GST_LOG_OBJECT (src, " status line:");
8765 GST_LOG_OBJECT (src, " code: '%d'", msg->type_data.response.code);
8766 GST_LOG_OBJECT (src, " reason: '%s'", msg->type_data.response.reason);
8767 GST_LOG_OBJECT (src, " version: '%s",
8768 gst_rtsp_version_as_text (msg->type_data.response.version));
8769 GST_LOG_OBJECT (src, " headers:");
8770 key_value_foreach (msg->hdr_fields, dump_key_value, src);
8771 gst_rtsp_message_get_body (msg, &data, &size);
8772 GST_LOG_OBJECT (src, " body: length %d", size);
8774 body_string = g_string_new_len ((const gchar *) data, size);
8775 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
8776 g_string_free (body_string, TRUE);
8780 case GST_RTSP_MESSAGE_HTTP_REQUEST:
8781 GST_LOG_OBJECT (src, "HTTP request message %p", msg);
8782 GST_LOG_OBJECT (src, " request line:");
8783 GST_LOG_OBJECT (src, " method: '%s'",
8784 gst_rtsp_method_as_text (msg->type_data.request.method));
8785 GST_LOG_OBJECT (src, " uri: '%s'", msg->type_data.request.uri);
8786 GST_LOG_OBJECT (src, " version: '%s'",
8787 gst_rtsp_version_as_text (msg->type_data.request.version));
8788 GST_LOG_OBJECT (src, " headers:");
8789 key_value_foreach (msg->hdr_fields, dump_key_value, src);
8790 GST_LOG_OBJECT (src, " body:");
8791 gst_rtsp_message_get_body (msg, &data, &size);
8793 body_string = g_string_new_len ((const gchar *) data, size);
8794 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
8795 g_string_free (body_string, TRUE);
8799 case GST_RTSP_MESSAGE_HTTP_RESPONSE:
8800 GST_LOG_OBJECT (src, "HTTP response message %p", msg);
8801 GST_LOG_OBJECT (src, " status line:");
8802 GST_LOG_OBJECT (src, " code: '%d'", msg->type_data.response.code);
8803 GST_LOG_OBJECT (src, " reason: '%s'", msg->type_data.response.reason);
8804 GST_LOG_OBJECT (src, " version: '%s'",
8805 gst_rtsp_version_as_text (msg->type_data.response.version));
8806 GST_LOG_OBJECT (src, " headers:");
8807 key_value_foreach (msg->hdr_fields, dump_key_value, src);
8808 gst_rtsp_message_get_body (msg, &data, &size);
8809 GST_LOG_OBJECT (src, " body: length %d", size);
8811 body_string = g_string_new_len ((const gchar *) data, size);
8812 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
8813 g_string_free (body_string, TRUE);
8817 case GST_RTSP_MESSAGE_DATA:
8818 GST_LOG_OBJECT (src, "RTSP data message %p", msg);
8819 GST_LOG_OBJECT (src, " channel: '%d'", msg->type_data.data.channel);
8820 GST_LOG_OBJECT (src, " size: '%d'", msg->body_size);
8821 gst_rtsp_message_get_body (msg, &data, &size);
8823 body_string = g_string_new_len ((const gchar *) data, size);
8824 GST_LOG_OBJECT (src, " %s(%d)", body_string->str, size);
8825 g_string_free (body_string, TRUE);
8830 GST_LOG_OBJECT (src, "unsupported message type %d", msg->type);
8833 GST_LOG_OBJECT (src, "--------------------------------------------");
8837 gst_rtspsrc_print_sdp_media (GstRTSPSrc * src, GstSDPMedia * media)
8839 GST_LOG_OBJECT (src, " media: '%s'", GST_STR_NULL (media->media));
8840 GST_LOG_OBJECT (src, " port: '%u'", media->port);
8841 GST_LOG_OBJECT (src, " num_ports: '%u'", media->num_ports);
8842 GST_LOG_OBJECT (src, " proto: '%s'", GST_STR_NULL (media->proto));
8843 if (media->fmts && media->fmts->len > 0) {
8846 GST_LOG_OBJECT (src, " formats:");
8847 for (i = 0; i < media->fmts->len; i++) {
8848 GST_LOG_OBJECT (src, " format '%s'", g_array_index (media->fmts,
8852 GST_LOG_OBJECT (src, " information: '%s'",
8853 GST_STR_NULL (media->information));
8854 if (media->connections && media->connections->len > 0) {
8857 GST_LOG_OBJECT (src, " connections:");
8858 for (i = 0; i < media->connections->len; i++) {
8859 GstSDPConnection *conn =
8860 &g_array_index (media->connections, GstSDPConnection, i);
8862 GST_LOG_OBJECT (src, " nettype: '%s'",
8863 GST_STR_NULL (conn->nettype));
8864 GST_LOG_OBJECT (src, " addrtype: '%s'",
8865 GST_STR_NULL (conn->addrtype));
8866 GST_LOG_OBJECT (src, " address: '%s'",
8867 GST_STR_NULL (conn->address));
8868 GST_LOG_OBJECT (src, " ttl: '%u'", conn->ttl);
8869 GST_LOG_OBJECT (src, " addr_number: '%u'", conn->addr_number);
8872 if (media->bandwidths && media->bandwidths->len > 0) {
8875 GST_LOG_OBJECT (src, " bandwidths:");
8876 for (i = 0; i < media->bandwidths->len; i++) {
8877 GstSDPBandwidth *bw =
8878 &g_array_index (media->bandwidths, GstSDPBandwidth, i);
8880 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (bw->bwtype));
8881 GST_LOG_OBJECT (src, " bandwidth: '%u'", bw->bandwidth);
8884 GST_LOG_OBJECT (src, " key:");
8885 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (media->key.type));
8886 GST_LOG_OBJECT (src, " data: '%s'", GST_STR_NULL (media->key.data));
8887 if (media->attributes && media->attributes->len > 0) {
8890 GST_LOG_OBJECT (src, " attributes:");
8891 for (i = 0; i < media->attributes->len; i++) {
8892 GstSDPAttribute *attr =
8893 &g_array_index (media->attributes, GstSDPAttribute, i);
8895 GST_LOG_OBJECT (src, " attribute '%s' : '%s'", attr->key, attr->value);
8901 gst_rtspsrc_print_sdp_message (GstRTSPSrc * src, const GstSDPMessage * msg)
8903 g_return_if_fail (src != NULL);
8904 g_return_if_fail (msg != NULL);
8906 if (gst_debug_category_get_threshold (GST_CAT_DEFAULT) < GST_LEVEL_LOG)
8909 GST_LOG_OBJECT (src, "--------------------------------------------");
8910 GST_LOG_OBJECT (src, "sdp packet %p:", msg);
8911 GST_LOG_OBJECT (src, " version: '%s'", GST_STR_NULL (msg->version));
8912 GST_LOG_OBJECT (src, " origin:");
8913 GST_LOG_OBJECT (src, " username: '%s'",
8914 GST_STR_NULL (msg->origin.username));
8915 GST_LOG_OBJECT (src, " sess_id: '%s'",
8916 GST_STR_NULL (msg->origin.sess_id));
8917 GST_LOG_OBJECT (src, " sess_version: '%s'",
8918 GST_STR_NULL (msg->origin.sess_version));
8919 GST_LOG_OBJECT (src, " nettype: '%s'",
8920 GST_STR_NULL (msg->origin.nettype));
8921 GST_LOG_OBJECT (src, " addrtype: '%s'",
8922 GST_STR_NULL (msg->origin.addrtype));
8923 GST_LOG_OBJECT (src, " addr: '%s'", GST_STR_NULL (msg->origin.addr));
8924 GST_LOG_OBJECT (src, " session_name: '%s'",
8925 GST_STR_NULL (msg->session_name));
8926 GST_LOG_OBJECT (src, " information: '%s'", GST_STR_NULL (msg->information));
8927 GST_LOG_OBJECT (src, " uri: '%s'", GST_STR_NULL (msg->uri));
8929 if (msg->emails && msg->emails->len > 0) {
8932 GST_LOG_OBJECT (src, " emails:");
8933 for (i = 0; i < msg->emails->len; i++) {
8934 GST_LOG_OBJECT (src, " email '%s'", g_array_index (msg->emails, gchar *,
8938 if (msg->phones && msg->phones->len > 0) {
8941 GST_LOG_OBJECT (src, " phones:");
8942 for (i = 0; i < msg->phones->len; i++) {
8943 GST_LOG_OBJECT (src, " phone '%s'", g_array_index (msg->phones, gchar *,
8947 GST_LOG_OBJECT (src, " connection:");
8948 GST_LOG_OBJECT (src, " nettype: '%s'",
8949 GST_STR_NULL (msg->connection.nettype));
8950 GST_LOG_OBJECT (src, " addrtype: '%s'",
8951 GST_STR_NULL (msg->connection.addrtype));
8952 GST_LOG_OBJECT (src, " address: '%s'",
8953 GST_STR_NULL (msg->connection.address));
8954 GST_LOG_OBJECT (src, " ttl: '%u'", msg->connection.ttl);
8955 GST_LOG_OBJECT (src, " addr_number: '%u'", msg->connection.addr_number);
8956 if (msg->bandwidths && msg->bandwidths->len > 0) {
8959 GST_LOG_OBJECT (src, " bandwidths:");
8960 for (i = 0; i < msg->bandwidths->len; i++) {
8961 GstSDPBandwidth *bw =
8962 &g_array_index (msg->bandwidths, GstSDPBandwidth, i);
8964 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (bw->bwtype));
8965 GST_LOG_OBJECT (src, " bandwidth: '%u'", bw->bandwidth);
8968 GST_LOG_OBJECT (src, " key:");
8969 GST_LOG_OBJECT (src, " type: '%s'", GST_STR_NULL (msg->key.type));
8970 GST_LOG_OBJECT (src, " data: '%s'", GST_STR_NULL (msg->key.data));
8971 if (msg->attributes && msg->attributes->len > 0) {
8974 GST_LOG_OBJECT (src, " attributes:");
8975 for (i = 0; i < msg->attributes->len; i++) {
8976 GstSDPAttribute *attr =
8977 &g_array_index (msg->attributes, GstSDPAttribute, i);
8979 GST_LOG_OBJECT (src, " attribute '%s' : '%s'", attr->key, attr->value);
8982 if (msg->medias && msg->medias->len > 0) {
8985 GST_LOG_OBJECT (src, " medias:");
8986 for (i = 0; i < msg->medias->len; i++) {
8987 GST_LOG_OBJECT (src, " media %u:", i);
8988 gst_rtspsrc_print_sdp_media (src, &g_array_index (msg->medias,
8992 GST_LOG_OBJECT (src, "--------------------------------------------");