2 * Copyright (C) <2005,2006> Wim Taymans <wim at fluendo dot com>
3 * <2006> Lutz Mueller <lutz at topfrose dot de>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
21 * Unless otherwise indicated, Source Code is licensed under MIT license.
22 * See further explanation attached in License Statement (distributed in the file
25 * Permission is hereby granted, free of charge, to any person obtaining a copy of
26 * this software and associated documentation files (the "Software"), to deal in
27 * the Software without restriction, including without limitation the rights to
28 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
29 * of the Software, and to permit persons to whom the Software is furnished to do
30 * so, subject to the following conditions:
32 * The above copyright notice and this permission notice shall be included in all
33 * copies or substantial portions of the Software.
35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
44 * SECTION:element-rtspsrc
46 * Makes a connection to an RTSP server and read the data.
47 * rtspsrc strictly follows RFC 2326 and therefore does not (yet) support
48 * RealMedia/Quicktime/Microsoft extensions.
50 * RTSP supports transport over TCP or UDP in unicast or multicast mode. By
51 * default rtspsrc will negotiate a connection in the following order:
52 * UDP unicast/UDP multicast/TCP. The order cannot be changed but the allowed
53 * protocols can be controlled with the #GstRTSPSrc:protocols property.
55 * rtspsrc currently understands SDP as the format of the session description.
56 * For each stream listed in the SDP a new rtp_stream\%d pad will be created
57 * with caps derived from the SDP media description. This is a caps of mime type
58 * "application/x-rtp" that can be connected to any available RTP depayloader
61 * rtspsrc will internally instantiate an RTP session manager element
62 * that will handle the RTCP messages to and from the server, jitter removal,
63 * packet reordering along with providing a clock for the pipeline.
64 * This feature is implemented using the gstrtpbin element.
66 * rtspsrc acts like a live source and will therefore only generate data in the
70 * <title>Example launch line</title>
72 * gst-launch-1.0 rtspsrc location=rtsp://some.server/url ! fakesink
73 * ]| Establish a connection to an RTSP server and send the raw RTP packets to a
84 #endif /* HAVE_UNISTD_H */
90 #include <gst/net/gstnet.h>
91 #include <gst/sdp/gstsdpmessage.h>
92 #include <gst/sdp/gstmikey.h>
93 #include <gst/rtp/rtp.h>
95 #include "gst/gst-i18n-plugin.h"
97 #include "gstrtspsrc.h"
99 GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
100 #define GST_CAT_DEFAULT (rtspsrc_debug)
102 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream_%u",
105 GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
107 /* templates used internally */
108 static GstStaticPadTemplate anysrctemplate =
109 GST_STATIC_PAD_TEMPLATE ("internalsrc_%u",
112 GST_STATIC_CAPS_ANY);
114 static GstStaticPadTemplate anysinktemplate =
115 GST_STATIC_PAD_TEMPLATE ("internalsink_%u",
118 GST_STATIC_CAPS_ANY);
122 SIGNAL_HANDLE_REQUEST,
124 SIGNAL_SELECT_STREAM,
126 SIGNAL_REQUEST_RTCP_KEY,
130 enum _GstRtspSrcRtcpSyncMode
137 enum _GstRtspSrcBufferMode
146 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
148 gst_rtsp_src_buffer_mode_get_type (void)
150 static GType buffer_mode_type = 0;
151 static const GEnumValue buffer_modes[] = {
152 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
153 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
154 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
155 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
156 {BUFFER_MODE_SYNCED, "Synchronized sender and receiver clocks", "synced"},
160 if (!buffer_mode_type) {
162 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
164 return buffer_mode_type;
167 enum _GstRtspSrcNtpTimeSource
170 NTP_TIME_SOURCE_UNIX,
171 NTP_TIME_SOURCE_RUNNING_TIME,
172 NTP_TIME_SOURCE_CLOCK_TIME
175 #define GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE (gst_rtsp_src_ntp_time_source_get_type())
177 gst_rtsp_src_ntp_time_source_get_type (void)
179 static GType ntp_time_source_type = 0;
180 static const GEnumValue ntp_time_source_values[] = {
181 {NTP_TIME_SOURCE_NTP, "NTP time based on realtime clock", "ntp"},
182 {NTP_TIME_SOURCE_UNIX, "UNIX time based on realtime clock", "unix"},
183 {NTP_TIME_SOURCE_RUNNING_TIME,
184 "Running time based on pipeline clock",
186 {NTP_TIME_SOURCE_CLOCK_TIME, "Pipeline clock time", "clock-time"},
190 if (!ntp_time_source_type) {
191 ntp_time_source_type =
192 g_enum_register_static ("GstRTSPSrcNtpTimeSource",
193 ntp_time_source_values);
195 return ntp_time_source_type;
198 #define DEFAULT_LOCATION NULL
199 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
200 #define DEFAULT_DEBUG FALSE
201 #define DEFAULT_RETRY 20
202 #define DEFAULT_TIMEOUT 5000000
203 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
204 #define DEFAULT_TCP_TIMEOUT 20000000
205 #define DEFAULT_LATENCY_MS 2000
206 #define DEFAULT_DROP_ON_LATENCY FALSE
207 #define DEFAULT_CONNECTION_SPEED 0
208 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
209 #define DEFAULT_DO_RTCP TRUE
210 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
211 #define DEFAULT_PROXY NULL
212 #define DEFAULT_RTP_BLOCKSIZE 0
213 #define DEFAULT_USER_ID NULL
214 #define DEFAULT_USER_PW NULL
215 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
216 #define DEFAULT_PORT_RANGE NULL
217 #define DEFAULT_SHORT_HEADER FALSE
218 #define DEFAULT_PROBATION 2
219 #define DEFAULT_UDP_RECONNECT TRUE
220 #define DEFAULT_MULTICAST_IFACE NULL
221 #define DEFAULT_NTP_SYNC FALSE
222 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
223 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
224 #define DEFAULT_TLS_DATABASE NULL
225 #define DEFAULT_TLS_INTERACTION NULL
226 #define DEFAULT_DO_RETRANSMISSION TRUE
227 #define DEFAULT_NTP_TIME_SOURCE NTP_TIME_SOURCE_NTP
228 #define DEFAULT_USER_AGENT "GStreamer/" PACKAGE_VERSION
229 #define DEFAULT_MAX_RTCP_RTP_TIME_DIFF 1000
230 #define DEFAULT_RFC7273_SYNC FALSE
231 #define DEFAULT_MAX_TS_OFFSET_ADJUSTMENT 0
243 PROP_DROP_ON_LATENCY,
244 PROP_CONNECTION_SPEED,
247 PROP_DO_RTSP_KEEP_ALIVE,
256 PROP_UDP_BUFFER_SIZE,
260 PROP_MULTICAST_IFACE,
262 PROP_USE_PIPELINE_CLOCK,
264 PROP_TLS_VALIDATION_FLAGS,
266 PROP_TLS_INTERACTION,
267 PROP_DO_RETRANSMISSION,
268 PROP_NTP_TIME_SOURCE,
270 PROP_MAX_RTCP_RTP_TIME_DIFF,
272 PROP_MAX_TS_OFFSET_ADJUSTMENT
275 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
277 gst_rtsp_nat_method_get_type (void)
279 static GType rtsp_nat_method_type = 0;
280 static const GEnumValue rtsp_nat_method[] = {
281 {GST_RTSP_NAT_NONE, "None", "none"},
282 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
286 if (!rtsp_nat_method_type) {
287 rtsp_nat_method_type =
288 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
290 return rtsp_nat_method_type;
293 #define RTSP_SRC_RESPONSE_ERROR(src, response_msg, err_cat, err_code, error_message) \
295 GST_ELEMENT_ERROR_WITH_DETAILS((src), err_cat, err_code, ("%s", error_message), \
296 ("%s (%d)", (response_msg)->type_data.response.reason, (response_msg)->type_data.response.code), \
297 ("rtsp-status-code", G_TYPE_UINT, (response_msg)->type_data.response.code, \
298 "rtsp-status-reason", G_TYPE_STRING, GST_STR_NULL((response_msg)->type_data.response.reason), NULL)); \
301 static void gst_rtspsrc_finalize (GObject * object);
303 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
304 const GValue * value, GParamSpec * pspec);
305 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
306 GValue * value, GParamSpec * pspec);
308 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
310 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
311 gpointer iface_data);
313 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
314 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
316 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
317 GstStateChange transition);
318 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
319 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
321 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
322 GstRTSPMessage * response);
324 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
326 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
327 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
329 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
330 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
332 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
333 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
334 gboolean only_close);
336 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
337 const gchar * uri, GError ** error);
338 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
340 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
341 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
342 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
343 GstRTSPStream * stream, GstEvent * event);
344 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
345 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
346 static GstRTSPResult gst_rtsp_conninfo_close (GstRTSPSrc * src,
347 GstRTSPConnInfo * info, gboolean free);
355 /* commands we send to out loop to notify it of events */
356 #define CMD_OPEN (1 << 0)
357 #define CMD_PLAY (1 << 1)
358 #define CMD_PAUSE (1 << 2)
359 #define CMD_CLOSE (1 << 3)
360 #define CMD_WAIT (1 << 4)
361 #define CMD_RECONNECT (1 << 5)
362 #define CMD_LOOP (1 << 6)
364 /* mask for all commands */
365 #define CMD_ALL ((CMD_LOOP << 1) - 1)
367 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
369 gchar *__txt = _gst_element_error_printf text; \
370 gst_element_post_message (GST_ELEMENT_CAST (el), \
371 gst_message_new_progress (GST_OBJECT_CAST (el), \
372 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
376 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
378 #define gst_rtspsrc_parent_class parent_class
379 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
380 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
382 #ifndef GST_DISABLE_GST_DEBUG
383 static inline const char *
384 cmd_to_string (guint cmd)
408 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
410 GST_DEBUG_OBJECT (src, "default handler");
415 select_stream_accum (GSignalInvocationHint * ihint,
416 GValue * return_accu, const GValue * handler_return, gpointer data)
420 myboolean = g_value_get_boolean (handler_return);
421 GST_DEBUG ("accum %d", myboolean);
422 g_value_set_boolean (return_accu, myboolean);
424 /* stop emission if FALSE */
429 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
431 GObjectClass *gobject_class;
432 GstElementClass *gstelement_class;
433 GstBinClass *gstbin_class;
435 gobject_class = (GObjectClass *) klass;
436 gstelement_class = (GstElementClass *) klass;
437 gstbin_class = (GstBinClass *) klass;
439 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
441 gobject_class->set_property = gst_rtspsrc_set_property;
442 gobject_class->get_property = gst_rtspsrc_get_property;
444 gobject_class->finalize = gst_rtspsrc_finalize;
446 g_object_class_install_property (gobject_class, PROP_LOCATION,
447 g_param_spec_string ("location", "RTSP Location",
448 "Location of the RTSP url to read",
449 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
451 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
452 g_param_spec_flags ("protocols", "Protocols",
453 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
454 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
456 g_object_class_install_property (gobject_class, PROP_DEBUG,
457 g_param_spec_boolean ("debug", "Debug",
458 "Dump request and response messages to stdout",
459 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
461 g_object_class_install_property (gobject_class, PROP_RETRY,
462 g_param_spec_uint ("retry", "Retry",
463 "Max number of retries when allocating RTP ports.",
464 0, G_MAXUINT16, DEFAULT_RETRY,
465 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
467 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
468 g_param_spec_uint64 ("timeout", "Timeout",
469 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
470 0, G_MAXUINT64, DEFAULT_TIMEOUT,
471 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
473 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
474 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
475 "Fail after timeout microseconds on TCP connections (0 = disabled)",
476 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
477 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
479 g_object_class_install_property (gobject_class, PROP_LATENCY,
480 g_param_spec_uint ("latency", "Buffer latency in ms",
481 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
482 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
484 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
485 g_param_spec_boolean ("drop-on-latency",
486 "Drop buffers when maximum latency is reached",
487 "Tells the jitterbuffer to never exceed the given latency in size",
488 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
490 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
491 g_param_spec_uint64 ("connection-speed", "Connection Speed",
492 "Network connection speed in kbps (0 = unknown)",
493 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
494 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
496 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
497 g_param_spec_enum ("nat-method", "NAT Method",
498 "Method to use for traversing firewalls and NAT",
499 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
500 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
503 * GstRTSPSrc:do-rtcp:
505 * Enable RTCP support. Some old server don't like RTCP and then this property
506 * needs to be set to FALSE.
508 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
509 g_param_spec_boolean ("do-rtcp", "Do RTCP",
510 "Send RTCP packets, disable for old incompatible server.",
511 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
514 * GstRTSPSrc:do-rtsp-keep-alive:
516 * Enable RTSP keep alive support. Some old server don't like RTSP
517 * keep alive and then this property needs to be set to FALSE.
519 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
520 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
521 "Send RTSP keep alive packets, disable for old incompatible server.",
522 DEFAULT_DO_RTSP_KEEP_ALIVE,
523 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
528 * Set the proxy parameters. This has to be a string of the format
529 * [http://][user:passwd@]host[:port].
531 g_object_class_install_property (gobject_class, PROP_PROXY,
532 g_param_spec_string ("proxy", "Proxy",
533 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
534 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
536 * GstRTSPSrc:proxy-id:
538 * Sets the proxy URI user id for authentication. If the URI set via the
539 * "proxy" property contains a user-id already, that will take precedence.
543 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
544 g_param_spec_string ("proxy-id", "proxy-id",
545 "HTTP proxy URI user id for authentication", "",
546 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
548 * GstRTSPSrc:proxy-pw:
550 * Sets the proxy URI password for authentication. If the URI set via the
551 * "proxy" property contains a password already, that will take precedence.
555 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
556 g_param_spec_string ("proxy-pw", "proxy-pw",
557 "HTTP proxy URI user password for authentication", "",
558 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
561 * GstRTSPSrc:rtp-blocksize:
563 * RTP package size to suggest to server.
565 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
566 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
567 "RTP package size to suggest to server (0 = disabled)",
568 0, 65536, DEFAULT_RTP_BLOCKSIZE,
569 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
571 g_object_class_install_property (gobject_class,
573 g_param_spec_string ("user-id", "user-id",
574 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
575 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
576 g_object_class_install_property (gobject_class, PROP_USER_PW,
577 g_param_spec_string ("user-pw", "user-pw",
578 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
579 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
582 * GstRTSPSrc:buffer-mode:
584 * Control the buffering and timestamping mode used by the jitterbuffer.
586 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
587 g_param_spec_enum ("buffer-mode", "Buffer Mode",
588 "Control the buffering algorithm in use",
589 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
590 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
593 * GstRTSPSrc:port-range:
595 * Configure the client port numbers that can be used to recieve RTP and
598 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
599 g_param_spec_string ("port-range", "Port range",
600 "Client port range that can be used to receive RTP and RTCP data, "
601 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
602 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
605 * GstRTSPSrc:udp-buffer-size:
607 * Size of the kernel UDP receive buffer in bytes.
609 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
610 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
611 "Size of the kernel UDP receive buffer in bytes, 0=default",
612 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
613 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
616 * GstRTSPSrc:short-header:
618 * Only send the basic RTSP headers for broken encoders.
620 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
621 g_param_spec_boolean ("short-header", "Short Header",
622 "Only send the basic RTSP headers for broken encoders",
623 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
625 g_object_class_install_property (gobject_class, PROP_PROBATION,
626 g_param_spec_uint ("probation", "Number of probations",
627 "Consecutive packet sequence numbers to accept the source",
628 0, G_MAXUINT, DEFAULT_PROBATION,
629 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
631 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
632 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
633 "Reconnect to the server if RTSP connection is closed when doing UDP",
634 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
636 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
637 g_param_spec_string ("multicast-iface", "Multicast Interface",
638 "The network interface on which to join the multicast group",
639 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
641 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
642 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
643 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
644 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
646 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
647 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
648 "Use the pipeline running-time to set the NTP time in the RTCP SR messages"
649 "(DEPRECATED: Use ntp-time-source property)",
650 DEFAULT_USE_PIPELINE_CLOCK,
651 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
653 g_object_class_install_property (gobject_class, PROP_SDES,
654 g_param_spec_boxed ("sdes", "SDES",
655 "The SDES items of this session",
656 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
659 * GstRTSPSrc::tls-validation-flags:
661 * TLS certificate validation flags used to validate server
666 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
667 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
668 "TLS certificate validation flags used to validate the server certificate",
669 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
670 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
673 * GstRTSPSrc::tls-database:
675 * TLS database with anchor certificate authorities used to validate
676 * the server certificate.
680 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
681 g_param_spec_object ("tls-database", "TLS database",
682 "TLS database with anchor certificate authorities used to validate the server certificate",
683 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
686 * GstRTSPSrc::tls-interaction:
688 * A #GTlsInteraction object to be used when the connection or certificate
689 * database need to interact with the user. This will be used to prompt the
690 * user for passwords where necessary.
694 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
695 g_param_spec_object ("tls-interaction", "TLS interaction",
696 "A GTlsInteraction object to promt the user for password or certificate",
697 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
700 * GstRTSPSrc::do-retransmission:
702 * Attempt to ask the server to retransmit lost packets according to RFC4588.
704 * Note: currently only works with SSRC-multiplexed retransmission streams
708 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
709 g_param_spec_boolean ("do-retransmission", "Retransmission",
710 "Ask the server to retransmit lost packets",
711 DEFAULT_DO_RETRANSMISSION,
712 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
715 * GstRTSPSrc::ntp-time-source:
717 * allows to select the time source that should be used
718 * for the NTP time in RTCP packets
722 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
723 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
724 "NTP time source for RTCP packets",
725 GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE, DEFAULT_NTP_TIME_SOURCE,
726 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
729 * GstRTSPSrc::user-agent:
731 * The string to set in the User-Agent header.
735 g_object_class_install_property (gobject_class, PROP_USER_AGENT,
736 g_param_spec_string ("user-agent", "User Agent",
737 "The User-Agent string to send to the server",
738 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
740 g_object_class_install_property (gobject_class, PROP_MAX_RTCP_RTP_TIME_DIFF,
741 g_param_spec_int ("max-rtcp-rtp-time-diff", "Max RTCP RTP Time Diff",
742 "Maximum amount of time in ms that the RTP time in RTCP SRs "
743 "is allowed to be ahead (-1 disabled)", -1, G_MAXINT,
744 DEFAULT_MAX_RTCP_RTP_TIME_DIFF,
745 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
747 g_object_class_install_property (gobject_class, PROP_RFC7273_SYNC,
748 g_param_spec_boolean ("rfc7273-sync", "Sync on RFC7273 clock",
749 "Synchronize received streams to the RFC7273 clock "
750 "(requires clock and offset to be provided)", DEFAULT_RFC7273_SYNC,
751 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
754 * GstRTSPSrc:max-ts-offset-adjustment:
756 * Syncing time stamps to NTP time adds a time offset. This parameter
757 * specifies the maximum number of nanoseconds per frame that this time offset
758 * may be adjusted with. This is used to avoid sudden large changes to time
761 g_object_class_install_property (gobject_class, PROP_MAX_TS_OFFSET_ADJUSTMENT,
762 g_param_spec_uint64 ("max-ts-offset-adjustment",
763 "Max Timestamp Offset Adjustment",
764 "The maximum number of nanoseconds per frame that time stamp offsets "
765 "may be adjusted (0 = no limit).", 0, G_MAXUINT64,
766 DEFAULT_MAX_TS_OFFSET_ADJUSTMENT, G_PARAM_READWRITE |
767 G_PARAM_STATIC_STRINGS));
770 * GstRTSPSrc::handle-request:
771 * @rtspsrc: a #GstRTSPSrc
772 * @request: a #GstRTSPMessage
773 * @response: a #GstRTSPMessage
775 * Handle a server request in @request and prepare @response.
777 * This signal is called from the streaming thread, you should therefore not
778 * do any state changes on @rtspsrc because this might deadlock. If you want
779 * to modify the state as a result of this signal, post a
780 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
785 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
786 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
787 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
788 G_TYPE_POINTER, G_TYPE_POINTER);
791 * GstRTSPSrc::on-sdp:
792 * @rtspsrc: a #GstRTSPSrc
793 * @sdp: a #GstSDPMessage
795 * Emited when the client has retrieved the SDP and before it configures the
796 * streams in the SDP. @sdp can be inspected and modified.
798 * This signal is called from the streaming thread, you should therefore not
799 * do any state changes on @rtspsrc because this might deadlock. If you want
800 * to modify the state as a result of this signal, post a
801 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
806 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
807 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
808 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
809 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
812 * GstRTSPSrc::select-stream:
813 * @rtspsrc: a #GstRTSPSrc
814 * @num: the stream number
815 * @caps: the stream caps
817 * Emited before the client decides to configure the stream @num with
820 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
825 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
826 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
827 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
828 (GCallback) default_select_stream, select_stream_accum, NULL,
829 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
832 * GstRTSPSrc::new-manager:
833 * @rtspsrc: a #GstRTSPSrc
834 * @manager: a #GstElement
836 * Emited after a new manager (like rtpbin) was created and the default
837 * properties were configured.
841 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
842 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
843 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
844 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
847 * GstRTSPSrc::request-rtcp-key:
848 * @rtspsrc: a #GstRTSPSrc
849 * @num: the stream number
851 * Signal emited to get the crypto parameters relevant to the RTCP
852 * stream. User should provide the key and the RTCP encryption ciphers
853 * and authentication, and return them wrapped in a GstCaps.
857 gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY] =
858 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
859 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
861 gstelement_class->send_event = gst_rtspsrc_send_event;
862 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
863 gstelement_class->change_state = gst_rtspsrc_change_state;
865 gst_element_class_add_static_pad_template (gstelement_class, &rtptemplate);
867 gst_element_class_set_static_metadata (gstelement_class,
868 "RTSP packet receiver", "Source/Network",
869 "Receive data over the network via RTSP (RFC 2326)",
870 "Wim Taymans <wim@fluendo.com>, "
871 "Thijs Vermeir <thijs.vermeir@barco.com>, "
872 "Lutz Mueller <lutz@topfrose.de>");
874 gstbin_class->handle_message = gst_rtspsrc_handle_message;
876 gst_rtsp_ext_list_init ();
880 gst_rtspsrc_init (GstRTSPSrc * src)
882 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
883 src->protocols = DEFAULT_PROTOCOLS;
884 src->debug = DEFAULT_DEBUG;
885 src->retry = DEFAULT_RETRY;
886 src->udp_timeout = DEFAULT_TIMEOUT;
887 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
888 src->latency = DEFAULT_LATENCY_MS;
889 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
890 src->connection_speed = DEFAULT_CONNECTION_SPEED;
891 src->nat_method = DEFAULT_NAT_METHOD;
892 src->do_rtcp = DEFAULT_DO_RTCP;
893 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
894 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
895 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
896 src->user_id = g_strdup (DEFAULT_USER_ID);
897 src->user_pw = g_strdup (DEFAULT_USER_PW);
898 src->buffer_mode = DEFAULT_BUFFER_MODE;
899 src->client_port_range.min = 0;
900 src->client_port_range.max = 0;
901 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
902 src->short_header = DEFAULT_SHORT_HEADER;
903 src->probation = DEFAULT_PROBATION;
904 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
905 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
906 src->ntp_sync = DEFAULT_NTP_SYNC;
907 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
909 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
910 src->tls_database = DEFAULT_TLS_DATABASE;
911 src->tls_interaction = DEFAULT_TLS_INTERACTION;
912 src->do_retransmission = DEFAULT_DO_RETRANSMISSION;
913 src->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
914 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
915 src->max_rtcp_rtp_time_diff = DEFAULT_MAX_RTCP_RTP_TIME_DIFF;
916 src->rfc7273_sync = DEFAULT_RFC7273_SYNC;
917 src->max_ts_offset_adjustment = DEFAULT_MAX_TS_OFFSET_ADJUSTMENT;
919 /* get a list of all extensions */
920 src->extensions = gst_rtsp_ext_list_get ();
922 /* connect to send signal */
923 gst_rtsp_ext_list_connect (src->extensions, "send",
924 (GCallback) gst_rtspsrc_send_cb, src);
926 /* protects the streaming thread in interleaved mode or the polling
927 * thread in UDP mode. */
928 g_rec_mutex_init (&src->stream_rec_lock);
930 /* protects our state changes from multiple invocations */
931 g_rec_mutex_init (&src->state_rec_lock);
933 src->state = GST_RTSP_STATE_INVALID;
935 g_mutex_init (&src->conninfo.send_lock);
936 g_mutex_init (&src->conninfo.recv_lock);
938 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
939 gst_bin_set_suppressed_flags (GST_BIN (src),
940 GST_ELEMENT_FLAG_SOURCE | GST_ELEMENT_FLAG_SINK);
944 gst_rtspsrc_finalize (GObject * object)
948 rtspsrc = GST_RTSPSRC (object);
950 gst_rtsp_ext_list_free (rtspsrc->extensions);
951 g_free (rtspsrc->conninfo.location);
952 gst_rtsp_url_free (rtspsrc->conninfo.url);
953 g_free (rtspsrc->conninfo.url_str);
954 g_free (rtspsrc->user_id);
955 g_free (rtspsrc->user_pw);
956 g_free (rtspsrc->multi_iface);
957 g_free (rtspsrc->user_agent);
960 gst_sdp_message_free (rtspsrc->sdp);
963 if (rtspsrc->provided_clock)
964 gst_object_unref (rtspsrc->provided_clock);
967 gst_structure_free (rtspsrc->sdes);
969 if (rtspsrc->tls_database)
970 g_object_unref (rtspsrc->tls_database);
972 if (rtspsrc->tls_interaction)
973 g_object_unref (rtspsrc->tls_interaction);
976 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
977 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
979 g_mutex_clear (&rtspsrc->conninfo.send_lock);
980 g_mutex_clear (&rtspsrc->conninfo.recv_lock);
982 G_OBJECT_CLASS (parent_class)->finalize (object);
986 gst_rtspsrc_provide_clock (GstElement * element)
988 GstRTSPSrc *src = GST_RTSPSRC (element);
991 if ((clock = src->provided_clock) != NULL)
992 return gst_object_ref (clock);
994 return GST_ELEMENT_CLASS (parent_class)->provide_clock (element);
997 /* a proxy string of the format [user:passwd@]host[:port] */
999 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
1001 gchar *p, *at, *col;
1003 g_free (rtsp->proxy_user);
1004 rtsp->proxy_user = NULL;
1005 g_free (rtsp->proxy_passwd);
1006 rtsp->proxy_passwd = NULL;
1007 g_free (rtsp->proxy_host);
1008 rtsp->proxy_host = NULL;
1009 rtsp->proxy_port = 0;
1011 p = (gchar *) proxy;
1016 /* we allow http:// in front but ignore it */
1017 if (g_str_has_prefix (p, "http://"))
1020 at = strchr (p, '@');
1022 /* look for user:passwd */
1023 col = strchr (proxy, ':');
1024 if (col == NULL || col > at)
1027 rtsp->proxy_user = g_strndup (p, col - p);
1029 rtsp->proxy_passwd = g_strndup (col, at - col);
1034 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
1035 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
1036 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
1037 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
1038 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
1039 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
1040 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
1043 col = strchr (p, ':');
1046 /* everything before the colon is the hostname */
1047 rtsp->proxy_host = g_strndup (p, col - p);
1049 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
1051 rtsp->proxy_host = g_strdup (p);
1052 rtsp->proxy_port = 8080;
1058 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
1060 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
1061 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
1064 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
1066 rtspsrc->ptcp_timeout = NULL;
1070 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
1073 GstRTSPSrc *rtspsrc;
1075 rtspsrc = GST_RTSPSRC (object);
1079 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
1080 g_value_get_string (value), NULL);
1082 case PROP_PROTOCOLS:
1083 rtspsrc->protocols = g_value_get_flags (value);
1086 rtspsrc->debug = g_value_get_boolean (value);
1089 rtspsrc->retry = g_value_get_uint (value);
1092 rtspsrc->udp_timeout = g_value_get_uint64 (value);
1094 case PROP_TCP_TIMEOUT:
1095 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
1098 rtspsrc->latency = g_value_get_uint (value);
1100 case PROP_DROP_ON_LATENCY:
1101 rtspsrc->drop_on_latency = g_value_get_boolean (value);
1103 case PROP_CONNECTION_SPEED:
1104 rtspsrc->connection_speed = g_value_get_uint64 (value);
1106 case PROP_NAT_METHOD:
1107 rtspsrc->nat_method = g_value_get_enum (value);
1110 rtspsrc->do_rtcp = g_value_get_boolean (value);
1112 case PROP_DO_RTSP_KEEP_ALIVE:
1113 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
1116 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
1119 g_free (rtspsrc->prop_proxy_id);
1120 rtspsrc->prop_proxy_id = g_value_dup_string (value);
1123 g_free (rtspsrc->prop_proxy_pw);
1124 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
1126 case PROP_RTP_BLOCKSIZE:
1127 rtspsrc->rtp_blocksize = g_value_get_uint (value);
1130 g_free (rtspsrc->user_id);
1131 rtspsrc->user_id = g_value_dup_string (value);
1134 g_free (rtspsrc->user_pw);
1135 rtspsrc->user_pw = g_value_dup_string (value);
1137 case PROP_BUFFER_MODE:
1138 rtspsrc->buffer_mode = g_value_get_enum (value);
1140 case PROP_PORT_RANGE:
1144 str = g_value_get_string (value);
1145 if (sscanf (str, "%u-%u", &rtspsrc->client_port_range.min,
1146 &rtspsrc->client_port_range.max) != 2) {
1147 rtspsrc->client_port_range.min = 0;
1148 rtspsrc->client_port_range.max = 0;
1152 case PROP_UDP_BUFFER_SIZE:
1153 rtspsrc->udp_buffer_size = g_value_get_int (value);
1155 case PROP_SHORT_HEADER:
1156 rtspsrc->short_header = g_value_get_boolean (value);
1158 case PROP_PROBATION:
1159 rtspsrc->probation = g_value_get_uint (value);
1161 case PROP_UDP_RECONNECT:
1162 rtspsrc->udp_reconnect = g_value_get_boolean (value);
1164 case PROP_MULTICAST_IFACE:
1165 g_free (rtspsrc->multi_iface);
1167 if (g_value_get_string (value) == NULL)
1168 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1170 rtspsrc->multi_iface = g_value_dup_string (value);
1173 rtspsrc->ntp_sync = g_value_get_boolean (value);
1175 case PROP_USE_PIPELINE_CLOCK:
1176 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
1179 rtspsrc->sdes = g_value_dup_boxed (value);
1181 case PROP_TLS_VALIDATION_FLAGS:
1182 rtspsrc->tls_validation_flags = g_value_get_flags (value);
1184 case PROP_TLS_DATABASE:
1185 g_clear_object (&rtspsrc->tls_database);
1186 rtspsrc->tls_database = g_value_dup_object (value);
1188 case PROP_TLS_INTERACTION:
1189 g_clear_object (&rtspsrc->tls_interaction);
1190 rtspsrc->tls_interaction = g_value_dup_object (value);
1192 case PROP_DO_RETRANSMISSION:
1193 rtspsrc->do_retransmission = g_value_get_boolean (value);
1195 case PROP_NTP_TIME_SOURCE:
1196 rtspsrc->ntp_time_source = g_value_get_enum (value);
1198 case PROP_USER_AGENT:
1199 g_free (rtspsrc->user_agent);
1200 rtspsrc->user_agent = g_value_dup_string (value);
1202 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1203 rtspsrc->max_rtcp_rtp_time_diff = g_value_get_int (value);
1205 case PROP_RFC7273_SYNC:
1206 rtspsrc->rfc7273_sync = g_value_get_boolean (value);
1208 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
1209 rtspsrc->max_ts_offset_adjustment = g_value_get_uint64 (value);
1212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1218 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1221 GstRTSPSrc *rtspsrc;
1223 rtspsrc = GST_RTSPSRC (object);
1227 g_value_set_string (value, rtspsrc->conninfo.location);
1229 case PROP_PROTOCOLS:
1230 g_value_set_flags (value, rtspsrc->protocols);
1233 g_value_set_boolean (value, rtspsrc->debug);
1236 g_value_set_uint (value, rtspsrc->retry);
1239 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1241 case PROP_TCP_TIMEOUT:
1245 timeout = ((guint64) rtspsrc->tcp_timeout.tv_sec) * G_USEC_PER_SEC +
1246 rtspsrc->tcp_timeout.tv_usec;
1247 g_value_set_uint64 (value, timeout);
1251 g_value_set_uint (value, rtspsrc->latency);
1253 case PROP_DROP_ON_LATENCY:
1254 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1256 case PROP_CONNECTION_SPEED:
1257 g_value_set_uint64 (value, rtspsrc->connection_speed);
1259 case PROP_NAT_METHOD:
1260 g_value_set_enum (value, rtspsrc->nat_method);
1263 g_value_set_boolean (value, rtspsrc->do_rtcp);
1265 case PROP_DO_RTSP_KEEP_ALIVE:
1266 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1272 if (rtspsrc->proxy_host) {
1274 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1278 g_value_take_string (value, str);
1282 g_value_set_string (value, rtspsrc->prop_proxy_id);
1285 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1287 case PROP_RTP_BLOCKSIZE:
1288 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1291 g_value_set_string (value, rtspsrc->user_id);
1294 g_value_set_string (value, rtspsrc->user_pw);
1296 case PROP_BUFFER_MODE:
1297 g_value_set_enum (value, rtspsrc->buffer_mode);
1299 case PROP_PORT_RANGE:
1303 if (rtspsrc->client_port_range.min != 0) {
1304 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1305 rtspsrc->client_port_range.max);
1309 g_value_take_string (value, str);
1312 case PROP_UDP_BUFFER_SIZE:
1313 g_value_set_int (value, rtspsrc->udp_buffer_size);
1315 case PROP_SHORT_HEADER:
1316 g_value_set_boolean (value, rtspsrc->short_header);
1318 case PROP_PROBATION:
1319 g_value_set_uint (value, rtspsrc->probation);
1321 case PROP_UDP_RECONNECT:
1322 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1324 case PROP_MULTICAST_IFACE:
1325 g_value_set_string (value, rtspsrc->multi_iface);
1328 g_value_set_boolean (value, rtspsrc->ntp_sync);
1330 case PROP_USE_PIPELINE_CLOCK:
1331 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1334 g_value_set_boxed (value, rtspsrc->sdes);
1336 case PROP_TLS_VALIDATION_FLAGS:
1337 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1339 case PROP_TLS_DATABASE:
1340 g_value_set_object (value, rtspsrc->tls_database);
1342 case PROP_TLS_INTERACTION:
1343 g_value_set_object (value, rtspsrc->tls_interaction);
1345 case PROP_DO_RETRANSMISSION:
1346 g_value_set_boolean (value, rtspsrc->do_retransmission);
1348 case PROP_NTP_TIME_SOURCE:
1349 g_value_set_enum (value, rtspsrc->ntp_time_source);
1351 case PROP_USER_AGENT:
1352 g_value_set_string (value, rtspsrc->user_agent);
1354 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1355 g_value_set_int (value, rtspsrc->max_rtcp_rtp_time_diff);
1357 case PROP_RFC7273_SYNC:
1358 g_value_set_boolean (value, rtspsrc->rfc7273_sync);
1360 case PROP_MAX_TS_OFFSET_ADJUSTMENT:
1361 g_value_set_uint64 (value, rtspsrc->max_ts_offset_adjustment);
1364 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1370 find_stream_by_id (GstRTSPStream * stream, gint * id)
1372 if (stream->id == *id)
1379 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1381 /* ignore unconfigured channels here (e.g., those that
1382 * were explicitly skipped during SETUP) */
1383 if ((stream->channelpad[0] != NULL) &&
1384 (stream->channel[0] == *channel || stream->channel[1] == *channel))
1391 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1393 GstElement *src = (GstElement *) a;
1395 if (stream->udpsrc[0] == src)
1397 if (stream->udpsrc[1] == src)
1404 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1406 if (stream->conninfo.location) {
1407 /* check qualified setup_url */
1408 if (!strcmp (stream->conninfo.location, (gchar *) a))
1411 if (stream->control_url) {
1412 /* check original control_url */
1413 if (!strcmp (stream->control_url, (gchar *) a))
1416 /* check if qualified setup_url ends with string */
1417 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1424 static GstRTSPStream *
1425 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1429 /* find and get stream */
1430 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1431 return (GstRTSPStream *) lstream->data;
1436 static const GstSDPBandwidth *
1437 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1438 const GstSDPMedia * media, const gchar * type)
1442 /* first look in the media specific section */
1443 len = gst_sdp_media_bandwidths_len (media);
1444 for (i = 0; i < len; i++) {
1445 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1447 if (strcmp (bw->bwtype, type) == 0)
1450 /* then look in the message specific section */
1451 len = gst_sdp_message_bandwidths_len (sdp);
1452 for (i = 0; i < len; i++) {
1453 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1455 if (strcmp (bw->bwtype, type) == 0)
1462 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1463 const GstSDPMedia * media, GstRTSPStream * stream)
1465 const GstSDPBandwidth *bw;
1467 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1468 stream->as_bandwidth = bw->bandwidth;
1470 stream->as_bandwidth = -1;
1472 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1473 stream->rr_bandwidth = bw->bandwidth;
1475 stream->rr_bandwidth = -1;
1477 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1478 stream->rs_bandwidth = bw->bandwidth;
1480 stream->rs_bandwidth = -1;
1484 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1485 const GstSDPConnection * conn)
1487 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1490 if (conn->addrtype == NULL)
1493 /* check for IPV6 */
1494 if (strcmp (conn->addrtype, "IP4") == 0)
1495 stream->is_ipv6 = FALSE;
1496 else if (strcmp (conn->addrtype, "IP6") == 0)
1497 stream->is_ipv6 = TRUE;
1502 g_free (stream->destination);
1503 stream->destination = g_strdup (conn->address);
1505 /* check for multicast */
1506 stream->is_multicast =
1507 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1509 stream->ttl = conn->ttl;
1512 /* Go over the connections for a stream.
1513 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1515 * - If we are dealing with a localhost address, we disable multicast
1518 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1519 const GstSDPMedia * media, GstRTSPStream * stream)
1521 const GstSDPConnection *conn;
1524 /* first look in the media specific section */
1525 len = gst_sdp_media_connections_len (media);
1526 for (i = 0; i < len; i++) {
1527 conn = gst_sdp_media_get_connection (media, i);
1529 gst_rtspsrc_do_stream_connection (src, stream, conn);
1531 /* then look in the message specific section */
1532 if ((conn = gst_sdp_message_get_connection (sdp))) {
1533 gst_rtspsrc_do_stream_connection (src, stream, conn);
1538 make_stream_id (GstRTSPStream * stream, const GstSDPMedia * media)
1541 g_strdup_printf ("%s:%d:%d:%s:%d", media->media, media->port,
1542 media->num_ports, media->proto, stream->default_pt);
1544 g_strcanon (stream_id, G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS, ':');
1549 /* m=<media> <UDP port> RTP/AVP <payload>
1552 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
1553 const GstSDPMedia * media, GstRTSPStream * stream)
1557 GstCaps *global_caps;
1560 proto = gst_sdp_media_get_proto (media);
1564 if (g_str_equal (proto, "RTP/AVP"))
1565 stream->profile = GST_RTSP_PROFILE_AVP;
1566 else if (g_str_equal (proto, "RTP/SAVP"))
1567 stream->profile = GST_RTSP_PROFILE_SAVP;
1568 else if (g_str_equal (proto, "RTP/AVPF"))
1569 stream->profile = GST_RTSP_PROFILE_AVPF;
1570 else if (g_str_equal (proto, "RTP/SAVPF"))
1571 stream->profile = GST_RTSP_PROFILE_SAVPF;
1575 /* Parse global SDP attributes once */
1576 global_caps = gst_caps_new_empty_simple ("application/x-unknown");
1577 GST_DEBUG ("mapping sdp session level attributes to caps");
1578 gst_sdp_message_attributes_to_caps (sdp, global_caps);
1579 GST_DEBUG ("mapping sdp media level attributes to caps");
1580 gst_sdp_media_attributes_to_caps (media, global_caps);
1582 /* Keep a copy of the SDP key management */
1583 gst_sdp_media_parse_keymgmt (media, &stream->mikey);
1584 if (stream->mikey == NULL)
1585 gst_sdp_message_parse_keymgmt (sdp, &stream->mikey);
1587 len = gst_sdp_media_formats_len (media);
1588 for (i = 0; i < len; i++) {
1590 GstCaps *caps, *outcaps;
1595 pt = atoi (gst_sdp_media_get_format (media, i));
1597 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
1600 caps = gst_sdp_media_get_caps_from_media (media, pt);
1602 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
1606 /* do some tweaks */
1607 s = gst_caps_get_structure (caps, 0);
1608 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
1609 stream->is_real = (strstr (enc, "-REAL") != NULL);
1610 if (strcmp (enc, "X-ASF-PF") == 0)
1611 stream->container = TRUE;
1614 /* Merge in global caps */
1615 /* Intersect will merge in missing fields to the current caps */
1616 outcaps = gst_caps_intersect (caps, global_caps);
1617 gst_caps_unref (caps);
1619 /* the first pt will be the default */
1620 if (stream->ptmap->len == 0)
1621 stream->default_pt = pt;
1624 item.caps = outcaps;
1626 g_array_append_val (stream->ptmap, item);
1629 stream->stream_id = make_stream_id (stream, media);
1631 gst_caps_unref (global_caps);
1636 GST_ERROR_OBJECT (src, "can't find proto in media");
1641 GST_ERROR_OBJECT (src, "unknown proto in media: '%s'", proto);
1646 static const gchar *
1647 get_aggregate_control (GstRTSPSrc * src)
1652 base = src->control;
1653 else if (src->content_base)
1654 base = src->content_base;
1655 else if (src->conninfo.url_str)
1656 base = src->conninfo.url_str;
1664 clear_ptmap_item (PtMapItem * item)
1667 gst_caps_unref (item->caps);
1670 static GstRTSPStream *
1671 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx,
1674 GstRTSPStream *stream;
1675 const gchar *control_url;
1676 const GstSDPMedia *media;
1678 /* get media, should not return NULL */
1679 media = gst_sdp_message_get_media (sdp, idx);
1683 stream = g_new0 (GstRTSPStream, 1);
1684 stream->parent = src;
1685 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1687 stream->last_ret = GST_FLOW_NOT_LINKED;
1688 stream->added = FALSE;
1689 stream->setup = FALSE;
1690 stream->skipped = FALSE;
1692 stream->eos = FALSE;
1693 stream->discont = TRUE;
1694 stream->seqbase = -1;
1695 stream->timebase = -1;
1696 stream->send_ssrc = g_random_int ();
1697 stream->profile = GST_RTSP_PROFILE_AVP;
1698 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1699 stream->mikey = NULL;
1700 stream->stream_id = NULL;
1701 g_mutex_init (&stream->conninfo.send_lock);
1702 g_mutex_init (&stream->conninfo.recv_lock);
1703 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
1705 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1706 * session manager to scale RTCP. */
1707 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1709 /* collect connection info */
1710 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1712 /* make the payload type map */
1713 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1715 /* collect port number */
1716 stream->port = gst_sdp_media_get_port (media);
1718 /* get control url to construct the setup url. The setup url is used to
1719 * configure the transport of the stream and is used to identity the stream in
1720 * the RTP-Info header field returned from PLAY. */
1721 control_url = gst_sdp_media_get_attribute_val (media, "control");
1722 if (control_url == NULL)
1723 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1725 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1726 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1727 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1728 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1730 /* RFC 2326, C.3: missing control_url permitted in case of a single stream */
1731 if (control_url == NULL && n_streams == 1) {
1735 if (control_url != NULL) {
1736 stream->control_url = g_strdup (control_url);
1737 /* Build a fully qualified url using the content_base if any or by prefixing
1738 * the original request.
1739 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1740 * likely build a URL that the server will fail to understand, this is ok,
1741 * we will fail then. */
1742 if (g_str_has_prefix (control_url, "rtsp://"))
1743 stream->conninfo.location = g_strdup (control_url);
1748 if (g_strcmp0 (control_url, "*") == 0)
1751 base = get_aggregate_control (src);
1753 /* check if the base ends or control starts with / */
1754 has_slash = g_str_has_prefix (control_url, "/");
1755 has_slash = has_slash || g_str_has_suffix (base, "/");
1757 /* concatenate the two strings, insert / when not present */
1758 stream->conninfo.location =
1759 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1762 GST_DEBUG_OBJECT (src, " setup: %s",
1763 GST_STR_NULL (stream->conninfo.location));
1765 /* we keep track of all streams */
1766 src->streams = g_list_append (src->streams, stream);
1774 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1778 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1780 g_array_free (stream->ptmap, TRUE);
1782 g_free (stream->destination);
1783 g_free (stream->control_url);
1784 g_free (stream->conninfo.location);
1785 g_free (stream->stream_id);
1787 for (i = 0; i < 2; i++) {
1788 if (stream->udpsrc[i]) {
1789 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1790 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1791 gst_object_unref (stream->udpsrc[i]);
1793 if (stream->channelpad[i])
1794 gst_object_unref (stream->channelpad[i]);
1796 if (stream->udpsink[i]) {
1797 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1798 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1799 gst_object_unref (stream->udpsink[i]);
1802 if (stream->fakesrc) {
1803 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1804 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1805 gst_object_unref (stream->fakesrc);
1807 if (stream->srcpad) {
1808 gst_pad_set_active (stream->srcpad, FALSE);
1810 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1812 if (stream->srtpenc)
1813 gst_object_unref (stream->srtpenc);
1814 if (stream->srtpdec)
1815 gst_object_unref (stream->srtpdec);
1816 if (stream->srtcpparams)
1817 gst_caps_unref (stream->srtcpparams);
1819 gst_mikey_message_unref (stream->mikey);
1820 if (stream->rtcppad)
1821 gst_object_unref (stream->rtcppad);
1822 if (stream->session)
1823 g_object_unref (stream->session);
1824 if (stream->rtx_pt_map)
1825 gst_structure_free (stream->rtx_pt_map);
1827 g_mutex_clear (&stream->conninfo.send_lock);
1828 g_mutex_clear (&stream->conninfo.recv_lock);
1834 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1838 GST_DEBUG_OBJECT (src, "cleanup");
1840 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1841 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1843 gst_rtspsrc_stream_free (src, stream);
1845 g_list_free (src->streams);
1846 src->streams = NULL;
1848 if (src->manager_sig_id) {
1849 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1850 src->manager_sig_id = 0;
1852 gst_element_set_state (src->manager, GST_STATE_NULL);
1853 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1854 src->manager = NULL;
1857 gst_structure_free (src->props);
1860 g_free (src->content_base);
1861 src->content_base = NULL;
1863 g_free (src->control);
1864 src->control = NULL;
1867 gst_rtsp_range_free (src->range);
1870 /* don't clear the SDP when it was used in the url */
1871 if (src->sdp && !src->from_sdp) {
1872 gst_sdp_message_free (src->sdp);
1876 src->need_segment = FALSE;
1878 if (src->provided_clock) {
1879 gst_object_unref (src->provided_clock);
1880 src->provided_clock = NULL;
1885 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1886 gint * rtpport, gint * rtcpport)
1889 GstStateChangeReturn ret;
1890 GstElement *udpsrc0, *udpsrc1;
1891 gint tmp_rtp, tmp_rtcp;
1895 src = stream->parent;
1901 /* Start at next port */
1902 tmp_rtp = src->next_port_num;
1904 if (stream->is_ipv6)
1905 host = "udp://[::0]";
1907 host = "udp://0.0.0.0";
1909 /* try to allocate 2 UDP ports, the RTP port should be an even
1910 * number and the RTCP port should be the next (uneven) port */
1913 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1914 tmp_rtp >= src->client_port_range.max)
1917 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1918 if (udpsrc0 == NULL)
1919 goto no_udp_protocol;
1920 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1922 if (src->udp_buffer_size != 0)
1923 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
1926 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
1927 if (ret == GST_STATE_CHANGE_FAILURE) {
1929 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
1932 if (++count > src->retry)
1935 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1936 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1937 gst_object_unref (udpsrc0);
1940 GST_DEBUG_OBJECT (src, "retry %d", count);
1943 goto no_udp_protocol;
1946 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
1947 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
1949 /* check if port is even */
1950 if ((tmp_rtp & 0x01) != 0) {
1951 /* port not even, close and allocate another */
1952 if (++count > src->retry)
1955 GST_DEBUG_OBJECT (src, "RTP port not even");
1957 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1958 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1959 gst_object_unref (udpsrc0);
1962 GST_DEBUG_OBJECT (src, "retry %d", count);
1967 /* allocate port+1 for RTCP now */
1968 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1969 if (udpsrc1 == NULL)
1970 goto no_udp_rtcp_protocol;
1973 tmp_rtcp = tmp_rtp + 1;
1974 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
1977 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
1979 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
1980 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
1981 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
1982 if (ret == GST_STATE_CHANGE_FAILURE) {
1983 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
1985 if (++count > src->retry)
1988 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1989 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1990 gst_object_unref (udpsrc0);
1993 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
1994 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1995 gst_object_unref (udpsrc1);
1999 GST_DEBUG_OBJECT (src, "retry %d", count);
2003 /* all fine, do port check */
2004 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2005 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2007 /* this should not happen... */
2008 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2011 /* we keep these elements, we configure all in configure_transport when the
2012 * server told us to really use the UDP ports. */
2013 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2014 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2015 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2016 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2018 /* keep track of next available port number when we have a range
2020 if (src->next_port_num != 0)
2021 src->next_port_num = tmp_rtcp + 1;
2028 GST_DEBUG_OBJECT (src, "could not get UDP source");
2033 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2037 no_udp_rtcp_protocol:
2039 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2044 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2045 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2051 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2052 gst_object_unref (udpsrc0);
2055 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2056 gst_object_unref (udpsrc1);
2063 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2068 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2070 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2071 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2074 for (i = 0; i < 2; i++) {
2075 if (stream->udpsrc[i])
2076 gst_element_set_state (stream->udpsrc[i], state);
2082 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2089 event = gst_event_new_flush_start ();
2090 GST_DEBUG_OBJECT (src, "start flush");
2092 state = GST_STATE_PAUSED;
2094 event = gst_event_new_flush_stop (FALSE);
2095 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2098 state = GST_STATE_PLAYING;
2100 state = GST_STATE_PAUSED;
2102 gst_rtspsrc_push_event (src, event);
2103 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2104 gst_rtspsrc_set_state (src, state);
2107 static GstRTSPResult
2108 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2109 GstRTSPMessage * message, GTimeVal * timeout)
2113 if (conninfo->connection) {
2114 g_mutex_lock (&conninfo->send_lock);
2115 ret = gst_rtsp_connection_send (conninfo->connection, message, timeout);
2116 g_mutex_unlock (&conninfo->send_lock);
2118 ret = GST_RTSP_ERROR;
2124 static GstRTSPResult
2125 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
2126 GstRTSPMessage * message, GTimeVal * timeout)
2130 if (conninfo->connection) {
2131 g_mutex_lock (&conninfo->recv_lock);
2132 ret = gst_rtsp_connection_receive (conninfo->connection, message, timeout);
2133 g_mutex_unlock (&conninfo->recv_lock);
2135 ret = GST_RTSP_ERROR;
2142 gst_rtspsrc_get_position (GstRTSPSrc * src)
2147 query = gst_query_new_position (GST_FORMAT_TIME);
2148 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2149 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2150 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2154 if (stream->srcpad) {
2155 if (gst_pad_query (stream->srcpad, query)) {
2156 gst_query_parse_position (query, &fmt, &pos);
2157 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2158 GST_TIME_ARGS (pos));
2159 src->last_pos = pos;
2169 gst_query_unref (query);
2173 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2178 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2180 gboolean flush, skip;
2183 GstSegment seeksegment = { 0, };
2187 GST_DEBUG_OBJECT (src, "doing seek with event");
2189 gst_event_parse_seek (event, &rate, &format, &flags,
2190 &cur_type, &cur, &stop_type, &stop);
2192 /* no negative rates yet */
2196 /* we need TIME format */
2197 if (format != src->segment.format)
2200 GST_DEBUG_OBJECT (src, "doing seek without event");
2202 cur_type = GST_SEEK_TYPE_SET;
2203 stop_type = GST_SEEK_TYPE_SET;
2206 /* get flush flag */
2207 flush = flags & GST_SEEK_FLAG_FLUSH;
2208 skip = flags & GST_SEEK_FLAG_SKIP;
2210 /* now we need to make sure the streaming thread is stopped. We do this by
2211 * either sending a FLUSH_START event downstream which will cause the
2212 * streaming thread to stop with a WRONG_STATE.
2213 * For a non-flushing seek we simply pause the task, which will happen as soon
2214 * as it completes one iteration (and thus might block when the sink is
2215 * blocking in preroll). */
2217 GST_DEBUG_OBJECT (src, "starting flush");
2218 gst_rtspsrc_flush (src, TRUE, FALSE);
2221 gst_task_pause (src->task);
2225 /* we should now be able to grab the streaming thread because we stopped it
2226 * with the above flush/pause code */
2227 GST_RTSP_STREAM_LOCK (src);
2229 GST_DEBUG_OBJECT (src, "stopped streaming");
2231 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2232 gst_rtspsrc_connection_flush (src, FALSE);
2234 /* copy segment, we need this because we still need the old
2235 * segment when we close the current segment. */
2236 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2238 /* configure the seek parameters in the seeksegment. We will then have the
2239 * right values in the segment to perform the seek */
2241 GST_DEBUG_OBJECT (src, "configuring seek");
2242 gst_segment_do_seek (&seeksegment, rate, format, flags,
2243 cur_type, cur, stop_type, stop, &update);
2246 /* figure out the last position we need to play. If it's configured (stop !=
2247 * -1), use that, else we play until the total duration of the file */
2248 if ((stop = seeksegment.stop) == -1)
2249 stop = seeksegment.duration;
2251 /* if we were playing, pause first */
2252 playing = (src->state == GST_RTSP_STATE_PLAYING);
2254 /* obtain current position in case seek fails */
2255 gst_rtspsrc_get_position (src);
2256 gst_rtspsrc_pause (src, FALSE);
2260 src->state = GST_RTSP_STATE_SEEKING;
2262 /* PLAY will add the range header now. */
2263 src->need_range = TRUE;
2265 /* prepare for streaming again */
2267 /* if we started flush, we stop now */
2268 GST_DEBUG_OBJECT (src, "stopping flush");
2269 gst_rtspsrc_flush (src, FALSE, playing);
2272 /* now we did the seek and can activate the new segment values */
2273 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2275 /* if we're doing a segment seek, post a SEGMENT_START message */
2276 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2277 gst_element_post_message (GST_ELEMENT_CAST (src),
2278 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2279 src->segment.format, src->segment.position));
2282 /* now create the newsegment */
2283 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2284 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2287 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2288 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2289 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2290 stream->discont = TRUE;
2293 /* and continue playing if needed */
2294 GST_OBJECT_LOCK (src);
2295 playing = (GST_STATE_PENDING (src) == GST_STATE_VOID_PENDING
2296 && GST_STATE (src) == GST_STATE_PLAYING)
2297 || (GST_STATE_PENDING (src) == GST_STATE_PLAYING);
2298 GST_OBJECT_UNLOCK (src);
2300 gst_rtspsrc_play (src, &seeksegment, FALSE);
2302 GST_RTSP_STREAM_UNLOCK (src);
2309 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2314 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2320 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2324 gboolean res = TRUE;
2327 src = GST_RTSPSRC_CAST (parent);
2329 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2330 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2332 switch (GST_EVENT_TYPE (event)) {
2333 case GST_EVENT_SEEK:
2334 res = gst_rtspsrc_perform_seek (src, event);
2338 case GST_EVENT_NAVIGATION:
2339 case GST_EVENT_LATENCY:
2347 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2348 res = gst_pad_send_event (target, event);
2349 gst_object_unref (target);
2351 gst_event_unref (event);
2354 gst_event_unref (event);
2361 gst_rtspsrc_handle_src_sink_event (GstPad * pad, GstObject * parent,
2364 GstRTSPStream *stream;
2366 stream = gst_pad_get_element_private (pad);
2368 switch (GST_EVENT_TYPE (event)) {
2369 case GST_EVENT_STREAM_START:{
2370 const gchar *upstream_id;
2373 gst_event_parse_stream_start (event, &upstream_id);
2374 stream_id = g_strdup_printf ("%s/%s", upstream_id, stream->stream_id);
2376 gst_event_unref (event);
2377 event = gst_event_new_stream_start (stream_id);
2384 return gst_pad_push_event (stream->srcpad, event);
2387 /* this is the final event function we receive on the internal source pad when
2388 * we deal with TCP connections */
2390 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2395 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2397 switch (GST_EVENT_TYPE (event)) {
2398 case GST_EVENT_SEEK:
2400 case GST_EVENT_NAVIGATION:
2401 case GST_EVENT_LATENCY:
2403 gst_event_unref (event);
2410 /* this is the final query function we receive on the internal source pad when
2411 * we deal with TCP connections */
2413 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2417 gboolean res = TRUE;
2419 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2421 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2422 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2424 switch (GST_QUERY_TYPE (query)) {
2425 case GST_QUERY_POSITION:
2430 case GST_QUERY_DURATION:
2434 gst_query_parse_duration (query, &format, NULL);
2437 case GST_FORMAT_TIME:
2438 gst_query_set_duration (query, format, src->segment.duration);
2446 case GST_QUERY_LATENCY:
2448 /* we are live with a min latency of 0 and unlimited max latency, this
2449 * result will be updated by the session manager if there is any. */
2450 gst_query_set_latency (query, TRUE, 0, -1);
2460 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2462 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2466 gboolean res = FALSE;
2468 src = GST_RTSPSRC_CAST (parent);
2470 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2471 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2473 switch (GST_QUERY_TYPE (query)) {
2474 case GST_QUERY_DURATION:
2478 gst_query_parse_duration (query, &format, NULL);
2481 case GST_FORMAT_TIME:
2482 gst_query_set_duration (query, format, src->segment.duration);
2490 case GST_QUERY_SEEKING:
2494 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2495 if (format == GST_FORMAT_TIME) {
2497 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2499 /* seeking without duration is unlikely */
2500 seekable = seekable && src->seekable && src->segment.duration &&
2501 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2503 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0,
2504 src->segment.duration);
2513 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2515 gst_query_set_uri (query, uri);
2523 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2525 /* forward the query to the proxy target pad */
2527 res = gst_pad_query (target, query);
2528 gst_object_unref (target);
2537 /* callback for RTCP messages to be sent to the server when operating in TCP
2539 static GstFlowReturn
2540 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2543 GstRTSPStream *stream;
2544 GstFlowReturn res = GST_FLOW_OK;
2549 GstRTSPMessage message = { 0 };
2550 GstRTSPConnInfo *conninfo;
2552 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2553 src = stream->parent;
2555 gst_buffer_map (buffer, &map, GST_MAP_READ);
2559 gst_rtsp_message_init_data (&message, stream->channel[1]);
2561 /* lend the body data to the message */
2562 gst_rtsp_message_take_body (&message, data, size);
2564 if (stream->conninfo.connection)
2565 conninfo = &stream->conninfo;
2567 conninfo = &src->conninfo;
2569 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2570 ret = gst_rtspsrc_connection_send (src, conninfo, &message, NULL);
2571 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2573 /* and steal it away again because we will free it when unreffing the
2575 gst_rtsp_message_steal_body (&message, &data, &size);
2576 gst_rtsp_message_unset (&message);
2578 gst_buffer_unmap (buffer, &map);
2579 gst_buffer_unref (buffer);
2584 static GstPadProbeReturn
2585 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2587 GstRTSPSrc *src = user_data;
2589 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2590 GST_DEBUG_PAD_NAME (pad));
2592 /* activate the streams */
2593 GST_OBJECT_LOCK (src);
2594 if (!src->need_activate)
2597 src->need_activate = FALSE;
2598 GST_OBJECT_UNLOCK (src);
2600 gst_rtspsrc_activate_streams (src);
2602 return GST_PAD_PROBE_OK;
2606 GST_OBJECT_UNLOCK (src);
2607 return GST_PAD_PROBE_OK;
2612 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
2614 GstPad *gpad = GST_PAD_CAST (user_data);
2616 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
2617 gst_pad_store_sticky_event (gpad, *event);
2622 /* this callback is called when the session manager generated a new src pad with
2623 * payloaded RTP packets. We simply ghost the pad here. */
2625 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2628 GstPadTemplate *template;
2631 GstRTSPStream *stream;
2633 GstPad *internal_src;
2635 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2637 GST_RTSP_STATE_LOCK (src);
2639 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2640 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2641 goto unknown_stream;
2643 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2645 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2647 goto unknown_stream;
2650 stream->ssrc = ssrc;
2652 /* we'll add it later see below */
2653 stream->added = TRUE;
2655 /* check if we added all streams */
2657 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2658 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2660 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
2661 ostream, ostream->container, ostream->added, ostream->setup);
2663 /* if we find a stream for which we did a setup that is not added, we
2664 * need to wait some more */
2665 if (ostream->setup && !ostream->added) {
2670 GST_RTSP_STATE_UNLOCK (src);
2672 /* create a new pad we will use to stream to */
2673 template = gst_static_pad_template_get (&rtptemplate);
2674 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2675 gst_object_unref (template);
2678 /* We intercept and modify the stream start event */
2680 GST_PAD (gst_proxy_pad_get_internal (GST_PROXY_PAD (stream->srcpad)));
2681 gst_pad_set_element_private (internal_src, stream);
2682 gst_pad_set_event_function (internal_src, gst_rtspsrc_handle_src_sink_event);
2683 gst_object_unref (internal_src);
2685 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2686 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2687 gst_pad_set_active (stream->srcpad, TRUE);
2688 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
2689 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2692 GST_DEBUG_OBJECT (src, "We added all streams");
2693 /* when we get here, all stream are added and we can fire the no-more-pads
2695 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2703 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2704 GST_RTSP_STATE_UNLOCK (src);
2711 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
2715 len = stream->ptmap->len;
2716 for (i = 0; i < len; i++) {
2717 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2725 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2727 GstRTSPStream *stream;
2730 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2732 GST_RTSP_STATE_LOCK (src);
2733 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2735 goto unknown_stream;
2737 if ((caps = stream_get_caps_for_pt (stream, pt)))
2738 gst_caps_ref (caps);
2739 GST_RTSP_STATE_UNLOCK (src);
2745 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2746 GST_RTSP_STATE_UNLOCK (src);
2752 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2754 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2760 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2766 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2772 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2774 GstRTSPSrc *src = stream->parent;
2777 g_object_get (source, "ssrc", &ssrc, NULL);
2779 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2780 ssrc, stream->ssrc, stream->id);
2782 if (ssrc == stream->ssrc)
2783 gst_rtspsrc_do_stream_eos (src, stream);
2787 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2789 GstRTSPSrc *src = stream->parent;
2792 g_object_get (source, "ssrc", &ssrc, NULL);
2794 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2795 ssrc, stream->ssrc, stream->id);
2797 if (ssrc == stream->ssrc)
2798 gst_rtspsrc_do_stream_eos (src, stream);
2802 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2804 GstRTSPStream *stream;
2806 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2808 /* get stream for session */
2809 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2811 gst_rtspsrc_do_stream_eos (src, stream);
2816 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2818 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2823 set_manager_buffer_mode (GstRTSPSrc * src)
2825 GObjectClass *klass;
2827 if (src->manager == NULL)
2830 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2832 if (!g_object_class_find_property (klass, "buffer-mode"))
2835 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2836 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2841 GST_DEBUG_OBJECT (src,
2842 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
2844 if (src->provided_clock) {
2845 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
2847 if (clock == src->provided_clock) {
2848 GST_DEBUG_OBJECT (src, "selected synced");
2849 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
2852 gst_object_unref (clock);
2857 /* Otherwise fall-through and use another buffer mode */
2859 gst_object_unref (clock);
2862 GST_DEBUG_OBJECT (src, "auto buffering mode");
2863 if (src->use_buffering) {
2864 GST_DEBUG_OBJECT (src, "selected buffer");
2865 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
2867 GST_DEBUG_OBJECT (src, "selected slave");
2868 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2873 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
2877 GstMIKEYMessage *msg = stream->mikey;
2879 GST_DEBUG ("request key SSRC %u", ssrc);
2881 caps = gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
2882 caps = gst_caps_make_writable (caps);
2884 /* parse crypto sessions and look for the SSRC rollover counter */
2885 msg = stream->mikey;
2886 for (i = 0; msg && i < gst_mikey_message_get_n_cs (msg); i++) {
2887 const GstMIKEYMapSRTP *map = gst_mikey_message_get_cs_srtp (msg, i);
2889 if (ssrc == map->ssrc) {
2890 gst_caps_set_simple (caps, "roc", G_TYPE_UINT, map->roc, NULL);
2899 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
2901 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
2902 if (stream->id != session)
2905 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
2906 stream->profile != GST_RTSP_PROFILE_SAVPF)
2909 if (stream->srtpdec == NULL) {
2912 name = g_strdup_printf ("srtpdec_%u", session);
2913 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
2916 if (stream->srtpdec == NULL) {
2917 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
2918 ("no srtpdec element present!"));
2921 g_signal_connect (stream->srtpdec, "request-key",
2922 (GCallback) request_key, stream);
2924 return gst_object_ref (stream->srtpdec);
2928 request_rtcp_encoder (GstElement * rtpbin, guint session,
2929 GstRTSPStream * stream)
2934 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
2935 if (stream->id != session)
2938 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
2939 stream->profile != GST_RTSP_PROFILE_SAVPF)
2942 if (stream->srtpenc == NULL) {
2945 name = g_strdup_printf ("srtpenc_%u", session);
2946 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
2949 if (stream->srtpenc == NULL) {
2950 GST_ELEMENT_ERROR (stream->parent, CORE, MISSING_PLUGIN, (NULL),
2951 ("no srtpenc element present!"));
2955 /* get RTCP crypto parameters from caps */
2956 s = gst_caps_get_structure (stream->srtcpparams, 0);
2960 GType ciphertype, authtype;
2961 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
2963 ciphertype = g_type_from_name ("GstSrtpCipherType");
2964 authtype = g_type_from_name ("GstSrtpAuthType");
2965 g_value_init (&rtcp_cipher, ciphertype);
2966 g_value_init (&rtcp_auth, authtype);
2968 str = gst_structure_get_string (s, "srtcp-cipher");
2969 gst_value_deserialize (&rtcp_cipher, str);
2970 str = gst_structure_get_string (s, "srtcp-auth");
2971 gst_value_deserialize (&rtcp_auth, str);
2972 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
2974 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-cipher",
2976 g_object_set_property (G_OBJECT (stream->srtpenc), "rtp-auth",
2978 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
2980 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
2982 g_object_set (stream->srtpenc, "key", buf, NULL);
2984 g_value_unset (&rtcp_cipher);
2985 g_value_unset (&rtcp_auth);
2986 gst_buffer_unref (buf);
2989 name = g_strdup_printf ("rtcp_sink_%d", session);
2990 pad = gst_element_get_request_pad (stream->srtpenc, name);
2992 gst_object_unref (pad);
2994 return gst_object_ref (stream->srtpenc);
2998 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
3000 GstElement *rtx, *bin;
3003 GstRTSPStream *stream;
3005 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
3007 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
3011 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
3012 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
3013 bin = gst_bin_new (NULL);
3014 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
3015 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
3016 gst_bin_add (GST_BIN (bin), rtx);
3018 pad = gst_element_get_static_pad (rtx, "src");
3019 name = g_strdup_printf ("src_%u", sessid);
3020 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3022 gst_object_unref (pad);
3024 pad = gst_element_get_static_pad (rtx, "sink");
3025 name = g_strdup_printf ("sink_%u", sessid);
3026 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3028 gst_object_unref (pad);
3034 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
3038 gboolean do_retransmission = FALSE;
3040 if (transport->trans != GST_RTSP_TRANS_RTP)
3042 if (transport->profile != GST_RTSP_PROFILE_AVPF &&
3043 transport->profile != GST_RTSP_PROFILE_SAVPF)
3046 signal_id = g_signal_lookup ("request-aux-receiver",
3047 G_OBJECT_TYPE (src->manager));
3048 /* there's already something connected */
3049 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
3050 NULL, NULL, NULL) != 0) {
3051 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
3052 "\"request-aux-receiver\" signal is "
3053 "already used by the application");
3057 /* build the retransmission payload type map */
3058 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3059 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3060 gboolean do_retransmission_stream = FALSE;
3063 if (stream->rtx_pt_map)
3064 gst_structure_free (stream->rtx_pt_map);
3065 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
3067 for (i = 0; i < stream->ptmap->len; i++) {
3068 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3069 GstStructure *s = gst_caps_get_structure (item->caps, 0);
3070 const gchar *encoding;
3072 /* we only care about RTX streams */
3073 if ((encoding = gst_structure_get_string (s, "encoding-name"))
3074 && g_strcmp0 (encoding, "RTX") == 0) {
3075 const gchar *stream_pt_s;
3078 if (gst_structure_get_int (s, "payload", &rtx_pt)
3079 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
3082 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
3084 do_retransmission_stream = TRUE;
3090 if (do_retransmission_stream) {
3091 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
3092 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
3093 do_retransmission = TRUE;
3095 GST_DEBUG_OBJECT (src, "no retransmission payload map for stream "
3096 "id %i", stream->id);
3097 gst_structure_free (stream->rtx_pt_map);
3098 stream->rtx_pt_map = NULL;
3102 if (do_retransmission) {
3103 GST_DEBUG_OBJECT (src, "Enabling retransmissions");
3105 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
3107 /* enable RFC4588 retransmission handling by setting rtprtxreceive
3108 * as the "aux" element of rtpbin */
3109 g_signal_connect (src->manager, "request-aux-receiver",
3110 (GCallback) request_aux_receiver, src);
3112 GST_DEBUG_OBJECT (src,
3113 "Not enabling retransmissions as no stream had a retransmission payload map");
3117 /* try to get and configure a manager */
3119 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3120 GstRTSPTransport * transport)
3122 const gchar *manager;
3124 GstStateChangeReturn ret;
3126 /* find a manager */
3127 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3131 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3133 /* configure the manager */
3134 if (src->manager == NULL) {
3135 GObjectClass *klass;
3137 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3139 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3143 goto use_no_manager;
3145 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3146 goto manager_failed;
3149 /* we manage this element */
3150 gst_element_set_locked_state (src->manager, TRUE);
3151 gst_bin_add (GST_BIN_CAST (src), src->manager);
3153 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3154 if (ret == GST_STATE_CHANGE_FAILURE)
3155 goto start_manager_failure;
3157 g_object_set (src->manager, "latency", src->latency, NULL);
3159 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3161 if (g_object_class_find_property (klass, "ntp-sync")) {
3162 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3165 if (g_object_class_find_property (klass, "rfc7273-sync")) {
3166 g_object_set (src->manager, "rfc7273-sync", src->rfc7273_sync, NULL);
3169 if (src->use_pipeline_clock) {
3170 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3171 g_object_set (src->manager, "use-pipeline-clock", TRUE, NULL);
3174 if (g_object_class_find_property (klass, "ntp-time-source")) {
3175 g_object_set (src->manager, "ntp-time-source", src->ntp_time_source,
3180 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3181 g_object_set (src->manager, "sdes", src->sdes, NULL);
3184 if (g_object_class_find_property (klass, "drop-on-latency")) {
3185 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3189 if (g_object_class_find_property (klass, "max-rtcp-rtp-time-diff")) {
3190 g_object_set (src->manager, "max-rtcp-rtp-time-diff",
3191 src->max_rtcp_rtp_time_diff, NULL);
3194 if (g_object_class_find_property (klass, "max-ts-offset-adjustment")) {
3195 g_object_set (src->manager, "max-ts-offset-adjustment",
3196 src->max_ts_offset_adjustment, NULL);
3199 /* buffer mode pauses are handled by adding offsets to buffer times,
3200 * but some depayloaders may have a hard time syncing output times
3201 * with such input times, e.g. container ones, most notably ASF */
3202 /* TODO alternatives are having an event that indicates these shifts,
3203 * or having rtsp extensions provide suggestion on buffer mode */
3204 /* valid duration implies not likely live pipeline,
3205 * so slaving in jitterbuffer does not make much sense
3206 * (and might mess things up due to bursts) */
3207 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3208 src->segment.duration && stream->container) {
3209 src->use_buffering = TRUE;
3211 src->use_buffering = FALSE;
3214 set_manager_buffer_mode (src);
3216 /* connect to signals */
3217 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3219 src->manager_sig_id =
3220 g_signal_connect (src->manager, "pad-added",
3221 (GCallback) new_manager_pad, src);
3222 src->manager_ptmap_id =
3223 g_signal_connect (src->manager, "request-pt-map",
3224 (GCallback) request_pt_map, src);
3226 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3229 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3232 if (src->do_retransmission)
3233 add_retransmission (src, transport);
3235 g_signal_connect (src->manager, "request-rtp-decoder",
3236 (GCallback) request_rtp_decoder, stream);
3237 g_signal_connect (src->manager, "request-rtcp-decoder",
3238 (GCallback) request_rtp_decoder, stream);
3239 g_signal_connect (src->manager, "request-rtcp-encoder",
3240 (GCallback) request_rtcp_encoder, stream);
3242 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3243 * into a separate RTP session. */
3244 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3245 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3247 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3248 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3251 /* now configure the bandwidth in the manager */
3252 if (g_signal_lookup ("get-internal-session",
3253 G_OBJECT_TYPE (src->manager)) != 0) {
3254 GObject *rtpsession;
3256 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3259 GstRTPProfile rtp_profile;
3261 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3263 stream->session = rtpsession;
3265 if (stream->as_bandwidth != -1) {
3266 GST_INFO_OBJECT (src, "setting AS: %f",
3267 (gdouble) (stream->as_bandwidth * 1000));
3268 g_object_set (rtpsession, "bandwidth",
3269 (gdouble) (stream->as_bandwidth * 1000), NULL);
3271 if (stream->rr_bandwidth != -1) {
3272 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3273 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3276 if (stream->rs_bandwidth != -1) {
3277 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3278 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3282 switch (stream->profile) {
3283 case GST_RTSP_PROFILE_AVPF:
3284 rtp_profile = GST_RTP_PROFILE_AVPF;
3286 case GST_RTSP_PROFILE_SAVP:
3287 rtp_profile = GST_RTP_PROFILE_SAVP;
3289 case GST_RTSP_PROFILE_SAVPF:
3290 rtp_profile = GST_RTP_PROFILE_SAVPF;
3292 case GST_RTSP_PROFILE_AVP:
3294 rtp_profile = GST_RTP_PROFILE_AVP;
3298 g_object_set (rtpsession, "rtp-profile", rtp_profile, NULL);
3300 g_object_set (rtpsession, "probation", src->probation, NULL);
3302 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3304 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3306 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3308 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3310 g_signal_connect (rtpsession, "on-ssrc-active",
3311 (GCallback) on_ssrc_active, stream);
3322 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3327 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3330 start_manager_failure:
3332 GST_DEBUG_OBJECT (src, "could not start session manager");
3337 /* free the UDP sources allocated when negotiating a transport.
3338 * This function is called when the server negotiated to a transport where the
3339 * UDP sources are not needed anymore, such as TCP or multicast. */
3341 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3345 for (i = 0; i < 2; i++) {
3346 if (stream->udpsrc[i]) {
3347 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3348 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3349 gst_object_unref (stream->udpsrc[i]);
3350 stream->udpsrc[i] = NULL;
3355 /* for TCP, create pads to send and receive data to and from the manager and to
3356 * intercept various events and queries
3359 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3360 GstRTSPTransport * transport, GstPad ** outpad)
3363 GstPadTemplate *template;
3364 GstPad *pad0, *pad1;
3366 /* configure for interleaved delivery, nothing needs to be done
3367 * here, the loop function will call the chain functions of the
3368 * session manager. */
3369 stream->channel[0] = transport->interleaved.min;
3370 stream->channel[1] = transport->interleaved.max;
3371 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3372 stream->channel[0], stream->channel[1]);
3374 /* we can remove the allocated UDP ports now */
3375 gst_rtspsrc_stream_free_udp (stream);
3377 /* no session manager, send data to srcpad directly */
3378 if (!stream->channelpad[0]) {
3379 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3381 /* create a new pad we will use to stream to */
3382 name = g_strdup_printf ("stream_%u", stream->id);
3383 template = gst_static_pad_template_get (&rtptemplate);
3384 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3385 gst_object_unref (template);
3388 /* set caps and activate */
3389 gst_pad_use_fixed_caps (stream->channelpad[0]);
3390 gst_pad_set_active (stream->channelpad[0], TRUE);
3392 *outpad = gst_object_ref (stream->channelpad[0]);
3394 GST_DEBUG_OBJECT (src, "using manager source pad");
3396 template = gst_static_pad_template_get (&anysrctemplate);
3398 /* allocate pads for sending the channel data into the manager */
3399 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3400 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3401 gst_object_unref (stream->channelpad[0]);
3402 stream->channelpad[0] = pad0;
3403 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3404 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3405 gst_pad_set_element_private (pad0, src);
3406 gst_pad_set_active (pad0, TRUE);
3408 if (stream->channelpad[1]) {
3409 /* if we have a sinkpad for the other channel, create a pad and link to the
3411 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3412 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3413 gst_pad_link_full (pad1, stream->channelpad[1],
3414 GST_PAD_LINK_CHECK_NOTHING);
3415 gst_object_unref (stream->channelpad[1]);
3416 stream->channelpad[1] = pad1;
3417 gst_pad_set_active (pad1, TRUE);
3419 gst_object_unref (template);
3421 /* setup RTCP transport back to the server if we have to. */
3422 if (src->manager && src->do_rtcp) {
3425 template = gst_static_pad_template_get (&anysinktemplate);
3427 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3428 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3429 gst_pad_set_element_private (stream->rtcppad, stream);
3430 gst_pad_set_active (stream->rtcppad, TRUE);
3432 /* get session RTCP pad */
3433 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3434 pad = gst_element_get_request_pad (src->manager, name);
3439 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3440 gst_object_unref (pad);
3443 gst_object_unref (template);
3449 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3450 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3451 gint * max, guint * ttl)
3453 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3455 if (!(*destination = transport->destination))
3456 *destination = stream->destination;
3459 /* transport first */
3460 *min = transport->port.min;
3461 *max = transport->port.max;
3462 if (*min == -1 && *max == -1) {
3463 /* then try from SDP */
3464 if (stream->port != 0) {
3465 *min = stream->port;
3466 *max = stream->port + 1;
3472 if (!(*ttl = transport->ttl))
3477 /* first take the source, then the endpoint to figure out where to send
3479 if (!(*destination = transport->source)) {
3480 if (src->conninfo.connection)
3481 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3482 else if (stream->conninfo.connection)
3484 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3488 /* for unicast we only expect the ports here */
3489 *min = transport->server_port.min;
3490 *max = transport->server_port.max;
3495 /* For multicast create UDP sources and join the multicast group. */
3497 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3498 GstRTSPTransport * transport, GstPad ** outpad)
3501 const gchar *destination;
3504 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3506 /* we can remove the allocated UDP ports now */
3507 gst_rtspsrc_stream_free_udp (stream);
3509 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3512 /* we need a destination now */
3513 if (destination == NULL)
3514 goto no_destination;
3516 /* we really need ports now or we won't be able to receive anything at all */
3517 if (min == -1 && max == -1)
3520 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3521 destination, min, max);
3523 /* creating UDP source for RTP */
3525 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3527 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3529 if (stream->udpsrc[0] == NULL)
3532 /* take ownership */
3533 gst_object_ref_sink (stream->udpsrc[0]);
3535 if (src->udp_buffer_size != 0)
3536 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3537 src->udp_buffer_size, NULL);
3539 if (src->multi_iface != NULL)
3540 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3541 src->multi_iface, NULL);
3544 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3545 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3548 /* creating another UDP source for RTCP */
3552 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3554 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3556 if (stream->udpsrc[1] == NULL)
3559 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3560 stream->profile == GST_RTSP_PROFILE_SAVPF)
3561 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3563 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3564 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3565 gst_caps_unref (caps);
3567 /* take ownership */
3568 gst_object_ref_sink (stream->udpsrc[1]);
3570 if (src->multi_iface != NULL)
3571 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3572 src->multi_iface, NULL);
3574 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
3581 GST_DEBUG_OBJECT (src, "no UDP source element found");
3586 GST_DEBUG_OBJECT (src, "no destination found");
3591 GST_DEBUG_OBJECT (src, "no ports found");
3596 /* configure the remainder of the UDP ports */
3598 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3599 GstRTSPTransport * transport, GstPad ** outpad)
3601 /* we manage the UDP elements now. For unicast, the UDP sources where
3602 * allocated in the stream when we suggested a transport. */
3603 if (stream->udpsrc[0]) {
3606 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3607 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3609 GST_DEBUG_OBJECT (src, "setting up UDP source");
3611 /* configure a timeout on the UDP port. When the timeout message is
3612 * posted, we assume UDP transport is not possible. We reconnect using TCP
3614 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3615 src->udp_timeout * 1000, NULL);
3617 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
3618 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3620 /* get output pad of the UDP source. */
3621 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3623 /* save it so we can unblock */
3624 stream->blockedpad = *outpad;
3626 /* configure pad block on the pad. As soon as there is dataflow on the
3627 * UDP source, we know that UDP is not blocked by a firewall and we can
3628 * configure all the streams to let the application autoplug decoders. */
3630 gst_pad_add_probe (stream->blockedpad,
3631 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3632 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
3634 if (stream->channelpad[0]) {
3635 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3636 /* configure for UDP delivery, we need to connect the UDP pads to
3637 * the session plugin. */
3638 gst_pad_link_full (*outpad, stream->channelpad[0],
3639 GST_PAD_LINK_CHECK_NOTHING);
3640 gst_object_unref (*outpad);
3642 /* we connected to pad-added signal to get pads from the manager */
3644 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3649 if (stream->udpsrc[1]) {
3652 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3653 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3655 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3656 stream->profile == GST_RTSP_PROFILE_SAVPF)
3657 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3659 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3660 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3661 gst_caps_unref (caps);
3663 if (stream->channelpad[1]) {
3666 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3668 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3669 gst_pad_link_full (pad, stream->channelpad[1],
3670 GST_PAD_LINK_CHECK_NOTHING);
3671 gst_object_unref (pad);
3673 /* leave unlinked */
3679 /* configure the UDP sink back to the server for status reports */
3681 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3682 GstRTSPStream * stream, GstRTSPTransport * transport)
3685 gint rtp_port, rtcp_port;
3686 gboolean do_rtp, do_rtcp;
3687 const gchar *destination;
3692 /* get transport info */
3693 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3694 &rtp_port, &rtcp_port, &ttl);
3696 /* see what we need to do */
3697 do_rtp = (rtp_port != -1);
3698 /* it's possible that the server does not want us to send RTCP in which case
3700 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3702 /* we need a destination when we have RTP or RTCP ports */
3703 if (destination == NULL && (do_rtp || do_rtcp))
3704 goto no_destination;
3706 /* try to construct the fakesrc to the RTP port of the server to open up any
3709 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3712 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3713 stream->udpsink[0] =
3714 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3716 if (stream->udpsink[0] == NULL)
3717 goto no_sink_element;
3719 /* don't join multicast group, we will have the source socket do that */
3720 /* no sync or async state changes needed */
3721 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3722 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3724 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3726 if (stream->udpsrc[0]) {
3727 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3728 * so that NAT firewalls will open a hole for us */
3729 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3733 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3734 /* configure socket and make sure udpsink does not close it when shutting
3735 * down, it belongs to udpsrc after all. */
3736 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3737 "close-socket", FALSE, NULL);
3738 g_object_unref (socket);
3741 /* the source for the dummy packets to open up NAT */
3742 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3743 if (stream->fakesrc == NULL)
3744 goto no_fakesrc_element;
3746 /* random data in 5 buffers, a size of 200 bytes should be fine */
3747 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3748 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3750 /* keep everything locked */
3751 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3752 gst_element_set_locked_state (stream->fakesrc, TRUE);
3754 gst_object_ref (stream->udpsink[0]);
3755 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3756 gst_object_ref (stream->fakesrc);
3757 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3759 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3760 "sink", GST_PAD_LINK_CHECK_NOTHING);
3763 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3766 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3767 stream->udpsink[1] =
3768 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3770 if (stream->udpsink[1] == NULL)
3771 goto no_sink_element;
3773 /* don't join multicast group, we will have the source socket do that */
3774 /* no sync or async state changes needed */
3775 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3776 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3778 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3780 if (stream->udpsrc[1]) {
3781 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3782 * because some servers check the port number of where it sends RTCP to identify
3783 * the RTCP packets it receives */
3784 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3788 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3789 /* configure socket and make sure udpsink does not close it when shutting
3790 * down, it belongs to udpsrc after all. */
3791 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3792 "close-socket", FALSE, NULL);
3793 g_object_unref (socket);
3796 /* we keep this playing always */
3797 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3798 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3800 gst_object_ref (stream->udpsink[1]);
3801 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3803 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3805 /* get session RTCP pad */
3806 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3807 pad = gst_element_get_request_pad (src->manager, name);
3812 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3813 gst_object_unref (pad);
3822 GST_ERROR_OBJECT (src, "no destination address specified");
3827 GST_ERROR_OBJECT (src, "no UDP sink element found");
3832 GST_ERROR_OBJECT (src, "no fakesrc element found");
3837 GST_ERROR_OBJECT (src, "failed to create socket");
3842 /* sets up all elements needed for streaming over the specified transport.
3843 * Does not yet expose the element pads, this will be done when there is actuall
3844 * dataflow detected, which might never happen when UDP is blocked in a
3845 * firewall, for example.
3848 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3849 GstRTSPTransport * transport)
3852 GstPad *outpad = NULL;
3853 GstPadTemplate *template;
3855 const gchar *media_type;
3858 src = stream->parent;
3860 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3862 /* get the proper media type for this stream now */
3863 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
3864 goto unknown_transport;
3866 goto unknown_transport;
3868 /* configure the final media type */
3869 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
3871 len = stream->ptmap->len;
3872 for (i = 0; i < len; i++) {
3874 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3876 if (item->caps == NULL)
3879 s = gst_caps_get_structure (item->caps, 0);
3880 gst_structure_set_name (s, media_type);
3881 /* set ssrc if known */
3882 if (transport->ssrc)
3883 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
3886 /* try to get and configure a manager, channelpad[0-1] will be configured with
3887 * the pads for the manager, or NULL when no manager is needed. */
3888 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3891 switch (transport->lower_transport) {
3892 case GST_RTSP_LOWER_TRANS_TCP:
3893 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3894 goto transport_failed;
3896 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3897 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3898 goto transport_failed;
3899 /* fallthrough, the rest is the same for UDP and MCAST */
3900 case GST_RTSP_LOWER_TRANS_UDP:
3901 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3902 goto transport_failed;
3903 /* configure udpsinks back to the server for RTCP messages and for the
3904 * dummy RTP messages to open NAT. */
3905 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3906 goto transport_failed;
3909 goto unknown_transport;
3913 GST_DEBUG_OBJECT (src, "creating ghostpad");
3915 gst_pad_use_fixed_caps (outpad);
3917 /* create ghostpad, don't add just yet, this will be done when we activate
3919 name = g_strdup_printf ("stream_%u", stream->id);
3920 template = gst_static_pad_template_get (&rtptemplate);
3921 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3922 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3923 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3924 gst_object_unref (template);
3927 gst_object_unref (outpad);
3929 /* mark pad as ok */
3930 stream->last_ret = GST_FLOW_OK;
3937 GST_DEBUG_OBJECT (src, "failed to configure transport");
3942 GST_DEBUG_OBJECT (src, "unknown transport");
3947 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3952 /* send a couple of dummy random packets on the receiver RTP port to the server,
3953 * this should make a firewall think we initiated the data transfer and
3954 * hopefully allow packets to go from the sender port to our RTP receiver port */
3956 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3960 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3963 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3964 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3966 if (stream->fakesrc && stream->udpsink[0]) {
3967 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3968 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3969 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3970 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3971 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3977 /* Adds the source pads of all configured streams to the element.
3978 * This code is performed when we detected dataflow.
3980 * We detect dataflow from either the _loop function or with pad probes on the
3984 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3988 GST_DEBUG_OBJECT (src, "activating streams");
3990 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3991 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3993 if (stream->udpsrc[0]) {
3994 /* remove timeout, we are streaming now and timeouts will be handled by
3995 * the session manager and jitter buffer */
3996 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3998 if (stream->srcpad) {
3999 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
4000 gst_pad_set_active (stream->srcpad, TRUE);
4002 /* if we don't have a session manager, set the caps now. If we have a
4003 * session, we will get a notification of the pad and the caps. */
4004 if (!src->manager) {
4007 caps = stream_get_caps_for_pt (stream, stream->default_pt);
4008 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
4009 gst_pad_set_caps (stream->srcpad, caps);
4012 if (!stream->added) {
4013 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
4014 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
4015 stream->added = TRUE;
4020 /* unblock all pads */
4021 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4022 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4024 if (stream->blockid) {
4025 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
4026 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
4027 stream->blockid = 0;
4035 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
4036 gboolean reset_manager)
4039 guint64 start, stop;
4040 gdouble play_speed, play_scale;
4042 GST_DEBUG_OBJECT (src, "configuring stream caps");
4044 start = segment->position;
4045 stop = segment->duration;
4046 play_speed = segment->rate;
4047 play_scale = segment->applied_rate;
4049 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4050 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4056 len = stream->ptmap->len;
4057 for (j = 0; j < len; j++) {
4059 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
4061 if (item->caps == NULL)
4064 caps = gst_caps_make_writable (item->caps);
4066 if (stream->timebase != -1)
4067 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
4068 (guint) stream->timebase, NULL);
4069 if (stream->seqbase != -1)
4070 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
4071 (guint) stream->seqbase, NULL);
4072 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
4074 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
4075 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
4076 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
4079 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
4082 if (item->pt == stream->default_pt) {
4083 if (stream->udpsrc[0])
4084 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4085 stream->need_caps = TRUE;
4089 if (reset_manager && src->manager) {
4090 GST_DEBUG_OBJECT (src, "clear session");
4091 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
4095 static GstFlowReturn
4096 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
4101 /* store the value */
4102 stream->last_ret = ret;
4104 /* if it's success we can return the value right away */
4105 if (ret == GST_FLOW_OK)
4108 /* any other error that is not-linked can be returned right
4110 if (ret != GST_FLOW_NOT_LINKED)
4113 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4114 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4115 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4117 ret = ostream->last_ret;
4118 /* some other return value (must be SUCCESS but we can return
4119 * other values as well) */
4120 if (ret != GST_FLOW_NOT_LINKED)
4123 /* if we get here, all other pads were unlinked and we return
4124 * NOT_LINKED then */
4130 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4133 gboolean res = TRUE;
4135 /* only streams that have a connection to the outside world */
4139 if (stream->udpsrc[0]) {
4140 gst_event_ref (event);
4141 res = gst_element_send_event (stream->udpsrc[0], event);
4142 } else if (stream->channelpad[0]) {
4143 gst_event_ref (event);
4144 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4145 res = gst_pad_push_event (stream->channelpad[0], event);
4147 res = gst_pad_send_event (stream->channelpad[0], event);
4150 if (stream->udpsrc[1]) {
4151 gst_event_ref (event);
4152 res &= gst_element_send_event (stream->udpsrc[1], event);
4153 } else if (stream->channelpad[1]) {
4154 gst_event_ref (event);
4155 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4156 res &= gst_pad_push_event (stream->channelpad[1], event);
4158 res &= gst_pad_send_event (stream->channelpad[1], event);
4162 gst_event_unref (event);
4168 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4171 gboolean res = TRUE;
4173 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4174 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4176 gst_event_ref (event);
4177 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4179 gst_event_unref (event);
4184 static GstRTSPResult
4185 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4189 GstRTSPMessage response;
4190 gboolean retry = FALSE;
4191 memset (&response, 0, sizeof (response));
4192 gst_rtsp_message_init (&response);
4194 if (info->connection == NULL) {
4195 if (info->url == NULL) {
4196 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4197 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4200 /* create connection */
4201 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4202 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4203 goto could_not_create;
4206 gst_rtspsrc_setup_auth (src, &response);
4209 g_free (info->url_str);
4210 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4212 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4214 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4215 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4216 src->tls_validation_flags))
4217 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4219 if (src->tls_database)
4220 gst_rtsp_connection_set_tls_database (info->connection,
4223 if (src->tls_interaction)
4224 gst_rtsp_connection_set_tls_interaction (info->connection,
4225 src->tls_interaction);
4228 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4229 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4231 if (src->proxy_host) {
4232 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4234 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4239 if (!info->connected) {
4242 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4243 ("Connecting to %s", info->location));
4244 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4245 res = gst_rtsp_connection_connect_with_response (info->connection,
4246 src->ptcp_timeout, &response);
4248 if (response.type == GST_RTSP_MESSAGE_HTTP_RESPONSE &&
4249 response.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4250 gst_rtsp_conninfo_close (src, info, TRUE);
4254 retry = FALSE; // we should not retry more than once
4259 if (res == GST_RTSP_OK)
4260 info->connected = TRUE;
4262 goto could_not_connect;
4264 } while (!info->connected && retry);
4266 gst_rtsp_message_unset (&response);
4272 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4273 gst_rtsp_message_unset (&response);
4278 gchar *str = gst_rtsp_strresult (res);
4279 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4281 gst_rtsp_message_unset (&response);
4286 gchar *str = gst_rtsp_strresult (res);
4287 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4289 gst_rtsp_message_unset (&response);
4294 static GstRTSPResult
4295 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4298 GST_RTSP_STATE_LOCK (src);
4299 if (info->connected) {
4300 GST_DEBUG_OBJECT (src, "closing connection...");
4301 gst_rtsp_connection_close (info->connection);
4302 info->connected = FALSE;
4304 if (free && info->connection) {
4305 /* free connection */
4306 GST_DEBUG_OBJECT (src, "freeing connection...");
4307 gst_rtsp_connection_free (info->connection);
4308 info->connection = NULL;
4309 info->flushing = FALSE;
4311 GST_RTSP_STATE_UNLOCK (src);
4315 static GstRTSPResult
4316 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4321 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4322 gst_rtsp_conninfo_close (src, info, FALSE);
4323 res = gst_rtsp_conninfo_connect (src, info, async);
4329 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4333 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4334 GST_RTSP_STATE_LOCK (src);
4335 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4336 GST_DEBUG_OBJECT (src, "connection flush");
4337 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4338 src->conninfo.flushing = flush;
4340 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4341 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4342 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4343 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4344 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4345 stream->conninfo.flushing = flush;
4348 GST_RTSP_STATE_UNLOCK (src);
4351 static GstRTSPResult
4352 gst_rtspsrc_init_request (GstRTSPSrc * src, GstRTSPMessage * msg,
4353 GstRTSPMethod method, const gchar * uri)
4357 res = gst_rtsp_message_init_request (msg, method, uri);
4361 /* set user-agent */
4362 if (src->user_agent)
4363 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, src->user_agent);
4368 /* FIXME, handle server request, reply with OK, for now */
4369 static GstRTSPResult
4370 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
4371 GstRTSPMessage * request)
4373 GstRTSPMessage response = { 0 };
4376 GST_DEBUG_OBJECT (src, "got server request message");
4379 gst_rtsp_message_dump (request);
4381 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4383 if (res == GST_RTSP_ENOTIMPL) {
4384 /* default implementation, send OK */
4385 GST_DEBUG_OBJECT (src, "prepare OK reply");
4387 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4392 /* let app parse and reply */
4393 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4394 0, request, &response);
4397 gst_rtsp_message_dump (&response);
4399 res = gst_rtspsrc_connection_send (src, conninfo, &response, NULL);
4403 gst_rtsp_message_unset (&response);
4404 } else if (res == GST_RTSP_EEOF)
4412 gst_rtsp_message_unset (&response);
4417 /* send server keep-alive */
4418 static GstRTSPResult
4419 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4421 GstRTSPMessage request = { 0 };
4423 GstRTSPMethod method;
4424 const gchar *control;
4426 if (src->do_rtsp_keep_alive == FALSE) {
4427 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4428 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4432 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4434 /* find a method to use for keep-alive */
4435 if (src->methods & GST_RTSP_GET_PARAMETER)
4436 method = GST_RTSP_GET_PARAMETER;
4438 method = GST_RTSP_OPTIONS;
4440 control = get_aggregate_control (src);
4441 if (control == NULL)
4444 res = gst_rtspsrc_init_request (src, &request, method, control);
4449 gst_rtsp_message_dump (&request);
4451 res = gst_rtspsrc_connection_send (src, &src->conninfo, &request, NULL);
4455 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4456 gst_rtsp_message_unset (&request);
4463 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4468 gchar *str = gst_rtsp_strresult (res);
4470 gst_rtsp_message_unset (&request);
4471 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4472 ("Could not send keep-alive. (%s)", str));
4478 static GstFlowReturn
4479 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4481 GstFlowReturn ret = GST_FLOW_OK;
4483 GstRTSPStream *stream;
4484 GstPad *outpad = NULL;
4490 channel = message->type_data.data.channel;
4492 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4494 goto unknown_stream;
4496 if (channel == stream->channel[0]) {
4497 outpad = stream->channelpad[0];
4499 } else if (channel == stream->channel[1]) {
4500 outpad = stream->channelpad[1];
4506 /* take a look at the body to figure out what we have */
4507 gst_rtsp_message_get_body (message, &data, &size);
4509 goto invalid_length;
4511 /* channels are not correct on some servers, do extra check */
4512 if (data[1] >= 200 && data[1] <= 204) {
4513 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4514 outpad = stream->channelpad[1];
4518 /* we have no clue what this is, just ignore then. */
4520 goto unknown_stream;
4522 /* take the message body for further processing */
4523 gst_rtsp_message_steal_body (message, &data, &size);
4525 /* strip the trailing \0 */
4528 buf = gst_buffer_new ();
4529 gst_buffer_append_memory (buf,
4530 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4532 /* don't need message anymore */
4533 gst_rtsp_message_unset (message);
4535 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4538 if (src->need_activate) {
4544 guint group_id = gst_util_group_id_next ();
4546 /* generate an SHA256 sum of the URI */
4547 cs = g_checksum_new (G_CHECKSUM_SHA256);
4548 uri = src->conninfo.location;
4549 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4551 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4552 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4556 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4557 event = gst_event_new_stream_start (stream_id);
4558 gst_event_set_group_id (event, group_id);
4561 gst_rtspsrc_stream_push_event (src, ostream, event);
4563 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
4564 /* only streams that have a connection to the outside world */
4565 if (ostream->setup) {
4566 if (ostream->udpsrc[0]) {
4567 gst_element_send_event (ostream->udpsrc[0],
4568 gst_event_new_caps (caps));
4569 } else if (ostream->channelpad[0]) {
4570 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
4571 gst_pad_push_event (ostream->channelpad[0],
4572 gst_event_new_caps (caps));
4574 gst_pad_send_event (ostream->channelpad[0],
4575 gst_event_new_caps (caps));
4577 ostream->need_caps = FALSE;
4579 if (ostream->profile == GST_RTSP_PROFILE_SAVP ||
4580 ostream->profile == GST_RTSP_PROFILE_SAVPF)
4581 caps = gst_caps_new_empty_simple ("application/x-srtcp");
4583 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4585 if (ostream->udpsrc[1]) {
4586 gst_element_send_event (ostream->udpsrc[1],
4587 gst_event_new_caps (caps));
4588 } else if (ostream->channelpad[1]) {
4589 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
4590 gst_pad_push_event (ostream->channelpad[1],
4591 gst_event_new_caps (caps));
4593 gst_pad_send_event (ostream->channelpad[1],
4594 gst_event_new_caps (caps));
4597 gst_caps_unref (caps);
4601 g_checksum_free (cs);
4603 gst_rtspsrc_activate_streams (src);
4604 src->need_activate = FALSE;
4605 src->need_segment = TRUE;
4608 if (src->base_time == -1) {
4609 /* Take current running_time. This timestamp will be put on
4610 * the first buffer of each stream because we are a live source and so we
4611 * timestamp with the running_time. When we are dealing with TCP, we also
4612 * only timestamp the first buffer (using the DISCONT flag) because a server
4613 * typically bursts data, for which we don't want to compensate by speeding
4614 * up the media. The other timestamps will be interpollated from this one
4615 * using the RTP timestamps. */
4616 GST_OBJECT_LOCK (src);
4617 if (GST_ELEMENT_CLOCK (src)) {
4619 GstClockTime base_time;
4621 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4622 base_time = GST_ELEMENT_CAST (src)->base_time;
4624 src->base_time = now - base_time;
4626 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4627 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4629 GST_OBJECT_UNLOCK (src);
4632 /* If needed send a new segment, don't forget we are live and buffer are
4633 * timestamped with running time */
4634 if (src->need_segment) {
4636 src->need_segment = FALSE;
4637 gst_segment_init (&segment, GST_FORMAT_TIME);
4638 gst_rtspsrc_push_event (src, gst_event_new_segment (&segment));
4641 if (stream->need_caps) {
4644 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt))) {
4645 /* only streams that have a connection to the outside world */
4646 if (stream->setup) {
4647 /* Only need to update the TCP caps here, UDP is already handled */
4648 if (stream->channelpad[0]) {
4649 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4650 gst_pad_push_event (stream->channelpad[0],
4651 gst_event_new_caps (caps));
4653 gst_pad_send_event (stream->channelpad[0],
4654 gst_event_new_caps (caps));
4656 stream->need_caps = FALSE;
4660 stream->need_caps = FALSE;
4663 if (stream->discont && !is_rtcp) {
4664 /* mark first RTP buffer as discont */
4665 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4666 stream->discont = FALSE;
4667 /* first buffer gets the timestamp, other buffers are not timestamped and
4668 * their presentation time will be interpollated from the rtp timestamps. */
4669 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4670 GST_TIME_ARGS (src->base_time));
4672 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4675 /* chain to the peer pad */
4676 if (GST_PAD_IS_SINK (outpad))
4677 ret = gst_pad_chain (outpad, buf);
4679 ret = gst_pad_push (outpad, buf);
4682 /* combine all stream flows for the data transport */
4683 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4690 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4691 gst_rtsp_message_unset (message);
4696 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4697 ("Short message received, ignoring."));
4698 gst_rtsp_message_unset (message);
4703 static GstFlowReturn
4704 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4706 GstRTSPMessage message = { 0 };
4708 GstFlowReturn ret = GST_FLOW_OK;
4709 GTimeVal tv_timeout;
4712 /* get the next timeout interval */
4713 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4715 /* see if the timeout period expired */
4716 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4717 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4718 /* send keep-alive, only act on interrupt, a warning will be posted for
4720 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4722 /* get new timeout */
4723 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4726 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4727 tv_timeout.tv_sec, tv_timeout.tv_usec);
4729 /* protect the connection with the connection lock so that we can see when
4730 * we are finished doing server communication */
4732 gst_rtspsrc_connection_receive (src, &src->conninfo,
4733 &message, src->ptcp_timeout);
4737 GST_DEBUG_OBJECT (src, "we received a server message");
4739 case GST_RTSP_EINTR:
4740 /* we got interrupted this means we need to stop */
4742 case GST_RTSP_ETIMEOUT:
4743 /* no reply, send keep alive */
4744 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4745 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4749 /* go EOS when the server closed the connection */
4755 switch (message.type) {
4756 case GST_RTSP_MESSAGE_REQUEST:
4757 /* server sends us a request message, handle it */
4758 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
4759 if (res == GST_RTSP_EEOF)
4762 goto handle_request_failed;
4764 case GST_RTSP_MESSAGE_RESPONSE:
4765 /* we ignore response messages */
4766 GST_DEBUG_OBJECT (src, "ignoring response message");
4768 gst_rtsp_message_dump (&message);
4770 case GST_RTSP_MESSAGE_DATA:
4771 GST_DEBUG_OBJECT (src, "got data message");
4772 ret = gst_rtspsrc_handle_data (src, &message);
4773 if (ret != GST_FLOW_OK)
4774 goto handle_data_failed;
4777 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4782 g_assert_not_reached ();
4787 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4788 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4789 ("The server closed the connection."));
4790 src->conninfo.connected = FALSE;
4791 gst_rtsp_message_unset (&message);
4792 return GST_FLOW_EOS;
4796 gst_rtsp_message_unset (&message);
4797 GST_DEBUG_OBJECT (src, "got interrupted");
4798 return GST_FLOW_FLUSHING;
4802 gchar *str = gst_rtsp_strresult (res);
4804 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4805 ("Could not receive message. (%s)", str));
4808 gst_rtsp_message_unset (&message);
4809 return GST_FLOW_ERROR;
4811 handle_request_failed:
4813 gchar *str = gst_rtsp_strresult (res);
4815 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4816 ("Could not handle server message. (%s)", str));
4818 gst_rtsp_message_unset (&message);
4819 return GST_FLOW_ERROR;
4823 GST_DEBUG_OBJECT (src, "could no handle data message");
4828 static GstFlowReturn
4829 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4832 GstRTSPMessage message = { 0 };
4836 GTimeVal tv_timeout;
4838 /* get the next timeout interval */
4839 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4841 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4842 (gint) tv_timeout.tv_sec);
4844 gst_rtsp_message_unset (&message);
4846 /* we should continue reading the TCP socket because the server might
4847 * send us requests. When the session timeout expires, we need to send a
4848 * keep-alive request to keep the session open. */
4849 res = gst_rtspsrc_connection_receive (src, &src->conninfo,
4850 &message, &tv_timeout);
4854 GST_DEBUG_OBJECT (src, "we received a server message");
4856 case GST_RTSP_EINTR:
4857 /* we got interrupted, see what we have to do */
4859 case GST_RTSP_ETIMEOUT:
4860 /* send keep-alive, ignore the result, a warning will be posted. */
4861 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4862 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4866 /* server closed the connection. not very fatal for UDP, reconnect and
4867 * see what happens. */
4868 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4869 ("The server closed the connection."));
4870 if (src->udp_reconnect) {
4872 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4879 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4881 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4882 ("Unhandled return value %d.", res));
4886 switch (message.type) {
4887 case GST_RTSP_MESSAGE_REQUEST:
4888 /* server sends us a request message, handle it */
4889 res = gst_rtspsrc_handle_request (src, &src->conninfo, &message);
4890 if (res == GST_RTSP_EEOF)
4893 goto handle_request_failed;
4895 case GST_RTSP_MESSAGE_RESPONSE:
4896 /* we ignore response and data messages */
4897 GST_DEBUG_OBJECT (src, "ignoring response message");
4899 gst_rtsp_message_dump (&message);
4900 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4901 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4902 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4903 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4904 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4911 case GST_RTSP_MESSAGE_DATA:
4912 /* we ignore response and data messages */
4913 GST_DEBUG_OBJECT (src, "ignoring data message");
4916 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4921 g_assert_not_reached ();
4923 /* we get here when the connection got interrupted */
4926 gst_rtsp_message_unset (&message);
4927 GST_DEBUG_OBJECT (src, "got interrupted");
4928 return GST_FLOW_FLUSHING;
4932 gchar *str = gst_rtsp_strresult (res);
4935 src->conninfo.connected = FALSE;
4936 if (res != GST_RTSP_EINTR) {
4937 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4938 ("Could not connect to server. (%s)", str));
4940 ret = GST_FLOW_ERROR;
4942 ret = GST_FLOW_FLUSHING;
4948 gchar *str = gst_rtsp_strresult (res);
4950 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4951 ("Could not receive message. (%s)", str));
4953 return GST_FLOW_ERROR;
4955 handle_request_failed:
4957 gchar *str = gst_rtsp_strresult (res);
4960 gst_rtsp_message_unset (&message);
4961 if (res != GST_RTSP_EINTR) {
4962 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4963 ("Could not handle server message. (%s)", str));
4965 ret = GST_FLOW_ERROR;
4967 ret = GST_FLOW_FLUSHING;
4973 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4974 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4975 ("The server closed the connection."));
4976 src->conninfo.connected = FALSE;
4977 gst_rtsp_message_unset (&message);
4978 return GST_FLOW_EOS;
4982 static GstRTSPResult
4983 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4985 GstRTSPResult res = GST_RTSP_OK;
4988 GST_DEBUG_OBJECT (src, "doing reconnect");
4990 GST_OBJECT_LOCK (src);
4991 /* only restart when the pads were not yet activated, else we were
4992 * streaming over UDP */
4993 restart = src->need_activate;
4994 GST_OBJECT_UNLOCK (src);
4996 /* no need to restart, we're done */
5000 /* we can try only TCP now */
5001 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
5003 /* close and cleanup our state */
5004 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
5007 /* see if we have TCP left to try. Also don't try TCP when we were configured
5009 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
5012 /* We post a warning message now to inform the user
5013 * that nothing happened. It's most likely a firewall thing. */
5014 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5015 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5016 "firewall is blocking it. Retrying using a tcp connection.",
5017 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
5019 /* open new connection using tcp */
5020 if (gst_rtspsrc_open (src, async) < 0)
5023 /* start playback */
5024 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
5033 src->cur_protocols = 0;
5034 /* no transport possible, post an error and stop */
5035 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5036 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5037 "firewall is blocking it. No other protocols to try.",
5038 gst_guint64_to_gdouble (src->udp_timeout) / 1000000.0));
5039 return GST_RTSP_ERROR;
5043 GST_DEBUG_OBJECT (src, "open failed");
5048 GST_DEBUG_OBJECT (src, "play failed");
5054 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
5058 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
5061 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
5064 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
5067 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
5075 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
5079 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
5082 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
5085 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
5088 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
5096 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
5100 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
5103 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
5106 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
5109 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
5117 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
5121 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
5124 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
5127 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
5130 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
5138 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
5140 if (ret == GST_RTSP_OK)
5141 gst_rtspsrc_loop_complete_cmd (src, cmd);
5142 else if (ret == GST_RTSP_EINTR)
5143 gst_rtspsrc_loop_cancel_cmd (src, cmd);
5145 gst_rtspsrc_loop_error_cmd (src, cmd);
5149 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
5152 gboolean flushed = FALSE;
5154 /* start new request */
5155 gst_rtspsrc_loop_start_cmd (src, cmd);
5157 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
5159 GST_OBJECT_LOCK (src);
5160 old = src->pending_cmd;
5161 if (old == CMD_RECONNECT) {
5162 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
5163 cmd = CMD_RECONNECT;
5164 } else if (old == CMD_CLOSE) {
5165 /* our CMD_CLOSE might have interrutped CMD_LOOP. gst_rtspsrc_loop
5166 * will send a CMD_WAIT which would cancel our pending CMD_CLOSE (if
5167 * still pending). We just avoid it here by making sure CMD_CLOSE is
5168 * still the pending command. */
5169 GST_DEBUG_OBJECT (src, "ignore, we were closing");
5171 } else if (old != CMD_WAIT) {
5172 src->pending_cmd = CMD_WAIT;
5173 GST_OBJECT_UNLOCK (src);
5174 /* cancel previous request */
5175 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
5176 gst_rtspsrc_loop_cancel_cmd (src, old);
5177 GST_OBJECT_LOCK (src);
5179 src->pending_cmd = cmd;
5180 /* interrupt if allowed */
5181 if (src->busy_cmd & mask) {
5182 GST_DEBUG_OBJECT (src, "connection flush busy %s",
5183 cmd_to_string (src->busy_cmd));
5184 gst_rtspsrc_connection_flush (src, TRUE);
5187 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
5188 cmd_to_string (src->busy_cmd));
5191 gst_task_start (src->task);
5192 GST_OBJECT_UNLOCK (src);
5198 gst_rtspsrc_loop (GstRTSPSrc * src)
5202 if (!src->conninfo.connection || !src->conninfo.connected)
5205 if (src->interleaved)
5206 ret = gst_rtspsrc_loop_interleaved (src);
5208 ret = gst_rtspsrc_loop_udp (src);
5210 if (ret != GST_FLOW_OK)
5218 GST_WARNING_OBJECT (src, "we are not connected");
5219 ret = GST_FLOW_FLUSHING;
5224 const gchar *reason = gst_flow_get_name (ret);
5226 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
5227 src->running = FALSE;
5228 if (ret == GST_FLOW_EOS) {
5229 /* perform EOS logic */
5230 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5231 gst_element_post_message (GST_ELEMENT_CAST (src),
5232 gst_message_new_segment_done (GST_OBJECT_CAST (src),
5233 src->segment.format, src->segment.position));
5234 gst_rtspsrc_push_event (src,
5235 gst_event_new_segment_done (src->segment.format,
5236 src->segment.position));
5238 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5240 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
5241 /* for fatal errors we post an error message, post the error before the
5242 * EOS so the app knows about the error first. */
5243 GST_ELEMENT_FLOW_ERROR (src, ret);
5244 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5246 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
5251 #ifndef GST_DISABLE_GST_DEBUG
5252 static const gchar *
5253 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
5257 while (method != 0) {
5274 /* Parse a WWW-Authenticate Response header and determine the
5275 * available authentication methods
5277 * This code should also cope with the fact that each WWW-Authenticate
5278 * header can contain multiple challenge methods + tokens
5280 * At the moment, for Basic auth, we just do a minimal check and don't
5281 * even parse out the realm */
5283 gst_rtspsrc_parse_auth_hdr (GstRTSPMessage * response,
5284 GstRTSPAuthMethod * methods, GstRTSPConnection * conn, gboolean * stale)
5286 GstRTSPAuthCredential **credentials, **credential;
5288 g_return_if_fail (response != NULL);
5289 g_return_if_fail (methods != NULL);
5290 g_return_if_fail (stale != NULL);
5293 gst_rtsp_message_parse_auth_credentials (response,
5294 GST_RTSP_HDR_WWW_AUTHENTICATE);
5298 credential = credentials;
5299 while (*credential) {
5300 if ((*credential)->scheme == GST_RTSP_AUTH_BASIC) {
5301 *methods |= GST_RTSP_AUTH_BASIC;
5302 } else if ((*credential)->scheme == GST_RTSP_AUTH_DIGEST) {
5303 GstRTSPAuthParam **param = (*credential)->params;
5305 *methods |= GST_RTSP_AUTH_DIGEST;
5307 gst_rtsp_connection_clear_auth_params (conn);
5311 if (strcmp ((*param)->name, "stale") == 0
5312 && g_ascii_strcasecmp ((*param)->value, "TRUE") == 0)
5314 gst_rtsp_connection_set_auth_param (conn, (*param)->name,
5323 gst_rtsp_auth_credentials_free (credentials);
5327 * gst_rtspsrc_setup_auth:
5328 * @src: the rtsp source
5330 * Configure a username and password and auth method on the
5331 * connection object based on a response we received from the
5334 * Currently, this requires that a username and password were supplied
5335 * in the uri. In the future, they may be requested on demand by sending
5336 * a message up the bus.
5338 * Returns: TRUE if authentication information could be set up correctly.
5341 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5345 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5346 GstRTSPAuthMethod method;
5347 GstRTSPResult auth_result;
5349 GstRTSPConnection *conn;
5350 gboolean stale = FALSE;
5352 conn = src->conninfo.connection;
5354 /* Identify the available auth methods and see if any are supported */
5355 gst_rtspsrc_parse_auth_hdr (response, &avail_methods, conn, &stale);
5357 if (avail_methods == GST_RTSP_AUTH_NONE)
5358 goto no_auth_available;
5360 /* For digest auth, if the response indicates that the session
5361 * data are stale, we just update them in the connection object and
5362 * return TRUE to retry the request */
5364 src->tried_url_auth = FALSE;
5366 url = gst_rtsp_connection_get_url (conn);
5368 /* Do we have username and password available? */
5369 if (url != NULL && !src->tried_url_auth && url->user != NULL
5370 && url->passwd != NULL) {
5373 src->tried_url_auth = TRUE;
5374 GST_DEBUG_OBJECT (src,
5375 "Attempting authentication using credentials from the URL");
5377 user = src->user_id;
5378 pass = src->user_pw;
5379 GST_DEBUG_OBJECT (src,
5380 "Attempting authentication using credentials from the properties");
5383 /* FIXME: If the url didn't contain username and password or we tried them
5384 * already, request a username and passwd from the application via some kind
5385 * of credentials request message */
5387 /* If we don't have a username and passwd at this point, bail out. */
5388 if (user == NULL || pass == NULL)
5391 /* Try to configure for each available authentication method, strongest to
5393 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5394 /* Check if this method is available on the server */
5395 if ((method & avail_methods) == 0)
5398 /* Pass the credentials to the connection to try on the next request */
5399 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5400 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5401 * ignore it and end up retrying later */
5402 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5403 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5404 gst_rtsp_auth_method_to_string (method));
5409 if (method == GST_RTSP_AUTH_NONE)
5410 goto no_auth_available;
5416 /* Output an error indicating that we couldn't connect because there were
5417 * no supported authentication protocols */
5418 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5419 ("No supported authentication protocol was found"));
5424 /* We don't fire an error message, we just return FALSE and let the
5425 * normal NOT_AUTHORIZED error be propagated */
5430 static GstRTSPResult
5431 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
5432 GstRTSPMessage * request, GstRTSPMessage * response,
5433 GstRTSPStatusCode * code)
5436 GstRTSPStatusCode thecode;
5437 gchar *content_base = NULL;
5441 if (!src->short_header)
5442 gst_rtsp_ext_list_before_send (src->extensions, request);
5444 GST_DEBUG_OBJECT (src, "sending message");
5447 gst_rtsp_message_dump (request);
5449 res = gst_rtspsrc_connection_send (src, conninfo, request, src->ptcp_timeout);
5453 gst_rtsp_connection_reset_timeout (conninfo->connection);
5457 gst_rtspsrc_connection_receive (src, conninfo, response,
5463 gst_rtsp_message_dump (response);
5465 switch (response->type) {
5466 case GST_RTSP_MESSAGE_REQUEST:
5467 res = gst_rtspsrc_handle_request (src, conninfo, response);
5468 if (res == GST_RTSP_EEOF)
5471 goto handle_request_failed;
5473 case GST_RTSP_MESSAGE_RESPONSE:
5474 /* ok, a response is good */
5475 GST_DEBUG_OBJECT (src, "received response message");
5477 case GST_RTSP_MESSAGE_DATA:
5478 /* get next response */
5479 GST_DEBUG_OBJECT (src, "handle data response message");
5480 gst_rtspsrc_handle_data (src, response);
5483 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5488 thecode = response->type_data.response.code;
5490 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5492 /* if the caller wanted the result code, we store it. */
5496 /* If the request didn't succeed, bail out before doing any more */
5497 if (thecode != GST_RTSP_STS_OK)
5500 /* store new content base if any */
5501 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5504 g_free (src->content_base);
5505 src->content_base = g_strdup (content_base);
5507 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5514 gchar *str = gst_rtsp_strresult (res);
5516 if (res != GST_RTSP_EINTR) {
5517 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5518 ("Could not send message. (%s)", str));
5520 GST_WARNING_OBJECT (src, "send interrupted");
5529 GST_WARNING_OBJECT (src, "server closed connection");
5530 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5532 /* if reconnect succeeds, try again */
5534 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5538 /* only try once after reconnect, then fallthrough and error out */
5541 gchar *str = gst_rtsp_strresult (res);
5543 if (res != GST_RTSP_EINTR) {
5544 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5545 ("Could not receive message. (%s)", str));
5547 GST_WARNING_OBJECT (src, "receive interrupted");
5555 handle_request_failed:
5557 /* ERROR was posted */
5558 gst_rtsp_message_unset (response);
5563 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5564 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5565 ("The server closed the connection."));
5566 gst_rtsp_message_unset (response);
5573 * @src: the rtsp source
5574 * @conn: the connection to send on
5575 * @request: must point to a valid request
5576 * @response: must point to an empty #GstRTSPMessage
5577 * @code: an optional code result
5579 * send @request and retrieve the response in @response. optionally @code can be
5580 * non-NULL in which case it will contain the status code of the response.
5582 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5583 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5585 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5586 * @response message) if the response code was not 200 (OK).
5588 * If the attempt results in an authentication failure, then this will attempt
5589 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5592 * Returns: #GST_RTSP_OK if the processing was successful.
5594 static GstRTSPResult
5595 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnInfo * conninfo,
5596 GstRTSPMessage * request, GstRTSPMessage * response,
5597 GstRTSPStatusCode * code)
5599 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5600 GstRTSPResult res = GST_RTSP_ERROR;
5603 GstRTSPMethod method = GST_RTSP_INVALID;
5609 /* make sure we don't loop forever */
5613 /* save method so we can disable it when the server complains */
5614 method = request->type_data.request.method;
5617 gst_rtspsrc_try_send (src, conninfo, request, response,
5622 case GST_RTSP_STS_UNAUTHORIZED:
5623 case GST_RTSP_STS_NOT_FOUND:
5624 if (gst_rtspsrc_setup_auth (src, response)) {
5625 /* Try the request/response again after configuring the auth info
5633 } while (retry == TRUE);
5635 /* If the user requested the code, let them handle errors, otherwise
5636 * post an error below */
5639 else if (int_code != GST_RTSP_STS_OK)
5640 goto error_response;
5647 GST_DEBUG_OBJECT (src, "got error %d", res);
5652 res = GST_RTSP_ERROR;
5654 switch (response->type_data.response.code) {
5655 case GST_RTSP_STS_NOT_FOUND:
5656 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_FOUND,
5659 case GST_RTSP_STS_UNAUTHORIZED:
5660 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, NOT_AUTHORIZED,
5663 case GST_RTSP_STS_MOVED_PERMANENTLY:
5664 case GST_RTSP_STS_MOVE_TEMPORARILY:
5666 gchar *new_location;
5667 GstRTSPLowerTrans transports;
5669 GST_DEBUG_OBJECT (src, "got redirection");
5670 /* if we don't have a Location Header, we must error */
5671 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5672 &new_location, 0) < 0)
5675 /* When we receive a redirect result, we go back to the INIT state after
5676 * parsing the new URI. The caller should do the needed steps to issue
5677 * a new setup when it detects this state change. */
5678 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5680 /* save current transports */
5681 if (src->conninfo.url)
5682 transports = src->conninfo.url->transports;
5684 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5686 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5688 /* set old transports */
5689 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5690 src->conninfo.url->transports = transports;
5692 src->need_redirect = TRUE;
5696 case GST_RTSP_STS_NOT_ACCEPTABLE:
5697 case GST_RTSP_STS_NOT_IMPLEMENTED:
5698 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5699 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5700 gst_rtsp_method_as_text (method));
5701 src->methods &= ~method;
5705 RTSP_SRC_RESPONSE_ERROR (src, response, RESOURCE, READ,
5709 /* if we return ERROR we should unset the response ourselves */
5710 if (res == GST_RTSP_ERROR)
5711 gst_rtsp_message_unset (response);
5717 static GstRTSPResult
5718 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5719 GstRTSPMessage * response, GstRTSPSrc * src)
5721 return gst_rtspsrc_send (src, &src->conninfo, request, response, NULL);
5725 /* parse the response and collect all the supported methods. We need this
5726 * information so that we don't try to send an unsupported request to the
5730 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5732 GstRTSPHeaderField field;
5736 /* reset supported methods */
5739 /* Try Allow Header first */
5740 field = GST_RTSP_HDR_ALLOW;
5743 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5744 if (indx == 0 && !respoptions) {
5745 /* if no Allow header was found then try the Public header... */
5746 field = GST_RTSP_HDR_PUBLIC;
5747 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5752 src->methods |= gst_rtsp_options_from_text (respoptions);
5757 if (src->methods == 0) {
5758 /* neither Allow nor Public are required, assume the server supports
5759 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5761 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5762 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5764 /* always assume PLAY, FIXME, extensions should be able to override
5766 src->methods |= GST_RTSP_PLAY;
5767 /* also assume it will support Range */
5768 src->seekable = TRUE;
5770 /* we need describe and setup */
5771 if (!(src->methods & GST_RTSP_DESCRIBE))
5773 if (!(src->methods & GST_RTSP_SETUP))
5781 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5782 ("Server does not support DESCRIBE."));
5787 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5788 ("Server does not support SETUP."));
5793 /* masks to be kept in sync with the hardcoded protocol order of preference
5795 static const guint protocol_masks[] = {
5796 GST_RTSP_LOWER_TRANS_UDP,
5797 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5798 GST_RTSP_LOWER_TRANS_TCP,
5802 static GstRTSPResult
5803 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5804 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
5808 gboolean add_udp_str;
5813 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5818 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5820 /* extension listed transports, use those */
5821 if (*transports != NULL)
5824 /* it's the default */
5825 add_udp_str = FALSE;
5827 /* the default RTSP transports */
5828 result = g_string_new ("RTP");
5831 case GST_RTSP_PROFILE_AVP:
5832 g_string_append (result, "/AVP");
5834 case GST_RTSP_PROFILE_SAVP:
5835 g_string_append (result, "/SAVP");
5837 case GST_RTSP_PROFILE_AVPF:
5838 g_string_append (result, "/AVPF");
5840 case GST_RTSP_PROFILE_SAVPF:
5841 g_string_append (result, "/SAVPF");
5847 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5848 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5850 g_string_append (result, "/UDP");
5851 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5852 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5853 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5854 /* we don't have to allocate any UDP ports yet, if the selected transport
5855 * turns out to be multicast we can create them and join the multicast
5856 * group indicated in the transport reply */
5858 g_string_append (result, "/UDP");
5859 g_string_append (result, ";multicast");
5860 if (src->next_port_num != 0) {
5861 if (src->client_port_range.max > 0 &&
5862 src->next_port_num >= src->client_port_range.max)
5865 g_string_append_printf (result, ";client_port=%d-%d",
5866 src->next_port_num, src->next_port_num + 1);
5868 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
5869 GST_DEBUG_OBJECT (src, "adding TCP");
5871 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
5873 *transports = g_string_free (result, FALSE);
5875 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5882 GST_ERROR ("extension gave error %d", res);
5887 GST_ERROR ("no more ports available");
5888 return GST_RTSP_ERROR;
5892 static GstRTSPResult
5893 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5894 gint orig_rtpport, gint orig_rtcpport)
5897 gint nr_udp, nr_int;
5899 gint rtpport = 0, rtcpport = 0;
5902 src = stream->parent;
5904 /* find number of placeholders first */
5905 if (strstr (*transports, "%%i2"))
5907 else if (strstr (*transports, "%%i1"))
5912 if (strstr (*transports, "%%u2"))
5914 else if (strstr (*transports, "%%u1"))
5919 if (nr_udp == 0 && nr_int == 0)
5923 if (!orig_rtpport || !orig_rtcpport) {
5924 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5927 rtpport = orig_rtpport;
5928 rtcpport = orig_rtcpport;
5932 str = g_string_new ("");
5934 while ((next = strstr (p, "%%"))) {
5935 g_string_append_len (str, p, next - p);
5936 if (next[2] == 'u') {
5938 g_string_append_printf (str, "%d", rtpport);
5939 else if (next[3] == '2')
5940 g_string_append_printf (str, "%d", rtcpport);
5942 if (next[2] == 'i') {
5944 g_string_append_printf (str, "%d", src->free_channel);
5945 else if (next[3] == '2')
5946 g_string_append_printf (str, "%d", src->free_channel + 1);
5951 /* append final part */
5952 g_string_append (str, p);
5954 g_free (*transports);
5955 *transports = g_string_free (str, FALSE);
5963 GST_ERROR ("failed to allocate udp ports");
5964 return GST_RTSP_ERROR;
5969 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
5971 GstCaps *caps = NULL;
5973 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
5977 GST_DEBUG_OBJECT (src, "SRTP parameters received");
5983 default_srtcp_params (void)
5990 guint data_size = GST_ROUND_UP_4 (KEY_SIZE);
5992 /* create a random key */
5993 key_data = g_malloc (data_size);
5994 for (i = 0; i < data_size; i += 4)
5995 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
5997 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
5999 caps = gst_caps_new_simple ("application/x-srtcp",
6000 "srtp-key", GST_TYPE_BUFFER, buf,
6001 "srtp-cipher", G_TYPE_STRING, "aes-128-icm",
6002 "srtp-auth", G_TYPE_STRING, "hmac-sha1-80",
6003 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
6004 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
6006 gst_buffer_unref (buf);
6012 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
6014 gchar *base64, *result = NULL;
6015 GstMIKEYMessage *mikey_msg;
6017 stream->srtcpparams = signal_get_srtcp_params (src, stream);
6018 if (stream->srtcpparams == NULL)
6019 stream->srtcpparams = default_srtcp_params ();
6021 mikey_msg = gst_mikey_message_new_from_caps (stream->srtcpparams);
6023 /* add policy '0' for our SSRC */
6024 gst_mikey_message_add_cs_srtp (mikey_msg, 0, stream->send_ssrc, 0);
6026 base64 = gst_mikey_message_base64_encode (mikey_msg);
6027 gst_mikey_message_unref (mikey_msg);
6030 result = gst_sdp_make_keymgmt (stream->conninfo.location, base64);
6038 /* Perform the SETUP request for all the streams.
6040 * We ask the server for a specific transport, which initially includes all the
6041 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
6042 * two local UDP ports that we send to the server.
6044 * Once the server replied with a transport, we configure the other streams
6045 * with the same transport.
6047 * This function will also configure the stream for the selected transport,
6048 * which basically means creating the pipeline.
6050 static GstRTSPResult
6051 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
6054 GstRTSPResult res = GST_RTSP_ERROR;
6055 GstRTSPMessage request = { 0 };
6056 GstRTSPMessage response = { 0 };
6057 GstRTSPStream *stream = NULL;
6058 GstRTSPLowerTrans protocols;
6059 GstRTSPStatusCode code;
6060 gboolean unsupported_real = FALSE;
6061 gint rtpport, rtcpport;
6065 if (src->conninfo.connection) {
6066 url = gst_rtsp_connection_get_url (src->conninfo.connection);
6067 /* we initially allow all configured lower transports. based on the URL
6068 * transports and the replies from the server we narrow them down. */
6069 protocols = url->transports & src->cur_protocols;
6072 protocols = src->cur_protocols;
6078 /* reset some state */
6079 src->free_channel = 0;
6080 src->interleaved = FALSE;
6081 src->need_activate = FALSE;
6082 /* keep track of next port number, 0 is random */
6083 src->next_port_num = src->client_port_range.min;
6084 rtpport = rtcpport = 0;
6086 if (G_UNLIKELY (src->streams == NULL))
6089 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6090 GstRTSPConnInfo *conninfo;
6097 stream = (GstRTSPStream *) walk->data;
6099 caps = stream_get_caps_for_pt (stream, stream->default_pt);
6101 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
6105 if (stream->skipped) {
6106 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
6110 /* see if we need to configure this stream */
6111 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
6112 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
6117 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
6118 stream->id, caps, &selected);
6120 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
6124 /* merge/overwrite global caps */
6129 s = gst_caps_get_structure (caps, 0);
6131 num = gst_structure_n_fields (src->props);
6132 for (j = 0; j < num; j++) {
6136 name = gst_structure_nth_field_name (src->props, j);
6137 val = gst_structure_get_value (src->props, name);
6138 gst_structure_set_value (s, name, val);
6140 GST_DEBUG_OBJECT (src, "copied %s", name);
6144 /* skip setup if we have no URL for it */
6145 if (stream->conninfo.location == NULL) {
6146 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
6150 if (src->conninfo.connection == NULL) {
6151 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
6152 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
6155 conninfo = &stream->conninfo;
6157 conninfo = &src->conninfo;
6159 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
6160 stream->conninfo.location);
6162 /* if we have a multicast connection, only suggest multicast from now on */
6163 if (stream->is_multicast)
6164 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
6167 /* first selectable protocol */
6168 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6170 if (!protocol_masks[mask])
6174 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
6175 protocol_masks[mask]);
6176 /* create a string with first transport in line */
6178 res = gst_rtspsrc_create_transports_string (src,
6179 protocols & protocol_masks[mask], stream->profile, &transports);
6180 if (res < 0 || transports == NULL)
6181 goto setup_transport_failed;
6183 if (strlen (transports) == 0) {
6184 g_free (transports);
6185 GST_DEBUG_OBJECT (src, "no transports found");
6190 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
6192 /* replace placeholders with real values, this function will optionally
6193 * allocate UDP ports and other info needed to execute the setup request */
6194 res = gst_rtspsrc_prepare_transports (stream, &transports,
6195 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
6197 g_free (transports);
6198 goto setup_transport_failed;
6201 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
6203 /* create SETUP request */
6205 gst_rtspsrc_init_request (src, &request, GST_RTSP_SETUP,
6206 stream->conninfo.location);
6208 g_free (transports);
6209 goto create_request_failed;
6212 /* select transport */
6213 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
6216 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
6217 stream->profile == GST_RTSP_PROFILE_SAVPF) {
6218 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
6219 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
6222 /* if the user wants a non default RTP packet size we add the blocksize
6224 if (src->rtp_blocksize > 0) {
6225 hval = g_strdup_printf ("%d", src->rtp_blocksize);
6226 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
6230 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
6233 /* handle the code ourselves */
6234 res = gst_rtspsrc_send (src, conninfo, &request, &response, &code);
6239 case GST_RTSP_STS_OK:
6241 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
6242 gst_rtsp_message_unset (&request);
6243 gst_rtsp_message_unset (&response);
6244 /* cleanup of leftover transport */
6245 gst_rtspsrc_stream_free_udp (stream);
6246 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
6247 * we might be in this case */
6248 if (stream->container && rtpport && rtcpport && !retry) {
6249 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
6254 /* this transport did not go down well, but we may have others to try
6255 * that we did not send yet, try those and only give up then
6256 * but not without checking for lost cause/extension so we can
6257 * post a nicer/more useful error message later */
6258 if (!unsupported_real)
6259 unsupported_real = stream->is_real;
6260 /* select next available protocol, give up on this stream if none */
6262 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6264 if (!protocol_masks[mask] || unsupported_real)
6269 /* cleanup of leftover transport and move to the next stream */
6270 gst_rtspsrc_stream_free_udp (stream);
6271 goto response_error;
6274 /* parse response transport */
6276 gchar *resptrans = NULL;
6277 GstRTSPTransport transport = { 0 };
6279 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
6282 gst_rtspsrc_stream_free_udp (stream);
6286 /* parse transport, go to next stream on parse error */
6287 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6288 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6292 /* update allowed transports for other streams. once the transport of
6293 * one stream has been determined, we make sure that all other streams
6294 * are configured in the same way */
6295 switch (transport.lower_transport) {
6296 case GST_RTSP_LOWER_TRANS_TCP:
6297 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6298 protocols = GST_RTSP_LOWER_TRANS_TCP;
6299 src->interleaved = TRUE;
6300 /* update free channels */
6302 MAX (transport.interleaved.min, src->free_channel);
6304 MAX (transport.interleaved.max, src->free_channel);
6305 src->free_channel++;
6307 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6308 /* only allow multicast for other streams */
6309 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6310 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6311 /* if the server selected our ports, increment our counters so that
6312 * we select a new port later */
6313 if (src->next_port_num == transport.port.min &&
6314 src->next_port_num + 1 == transport.port.max) {
6315 src->next_port_num += 2;
6318 case GST_RTSP_LOWER_TRANS_UDP:
6319 /* only allow unicast for other streams */
6320 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6321 protocols = GST_RTSP_LOWER_TRANS_UDP;
6324 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6325 transport.lower_transport);
6329 if (!src->interleaved || !retry) {
6330 /* now configure the stream with the selected transport */
6331 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6332 GST_DEBUG_OBJECT (src,
6333 "could not configure stream %p transport, skipping stream",
6336 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
6337 /* retain the first allocated UDP port pair */
6338 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
6339 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
6342 /* we need to activate at least one streams when we detect activity */
6343 src->need_activate = TRUE;
6345 /* stream is setup now */
6346 stream->setup = TRUE;
6351 GstRTSPStream *sskip;
6353 skip = g_list_next (skip);
6357 sskip = (GstRTSPStream *) skip->data;
6359 /* skip all streams with the same control url */
6360 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
6361 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
6362 sskip, sskip->conninfo.location);
6363 sskip->skipped = TRUE;
6368 /* clean up our transport struct */
6369 gst_rtsp_transport_init (&transport);
6370 /* clean up used RTSP messages */
6371 gst_rtsp_message_unset (&request);
6372 gst_rtsp_message_unset (&response);
6376 /* store the transport protocol that was configured */
6377 src->cur_protocols = protocols;
6379 gst_rtsp_ext_list_stream_select (src->extensions, url);
6381 /* if there is nothing to activate, error out */
6382 if (!src->need_activate)
6383 goto nothing_to_activate;
6390 /* no transport possible, post an error and stop */
6391 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6392 ("Could not connect to server, no protocols left"));
6393 return GST_RTSP_ERROR;
6397 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6398 ("SDP contains no streams"));
6399 return GST_RTSP_ERROR;
6401 create_request_failed:
6403 gchar *str = gst_rtsp_strresult (res);
6405 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6406 ("Could not create request. (%s)", str));
6410 setup_transport_failed:
6412 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6413 ("Could not setup transport."));
6414 res = GST_RTSP_ERROR;
6419 const gchar *str = gst_rtsp_status_as_text (code);
6421 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6422 ("Error (%d): %s", code, GST_STR_NULL (str)));
6423 res = GST_RTSP_ERROR;
6428 gchar *str = gst_rtsp_strresult (res);
6430 if (res != GST_RTSP_EINTR) {
6431 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6432 ("Could not send message. (%s)", str));
6434 GST_WARNING_OBJECT (src, "send interrupted");
6441 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6442 ("Server did not select transport."));
6443 res = GST_RTSP_ERROR;
6446 nothing_to_activate:
6448 /* none of the available error codes is really right .. */
6449 if (unsupported_real) {
6450 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6451 (_("No supported stream was found. You might need to install a "
6452 "GStreamer RTSP extension plugin for Real media streams.")),
6455 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6456 (_("No supported stream was found. You might need to allow "
6457 "more transport protocols or may otherwise be missing "
6458 "the right GStreamer RTSP extension plugin.")), (NULL));
6460 return GST_RTSP_ERROR;
6464 gst_rtsp_message_unset (&request);
6465 gst_rtsp_message_unset (&response);
6471 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
6472 GstSegment * segment)
6475 GstRTSPTimeRange *therange;
6478 gst_rtsp_range_free (src->range);
6480 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
6481 GST_DEBUG_OBJECT (src, "parsed range %s", range);
6482 src->range = therange;
6484 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6486 gst_segment_init (segment, GST_FORMAT_TIME);
6490 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6491 therange->min.type, therange->min.seconds, therange->max.type,
6492 therange->max.seconds);
6494 if (therange->min.type == GST_RTSP_TIME_NOW)
6496 else if (therange->min.type == GST_RTSP_TIME_END)
6499 seconds = therange->min.seconds * GST_SECOND;
6501 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6502 GST_TIME_ARGS (seconds));
6504 /* we need to start playback without clipping from the position reported by
6506 segment->start = seconds;
6507 segment->position = seconds;
6509 if (therange->max.type == GST_RTSP_TIME_NOW)
6511 else if (therange->max.type == GST_RTSP_TIME_END)
6514 seconds = therange->max.seconds * GST_SECOND;
6516 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6517 GST_TIME_ARGS (seconds));
6519 /* live (WMS) server might send overflowed large max as its idea of infinity,
6520 * compensate to prevent problems later on */
6521 if (seconds != -1 && seconds < 0) {
6523 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6526 /* live (WMS) might send min == max, which is not worth recording */
6527 if (segment->duration == -1 && seconds == segment->start)
6530 /* don't change duration with unknown value, we might have a valid value
6531 * there that we want to keep. */
6533 segment->duration = seconds;
6538 /* Parse clock profived by the server with following syntax:
6540 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6543 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6545 gboolean res = FALSE;
6547 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6548 gchar **fields = NULL, **parts = NULL;
6549 gchar *remote_ip, *str;
6551 GstClockTime base_time;
6554 fields = g_strsplit (gstclock, " ", 0);
6556 /* wrapped clock, not very interesting for now */
6557 if (fields[1] == NULL)
6560 /* remote IP address and port */
6561 if ((str = fields[2]) == NULL)
6564 parts = g_strsplit (str, ":", 0);
6566 if ((remote_ip = parts[0]) == NULL)
6569 if ((str = parts[1]) == NULL)
6577 if ((str = fields[3]) == NULL)
6580 base_time = g_ascii_strtoull (str, NULL, 10);
6583 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
6586 if (src->provided_clock)
6587 gst_object_unref (src->provided_clock);
6588 src->provided_clock = netclock;
6590 gst_element_post_message (GST_ELEMENT_CAST (src),
6591 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
6592 src->provided_clock, TRUE));
6596 g_strfreev (fields);
6602 /* must be called with the RTSP state lock */
6603 static GstRTSPResult
6604 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
6610 /* prepare global stream caps properties */
6612 gst_structure_remove_all_fields (src->props);
6614 src->props = gst_structure_new_empty ("RTSPProperties");
6617 gst_sdp_message_dump (sdp);
6619 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
6621 /* let the app inspect and change the SDP */
6622 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6624 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6626 /* parse range for duration reporting. */
6631 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6635 /* keep track of the range and configure it in the segment */
6636 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6640 /* parse clock information. This is GStreamer specific, a server can tell the
6641 * client what clock it is using and wrap that in a network clock. The
6642 * advantage of that is that we can slave to it. */
6644 const gchar *gstclock;
6647 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6648 if (gstclock == NULL)
6651 /* parse the clock and expose it in the provide_clock method */
6652 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6656 /* try to find a global control attribute. Note that a '*' means that we should
6657 * do aggregate control with the current url (so we don't do anything and
6658 * leave the current connection as is) */
6660 const gchar *control;
6663 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6664 if (control == NULL)
6667 /* only take fully qualified urls */
6668 if (g_str_has_prefix (control, "rtsp://"))
6672 g_free (src->conninfo.location);
6673 src->conninfo.location = g_strdup (control);
6674 /* make a connection for this, if there was a connection already, nothing
6676 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6677 GST_ERROR_OBJECT (src, "could not connect");
6680 /* we need to keep the control url separate from the connection url because
6681 * the rules for constructing the media control url need it */
6682 g_free (src->control);
6683 src->control = g_strdup (control);
6686 /* create streams */
6687 n_streams = gst_sdp_message_medias_len (sdp);
6688 for (i = 0; i < n_streams; i++) {
6689 gst_rtspsrc_create_stream (src, sdp, i, n_streams);
6692 src->state = GST_RTSP_STATE_INIT;
6695 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6698 /* reset our state */
6699 src->need_range = TRUE;
6702 src->state = GST_RTSP_STATE_READY;
6709 GST_ERROR_OBJECT (src, "setup failed");
6710 gst_rtspsrc_cleanup (src);
6715 static GstRTSPResult
6716 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6720 GstRTSPMessage request = { 0 };
6721 GstRTSPMessage response = { 0 };
6724 gchar *respcont = NULL;
6727 src->need_redirect = FALSE;
6729 /* can't continue without a valid url */
6730 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6731 res = GST_RTSP_EINVAL;
6734 src->tried_url_auth = FALSE;
6736 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6737 goto connect_failed;
6739 /* create OPTIONS */
6740 GST_DEBUG_OBJECT (src, "create options...");
6742 gst_rtspsrc_init_request (src, &request, GST_RTSP_OPTIONS,
6743 src->conninfo.url_str);
6745 goto create_request_failed;
6748 GST_DEBUG_OBJECT (src, "send options...");
6751 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6754 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
6759 if (!gst_rtspsrc_parse_methods (src, &response))
6762 /* create DESCRIBE */
6763 GST_DEBUG_OBJECT (src, "create describe...");
6765 gst_rtspsrc_init_request (src, &request, GST_RTSP_DESCRIBE,
6766 src->conninfo.url_str);
6768 goto create_request_failed;
6770 /* we only accept SDP for now */
6771 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
6775 GST_DEBUG_OBJECT (src, "send describe...");
6778 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
6781 gst_rtspsrc_send (src, &src->conninfo, &request, &response,
6785 /* we only perform redirect for describe and play, currently */
6786 if (src->need_redirect) {
6787 /* close connection, we don't have to send a TEARDOWN yet, ignore the
6789 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6791 gst_rtsp_message_unset (&request);
6792 gst_rtsp_message_unset (&response);
6798 /* it could be that the DESCRIBE method was not implemented */
6799 if (!(src->methods & GST_RTSP_DESCRIBE))
6802 /* check if reply is SDP */
6803 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
6805 /* could not be set but since the request returned OK, we assume it
6806 * was SDP, else check it. */
6808 const gchar *props = strchr (respcont, ';');
6811 gchar *mimetype = g_strndup (respcont, props - respcont);
6813 mimetype = g_strstrip (mimetype);
6814 if (g_ascii_strcasecmp (mimetype, "application/sdp") != 0) {
6816 goto wrong_content_type;
6819 /* TODO: Check for charset property and do conversions of all messages if
6820 * needed. Some servers actually send that property */
6823 } else if (g_ascii_strcasecmp (respcont, "application/sdp") != 0) {
6824 goto wrong_content_type;
6828 /* get message body and parse as SDP */
6829 gst_rtsp_message_get_body (&response, &data, &size);
6830 if (data == NULL || size == 0)
6833 GST_DEBUG_OBJECT (src, "parse SDP...");
6834 gst_sdp_message_new (sdp);
6835 gst_sdp_message_parse_buffer (data, size, *sdp);
6837 /* clean up any messages */
6838 gst_rtsp_message_unset (&request);
6839 gst_rtsp_message_unset (&response);
6846 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
6847 ("No valid RTSP URL was provided"));
6852 gchar *str = gst_rtsp_strresult (res);
6854 if (res != GST_RTSP_EINTR) {
6855 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6856 ("Failed to connect. (%s)", str));
6858 GST_WARNING_OBJECT (src, "connect interrupted");
6863 create_request_failed:
6865 gchar *str = gst_rtsp_strresult (res);
6867 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6868 ("Could not create request. (%s)", str));
6874 /* Don't post a message - the rtsp_send method will have
6875 * taken care of it because we passed NULL for the response code */
6880 /* error was posted */
6881 res = GST_RTSP_ERROR;
6886 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6887 ("Server does not support SDP, got %s.", respcont));
6888 res = GST_RTSP_ERROR;
6893 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6894 ("Server can not provide an SDP."));
6895 res = GST_RTSP_ERROR;
6900 if (src->conninfo.connection) {
6901 GST_DEBUG_OBJECT (src, "free connection");
6902 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6904 gst_rtsp_message_unset (&request);
6905 gst_rtsp_message_unset (&response);
6910 static GstRTSPResult
6911 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
6916 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
6918 if (src->sdp == NULL) {
6919 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
6923 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
6928 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
6935 GST_WARNING_OBJECT (src, "can't get sdp");
6936 src->open_error = TRUE;
6941 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
6942 src->open_error = TRUE;
6947 static GstRTSPResult
6948 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
6950 GstRTSPMessage request = { 0 };
6951 GstRTSPMessage response = { 0 };
6952 GstRTSPResult res = GST_RTSP_OK;
6954 const gchar *control;
6956 GST_DEBUG_OBJECT (src, "TEARDOWN...");
6958 gst_rtspsrc_set_state (src, GST_STATE_READY);
6960 if (src->state < GST_RTSP_STATE_READY) {
6961 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
6968 /* construct a control url */
6969 control = get_aggregate_control (src);
6971 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
6974 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6975 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6976 const gchar *setup_url;
6977 GstRTSPConnInfo *info;
6979 /* try aggregate control first but do non-aggregate control otherwise */
6981 setup_url = control;
6982 else if ((setup_url = stream->conninfo.location) == NULL)
6985 if (src->conninfo.connection) {
6986 info = &src->conninfo;
6987 } else if (stream->conninfo.connection) {
6988 info = &stream->conninfo;
6992 if (!info->connected)
6997 gst_rtspsrc_init_request (src, &request, GST_RTSP_TEARDOWN, setup_url);
6999 goto create_request_failed;
7002 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
7004 if ((res = gst_rtspsrc_send (src, info, &request, &response, NULL)) < 0)
7007 /* FIXME, parse result? */
7008 gst_rtsp_message_unset (&request);
7009 gst_rtsp_message_unset (&response);
7012 /* early exit when we did aggregate control */
7018 /* close connections */
7019 GST_DEBUG_OBJECT (src, "closing connection...");
7020 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7021 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7022 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7023 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
7027 gst_rtspsrc_cleanup (src);
7029 src->state = GST_RTSP_STATE_INVALID;
7032 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
7037 create_request_failed:
7039 gchar *str = gst_rtsp_strresult (res);
7041 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7042 ("Could not create request. (%s)", str));
7048 gchar *str = gst_rtsp_strresult (res);
7050 gst_rtsp_message_unset (&request);
7051 if (res != GST_RTSP_EINTR) {
7052 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7053 ("Could not send message. (%s)", str));
7055 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
7062 GST_DEBUG_OBJECT (src,
7063 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
7068 /* RTP-Info is of the format:
7070 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
7072 * rtptime corresponds to the timestamp for the NPT time given in the header
7073 * seqbase corresponds to the next sequence number we received. This number
7074 * indicates the first seqnum after the seek and should be used to discard
7075 * packets that are from before the seek.
7078 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
7083 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
7085 infos = g_strsplit (rtpinfo, ",", 0);
7086 for (i = 0; infos[i]; i++) {
7088 GstRTSPStream *stream;
7092 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
7094 /* init values, types of seqbase and timebase are bigger than needed so we
7095 * can store -1 as uninitialized values */
7100 /* parse url, find stream for url.
7101 * parse seq and rtptime. The seq number should be configured in the rtp
7102 * depayloader or session manager to detect gaps. Same for the rtptime, it
7103 * should be used to create an initial time newsegment. */
7104 fields = g_strsplit (infos[i], ";", 0);
7105 for (j = 0; fields[j]; j++) {
7106 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
7107 /* remove leading whitespace */
7108 fields[j] = g_strchug (fields[j]);
7109 if (g_str_has_prefix (fields[j], "url=")) {
7110 /* get the url and the stream */
7112 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
7113 } else if (g_str_has_prefix (fields[j], "seq=")) {
7114 seqbase = atoi (fields[j] + 4);
7115 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
7116 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
7119 g_strfreev (fields);
7120 /* now we need to store the values for the caps of the stream */
7121 if (stream != NULL) {
7122 GST_DEBUG_OBJECT (src,
7123 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
7124 stream, seqbase, timebase);
7126 /* we have a stream, configure detected params */
7127 stream->seqbase = seqbase;
7128 stream->timebase = timebase;
7137 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
7142 interval = strtoul (rtcp, NULL, 10);
7143 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
7148 interval *= GST_MSECOND;
7150 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7151 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7153 /* already (optionally) retrieved this when configuring manager */
7154 if (stream->session) {
7155 GObject *rtpsession = stream->session;
7157 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
7159 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
7163 /* now it happens that (Xenon) server sending this may also provide bogus
7164 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
7165 * and just use RTP-Info to sync */
7167 GObjectClass *klass;
7169 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
7170 if (g_object_class_find_property (klass, "rtcp-sync")) {
7171 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
7172 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
7178 gst_rtspsrc_get_float (const gchar * dstr)
7180 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7182 /* canonicalise floating point string so we can handle float strings
7183 * in the form "24.930" or "24,930" irrespective of the current locale */
7184 g_strlcpy (s, dstr, sizeof (s));
7185 g_strdelimit (s, ",", '.');
7186 return g_ascii_strtod (s, NULL);
7190 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
7192 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7194 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
7195 g_strlcpy (val_str, "now", sizeof (val_str));
7197 if (segment->position == 0) {
7198 g_strlcpy (val_str, "0", sizeof (val_str));
7200 g_ascii_dtostr (val_str, sizeof (val_str),
7201 ((gdouble) segment->position) / GST_SECOND);
7204 return g_strdup_printf ("npt=%s-", val_str);
7208 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
7212 stream->timebase = -1;
7213 stream->seqbase = -1;
7215 len = stream->ptmap->len;
7216 for (i = 0; i < len; i++) {
7217 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
7220 if (item->caps == NULL)
7223 item->caps = gst_caps_make_writable (item->caps);
7224 s = gst_caps_get_structure (item->caps, 0);
7225 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
7226 if (item->pt == stream->default_pt && stream->udpsrc[0])
7227 g_object_set (stream->udpsrc[0], "caps", item->caps, NULL);
7229 stream->need_caps = TRUE;
7232 static GstRTSPResult
7233 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
7235 GstRTSPResult res = GST_RTSP_OK;
7237 if (src->state < GST_RTSP_STATE_READY) {
7238 res = GST_RTSP_ERROR;
7239 if (src->open_error) {
7240 GST_DEBUG_OBJECT (src, "the stream was in error");
7244 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
7246 if ((res = gst_rtspsrc_open (src, async)) < 0) {
7247 GST_DEBUG_OBJECT (src, "failed to open stream");
7256 static GstRTSPResult
7257 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
7259 GstRTSPMessage request = { 0 };
7260 GstRTSPMessage response = { 0 };
7261 GstRTSPResult res = GST_RTSP_OK;
7265 const gchar *control;
7267 GST_DEBUG_OBJECT (src, "PLAY...");
7270 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7273 if (!(src->methods & GST_RTSP_PLAY))
7276 if (src->state == GST_RTSP_STATE_PLAYING)
7279 if (!src->conninfo.connection || !src->conninfo.connected)
7282 /* send some dummy packets before we activate the receive in the
7284 gst_rtspsrc_send_dummy_packets (src);
7286 /* require new SR packets */
7288 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
7290 /* construct a control url */
7291 control = get_aggregate_control (src);
7293 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7294 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7295 const gchar *setup_url;
7296 GstRTSPConnInfo *conninfo;
7298 /* try aggregate control first but do non-aggregate control otherwise */
7300 setup_url = control;
7301 else if ((setup_url = stream->conninfo.location) == NULL)
7304 if (src->conninfo.connection) {
7305 conninfo = &src->conninfo;
7306 } else if (stream->conninfo.connection) {
7307 conninfo = &stream->conninfo;
7313 res = gst_rtspsrc_init_request (src, &request, GST_RTSP_PLAY, setup_url);
7315 goto create_request_failed;
7317 if (src->need_range) {
7318 hval = gen_range_header (src, segment);
7320 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
7322 /* store the newsegment event so it can be sent from the streaming thread. */
7323 src->need_segment = TRUE;
7326 if (segment->rate != 1.0) {
7327 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
7329 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
7331 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
7333 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
7337 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
7339 if ((res = gst_rtspsrc_send (src, conninfo, &request, &response, NULL)) < 0)
7342 if (src->need_redirect) {
7343 GST_DEBUG_OBJECT (src,
7344 "redirect: tearing down and restarting with new url");
7345 /* teardown and restart with new url */
7346 gst_rtspsrc_close (src, TRUE, FALSE);
7347 /* reset protocols to force re-negotiation with redirected url */
7348 src->cur_protocols = src->protocols;
7349 gst_rtsp_message_unset (&request);
7350 gst_rtsp_message_unset (&response);
7354 /* seek may have silently failed as it is not supported */
7355 if (!(src->methods & GST_RTSP_PLAY)) {
7356 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
7357 /* obviously it is supported as we made it here */
7358 src->methods |= GST_RTSP_PLAY;
7359 src->seekable = FALSE;
7360 /* but there is nothing to parse in the response,
7361 * so convey we have no idea and not to expect anything particular */
7362 clear_rtp_base (src, stream);
7366 /* need to do for all streams */
7367 for (run = src->streams; run; run = g_list_next (run))
7368 clear_rtp_base (src, (GstRTSPStream *) run->data);
7370 /* NOTE the above also disables npt based eos detection */
7371 /* and below forces position to 0,
7372 * which is visible feedback we lost the plot */
7373 segment->start = segment->position = src->last_pos;
7376 gst_rtsp_message_unset (&request);
7378 /* parse RTP npt field. This is the current position in the stream (Normal
7379 * Play Time) and should be put in the NEWSEGMENT position field. */
7380 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
7382 gst_rtspsrc_parse_range (src, hval, segment);
7384 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
7385 segment->rate = 1.0;
7387 /* parse Speed header. This is the intended playback rate of the stream
7388 * and should be put in the NEWSEGMENT rate field. */
7389 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
7390 0) == GST_RTSP_OK) {
7391 segment->rate = gst_rtspsrc_get_float (hval);
7392 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
7393 &hval, 0) == GST_RTSP_OK) {
7394 segment->rate = gst_rtspsrc_get_float (hval);
7397 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
7398 * for the RTP packets. If this is not present, we assume all starts from 0...
7399 * This is info for the RTP session manager that we pass to it in caps. */
7401 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
7402 &hval, hval_idx++) == GST_RTSP_OK)
7403 gst_rtspsrc_parse_rtpinfo (src, hval);
7405 /* some servers indicate RTCP parameters in PLAY response,
7406 * rather than properly in SDP */
7407 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
7408 &hval, 0) == GST_RTSP_OK)
7409 gst_rtspsrc_handle_rtcp_interval (src, hval);
7411 gst_rtsp_message_unset (&response);
7413 /* early exit when we did aggregate control */
7417 /* configure the caps of the streams after we parsed all headers. Only reset
7418 * the manager object when we set a new Range header (we did a seek) */
7419 gst_rtspsrc_configure_caps (src, segment, src->need_range);
7421 /* set to PLAYING after we have configured the caps, otherwise we
7422 * might end up calling request_key (with SRTP) while caps are still
7423 * being configured. */
7424 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
7426 /* set again when needed */
7427 src->need_range = FALSE;
7429 src->running = TRUE;
7430 src->base_time = -1;
7431 src->state = GST_RTSP_STATE_PLAYING;
7434 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
7435 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7436 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7437 stream->discont = TRUE;
7442 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
7449 GST_DEBUG_OBJECT (src, "failed to open stream");
7454 GST_DEBUG_OBJECT (src, "PLAY is not supported");
7459 GST_DEBUG_OBJECT (src, "we were already PLAYING");
7462 create_request_failed:
7464 gchar *str = gst_rtsp_strresult (res);
7466 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7467 ("Could not create request. (%s)", str));
7473 gchar *str = gst_rtsp_strresult (res);
7475 gst_rtsp_message_unset (&request);
7476 if (res != GST_RTSP_EINTR) {
7477 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7478 ("Could not send message. (%s)", str));
7480 GST_WARNING_OBJECT (src, "PLAY interrupted");
7487 static GstRTSPResult
7488 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
7490 GstRTSPResult res = GST_RTSP_OK;
7491 GstRTSPMessage request = { 0 };
7492 GstRTSPMessage response = { 0 };
7494 const gchar *control;
7496 GST_DEBUG_OBJECT (src, "PAUSE...");
7498 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7501 if (!(src->methods & GST_RTSP_PAUSE))
7504 if (src->state == GST_RTSP_STATE_READY)
7507 if (!src->conninfo.connection || !src->conninfo.connected)
7510 /* construct a control url */
7511 control = get_aggregate_control (src);
7513 /* loop over the streams. We might exit the loop early when we could do an
7514 * aggregate control */
7515 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7516 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7517 GstRTSPConnInfo *conninfo;
7518 const gchar *setup_url;
7520 /* try aggregate control first but do non-aggregate control otherwise */
7522 setup_url = control;
7523 else if ((setup_url = stream->conninfo.location) == NULL)
7526 if (src->conninfo.connection) {
7527 conninfo = &src->conninfo;
7528 } else if (stream->conninfo.connection) {
7529 conninfo = &stream->conninfo;
7535 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7536 ("Sending PAUSE request"));
7539 gst_rtspsrc_init_request (src, &request, GST_RTSP_PAUSE,
7541 goto create_request_failed;
7543 if ((res = gst_rtspsrc_send (src, conninfo, &request, &response, NULL)) < 0)
7546 gst_rtsp_message_unset (&request);
7547 gst_rtsp_message_unset (&response);
7549 /* exit early when we did agregate control */
7554 /* change element states now */
7555 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7558 src->state = GST_RTSP_STATE_READY;
7562 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7569 GST_DEBUG_OBJECT (src, "failed to open stream");
7574 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7579 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7582 create_request_failed:
7584 gchar *str = gst_rtsp_strresult (res);
7586 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7587 ("Could not create request. (%s)", str));
7593 gchar *str = gst_rtsp_strresult (res);
7595 gst_rtsp_message_unset (&request);
7596 if (res != GST_RTSP_EINTR) {
7597 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7598 ("Could not send message. (%s)", str));
7600 GST_WARNING_OBJECT (src, "PAUSE interrupted");
7608 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
7610 GstRTSPSrc *rtspsrc;
7612 rtspsrc = GST_RTSPSRC (bin);
7614 switch (GST_MESSAGE_TYPE (message)) {
7615 case GST_MESSAGE_EOS:
7616 gst_message_unref (message);
7618 case GST_MESSAGE_ELEMENT:
7620 const GstStructure *s = gst_message_get_structure (message);
7622 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
7623 gboolean ignore_timeout;
7625 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
7627 GST_OBJECT_LOCK (rtspsrc);
7628 ignore_timeout = rtspsrc->ignore_timeout;
7629 rtspsrc->ignore_timeout = TRUE;
7630 GST_OBJECT_UNLOCK (rtspsrc);
7632 /* we only act on the first udp timeout message, others are irrelevant
7633 * and can be ignored. */
7634 if (!ignore_timeout)
7635 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
7637 gst_message_unref (message);
7640 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7643 case GST_MESSAGE_ERROR:
7646 GstRTSPStream *stream;
7649 udpsrc = GST_MESSAGE_SRC (message);
7651 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7652 GST_ELEMENT_NAME (udpsrc));
7654 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7658 /* we ignore the RTCP udpsrc */
7659 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7662 /* if we get error messages from the udp sources, that's not a problem as
7663 * long as not all of them error out. We also don't really know what the
7664 * problem is, the message does not give enough detail... */
7665 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7666 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7667 if (ret != GST_FLOW_OK)
7671 gst_message_unref (message);
7675 /* fatal but not our message, forward */
7676 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7681 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7687 /* the thread where everything happens */
7689 gst_rtspsrc_thread (GstRTSPSrc * src)
7693 GST_OBJECT_LOCK (src);
7694 cmd = src->pending_cmd;
7695 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7696 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7697 src->pending_cmd = CMD_LOOP;
7699 src->pending_cmd = CMD_WAIT;
7700 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
7702 /* we got the message command, so ensure communication is possible again */
7703 gst_rtspsrc_connection_flush (src, FALSE);
7705 src->busy_cmd = cmd;
7706 GST_OBJECT_UNLOCK (src);
7710 gst_rtspsrc_open (src, TRUE);
7713 gst_rtspsrc_play (src, &src->segment, TRUE);
7716 gst_rtspsrc_pause (src, TRUE);
7719 gst_rtspsrc_close (src, TRUE, FALSE);
7722 gst_rtspsrc_loop (src);
7725 gst_rtspsrc_reconnect (src, FALSE);
7731 GST_OBJECT_LOCK (src);
7732 /* and go back to sleep */
7733 if (src->pending_cmd == CMD_WAIT) {
7735 gst_task_pause (src->task);
7738 src->busy_cmd = CMD_WAIT;
7739 GST_OBJECT_UNLOCK (src);
7743 gst_rtspsrc_start (GstRTSPSrc * src)
7745 GST_DEBUG_OBJECT (src, "starting");
7747 GST_OBJECT_LOCK (src);
7749 src->pending_cmd = CMD_WAIT;
7751 if (src->task == NULL) {
7752 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7753 if (src->task == NULL)
7756 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7758 GST_OBJECT_UNLOCK (src);
7765 GST_OBJECT_UNLOCK (src);
7766 GST_ERROR_OBJECT (src, "failed to create task");
7772 gst_rtspsrc_stop (GstRTSPSrc * src)
7776 GST_DEBUG_OBJECT (src, "stopping");
7778 /* also cancels pending task */
7779 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7781 GST_OBJECT_LOCK (src);
7782 if ((task = src->task)) {
7784 GST_OBJECT_UNLOCK (src);
7786 gst_task_stop (task);
7788 /* make sure it is not running */
7789 GST_RTSP_STREAM_LOCK (src);
7790 GST_RTSP_STREAM_UNLOCK (src);
7792 /* now wait for the task to finish */
7793 gst_task_join (task);
7795 /* and free the task */
7796 gst_object_unref (GST_OBJECT (task));
7798 GST_OBJECT_LOCK (src);
7800 GST_OBJECT_UNLOCK (src);
7802 /* ensure synchronously all is closed and clean */
7803 gst_rtspsrc_close (src, FALSE, TRUE);
7808 static GstStateChangeReturn
7809 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
7811 GstRTSPSrc *rtspsrc;
7812 GstStateChangeReturn ret;
7814 rtspsrc = GST_RTSPSRC (element);
7816 switch (transition) {
7817 case GST_STATE_CHANGE_NULL_TO_READY:
7818 if (!gst_rtspsrc_start (rtspsrc))
7821 case GST_STATE_CHANGE_READY_TO_PAUSED:
7822 /* init some state */
7823 rtspsrc->cur_protocols = rtspsrc->protocols;
7824 /* first attempt, don't ignore timeouts */
7825 rtspsrc->ignore_timeout = FALSE;
7826 rtspsrc->open_error = FALSE;
7827 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
7829 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7830 set_manager_buffer_mode (rtspsrc);
7832 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7833 /* unblock the tcp tasks and make the loop waiting */
7834 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
7835 /* make sure it is waiting before we send PAUSE or PLAY below */
7836 GST_RTSP_STREAM_LOCK (rtspsrc);
7837 GST_RTSP_STREAM_UNLOCK (rtspsrc);
7840 case GST_STATE_CHANGE_PAUSED_TO_READY:
7846 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
7847 if (ret == GST_STATE_CHANGE_FAILURE)
7850 switch (transition) {
7851 case GST_STATE_CHANGE_NULL_TO_READY:
7852 ret = GST_STATE_CHANGE_SUCCESS;
7854 case GST_STATE_CHANGE_READY_TO_PAUSED:
7855 ret = GST_STATE_CHANGE_NO_PREROLL;
7857 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7858 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
7859 ret = GST_STATE_CHANGE_SUCCESS;
7861 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7862 /* send pause request and keep the idle task around */
7863 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
7864 ret = GST_STATE_CHANGE_NO_PREROLL;
7866 case GST_STATE_CHANGE_PAUSED_TO_READY:
7867 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_ALL);
7868 ret = GST_STATE_CHANGE_SUCCESS;
7870 case GST_STATE_CHANGE_READY_TO_NULL:
7871 gst_rtspsrc_stop (rtspsrc);
7872 ret = GST_STATE_CHANGE_SUCCESS;
7875 /* Otherwise it's success, we don't want to return spurious
7876 * NO_PREROLL or ASYNC from internal elements as we care for
7877 * state changes ourselves here
7879 * This is to catch PAUSED->PAUSED and PLAYING->PLAYING transitions.
7881 if (GST_STATE_TRANSITION_NEXT (transition) == GST_STATE_PAUSED)
7882 ret = GST_STATE_CHANGE_NO_PREROLL;
7884 ret = GST_STATE_CHANGE_SUCCESS;
7893 GST_DEBUG_OBJECT (rtspsrc, "start failed");
7894 return GST_STATE_CHANGE_FAILURE;
7899 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
7902 GstRTSPSrc *rtspsrc;
7904 rtspsrc = GST_RTSPSRC (element);
7906 if (GST_EVENT_IS_DOWNSTREAM (event)) {
7907 res = gst_rtspsrc_push_event (rtspsrc, event);
7909 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
7916 /*** GSTURIHANDLER INTERFACE *************************************************/
7919 gst_rtspsrc_uri_get_type (GType type)
7924 static const gchar *const *
7925 gst_rtspsrc_uri_get_protocols (GType type)
7927 static const gchar *protocols[] =
7928 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
7929 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
7936 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
7938 GstRTSPSrc *src = GST_RTSPSRC (handler);
7940 /* FIXME: make thread-safe */
7941 return g_strdup (src->conninfo.location);
7945 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
7951 GstRTSPUrl *newurl = NULL;
7952 GstSDPMessage *sdp = NULL;
7954 src = GST_RTSPSRC (handler);
7956 /* same URI, we're fine */
7957 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
7960 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
7961 sres = gst_sdp_message_new (&sdp);
7965 GST_DEBUG_OBJECT (src, "parsing SDP message");
7966 sres = gst_sdp_message_parse_uri (uri, sdp);
7971 GST_DEBUG_OBJECT (src, "parsing URI");
7972 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
7976 /* if worked, free previous and store new url object along with the original
7978 GST_DEBUG_OBJECT (src, "configuring URI");
7979 g_free (src->conninfo.location);
7980 src->conninfo.location = g_strdup (uri);
7981 gst_rtsp_url_free (src->conninfo.url);
7982 src->conninfo.url = newurl;
7983 g_free (src->conninfo.url_str);
7985 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
7987 src->conninfo.url_str = NULL;
7990 gst_sdp_message_free (src->sdp);
7992 src->from_sdp = sdp != NULL;
7994 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
7995 GST_DEBUG_OBJECT (src, "request uri is: %s",
7996 GST_STR_NULL (src->conninfo.url_str));
8003 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
8008 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
8009 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8010 "Could not create SDP");
8015 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
8016 GST_STR_NULL (uri));
8017 gst_sdp_message_free (sdp);
8018 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8024 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
8025 GST_STR_NULL (uri), res);
8026 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8027 "Invalid RTSP URI");
8033 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
8035 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
8037 iface->get_type = gst_rtspsrc_uri_get_type;
8038 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
8039 iface->get_uri = gst_rtspsrc_uri_get_uri;
8040 iface->set_uri = gst_rtspsrc_uri_set_uri;