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 AES_128_KEY_LEN 16
199 #define AES_256_KEY_LEN 32
201 #define HMAC_32_KEY_LEN 4
202 #define HMAC_80_KEY_LEN 10
204 #define DEFAULT_LOCATION NULL
205 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
206 #define DEFAULT_DEBUG FALSE
207 #define DEFAULT_RETRY 20
208 #define DEFAULT_TIMEOUT 5000000
209 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
210 #define DEFAULT_TCP_TIMEOUT 20000000
211 #define DEFAULT_LATENCY_MS 2000
212 #define DEFAULT_DROP_ON_LATENCY FALSE
213 #define DEFAULT_CONNECTION_SPEED 0
214 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
215 #define DEFAULT_DO_RTCP TRUE
216 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
217 #define DEFAULT_PROXY NULL
218 #define DEFAULT_RTP_BLOCKSIZE 0
219 #define DEFAULT_USER_ID NULL
220 #define DEFAULT_USER_PW NULL
221 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
222 #define DEFAULT_PORT_RANGE NULL
223 #define DEFAULT_SHORT_HEADER FALSE
224 #define DEFAULT_PROBATION 2
225 #define DEFAULT_UDP_RECONNECT TRUE
226 #define DEFAULT_MULTICAST_IFACE NULL
227 #define DEFAULT_NTP_SYNC FALSE
228 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
229 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
230 #define DEFAULT_TLS_DATABASE NULL
231 #define DEFAULT_TLS_INTERACTION NULL
232 #define DEFAULT_DO_RETRANSMISSION TRUE
233 #define DEFAULT_NTP_TIME_SOURCE NTP_TIME_SOURCE_NTP
234 #define DEFAULT_USER_AGENT "GStreamer/" PACKAGE_VERSION
235 #define DEFAULT_MAX_RTCP_RTP_TIME_DIFF 1000
247 PROP_DROP_ON_LATENCY,
248 PROP_CONNECTION_SPEED,
251 PROP_DO_RTSP_KEEP_ALIVE,
260 PROP_UDP_BUFFER_SIZE,
264 PROP_MULTICAST_IFACE,
266 PROP_USE_PIPELINE_CLOCK,
268 PROP_TLS_VALIDATION_FLAGS,
270 PROP_TLS_INTERACTION,
271 PROP_DO_RETRANSMISSION,
272 PROP_NTP_TIME_SOURCE,
274 PROP_MAX_RTCP_RTP_TIME_DIFF
277 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
279 gst_rtsp_nat_method_get_type (void)
281 static GType rtsp_nat_method_type = 0;
282 static const GEnumValue rtsp_nat_method[] = {
283 {GST_RTSP_NAT_NONE, "None", "none"},
284 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
288 if (!rtsp_nat_method_type) {
289 rtsp_nat_method_type =
290 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
292 return rtsp_nat_method_type;
295 static void gst_rtspsrc_finalize (GObject * object);
297 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
298 const GValue * value, GParamSpec * pspec);
299 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
300 GValue * value, GParamSpec * pspec);
302 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
304 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
305 gpointer iface_data);
307 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
310 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
311 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
313 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
315 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
316 GstStateChange transition);
317 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
318 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
320 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
321 GstRTSPMessage * response);
323 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
325 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
326 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
328 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
329 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
331 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
332 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
333 gboolean only_close);
335 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
336 const gchar * uri, GError ** error);
337 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
339 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
340 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
341 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
342 GstRTSPStream * stream, GstEvent * event);
343 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
344 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
352 /* commands we send to out loop to notify it of events */
353 #define CMD_OPEN (1 << 0)
354 #define CMD_PLAY (1 << 1)
355 #define CMD_PAUSE (1 << 2)
356 #define CMD_CLOSE (1 << 3)
357 #define CMD_WAIT (1 << 4)
358 #define CMD_RECONNECT (1 << 5)
359 #define CMD_LOOP (1 << 6)
361 /* mask for all commands */
362 #define CMD_ALL ((CMD_LOOP << 1) - 1)
364 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
366 gchar *__txt = _gst_element_error_printf text; \
367 gst_element_post_message (GST_ELEMENT_CAST (el), \
368 gst_message_new_progress (GST_OBJECT_CAST (el), \
369 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
373 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
375 #define gst_rtspsrc_parent_class parent_class
376 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
377 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
379 #ifndef GST_DISABLE_GST_DEBUG
380 static inline const char *
381 cmd_to_string (guint cmd)
405 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
407 GST_DEBUG_OBJECT (src, "default handler");
412 select_stream_accum (GSignalInvocationHint * ihint,
413 GValue * return_accu, const GValue * handler_return, gpointer data)
417 myboolean = g_value_get_boolean (handler_return);
418 GST_DEBUG ("accum %d", myboolean);
419 g_value_set_boolean (return_accu, myboolean);
421 /* stop emission if FALSE */
426 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
428 GObjectClass *gobject_class;
429 GstElementClass *gstelement_class;
430 GstBinClass *gstbin_class;
432 gobject_class = (GObjectClass *) klass;
433 gstelement_class = (GstElementClass *) klass;
434 gstbin_class = (GstBinClass *) klass;
436 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
438 gobject_class->set_property = gst_rtspsrc_set_property;
439 gobject_class->get_property = gst_rtspsrc_get_property;
441 gobject_class->finalize = gst_rtspsrc_finalize;
443 g_object_class_install_property (gobject_class, PROP_LOCATION,
444 g_param_spec_string ("location", "RTSP Location",
445 "Location of the RTSP url to read",
446 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
448 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
449 g_param_spec_flags ("protocols", "Protocols",
450 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
451 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
453 g_object_class_install_property (gobject_class, PROP_DEBUG,
454 g_param_spec_boolean ("debug", "Debug",
455 "Dump request and response messages to stdout",
456 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
458 g_object_class_install_property (gobject_class, PROP_RETRY,
459 g_param_spec_uint ("retry", "Retry",
460 "Max number of retries when allocating RTP ports.",
461 0, G_MAXUINT16, DEFAULT_RETRY,
462 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
464 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
465 g_param_spec_uint64 ("timeout", "Timeout",
466 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
467 0, G_MAXUINT64, DEFAULT_TIMEOUT,
468 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
470 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
471 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
472 "Fail after timeout microseconds on TCP connections (0 = disabled)",
473 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
474 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
476 g_object_class_install_property (gobject_class, PROP_LATENCY,
477 g_param_spec_uint ("latency", "Buffer latency in ms",
478 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
479 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
481 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
482 g_param_spec_boolean ("drop-on-latency",
483 "Drop buffers when maximum latency is reached",
484 "Tells the jitterbuffer to never exceed the given latency in size",
485 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
487 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
488 g_param_spec_uint64 ("connection-speed", "Connection Speed",
489 "Network connection speed in kbps (0 = unknown)",
490 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
491 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
493 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
494 g_param_spec_enum ("nat-method", "NAT Method",
495 "Method to use for traversing firewalls and NAT",
496 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
497 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
500 * GstRTSPSrc:do-rtcp:
502 * Enable RTCP support. Some old server don't like RTCP and then this property
503 * needs to be set to FALSE.
505 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
506 g_param_spec_boolean ("do-rtcp", "Do RTCP",
507 "Send RTCP packets, disable for old incompatible server.",
508 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
511 * GstRTSPSrc:do-rtsp-keep-alive:
513 * Enable RTSP keep alive support. Some old server don't like RTSP
514 * keep alive and then this property needs to be set to FALSE.
516 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
517 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
518 "Send RTSP keep alive packets, disable for old incompatible server.",
519 DEFAULT_DO_RTSP_KEEP_ALIVE,
520 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
525 * Set the proxy parameters. This has to be a string of the format
526 * [http://][user:passwd@]host[:port].
528 g_object_class_install_property (gobject_class, PROP_PROXY,
529 g_param_spec_string ("proxy", "Proxy",
530 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
531 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
533 * GstRTSPSrc:proxy-id:
535 * Sets the proxy URI user id for authentication. If the URI set via the
536 * "proxy" property contains a user-id already, that will take precedence.
540 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
541 g_param_spec_string ("proxy-id", "proxy-id",
542 "HTTP proxy URI user id for authentication", "",
543 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
545 * GstRTSPSrc:proxy-pw:
547 * Sets the proxy URI password for authentication. If the URI set via the
548 * "proxy" property contains a password already, that will take precedence.
552 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
553 g_param_spec_string ("proxy-pw", "proxy-pw",
554 "HTTP proxy URI user password for authentication", "",
555 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
558 * GstRTSPSrc:rtp-blocksize:
560 * RTP package size to suggest to server.
562 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
563 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
564 "RTP package size to suggest to server (0 = disabled)",
565 0, 65536, DEFAULT_RTP_BLOCKSIZE,
566 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
568 g_object_class_install_property (gobject_class,
570 g_param_spec_string ("user-id", "user-id",
571 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
572 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
573 g_object_class_install_property (gobject_class, PROP_USER_PW,
574 g_param_spec_string ("user-pw", "user-pw",
575 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
576 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
579 * GstRTSPSrc:buffer-mode:
581 * Control the buffering and timestamping mode used by the jitterbuffer.
583 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
584 g_param_spec_enum ("buffer-mode", "Buffer Mode",
585 "Control the buffering algorithm in use",
586 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
587 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
590 * GstRTSPSrc:port-range:
592 * Configure the client port numbers that can be used to recieve RTP and
595 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
596 g_param_spec_string ("port-range", "Port range",
597 "Client port range that can be used to receive RTP and RTCP data, "
598 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
599 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
602 * GstRTSPSrc:udp-buffer-size:
604 * Size of the kernel UDP receive buffer in bytes.
606 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
607 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
608 "Size of the kernel UDP receive buffer in bytes, 0=default",
609 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
610 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
613 * GstRTSPSrc:short-header:
615 * Only send the basic RTSP headers for broken encoders.
617 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
618 g_param_spec_boolean ("short-header", "Short Header",
619 "Only send the basic RTSP headers for broken encoders",
620 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
622 g_object_class_install_property (gobject_class, PROP_PROBATION,
623 g_param_spec_uint ("probation", "Number of probations",
624 "Consecutive packet sequence numbers to accept the source",
625 0, G_MAXUINT, DEFAULT_PROBATION,
626 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
628 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
629 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
630 "Reconnect to the server if RTSP connection is closed when doing UDP",
631 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
633 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
634 g_param_spec_string ("multicast-iface", "Multicast Interface",
635 "The network interface on which to join the multicast group",
636 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
638 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
639 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
640 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
641 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
643 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
644 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
645 "Use the pipeline running-time to set the NTP time in the RTCP SR messages"
646 "(DEPRECATED: Use ntp-time-source property)",
647 DEFAULT_USE_PIPELINE_CLOCK,
648 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
650 g_object_class_install_property (gobject_class, PROP_SDES,
651 g_param_spec_boxed ("sdes", "SDES",
652 "The SDES items of this session",
653 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
656 * GstRTSPSrc::tls-validation-flags:
658 * TLS certificate validation flags used to validate server
663 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
664 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
665 "TLS certificate validation flags used to validate the server certificate",
666 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
667 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
670 * GstRTSPSrc::tls-database:
672 * TLS database with anchor certificate authorities used to validate
673 * the server certificate.
677 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
678 g_param_spec_object ("tls-database", "TLS database",
679 "TLS database with anchor certificate authorities used to validate the server certificate",
680 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
683 * GstRTSPSrc::tls-interaction:
685 * A #GTlsInteraction object to be used when the connection or certificate
686 * database need to interact with the user. This will be used to prompt the
687 * user for passwords where necessary.
691 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
692 g_param_spec_object ("tls-interaction", "TLS interaction",
693 "A GTlsInteraction object to promt the user for password or certificate",
694 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
697 * GstRTSPSrc::do-retransmission:
699 * Attempt to ask the server to retransmit lost packets according to RFC4588.
701 * Note: currently only works with SSRC-multiplexed retransmission streams
705 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
706 g_param_spec_boolean ("do-retransmission", "Retransmission",
707 "Ask the server to retransmit lost packets",
708 DEFAULT_DO_RETRANSMISSION,
709 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
712 * GstRTSPSrc::ntp-time-source:
714 * allows to select the time source that should be used
715 * for the NTP time in RTCP packets
719 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
720 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
721 "NTP time source for RTCP packets",
722 GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE, DEFAULT_NTP_TIME_SOURCE,
723 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
726 * GstRTSPSrc::user-agent:
728 * The string to set in the User-Agent header.
732 g_object_class_install_property (gobject_class, PROP_USER_AGENT,
733 g_param_spec_string ("user-agent", "User Agent",
734 "The User-Agent string to send to the server",
735 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
737 g_object_class_install_property (gobject_class, PROP_MAX_RTCP_RTP_TIME_DIFF,
738 g_param_spec_int ("max-rtcp-rtp-time-diff", "Max RTCP RTP Time Diff",
739 "Maximum amount of time in ms that the RTP time in RTCP SRs "
740 "is allowed to be ahead (-1 disabled)", -1, G_MAXINT,
741 DEFAULT_MAX_RTCP_RTP_TIME_DIFF,
742 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
745 * GstRTSPSrc::handle-request:
746 * @rtspsrc: a #GstRTSPSrc
747 * @request: a #GstRTSPMessage
748 * @response: a #GstRTSPMessage
750 * Handle a server request in @request and prepare @response.
752 * This signal is called from the streaming thread, you should therefore not
753 * do any state changes on @rtspsrc because this might deadlock. If you want
754 * to modify the state as a result of this signal, post a
755 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
760 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
761 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
762 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
763 G_TYPE_POINTER, G_TYPE_POINTER);
766 * GstRTSPSrc::on-sdp:
767 * @rtspsrc: a #GstRTSPSrc
768 * @sdp: a #GstSDPMessage
770 * Emited when the client has retrieved the SDP and before it configures the
771 * streams in the SDP. @sdp can be inspected and modified.
773 * This signal is called from the streaming thread, you should therefore not
774 * do any state changes on @rtspsrc because this might deadlock. If you want
775 * to modify the state as a result of this signal, post a
776 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
781 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
782 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
783 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
784 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
787 * GstRTSPSrc::select-stream:
788 * @rtspsrc: a #GstRTSPSrc
789 * @num: the stream number
790 * @caps: the stream caps
792 * Emited before the client decides to configure the stream @num with
795 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
800 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
801 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
802 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
803 (GCallback) default_select_stream, select_stream_accum, NULL,
804 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
807 * GstRTSPSrc::new-manager:
808 * @rtspsrc: a #GstRTSPSrc
809 * @manager: a #GstElement
811 * Emited after a new manager (like rtpbin) was created and the default
812 * properties were configured.
816 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
817 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
818 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
819 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
822 * GstRTSPSrc::request-rtcp-key:
823 * @rtspsrc: a #GstRTSPSrc
824 * @num: the stream number
826 * Signal emited to get the crypto parameters relevant to the RTCP
827 * stream. User should provide the key and the RTCP encryption ciphers
828 * and authentication, and return them wrapped in a GstCaps.
832 gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY] =
833 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
834 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
836 gstelement_class->send_event = gst_rtspsrc_send_event;
837 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
838 gstelement_class->change_state = gst_rtspsrc_change_state;
840 gst_element_class_add_pad_template (gstelement_class,
841 gst_static_pad_template_get (&rtptemplate));
843 gst_element_class_set_static_metadata (gstelement_class,
844 "RTSP packet receiver", "Source/Network",
845 "Receive data over the network via RTSP (RFC 2326)",
846 "Wim Taymans <wim@fluendo.com>, "
847 "Thijs Vermeir <thijs.vermeir@barco.com>, "
848 "Lutz Mueller <lutz@topfrose.de>");
850 gstbin_class->handle_message = gst_rtspsrc_handle_message;
852 gst_rtsp_ext_list_init ();
856 gst_rtspsrc_init (GstRTSPSrc * src)
858 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
859 src->protocols = DEFAULT_PROTOCOLS;
860 src->debug = DEFAULT_DEBUG;
861 src->retry = DEFAULT_RETRY;
862 src->udp_timeout = DEFAULT_TIMEOUT;
863 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
864 src->latency = DEFAULT_LATENCY_MS;
865 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
866 src->connection_speed = DEFAULT_CONNECTION_SPEED;
867 src->nat_method = DEFAULT_NAT_METHOD;
868 src->do_rtcp = DEFAULT_DO_RTCP;
869 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
870 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
871 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
872 src->user_id = g_strdup (DEFAULT_USER_ID);
873 src->user_pw = g_strdup (DEFAULT_USER_PW);
874 src->buffer_mode = DEFAULT_BUFFER_MODE;
875 src->client_port_range.min = 0;
876 src->client_port_range.max = 0;
877 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
878 src->short_header = DEFAULT_SHORT_HEADER;
879 src->probation = DEFAULT_PROBATION;
880 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
881 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
882 src->ntp_sync = DEFAULT_NTP_SYNC;
883 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
885 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
886 src->tls_database = DEFAULT_TLS_DATABASE;
887 src->tls_interaction = DEFAULT_TLS_INTERACTION;
888 src->do_retransmission = DEFAULT_DO_RETRANSMISSION;
889 src->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
890 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
891 src->max_rtcp_rtp_time_diff = DEFAULT_MAX_RTCP_RTP_TIME_DIFF;
893 /* get a list of all extensions */
894 src->extensions = gst_rtsp_ext_list_get ();
896 /* connect to send signal */
897 gst_rtsp_ext_list_connect (src->extensions, "send",
898 (GCallback) gst_rtspsrc_send_cb, src);
900 /* protects the streaming thread in interleaved mode or the polling
901 * thread in UDP mode. */
902 g_rec_mutex_init (&src->stream_rec_lock);
904 /* protects our state changes from multiple invocations */
905 g_rec_mutex_init (&src->state_rec_lock);
907 src->state = GST_RTSP_STATE_INVALID;
909 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
913 gst_rtspsrc_finalize (GObject * object)
917 rtspsrc = GST_RTSPSRC (object);
919 gst_rtsp_ext_list_free (rtspsrc->extensions);
920 g_free (rtspsrc->conninfo.location);
921 gst_rtsp_url_free (rtspsrc->conninfo.url);
922 g_free (rtspsrc->conninfo.url_str);
923 g_free (rtspsrc->user_id);
924 g_free (rtspsrc->user_pw);
925 g_free (rtspsrc->multi_iface);
926 g_free (rtspsrc->user_agent);
929 gst_sdp_message_free (rtspsrc->sdp);
932 if (rtspsrc->provided_clock)
933 gst_object_unref (rtspsrc->provided_clock);
936 gst_structure_free (rtspsrc->sdes);
938 if (rtspsrc->tls_database)
939 g_object_unref (rtspsrc->tls_database);
941 if (rtspsrc->tls_interaction)
942 g_object_unref (rtspsrc->tls_interaction);
945 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
946 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
948 G_OBJECT_CLASS (parent_class)->finalize (object);
952 gst_rtspsrc_provide_clock (GstElement * element)
954 GstRTSPSrc *src = GST_RTSPSRC (element);
957 if ((clock = src->provided_clock) != NULL)
958 gst_object_ref (clock);
963 /* a proxy string of the format [user:passwd@]host[:port] */
965 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
969 g_free (rtsp->proxy_user);
970 rtsp->proxy_user = NULL;
971 g_free (rtsp->proxy_passwd);
972 rtsp->proxy_passwd = NULL;
973 g_free (rtsp->proxy_host);
974 rtsp->proxy_host = NULL;
975 rtsp->proxy_port = 0;
982 /* we allow http:// in front but ignore it */
983 if (g_str_has_prefix (p, "http://"))
986 at = strchr (p, '@');
988 /* look for user:passwd */
989 col = strchr (proxy, ':');
990 if (col == NULL || col > at)
993 rtsp->proxy_user = g_strndup (p, col - p);
995 rtsp->proxy_passwd = g_strndup (col, at - col);
1000 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
1001 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
1002 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
1003 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
1004 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
1005 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
1006 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
1009 col = strchr (p, ':');
1012 /* everything before the colon is the hostname */
1013 rtsp->proxy_host = g_strndup (p, col - p);
1015 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
1017 rtsp->proxy_host = g_strdup (p);
1018 rtsp->proxy_port = 8080;
1024 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
1026 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
1027 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
1030 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
1032 rtspsrc->ptcp_timeout = NULL;
1036 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
1039 GstRTSPSrc *rtspsrc;
1041 rtspsrc = GST_RTSPSRC (object);
1045 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
1046 g_value_get_string (value), NULL);
1048 case PROP_PROTOCOLS:
1049 rtspsrc->protocols = g_value_get_flags (value);
1052 rtspsrc->debug = g_value_get_boolean (value);
1055 rtspsrc->retry = g_value_get_uint (value);
1058 rtspsrc->udp_timeout = g_value_get_uint64 (value);
1060 case PROP_TCP_TIMEOUT:
1061 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
1064 rtspsrc->latency = g_value_get_uint (value);
1066 case PROP_DROP_ON_LATENCY:
1067 rtspsrc->drop_on_latency = g_value_get_boolean (value);
1069 case PROP_CONNECTION_SPEED:
1070 rtspsrc->connection_speed = g_value_get_uint64 (value);
1072 case PROP_NAT_METHOD:
1073 rtspsrc->nat_method = g_value_get_enum (value);
1076 rtspsrc->do_rtcp = g_value_get_boolean (value);
1078 case PROP_DO_RTSP_KEEP_ALIVE:
1079 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
1082 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
1085 g_free (rtspsrc->prop_proxy_id);
1086 rtspsrc->prop_proxy_id = g_value_dup_string (value);
1089 g_free (rtspsrc->prop_proxy_pw);
1090 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
1092 case PROP_RTP_BLOCKSIZE:
1093 rtspsrc->rtp_blocksize = g_value_get_uint (value);
1096 g_free (rtspsrc->user_id);
1097 rtspsrc->user_id = g_value_dup_string (value);
1100 g_free (rtspsrc->user_pw);
1101 rtspsrc->user_pw = g_value_dup_string (value);
1103 case PROP_BUFFER_MODE:
1104 rtspsrc->buffer_mode = g_value_get_enum (value);
1106 case PROP_PORT_RANGE:
1110 str = g_value_get_string (value);
1112 sscanf (str, "%u-%u",
1113 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
1115 rtspsrc->client_port_range.min = 0;
1116 rtspsrc->client_port_range.max = 0;
1120 case PROP_UDP_BUFFER_SIZE:
1121 rtspsrc->udp_buffer_size = g_value_get_int (value);
1123 case PROP_SHORT_HEADER:
1124 rtspsrc->short_header = g_value_get_boolean (value);
1126 case PROP_PROBATION:
1127 rtspsrc->probation = g_value_get_uint (value);
1129 case PROP_UDP_RECONNECT:
1130 rtspsrc->udp_reconnect = g_value_get_boolean (value);
1132 case PROP_MULTICAST_IFACE:
1133 g_free (rtspsrc->multi_iface);
1135 if (g_value_get_string (value) == NULL)
1136 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1138 rtspsrc->multi_iface = g_value_dup_string (value);
1141 rtspsrc->ntp_sync = g_value_get_boolean (value);
1143 case PROP_USE_PIPELINE_CLOCK:
1144 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
1147 rtspsrc->sdes = g_value_dup_boxed (value);
1149 case PROP_TLS_VALIDATION_FLAGS:
1150 rtspsrc->tls_validation_flags = g_value_get_flags (value);
1152 case PROP_TLS_DATABASE:
1153 g_clear_object (&rtspsrc->tls_database);
1154 rtspsrc->tls_database = g_value_dup_object (value);
1156 case PROP_TLS_INTERACTION:
1157 g_clear_object (&rtspsrc->tls_interaction);
1158 rtspsrc->tls_interaction = g_value_dup_object (value);
1160 case PROP_DO_RETRANSMISSION:
1161 rtspsrc->do_retransmission = g_value_get_boolean (value);
1163 case PROP_NTP_TIME_SOURCE:
1164 rtspsrc->ntp_time_source = g_value_get_enum (value);
1166 case PROP_USER_AGENT:
1167 g_free (rtspsrc->user_agent);
1168 rtspsrc->user_agent = g_value_dup_string (value);
1170 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1171 rtspsrc->max_rtcp_rtp_time_diff = g_value_get_int (value);
1174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1180 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1183 GstRTSPSrc *rtspsrc;
1185 rtspsrc = GST_RTSPSRC (object);
1189 g_value_set_string (value, rtspsrc->conninfo.location);
1191 case PROP_PROTOCOLS:
1192 g_value_set_flags (value, rtspsrc->protocols);
1195 g_value_set_boolean (value, rtspsrc->debug);
1198 g_value_set_uint (value, rtspsrc->retry);
1201 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1203 case PROP_TCP_TIMEOUT:
1207 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1208 rtspsrc->tcp_timeout.tv_usec;
1209 g_value_set_uint64 (value, timeout);
1213 g_value_set_uint (value, rtspsrc->latency);
1215 case PROP_DROP_ON_LATENCY:
1216 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1218 case PROP_CONNECTION_SPEED:
1219 g_value_set_uint64 (value, rtspsrc->connection_speed);
1221 case PROP_NAT_METHOD:
1222 g_value_set_enum (value, rtspsrc->nat_method);
1225 g_value_set_boolean (value, rtspsrc->do_rtcp);
1227 case PROP_DO_RTSP_KEEP_ALIVE:
1228 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1234 if (rtspsrc->proxy_host) {
1236 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1240 g_value_take_string (value, str);
1244 g_value_set_string (value, rtspsrc->prop_proxy_id);
1247 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1249 case PROP_RTP_BLOCKSIZE:
1250 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1253 g_value_set_string (value, rtspsrc->user_id);
1256 g_value_set_string (value, rtspsrc->user_pw);
1258 case PROP_BUFFER_MODE:
1259 g_value_set_enum (value, rtspsrc->buffer_mode);
1261 case PROP_PORT_RANGE:
1265 if (rtspsrc->client_port_range.min != 0) {
1266 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1267 rtspsrc->client_port_range.max);
1271 g_value_take_string (value, str);
1274 case PROP_UDP_BUFFER_SIZE:
1275 g_value_set_int (value, rtspsrc->udp_buffer_size);
1277 case PROP_SHORT_HEADER:
1278 g_value_set_boolean (value, rtspsrc->short_header);
1280 case PROP_PROBATION:
1281 g_value_set_uint (value, rtspsrc->probation);
1283 case PROP_UDP_RECONNECT:
1284 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1286 case PROP_MULTICAST_IFACE:
1287 g_value_set_string (value, rtspsrc->multi_iface);
1290 g_value_set_boolean (value, rtspsrc->ntp_sync);
1292 case PROP_USE_PIPELINE_CLOCK:
1293 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1296 g_value_set_boxed (value, rtspsrc->sdes);
1298 case PROP_TLS_VALIDATION_FLAGS:
1299 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1301 case PROP_TLS_DATABASE:
1302 g_value_set_object (value, rtspsrc->tls_database);
1304 case PROP_TLS_INTERACTION:
1305 g_value_set_object (value, rtspsrc->tls_interaction);
1307 case PROP_DO_RETRANSMISSION:
1308 g_value_set_boolean (value, rtspsrc->do_retransmission);
1310 case PROP_NTP_TIME_SOURCE:
1311 g_value_set_enum (value, rtspsrc->ntp_time_source);
1313 case PROP_USER_AGENT:
1314 g_value_set_string (value, rtspsrc->user_agent);
1316 case PROP_MAX_RTCP_RTP_TIME_DIFF:
1317 g_value_set_int (value, rtspsrc->max_rtcp_rtp_time_diff);
1320 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1326 find_stream_by_id (GstRTSPStream * stream, gint * id)
1328 if (stream->id == *id)
1335 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1337 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1344 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1346 GstElement *src = (GstElement *) a;
1348 if (stream->udpsrc[0] == src)
1350 if (stream->udpsrc[1] == src)
1357 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1359 if (stream->conninfo.location) {
1360 /* check qualified setup_url */
1361 if (!strcmp (stream->conninfo.location, (gchar *) a))
1364 if (stream->control_url) {
1365 /* check original control_url */
1366 if (!strcmp (stream->control_url, (gchar *) a))
1369 /* check if qualified setup_url ends with string */
1370 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1377 static GstRTSPStream *
1378 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1382 /* find and get stream */
1383 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1384 return (GstRTSPStream *) lstream->data;
1389 static const GstSDPBandwidth *
1390 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1391 const GstSDPMedia * media, const gchar * type)
1395 /* first look in the media specific section */
1396 len = gst_sdp_media_bandwidths_len (media);
1397 for (i = 0; i < len; i++) {
1398 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1400 if (strcmp (bw->bwtype, type) == 0)
1403 /* then look in the message specific section */
1404 len = gst_sdp_message_bandwidths_len (sdp);
1405 for (i = 0; i < len; i++) {
1406 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1408 if (strcmp (bw->bwtype, type) == 0)
1415 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1416 const GstSDPMedia * media, GstRTSPStream * stream)
1418 const GstSDPBandwidth *bw;
1420 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1421 stream->as_bandwidth = bw->bandwidth;
1423 stream->as_bandwidth = -1;
1425 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1426 stream->rr_bandwidth = bw->bandwidth;
1428 stream->rr_bandwidth = -1;
1430 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1431 stream->rs_bandwidth = bw->bandwidth;
1433 stream->rs_bandwidth = -1;
1437 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1438 const GstSDPConnection * conn)
1440 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1443 if (conn->addrtype == NULL)
1446 /* check for IPV6 */
1447 if (strcmp (conn->addrtype, "IP4") == 0)
1448 stream->is_ipv6 = FALSE;
1449 else if (strcmp (conn->addrtype, "IP6") == 0)
1450 stream->is_ipv6 = TRUE;
1455 g_free (stream->destination);
1456 stream->destination = g_strdup (conn->address);
1458 /* check for multicast */
1459 stream->is_multicast =
1460 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1462 stream->ttl = conn->ttl;
1465 /* Go over the connections for a stream.
1466 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1468 * - If we are dealing with a localhost address, we disable multicast
1471 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1472 const GstSDPMedia * media, GstRTSPStream * stream)
1474 const GstSDPConnection *conn;
1477 /* first look in the media specific section */
1478 len = gst_sdp_media_connections_len (media);
1479 for (i = 0; i < len; i++) {
1480 conn = gst_sdp_media_get_connection (media, i);
1482 gst_rtspsrc_do_stream_connection (src, stream, conn);
1484 /* then look in the message specific section */
1485 if ((conn = gst_sdp_message_get_connection (sdp))) {
1486 gst_rtspsrc_do_stream_connection (src, stream, conn);
1490 /* m=<media> <UDP port> RTP/AVP <payload>
1493 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
1494 const GstSDPMedia * media, GstRTSPStream * stream)
1498 GstCaps *global_caps;
1501 proto = gst_sdp_media_get_proto (media);
1505 if (g_str_equal (proto, "RTP/AVP"))
1506 stream->profile = GST_RTSP_PROFILE_AVP;
1507 else if (g_str_equal (proto, "RTP/SAVP"))
1508 stream->profile = GST_RTSP_PROFILE_SAVP;
1509 else if (g_str_equal (proto, "RTP/AVPF"))
1510 stream->profile = GST_RTSP_PROFILE_AVPF;
1511 else if (g_str_equal (proto, "RTP/SAVPF"))
1512 stream->profile = GST_RTSP_PROFILE_SAVPF;
1516 /* Parse global SDP attributes once */
1517 global_caps = gst_caps_new_empty_simple ("application/x-unknown");
1518 GST_DEBUG ("mapping sdp session level attributes to caps");
1519 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, global_caps);
1520 GST_DEBUG ("mapping sdp media level attributes to caps");
1521 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, global_caps);
1523 len = gst_sdp_media_formats_len (media);
1524 for (i = 0; i < len; i++) {
1526 GstCaps *caps, *outcaps;
1531 pt = atoi (gst_sdp_media_get_format (media, i));
1533 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
1536 caps = gst_rtspsrc_media_to_caps (pt, media);
1538 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
1542 /* do some tweaks */
1543 s = gst_caps_get_structure (caps, 0);
1544 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
1545 stream->is_real = (strstr (enc, "-REAL") != NULL);
1546 if (strcmp (enc, "X-ASF-PF") == 0)
1547 stream->container = TRUE;
1550 /* Merge in global caps */
1551 /* Intersect will merge in missing fields to the current caps */
1552 outcaps = gst_caps_intersect (caps, global_caps);
1553 gst_caps_unref (caps);
1555 /* the first pt will be the default */
1556 if (stream->ptmap->len == 0)
1557 stream->default_pt = pt;
1560 item.caps = outcaps;
1562 g_array_append_val (stream->ptmap, item);
1565 gst_caps_unref (global_caps);
1570 GST_ERROR_OBJECT (src, "can't find proto in media");
1575 GST_ERROR_OBJECT (src, "unknown proto in media %s", proto);
1580 static const gchar *
1581 get_aggregate_control (GstRTSPSrc * src)
1586 base = src->control;
1587 else if (src->content_base)
1588 base = src->content_base;
1589 else if (src->conninfo.url_str)
1590 base = src->conninfo.url_str;
1598 clear_ptmap_item (PtMapItem * item)
1601 gst_caps_unref (item->caps);
1604 static GstRTSPStream *
1605 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1607 GstRTSPStream *stream;
1608 const gchar *control_url;
1609 const GstSDPMedia *media;
1611 /* get media, should not return NULL */
1612 media = gst_sdp_message_get_media (sdp, idx);
1616 stream = g_new0 (GstRTSPStream, 1);
1617 stream->parent = src;
1618 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1620 stream->last_ret = GST_FLOW_NOT_LINKED;
1621 stream->added = FALSE;
1622 stream->setup = FALSE;
1623 stream->skipped = FALSE;
1625 stream->eos = FALSE;
1626 stream->discont = TRUE;
1627 stream->seqbase = -1;
1628 stream->timebase = -1;
1629 stream->send_ssrc = g_random_int ();
1630 stream->profile = GST_RTSP_PROFILE_AVP;
1631 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1632 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
1634 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1635 * session manager to scale RTCP. */
1636 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1638 /* collect connection info */
1639 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1641 /* make the payload type map */
1642 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1644 /* collect port number */
1645 stream->port = gst_sdp_media_get_port (media);
1647 /* get control url to construct the setup url. The setup url is used to
1648 * configure the transport of the stream and is used to identity the stream in
1649 * the RTP-Info header field returned from PLAY. */
1650 control_url = gst_sdp_media_get_attribute_val (media, "control");
1651 if (control_url == NULL)
1652 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1654 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1655 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1656 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1657 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1659 if (control_url != NULL) {
1660 stream->control_url = g_strdup (control_url);
1661 /* Build a fully qualified url using the content_base if any or by prefixing
1662 * the original request.
1663 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1664 * likely build a URL that the server will fail to understand, this is ok,
1665 * we will fail then. */
1666 if (g_str_has_prefix (control_url, "rtsp://"))
1667 stream->conninfo.location = g_strdup (control_url);
1672 if (g_strcmp0 (control_url, "*") == 0)
1675 base = get_aggregate_control (src);
1677 /* check if the base ends or control starts with / */
1678 has_slash = g_str_has_prefix (control_url, "/");
1679 has_slash = has_slash || g_str_has_suffix (base, "/");
1681 /* concatenate the two strings, insert / when not present */
1682 stream->conninfo.location =
1683 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1686 GST_DEBUG_OBJECT (src, " setup: %s",
1687 GST_STR_NULL (stream->conninfo.location));
1689 /* we keep track of all streams */
1690 src->streams = g_list_append (src->streams, stream);
1698 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1702 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1704 g_array_free (stream->ptmap, TRUE);
1706 g_free (stream->destination);
1707 g_free (stream->control_url);
1708 g_free (stream->conninfo.location);
1710 for (i = 0; i < 2; i++) {
1711 if (stream->udpsrc[i]) {
1712 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1713 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1714 gst_object_unref (stream->udpsrc[i]);
1716 if (stream->channelpad[i])
1717 gst_object_unref (stream->channelpad[i]);
1719 if (stream->udpsink[i]) {
1720 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1721 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1722 gst_object_unref (stream->udpsink[i]);
1725 if (stream->fakesrc) {
1726 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1727 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1728 gst_object_unref (stream->fakesrc);
1730 if (stream->srcpad) {
1731 gst_pad_set_active (stream->srcpad, FALSE);
1733 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1735 if (stream->srtpenc)
1736 gst_object_unref (stream->srtpenc);
1737 if (stream->srtpdec)
1738 gst_object_unref (stream->srtpdec);
1739 if (stream->srtcpparams)
1740 gst_caps_unref (stream->srtcpparams);
1741 if (stream->rtcppad)
1742 gst_object_unref (stream->rtcppad);
1743 if (stream->session)
1744 g_object_unref (stream->session);
1745 if (stream->rtx_pt_map)
1746 gst_structure_free (stream->rtx_pt_map);
1751 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1755 GST_DEBUG_OBJECT (src, "cleanup");
1757 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1758 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1760 gst_rtspsrc_stream_free (src, stream);
1762 g_list_free (src->streams);
1763 src->streams = NULL;
1765 if (src->manager_sig_id) {
1766 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1767 src->manager_sig_id = 0;
1769 gst_element_set_state (src->manager, GST_STATE_NULL);
1770 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1771 src->manager = NULL;
1774 gst_structure_free (src->props);
1777 g_free (src->content_base);
1778 src->content_base = NULL;
1780 g_free (src->control);
1781 src->control = NULL;
1784 gst_rtsp_range_free (src->range);
1787 /* don't clear the SDP when it was used in the url */
1788 if (src->sdp && !src->from_sdp) {
1789 gst_sdp_message_free (src->sdp);
1793 src->need_segment = FALSE;
1795 if (src->provided_clock) {
1796 gst_object_unref (src->provided_clock);
1797 src->provided_clock = NULL;
1801 #define PARSE_INT(p, del, res) \
1804 p = strstr (p, del); \
1814 #define PARSE_STRING(p, del, res) \
1817 p = strstr (p, del); \
1829 #define SKIP_SPACES(p) \
1830 while (*p && g_ascii_isspace (*p)) \
1835 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1838 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1839 gint * rate, gchar ** params)
1843 p = (gchar *) rtpmap;
1845 PARSE_INT (p, " ", *payload);
1853 PARSE_STRING (p, "/", *name);
1854 if (*name == NULL) {
1855 GST_DEBUG ("no rate, name %s", p);
1856 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1857 * streams seem to omit the rate. */
1864 p = strstr (p, "/");
1882 parse_keymgmt (const gchar * keymgmt, GstCaps * caps)
1884 gboolean res = FALSE;
1887 GstMIKEYMessage *msg;
1888 const GstMIKEYPayload *payload;
1889 const gchar *srtp_cipher;
1890 const gchar *srtp_auth;
1896 p = orig_value = g_strdup (keymgmt);
1900 g_free (orig_value);
1904 PARSE_STRING (p, " ", kmpid);
1905 if (kmpid == NULL || !g_str_equal (kmpid, "mikey")) {
1906 g_free (orig_value);
1909 data = g_base64_decode (p, &size);
1911 g_free (orig_value); /* Don't need this any more */
1917 msg = gst_mikey_message_new_from_data (data, size, NULL, NULL);
1922 srtp_cipher = "aes-128-icm";
1923 srtp_auth = "hmac-sha1-80";
1925 /* check the Security policy if any */
1926 if ((payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, 0))) {
1927 GstMIKEYPayloadSP *p = (GstMIKEYPayloadSP *) payload;
1930 if (p->proto != GST_MIKEY_SEC_PROTO_SRTP)
1933 len = gst_mikey_payload_sp_get_n_params (payload);
1934 for (i = 0; i < len; i++) {
1935 const GstMIKEYPayloadSPParam *param =
1936 gst_mikey_payload_sp_get_param (payload, i);
1938 switch (param->type) {
1939 case GST_MIKEY_SP_SRTP_ENC_ALG:
1940 switch (param->val[0]) {
1942 srtp_cipher = "null";
1946 srtp_cipher = "aes-128-icm";
1952 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
1953 switch (param->val[0]) {
1954 case AES_128_KEY_LEN:
1955 srtp_cipher = "aes-128-icm";
1957 case AES_256_KEY_LEN:
1958 srtp_cipher = "aes-256-icm";
1964 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1965 switch (param->val[0]) {
1971 srtp_auth = "hmac-sha1-80";
1977 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
1978 switch (param->val[0]) {
1979 case HMAC_32_KEY_LEN:
1980 srtp_auth = "hmac-sha1-32";
1982 case HMAC_80_KEY_LEN:
1983 srtp_auth = "hmac-sha1-80";
1989 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1991 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1999 if (!(payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_KEMAC, 0)))
2002 GstMIKEYPayloadKEMAC *p = (GstMIKEYPayloadKEMAC *) payload;
2003 const GstMIKEYPayload *sub;
2004 GstMIKEYPayloadKeyData *pkd;
2007 if (p->enc_alg != GST_MIKEY_ENC_NULL || p->mac_alg != GST_MIKEY_MAC_NULL)
2010 if (!(sub = gst_mikey_payload_kemac_get_sub (payload, 0)))
2013 if (sub->type != GST_MIKEY_PT_KEY_DATA)
2016 pkd = (GstMIKEYPayloadKeyData *) sub;
2018 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
2020 gst_caps_set_simple (caps, "srtp-key", GST_TYPE_BUFFER, buf, NULL);
2021 gst_buffer_unref (buf);
2024 gst_caps_set_simple (caps,
2025 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
2026 "srtp-auth", G_TYPE_STRING, srtp_auth,
2027 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
2028 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
2032 gst_mikey_message_unref (msg);
2038 * Mapping SDP attributes to caps
2040 * prepend 'a-' to IANA registered sdp attributes names
2041 * (ie: not prefixed with 'x-') in order to avoid
2042 * collision with gstreamer standard caps properties names
2045 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
2047 if (attributes->len > 0) {
2051 s = gst_caps_get_structure (caps, 0);
2053 for (i = 0; i < attributes->len; i++) {
2054 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
2055 gchar *tofree, *key;
2059 /* skip some of the attribute we already handle */
2060 if (!strcmp (key, "fmtp"))
2062 if (!strcmp (key, "rtpmap"))
2064 if (!strcmp (key, "control"))
2066 if (!strcmp (key, "range"))
2068 if (!strcmp (key, "framesize"))
2070 if (g_str_equal (key, "key-mgmt")) {
2071 parse_keymgmt (attr->value, caps);
2075 /* string must be valid UTF8 */
2076 if (!g_utf8_validate (attr->value, -1, NULL))
2079 if (!g_str_has_prefix (key, "x-"))
2080 tofree = key = g_strdup_printf ("a-%s", key);
2084 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
2085 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
2091 static const gchar *
2092 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
2101 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
2104 if (sscanf (attr, "%d ", &val) != 1)
2114 * Mapping of caps to and from SDP fields:
2116 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
2117 * a=framesize:<payload> <width>-<height>
2118 * a=fmtp:<payload> <param>[=<value>];...
2121 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
2124 const gchar *rtpmap;
2126 const gchar *framesize;
2129 gchar *params = NULL;
2135 /* get and parse rtpmap */
2136 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
2139 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
2141 g_warning ("error parsing rtpmap, ignoring");
2145 /* dynamic payloads need rtpmap or we fail */
2146 if (rtpmap == NULL && pt >= 96)
2149 /* check if we have a rate, if not, we need to look up the rate from the
2150 * default rates based on the payload types. */
2152 const GstRTPPayloadInfo *info;
2154 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
2155 /* dynamic types, use media and encoding_name */
2156 tmp = g_ascii_strdown (media->media, -1);
2157 info = gst_rtp_payload_info_for_name (tmp, name);
2160 /* static types, use payload type */
2161 info = gst_rtp_payload_info_for_pt (pt);
2165 if ((rate = info->clock_rate) == 0)
2168 /* we fail if we cannot find one */
2173 tmp = g_ascii_strdown (media->media, -1);
2174 caps = gst_caps_new_simple ("application/x-unknown",
2175 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
2177 s = gst_caps_get_structure (caps, 0);
2179 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
2181 /* encoding name must be upper case */
2183 tmp = g_ascii_strup (name, -1);
2184 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
2188 /* params must be lower case */
2189 if (params != NULL) {
2190 tmp = g_ascii_strdown (params, -1);
2191 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
2195 /* parse optional fmtp: field */
2196 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
2202 /* p is now of the format <payload> <param>[=<value>];... */
2203 PARSE_INT (p, " ", payload);
2204 if (payload != -1 && payload == pt) {
2208 /* <param>[=<value>] are separated with ';' */
2209 pairs = g_strsplit (p, ";", 0);
2210 for (i = 0; pairs[i]; i++) {
2212 const gchar *val, *key;
2214 const gchar *reserved_keys[] =
2215 { "media", "payload", "clock-rate", "encoding-name",
2219 /* the key may not have a '=', the value can have other '='s */
2220 valpos = strstr (pairs[i], "=");
2222 /* we have a '=' and thus a value, remove the '=' with \0 */
2224 /* value is everything between '=' and ';'. We split the pairs at ;
2225 * boundaries so we can take the remainder of the value. Some servers
2226 * put spaces around the value which we strip off here. Alternatively
2227 * we could strip those spaces in the depayloaders should these spaces
2228 * actually carry any meaning in the future. */
2229 val = g_strstrip (valpos + 1);
2231 /* simple <param>;.. is translated into <param>=1;... */
2234 /* strip the key of spaces, convert key to lowercase but not the value. */
2235 key = g_strstrip (pairs[i]);
2237 /* skip keys from the fmtp, which we already use ourselves for the
2238 * caps. Some software is adding random things like clock-rate into
2239 * the fmtp, and we would otherwise here set a string-typed clock-rate
2240 * in the caps... and thus fail to create valid RTP caps
2242 for (j = 0; j < G_N_ELEMENTS (reserved_keys); j++) {
2243 if (g_ascii_strcasecmp (reserved_keys[j], key) == 0) {
2249 if (strlen (key) > 1) {
2250 tmp = g_ascii_strdown (key, -1);
2251 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
2259 /* parse framesize: field */
2260 if ((framesize = gst_sdp_media_get_attribute_val (media, "framesize"))) {
2263 /* p is now of the format <payload> <width>-<height> */
2264 p = (gchar *) framesize;
2266 PARSE_INT (p, " ", payload);
2267 if (payload != -1 && payload == pt) {
2268 gst_structure_set (s, "a-framesize", G_TYPE_STRING, p, NULL);
2276 g_warning ("rtpmap type not given for dynamic payload %d", pt);
2281 g_warning ("rate unknown for payload type %d", pt);
2287 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
2288 gint * rtpport, gint * rtcpport)
2291 GstStateChangeReturn ret;
2292 GstElement *udpsrc0, *udpsrc1;
2293 gint tmp_rtp, tmp_rtcp;
2297 src = stream->parent;
2303 /* Start at next port */
2304 tmp_rtp = src->next_port_num;
2306 if (stream->is_ipv6)
2307 host = "udp://[::0]";
2309 host = "udp://0.0.0.0";
2311 /* try to allocate 2 UDP ports, the RTP port should be an even
2312 * number and the RTCP port should be the next (uneven) port */
2315 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
2316 tmp_rtp >= src->client_port_range.max)
2319 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2320 if (udpsrc0 == NULL)
2321 goto no_udp_protocol;
2322 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
2324 if (src->udp_buffer_size != 0)
2325 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
2328 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
2329 if (ret == GST_STATE_CHANGE_FAILURE) {
2331 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
2334 if (++count > src->retry)
2337 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2338 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2339 gst_object_unref (udpsrc0);
2342 GST_DEBUG_OBJECT (src, "retry %d", count);
2345 goto no_udp_protocol;
2348 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
2349 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
2351 /* check if port is even */
2352 if ((tmp_rtp & 0x01) != 0) {
2353 /* port not even, close and allocate another */
2354 if (++count > src->retry)
2357 GST_DEBUG_OBJECT (src, "RTP port not even");
2359 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2360 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2361 gst_object_unref (udpsrc0);
2364 GST_DEBUG_OBJECT (src, "retry %d", count);
2369 /* allocate port+1 for RTCP now */
2370 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2371 if (udpsrc1 == NULL)
2372 goto no_udp_rtcp_protocol;
2375 tmp_rtcp = tmp_rtp + 1;
2376 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
2379 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
2381 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
2382 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
2383 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
2384 if (ret == GST_STATE_CHANGE_FAILURE) {
2385 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
2387 if (++count > src->retry)
2390 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2391 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2392 gst_object_unref (udpsrc0);
2395 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2396 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2397 gst_object_unref (udpsrc1);
2401 GST_DEBUG_OBJECT (src, "retry %d", count);
2405 /* all fine, do port check */
2406 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2407 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2409 /* this should not happen... */
2410 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2413 /* we keep these elements, we configure all in configure_transport when the
2414 * server told us to really use the UDP ports. */
2415 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2416 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2417 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2418 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2420 /* keep track of next available port number when we have a range
2422 if (src->next_port_num != 0)
2423 src->next_port_num = tmp_rtcp + 1;
2430 GST_DEBUG_OBJECT (src, "could not get UDP source");
2435 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2439 no_udp_rtcp_protocol:
2441 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2446 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2447 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2453 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2454 gst_object_unref (udpsrc0);
2457 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2458 gst_object_unref (udpsrc1);
2465 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2470 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2472 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2473 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2476 for (i = 0; i < 2; i++) {
2477 if (stream->udpsrc[i])
2478 gst_element_set_state (stream->udpsrc[i], state);
2484 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2491 event = gst_event_new_flush_start ();
2492 GST_DEBUG_OBJECT (src, "start flush");
2494 state = GST_STATE_PAUSED;
2496 event = gst_event_new_flush_stop (FALSE);
2497 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2500 state = GST_STATE_PLAYING;
2502 state = GST_STATE_PAUSED;
2504 gst_rtspsrc_push_event (src, event);
2505 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2506 gst_rtspsrc_set_state (src, state);
2509 static GstRTSPResult
2510 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2511 GstRTSPMessage * message, GTimeVal * timeout)
2516 ret = gst_rtsp_connection_send (conn, message, timeout);
2518 ret = GST_RTSP_ERROR;
2523 static GstRTSPResult
2524 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2525 GstRTSPMessage * message, GTimeVal * timeout)
2530 ret = gst_rtsp_connection_receive (conn, message, timeout);
2532 ret = GST_RTSP_ERROR;
2538 gst_rtspsrc_get_position (GstRTSPSrc * src)
2543 query = gst_query_new_position (GST_FORMAT_TIME);
2544 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2545 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2546 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2550 if (stream->srcpad) {
2551 if (gst_pad_query (stream->srcpad, query)) {
2552 gst_query_parse_position (query, &fmt, &pos);
2553 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2554 GST_TIME_ARGS (pos));
2555 src->last_pos = pos;
2565 gst_query_unref (query);
2569 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2574 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2576 gboolean flush, skip;
2579 GstSegment seeksegment = { 0, };
2583 GST_DEBUG_OBJECT (src, "doing seek with event");
2585 gst_event_parse_seek (event, &rate, &format, &flags,
2586 &cur_type, &cur, &stop_type, &stop);
2588 /* no negative rates yet */
2592 /* we need TIME format */
2593 if (format != src->segment.format)
2596 GST_DEBUG_OBJECT (src, "doing seek without event");
2598 cur_type = GST_SEEK_TYPE_SET;
2599 stop_type = GST_SEEK_TYPE_SET;
2602 /* get flush flag */
2603 flush = flags & GST_SEEK_FLAG_FLUSH;
2604 skip = flags & GST_SEEK_FLAG_SKIP;
2606 /* now we need to make sure the streaming thread is stopped. We do this by
2607 * either sending a FLUSH_START event downstream which will cause the
2608 * streaming thread to stop with a WRONG_STATE.
2609 * For a non-flushing seek we simply pause the task, which will happen as soon
2610 * as it completes one iteration (and thus might block when the sink is
2611 * blocking in preroll). */
2613 GST_DEBUG_OBJECT (src, "starting flush");
2614 gst_rtspsrc_flush (src, TRUE, FALSE);
2617 gst_task_pause (src->task);
2621 /* we should now be able to grab the streaming thread because we stopped it
2622 * with the above flush/pause code */
2623 GST_RTSP_STREAM_LOCK (src);
2625 GST_DEBUG_OBJECT (src, "stopped streaming");
2627 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2628 gst_rtspsrc_connection_flush (src, FALSE);
2630 /* copy segment, we need this because we still need the old
2631 * segment when we close the current segment. */
2632 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2634 /* configure the seek parameters in the seeksegment. We will then have the
2635 * right values in the segment to perform the seek */
2637 GST_DEBUG_OBJECT (src, "configuring seek");
2638 gst_segment_do_seek (&seeksegment, rate, format, flags,
2639 cur_type, cur, stop_type, stop, &update);
2642 /* figure out the last position we need to play. If it's configured (stop !=
2643 * -1), use that, else we play until the total duration of the file */
2644 if ((stop = seeksegment.stop) == -1)
2645 stop = seeksegment.duration;
2647 playing = (src->state == GST_RTSP_STATE_PLAYING);
2649 /* if we were playing, pause first */
2651 /* obtain current position in case seek fails */
2652 gst_rtspsrc_get_position (src);
2653 gst_rtspsrc_pause (src, FALSE);
2657 src->state = GST_RTSP_STATE_SEEKING;
2659 /* PLAY will add the range header now. */
2660 src->need_range = TRUE;
2662 /* and continue playing */
2664 gst_rtspsrc_play (src, &seeksegment, FALSE);
2666 /* prepare for streaming again */
2668 /* if we started flush, we stop now */
2669 GST_DEBUG_OBJECT (src, "stopping flush");
2670 gst_rtspsrc_flush (src, FALSE, playing);
2673 /* now we did the seek and can activate the new segment values */
2674 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2676 /* if we're doing a segment seek, post a SEGMENT_START message */
2677 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2678 gst_element_post_message (GST_ELEMENT_CAST (src),
2679 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2680 src->segment.format, src->segment.position));
2683 /* now create the newsegment */
2684 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2685 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2688 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2689 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2690 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2691 stream->discont = TRUE;
2694 GST_RTSP_STREAM_UNLOCK (src);
2701 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2706 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2712 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2716 gboolean res = TRUE;
2719 src = GST_RTSPSRC_CAST (parent);
2721 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2722 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2724 switch (GST_EVENT_TYPE (event)) {
2725 case GST_EVENT_SEEK:
2726 res = gst_rtspsrc_perform_seek (src, event);
2730 case GST_EVENT_NAVIGATION:
2731 case GST_EVENT_LATENCY:
2739 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2740 res = gst_pad_send_event (target, event);
2741 gst_object_unref (target);
2743 gst_event_unref (event);
2746 gst_event_unref (event);
2752 /* this is the final event function we receive on the internal source pad when
2753 * we deal with TCP connections */
2755 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2760 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2762 switch (GST_EVENT_TYPE (event)) {
2763 case GST_EVENT_SEEK:
2765 case GST_EVENT_NAVIGATION:
2766 case GST_EVENT_LATENCY:
2768 gst_event_unref (event);
2775 /* this is the final query function we receive on the internal source pad when
2776 * we deal with TCP connections */
2778 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2782 gboolean res = TRUE;
2784 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2786 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2787 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2789 switch (GST_QUERY_TYPE (query)) {
2790 case GST_QUERY_POSITION:
2795 case GST_QUERY_DURATION:
2799 gst_query_parse_duration (query, &format, NULL);
2802 case GST_FORMAT_TIME:
2803 gst_query_set_duration (query, format, src->segment.duration);
2811 case GST_QUERY_LATENCY:
2813 /* we are live with a min latency of 0 and unlimited max latency, this
2814 * result will be updated by the session manager if there is any. */
2815 gst_query_set_latency (query, TRUE, 0, -1);
2825 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2827 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2831 gboolean res = FALSE;
2833 src = GST_RTSPSRC_CAST (parent);
2835 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2836 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2838 switch (GST_QUERY_TYPE (query)) {
2839 case GST_QUERY_DURATION:
2843 gst_query_parse_duration (query, &format, NULL);
2846 case GST_FORMAT_TIME:
2847 gst_query_set_duration (query, format, src->segment.duration);
2855 case GST_QUERY_SEEKING:
2859 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2860 if (format == GST_FORMAT_TIME) {
2862 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2864 /* seeking without duration is unlikely */
2865 seekable = seekable && src->seekable && src->segment.duration &&
2866 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2868 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0,
2869 src->segment.duration);
2878 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2880 gst_query_set_uri (query, uri);
2888 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2890 /* forward the query to the proxy target pad */
2892 res = gst_pad_query (target, query);
2893 gst_object_unref (target);
2902 /* callback for RTCP messages to be sent to the server when operating in TCP
2904 static GstFlowReturn
2905 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2908 GstRTSPStream *stream;
2909 GstFlowReturn res = GST_FLOW_OK;
2914 GstRTSPMessage message = { 0 };
2915 GstRTSPConnection *conn;
2917 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2918 src = stream->parent;
2920 gst_buffer_map (buffer, &map, GST_MAP_READ);
2924 gst_rtsp_message_init_data (&message, stream->channel[1]);
2926 /* lend the body data to the message */
2927 gst_rtsp_message_take_body (&message, data, size);
2929 if (stream->conninfo.connection)
2930 conn = stream->conninfo.connection;
2932 conn = src->conninfo.connection;
2934 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2935 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2936 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2938 /* and steal it away again because we will free it when unreffing the
2940 gst_rtsp_message_steal_body (&message, &data, &size);
2941 gst_rtsp_message_unset (&message);
2943 gst_buffer_unmap (buffer, &map);
2944 gst_buffer_unref (buffer);
2949 static GstPadProbeReturn
2950 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2952 GstRTSPSrc *src = user_data;
2954 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2955 GST_DEBUG_PAD_NAME (pad));
2957 /* activate the streams */
2958 GST_OBJECT_LOCK (src);
2959 if (!src->need_activate)
2962 src->need_activate = FALSE;
2963 GST_OBJECT_UNLOCK (src);
2965 gst_rtspsrc_activate_streams (src);
2967 return GST_PAD_PROBE_OK;
2971 GST_OBJECT_UNLOCK (src);
2972 return GST_PAD_PROBE_OK;
2977 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
2979 GstPad *gpad = GST_PAD_CAST (user_data);
2981 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
2982 gst_pad_store_sticky_event (gpad, *event);
2987 /* this callback is called when the session manager generated a new src pad with
2988 * payloaded RTP packets. We simply ghost the pad here. */
2990 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2993 GstPadTemplate *template;
2996 GstRTSPStream *stream;
2999 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
3001 GST_RTSP_STATE_LOCK (src);
3003 name = gst_object_get_name (GST_OBJECT_CAST (pad));
3004 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
3005 goto unknown_stream;
3007 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
3009 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
3011 goto unknown_stream;
3014 stream->ssrc = ssrc;
3016 /* we'll add it later see below */
3017 stream->added = TRUE;
3019 /* check if we added all streams */
3021 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
3022 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
3024 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
3025 ostream, ostream->container, ostream->added, ostream->setup);
3027 /* if we find a stream for which we did a setup that is not added, we
3028 * need to wait some more */
3029 if (ostream->setup && !ostream->added) {
3034 GST_RTSP_STATE_UNLOCK (src);
3036 /* create a new pad we will use to stream to */
3037 template = gst_static_pad_template_get (&rtptemplate);
3038 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
3039 gst_object_unref (template);
3042 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3043 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3044 gst_pad_set_active (stream->srcpad, TRUE);
3045 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
3046 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3049 GST_DEBUG_OBJECT (src, "We added all streams");
3050 /* when we get here, all stream are added and we can fire the no-more-pads
3052 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
3060 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
3061 GST_RTSP_STATE_UNLOCK (src);
3068 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
3072 len = stream->ptmap->len;
3073 for (i = 0; i < len; i++) {
3074 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3082 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
3084 GstRTSPStream *stream;
3087 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
3089 GST_RTSP_STATE_LOCK (src);
3090 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3092 goto unknown_stream;
3094 if ((caps = stream_get_caps_for_pt (stream, pt)))
3095 gst_caps_ref (caps);
3096 GST_RTSP_STATE_UNLOCK (src);
3102 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
3103 GST_RTSP_STATE_UNLOCK (src);
3109 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
3111 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
3117 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
3123 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
3129 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
3131 GstRTSPSrc *src = stream->parent;
3134 g_object_get (source, "ssrc", &ssrc, NULL);
3136 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
3137 ssrc, stream->ssrc, stream->id);
3139 if (ssrc == stream->ssrc)
3140 gst_rtspsrc_do_stream_eos (src, stream);
3144 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
3146 GstRTSPSrc *src = stream->parent;
3149 g_object_get (source, "ssrc", &ssrc, NULL);
3151 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
3152 ssrc, stream->ssrc, stream->id);
3154 if (ssrc == stream->ssrc)
3155 gst_rtspsrc_do_stream_eos (src, stream);
3159 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
3161 GstRTSPStream *stream;
3163 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
3165 /* get stream for session */
3166 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3168 gst_rtspsrc_do_stream_eos (src, stream);
3173 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
3175 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
3180 set_manager_buffer_mode (GstRTSPSrc * src)
3182 GObjectClass *klass;
3184 if (src->manager == NULL)
3187 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3189 if (!g_object_class_find_property (klass, "buffer-mode"))
3192 if (src->buffer_mode != BUFFER_MODE_AUTO) {
3193 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
3198 GST_DEBUG_OBJECT (src,
3199 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
3201 if (src->provided_clock) {
3202 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
3204 if (clock == src->provided_clock) {
3205 GST_DEBUG_OBJECT (src, "selected synced");
3206 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
3209 gst_object_unref (clock);
3214 /* Otherwise fall-through and use another buffer mode */
3216 gst_object_unref (clock);
3219 GST_DEBUG_OBJECT (src, "auto buffering mode");
3220 if (src->use_buffering) {
3221 GST_DEBUG_OBJECT (src, "selected buffer");
3222 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
3224 GST_DEBUG_OBJECT (src, "selected slave");
3225 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
3230 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
3232 GST_DEBUG ("request key %u", ssrc);
3233 return gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
3237 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
3239 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3240 if (stream->id != session)
3243 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3244 stream->profile != GST_RTSP_PROFILE_SAVPF)
3247 if (stream->srtpdec == NULL) {
3250 name = g_strdup_printf ("srtpdec_%u", session);
3251 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
3254 g_signal_connect (stream->srtpdec, "request-key",
3255 (GCallback) request_key, stream);
3257 return gst_object_ref (stream->srtpdec);
3261 request_rtcp_encoder (GstElement * rtpbin, guint session,
3262 GstRTSPStream * stream)
3267 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3268 if (stream->id != session)
3271 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3272 stream->profile != GST_RTSP_PROFILE_SAVPF)
3275 if (stream->srtpenc == NULL) {
3278 name = g_strdup_printf ("srtpenc_%u", session);
3279 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
3282 /* get RTCP crypto parameters from caps */
3283 s = gst_caps_get_structure (stream->srtcpparams, 0);
3287 GType ciphertype, authtype;
3288 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
3290 ciphertype = g_type_from_name ("GstSrtpCipherType");
3291 authtype = g_type_from_name ("GstSrtpAuthType");
3292 g_value_init (&rtcp_cipher, ciphertype);
3293 g_value_init (&rtcp_auth, authtype);
3295 str = gst_structure_get_string (s, "srtcp-cipher");
3296 gst_value_deserialize (&rtcp_cipher, str);
3297 str = gst_structure_get_string (s, "srtcp-auth");
3298 gst_value_deserialize (&rtcp_auth, str);
3299 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
3301 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
3303 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
3305 g_object_set (stream->srtpenc, "key", buf, NULL);
3307 g_value_unset (&rtcp_cipher);
3308 g_value_unset (&rtcp_auth);
3309 gst_buffer_unref (buf);
3312 name = g_strdup_printf ("rtcp_sink_%d", session);
3313 pad = gst_element_get_request_pad (stream->srtpenc, name);
3315 gst_object_unref (pad);
3317 return gst_object_ref (stream->srtpenc);
3321 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
3323 GstElement *rtx, *bin;
3326 GstRTSPStream *stream;
3328 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
3330 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
3334 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
3335 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
3336 bin = gst_bin_new (NULL);
3337 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
3338 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
3339 gst_bin_add (GST_BIN (bin), rtx);
3341 pad = gst_element_get_static_pad (rtx, "src");
3342 name = g_strdup_printf ("src_%u", sessid);
3343 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3345 gst_object_unref (pad);
3347 pad = gst_element_get_static_pad (rtx, "sink");
3348 name = g_strdup_printf ("sink_%u", sessid);
3349 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3351 gst_object_unref (pad);
3357 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
3361 gboolean do_retransmission = FALSE;
3363 if (transport->trans != GST_RTSP_TRANS_RTP)
3365 if (transport->profile != GST_RTSP_PROFILE_AVPF &&
3366 transport->profile != GST_RTSP_PROFILE_SAVPF)
3369 signal_id = g_signal_lookup ("request-aux-receiver",
3370 G_OBJECT_TYPE (src->manager));
3371 /* there's already something connected */
3372 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
3373 NULL, NULL, NULL) != 0) {
3374 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
3375 "\"request-aux-receiver\" signal is "
3376 "already used by the application");
3380 /* build the retransmission payload type map */
3381 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3382 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3383 gboolean do_retransmission_stream = FALSE;
3386 if (stream->rtx_pt_map)
3387 gst_structure_free (stream->rtx_pt_map);
3388 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
3390 for (i = 0; i < stream->ptmap->len; i++) {
3391 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3392 GstStructure *s = gst_caps_get_structure (item->caps, 0);
3393 const gchar *encoding;
3395 /* we only care about RTX streams */
3396 if ((encoding = gst_structure_get_string (s, "encoding-name"))
3397 && g_strcmp0 (encoding, "RTX") == 0) {
3398 const gchar *stream_pt_s;
3401 if (gst_structure_get_int (s, "payload", &rtx_pt)
3402 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
3405 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
3407 do_retransmission_stream = TRUE;
3413 if (do_retransmission_stream) {
3414 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
3415 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
3416 do_retransmission = TRUE;
3418 GST_DEBUG_OBJECT (src, "no retransmission payload map for stream "
3419 "id %i", stream->id);
3420 gst_structure_free (stream->rtx_pt_map);
3421 stream->rtx_pt_map = NULL;
3425 if (do_retransmission) {
3426 GST_DEBUG_OBJECT (src, "Enabling retransmissions");
3428 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
3430 /* enable RFC4588 retransmission handling by setting rtprtxreceive
3431 * as the "aux" element of rtpbin */
3432 g_signal_connect (src->manager, "request-aux-receiver",
3433 (GCallback) request_aux_receiver, src);
3435 GST_DEBUG_OBJECT (src,
3436 "Not enabling retransmissions as no stream had a retransmission payload map");
3440 /* try to get and configure a manager */
3442 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3443 GstRTSPTransport * transport)
3445 const gchar *manager;
3447 GstStateChangeReturn ret;
3449 /* find a manager */
3450 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3454 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3456 /* configure the manager */
3457 if (src->manager == NULL) {
3458 GObjectClass *klass;
3460 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3462 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3466 goto use_no_manager;
3468 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3469 goto manager_failed;
3472 /* we manage this element */
3473 gst_element_set_locked_state (src->manager, TRUE);
3474 gst_bin_add (GST_BIN_CAST (src), src->manager);
3476 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3477 if (ret == GST_STATE_CHANGE_FAILURE)
3478 goto start_manager_failure;
3480 g_object_set (src->manager, "latency", src->latency, NULL);
3482 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3484 if (g_object_class_find_property (klass, "ntp-sync")) {
3485 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3488 if (src->use_pipeline_clock) {
3489 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3490 g_object_set (src->manager, "use-pipeline-clock", TRUE, NULL);
3493 if (g_object_class_find_property (klass, "ntp-time-source")) {
3494 g_object_set (src->manager, "ntp-time-source", src->ntp_time_source,
3499 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3500 g_object_set (src->manager, "sdes", src->sdes, NULL);
3503 if (g_object_class_find_property (klass, "drop-on-latency")) {
3504 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3508 if (g_object_class_find_property (klass, "max-rtcp-rtp-time-diff")) {
3509 g_object_set (src->manager, "max-rtcp-rtp-time-diff",
3510 src->max_rtcp_rtp_time_diff, NULL);
3513 /* buffer mode pauses are handled by adding offsets to buffer times,
3514 * but some depayloaders may have a hard time syncing output times
3515 * with such input times, e.g. container ones, most notably ASF */
3516 /* TODO alternatives are having an event that indicates these shifts,
3517 * or having rtsp extensions provide suggestion on buffer mode */
3518 /* valid duration implies not likely live pipeline,
3519 * so slaving in jitterbuffer does not make much sense
3520 * (and might mess things up due to bursts) */
3521 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3522 src->segment.duration && stream->container) {
3523 src->use_buffering = TRUE;
3525 src->use_buffering = FALSE;
3528 set_manager_buffer_mode (src);
3530 /* connect to signals */
3531 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3533 src->manager_sig_id =
3534 g_signal_connect (src->manager, "pad-added",
3535 (GCallback) new_manager_pad, src);
3536 src->manager_ptmap_id =
3537 g_signal_connect (src->manager, "request-pt-map",
3538 (GCallback) request_pt_map, src);
3540 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3543 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3546 if (src->do_retransmission)
3547 add_retransmission (src, transport);
3549 g_signal_connect (src->manager, "request-rtp-decoder",
3550 (GCallback) request_rtp_decoder, stream);
3551 g_signal_connect (src->manager, "request-rtcp-decoder",
3552 (GCallback) request_rtp_decoder, stream);
3553 g_signal_connect (src->manager, "request-rtcp-encoder",
3554 (GCallback) request_rtcp_encoder, stream);
3556 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3557 * into a separate RTP session. */
3558 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3559 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3561 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3562 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3565 /* now configure the bandwidth in the manager */
3566 if (g_signal_lookup ("get-internal-session",
3567 G_OBJECT_TYPE (src->manager)) != 0) {
3568 GObject *rtpsession;
3570 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3573 GstRTPProfile rtp_profile;
3575 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3577 stream->session = rtpsession;
3579 if (stream->as_bandwidth != -1) {
3580 GST_INFO_OBJECT (src, "setting AS: %f",
3581 (gdouble) (stream->as_bandwidth * 1000));
3582 g_object_set (rtpsession, "bandwidth",
3583 (gdouble) (stream->as_bandwidth * 1000), NULL);
3585 if (stream->rr_bandwidth != -1) {
3586 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3587 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3590 if (stream->rs_bandwidth != -1) {
3591 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3592 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3596 switch (stream->profile) {
3597 case GST_RTSP_PROFILE_AVPF:
3598 rtp_profile = GST_RTP_PROFILE_AVPF;
3600 case GST_RTSP_PROFILE_SAVP:
3601 rtp_profile = GST_RTP_PROFILE_SAVP;
3603 case GST_RTSP_PROFILE_SAVPF:
3604 rtp_profile = GST_RTP_PROFILE_SAVPF;
3606 case GST_RTSP_PROFILE_AVP:
3608 rtp_profile = GST_RTP_PROFILE_AVP;
3612 g_object_set (rtpsession, "rtp-profile", rtp_profile, NULL);
3614 g_object_set (rtpsession, "probation", src->probation, NULL);
3616 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3618 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3620 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3622 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3624 g_signal_connect (rtpsession, "on-ssrc-active",
3625 (GCallback) on_ssrc_active, stream);
3636 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3641 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3644 start_manager_failure:
3646 GST_DEBUG_OBJECT (src, "could not start session manager");
3651 /* free the UDP sources allocated when negotiating a transport.
3652 * This function is called when the server negotiated to a transport where the
3653 * UDP sources are not needed anymore, such as TCP or multicast. */
3655 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3659 for (i = 0; i < 2; i++) {
3660 if (stream->udpsrc[i]) {
3661 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3662 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3663 gst_object_unref (stream->udpsrc[i]);
3664 stream->udpsrc[i] = NULL;
3669 /* for TCP, create pads to send and receive data to and from the manager and to
3670 * intercept various events and queries
3673 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3674 GstRTSPTransport * transport, GstPad ** outpad)
3677 GstPadTemplate *template;
3678 GstPad *pad0, *pad1;
3680 /* configure for interleaved delivery, nothing needs to be done
3681 * here, the loop function will call the chain functions of the
3682 * session manager. */
3683 stream->channel[0] = transport->interleaved.min;
3684 stream->channel[1] = transport->interleaved.max;
3685 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3686 stream->channel[0], stream->channel[1]);
3688 /* we can remove the allocated UDP ports now */
3689 gst_rtspsrc_stream_free_udp (stream);
3691 /* no session manager, send data to srcpad directly */
3692 if (!stream->channelpad[0]) {
3693 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3695 /* create a new pad we will use to stream to */
3696 name = g_strdup_printf ("stream_%u", stream->id);
3697 template = gst_static_pad_template_get (&rtptemplate);
3698 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3699 gst_object_unref (template);
3702 /* set caps and activate */
3703 gst_pad_use_fixed_caps (stream->channelpad[0]);
3704 gst_pad_set_active (stream->channelpad[0], TRUE);
3706 *outpad = gst_object_ref (stream->channelpad[0]);
3708 GST_DEBUG_OBJECT (src, "using manager source pad");
3710 template = gst_static_pad_template_get (&anysrctemplate);
3712 /* allocate pads for sending the channel data into the manager */
3713 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3714 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3715 gst_object_unref (stream->channelpad[0]);
3716 stream->channelpad[0] = pad0;
3717 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3718 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3719 gst_pad_set_element_private (pad0, src);
3720 gst_pad_set_active (pad0, TRUE);
3722 if (stream->channelpad[1]) {
3723 /* if we have a sinkpad for the other channel, create a pad and link to the
3725 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3726 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3727 gst_pad_link_full (pad1, stream->channelpad[1],
3728 GST_PAD_LINK_CHECK_NOTHING);
3729 gst_object_unref (stream->channelpad[1]);
3730 stream->channelpad[1] = pad1;
3731 gst_pad_set_active (pad1, TRUE);
3733 gst_object_unref (template);
3735 /* setup RTCP transport back to the server if we have to. */
3736 if (src->manager && src->do_rtcp) {
3739 template = gst_static_pad_template_get (&anysinktemplate);
3741 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3742 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3743 gst_pad_set_element_private (stream->rtcppad, stream);
3744 gst_pad_set_active (stream->rtcppad, TRUE);
3746 /* get session RTCP pad */
3747 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3748 pad = gst_element_get_request_pad (src->manager, name);
3753 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3754 gst_object_unref (pad);
3757 gst_object_unref (template);
3763 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3764 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3765 gint * max, guint * ttl)
3767 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3769 if (!(*destination = transport->destination))
3770 *destination = stream->destination;
3773 /* transport first */
3774 *min = transport->port.min;
3775 *max = transport->port.max;
3776 if (*min == -1 && *max == -1) {
3777 /* then try from SDP */
3778 if (stream->port != 0) {
3779 *min = stream->port;
3780 *max = stream->port + 1;
3786 if (!(*ttl = transport->ttl))
3791 /* first take the source, then the endpoint to figure out where to send
3793 if (!(*destination = transport->source)) {
3794 if (src->conninfo.connection)
3795 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3796 else if (stream->conninfo.connection)
3798 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3802 /* for unicast we only expect the ports here */
3803 *min = transport->server_port.min;
3804 *max = transport->server_port.max;
3809 /* For multicast create UDP sources and join the multicast group. */
3811 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3812 GstRTSPTransport * transport, GstPad ** outpad)
3815 const gchar *destination;
3818 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3820 /* we can remove the allocated UDP ports now */
3821 gst_rtspsrc_stream_free_udp (stream);
3823 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3826 /* we need a destination now */
3827 if (destination == NULL)
3828 goto no_destination;
3830 /* we really need ports now or we won't be able to receive anything at all */
3831 if (min == -1 && max == -1)
3834 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3835 destination, min, max);
3837 /* creating UDP source for RTP */
3839 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3841 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3843 if (stream->udpsrc[0] == NULL)
3846 /* take ownership */
3847 gst_object_ref_sink (stream->udpsrc[0]);
3849 if (src->udp_buffer_size != 0)
3850 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3851 src->udp_buffer_size, NULL);
3853 if (src->multi_iface != NULL)
3854 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3855 src->multi_iface, NULL);
3858 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3859 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3862 /* creating another UDP source for RTCP */
3866 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3868 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3870 if (stream->udpsrc[1] == NULL)
3873 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3874 stream->profile == GST_RTSP_PROFILE_SAVPF)
3875 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3877 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3878 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3879 gst_caps_unref (caps);
3881 /* take ownership */
3882 gst_object_ref_sink (stream->udpsrc[1]);
3884 if (src->multi_iface != NULL)
3885 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3886 src->multi_iface, NULL);
3888 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
3895 GST_DEBUG_OBJECT (src, "no UDP source element found");
3900 GST_DEBUG_OBJECT (src, "no destination found");
3905 GST_DEBUG_OBJECT (src, "no ports found");
3910 /* configure the remainder of the UDP ports */
3912 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3913 GstRTSPTransport * transport, GstPad ** outpad)
3915 /* we manage the UDP elements now. For unicast, the UDP sources where
3916 * allocated in the stream when we suggested a transport. */
3917 if (stream->udpsrc[0]) {
3920 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3921 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3923 GST_DEBUG_OBJECT (src, "setting up UDP source");
3925 /* configure a timeout on the UDP port. When the timeout message is
3926 * posted, we assume UDP transport is not possible. We reconnect using TCP
3928 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3929 src->udp_timeout * 1000, NULL);
3931 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
3932 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3934 /* get output pad of the UDP source. */
3935 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3937 /* save it so we can unblock */
3938 stream->blockedpad = *outpad;
3940 /* configure pad block on the pad. As soon as there is dataflow on the
3941 * UDP source, we know that UDP is not blocked by a firewall and we can
3942 * configure all the streams to let the application autoplug decoders. */
3944 gst_pad_add_probe (stream->blockedpad,
3945 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3946 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
3948 if (stream->channelpad[0]) {
3949 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3950 /* configure for UDP delivery, we need to connect the UDP pads to
3951 * the session plugin. */
3952 gst_pad_link_full (*outpad, stream->channelpad[0],
3953 GST_PAD_LINK_CHECK_NOTHING);
3954 gst_object_unref (*outpad);
3956 /* we connected to pad-added signal to get pads from the manager */
3958 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3963 if (stream->udpsrc[1]) {
3966 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3967 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3969 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3970 stream->profile == GST_RTSP_PROFILE_SAVPF)
3971 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3973 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3974 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3975 gst_caps_unref (caps);
3977 if (stream->channelpad[1]) {
3980 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3982 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3983 gst_pad_link_full (pad, stream->channelpad[1],
3984 GST_PAD_LINK_CHECK_NOTHING);
3985 gst_object_unref (pad);
3987 /* leave unlinked */
3993 /* configure the UDP sink back to the server for status reports */
3995 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3996 GstRTSPStream * stream, GstRTSPTransport * transport)
3999 gint rtp_port, rtcp_port;
4000 gboolean do_rtp, do_rtcp;
4001 const gchar *destination;
4006 /* get transport info */
4007 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
4008 &rtp_port, &rtcp_port, &ttl);
4010 /* see what we need to do */
4011 do_rtp = (rtp_port != -1);
4012 /* it's possible that the server does not want us to send RTCP in which case
4014 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
4016 /* we need a destination when we have RTP or RTCP ports */
4017 if (destination == NULL && (do_rtp || do_rtcp))
4018 goto no_destination;
4020 /* try to construct the fakesrc to the RTP port of the server to open up any
4023 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
4026 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
4027 stream->udpsink[0] =
4028 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
4030 if (stream->udpsink[0] == NULL)
4031 goto no_sink_element;
4033 /* don't join multicast group, we will have the source socket do that */
4034 /* no sync or async state changes needed */
4035 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
4036 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
4038 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4040 if (stream->udpsrc[0]) {
4041 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
4042 * so that NAT firewalls will open a hole for us */
4043 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
4044 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
4045 /* configure socket and make sure udpsink does not close it when shutting
4046 * down, it belongs to udpsrc after all. */
4047 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
4048 "close-socket", FALSE, NULL);
4049 g_object_unref (socket);
4052 /* the source for the dummy packets to open up NAT */
4053 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
4054 if (stream->fakesrc == NULL)
4055 goto no_fakesrc_element;
4057 /* random data in 5 buffers, a size of 200 bytes should be fine */
4058 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
4059 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
4061 /* we don't want to consider this a sink */
4062 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
4064 /* keep everything locked */
4065 gst_element_set_locked_state (stream->udpsink[0], TRUE);
4066 gst_element_set_locked_state (stream->fakesrc, TRUE);
4068 gst_object_ref (stream->udpsink[0]);
4069 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
4070 gst_object_ref (stream->fakesrc);
4071 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
4073 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
4074 "sink", GST_PAD_LINK_CHECK_NOTHING);
4077 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
4080 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
4081 stream->udpsink[1] =
4082 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
4084 if (stream->udpsink[1] == NULL)
4085 goto no_sink_element;
4087 /* don't join multicast group, we will have the source socket do that */
4088 /* no sync or async state changes needed */
4089 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
4090 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
4092 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4094 if (stream->udpsrc[1]) {
4095 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
4096 * because some servers check the port number of where it sends RTCP to identify
4097 * the RTCP packets it receives */
4098 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
4099 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
4100 /* configure socket and make sure udpsink does not close it when shutting
4101 * down, it belongs to udpsrc after all. */
4102 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
4103 "close-socket", FALSE, NULL);
4104 g_object_unref (socket);
4107 /* we don't want to consider this a sink */
4108 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
4110 /* we keep this playing always */
4111 gst_element_set_locked_state (stream->udpsink[1], TRUE);
4112 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
4114 gst_object_ref (stream->udpsink[1]);
4115 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
4117 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
4119 /* get session RTCP pad */
4120 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
4121 pad = gst_element_get_request_pad (src->manager, name);
4126 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
4127 gst_object_unref (pad);
4136 GST_DEBUG_OBJECT (src, "no destination address specified");
4141 GST_DEBUG_OBJECT (src, "no UDP sink element found");
4146 GST_DEBUG_OBJECT (src, "no fakesrc element found");
4151 /* sets up all elements needed for streaming over the specified transport.
4152 * Does not yet expose the element pads, this will be done when there is actuall
4153 * dataflow detected, which might never happen when UDP is blocked in a
4154 * firewall, for example.
4157 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
4158 GstRTSPTransport * transport)
4161 GstPad *outpad = NULL;
4162 GstPadTemplate *template;
4164 const gchar *media_type;
4167 src = stream->parent;
4169 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
4171 /* get the proper media type for this stream now */
4172 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
4173 goto unknown_transport;
4175 goto unknown_transport;
4177 /* configure the final media type */
4178 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
4180 len = stream->ptmap->len;
4181 for (i = 0; i < len; i++) {
4183 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
4185 if (item->caps == NULL)
4188 s = gst_caps_get_structure (item->caps, 0);
4189 gst_structure_set_name (s, media_type);
4190 /* set ssrc if known */
4191 if (transport->ssrc)
4192 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
4195 /* try to get and configure a manager, channelpad[0-1] will be configured with
4196 * the pads for the manager, or NULL when no manager is needed. */
4197 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
4200 switch (transport->lower_transport) {
4201 case GST_RTSP_LOWER_TRANS_TCP:
4202 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
4203 goto transport_failed;
4205 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
4206 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
4207 goto transport_failed;
4208 /* fallthrough, the rest is the same for UDP and MCAST */
4209 case GST_RTSP_LOWER_TRANS_UDP:
4210 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
4211 goto transport_failed;
4212 /* configure udpsinks back to the server for RTCP messages and for the
4213 * dummy RTP messages to open NAT. */
4214 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
4215 goto transport_failed;
4218 goto unknown_transport;
4222 GST_DEBUG_OBJECT (src, "creating ghostpad");
4224 gst_pad_use_fixed_caps (outpad);
4226 /* create ghostpad, don't add just yet, this will be done when we activate
4228 name = g_strdup_printf ("stream_%u", stream->id);
4229 template = gst_static_pad_template_get (&rtptemplate);
4230 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
4231 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
4232 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
4233 gst_object_unref (template);
4236 gst_object_unref (outpad);
4238 /* mark pad as ok */
4239 stream->last_ret = GST_FLOW_OK;
4246 GST_DEBUG_OBJECT (src, "failed to configure transport");
4251 GST_DEBUG_OBJECT (src, "unknown transport");
4256 GST_DEBUG_OBJECT (src, "cannot get a session manager");
4261 /* send a couple of dummy random packets on the receiver RTP port to the server,
4262 * this should make a firewall think we initiated the data transfer and
4263 * hopefully allow packets to go from the sender port to our RTP receiver port */
4265 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
4269 if (src->nat_method != GST_RTSP_NAT_DUMMY)
4272 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4273 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4275 if (stream->fakesrc && stream->udpsink[0]) {
4276 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
4277 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
4278 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
4279 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
4280 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
4286 /* Adds the source pads of all configured streams to the element.
4287 * This code is performed when we detected dataflow.
4289 * We detect dataflow from either the _loop function or with pad probes on the
4293 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
4297 GST_DEBUG_OBJECT (src, "activating streams");
4299 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4300 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4302 if (stream->udpsrc[0]) {
4303 /* remove timeout, we are streaming now and timeouts will be handled by
4304 * the session manager and jitter buffer */
4305 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
4307 if (stream->srcpad) {
4308 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
4309 gst_pad_set_active (stream->srcpad, TRUE);
4311 /* if we don't have a session manager, set the caps now. If we have a
4312 * session, we will get a notification of the pad and the caps. */
4313 if (!src->manager) {
4316 caps = stream_get_caps_for_pt (stream, stream->default_pt);
4317 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
4318 gst_pad_set_caps (stream->srcpad, caps);
4321 if (!stream->added) {
4322 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
4323 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
4324 stream->added = TRUE;
4329 /* unblock all pads */
4330 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4331 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4333 if (stream->blockid) {
4334 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
4335 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
4336 stream->blockid = 0;
4344 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
4345 gboolean reset_manager)
4348 guint64 start, stop;
4349 gdouble play_speed, play_scale;
4351 GST_DEBUG_OBJECT (src, "configuring stream caps");
4353 start = segment->position;
4354 stop = segment->duration;
4355 play_speed = segment->rate;
4356 play_scale = segment->applied_rate;
4358 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4359 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4365 len = stream->ptmap->len;
4366 for (j = 0; j < len; j++) {
4368 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
4370 if (item->caps == NULL)
4373 caps = gst_caps_make_writable (item->caps);
4375 if (stream->timebase != -1)
4376 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
4377 (guint) stream->timebase, NULL);
4378 if (stream->seqbase != -1)
4379 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
4380 (guint) stream->seqbase, NULL);
4381 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
4383 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
4384 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
4385 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
4388 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
4391 if (item->pt == stream->default_pt && stream->udpsrc[0]) {
4392 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4396 if (reset_manager && src->manager) {
4397 GST_DEBUG_OBJECT (src, "clear session");
4398 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
4402 static GstFlowReturn
4403 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
4408 /* store the value */
4409 stream->last_ret = ret;
4411 /* if it's success we can return the value right away */
4412 if (ret == GST_FLOW_OK)
4415 /* any other error that is not-linked can be returned right
4417 if (ret != GST_FLOW_NOT_LINKED)
4420 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4421 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4422 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4424 ret = ostream->last_ret;
4425 /* some other return value (must be SUCCESS but we can return
4426 * other values as well) */
4427 if (ret != GST_FLOW_NOT_LINKED)
4430 /* if we get here, all other pads were unlinked and we return
4431 * NOT_LINKED then */
4437 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4440 gboolean res = TRUE;
4442 /* only streams that have a connection to the outside world */
4446 if (stream->udpsrc[0]) {
4447 gst_event_ref (event);
4448 res = gst_element_send_event (stream->udpsrc[0], event);
4449 } else if (stream->channelpad[0]) {
4450 gst_event_ref (event);
4451 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4452 res = gst_pad_push_event (stream->channelpad[0], event);
4454 res = gst_pad_send_event (stream->channelpad[0], event);
4457 if (stream->udpsrc[1]) {
4458 gst_event_ref (event);
4459 res &= gst_element_send_event (stream->udpsrc[1], event);
4460 } else if (stream->channelpad[1]) {
4461 gst_event_ref (event);
4462 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4463 res &= gst_pad_push_event (stream->channelpad[1], event);
4465 res &= gst_pad_send_event (stream->channelpad[1], event);
4469 gst_event_unref (event);
4475 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4478 gboolean res = TRUE;
4480 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4481 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4483 gst_event_ref (event);
4484 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4486 gst_event_unref (event);
4491 static GstRTSPResult
4492 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4497 if (info->connection == NULL) {
4498 if (info->url == NULL) {
4499 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4500 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4504 /* create connection */
4505 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4506 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4507 goto could_not_create;
4509 g_free (info->url_str);
4510 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4512 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4514 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4515 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4516 src->tls_validation_flags))
4517 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4519 if (src->tls_database)
4520 gst_rtsp_connection_set_tls_database (info->connection,
4523 if (src->tls_interaction)
4524 gst_rtsp_connection_set_tls_interaction (info->connection,
4525 src->tls_interaction);
4528 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4529 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4531 if (src->proxy_host) {
4532 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4534 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4539 if (!info->connected) {
4542 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4543 ("Connecting to %s", info->location));
4544 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4546 gst_rtsp_connection_connect (info->connection,
4547 src->ptcp_timeout)) < 0)
4548 goto could_not_connect;
4550 info->connected = TRUE;
4557 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4562 gchar *str = gst_rtsp_strresult (res);
4563 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4569 gchar *str = gst_rtsp_strresult (res);
4570 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4576 static GstRTSPResult
4577 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4580 GST_RTSP_STATE_LOCK (src);
4581 if (info->connected) {
4582 GST_DEBUG_OBJECT (src, "closing connection...");
4583 gst_rtsp_connection_close (info->connection);
4584 info->connected = FALSE;
4586 if (free && info->connection) {
4587 /* free connection */
4588 GST_DEBUG_OBJECT (src, "freeing connection...");
4589 gst_rtsp_connection_free (info->connection);
4590 info->connection = NULL;
4592 GST_RTSP_STATE_UNLOCK (src);
4596 static GstRTSPResult
4597 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4602 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4603 gst_rtsp_conninfo_close (src, info, FALSE);
4604 res = gst_rtsp_conninfo_connect (src, info, async);
4610 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4614 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4615 GST_RTSP_STATE_LOCK (src);
4616 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4617 GST_DEBUG_OBJECT (src, "connection flush");
4618 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4619 src->conninfo.flushing = flush;
4621 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4622 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4623 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4624 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4625 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4626 stream->conninfo.flushing = flush;
4629 GST_RTSP_STATE_UNLOCK (src);
4632 static GstRTSPResult
4633 gst_rtspsrc_init_request (GstRTSPSrc * src, GstRTSPMessage * msg,
4634 GstRTSPMethod method, const gchar * uri)
4638 res = gst_rtsp_message_init_request (msg, method, uri);
4642 /* set user-agent */
4643 if (src->user_agent)
4644 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, src->user_agent);
4649 /* FIXME, handle server request, reply with OK, for now */
4650 static GstRTSPResult
4651 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
4652 GstRTSPMessage * request)
4654 GstRTSPMessage response = { 0 };
4657 GST_DEBUG_OBJECT (src, "got server request message");
4660 gst_rtsp_message_dump (request);
4662 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4664 if (res == GST_RTSP_ENOTIMPL) {
4665 /* default implementation, send OK */
4666 GST_DEBUG_OBJECT (src, "prepare OK reply");
4668 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4673 /* let app parse and reply */
4674 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4675 0, request, &response);
4678 gst_rtsp_message_dump (&response);
4680 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
4684 gst_rtsp_message_unset (&response);
4685 } else if (res == GST_RTSP_EEOF)
4693 gst_rtsp_message_unset (&response);
4698 /* send server keep-alive */
4699 static GstRTSPResult
4700 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4702 GstRTSPMessage request = { 0 };
4704 GstRTSPMethod method;
4705 const gchar *control;
4707 if (src->do_rtsp_keep_alive == FALSE) {
4708 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4709 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4713 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4715 /* find a method to use for keep-alive */
4716 if (src->methods & GST_RTSP_GET_PARAMETER)
4717 method = GST_RTSP_GET_PARAMETER;
4719 method = GST_RTSP_OPTIONS;
4721 control = get_aggregate_control (src);
4722 if (control == NULL)
4725 res = gst_rtspsrc_init_request (src, &request, method, control);
4730 gst_rtsp_message_dump (&request);
4733 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
4738 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4739 gst_rtsp_message_unset (&request);
4746 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4751 gchar *str = gst_rtsp_strresult (res);
4753 gst_rtsp_message_unset (&request);
4754 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4755 ("Could not send keep-alive. (%s)", str));
4761 static GstFlowReturn
4762 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4764 GstFlowReturn ret = GST_FLOW_OK;
4766 GstRTSPStream *stream;
4767 GstPad *outpad = NULL;
4773 channel = message->type_data.data.channel;
4775 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4777 goto unknown_stream;
4779 if (channel == stream->channel[0]) {
4780 outpad = stream->channelpad[0];
4782 } else if (channel == stream->channel[1]) {
4783 outpad = stream->channelpad[1];
4789 /* take a look at the body to figure out what we have */
4790 gst_rtsp_message_get_body (message, &data, &size);
4792 goto invalid_length;
4794 /* channels are not correct on some servers, do extra check */
4795 if (data[1] >= 200 && data[1] <= 204) {
4796 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4797 outpad = stream->channelpad[1];
4801 /* we have no clue what this is, just ignore then. */
4803 goto unknown_stream;
4805 /* take the message body for further processing */
4806 gst_rtsp_message_steal_body (message, &data, &size);
4808 /* strip the trailing \0 */
4811 buf = gst_buffer_new ();
4812 gst_buffer_append_memory (buf,
4813 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4815 /* don't need message anymore */
4816 gst_rtsp_message_unset (message);
4818 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4821 if (src->need_activate) {
4827 guint group_id = gst_util_group_id_next ();
4829 /* generate an SHA256 sum of the URI */
4830 cs = g_checksum_new (G_CHECKSUM_SHA256);
4831 uri = src->conninfo.location;
4832 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4834 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4835 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4839 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4840 event = gst_event_new_stream_start (stream_id);
4841 gst_event_set_group_id (event, group_id);
4844 gst_rtspsrc_stream_push_event (src, ostream, event);
4846 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
4847 /* only streams that have a connection to the outside world */
4848 if (ostream->setup) {
4849 if (ostream->udpsrc[0]) {
4850 gst_element_send_event (ostream->udpsrc[0],
4851 gst_event_new_caps (caps));
4852 } else if (ostream->channelpad[0]) {
4853 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
4854 gst_pad_push_event (ostream->channelpad[0],
4855 gst_event_new_caps (caps));
4857 gst_pad_send_event (ostream->channelpad[0],
4858 gst_event_new_caps (caps));
4861 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4863 if (ostream->udpsrc[1]) {
4864 gst_element_send_event (ostream->udpsrc[1],
4865 gst_event_new_caps (caps));
4866 } else if (ostream->channelpad[1]) {
4867 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
4868 gst_pad_push_event (ostream->channelpad[1],
4869 gst_event_new_caps (caps));
4871 gst_pad_send_event (ostream->channelpad[1],
4872 gst_event_new_caps (caps));
4875 gst_caps_unref (caps);
4879 g_checksum_free (cs);
4881 gst_rtspsrc_activate_streams (src);
4882 src->need_activate = FALSE;
4883 src->need_segment = TRUE;
4886 if (src->base_time == -1) {
4887 /* Take current running_time. This timestamp will be put on
4888 * the first buffer of each stream because we are a live source and so we
4889 * timestamp with the running_time. When we are dealing with TCP, we also
4890 * only timestamp the first buffer (using the DISCONT flag) because a server
4891 * typically bursts data, for which we don't want to compensate by speeding
4892 * up the media. The other timestamps will be interpollated from this one
4893 * using the RTP timestamps. */
4894 GST_OBJECT_LOCK (src);
4895 if (GST_ELEMENT_CLOCK (src)) {
4897 GstClockTime base_time;
4899 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4900 base_time = GST_ELEMENT_CAST (src)->base_time;
4902 src->base_time = now - base_time;
4904 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4905 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4907 GST_OBJECT_UNLOCK (src);
4910 /* If needed send a new segment, don't forget we are live and buffer are
4911 * timestamped with running time */
4912 if (src->need_segment) {
4914 src->need_segment = FALSE;
4915 gst_segment_init (&segment, GST_FORMAT_TIME);
4916 gst_rtspsrc_push_event (src, gst_event_new_segment (&segment));
4919 if (stream->discont && !is_rtcp) {
4920 /* mark first RTP buffer as discont */
4921 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4922 stream->discont = FALSE;
4923 /* first buffer gets the timestamp, other buffers are not timestamped and
4924 * their presentation time will be interpollated from the rtp timestamps. */
4925 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4926 GST_TIME_ARGS (src->base_time));
4928 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4931 /* chain to the peer pad */
4932 if (GST_PAD_IS_SINK (outpad))
4933 ret = gst_pad_chain (outpad, buf);
4935 ret = gst_pad_push (outpad, buf);
4938 /* combine all stream flows for the data transport */
4939 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4946 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4947 gst_rtsp_message_unset (message);
4952 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4953 ("Short message received, ignoring."));
4954 gst_rtsp_message_unset (message);
4959 static GstFlowReturn
4960 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4962 GstRTSPMessage message = { 0 };
4964 GstFlowReturn ret = GST_FLOW_OK;
4965 GTimeVal tv_timeout;
4968 /* get the next timeout interval */
4969 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4971 /* see if the timeout period expired */
4972 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4973 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4974 /* send keep-alive, only act on interrupt, a warning will be posted for
4976 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4978 /* get new timeout */
4979 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4982 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4983 tv_timeout.tv_sec, tv_timeout.tv_usec);
4985 /* protect the connection with the connection lock so that we can see when
4986 * we are finished doing server communication */
4988 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4989 &message, src->ptcp_timeout);
4993 GST_DEBUG_OBJECT (src, "we received a server message");
4995 case GST_RTSP_EINTR:
4996 /* we got interrupted this means we need to stop */
4998 case GST_RTSP_ETIMEOUT:
4999 /* no reply, send keep alive */
5000 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
5001 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5005 /* go EOS when the server closed the connection */
5011 switch (message.type) {
5012 case GST_RTSP_MESSAGE_REQUEST:
5013 /* server sends us a request message, handle it */
5015 gst_rtspsrc_handle_request (src, src->conninfo.connection,
5017 if (res == GST_RTSP_EEOF)
5020 goto handle_request_failed;
5022 case GST_RTSP_MESSAGE_RESPONSE:
5023 /* we ignore response messages */
5024 GST_DEBUG_OBJECT (src, "ignoring response message");
5026 gst_rtsp_message_dump (&message);
5028 case GST_RTSP_MESSAGE_DATA:
5029 GST_DEBUG_OBJECT (src, "got data message");
5030 ret = gst_rtspsrc_handle_data (src, &message);
5031 if (ret != GST_FLOW_OK)
5032 goto handle_data_failed;
5035 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5040 g_assert_not_reached ();
5045 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5046 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5047 ("The server closed the connection."));
5048 src->conninfo.connected = FALSE;
5049 gst_rtsp_message_unset (&message);
5050 return GST_FLOW_EOS;
5054 gst_rtsp_message_unset (&message);
5055 GST_DEBUG_OBJECT (src, "got interrupted");
5056 return GST_FLOW_FLUSHING;
5060 gchar *str = gst_rtsp_strresult (res);
5062 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5063 ("Could not receive message. (%s)", str));
5066 gst_rtsp_message_unset (&message);
5067 return GST_FLOW_ERROR;
5069 handle_request_failed:
5071 gchar *str = gst_rtsp_strresult (res);
5073 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5074 ("Could not handle server message. (%s)", str));
5076 gst_rtsp_message_unset (&message);
5077 return GST_FLOW_ERROR;
5081 GST_DEBUG_OBJECT (src, "could no handle data message");
5086 static GstFlowReturn
5087 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
5090 GstRTSPMessage message = { 0 };
5094 GTimeVal tv_timeout;
5096 /* get the next timeout interval */
5097 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
5099 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
5100 (gint) tv_timeout.tv_sec);
5102 gst_rtsp_message_unset (&message);
5104 /* we should continue reading the TCP socket because the server might
5105 * send us requests. When the session timeout expires, we need to send a
5106 * keep-alive request to keep the session open. */
5107 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
5108 &message, &tv_timeout);
5112 GST_DEBUG_OBJECT (src, "we received a server message");
5114 case GST_RTSP_EINTR:
5115 /* we got interrupted, see what we have to do */
5117 case GST_RTSP_ETIMEOUT:
5118 /* send keep-alive, ignore the result, a warning will be posted. */
5119 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
5120 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5124 /* server closed the connection. not very fatal for UDP, reconnect and
5125 * see what happens. */
5126 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5127 ("The server closed the connection."));
5128 if (src->udp_reconnect) {
5130 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
5137 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
5139 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5140 ("Unhandled return value %d.", res));
5144 switch (message.type) {
5145 case GST_RTSP_MESSAGE_REQUEST:
5146 /* server sends us a request message, handle it */
5148 gst_rtspsrc_handle_request (src, src->conninfo.connection,
5150 if (res == GST_RTSP_EEOF)
5153 goto handle_request_failed;
5155 case GST_RTSP_MESSAGE_RESPONSE:
5156 /* we ignore response and data messages */
5157 GST_DEBUG_OBJECT (src, "ignoring response message");
5159 gst_rtsp_message_dump (&message);
5160 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
5161 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
5162 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
5163 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
5164 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5171 case GST_RTSP_MESSAGE_DATA:
5172 /* we ignore response and data messages */
5173 GST_DEBUG_OBJECT (src, "ignoring data message");
5176 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5181 g_assert_not_reached ();
5183 /* we get here when the connection got interrupted */
5186 gst_rtsp_message_unset (&message);
5187 GST_DEBUG_OBJECT (src, "got interrupted");
5188 return GST_FLOW_FLUSHING;
5192 gchar *str = gst_rtsp_strresult (res);
5195 src->conninfo.connected = FALSE;
5196 if (res != GST_RTSP_EINTR) {
5197 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
5198 ("Could not connect to server. (%s)", str));
5200 ret = GST_FLOW_ERROR;
5202 ret = GST_FLOW_FLUSHING;
5208 gchar *str = gst_rtsp_strresult (res);
5210 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5211 ("Could not receive message. (%s)", str));
5213 return GST_FLOW_ERROR;
5215 handle_request_failed:
5217 gchar *str = gst_rtsp_strresult (res);
5220 gst_rtsp_message_unset (&message);
5221 if (res != GST_RTSP_EINTR) {
5222 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5223 ("Could not handle server message. (%s)", str));
5225 ret = GST_FLOW_ERROR;
5227 ret = GST_FLOW_FLUSHING;
5233 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5234 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5235 ("The server closed the connection."));
5236 src->conninfo.connected = FALSE;
5237 gst_rtsp_message_unset (&message);
5238 return GST_FLOW_EOS;
5242 static GstRTSPResult
5243 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
5245 GstRTSPResult res = GST_RTSP_OK;
5248 GST_DEBUG_OBJECT (src, "doing reconnect");
5250 GST_OBJECT_LOCK (src);
5251 /* only restart when the pads were not yet activated, else we were
5252 * streaming over UDP */
5253 restart = src->need_activate;
5254 GST_OBJECT_UNLOCK (src);
5256 /* no need to restart, we're done */
5260 /* we can try only TCP now */
5261 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
5263 /* close and cleanup our state */
5264 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
5267 /* see if we have TCP left to try. Also don't try TCP when we were configured
5269 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
5272 /* We post a warning message now to inform the user
5273 * that nothing happened. It's most likely a firewall thing. */
5274 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5275 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5276 "firewall is blocking it. Retrying using a TCP connection.",
5277 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
5279 /* open new connection using tcp */
5280 if (gst_rtspsrc_open (src, async) < 0)
5283 /* start playback */
5284 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
5293 src->cur_protocols = 0;
5294 /* no transport possible, post an error and stop */
5295 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5296 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5297 "firewall is blocking it. No other protocols to try.",
5298 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
5299 return GST_RTSP_ERROR;
5303 GST_DEBUG_OBJECT (src, "open failed");
5308 GST_DEBUG_OBJECT (src, "play failed");
5314 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
5318 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
5321 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
5324 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
5327 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
5335 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
5339 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
5342 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
5345 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
5348 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
5356 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
5360 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
5363 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
5366 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
5369 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
5377 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
5381 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
5384 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
5387 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
5390 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
5398 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
5400 if (ret == GST_RTSP_OK)
5401 gst_rtspsrc_loop_complete_cmd (src, cmd);
5402 else if (ret == GST_RTSP_EINTR)
5403 gst_rtspsrc_loop_cancel_cmd (src, cmd);
5405 gst_rtspsrc_loop_error_cmd (src, cmd);
5409 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
5412 gboolean flushed = FALSE;
5414 /* start new request */
5415 gst_rtspsrc_loop_start_cmd (src, cmd);
5417 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
5419 GST_OBJECT_LOCK (src);
5420 old = src->pending_cmd;
5421 if (old == CMD_RECONNECT) {
5422 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
5423 cmd = CMD_RECONNECT;
5425 if (old != CMD_WAIT) {
5426 src->pending_cmd = CMD_WAIT;
5427 GST_OBJECT_UNLOCK (src);
5428 /* cancel previous request */
5429 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
5430 gst_rtspsrc_loop_cancel_cmd (src, old);
5431 GST_OBJECT_LOCK (src);
5433 src->pending_cmd = cmd;
5434 /* interrupt if allowed */
5435 if (src->busy_cmd & mask) {
5436 GST_DEBUG_OBJECT (src, "connection flush busy %s",
5437 cmd_to_string (src->busy_cmd));
5438 gst_rtspsrc_connection_flush (src, TRUE);
5441 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
5442 cmd_to_string (src->busy_cmd));
5445 gst_task_start (src->task);
5446 GST_OBJECT_UNLOCK (src);
5452 gst_rtspsrc_loop (GstRTSPSrc * src)
5456 if (!src->conninfo.connection || !src->conninfo.connected)
5459 if (src->interleaved)
5460 ret = gst_rtspsrc_loop_interleaved (src);
5462 ret = gst_rtspsrc_loop_udp (src);
5464 if (ret != GST_FLOW_OK)
5472 GST_WARNING_OBJECT (src, "we are not connected");
5473 ret = GST_FLOW_FLUSHING;
5478 const gchar *reason = gst_flow_get_name (ret);
5480 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
5481 src->running = FALSE;
5482 if (ret == GST_FLOW_EOS) {
5483 /* perform EOS logic */
5484 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5485 gst_element_post_message (GST_ELEMENT_CAST (src),
5486 gst_message_new_segment_done (GST_OBJECT_CAST (src),
5487 src->segment.format, src->segment.position));
5488 gst_rtspsrc_push_event (src,
5489 gst_event_new_segment_done (src->segment.format,
5490 src->segment.position));
5492 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5494 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
5495 /* for fatal errors we post an error message, post the error before the
5496 * EOS so the app knows about the error first. */
5497 GST_ELEMENT_ERROR (src, STREAM, FAILED,
5498 ("Internal data flow error."),
5499 ("streaming task paused, reason %s (%d)", reason, ret));
5500 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5502 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
5507 #ifndef GST_DISABLE_GST_DEBUG
5508 static const gchar *
5509 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
5513 while (method != 0) {
5530 static const gchar *
5531 gst_rtspsrc_skip_lws (const gchar * s)
5533 while (g_ascii_isspace (*s))
5538 static const gchar *
5539 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
5541 while (s > start && g_ascii_isspace (*(s - 1)))
5546 static const gchar *
5547 gst_rtspsrc_skip_commas (const gchar * s)
5549 /* The grammar allows for multiple commas */
5550 while (g_ascii_isspace (*s) || *s == ',')
5555 static const gchar *
5556 gst_rtspsrc_skip_item (const gchar * s)
5558 gboolean quoted = FALSE;
5559 const gchar *start = s;
5561 /* A list item ends at the last non-whitespace character
5562 * before a comma which is not inside a quoted-string. Or at
5563 * the end of the string.
5569 if (*s == '\\' && *(s + 1))
5578 return gst_rtspsrc_unskip_lws (s, start);
5582 gst_rtsp_decode_quoted_string (gchar * quoted_string)
5586 src = quoted_string + 1;
5587 dst = quoted_string;
5588 while (*src && *src != '"') {
5589 if (*src == '\\' && *(src + 1))
5596 /* Extract the authentication tokens that the server provided for each method
5597 * into an array of structures and give those to the connection object.
5600 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
5601 const gchar * header, gboolean * stale)
5603 GSList *list = NULL, *iter;
5605 gchar *item, *eq, *name_end, *value;
5607 g_return_if_fail (stale != NULL);
5609 gst_rtsp_connection_clear_auth_params (conn);
5612 /* Parse a header whose content is described by RFC2616 as
5613 * "#something", where "something" does not itself contain commas,
5614 * except as part of quoted-strings, into a list of allocated strings.
5616 header = gst_rtspsrc_skip_commas (header);
5618 end = gst_rtspsrc_skip_item (header);
5619 list = g_slist_prepend (list, g_strndup (header, end - header));
5620 header = gst_rtspsrc_skip_commas (end);
5625 list = g_slist_reverse (list);
5626 for (iter = list; iter; iter = iter->next) {
5629 eq = strchr (item, '=');
5631 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
5632 if (name_end == item) {
5633 /* That's no good... */
5640 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
5642 gst_rtsp_decode_quoted_string (value);
5646 if (value && strcmp (item, "stale") == 0 && strcmp (value, "TRUE") == 0)
5648 gst_rtsp_connection_set_auth_param (conn, item, value);
5652 g_slist_free (list);
5655 /* Parse a WWW-Authenticate Response header and determine the
5656 * available authentication methods
5658 * This code should also cope with the fact that each WWW-Authenticate
5659 * header can contain multiple challenge methods + tokens
5661 * At the moment, for Basic auth, we just do a minimal check and don't
5662 * even parse out the realm */
5664 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
5665 GstRTSPConnection * conn, gboolean * stale)
5669 g_return_if_fail (hdr != NULL);
5670 g_return_if_fail (methods != NULL);
5671 g_return_if_fail (stale != NULL);
5673 /* Skip whitespace at the start of the string */
5674 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
5676 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
5677 *methods |= GST_RTSP_AUTH_BASIC;
5678 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
5679 *methods |= GST_RTSP_AUTH_DIGEST;
5680 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
5685 * gst_rtspsrc_setup_auth:
5686 * @src: the rtsp source
5688 * Configure a username and password and auth method on the
5689 * connection object based on a response we received from the
5692 * Currently, this requires that a username and password were supplied
5693 * in the uri. In the future, they may be requested on demand by sending
5694 * a message up the bus.
5696 * Returns: TRUE if authentication information could be set up correctly.
5699 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5703 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5704 GstRTSPAuthMethod method;
5705 GstRTSPResult auth_result;
5707 GstRTSPConnection *conn;
5709 gboolean stale = FALSE;
5711 conn = src->conninfo.connection;
5713 /* Identify the available auth methods and see if any are supported */
5714 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
5715 &hdr, 0) == GST_RTSP_OK) {
5716 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
5719 if (avail_methods == GST_RTSP_AUTH_NONE)
5720 goto no_auth_available;
5722 /* For digest auth, if the response indicates that the session
5723 * data are stale, we just update them in the connection object and
5724 * return TRUE to retry the request */
5726 src->tried_url_auth = FALSE;
5728 url = gst_rtsp_connection_get_url (conn);
5730 /* Do we have username and password available? */
5731 if (url != NULL && !src->tried_url_auth && url->user != NULL
5732 && url->passwd != NULL) {
5735 src->tried_url_auth = TRUE;
5736 GST_DEBUG_OBJECT (src,
5737 "Attempting authentication using credentials from the URL");
5739 user = src->user_id;
5740 pass = src->user_pw;
5741 GST_DEBUG_OBJECT (src,
5742 "Attempting authentication using credentials from the properties");
5745 /* FIXME: If the url didn't contain username and password or we tried them
5746 * already, request a username and passwd from the application via some kind
5747 * of credentials request message */
5749 /* If we don't have a username and passwd at this point, bail out. */
5750 if (user == NULL || pass == NULL)
5753 /* Try to configure for each available authentication method, strongest to
5755 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5756 /* Check if this method is available on the server */
5757 if ((method & avail_methods) == 0)
5760 /* Pass the credentials to the connection to try on the next request */
5761 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5762 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5763 * ignore it and end up retrying later */
5764 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5765 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5766 gst_rtsp_auth_method_to_string (method));
5771 if (method == GST_RTSP_AUTH_NONE)
5772 goto no_auth_available;
5778 /* Output an error indicating that we couldn't connect because there were
5779 * no supported authentication protocols */
5780 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5781 ("No supported authentication protocol was found"));
5786 /* We don't fire an error message, we just return FALSE and let the
5787 * normal NOT_AUTHORIZED error be propagated */
5792 static GstRTSPResult
5793 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5794 GstRTSPMessage * request, GstRTSPMessage * response,
5795 GstRTSPStatusCode * code)
5798 GstRTSPStatusCode thecode;
5799 gchar *content_base = NULL;
5803 if (!src->short_header)
5804 gst_rtsp_ext_list_before_send (src->extensions, request);
5806 GST_DEBUG_OBJECT (src, "sending message");
5809 gst_rtsp_message_dump (request);
5811 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
5815 gst_rtsp_connection_reset_timeout (conn);
5818 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
5823 gst_rtsp_message_dump (response);
5825 switch (response->type) {
5826 case GST_RTSP_MESSAGE_REQUEST:
5827 res = gst_rtspsrc_handle_request (src, conn, response);
5828 if (res == GST_RTSP_EEOF)
5831 goto handle_request_failed;
5833 case GST_RTSP_MESSAGE_RESPONSE:
5834 /* ok, a response is good */
5835 GST_DEBUG_OBJECT (src, "received response message");
5837 case GST_RTSP_MESSAGE_DATA:
5838 /* get next response */
5839 GST_DEBUG_OBJECT (src, "handle data response message");
5840 gst_rtspsrc_handle_data (src, response);
5843 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5848 thecode = response->type_data.response.code;
5850 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5852 /* if the caller wanted the result code, we store it. */
5856 /* If the request didn't succeed, bail out before doing any more */
5857 if (thecode != GST_RTSP_STS_OK)
5860 /* store new content base if any */
5861 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5864 g_free (src->content_base);
5865 src->content_base = g_strdup (content_base);
5867 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5874 gchar *str = gst_rtsp_strresult (res);
5876 if (res != GST_RTSP_EINTR) {
5877 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5878 ("Could not send message. (%s)", str));
5880 GST_WARNING_OBJECT (src, "send interrupted");
5889 GST_WARNING_OBJECT (src, "server closed connection");
5890 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5892 /* if reconnect succeeds, try again */
5894 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5898 /* only try once after reconnect, then fallthrough and error out */
5901 gchar *str = gst_rtsp_strresult (res);
5903 if (res != GST_RTSP_EINTR) {
5904 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5905 ("Could not receive message. (%s)", str));
5907 GST_WARNING_OBJECT (src, "receive interrupted");
5915 handle_request_failed:
5917 /* ERROR was posted */
5918 gst_rtsp_message_unset (response);
5923 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5924 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5925 ("The server closed the connection."));
5926 gst_rtsp_message_unset (response);
5933 * @src: the rtsp source
5934 * @conn: the connection to send on
5935 * @request: must point to a valid request
5936 * @response: must point to an empty #GstRTSPMessage
5937 * @code: an optional code result
5939 * send @request and retrieve the response in @response. optionally @code can be
5940 * non-NULL in which case it will contain the status code of the response.
5942 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5943 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5945 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5946 * @response message) if the response code was not 200 (OK).
5948 * If the attempt results in an authentication failure, then this will attempt
5949 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5952 * Returns: #GST_RTSP_OK if the processing was successful.
5954 static GstRTSPResult
5955 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5956 GstRTSPMessage * request, GstRTSPMessage * response,
5957 GstRTSPStatusCode * code)
5959 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5960 GstRTSPResult res = GST_RTSP_ERROR;
5963 GstRTSPMethod method = GST_RTSP_INVALID;
5969 /* make sure we don't loop forever */
5973 /* save method so we can disable it when the server complains */
5974 method = request->type_data.request.method;
5977 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5981 case GST_RTSP_STS_UNAUTHORIZED:
5982 case GST_RTSP_STS_NOT_FOUND:
5983 if (gst_rtspsrc_setup_auth (src, response)) {
5984 /* Try the request/response again after configuring the auth info
5992 } while (retry == TRUE);
5994 /* If the user requested the code, let them handle errors, otherwise
5995 * post an error below */
5998 else if (int_code != GST_RTSP_STS_OK)
5999 goto error_response;
6006 GST_DEBUG_OBJECT (src, "got error %d", res);
6011 res = GST_RTSP_ERROR;
6013 switch (response->type_data.response.code) {
6014 case GST_RTSP_STS_NOT_FOUND:
6015 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
6016 response->type_data.response.reason));
6018 case GST_RTSP_STS_UNAUTHORIZED:
6019 GST_ELEMENT_ERROR (src, RESOURCE, NOT_AUTHORIZED, (NULL), ("%s",
6020 response->type_data.response.reason));
6022 case GST_RTSP_STS_MOVED_PERMANENTLY:
6023 case GST_RTSP_STS_MOVE_TEMPORARILY:
6025 gchar *new_location;
6026 GstRTSPLowerTrans transports;
6028 GST_DEBUG_OBJECT (src, "got redirection");
6029 /* if we don't have a Location Header, we must error */
6030 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
6031 &new_location, 0) < 0)
6034 /* When we receive a redirect result, we go back to the INIT state after
6035 * parsing the new URI. The caller should do the needed steps to issue
6036 * a new setup when it detects this state change. */
6037 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
6039 /* save current transports */
6040 if (src->conninfo.url)
6041 transports = src->conninfo.url->transports;
6043 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
6045 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
6047 /* set old transports */
6048 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
6049 src->conninfo.url->transports = transports;
6051 src->need_redirect = TRUE;
6052 src->state = GST_RTSP_STATE_INIT;
6056 case GST_RTSP_STS_NOT_ACCEPTABLE:
6057 case GST_RTSP_STS_NOT_IMPLEMENTED:
6058 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
6059 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
6060 gst_rtsp_method_as_text (method));
6061 src->methods &= ~method;
6065 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6066 ("Got error response: %d (%s).", response->type_data.response.code,
6067 response->type_data.response.reason));
6070 /* if we return ERROR we should unset the response ourselves */
6071 if (res == GST_RTSP_ERROR)
6072 gst_rtsp_message_unset (response);
6078 static GstRTSPResult
6079 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
6080 GstRTSPMessage * response, GstRTSPSrc * src)
6082 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
6087 /* parse the response and collect all the supported methods. We need this
6088 * information so that we don't try to send an unsupported request to the
6092 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
6094 GstRTSPHeaderField field;
6098 /* reset supported methods */
6101 /* Try Allow Header first */
6102 field = GST_RTSP_HDR_ALLOW;
6105 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6106 if (indx == 0 && !respoptions) {
6107 /* if no Allow header was found then try the Public header... */
6108 field = GST_RTSP_HDR_PUBLIC;
6109 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6114 src->methods |= gst_rtsp_options_from_text (respoptions);
6119 if (src->methods == 0) {
6120 /* neither Allow nor Public are required, assume the server supports
6121 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
6123 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
6124 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
6126 /* always assume PLAY, FIXME, extensions should be able to override
6128 src->methods |= GST_RTSP_PLAY;
6129 /* also assume it will support Range */
6130 src->seekable = TRUE;
6132 /* we need describe and setup */
6133 if (!(src->methods & GST_RTSP_DESCRIBE))
6135 if (!(src->methods & GST_RTSP_SETUP))
6143 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6144 ("Server does not support DESCRIBE."));
6149 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6150 ("Server does not support SETUP."));
6155 /* masks to be kept in sync with the hardcoded protocol order of preference
6157 static const guint protocol_masks[] = {
6158 GST_RTSP_LOWER_TRANS_UDP,
6159 GST_RTSP_LOWER_TRANS_UDP_MCAST,
6160 GST_RTSP_LOWER_TRANS_TCP,
6164 static GstRTSPResult
6165 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
6166 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
6170 gboolean add_udp_str;
6175 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
6180 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
6182 /* extension listed transports, use those */
6183 if (*transports != NULL)
6186 /* it's the default */
6187 add_udp_str = FALSE;
6189 /* the default RTSP transports */
6190 result = g_string_new ("RTP");
6193 case GST_RTSP_PROFILE_AVP:
6194 g_string_append (result, "/AVP");
6196 case GST_RTSP_PROFILE_SAVP:
6197 g_string_append (result, "/SAVP");
6199 case GST_RTSP_PROFILE_AVPF:
6200 g_string_append (result, "/AVPF");
6202 case GST_RTSP_PROFILE_SAVPF:
6203 g_string_append (result, "/SAVPF");
6209 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
6210 GST_DEBUG_OBJECT (src, "adding UDP unicast");
6212 g_string_append (result, "/UDP");
6213 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
6214 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
6215 GST_DEBUG_OBJECT (src, "adding UDP multicast");
6216 /* we don't have to allocate any UDP ports yet, if the selected transport
6217 * turns out to be multicast we can create them and join the multicast
6218 * group indicated in the transport reply */
6220 g_string_append (result, "/UDP");
6221 g_string_append (result, ";multicast");
6222 if (src->next_port_num != 0) {
6223 if (src->client_port_range.max > 0 &&
6224 src->next_port_num >= src->client_port_range.max)
6227 g_string_append_printf (result, ";client_port=%d-%d",
6228 src->next_port_num, src->next_port_num + 1);
6230 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
6231 GST_DEBUG_OBJECT (src, "adding TCP");
6233 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
6235 *transports = g_string_free (result, FALSE);
6237 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
6244 GST_ERROR ("extension gave error %d", res);
6249 GST_ERROR ("no more ports available");
6250 return GST_RTSP_ERROR;
6254 static GstRTSPResult
6255 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
6256 gint orig_rtpport, gint orig_rtcpport)
6259 gint nr_udp, nr_int;
6261 gint rtpport = 0, rtcpport = 0;
6264 src = stream->parent;
6266 /* find number of placeholders first */
6267 if (strstr (*transports, "%%i2"))
6269 else if (strstr (*transports, "%%i1"))
6274 if (strstr (*transports, "%%u2"))
6276 else if (strstr (*transports, "%%u1"))
6281 if (nr_udp == 0 && nr_int == 0)
6285 if (!orig_rtpport || !orig_rtcpport) {
6286 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
6289 rtpport = orig_rtpport;
6290 rtcpport = orig_rtcpport;
6294 str = g_string_new ("");
6296 while ((next = strstr (p, "%%"))) {
6297 g_string_append_len (str, p, next - p);
6298 if (next[2] == 'u') {
6300 g_string_append_printf (str, "%d", rtpport);
6301 else if (next[3] == '2')
6302 g_string_append_printf (str, "%d", rtcpport);
6304 if (next[2] == 'i') {
6306 g_string_append_printf (str, "%d", src->free_channel);
6307 else if (next[3] == '2')
6308 g_string_append_printf (str, "%d", src->free_channel + 1);
6313 /* append final part */
6314 g_string_append (str, p);
6316 g_free (*transports);
6317 *transports = g_string_free (str, FALSE);
6325 GST_ERROR ("failed to allocate udp ports");
6326 return GST_RTSP_ERROR;
6331 enc_key_length_from_cipher_name (const gchar * cipher)
6333 if (g_strcmp0 (cipher, "aes-128-icm") == 0)
6334 return AES_128_KEY_LEN;
6335 else if (g_strcmp0 (cipher, "aes-256-icm") == 0)
6336 return AES_256_KEY_LEN;
6338 GST_ERROR ("encryption algorithm '%s' not supported", cipher);
6344 auth_key_length_from_auth_name (const gchar * auth)
6346 if (g_strcmp0 (auth, "hmac-sha1-32") == 0)
6347 return HMAC_32_KEY_LEN;
6348 else if (g_strcmp0 (auth, "hmac-sha1-80") == 0)
6349 return HMAC_80_KEY_LEN;
6351 GST_ERROR ("authentication algorithm '%s' not supported", auth);
6357 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
6359 GstCaps *caps = NULL;
6361 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
6365 GST_DEBUG_OBJECT (src, "SRTP parameters received");
6371 default_srtcp_params (void)
6378 guint data_size = GST_ROUND_UP_4 (KEY_SIZE);
6380 /* create a random key */
6381 key_data = g_malloc (data_size);
6382 for (i = 0; i < data_size; i += 4)
6383 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
6385 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
6387 caps = gst_caps_new_simple ("application/x-srtp",
6388 "srtp-key", GST_TYPE_BUFFER, buf,
6389 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
6390 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
6392 gst_buffer_unref (buf);
6398 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
6401 gchar *result, *base64;
6404 GstMIKEYMessage *msg;
6405 GstMIKEYPayload *payload, *pkd;
6411 const gchar *srtcpcipher, *srtcpauth;
6413 stream->srtcpparams = signal_get_srtcp_params (src, stream);
6414 if (stream->srtcpparams == NULL)
6415 stream->srtcpparams = default_srtcp_params ();
6417 s = gst_caps_get_structure (stream->srtcpparams, 0);
6419 srtcpcipher = gst_structure_get_string (s, "srtcp-cipher");
6420 srtcpauth = gst_structure_get_string (s, "srtcp-auth");
6421 val = gst_structure_get_value (s, "srtp-key");
6423 if (srtcpcipher == NULL || srtcpauth == NULL || val == NULL) {
6424 GST_ERROR_OBJECT (src, "could not find the right SRTP parameters in caps");
6428 srtpkey = gst_value_get_buffer (val);
6430 msg = gst_mikey_message_new ();
6431 /* unencrypted MIKEY message, we send this over TLS so this is allowed */
6432 gst_mikey_message_set_info (msg, GST_MIKEY_VERSION, GST_MIKEY_TYPE_PSK_INIT,
6433 FALSE, GST_MIKEY_PRF_MIKEY_1, g_random_int (), GST_MIKEY_MAP_TYPE_SRTP);
6434 /* add policy '0' for our SSRC */
6435 gst_mikey_message_add_cs_srtp (msg, 0, stream->send_ssrc, 0);
6436 /* timestamp is now */
6437 gst_mikey_message_add_t_now_ntp_utc (msg);
6438 /* add some random data */
6439 gst_mikey_message_add_rand_len (msg, 16);
6441 /* the policy '0' is SRTP */
6442 payload = gst_mikey_payload_new (GST_MIKEY_PT_SP);
6443 gst_mikey_payload_sp_set (payload, 0, GST_MIKEY_SEC_PROTO_SRTP);
6445 /* only AES-CM is supported */
6447 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_ALG, 1, &byte);
6448 /* encryption key length */
6449 byte = enc_key_length_from_cipher_name (srtcpcipher);
6450 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_KEY_LEN, 1,
6452 /* only HMAC-SHA1 */
6453 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_ALG, 1,
6455 /* authentication key length */
6456 byte = auth_key_length_from_auth_name (srtcpauth);
6457 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_KEY_LEN, 1,
6459 /* we enable encryption on RTP and RTCP */
6460 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_ENC, 1,
6462 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTCP_ENC, 1,
6464 /* we enable authentication on RTP and RTCP */
6465 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_AUTH, 1,
6467 gst_mikey_message_add_payload (msg, payload);
6469 /* make unencrypted KEMAC */
6470 payload = gst_mikey_payload_new (GST_MIKEY_PT_KEMAC);
6471 gst_mikey_payload_kemac_set (payload, GST_MIKEY_ENC_NULL, GST_MIKEY_MAC_NULL);
6472 /* add the key in KEMAC */
6473 pkd = gst_mikey_payload_new (GST_MIKEY_PT_KEY_DATA);
6474 gst_buffer_map (srtpkey, &info, GST_MAP_READ);
6475 gst_mikey_payload_key_data_set_key (pkd, GST_MIKEY_KD_TEK, info.size,
6477 gst_buffer_unmap (srtpkey, &info);
6478 gst_mikey_payload_kemac_add_sub (payload, pkd);
6479 gst_mikey_message_add_payload (msg, payload);
6481 /* now serialize this to bytes */
6482 bytes = gst_mikey_message_to_bytes (msg, NULL, NULL);
6483 gst_mikey_message_unref (msg);
6484 /* and make it into base64 */
6485 data = g_bytes_get_data (bytes, &size);
6486 base64 = g_base64_encode (data, size);
6487 g_bytes_unref (bytes);
6489 result = g_strdup_printf ("prot=mikey;uri=\"%s\";data=\"%s\"",
6490 stream->conninfo.location, base64);
6497 /* Perform the SETUP request for all the streams.
6499 * We ask the server for a specific transport, which initially includes all the
6500 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
6501 * two local UDP ports that we send to the server.
6503 * Once the server replied with a transport, we configure the other streams
6504 * with the same transport.
6506 * This function will also configure the stream for the selected transport,
6507 * which basically means creating the pipeline.
6509 static GstRTSPResult
6510 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
6513 GstRTSPResult res = GST_RTSP_ERROR;
6514 GstRTSPMessage request = { 0 };
6515 GstRTSPMessage response = { 0 };
6516 GstRTSPStream *stream = NULL;
6517 GstRTSPLowerTrans protocols;
6518 GstRTSPStatusCode code;
6519 gboolean unsupported_real = FALSE;
6520 gint rtpport, rtcpport;
6524 if (src->conninfo.connection) {
6525 url = gst_rtsp_connection_get_url (src->conninfo.connection);
6526 /* we initially allow all configured lower transports. based on the URL
6527 * transports and the replies from the server we narrow them down. */
6528 protocols = url->transports & src->cur_protocols;
6531 protocols = src->cur_protocols;
6537 /* reset some state */
6538 src->free_channel = 0;
6539 src->interleaved = FALSE;
6540 src->need_activate = FALSE;
6541 /* keep track of next port number, 0 is random */
6542 src->next_port_num = src->client_port_range.min;
6543 rtpport = rtcpport = 0;
6545 if (G_UNLIKELY (src->streams == NULL))
6548 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6549 GstRTSPConnection *conn;
6556 stream = (GstRTSPStream *) walk->data;
6558 caps = stream_get_caps_for_pt (stream, stream->default_pt);
6560 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
6564 if (stream->skipped) {
6565 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
6569 /* see if we need to configure this stream */
6570 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
6571 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
6576 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
6577 stream->id, caps, &selected);
6579 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
6583 /* merge/overwrite global caps */
6588 s = gst_caps_get_structure (caps, 0);
6590 num = gst_structure_n_fields (src->props);
6591 for (j = 0; j < num; j++) {
6595 name = gst_structure_nth_field_name (src->props, j);
6596 val = gst_structure_get_value (src->props, name);
6597 gst_structure_set_value (s, name, val);
6599 GST_DEBUG_OBJECT (src, "copied %s", name);
6603 /* skip setup if we have no URL for it */
6604 if (stream->conninfo.location == NULL) {
6605 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
6609 if (src->conninfo.connection == NULL) {
6610 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
6611 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
6614 conn = stream->conninfo.connection;
6616 conn = src->conninfo.connection;
6618 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
6619 stream->conninfo.location);
6621 /* if we have a multicast connection, only suggest multicast from now on */
6622 if (stream->is_multicast)
6623 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
6626 /* first selectable protocol */
6627 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6629 if (!protocol_masks[mask])
6633 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
6634 protocol_masks[mask]);
6635 /* create a string with first transport in line */
6637 res = gst_rtspsrc_create_transports_string (src,
6638 protocols & protocol_masks[mask], stream->profile, &transports);
6639 if (res < 0 || transports == NULL)
6640 goto setup_transport_failed;
6642 if (strlen (transports) == 0) {
6643 g_free (transports);
6644 GST_DEBUG_OBJECT (src, "no transports found");
6649 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
6651 /* replace placeholders with real values, this function will optionally
6652 * allocate UDP ports and other info needed to execute the setup request */
6653 res = gst_rtspsrc_prepare_transports (stream, &transports,
6654 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
6656 g_free (transports);
6657 goto setup_transport_failed;
6660 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
6662 /* create SETUP request */
6664 gst_rtspsrc_init_request (src, &request, GST_RTSP_SETUP,
6665 stream->conninfo.location);
6667 g_free (transports);
6668 goto create_request_failed;
6671 /* select transport */
6672 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
6675 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
6676 stream->profile == GST_RTSP_PROFILE_SAVPF) {
6677 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
6678 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
6681 /* if the user wants a non default RTP packet size we add the blocksize
6683 if (src->rtp_blocksize > 0) {
6684 hval = g_strdup_printf ("%d", src->rtp_blocksize);
6685 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
6689 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
6692 /* handle the code ourselves */
6693 res = gst_rtspsrc_send (src, conn, &request, &response, &code);
6698 case GST_RTSP_STS_OK:
6700 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
6701 gst_rtsp_message_unset (&request);
6702 gst_rtsp_message_unset (&response);
6703 /* cleanup of leftover transport */
6704 gst_rtspsrc_stream_free_udp (stream);
6705 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
6706 * we might be in this case */
6707 if (stream->container && rtpport && rtcpport && !retry) {
6708 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
6713 /* this transport did not go down well, but we may have others to try
6714 * that we did not send yet, try those and only give up then
6715 * but not without checking for lost cause/extension so we can
6716 * post a nicer/more useful error message later */
6717 if (!unsupported_real)
6718 unsupported_real = stream->is_real;
6719 /* select next available protocol, give up on this stream if none */
6721 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6723 if (!protocol_masks[mask] || unsupported_real)
6728 /* cleanup of leftover transport and move to the next stream */
6729 gst_rtspsrc_stream_free_udp (stream);
6730 goto response_error;
6733 /* parse response transport */
6735 gchar *resptrans = NULL;
6736 GstRTSPTransport transport = { 0 };
6738 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
6741 gst_rtspsrc_stream_free_udp (stream);
6745 /* parse transport, go to next stream on parse error */
6746 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6747 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6751 /* update allowed transports for other streams. once the transport of
6752 * one stream has been determined, we make sure that all other streams
6753 * are configured in the same way */
6754 switch (transport.lower_transport) {
6755 case GST_RTSP_LOWER_TRANS_TCP:
6756 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6757 protocols = GST_RTSP_LOWER_TRANS_TCP;
6758 src->interleaved = TRUE;
6759 /* update free channels */
6761 MAX (transport.interleaved.min, src->free_channel);
6763 MAX (transport.interleaved.max, src->free_channel);
6764 src->free_channel++;
6766 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6767 /* only allow multicast for other streams */
6768 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6769 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6770 /* if the server selected our ports, increment our counters so that
6771 * we select a new port later */
6772 if (src->next_port_num == transport.port.min &&
6773 src->next_port_num + 1 == transport.port.max) {
6774 src->next_port_num += 2;
6777 case GST_RTSP_LOWER_TRANS_UDP:
6778 /* only allow unicast for other streams */
6779 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6780 protocols = GST_RTSP_LOWER_TRANS_UDP;
6783 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6784 transport.lower_transport);
6788 if (!src->interleaved || !retry) {
6789 /* now configure the stream with the selected transport */
6790 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6791 GST_DEBUG_OBJECT (src,
6792 "could not configure stream %p transport, skipping stream",
6795 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
6796 /* retain the first allocated UDP port pair */
6797 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
6798 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
6801 /* we need to activate at least one streams when we detect activity */
6802 src->need_activate = TRUE;
6804 /* stream is setup now */
6805 stream->setup = TRUE;
6810 GstRTSPStream *sskip;
6812 skip = g_list_next (skip);
6816 sskip = (GstRTSPStream *) skip->data;
6818 /* skip all streams with the same control url */
6819 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
6820 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
6821 sskip, sskip->conninfo.location);
6822 sskip->skipped = TRUE;
6827 /* clean up our transport struct */
6828 gst_rtsp_transport_init (&transport);
6829 /* clean up used RTSP messages */
6830 gst_rtsp_message_unset (&request);
6831 gst_rtsp_message_unset (&response);
6835 /* store the transport protocol that was configured */
6836 src->cur_protocols = protocols;
6838 gst_rtsp_ext_list_stream_select (src->extensions, url);
6840 /* if there is nothing to activate, error out */
6841 if (!src->need_activate)
6842 goto nothing_to_activate;
6849 /* no transport possible, post an error and stop */
6850 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6851 ("Could not connect to server, no protocols left"));
6852 return GST_RTSP_ERROR;
6856 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6857 ("SDP contains no streams"));
6858 return GST_RTSP_ERROR;
6860 create_request_failed:
6862 gchar *str = gst_rtsp_strresult (res);
6864 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6865 ("Could not create request. (%s)", str));
6869 setup_transport_failed:
6871 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6872 ("Could not setup transport."));
6873 res = GST_RTSP_ERROR;
6878 const gchar *str = gst_rtsp_status_as_text (code);
6880 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6881 ("Error (%d): %s", code, GST_STR_NULL (str)));
6882 res = GST_RTSP_ERROR;
6887 gchar *str = gst_rtsp_strresult (res);
6889 if (res != GST_RTSP_EINTR) {
6890 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6891 ("Could not send message. (%s)", str));
6893 GST_WARNING_OBJECT (src, "send interrupted");
6900 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6901 ("Server did not select transport."));
6902 res = GST_RTSP_ERROR;
6905 nothing_to_activate:
6907 /* none of the available error codes is really right .. */
6908 if (unsupported_real) {
6909 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6910 (_("No supported stream was found. You might need to install a "
6911 "GStreamer RTSP extension plugin for Real media streams.")),
6914 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6915 (_("No supported stream was found. You might need to allow "
6916 "more transport protocols or may otherwise be missing "
6917 "the right GStreamer RTSP extension plugin.")), (NULL));
6919 return GST_RTSP_ERROR;
6923 gst_rtsp_message_unset (&request);
6924 gst_rtsp_message_unset (&response);
6930 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
6931 GstSegment * segment)
6934 GstRTSPTimeRange *therange;
6937 gst_rtsp_range_free (src->range);
6939 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
6940 GST_DEBUG_OBJECT (src, "parsed range %s", range);
6941 src->range = therange;
6943 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6945 gst_segment_init (segment, GST_FORMAT_TIME);
6949 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6950 therange->min.type, therange->min.seconds, therange->max.type,
6951 therange->max.seconds);
6953 if (therange->min.type == GST_RTSP_TIME_NOW)
6955 else if (therange->min.type == GST_RTSP_TIME_END)
6958 seconds = therange->min.seconds * GST_SECOND;
6960 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6961 GST_TIME_ARGS (seconds));
6963 /* we need to start playback without clipping from the position reported by
6965 segment->start = seconds;
6966 segment->position = seconds;
6968 if (therange->max.type == GST_RTSP_TIME_NOW)
6970 else if (therange->max.type == GST_RTSP_TIME_END)
6973 seconds = therange->max.seconds * GST_SECOND;
6975 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6976 GST_TIME_ARGS (seconds));
6978 /* live (WMS) server might send overflowed large max as its idea of infinity,
6979 * compensate to prevent problems later on */
6980 if (seconds != -1 && seconds < 0) {
6982 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6985 /* live (WMS) might send min == max, which is not worth recording */
6986 if (segment->duration == -1 && seconds == segment->start)
6989 /* don't change duration with unknown value, we might have a valid value
6990 * there that we want to keep. */
6992 segment->duration = seconds;
6997 /* Parse clock profived by the server with following syntax:
6999 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
7002 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
7004 gboolean res = FALSE;
7006 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
7007 gchar **fields = NULL, **parts = NULL;
7008 gchar *remote_ip, *str;
7010 GstClockTime base_time;
7013 fields = g_strsplit (gstclock, " ", 0);
7015 /* wrapped clock, not very interesting for now */
7016 if (fields[1] == NULL)
7019 /* remote IP address and port */
7020 if ((str = fields[2]) == NULL)
7023 parts = g_strsplit (str, ":", 0);
7025 if ((remote_ip = parts[0]) == NULL)
7028 if ((str = parts[1]) == NULL)
7036 if ((str = fields[3]) == NULL)
7039 base_time = g_ascii_strtoull (str, NULL, 10);
7042 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
7045 if (src->provided_clock)
7046 gst_object_unref (src->provided_clock);
7047 src->provided_clock = netclock;
7049 gst_element_post_message (GST_ELEMENT_CAST (src),
7050 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
7051 src->provided_clock, TRUE));
7055 g_strfreev (fields);
7061 /* must be called with the RTSP state lock */
7062 static GstRTSPResult
7063 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
7069 /* prepare global stream caps properties */
7071 gst_structure_remove_all_fields (src->props);
7073 src->props = gst_structure_new_empty ("RTSPProperties");
7076 gst_sdp_message_dump (sdp);
7078 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
7080 /* let the app inspect and change the SDP */
7081 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
7083 gst_segment_init (&src->segment, GST_FORMAT_TIME);
7085 /* parse range for duration reporting. */
7090 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
7094 /* keep track of the range and configure it in the segment */
7095 if (gst_rtspsrc_parse_range (src, range, &src->segment))
7099 /* parse clock information. This is GStreamer specific, a server can tell the
7100 * client what clock it is using and wrap that in a network clock. The
7101 * advantage of that is that we can slave to it. */
7103 const gchar *gstclock;
7106 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
7107 if (gstclock == NULL)
7110 /* parse the clock and expose it in the provide_clock method */
7111 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
7115 /* try to find a global control attribute. Note that a '*' means that we should
7116 * do aggregate control with the current url (so we don't do anything and
7117 * leave the current connection as is) */
7119 const gchar *control;
7122 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
7123 if (control == NULL)
7126 /* only take fully qualified urls */
7127 if (g_str_has_prefix (control, "rtsp://"))
7131 g_free (src->conninfo.location);
7132 src->conninfo.location = g_strdup (control);
7133 /* make a connection for this, if there was a connection already, nothing
7135 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
7136 GST_ERROR_OBJECT (src, "could not connect");
7139 /* we need to keep the control url separate from the connection url because
7140 * the rules for constructing the media control url need it */
7141 g_free (src->control);
7142 src->control = g_strdup (control);
7145 /* create streams */
7146 n_streams = gst_sdp_message_medias_len (sdp);
7147 for (i = 0; i < n_streams; i++) {
7148 gst_rtspsrc_create_stream (src, sdp, i);
7151 src->state = GST_RTSP_STATE_INIT;
7154 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
7157 /* reset our state */
7158 src->need_range = TRUE;
7161 src->state = GST_RTSP_STATE_READY;
7168 GST_ERROR_OBJECT (src, "setup failed");
7169 gst_rtspsrc_cleanup (src);
7174 static GstRTSPResult
7175 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
7179 GstRTSPMessage request = { 0 };
7180 GstRTSPMessage response = { 0 };
7183 gchar *respcont = NULL;
7186 src->need_redirect = FALSE;
7188 /* can't continue without a valid url */
7189 if (G_UNLIKELY (src->conninfo.url == NULL)) {
7190 res = GST_RTSP_EINVAL;
7193 src->tried_url_auth = FALSE;
7195 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
7196 goto connect_failed;
7198 /* create OPTIONS */
7199 GST_DEBUG_OBJECT (src, "create options...");
7201 gst_rtspsrc_init_request (src, &request, GST_RTSP_OPTIONS,
7202 src->conninfo.url_str);
7204 goto create_request_failed;
7207 GST_DEBUG_OBJECT (src, "send options...");
7210 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
7213 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
7218 if (!gst_rtspsrc_parse_methods (src, &response))
7221 /* create DESCRIBE */
7222 GST_DEBUG_OBJECT (src, "create describe...");
7224 gst_rtspsrc_init_request (src, &request, GST_RTSP_DESCRIBE,
7225 src->conninfo.url_str);
7227 goto create_request_failed;
7229 /* we only accept SDP for now */
7230 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
7234 GST_DEBUG_OBJECT (src, "send describe...");
7237 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
7240 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
7244 /* we only perform redirect for the describe, currently */
7245 if (src->need_redirect) {
7246 /* close connection, we don't have to send a TEARDOWN yet, ignore the
7248 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7250 gst_rtsp_message_unset (&request);
7251 gst_rtsp_message_unset (&response);
7257 /* it could be that the DESCRIBE method was not implemented */
7258 if (!(src->methods & GST_RTSP_DESCRIBE))
7261 /* check if reply is SDP */
7262 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
7264 /* could not be set but since the request returned OK, we assume it
7265 * was SDP, else check it. */
7267 if (g_ascii_strcasecmp (respcont, "application/sdp") != 0)
7268 goto wrong_content_type;
7271 /* get message body and parse as SDP */
7272 gst_rtsp_message_get_body (&response, &data, &size);
7273 if (data == NULL || size == 0)
7276 GST_DEBUG_OBJECT (src, "parse SDP...");
7277 gst_sdp_message_new (sdp);
7278 gst_sdp_message_parse_buffer (data, size, *sdp);
7280 /* clean up any messages */
7281 gst_rtsp_message_unset (&request);
7282 gst_rtsp_message_unset (&response);
7289 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
7290 ("No valid RTSP URL was provided"));
7295 gchar *str = gst_rtsp_strresult (res);
7297 if (res != GST_RTSP_EINTR) {
7298 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
7299 ("Failed to connect. (%s)", str));
7301 GST_WARNING_OBJECT (src, "connect interrupted");
7306 create_request_failed:
7308 gchar *str = gst_rtsp_strresult (res);
7310 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7311 ("Could not create request. (%s)", str));
7317 /* Don't post a message - the rtsp_send method will have
7318 * taken care of it because we passed NULL for the response code */
7323 /* error was posted */
7324 res = GST_RTSP_ERROR;
7329 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7330 ("Server does not support SDP, got %s.", respcont));
7331 res = GST_RTSP_ERROR;
7336 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7337 ("Server can not provide an SDP."));
7338 res = GST_RTSP_ERROR;
7343 if (src->conninfo.connection) {
7344 GST_DEBUG_OBJECT (src, "free connection");
7345 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7347 gst_rtsp_message_unset (&request);
7348 gst_rtsp_message_unset (&response);
7353 static GstRTSPResult
7354 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
7359 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
7361 if (src->sdp == NULL) {
7362 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
7366 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
7371 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
7378 GST_WARNING_OBJECT (src, "can't get sdp");
7379 src->open_error = TRUE;
7384 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
7385 src->open_error = TRUE;
7390 static GstRTSPResult
7391 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
7393 GstRTSPMessage request = { 0 };
7394 GstRTSPMessage response = { 0 };
7395 GstRTSPResult res = GST_RTSP_OK;
7397 const gchar *control;
7399 GST_DEBUG_OBJECT (src, "TEARDOWN...");
7401 gst_rtspsrc_set_state (src, GST_STATE_READY);
7403 if (src->state < GST_RTSP_STATE_READY) {
7404 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
7411 /* construct a control url */
7412 control = get_aggregate_control (src);
7414 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
7417 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7418 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7419 const gchar *setup_url;
7420 GstRTSPConnInfo *info;
7422 /* try aggregate control first but do non-aggregate control otherwise */
7424 setup_url = control;
7425 else if ((setup_url = stream->conninfo.location) == NULL)
7428 if (src->conninfo.connection) {
7429 info = &src->conninfo;
7430 } else if (stream->conninfo.connection) {
7431 info = &stream->conninfo;
7435 if (!info->connected)
7440 gst_rtspsrc_init_request (src, &request, GST_RTSP_TEARDOWN, setup_url);
7442 goto create_request_failed;
7445 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
7448 gst_rtspsrc_send (src, info->connection, &request, &response,
7452 /* FIXME, parse result? */
7453 gst_rtsp_message_unset (&request);
7454 gst_rtsp_message_unset (&response);
7457 /* early exit when we did aggregate control */
7463 /* close connections */
7464 GST_DEBUG_OBJECT (src, "closing connection...");
7465 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7466 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7467 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7468 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
7472 gst_rtspsrc_cleanup (src);
7474 src->state = GST_RTSP_STATE_INVALID;
7477 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
7482 create_request_failed:
7484 gchar *str = gst_rtsp_strresult (res);
7486 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7487 ("Could not create request. (%s)", str));
7493 gchar *str = gst_rtsp_strresult (res);
7495 gst_rtsp_message_unset (&request);
7496 if (res != GST_RTSP_EINTR) {
7497 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7498 ("Could not send message. (%s)", str));
7500 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
7507 GST_DEBUG_OBJECT (src,
7508 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
7513 /* RTP-Info is of the format:
7515 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
7517 * rtptime corresponds to the timestamp for the NPT time given in the header
7518 * seqbase corresponds to the next sequence number we received. This number
7519 * indicates the first seqnum after the seek and should be used to discard
7520 * packets that are from before the seek.
7523 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
7528 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
7530 infos = g_strsplit (rtpinfo, ",", 0);
7531 for (i = 0; infos[i]; i++) {
7533 GstRTSPStream *stream;
7537 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
7539 /* init values, types of seqbase and timebase are bigger than needed so we
7540 * can store -1 as uninitialized values */
7545 /* parse url, find stream for url.
7546 * parse seq and rtptime. The seq number should be configured in the rtp
7547 * depayloader or session manager to detect gaps. Same for the rtptime, it
7548 * should be used to create an initial time newsegment. */
7549 fields = g_strsplit (infos[i], ";", 0);
7550 for (j = 0; fields[j]; j++) {
7551 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
7552 /* remove leading whitespace */
7553 fields[j] = g_strchug (fields[j]);
7554 if (g_str_has_prefix (fields[j], "url=")) {
7555 /* get the url and the stream */
7557 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
7558 } else if (g_str_has_prefix (fields[j], "seq=")) {
7559 seqbase = atoi (fields[j] + 4);
7560 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
7561 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
7564 g_strfreev (fields);
7565 /* now we need to store the values for the caps of the stream */
7566 if (stream != NULL) {
7567 GST_DEBUG_OBJECT (src,
7568 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
7569 stream, seqbase, timebase);
7571 /* we have a stream, configure detected params */
7572 stream->seqbase = seqbase;
7573 stream->timebase = timebase;
7582 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
7587 interval = strtoul (rtcp, NULL, 10);
7588 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
7593 interval *= GST_MSECOND;
7595 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7596 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7598 /* already (optionally) retrieved this when configuring manager */
7599 if (stream->session) {
7600 GObject *rtpsession = stream->session;
7602 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
7604 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
7608 /* now it happens that (Xenon) server sending this may also provide bogus
7609 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
7610 * and just use RTP-Info to sync */
7612 GObjectClass *klass;
7614 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
7615 if (g_object_class_find_property (klass, "rtcp-sync")) {
7616 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
7617 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
7623 gst_rtspsrc_get_float (const gchar * dstr)
7625 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7627 /* canonicalise floating point string so we can handle float strings
7628 * in the form "24.930" or "24,930" irrespective of the current locale */
7629 g_strlcpy (s, dstr, sizeof (s));
7630 g_strdelimit (s, ",", '.');
7631 return g_ascii_strtod (s, NULL);
7635 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
7637 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7639 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
7640 g_strlcpy (val_str, "now", sizeof (val_str));
7642 if (segment->position == 0) {
7643 g_strlcpy (val_str, "0", sizeof (val_str));
7645 g_ascii_dtostr (val_str, sizeof (val_str),
7646 ((gdouble) segment->position) / GST_SECOND);
7649 return g_strdup_printf ("npt=%s-", val_str);
7653 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
7657 stream->timebase = -1;
7658 stream->seqbase = -1;
7660 len = stream->ptmap->len;
7661 for (i = 0; i < len; i++) {
7662 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
7665 if (item->caps == NULL)
7668 item->caps = gst_caps_make_writable (item->caps);
7669 s = gst_caps_get_structure (item->caps, 0);
7670 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
7674 static GstRTSPResult
7675 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
7677 GstRTSPResult res = GST_RTSP_OK;
7679 if (src->state < GST_RTSP_STATE_READY) {
7680 res = GST_RTSP_ERROR;
7681 if (src->open_error) {
7682 GST_DEBUG_OBJECT (src, "the stream was in error");
7686 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
7688 if ((res = gst_rtspsrc_open (src, async)) < 0) {
7689 GST_DEBUG_OBJECT (src, "failed to open stream");
7698 static GstRTSPResult
7699 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
7701 GstRTSPMessage request = { 0 };
7702 GstRTSPMessage response = { 0 };
7703 GstRTSPResult res = GST_RTSP_OK;
7707 const gchar *control;
7709 GST_DEBUG_OBJECT (src, "PLAY...");
7711 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7714 if (!(src->methods & GST_RTSP_PLAY))
7717 if (src->state == GST_RTSP_STATE_PLAYING)
7720 if (!src->conninfo.connection || !src->conninfo.connected)
7723 /* send some dummy packets before we activate the receive in the
7725 gst_rtspsrc_send_dummy_packets (src);
7727 /* require new SR packets */
7729 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
7731 /* construct a control url */
7732 control = get_aggregate_control (src);
7734 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7735 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7736 const gchar *setup_url;
7737 GstRTSPConnection *conn;
7739 /* try aggregate control first but do non-aggregate control otherwise */
7741 setup_url = control;
7742 else if ((setup_url = stream->conninfo.location) == NULL)
7745 if (src->conninfo.connection) {
7746 conn = src->conninfo.connection;
7747 } else if (stream->conninfo.connection) {
7748 conn = stream->conninfo.connection;
7754 res = gst_rtspsrc_init_request (src, &request, GST_RTSP_PLAY, setup_url);
7756 goto create_request_failed;
7758 if (src->need_range) {
7759 hval = gen_range_header (src, segment);
7761 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
7763 /* store the newsegment event so it can be sent from the streaming thread. */
7764 src->need_segment = TRUE;
7767 if (segment->rate != 1.0) {
7768 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
7770 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
7772 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
7774 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
7778 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
7780 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7783 /* seek may have silently failed as it is not supported */
7784 if (!(src->methods & GST_RTSP_PLAY)) {
7785 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
7786 /* obviously it is supported as we made it here */
7787 src->methods |= GST_RTSP_PLAY;
7788 src->seekable = FALSE;
7789 /* but there is nothing to parse in the response,
7790 * so convey we have no idea and not to expect anything particular */
7791 clear_rtp_base (src, stream);
7795 /* need to do for all streams */
7796 for (run = src->streams; run; run = g_list_next (run))
7797 clear_rtp_base (src, (GstRTSPStream *) run->data);
7799 /* NOTE the above also disables npt based eos detection */
7800 /* and below forces position to 0,
7801 * which is visible feedback we lost the plot */
7802 segment->start = segment->position = src->last_pos;
7805 gst_rtsp_message_unset (&request);
7807 /* parse RTP npt field. This is the current position in the stream (Normal
7808 * Play Time) and should be put in the NEWSEGMENT position field. */
7809 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
7811 gst_rtspsrc_parse_range (src, hval, segment);
7813 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
7814 segment->rate = 1.0;
7816 /* parse Speed header. This is the intended playback rate of the stream
7817 * and should be put in the NEWSEGMENT rate field. */
7818 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
7819 0) == GST_RTSP_OK) {
7820 segment->rate = gst_rtspsrc_get_float (hval);
7821 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
7822 &hval, 0) == GST_RTSP_OK) {
7823 segment->rate = gst_rtspsrc_get_float (hval);
7826 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
7827 * for the RTP packets. If this is not present, we assume all starts from 0...
7828 * This is info for the RTP session manager that we pass to it in caps. */
7830 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
7831 &hval, hval_idx++) == GST_RTSP_OK)
7832 gst_rtspsrc_parse_rtpinfo (src, hval);
7834 /* some servers indicate RTCP parameters in PLAY response,
7835 * rather than properly in SDP */
7836 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
7837 &hval, 0) == GST_RTSP_OK)
7838 gst_rtspsrc_handle_rtcp_interval (src, hval);
7840 gst_rtsp_message_unset (&response);
7842 /* early exit when we did aggregate control */
7846 /* configure the caps of the streams after we parsed all headers. Only reset
7847 * the manager object when we set a new Range header (we did a seek) */
7848 gst_rtspsrc_configure_caps (src, segment, src->need_range);
7850 /* set to PLAYING after we have configured the caps, otherwise we
7851 * might end up calling request_key (with SRTP) while caps are still
7852 * being configured. */
7853 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
7855 /* set again when needed */
7856 src->need_range = FALSE;
7858 src->running = TRUE;
7859 src->base_time = -1;
7860 src->state = GST_RTSP_STATE_PLAYING;
7863 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
7864 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7865 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7866 stream->discont = TRUE;
7871 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
7878 GST_DEBUG_OBJECT (src, "failed to open stream");
7883 GST_DEBUG_OBJECT (src, "PLAY is not supported");
7888 GST_DEBUG_OBJECT (src, "we were already PLAYING");
7891 create_request_failed:
7893 gchar *str = gst_rtsp_strresult (res);
7895 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7896 ("Could not create request. (%s)", str));
7902 gchar *str = gst_rtsp_strresult (res);
7904 gst_rtsp_message_unset (&request);
7905 if (res != GST_RTSP_EINTR) {
7906 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7907 ("Could not send message. (%s)", str));
7909 GST_WARNING_OBJECT (src, "PLAY interrupted");
7916 static GstRTSPResult
7917 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
7919 GstRTSPResult res = GST_RTSP_OK;
7920 GstRTSPMessage request = { 0 };
7921 GstRTSPMessage response = { 0 };
7923 const gchar *control;
7925 GST_DEBUG_OBJECT (src, "PAUSE...");
7927 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7930 if (!(src->methods & GST_RTSP_PAUSE))
7933 if (src->state == GST_RTSP_STATE_READY)
7936 if (!src->conninfo.connection || !src->conninfo.connected)
7939 /* construct a control url */
7940 control = get_aggregate_control (src);
7942 /* loop over the streams. We might exit the loop early when we could do an
7943 * aggregate control */
7944 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7945 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7946 GstRTSPConnection *conn;
7947 const gchar *setup_url;
7949 /* try aggregate control first but do non-aggregate control otherwise */
7951 setup_url = control;
7952 else if ((setup_url = stream->conninfo.location) == NULL)
7955 if (src->conninfo.connection) {
7956 conn = src->conninfo.connection;
7957 } else if (stream->conninfo.connection) {
7958 conn = stream->conninfo.connection;
7964 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7965 ("Sending PAUSE request"));
7968 gst_rtspsrc_init_request (src, &request, GST_RTSP_PAUSE,
7970 goto create_request_failed;
7972 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7975 gst_rtsp_message_unset (&request);
7976 gst_rtsp_message_unset (&response);
7978 /* exit early when we did agregate control */
7983 /* change element states now */
7984 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7987 src->state = GST_RTSP_STATE_READY;
7991 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7998 GST_DEBUG_OBJECT (src, "failed to open stream");
8003 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
8008 GST_DEBUG_OBJECT (src, "we were already PAUSED");
8011 create_request_failed:
8013 gchar *str = gst_rtsp_strresult (res);
8015 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
8016 ("Could not create request. (%s)", str));
8022 gchar *str = gst_rtsp_strresult (res);
8024 gst_rtsp_message_unset (&request);
8025 if (res != GST_RTSP_EINTR) {
8026 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
8027 ("Could not send message. (%s)", str));
8029 GST_WARNING_OBJECT (src, "PAUSE interrupted");
8037 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
8039 GstRTSPSrc *rtspsrc;
8041 rtspsrc = GST_RTSPSRC (bin);
8043 switch (GST_MESSAGE_TYPE (message)) {
8044 case GST_MESSAGE_EOS:
8045 gst_message_unref (message);
8047 case GST_MESSAGE_ELEMENT:
8049 const GstStructure *s = gst_message_get_structure (message);
8051 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
8052 gboolean ignore_timeout;
8054 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
8056 GST_OBJECT_LOCK (rtspsrc);
8057 ignore_timeout = rtspsrc->ignore_timeout;
8058 rtspsrc->ignore_timeout = TRUE;
8059 GST_OBJECT_UNLOCK (rtspsrc);
8061 /* we only act on the first udp timeout message, others are irrelevant
8062 * and can be ignored. */
8063 if (!ignore_timeout)
8064 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
8066 gst_message_unref (message);
8069 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8072 case GST_MESSAGE_ERROR:
8075 GstRTSPStream *stream;
8078 udpsrc = GST_MESSAGE_SRC (message);
8080 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
8081 GST_ELEMENT_NAME (udpsrc));
8083 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
8087 /* we ignore the RTCP udpsrc */
8088 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
8091 /* if we get error messages from the udp sources, that's not a problem as
8092 * long as not all of them error out. We also don't really know what the
8093 * problem is, the message does not give enough detail... */
8094 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
8095 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
8096 if (ret != GST_FLOW_OK)
8100 gst_message_unref (message);
8104 /* fatal but not our message, forward */
8105 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8110 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8116 /* the thread where everything happens */
8118 gst_rtspsrc_thread (GstRTSPSrc * src)
8122 GST_OBJECT_LOCK (src);
8123 cmd = src->pending_cmd;
8124 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
8125 || cmd == CMD_LOOP || cmd == CMD_OPEN)
8126 src->pending_cmd = CMD_LOOP;
8128 src->pending_cmd = CMD_WAIT;
8129 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
8131 /* we got the message command, so ensure communication is possible again */
8132 gst_rtspsrc_connection_flush (src, FALSE);
8134 src->busy_cmd = cmd;
8135 GST_OBJECT_UNLOCK (src);
8139 gst_rtspsrc_open (src, TRUE);
8142 gst_rtspsrc_play (src, &src->segment, TRUE);
8145 gst_rtspsrc_pause (src, TRUE);
8148 gst_rtspsrc_close (src, TRUE, FALSE);
8151 gst_rtspsrc_loop (src);
8154 gst_rtspsrc_reconnect (src, FALSE);
8160 GST_OBJECT_LOCK (src);
8161 /* and go back to sleep */
8162 if (src->pending_cmd == CMD_WAIT) {
8164 gst_task_pause (src->task);
8167 src->busy_cmd = CMD_WAIT;
8168 GST_OBJECT_UNLOCK (src);
8172 gst_rtspsrc_start (GstRTSPSrc * src)
8174 GST_DEBUG_OBJECT (src, "starting");
8176 GST_OBJECT_LOCK (src);
8178 src->pending_cmd = CMD_WAIT;
8180 if (src->task == NULL) {
8181 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
8182 if (src->task == NULL)
8185 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
8187 GST_OBJECT_UNLOCK (src);
8194 GST_OBJECT_UNLOCK (src);
8195 GST_ERROR_OBJECT (src, "failed to create task");
8201 gst_rtspsrc_stop (GstRTSPSrc * src)
8205 GST_DEBUG_OBJECT (src, "stopping");
8207 /* also cancels pending task */
8208 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
8210 GST_OBJECT_LOCK (src);
8211 if ((task = src->task)) {
8213 GST_OBJECT_UNLOCK (src);
8215 gst_task_stop (task);
8217 /* make sure it is not running */
8218 GST_RTSP_STREAM_LOCK (src);
8219 GST_RTSP_STREAM_UNLOCK (src);
8221 /* now wait for the task to finish */
8222 gst_task_join (task);
8224 /* and free the task */
8225 gst_object_unref (GST_OBJECT (task));
8227 GST_OBJECT_LOCK (src);
8229 GST_OBJECT_UNLOCK (src);
8231 /* ensure synchronously all is closed and clean */
8232 gst_rtspsrc_close (src, FALSE, TRUE);
8237 static GstStateChangeReturn
8238 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
8240 GstRTSPSrc *rtspsrc;
8241 GstStateChangeReturn ret;
8243 rtspsrc = GST_RTSPSRC (element);
8245 switch (transition) {
8246 case GST_STATE_CHANGE_NULL_TO_READY:
8247 if (!gst_rtspsrc_start (rtspsrc))
8250 case GST_STATE_CHANGE_READY_TO_PAUSED:
8251 /* init some state */
8252 rtspsrc->cur_protocols = rtspsrc->protocols;
8253 /* first attempt, don't ignore timeouts */
8254 rtspsrc->ignore_timeout = FALSE;
8255 rtspsrc->open_error = FALSE;
8256 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
8258 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8259 set_manager_buffer_mode (rtspsrc);
8261 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8262 /* unblock the tcp tasks and make the loop waiting */
8263 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
8264 /* make sure it is waiting before we send PAUSE or PLAY below */
8265 GST_RTSP_STREAM_LOCK (rtspsrc);
8266 GST_RTSP_STREAM_UNLOCK (rtspsrc);
8269 case GST_STATE_CHANGE_PAUSED_TO_READY:
8275 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
8276 if (ret == GST_STATE_CHANGE_FAILURE)
8279 switch (transition) {
8280 case GST_STATE_CHANGE_NULL_TO_READY:
8281 ret = GST_STATE_CHANGE_SUCCESS;
8283 case GST_STATE_CHANGE_READY_TO_PAUSED:
8284 ret = GST_STATE_CHANGE_NO_PREROLL;
8286 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8287 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
8288 ret = GST_STATE_CHANGE_SUCCESS;
8290 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8291 /* send pause request and keep the idle task around */
8292 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
8293 ret = GST_STATE_CHANGE_NO_PREROLL;
8295 case GST_STATE_CHANGE_PAUSED_TO_READY:
8296 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
8297 ret = GST_STATE_CHANGE_SUCCESS;
8299 case GST_STATE_CHANGE_READY_TO_NULL:
8300 gst_rtspsrc_stop (rtspsrc);
8301 ret = GST_STATE_CHANGE_SUCCESS;
8312 GST_DEBUG_OBJECT (rtspsrc, "start failed");
8313 return GST_STATE_CHANGE_FAILURE;
8318 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
8321 GstRTSPSrc *rtspsrc;
8323 rtspsrc = GST_RTSPSRC (element);
8325 if (GST_EVENT_IS_DOWNSTREAM (event)) {
8326 res = gst_rtspsrc_push_event (rtspsrc, event);
8328 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
8335 /*** GSTURIHANDLER INTERFACE *************************************************/
8338 gst_rtspsrc_uri_get_type (GType type)
8343 static const gchar *const *
8344 gst_rtspsrc_uri_get_protocols (GType type)
8346 static const gchar *protocols[] =
8347 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
8348 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
8355 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
8357 GstRTSPSrc *src = GST_RTSPSRC (handler);
8359 /* FIXME: make thread-safe */
8360 return g_strdup (src->conninfo.location);
8364 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
8370 GstRTSPUrl *newurl = NULL;
8371 GstSDPMessage *sdp = NULL;
8373 src = GST_RTSPSRC (handler);
8375 /* same URI, we're fine */
8376 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
8379 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
8380 sres = gst_sdp_message_new (&sdp);
8384 GST_DEBUG_OBJECT (src, "parsing SDP message");
8385 sres = gst_sdp_message_parse_uri (uri, sdp);
8390 GST_DEBUG_OBJECT (src, "parsing URI");
8391 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
8395 /* if worked, free previous and store new url object along with the original
8397 GST_DEBUG_OBJECT (src, "configuring URI");
8398 g_free (src->conninfo.location);
8399 src->conninfo.location = g_strdup (uri);
8400 gst_rtsp_url_free (src->conninfo.url);
8401 src->conninfo.url = newurl;
8402 g_free (src->conninfo.url_str);
8404 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
8406 src->conninfo.url_str = NULL;
8409 gst_sdp_message_free (src->sdp);
8411 src->from_sdp = sdp != NULL;
8413 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
8414 GST_DEBUG_OBJECT (src, "request uri is: %s",
8415 GST_STR_NULL (src->conninfo.url_str));
8422 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
8427 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
8428 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8429 "Could not create SDP");
8434 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
8435 GST_STR_NULL (uri));
8436 gst_sdp_message_free (sdp);
8437 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8443 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
8444 GST_STR_NULL (uri), res);
8445 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8446 "Invalid RTSP URI");
8452 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
8454 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
8456 iface->get_type = gst_rtspsrc_uri_get_type;
8457 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
8458 iface->get_uri = gst_rtspsrc_uri_get_uri;
8459 iface->set_uri = gst_rtspsrc_uri_set_uri;