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
246 PROP_DROP_ON_LATENCY,
247 PROP_CONNECTION_SPEED,
250 PROP_DO_RTSP_KEEP_ALIVE,
259 PROP_UDP_BUFFER_SIZE,
263 PROP_MULTICAST_IFACE,
265 PROP_USE_PIPELINE_CLOCK,
267 PROP_TLS_VALIDATION_FLAGS,
269 PROP_TLS_INTERACTION,
270 PROP_DO_RETRANSMISSION,
271 PROP_NTP_TIME_SOURCE,
275 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
277 gst_rtsp_nat_method_get_type (void)
279 static GType rtsp_nat_method_type = 0;
280 static const GEnumValue rtsp_nat_method[] = {
281 {GST_RTSP_NAT_NONE, "None", "none"},
282 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
286 if (!rtsp_nat_method_type) {
287 rtsp_nat_method_type =
288 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
290 return rtsp_nat_method_type;
293 static void gst_rtspsrc_finalize (GObject * object);
295 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
296 const GValue * value, GParamSpec * pspec);
297 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
298 GValue * value, GParamSpec * pspec);
300 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
302 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
303 gpointer iface_data);
305 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
308 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
309 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
311 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
313 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
314 GstStateChange transition);
315 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
316 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
318 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
319 GstRTSPMessage * response);
321 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
323 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
324 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
326 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
327 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
329 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
330 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
331 gboolean only_close);
333 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
334 const gchar * uri, GError ** error);
335 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
337 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
338 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
339 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
340 GstRTSPStream * stream, GstEvent * event);
341 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
342 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
350 /* commands we send to out loop to notify it of events */
351 #define CMD_OPEN (1 << 0)
352 #define CMD_PLAY (1 << 1)
353 #define CMD_PAUSE (1 << 2)
354 #define CMD_CLOSE (1 << 3)
355 #define CMD_WAIT (1 << 4)
356 #define CMD_RECONNECT (1 << 5)
357 #define CMD_LOOP (1 << 6)
359 /* mask for all commands */
360 #define CMD_ALL ((CMD_LOOP << 1) - 1)
362 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
364 gchar *__txt = _gst_element_error_printf text; \
365 gst_element_post_message (GST_ELEMENT_CAST (el), \
366 gst_message_new_progress (GST_OBJECT_CAST (el), \
367 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
371 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
373 #define gst_rtspsrc_parent_class parent_class
374 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
375 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
377 #ifndef GST_DISABLE_GST_DEBUG
378 static inline const char *
379 cmd_to_string (guint cmd)
403 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
405 GST_DEBUG_OBJECT (src, "default handler");
410 select_stream_accum (GSignalInvocationHint * ihint,
411 GValue * return_accu, const GValue * handler_return, gpointer data)
415 myboolean = g_value_get_boolean (handler_return);
416 GST_DEBUG ("accum %d", myboolean);
417 g_value_set_boolean (return_accu, myboolean);
419 /* stop emission if FALSE */
424 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
426 GObjectClass *gobject_class;
427 GstElementClass *gstelement_class;
428 GstBinClass *gstbin_class;
430 gobject_class = (GObjectClass *) klass;
431 gstelement_class = (GstElementClass *) klass;
432 gstbin_class = (GstBinClass *) klass;
434 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
436 gobject_class->set_property = gst_rtspsrc_set_property;
437 gobject_class->get_property = gst_rtspsrc_get_property;
439 gobject_class->finalize = gst_rtspsrc_finalize;
441 g_object_class_install_property (gobject_class, PROP_LOCATION,
442 g_param_spec_string ("location", "RTSP Location",
443 "Location of the RTSP url to read",
444 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
446 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
447 g_param_spec_flags ("protocols", "Protocols",
448 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
449 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
451 g_object_class_install_property (gobject_class, PROP_DEBUG,
452 g_param_spec_boolean ("debug", "Debug",
453 "Dump request and response messages to stdout",
454 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
456 g_object_class_install_property (gobject_class, PROP_RETRY,
457 g_param_spec_uint ("retry", "Retry",
458 "Max number of retries when allocating RTP ports.",
459 0, G_MAXUINT16, DEFAULT_RETRY,
460 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
462 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
463 g_param_spec_uint64 ("timeout", "Timeout",
464 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
465 0, G_MAXUINT64, DEFAULT_TIMEOUT,
466 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
468 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
469 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
470 "Fail after timeout microseconds on TCP connections (0 = disabled)",
471 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
472 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
474 g_object_class_install_property (gobject_class, PROP_LATENCY,
475 g_param_spec_uint ("latency", "Buffer latency in ms",
476 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
477 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
479 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
480 g_param_spec_boolean ("drop-on-latency",
481 "Drop buffers when maximum latency is reached",
482 "Tells the jitterbuffer to never exceed the given latency in size",
483 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
485 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
486 g_param_spec_uint64 ("connection-speed", "Connection Speed",
487 "Network connection speed in kbps (0 = unknown)",
488 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
489 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
491 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
492 g_param_spec_enum ("nat-method", "NAT Method",
493 "Method to use for traversing firewalls and NAT",
494 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
495 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
498 * GstRTSPSrc:do-rtcp:
500 * Enable RTCP support. Some old server don't like RTCP and then this property
501 * needs to be set to FALSE.
503 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
504 g_param_spec_boolean ("do-rtcp", "Do RTCP",
505 "Send RTCP packets, disable for old incompatible server.",
506 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
509 * GstRTSPSrc:do-rtsp-keep-alive:
511 * Enable RTSP keep alive support. Some old server don't like RTSP
512 * keep alive and then this property needs to be set to FALSE.
514 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
515 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
516 "Send RTSP keep alive packets, disable for old incompatible server.",
517 DEFAULT_DO_RTSP_KEEP_ALIVE,
518 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
523 * Set the proxy parameters. This has to be a string of the format
524 * [http://][user:passwd@]host[:port].
526 g_object_class_install_property (gobject_class, PROP_PROXY,
527 g_param_spec_string ("proxy", "Proxy",
528 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
529 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
531 * GstRTSPSrc:proxy-id:
533 * Sets the proxy URI user id for authentication. If the URI set via the
534 * "proxy" property contains a user-id already, that will take precedence.
538 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
539 g_param_spec_string ("proxy-id", "proxy-id",
540 "HTTP proxy URI user id for authentication", "",
541 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
543 * GstRTSPSrc:proxy-pw:
545 * Sets the proxy URI password for authentication. If the URI set via the
546 * "proxy" property contains a password already, that will take precedence.
550 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
551 g_param_spec_string ("proxy-pw", "proxy-pw",
552 "HTTP proxy URI user password for authentication", "",
553 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
556 * GstRTSPSrc:rtp-blocksize:
558 * RTP package size to suggest to server.
560 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
561 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
562 "RTP package size to suggest to server (0 = disabled)",
563 0, 65536, DEFAULT_RTP_BLOCKSIZE,
564 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
566 g_object_class_install_property (gobject_class,
568 g_param_spec_string ("user-id", "user-id",
569 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
570 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
571 g_object_class_install_property (gobject_class, PROP_USER_PW,
572 g_param_spec_string ("user-pw", "user-pw",
573 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
574 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
577 * GstRTSPSrc:buffer-mode:
579 * Control the buffering and timestamping mode used by the jitterbuffer.
581 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
582 g_param_spec_enum ("buffer-mode", "Buffer Mode",
583 "Control the buffering algorithm in use",
584 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
585 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
588 * GstRTSPSrc:port-range:
590 * Configure the client port numbers that can be used to recieve RTP and
593 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
594 g_param_spec_string ("port-range", "Port range",
595 "Client port range that can be used to receive RTP and RTCP data, "
596 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
597 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
600 * GstRTSPSrc:udp-buffer-size:
602 * Size of the kernel UDP receive buffer in bytes.
604 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
605 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
606 "Size of the kernel UDP receive buffer in bytes, 0=default",
607 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
608 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
611 * GstRTSPSrc:short-header:
613 * Only send the basic RTSP headers for broken encoders.
615 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
616 g_param_spec_boolean ("short-header", "Short Header",
617 "Only send the basic RTSP headers for broken encoders",
618 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
620 g_object_class_install_property (gobject_class, PROP_PROBATION,
621 g_param_spec_uint ("probation", "Number of probations",
622 "Consecutive packet sequence numbers to accept the source",
623 0, G_MAXUINT, DEFAULT_PROBATION,
624 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
626 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
627 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
628 "Reconnect to the server if RTSP connection is closed when doing UDP",
629 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
631 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
632 g_param_spec_string ("multicast-iface", "Multicast Interface",
633 "The network interface on which to join the multicast group",
634 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
636 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
637 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
638 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
639 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
641 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
642 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
643 "Use the pipeline running-time to set the NTP time in the RTCP SR messages"
644 "(DEPRECATED: Use ntp-time-source property)",
645 DEFAULT_USE_PIPELINE_CLOCK,
646 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_DEPRECATED));
648 g_object_class_install_property (gobject_class, PROP_SDES,
649 g_param_spec_boxed ("sdes", "SDES",
650 "The SDES items of this session",
651 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
654 * GstRTSPSrc::tls-validation-flags:
656 * TLS certificate validation flags used to validate server
661 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
662 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
663 "TLS certificate validation flags used to validate the server certificate",
664 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
665 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
668 * GstRTSPSrc::tls-database:
670 * TLS database with anchor certificate authorities used to validate
671 * the server certificate.
675 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
676 g_param_spec_object ("tls-database", "TLS database",
677 "TLS database with anchor certificate authorities used to validate the server certificate",
678 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
681 * GstRTSPSrc::tls-interaction:
683 * A #GTlsInteraction object to be used when the connection or certificate
684 * database need to interact with the user. This will be used to prompt the
685 * user for passwords where necessary.
689 g_object_class_install_property (gobject_class, PROP_TLS_INTERACTION,
690 g_param_spec_object ("tls-interaction", "TLS interaction",
691 "A GTlsInteraction object to promt the user for password or certificate",
692 G_TYPE_TLS_INTERACTION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
695 * GstRTSPSrc::do-retransmission:
697 * Attempt to ask the server to retransmit lost packets according to RFC4588.
699 * Note: currently only works with SSRC-multiplexed retransmission streams
703 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
704 g_param_spec_boolean ("do-retransmission", "Retransmission",
705 "Ask the server to retransmit lost packets",
706 DEFAULT_DO_RETRANSMISSION,
707 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
710 * GstRTSPSrc::ntp-time-source:
712 * allows to select the time source that should be used
713 * for the NTP time in RTCP packets
717 g_object_class_install_property (gobject_class, PROP_NTP_TIME_SOURCE,
718 g_param_spec_enum ("ntp-time-source", "NTP Time Source",
719 "NTP time source for RTCP packets",
720 GST_TYPE_RTSP_SRC_NTP_TIME_SOURCE, DEFAULT_NTP_TIME_SOURCE,
721 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
724 * GstRTSPSrc::user-agent:
726 * The string to set in the User-Agent header.
730 g_object_class_install_property (gobject_class, PROP_USER_AGENT,
731 g_param_spec_string ("user-agent", "User Agent",
732 "The User-Agent string to send to the server",
733 DEFAULT_USER_AGENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
736 * GstRTSPSrc::handle-request:
737 * @rtspsrc: a #GstRTSPSrc
738 * @request: a #GstRTSPMessage
739 * @response: a #GstRTSPMessage
741 * Handle a server request in @request and prepare @response.
743 * This signal is called from the streaming thread, you should therefore not
744 * do any state changes on @rtspsrc because this might deadlock. If you want
745 * to modify the state as a result of this signal, post a
746 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
751 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
752 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
753 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
754 G_TYPE_POINTER, G_TYPE_POINTER);
757 * GstRTSPSrc::on-sdp:
758 * @rtspsrc: a #GstRTSPSrc
759 * @sdp: a #GstSDPMessage
761 * Emited when the client has retrieved the SDP and before it configures the
762 * streams in the SDP. @sdp can be inspected and modified.
764 * This signal is called from the streaming thread, you should therefore not
765 * do any state changes on @rtspsrc because this might deadlock. If you want
766 * to modify the state as a result of this signal, post a
767 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
772 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
773 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
774 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
775 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
778 * GstRTSPSrc::select-stream:
779 * @rtspsrc: a #GstRTSPSrc
780 * @num: the stream number
781 * @caps: the stream caps
783 * Emited before the client decides to configure the stream @num with
786 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
791 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
792 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
793 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
794 (GCallback) default_select_stream, select_stream_accum, NULL,
795 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
798 * GstRTSPSrc::new-manager:
799 * @rtspsrc: a #GstRTSPSrc
800 * @manager: a #GstElement
802 * Emited after a new manager (like rtpbin) was created and the default
803 * properties were configured.
807 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
808 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
809 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
810 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
813 * GstRTSPSrc::request-rtcp-key:
814 * @rtspsrc: a #GstRTSPSrc
815 * @num: the stream number
817 * Signal emited to get the crypto parameters relevant to the RTCP
818 * stream. User should provide the key and the RTCP encryption ciphers
819 * and authentication, and return them wrapped in a GstCaps.
823 gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY] =
824 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
825 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
827 gstelement_class->send_event = gst_rtspsrc_send_event;
828 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
829 gstelement_class->change_state = gst_rtspsrc_change_state;
831 gst_element_class_add_pad_template (gstelement_class,
832 gst_static_pad_template_get (&rtptemplate));
834 gst_element_class_set_static_metadata (gstelement_class,
835 "RTSP packet receiver", "Source/Network",
836 "Receive data over the network via RTSP (RFC 2326)",
837 "Wim Taymans <wim@fluendo.com>, "
838 "Thijs Vermeir <thijs.vermeir@barco.com>, "
839 "Lutz Mueller <lutz@topfrose.de>");
841 gstbin_class->handle_message = gst_rtspsrc_handle_message;
843 gst_rtsp_ext_list_init ();
847 gst_rtspsrc_init (GstRTSPSrc * src)
849 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
850 src->protocols = DEFAULT_PROTOCOLS;
851 src->debug = DEFAULT_DEBUG;
852 src->retry = DEFAULT_RETRY;
853 src->udp_timeout = DEFAULT_TIMEOUT;
854 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
855 src->latency = DEFAULT_LATENCY_MS;
856 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
857 src->connection_speed = DEFAULT_CONNECTION_SPEED;
858 src->nat_method = DEFAULT_NAT_METHOD;
859 src->do_rtcp = DEFAULT_DO_RTCP;
860 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
861 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
862 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
863 src->user_id = g_strdup (DEFAULT_USER_ID);
864 src->user_pw = g_strdup (DEFAULT_USER_PW);
865 src->buffer_mode = DEFAULT_BUFFER_MODE;
866 src->client_port_range.min = 0;
867 src->client_port_range.max = 0;
868 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
869 src->short_header = DEFAULT_SHORT_HEADER;
870 src->probation = DEFAULT_PROBATION;
871 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
872 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
873 src->ntp_sync = DEFAULT_NTP_SYNC;
874 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
876 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
877 src->tls_database = DEFAULT_TLS_DATABASE;
878 src->tls_interaction = DEFAULT_TLS_INTERACTION;
879 src->do_retransmission = DEFAULT_DO_RETRANSMISSION;
880 src->ntp_time_source = DEFAULT_NTP_TIME_SOURCE;
881 src->user_agent = g_strdup (DEFAULT_USER_AGENT);
883 /* get a list of all extensions */
884 src->extensions = gst_rtsp_ext_list_get ();
886 /* connect to send signal */
887 gst_rtsp_ext_list_connect (src->extensions, "send",
888 (GCallback) gst_rtspsrc_send_cb, src);
890 /* protects the streaming thread in interleaved mode or the polling
891 * thread in UDP mode. */
892 g_rec_mutex_init (&src->stream_rec_lock);
894 /* protects our state changes from multiple invocations */
895 g_rec_mutex_init (&src->state_rec_lock);
897 src->state = GST_RTSP_STATE_INVALID;
899 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
903 gst_rtspsrc_finalize (GObject * object)
907 rtspsrc = GST_RTSPSRC (object);
909 gst_rtsp_ext_list_free (rtspsrc->extensions);
910 g_free (rtspsrc->conninfo.location);
911 gst_rtsp_url_free (rtspsrc->conninfo.url);
912 g_free (rtspsrc->conninfo.url_str);
913 g_free (rtspsrc->user_id);
914 g_free (rtspsrc->user_pw);
915 g_free (rtspsrc->multi_iface);
916 g_free (rtspsrc->user_agent);
919 gst_sdp_message_free (rtspsrc->sdp);
922 if (rtspsrc->provided_clock)
923 gst_object_unref (rtspsrc->provided_clock);
926 gst_structure_free (rtspsrc->sdes);
928 if (rtspsrc->tls_database)
929 g_object_unref (rtspsrc->tls_database);
931 if (rtspsrc->tls_interaction)
932 g_object_unref (rtspsrc->tls_interaction);
935 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
936 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
938 G_OBJECT_CLASS (parent_class)->finalize (object);
942 gst_rtspsrc_provide_clock (GstElement * element)
944 GstRTSPSrc *src = GST_RTSPSRC (element);
947 if ((clock = src->provided_clock) != NULL)
948 gst_object_ref (clock);
953 /* a proxy string of the format [user:passwd@]host[:port] */
955 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
959 g_free (rtsp->proxy_user);
960 rtsp->proxy_user = NULL;
961 g_free (rtsp->proxy_passwd);
962 rtsp->proxy_passwd = NULL;
963 g_free (rtsp->proxy_host);
964 rtsp->proxy_host = NULL;
965 rtsp->proxy_port = 0;
972 /* we allow http:// in front but ignore it */
973 if (g_str_has_prefix (p, "http://"))
976 at = strchr (p, '@');
978 /* look for user:passwd */
979 col = strchr (proxy, ':');
980 if (col == NULL || col > at)
983 rtsp->proxy_user = g_strndup (p, col - p);
985 rtsp->proxy_passwd = g_strndup (col, at - col);
990 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
991 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
992 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
993 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
994 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
995 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
996 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
999 col = strchr (p, ':');
1002 /* everything before the colon is the hostname */
1003 rtsp->proxy_host = g_strndup (p, col - p);
1005 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
1007 rtsp->proxy_host = g_strdup (p);
1008 rtsp->proxy_port = 8080;
1014 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
1016 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
1017 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
1020 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
1022 rtspsrc->ptcp_timeout = NULL;
1026 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
1029 GstRTSPSrc *rtspsrc;
1031 rtspsrc = GST_RTSPSRC (object);
1035 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
1036 g_value_get_string (value), NULL);
1038 case PROP_PROTOCOLS:
1039 rtspsrc->protocols = g_value_get_flags (value);
1042 rtspsrc->debug = g_value_get_boolean (value);
1045 rtspsrc->retry = g_value_get_uint (value);
1048 rtspsrc->udp_timeout = g_value_get_uint64 (value);
1050 case PROP_TCP_TIMEOUT:
1051 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
1054 rtspsrc->latency = g_value_get_uint (value);
1056 case PROP_DROP_ON_LATENCY:
1057 rtspsrc->drop_on_latency = g_value_get_boolean (value);
1059 case PROP_CONNECTION_SPEED:
1060 rtspsrc->connection_speed = g_value_get_uint64 (value);
1062 case PROP_NAT_METHOD:
1063 rtspsrc->nat_method = g_value_get_enum (value);
1066 rtspsrc->do_rtcp = g_value_get_boolean (value);
1068 case PROP_DO_RTSP_KEEP_ALIVE:
1069 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
1072 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
1075 if (rtspsrc->prop_proxy_id)
1076 g_free (rtspsrc->prop_proxy_id);
1077 rtspsrc->prop_proxy_id = g_value_dup_string (value);
1080 if (rtspsrc->prop_proxy_pw)
1081 g_free (rtspsrc->prop_proxy_pw);
1082 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
1084 case PROP_RTP_BLOCKSIZE:
1085 rtspsrc->rtp_blocksize = g_value_get_uint (value);
1088 if (rtspsrc->user_id)
1089 g_free (rtspsrc->user_id);
1090 rtspsrc->user_id = g_value_dup_string (value);
1093 if (rtspsrc->user_pw)
1094 g_free (rtspsrc->user_pw);
1095 rtspsrc->user_pw = g_value_dup_string (value);
1097 case PROP_BUFFER_MODE:
1098 rtspsrc->buffer_mode = g_value_get_enum (value);
1100 case PROP_PORT_RANGE:
1104 str = g_value_get_string (value);
1106 sscanf (str, "%u-%u",
1107 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
1109 rtspsrc->client_port_range.min = 0;
1110 rtspsrc->client_port_range.max = 0;
1114 case PROP_UDP_BUFFER_SIZE:
1115 rtspsrc->udp_buffer_size = g_value_get_int (value);
1117 case PROP_SHORT_HEADER:
1118 rtspsrc->short_header = g_value_get_boolean (value);
1120 case PROP_PROBATION:
1121 rtspsrc->probation = g_value_get_uint (value);
1123 case PROP_UDP_RECONNECT:
1124 rtspsrc->udp_reconnect = g_value_get_boolean (value);
1126 case PROP_MULTICAST_IFACE:
1127 g_free (rtspsrc->multi_iface);
1129 if (g_value_get_string (value) == NULL)
1130 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1132 rtspsrc->multi_iface = g_value_dup_string (value);
1135 rtspsrc->ntp_sync = g_value_get_boolean (value);
1137 case PROP_USE_PIPELINE_CLOCK:
1138 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
1141 rtspsrc->sdes = g_value_dup_boxed (value);
1143 case PROP_TLS_VALIDATION_FLAGS:
1144 rtspsrc->tls_validation_flags = g_value_get_flags (value);
1146 case PROP_TLS_DATABASE:
1147 g_clear_object (&rtspsrc->tls_database);
1148 rtspsrc->tls_database = g_value_dup_object (value);
1150 case PROP_TLS_INTERACTION:
1151 g_clear_object (&rtspsrc->tls_interaction);
1152 rtspsrc->tls_interaction = g_value_dup_object (value);
1154 case PROP_DO_RETRANSMISSION:
1155 rtspsrc->do_retransmission = g_value_get_boolean (value);
1157 case PROP_NTP_TIME_SOURCE:
1158 rtspsrc->ntp_time_source = g_value_get_enum (value);
1160 case PROP_USER_AGENT:
1161 g_free (rtspsrc->user_agent);
1162 rtspsrc->user_agent = g_value_dup_string (value);
1165 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1171 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1174 GstRTSPSrc *rtspsrc;
1176 rtspsrc = GST_RTSPSRC (object);
1180 g_value_set_string (value, rtspsrc->conninfo.location);
1182 case PROP_PROTOCOLS:
1183 g_value_set_flags (value, rtspsrc->protocols);
1186 g_value_set_boolean (value, rtspsrc->debug);
1189 g_value_set_uint (value, rtspsrc->retry);
1192 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1194 case PROP_TCP_TIMEOUT:
1198 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1199 rtspsrc->tcp_timeout.tv_usec;
1200 g_value_set_uint64 (value, timeout);
1204 g_value_set_uint (value, rtspsrc->latency);
1206 case PROP_DROP_ON_LATENCY:
1207 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1209 case PROP_CONNECTION_SPEED:
1210 g_value_set_uint64 (value, rtspsrc->connection_speed);
1212 case PROP_NAT_METHOD:
1213 g_value_set_enum (value, rtspsrc->nat_method);
1216 g_value_set_boolean (value, rtspsrc->do_rtcp);
1218 case PROP_DO_RTSP_KEEP_ALIVE:
1219 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1225 if (rtspsrc->proxy_host) {
1227 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1231 g_value_take_string (value, str);
1235 g_value_set_string (value, rtspsrc->prop_proxy_id);
1238 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1240 case PROP_RTP_BLOCKSIZE:
1241 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1244 g_value_set_string (value, rtspsrc->user_id);
1247 g_value_set_string (value, rtspsrc->user_pw);
1249 case PROP_BUFFER_MODE:
1250 g_value_set_enum (value, rtspsrc->buffer_mode);
1252 case PROP_PORT_RANGE:
1256 if (rtspsrc->client_port_range.min != 0) {
1257 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1258 rtspsrc->client_port_range.max);
1262 g_value_take_string (value, str);
1265 case PROP_UDP_BUFFER_SIZE:
1266 g_value_set_int (value, rtspsrc->udp_buffer_size);
1268 case PROP_SHORT_HEADER:
1269 g_value_set_boolean (value, rtspsrc->short_header);
1271 case PROP_PROBATION:
1272 g_value_set_uint (value, rtspsrc->probation);
1274 case PROP_UDP_RECONNECT:
1275 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1277 case PROP_MULTICAST_IFACE:
1278 g_value_set_string (value, rtspsrc->multi_iface);
1281 g_value_set_boolean (value, rtspsrc->ntp_sync);
1283 case PROP_USE_PIPELINE_CLOCK:
1284 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1287 g_value_set_boxed (value, rtspsrc->sdes);
1289 case PROP_TLS_VALIDATION_FLAGS:
1290 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1292 case PROP_TLS_DATABASE:
1293 g_value_set_object (value, rtspsrc->tls_database);
1295 case PROP_TLS_INTERACTION:
1296 g_value_set_object (value, rtspsrc->tls_interaction);
1298 case PROP_DO_RETRANSMISSION:
1299 g_value_set_boolean (value, rtspsrc->do_retransmission);
1301 case PROP_NTP_TIME_SOURCE:
1302 g_value_set_enum (value, rtspsrc->ntp_time_source);
1304 case PROP_USER_AGENT:
1305 g_value_set_string (value, rtspsrc->user_agent);
1308 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1314 find_stream_by_id (GstRTSPStream * stream, gint * id)
1316 if (stream->id == *id)
1323 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1325 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1332 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1334 GstElement *src = (GstElement *) a;
1336 if (stream->udpsrc[0] == src)
1338 if (stream->udpsrc[1] == src)
1345 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1347 if (stream->conninfo.location) {
1348 /* check qualified setup_url */
1349 if (!strcmp (stream->conninfo.location, (gchar *) a))
1352 if (stream->control_url) {
1353 /* check original control_url */
1354 if (!strcmp (stream->control_url, (gchar *) a))
1357 /* check if qualified setup_url ends with string */
1358 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1365 static GstRTSPStream *
1366 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1370 /* find and get stream */
1371 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1372 return (GstRTSPStream *) lstream->data;
1377 static const GstSDPBandwidth *
1378 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1379 const GstSDPMedia * media, const gchar * type)
1383 /* first look in the media specific section */
1384 len = gst_sdp_media_bandwidths_len (media);
1385 for (i = 0; i < len; i++) {
1386 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1388 if (strcmp (bw->bwtype, type) == 0)
1391 /* then look in the message specific section */
1392 len = gst_sdp_message_bandwidths_len (sdp);
1393 for (i = 0; i < len; i++) {
1394 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1396 if (strcmp (bw->bwtype, type) == 0)
1403 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1404 const GstSDPMedia * media, GstRTSPStream * stream)
1406 const GstSDPBandwidth *bw;
1408 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1409 stream->as_bandwidth = bw->bandwidth;
1411 stream->as_bandwidth = -1;
1413 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1414 stream->rr_bandwidth = bw->bandwidth;
1416 stream->rr_bandwidth = -1;
1418 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1419 stream->rs_bandwidth = bw->bandwidth;
1421 stream->rs_bandwidth = -1;
1425 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1426 const GstSDPConnection * conn)
1428 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1431 if (conn->addrtype == NULL)
1434 /* check for IPV6 */
1435 if (strcmp (conn->addrtype, "IP4") == 0)
1436 stream->is_ipv6 = FALSE;
1437 else if (strcmp (conn->addrtype, "IP6") == 0)
1438 stream->is_ipv6 = TRUE;
1443 g_free (stream->destination);
1444 stream->destination = g_strdup (conn->address);
1446 /* check for multicast */
1447 stream->is_multicast =
1448 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1450 stream->ttl = conn->ttl;
1453 /* Go over the connections for a stream.
1454 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1456 * - If we are dealing with a localhost address, we disable multicast
1459 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1460 const GstSDPMedia * media, GstRTSPStream * stream)
1462 const GstSDPConnection *conn;
1465 /* first look in the media specific section */
1466 len = gst_sdp_media_connections_len (media);
1467 for (i = 0; i < len; i++) {
1468 conn = gst_sdp_media_get_connection (media, i);
1470 gst_rtspsrc_do_stream_connection (src, stream, conn);
1472 /* then look in the message specific section */
1473 if ((conn = gst_sdp_message_get_connection (sdp))) {
1474 gst_rtspsrc_do_stream_connection (src, stream, conn);
1478 /* m=<media> <UDP port> RTP/AVP <payload>
1481 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
1482 const GstSDPMedia * media, GstRTSPStream * stream)
1488 proto = gst_sdp_media_get_proto (media);
1492 if (g_str_equal (proto, "RTP/AVP"))
1493 stream->profile = GST_RTSP_PROFILE_AVP;
1494 else if (g_str_equal (proto, "RTP/SAVP"))
1495 stream->profile = GST_RTSP_PROFILE_SAVP;
1496 else if (g_str_equal (proto, "RTP/AVPF"))
1497 stream->profile = GST_RTSP_PROFILE_AVPF;
1498 else if (g_str_equal (proto, "RTP/SAVPF"))
1499 stream->profile = GST_RTSP_PROFILE_SAVPF;
1503 len = gst_sdp_media_formats_len (media);
1504 for (i = 0; i < len; i++) {
1511 pt = atoi (gst_sdp_media_get_format (media, i));
1513 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
1516 caps = gst_rtspsrc_media_to_caps (pt, media);
1518 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
1522 /* do some tweaks */
1523 s = gst_caps_get_structure (caps, 0);
1524 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
1525 stream->is_real = (strstr (enc, "-REAL") != NULL);
1526 if (strcmp (enc, "X-ASF-PF") == 0)
1527 stream->container = TRUE;
1529 GST_DEBUG ("mapping sdp session level attributes to caps");
1530 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, caps);
1531 GST_DEBUG ("mapping sdp media level attributes to caps");
1532 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, caps);
1534 /* the first pt will be the default */
1535 if (stream->ptmap->len == 0)
1536 stream->default_pt = pt;
1540 g_array_append_val (stream->ptmap, item);
1546 GST_ERROR_OBJECT (src, "can't find proto in media");
1551 GST_ERROR_OBJECT (src, "unknown proto in media %s", proto);
1556 static const gchar *
1557 get_aggregate_control (GstRTSPSrc * src)
1562 base = src->control;
1563 else if (src->content_base)
1564 base = src->content_base;
1565 else if (src->conninfo.url_str)
1566 base = src->conninfo.url_str;
1574 clear_ptmap_item (PtMapItem * item)
1577 gst_caps_unref (item->caps);
1580 static GstRTSPStream *
1581 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1583 GstRTSPStream *stream;
1584 const gchar *control_url;
1585 const GstSDPMedia *media;
1587 /* get media, should not return NULL */
1588 media = gst_sdp_message_get_media (sdp, idx);
1592 stream = g_new0 (GstRTSPStream, 1);
1593 stream->parent = src;
1594 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1596 stream->last_ret = GST_FLOW_NOT_LINKED;
1597 stream->added = FALSE;
1598 stream->setup = FALSE;
1599 stream->skipped = FALSE;
1601 stream->eos = FALSE;
1602 stream->discont = TRUE;
1603 stream->seqbase = -1;
1604 stream->timebase = -1;
1605 stream->send_ssrc = g_random_int ();
1606 stream->profile = GST_RTSP_PROFILE_AVP;
1607 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1608 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
1610 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1611 * session manager to scale RTCP. */
1612 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1614 /* collect connection info */
1615 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1617 /* make the payload type map */
1618 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1620 /* collect port number */
1621 stream->port = gst_sdp_media_get_port (media);
1623 /* get control url to construct the setup url. The setup url is used to
1624 * configure the transport of the stream and is used to identity the stream in
1625 * the RTP-Info header field returned from PLAY. */
1626 control_url = gst_sdp_media_get_attribute_val (media, "control");
1627 if (control_url == NULL)
1628 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1630 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1631 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1632 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1633 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1635 if (control_url != NULL) {
1636 stream->control_url = g_strdup (control_url);
1637 /* Build a fully qualified url using the content_base if any or by prefixing
1638 * the original request.
1639 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1640 * likely build a URL that the server will fail to understand, this is ok,
1641 * we will fail then. */
1642 if (g_str_has_prefix (control_url, "rtsp://"))
1643 stream->conninfo.location = g_strdup (control_url);
1648 if (g_strcmp0 (control_url, "*") == 0)
1651 base = get_aggregate_control (src);
1653 /* check if the base ends or control starts with / */
1654 has_slash = g_str_has_prefix (control_url, "/");
1655 has_slash = has_slash || g_str_has_suffix (base, "/");
1657 /* concatenate the two strings, insert / when not present */
1658 stream->conninfo.location =
1659 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1662 GST_DEBUG_OBJECT (src, " setup: %s",
1663 GST_STR_NULL (stream->conninfo.location));
1665 /* we keep track of all streams */
1666 src->streams = g_list_append (src->streams, stream);
1674 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1678 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1680 g_array_free (stream->ptmap, TRUE);
1682 g_free (stream->destination);
1683 g_free (stream->control_url);
1684 g_free (stream->conninfo.location);
1686 for (i = 0; i < 2; i++) {
1687 if (stream->udpsrc[i]) {
1688 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1689 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1690 gst_object_unref (stream->udpsrc[i]);
1692 if (stream->channelpad[i])
1693 gst_object_unref (stream->channelpad[i]);
1695 if (stream->udpsink[i]) {
1696 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1697 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1698 gst_object_unref (stream->udpsink[i]);
1701 if (stream->fakesrc) {
1702 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1703 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1704 gst_object_unref (stream->fakesrc);
1706 if (stream->srcpad) {
1707 gst_pad_set_active (stream->srcpad, FALSE);
1709 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1711 if (stream->srtpenc)
1712 gst_object_unref (stream->srtpenc);
1713 if (stream->srtpdec)
1714 gst_object_unref (stream->srtpdec);
1715 if (stream->srtcpparams)
1716 gst_caps_unref (stream->srtcpparams);
1717 if (stream->rtcppad)
1718 gst_object_unref (stream->rtcppad);
1719 if (stream->session)
1720 g_object_unref (stream->session);
1721 if (stream->rtx_pt_map)
1722 gst_structure_free (stream->rtx_pt_map);
1727 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1731 GST_DEBUG_OBJECT (src, "cleanup");
1733 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1734 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1736 gst_rtspsrc_stream_free (src, stream);
1738 g_list_free (src->streams);
1739 src->streams = NULL;
1741 if (src->manager_sig_id) {
1742 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1743 src->manager_sig_id = 0;
1745 gst_element_set_state (src->manager, GST_STATE_NULL);
1746 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1747 src->manager = NULL;
1750 gst_structure_free (src->props);
1753 g_free (src->content_base);
1754 src->content_base = NULL;
1756 g_free (src->control);
1757 src->control = NULL;
1760 gst_rtsp_range_free (src->range);
1763 /* don't clear the SDP when it was used in the url */
1764 if (src->sdp && !src->from_sdp) {
1765 gst_sdp_message_free (src->sdp);
1769 src->need_segment = FALSE;
1771 if (src->provided_clock) {
1772 gst_object_unref (src->provided_clock);
1773 src->provided_clock = NULL;
1777 #define PARSE_INT(p, del, res) \
1780 p = strstr (p, del); \
1790 #define PARSE_STRING(p, del, res) \
1793 p = strstr (p, del); \
1805 #define SKIP_SPACES(p) \
1806 while (*p && g_ascii_isspace (*p)) \
1811 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1814 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1815 gint * rate, gchar ** params)
1819 p = (gchar *) rtpmap;
1821 PARSE_INT (p, " ", *payload);
1829 PARSE_STRING (p, "/", *name);
1830 if (*name == NULL) {
1831 GST_DEBUG ("no rate, name %s", p);
1832 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1833 * streams seem to omit the rate. */
1840 p = strstr (p, "/");
1858 parse_keymgmt (const gchar * keymgmt, GstCaps * caps)
1860 gboolean res = FALSE;
1864 GstMIKEYMessage *msg;
1865 const GstMIKEYPayload *payload;
1866 const gchar *srtp_cipher;
1867 const gchar *srtp_auth;
1869 p = (gchar *) keymgmt;
1875 PARSE_STRING (p, " ", kmpid);
1876 if (!g_str_equal (kmpid, "mikey"))
1879 data = g_base64_decode (p, &size);
1883 msg = gst_mikey_message_new_from_data (data, size, NULL, NULL);
1888 srtp_cipher = "aes-128-icm";
1889 srtp_auth = "hmac-sha1-80";
1891 /* check the Security policy if any */
1892 if ((payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, 0))) {
1893 GstMIKEYPayloadSP *p = (GstMIKEYPayloadSP *) payload;
1896 if (p->proto != GST_MIKEY_SEC_PROTO_SRTP)
1899 len = gst_mikey_payload_sp_get_n_params (payload);
1900 for (i = 0; i < len; i++) {
1901 const GstMIKEYPayloadSPParam *param =
1902 gst_mikey_payload_sp_get_param (payload, i);
1904 switch (param->type) {
1905 case GST_MIKEY_SP_SRTP_ENC_ALG:
1906 switch (param->val[0]) {
1908 srtp_cipher = "null";
1912 srtp_cipher = "aes-128-icm";
1918 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
1919 switch (param->val[0]) {
1920 case AES_128_KEY_LEN:
1921 srtp_cipher = "aes-128-icm";
1923 case AES_256_KEY_LEN:
1924 srtp_cipher = "aes-256-icm";
1930 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1931 switch (param->val[0]) {
1937 srtp_auth = "hmac-sha1-80";
1943 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
1944 switch (param->val[0]) {
1945 case HMAC_32_KEY_LEN:
1946 srtp_auth = "hmac-sha1-32";
1948 case HMAC_80_KEY_LEN:
1949 srtp_auth = "hmac-sha1-80";
1955 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1957 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1965 if (!(payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_KEMAC, 0)))
1968 GstMIKEYPayloadKEMAC *p = (GstMIKEYPayloadKEMAC *) payload;
1969 const GstMIKEYPayload *sub;
1970 GstMIKEYPayloadKeyData *pkd;
1973 if (p->enc_alg != GST_MIKEY_ENC_NULL || p->mac_alg != GST_MIKEY_MAC_NULL)
1976 if (!(sub = gst_mikey_payload_kemac_get_sub (payload, 0)))
1979 if (sub->type != GST_MIKEY_PT_KEY_DATA)
1982 pkd = (GstMIKEYPayloadKeyData *) sub;
1984 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
1986 gst_caps_set_simple (caps, "srtp-key", GST_TYPE_BUFFER, buf, NULL);
1989 gst_caps_set_simple (caps,
1990 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1991 "srtp-auth", G_TYPE_STRING, srtp_auth,
1992 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1993 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1997 gst_mikey_message_unref (msg);
2003 * Mapping SDP attributes to caps
2005 * prepend 'a-' to IANA registered sdp attributes names
2006 * (ie: not prefixed with 'x-') in order to avoid
2007 * collision with gstreamer standard caps properties names
2010 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
2012 if (attributes->len > 0) {
2016 s = gst_caps_get_structure (caps, 0);
2018 for (i = 0; i < attributes->len; i++) {
2019 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
2020 gchar *tofree, *key;
2024 /* skip some of the attribute we already handle */
2025 if (!strcmp (key, "fmtp"))
2027 if (!strcmp (key, "rtpmap"))
2029 if (!strcmp (key, "control"))
2031 if (!strcmp (key, "range"))
2033 if (!strcmp (key, "framesize"))
2035 if (g_str_equal (key, "key-mgmt")) {
2036 parse_keymgmt (attr->value, caps);
2040 /* string must be valid UTF8 */
2041 if (!g_utf8_validate (attr->value, -1, NULL))
2044 if (!g_str_has_prefix (key, "x-"))
2045 tofree = key = g_strdup_printf ("a-%s", key);
2049 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
2050 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
2056 static const gchar *
2057 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
2066 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
2069 if (sscanf (attr, "%d ", &val) != 1)
2079 * Mapping of caps to and from SDP fields:
2081 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
2082 * a=framesize:<payload> <width>-<height>
2083 * a=fmtp:<payload> <param>[=<value>];...
2086 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
2089 const gchar *rtpmap;
2091 const gchar *framesize;
2094 gchar *params = NULL;
2100 /* get and parse rtpmap */
2101 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
2104 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
2106 g_warning ("error parsing rtpmap, ignoring");
2110 /* dynamic payloads need rtpmap or we fail */
2111 if (rtpmap == NULL && pt >= 96)
2114 /* check if we have a rate, if not, we need to look up the rate from the
2115 * default rates based on the payload types. */
2117 const GstRTPPayloadInfo *info;
2119 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
2120 /* dynamic types, use media and encoding_name */
2121 tmp = g_ascii_strdown (media->media, -1);
2122 info = gst_rtp_payload_info_for_name (tmp, name);
2125 /* static types, use payload type */
2126 info = gst_rtp_payload_info_for_pt (pt);
2130 if ((rate = info->clock_rate) == 0)
2133 /* we fail if we cannot find one */
2138 tmp = g_ascii_strdown (media->media, -1);
2139 caps = gst_caps_new_simple ("application/x-unknown",
2140 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
2142 s = gst_caps_get_structure (caps, 0);
2144 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
2146 /* encoding name must be upper case */
2148 tmp = g_ascii_strup (name, -1);
2149 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
2153 /* params must be lower case */
2154 if (params != NULL) {
2155 tmp = g_ascii_strdown (params, -1);
2156 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
2160 /* parse optional fmtp: field */
2161 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
2167 /* p is now of the format <payload> <param>[=<value>];... */
2168 PARSE_INT (p, " ", payload);
2169 if (payload != -1 && payload == pt) {
2173 /* <param>[=<value>] are separated with ';' */
2174 pairs = g_strsplit (p, ";", 0);
2175 for (i = 0; pairs[i]; i++) {
2177 const gchar *val, *key;
2179 const gchar *reserved_keys[] =
2180 { "media", "payload", "clock-rate", "encoding-name",
2184 /* the key may not have a '=', the value can have other '='s */
2185 valpos = strstr (pairs[i], "=");
2187 /* we have a '=' and thus a value, remove the '=' with \0 */
2189 /* value is everything between '=' and ';'. We split the pairs at ;
2190 * boundaries so we can take the remainder of the value. Some servers
2191 * put spaces around the value which we strip off here. Alternatively
2192 * we could strip those spaces in the depayloaders should these spaces
2193 * actually carry any meaning in the future. */
2194 val = g_strstrip (valpos + 1);
2196 /* simple <param>;.. is translated into <param>=1;... */
2199 /* strip the key of spaces, convert key to lowercase but not the value. */
2200 key = g_strstrip (pairs[i]);
2202 /* skip keys from the fmtp, which we already use ourselves for the
2203 * caps. Some software is adding random things like clock-rate into
2204 * the fmtp, and we would otherwise here set a string-typed clock-rate
2205 * in the caps... and thus fail to create valid RTP caps
2207 for (j = 0; j < G_N_ELEMENTS (reserved_keys); j++) {
2208 if (g_ascii_strcasecmp (reserved_keys[j], key) == 0) {
2214 if (strlen (key) > 1) {
2215 tmp = g_ascii_strdown (key, -1);
2216 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
2224 /* parse framesize: field */
2225 if ((framesize = gst_sdp_media_get_attribute_val (media, "framesize"))) {
2228 /* p is now of the format <payload> <width>-<height> */
2229 p = (gchar *) framesize;
2231 PARSE_INT (p, " ", payload);
2232 if (payload != -1 && payload == pt) {
2233 gst_structure_set (s, "a-framesize", G_TYPE_STRING, p, NULL);
2241 g_warning ("rtpmap type not given for dynamic payload %d", pt);
2246 g_warning ("rate unknown for payload type %d", pt);
2252 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
2253 gint * rtpport, gint * rtcpport)
2256 GstStateChangeReturn ret;
2257 GstElement *udpsrc0, *udpsrc1;
2258 gint tmp_rtp, tmp_rtcp;
2262 src = stream->parent;
2268 /* Start at next port */
2269 tmp_rtp = src->next_port_num;
2271 if (stream->is_ipv6)
2272 host = "udp://[::0]";
2274 host = "udp://0.0.0.0";
2276 /* try to allocate 2 UDP ports, the RTP port should be an even
2277 * number and the RTCP port should be the next (uneven) port */
2280 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
2281 tmp_rtp >= src->client_port_range.max)
2284 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2285 if (udpsrc0 == NULL)
2286 goto no_udp_protocol;
2287 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
2289 if (src->udp_buffer_size != 0)
2290 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
2293 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
2294 if (ret == GST_STATE_CHANGE_FAILURE) {
2296 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
2299 if (++count > src->retry)
2302 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2303 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2304 gst_object_unref (udpsrc0);
2307 GST_DEBUG_OBJECT (src, "retry %d", count);
2310 goto no_udp_protocol;
2313 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
2314 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
2316 /* check if port is even */
2317 if ((tmp_rtp & 0x01) != 0) {
2318 /* port not even, close and allocate another */
2319 if (++count > src->retry)
2322 GST_DEBUG_OBJECT (src, "RTP port not even");
2324 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2325 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2326 gst_object_unref (udpsrc0);
2329 GST_DEBUG_OBJECT (src, "retry %d", count);
2334 /* allocate port+1 for RTCP now */
2335 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2336 if (udpsrc1 == NULL)
2337 goto no_udp_rtcp_protocol;
2340 tmp_rtcp = tmp_rtp + 1;
2341 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
2344 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
2346 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
2347 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
2348 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
2349 if (ret == GST_STATE_CHANGE_FAILURE) {
2350 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
2352 if (++count > src->retry)
2355 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2356 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2357 gst_object_unref (udpsrc0);
2360 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2361 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2362 gst_object_unref (udpsrc1);
2366 GST_DEBUG_OBJECT (src, "retry %d", count);
2370 /* all fine, do port check */
2371 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2372 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2374 /* this should not happen... */
2375 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2378 /* we keep these elements, we configure all in configure_transport when the
2379 * server told us to really use the UDP ports. */
2380 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2381 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2382 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2383 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2385 /* keep track of next available port number when we have a range
2387 if (src->next_port_num != 0)
2388 src->next_port_num = tmp_rtcp + 1;
2395 GST_DEBUG_OBJECT (src, "could not get UDP source");
2400 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2404 no_udp_rtcp_protocol:
2406 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2411 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2412 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2418 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2419 gst_object_unref (udpsrc0);
2422 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2423 gst_object_unref (udpsrc1);
2430 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2435 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2437 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2438 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2441 for (i = 0; i < 2; i++) {
2442 if (stream->udpsrc[i])
2443 gst_element_set_state (stream->udpsrc[i], state);
2449 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2456 event = gst_event_new_flush_start ();
2457 GST_DEBUG_OBJECT (src, "start flush");
2459 state = GST_STATE_PAUSED;
2461 event = gst_event_new_flush_stop (FALSE);
2462 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2465 state = GST_STATE_PLAYING;
2467 state = GST_STATE_PAUSED;
2469 gst_rtspsrc_push_event (src, event);
2470 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2471 gst_rtspsrc_set_state (src, state);
2474 static GstRTSPResult
2475 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2476 GstRTSPMessage * message, GTimeVal * timeout)
2481 ret = gst_rtsp_connection_send (conn, message, timeout);
2483 ret = GST_RTSP_ERROR;
2488 static GstRTSPResult
2489 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2490 GstRTSPMessage * message, GTimeVal * timeout)
2495 ret = gst_rtsp_connection_receive (conn, message, timeout);
2497 ret = GST_RTSP_ERROR;
2503 gst_rtspsrc_get_position (GstRTSPSrc * src)
2508 query = gst_query_new_position (GST_FORMAT_TIME);
2509 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2510 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2511 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2515 if (stream->srcpad) {
2516 if (gst_pad_query (stream->srcpad, query)) {
2517 gst_query_parse_position (query, &fmt, &pos);
2518 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2519 GST_TIME_ARGS (pos));
2520 src->last_pos = pos;
2530 gst_query_unref (query);
2534 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2539 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2541 gboolean flush, skip;
2544 GstSegment seeksegment = { 0, };
2548 GST_DEBUG_OBJECT (src, "doing seek with event");
2550 gst_event_parse_seek (event, &rate, &format, &flags,
2551 &cur_type, &cur, &stop_type, &stop);
2553 /* no negative rates yet */
2557 /* we need TIME format */
2558 if (format != src->segment.format)
2561 GST_DEBUG_OBJECT (src, "doing seek without event");
2563 cur_type = GST_SEEK_TYPE_SET;
2564 stop_type = GST_SEEK_TYPE_SET;
2567 /* get flush flag */
2568 flush = flags & GST_SEEK_FLAG_FLUSH;
2569 skip = flags & GST_SEEK_FLAG_SKIP;
2571 /* now we need to make sure the streaming thread is stopped. We do this by
2572 * either sending a FLUSH_START event downstream which will cause the
2573 * streaming thread to stop with a WRONG_STATE.
2574 * For a non-flushing seek we simply pause the task, which will happen as soon
2575 * as it completes one iteration (and thus might block when the sink is
2576 * blocking in preroll). */
2578 GST_DEBUG_OBJECT (src, "starting flush");
2579 gst_rtspsrc_flush (src, TRUE, FALSE);
2582 gst_task_pause (src->task);
2586 /* we should now be able to grab the streaming thread because we stopped it
2587 * with the above flush/pause code */
2588 GST_RTSP_STREAM_LOCK (src);
2590 GST_DEBUG_OBJECT (src, "stopped streaming");
2592 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2593 gst_rtspsrc_connection_flush (src, FALSE);
2595 /* copy segment, we need this because we still need the old
2596 * segment when we close the current segment. */
2597 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2599 /* configure the seek parameters in the seeksegment. We will then have the
2600 * right values in the segment to perform the seek */
2602 GST_DEBUG_OBJECT (src, "configuring seek");
2603 gst_segment_do_seek (&seeksegment, rate, format, flags,
2604 cur_type, cur, stop_type, stop, &update);
2607 /* figure out the last position we need to play. If it's configured (stop !=
2608 * -1), use that, else we play until the total duration of the file */
2609 if ((stop = seeksegment.stop) == -1)
2610 stop = seeksegment.duration;
2612 playing = (src->state == GST_RTSP_STATE_PLAYING);
2614 /* if we were playing, pause first */
2616 /* obtain current position in case seek fails */
2617 gst_rtspsrc_get_position (src);
2618 gst_rtspsrc_pause (src, FALSE);
2622 src->state = GST_RTSP_STATE_SEEKING;
2624 /* PLAY will add the range header now. */
2625 src->need_range = TRUE;
2627 /* and continue playing */
2629 gst_rtspsrc_play (src, &seeksegment, FALSE);
2631 /* prepare for streaming again */
2633 /* if we started flush, we stop now */
2634 GST_DEBUG_OBJECT (src, "stopping flush");
2635 gst_rtspsrc_flush (src, FALSE, playing);
2638 /* now we did the seek and can activate the new segment values */
2639 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2641 /* if we're doing a segment seek, post a SEGMENT_START message */
2642 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2643 gst_element_post_message (GST_ELEMENT_CAST (src),
2644 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2645 src->segment.format, src->segment.position));
2648 /* now create the newsegment */
2649 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2650 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2653 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2654 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2655 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2656 stream->discont = TRUE;
2659 GST_RTSP_STREAM_UNLOCK (src);
2666 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2671 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2677 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2681 gboolean res = TRUE;
2684 src = GST_RTSPSRC_CAST (parent);
2686 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2687 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2689 switch (GST_EVENT_TYPE (event)) {
2690 case GST_EVENT_SEEK:
2691 res = gst_rtspsrc_perform_seek (src, event);
2695 case GST_EVENT_NAVIGATION:
2696 case GST_EVENT_LATENCY:
2704 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2705 res = gst_pad_send_event (target, event);
2706 gst_object_unref (target);
2708 gst_event_unref (event);
2711 gst_event_unref (event);
2717 /* this is the final event function we receive on the internal source pad when
2718 * we deal with TCP connections */
2720 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2725 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2727 switch (GST_EVENT_TYPE (event)) {
2728 case GST_EVENT_SEEK:
2730 case GST_EVENT_NAVIGATION:
2731 case GST_EVENT_LATENCY:
2733 gst_event_unref (event);
2740 /* this is the final query function we receive on the internal source pad when
2741 * we deal with TCP connections */
2743 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2747 gboolean res = TRUE;
2749 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2751 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2752 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2754 switch (GST_QUERY_TYPE (query)) {
2755 case GST_QUERY_POSITION:
2760 case GST_QUERY_DURATION:
2764 gst_query_parse_duration (query, &format, NULL);
2767 case GST_FORMAT_TIME:
2768 gst_query_set_duration (query, format, src->segment.duration);
2776 case GST_QUERY_LATENCY:
2778 /* we are live with a min latency of 0 and unlimited max latency, this
2779 * result will be updated by the session manager if there is any. */
2780 gst_query_set_latency (query, TRUE, 0, -1);
2790 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2792 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2796 gboolean res = FALSE;
2798 src = GST_RTSPSRC_CAST (parent);
2800 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2801 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2803 switch (GST_QUERY_TYPE (query)) {
2804 case GST_QUERY_DURATION:
2808 gst_query_parse_duration (query, &format, NULL);
2811 case GST_FORMAT_TIME:
2812 gst_query_set_duration (query, format, src->segment.duration);
2820 case GST_QUERY_SEEKING:
2824 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2825 if (format == GST_FORMAT_TIME) {
2827 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2829 /* seeking without duration is unlikely */
2830 seekable = seekable && src->seekable && src->segment.duration &&
2831 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2833 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0,
2834 src->segment.duration);
2843 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2845 gst_query_set_uri (query, uri);
2853 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2855 /* forward the query to the proxy target pad */
2857 res = gst_pad_query (target, query);
2858 gst_object_unref (target);
2867 /* callback for RTCP messages to be sent to the server when operating in TCP
2869 static GstFlowReturn
2870 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2873 GstRTSPStream *stream;
2874 GstFlowReturn res = GST_FLOW_OK;
2879 GstRTSPMessage message = { 0 };
2880 GstRTSPConnection *conn;
2882 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2883 src = stream->parent;
2885 gst_buffer_map (buffer, &map, GST_MAP_READ);
2889 gst_rtsp_message_init_data (&message, stream->channel[1]);
2891 /* lend the body data to the message */
2892 gst_rtsp_message_take_body (&message, data, size);
2894 if (stream->conninfo.connection)
2895 conn = stream->conninfo.connection;
2897 conn = src->conninfo.connection;
2899 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2900 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2901 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2903 /* and steal it away again because we will free it when unreffing the
2905 gst_rtsp_message_steal_body (&message, &data, &size);
2906 gst_rtsp_message_unset (&message);
2908 gst_buffer_unmap (buffer, &map);
2909 gst_buffer_unref (buffer);
2914 static GstPadProbeReturn
2915 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2917 GstRTSPSrc *src = user_data;
2919 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2920 GST_DEBUG_PAD_NAME (pad));
2922 /* activate the streams */
2923 GST_OBJECT_LOCK (src);
2924 if (!src->need_activate)
2927 src->need_activate = FALSE;
2928 GST_OBJECT_UNLOCK (src);
2930 gst_rtspsrc_activate_streams (src);
2932 return GST_PAD_PROBE_OK;
2936 GST_OBJECT_UNLOCK (src);
2937 return GST_PAD_PROBE_OK;
2942 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
2944 GstPad *gpad = GST_PAD_CAST (user_data);
2946 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
2947 gst_pad_store_sticky_event (gpad, *event);
2952 /* this callback is called when the session manager generated a new src pad with
2953 * payloaded RTP packets. We simply ghost the pad here. */
2955 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2958 GstPadTemplate *template;
2961 GstRTSPStream *stream;
2964 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2966 GST_RTSP_STATE_LOCK (src);
2968 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2969 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2970 goto unknown_stream;
2972 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2974 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2976 goto unknown_stream;
2979 stream->ssrc = ssrc;
2981 /* we'll add it later see below */
2982 stream->added = TRUE;
2984 /* check if we added all streams */
2986 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2987 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2989 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
2990 ostream, ostream->container, ostream->added, ostream->setup);
2992 /* if we find a stream for which we did a setup that is not added, we
2993 * need to wait some more */
2994 if (ostream->setup && !ostream->added) {
2999 GST_RTSP_STATE_UNLOCK (src);
3001 /* create a new pad we will use to stream to */
3002 template = gst_static_pad_template_get (&rtptemplate);
3003 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
3004 gst_object_unref (template);
3007 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3008 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3009 gst_pad_set_active (stream->srcpad, TRUE);
3010 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
3011 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3014 GST_DEBUG_OBJECT (src, "We added all streams");
3015 /* when we get here, all stream are added and we can fire the no-more-pads
3017 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
3025 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
3026 GST_RTSP_STATE_UNLOCK (src);
3033 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
3037 len = stream->ptmap->len;
3038 for (i = 0; i < len; i++) {
3039 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3047 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
3049 GstRTSPStream *stream;
3052 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
3054 GST_RTSP_STATE_LOCK (src);
3055 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3057 goto unknown_stream;
3059 if ((caps = stream_get_caps_for_pt (stream, pt)))
3060 gst_caps_ref (caps);
3061 GST_RTSP_STATE_UNLOCK (src);
3067 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
3068 GST_RTSP_STATE_UNLOCK (src);
3074 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
3076 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
3082 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
3088 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
3094 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
3096 GstRTSPSrc *src = stream->parent;
3099 g_object_get (source, "ssrc", &ssrc, NULL);
3101 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
3102 ssrc, stream->ssrc, stream->id);
3104 if (ssrc == stream->ssrc)
3105 gst_rtspsrc_do_stream_eos (src, stream);
3109 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
3111 GstRTSPSrc *src = stream->parent;
3114 g_object_get (source, "ssrc", &ssrc, NULL);
3116 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
3117 ssrc, stream->ssrc, stream->id);
3119 if (ssrc == stream->ssrc)
3120 gst_rtspsrc_do_stream_eos (src, stream);
3124 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
3126 GstRTSPStream *stream;
3128 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
3130 /* get stream for session */
3131 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3133 gst_rtspsrc_do_stream_eos (src, stream);
3138 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
3140 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
3145 set_manager_buffer_mode (GstRTSPSrc * src)
3147 GObjectClass *klass;
3149 if (src->manager == NULL)
3152 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3154 if (!g_object_class_find_property (klass, "buffer-mode"))
3157 if (src->buffer_mode != BUFFER_MODE_AUTO) {
3158 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
3163 GST_DEBUG_OBJECT (src,
3164 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
3166 if (src->provided_clock) {
3167 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
3169 if (clock == src->provided_clock) {
3170 GST_DEBUG_OBJECT (src, "selected synced");
3171 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
3174 gst_object_unref (clock);
3179 /* Otherwise fall-through and use another buffer mode */
3181 gst_object_unref (clock);
3184 GST_DEBUG_OBJECT (src, "auto buffering mode");
3185 if (src->use_buffering) {
3186 GST_DEBUG_OBJECT (src, "selected buffer");
3187 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
3189 GST_DEBUG_OBJECT (src, "selected slave");
3190 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
3195 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
3197 GST_DEBUG ("request key %u", ssrc);
3198 return gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
3202 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
3204 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3205 if (stream->id != session)
3208 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3209 stream->profile != GST_RTSP_PROFILE_SAVPF)
3212 if (stream->srtpdec == NULL) {
3215 name = g_strdup_printf ("srtpdec_%u", session);
3216 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
3219 g_signal_connect (stream->srtpdec, "request-key",
3220 (GCallback) request_key, stream);
3222 return gst_object_ref (stream->srtpdec);
3226 request_rtcp_encoder (GstElement * rtpbin, guint session,
3227 GstRTSPStream * stream)
3232 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3233 if (stream->id != session)
3236 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3237 stream->profile != GST_RTSP_PROFILE_SAVPF)
3240 if (stream->srtpenc == NULL) {
3243 name = g_strdup_printf ("srtpenc_%u", session);
3244 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
3247 /* get RTCP crypto parameters from caps */
3248 s = gst_caps_get_structure (stream->srtcpparams, 0);
3252 GType ciphertype, authtype;
3253 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
3255 ciphertype = g_type_from_name ("GstSrtpCipherType");
3256 authtype = g_type_from_name ("GstSrtpAuthType");
3257 g_value_init (&rtcp_cipher, ciphertype);
3258 g_value_init (&rtcp_auth, authtype);
3260 str = gst_structure_get_string (s, "srtcp-cipher");
3261 gst_value_deserialize (&rtcp_cipher, str);
3262 str = gst_structure_get_string (s, "srtcp-auth");
3263 gst_value_deserialize (&rtcp_auth, str);
3264 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
3266 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
3268 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
3270 g_object_set (stream->srtpenc, "key", buf, NULL);
3272 g_value_unset (&rtcp_cipher);
3273 g_value_unset (&rtcp_auth);
3274 gst_buffer_unref (buf);
3277 name = g_strdup_printf ("rtcp_sink_%d", session);
3278 pad = gst_element_get_request_pad (stream->srtpenc, name);
3280 gst_object_unref (pad);
3282 return gst_object_ref (stream->srtpenc);
3286 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
3288 GstElement *rtx, *bin;
3291 GstRTSPStream *stream;
3293 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
3295 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
3299 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
3300 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
3301 bin = gst_bin_new (NULL);
3302 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
3303 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
3304 gst_bin_add (GST_BIN (bin), rtx);
3306 pad = gst_element_get_static_pad (rtx, "src");
3307 name = g_strdup_printf ("src_%u", sessid);
3308 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3310 gst_object_unref (pad);
3312 pad = gst_element_get_static_pad (rtx, "sink");
3313 name = g_strdup_printf ("sink_%u", sessid);
3314 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3316 gst_object_unref (pad);
3322 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
3326 gboolean do_retransmission = FALSE;
3328 if (transport->trans != GST_RTSP_TRANS_RTP)
3330 if (transport->profile != GST_RTSP_PROFILE_AVPF &&
3331 transport->profile != GST_RTSP_PROFILE_SAVPF)
3334 signal_id = g_signal_lookup ("request-aux-receiver",
3335 G_OBJECT_TYPE (src->manager));
3336 /* there's already something connected */
3337 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
3338 NULL, NULL, NULL) != 0) {
3339 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
3340 "\"request-aux-receiver\" signal is "
3341 "already used by the application");
3345 /* build the retransmission payload type map */
3346 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3347 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3348 gboolean do_retransmission_stream = FALSE;
3351 if (stream->rtx_pt_map)
3352 gst_structure_free (stream->rtx_pt_map);
3353 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
3355 for (i = 0; i < stream->ptmap->len; i++) {
3356 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3357 GstStructure *s = gst_caps_get_structure (item->caps, 0);
3358 const gchar *encoding;
3360 /* we only care about RTX streams */
3361 if ((encoding = gst_structure_get_string (s, "encoding-name"))
3362 && g_strcmp0 (encoding, "RTX") == 0) {
3363 const gchar *stream_pt_s;
3366 if (gst_structure_get_int (s, "payload", &rtx_pt)
3367 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
3370 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
3372 do_retransmission_stream = TRUE;
3378 if (do_retransmission_stream) {
3379 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
3380 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
3381 do_retransmission = TRUE;
3383 GST_DEBUG_OBJECT (src, "no retransmission payload map for stream "
3384 "id %i", stream->id);
3385 gst_structure_free (stream->rtx_pt_map);
3386 stream->rtx_pt_map = NULL;
3390 if (do_retransmission) {
3391 GST_DEBUG_OBJECT (src, "Enabling retransmissions");
3393 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
3395 /* enable RFC4588 retransmission handling by setting rtprtxreceive
3396 * as the "aux" element of rtpbin */
3397 g_signal_connect (src->manager, "request-aux-receiver",
3398 (GCallback) request_aux_receiver, src);
3400 GST_DEBUG_OBJECT (src,
3401 "Not enabling retransmissions as no stream had a retransmission payload map");
3405 /* try to get and configure a manager */
3407 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3408 GstRTSPTransport * transport)
3410 const gchar *manager;
3412 GstStateChangeReturn ret;
3414 /* find a manager */
3415 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3419 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3421 /* configure the manager */
3422 if (src->manager == NULL) {
3423 GObjectClass *klass;
3425 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3427 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3431 goto use_no_manager;
3433 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3434 goto manager_failed;
3437 /* we manage this element */
3438 gst_element_set_locked_state (src->manager, TRUE);
3439 gst_bin_add (GST_BIN_CAST (src), src->manager);
3441 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3442 if (ret == GST_STATE_CHANGE_FAILURE)
3443 goto start_manager_failure;
3445 g_object_set (src->manager, "latency", src->latency, NULL);
3447 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3449 if (g_object_class_find_property (klass, "ntp-sync")) {
3450 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3453 if (src->use_pipeline_clock) {
3454 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3455 g_object_set (src->manager, "use-pipeline-clock", TRUE, NULL);
3458 if (g_object_class_find_property (klass, "ntp-time-source")) {
3459 g_object_set (src->manager, "ntp-time-source", src->ntp_time_source,
3464 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3465 g_object_set (src->manager, "sdes", src->sdes, NULL);
3468 if (g_object_class_find_property (klass, "drop-on-latency")) {
3469 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3473 /* buffer mode pauses are handled by adding offsets to buffer times,
3474 * but some depayloaders may have a hard time syncing output times
3475 * with such input times, e.g. container ones, most notably ASF */
3476 /* TODO alternatives are having an event that indicates these shifts,
3477 * or having rtsp extensions provide suggestion on buffer mode */
3478 /* valid duration implies not likely live pipeline,
3479 * so slaving in jitterbuffer does not make much sense
3480 * (and might mess things up due to bursts) */
3481 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3482 src->segment.duration && stream->container) {
3483 src->use_buffering = TRUE;
3485 src->use_buffering = FALSE;
3488 set_manager_buffer_mode (src);
3490 /* connect to signals */
3491 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3493 src->manager_sig_id =
3494 g_signal_connect (src->manager, "pad-added",
3495 (GCallback) new_manager_pad, src);
3496 src->manager_ptmap_id =
3497 g_signal_connect (src->manager, "request-pt-map",
3498 (GCallback) request_pt_map, src);
3500 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3503 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3506 if (src->do_retransmission)
3507 add_retransmission (src, transport);
3509 g_signal_connect (src->manager, "request-rtp-decoder",
3510 (GCallback) request_rtp_decoder, stream);
3511 g_signal_connect (src->manager, "request-rtcp-decoder",
3512 (GCallback) request_rtp_decoder, stream);
3513 g_signal_connect (src->manager, "request-rtcp-encoder",
3514 (GCallback) request_rtcp_encoder, stream);
3516 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3517 * into a separate RTP session. */
3518 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3519 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3521 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3522 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3525 /* now configure the bandwidth in the manager */
3526 if (g_signal_lookup ("get-internal-session",
3527 G_OBJECT_TYPE (src->manager)) != 0) {
3528 GObject *rtpsession;
3530 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3533 GstRTPProfile rtp_profile;
3535 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3537 stream->session = rtpsession;
3539 if (stream->as_bandwidth != -1) {
3540 GST_INFO_OBJECT (src, "setting AS: %f",
3541 (gdouble) (stream->as_bandwidth * 1000));
3542 g_object_set (rtpsession, "bandwidth",
3543 (gdouble) (stream->as_bandwidth * 1000), NULL);
3545 if (stream->rr_bandwidth != -1) {
3546 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3547 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3550 if (stream->rs_bandwidth != -1) {
3551 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3552 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3556 switch (stream->profile) {
3557 case GST_RTSP_PROFILE_AVPF:
3558 rtp_profile = GST_RTP_PROFILE_AVPF;
3560 case GST_RTSP_PROFILE_SAVP:
3561 rtp_profile = GST_RTP_PROFILE_SAVP;
3563 case GST_RTSP_PROFILE_SAVPF:
3564 rtp_profile = GST_RTP_PROFILE_SAVPF;
3566 case GST_RTSP_PROFILE_AVP:
3568 rtp_profile = GST_RTP_PROFILE_AVP;
3572 g_object_set (rtpsession, "rtp-profile", rtp_profile, NULL);
3574 g_object_set (rtpsession, "probation", src->probation, NULL);
3576 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3578 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3580 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3582 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3584 g_signal_connect (rtpsession, "on-ssrc-active",
3585 (GCallback) on_ssrc_active, stream);
3596 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3601 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3604 start_manager_failure:
3606 GST_DEBUG_OBJECT (src, "could not start session manager");
3611 /* free the UDP sources allocated when negotiating a transport.
3612 * This function is called when the server negotiated to a transport where the
3613 * UDP sources are not needed anymore, such as TCP or multicast. */
3615 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3619 for (i = 0; i < 2; i++) {
3620 if (stream->udpsrc[i]) {
3621 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3622 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3623 gst_object_unref (stream->udpsrc[i]);
3624 stream->udpsrc[i] = NULL;
3629 /* for TCP, create pads to send and receive data to and from the manager and to
3630 * intercept various events and queries
3633 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3634 GstRTSPTransport * transport, GstPad ** outpad)
3637 GstPadTemplate *template;
3638 GstPad *pad0, *pad1;
3640 /* configure for interleaved delivery, nothing needs to be done
3641 * here, the loop function will call the chain functions of the
3642 * session manager. */
3643 stream->channel[0] = transport->interleaved.min;
3644 stream->channel[1] = transport->interleaved.max;
3645 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3646 stream->channel[0], stream->channel[1]);
3648 /* we can remove the allocated UDP ports now */
3649 gst_rtspsrc_stream_free_udp (stream);
3651 /* no session manager, send data to srcpad directly */
3652 if (!stream->channelpad[0]) {
3653 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3655 /* create a new pad we will use to stream to */
3656 name = g_strdup_printf ("stream_%u", stream->id);
3657 template = gst_static_pad_template_get (&rtptemplate);
3658 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3659 gst_object_unref (template);
3662 /* set caps and activate */
3663 gst_pad_use_fixed_caps (stream->channelpad[0]);
3664 gst_pad_set_active (stream->channelpad[0], TRUE);
3666 *outpad = gst_object_ref (stream->channelpad[0]);
3668 GST_DEBUG_OBJECT (src, "using manager source pad");
3670 template = gst_static_pad_template_get (&anysrctemplate);
3672 /* allocate pads for sending the channel data into the manager */
3673 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3674 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3675 gst_object_unref (stream->channelpad[0]);
3676 stream->channelpad[0] = pad0;
3677 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3678 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3679 gst_pad_set_element_private (pad0, src);
3680 gst_pad_set_active (pad0, TRUE);
3682 if (stream->channelpad[1]) {
3683 /* if we have a sinkpad for the other channel, create a pad and link to the
3685 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3686 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3687 gst_pad_link_full (pad1, stream->channelpad[1],
3688 GST_PAD_LINK_CHECK_NOTHING);
3689 gst_object_unref (stream->channelpad[1]);
3690 stream->channelpad[1] = pad1;
3691 gst_pad_set_active (pad1, TRUE);
3693 gst_object_unref (template);
3695 /* setup RTCP transport back to the server if we have to. */
3696 if (src->manager && src->do_rtcp) {
3699 template = gst_static_pad_template_get (&anysinktemplate);
3701 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3702 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3703 gst_pad_set_element_private (stream->rtcppad, stream);
3704 gst_pad_set_active (stream->rtcppad, TRUE);
3706 /* get session RTCP pad */
3707 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3708 pad = gst_element_get_request_pad (src->manager, name);
3713 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3714 gst_object_unref (pad);
3717 gst_object_unref (template);
3723 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3724 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3725 gint * max, guint * ttl)
3727 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3729 if (!(*destination = transport->destination))
3730 *destination = stream->destination;
3733 /* transport first */
3734 *min = transport->port.min;
3735 *max = transport->port.max;
3736 if (*min == -1 && *max == -1) {
3737 /* then try from SDP */
3738 if (stream->port != 0) {
3739 *min = stream->port;
3740 *max = stream->port + 1;
3746 if (!(*ttl = transport->ttl))
3751 /* first take the source, then the endpoint to figure out where to send
3753 if (!(*destination = transport->source)) {
3754 if (src->conninfo.connection)
3755 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3756 else if (stream->conninfo.connection)
3758 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3762 /* for unicast we only expect the ports here */
3763 *min = transport->server_port.min;
3764 *max = transport->server_port.max;
3769 /* For multicast create UDP sources and join the multicast group. */
3771 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3772 GstRTSPTransport * transport, GstPad ** outpad)
3775 const gchar *destination;
3778 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3780 /* we can remove the allocated UDP ports now */
3781 gst_rtspsrc_stream_free_udp (stream);
3783 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3786 /* we need a destination now */
3787 if (destination == NULL)
3788 goto no_destination;
3790 /* we really need ports now or we won't be able to receive anything at all */
3791 if (min == -1 && max == -1)
3794 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3795 destination, min, max);
3797 /* creating UDP source for RTP */
3799 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3801 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3803 if (stream->udpsrc[0] == NULL)
3806 /* take ownership */
3807 gst_object_ref_sink (stream->udpsrc[0]);
3809 if (src->udp_buffer_size != 0)
3810 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3811 src->udp_buffer_size, NULL);
3813 if (src->multi_iface != NULL)
3814 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3815 src->multi_iface, NULL);
3818 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3819 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3822 /* creating another UDP source for RTCP */
3826 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3828 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3830 if (stream->udpsrc[1] == NULL)
3833 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3834 stream->profile == GST_RTSP_PROFILE_SAVPF)
3835 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3837 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3838 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3839 gst_caps_unref (caps);
3841 /* take ownership */
3842 gst_object_ref_sink (stream->udpsrc[1]);
3844 if (src->multi_iface != NULL)
3845 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3846 src->multi_iface, NULL);
3848 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
3855 GST_DEBUG_OBJECT (src, "no UDP source element found");
3860 GST_DEBUG_OBJECT (src, "no destination found");
3865 GST_DEBUG_OBJECT (src, "no ports found");
3870 /* configure the remainder of the UDP ports */
3872 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3873 GstRTSPTransport * transport, GstPad ** outpad)
3875 /* we manage the UDP elements now. For unicast, the UDP sources where
3876 * allocated in the stream when we suggested a transport. */
3877 if (stream->udpsrc[0]) {
3880 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3881 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3883 GST_DEBUG_OBJECT (src, "setting up UDP source");
3885 /* configure a timeout on the UDP port. When the timeout message is
3886 * posted, we assume UDP transport is not possible. We reconnect using TCP
3888 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3889 src->udp_timeout * 1000, NULL);
3891 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
3892 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3894 /* get output pad of the UDP source. */
3895 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3897 /* save it so we can unblock */
3898 stream->blockedpad = *outpad;
3900 /* configure pad block on the pad. As soon as there is dataflow on the
3901 * UDP source, we know that UDP is not blocked by a firewall and we can
3902 * configure all the streams to let the application autoplug decoders. */
3904 gst_pad_add_probe (stream->blockedpad,
3905 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3906 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
3908 if (stream->channelpad[0]) {
3909 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3910 /* configure for UDP delivery, we need to connect the UDP pads to
3911 * the session plugin. */
3912 gst_pad_link_full (*outpad, stream->channelpad[0],
3913 GST_PAD_LINK_CHECK_NOTHING);
3914 gst_object_unref (*outpad);
3916 /* we connected to pad-added signal to get pads from the manager */
3918 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3923 if (stream->udpsrc[1]) {
3926 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3927 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3929 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3930 stream->profile == GST_RTSP_PROFILE_SAVPF)
3931 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3933 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3934 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3935 gst_caps_unref (caps);
3937 if (stream->channelpad[1]) {
3940 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3942 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3943 gst_pad_link_full (pad, stream->channelpad[1],
3944 GST_PAD_LINK_CHECK_NOTHING);
3945 gst_object_unref (pad);
3947 /* leave unlinked */
3953 /* configure the UDP sink back to the server for status reports */
3955 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3956 GstRTSPStream * stream, GstRTSPTransport * transport)
3959 gint rtp_port, rtcp_port;
3960 gboolean do_rtp, do_rtcp;
3961 const gchar *destination;
3966 /* get transport info */
3967 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3968 &rtp_port, &rtcp_port, &ttl);
3970 /* see what we need to do */
3971 do_rtp = (rtp_port != -1);
3972 /* it's possible that the server does not want us to send RTCP in which case
3974 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3976 /* we need a destination when we have RTP or RTCP ports */
3977 if (destination == NULL && (do_rtp || do_rtcp))
3978 goto no_destination;
3980 /* try to construct the fakesrc to the RTP port of the server to open up any
3983 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3986 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3987 stream->udpsink[0] =
3988 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3990 if (stream->udpsink[0] == NULL)
3991 goto no_sink_element;
3993 /* don't join multicast group, we will have the source socket do that */
3994 /* no sync or async state changes needed */
3995 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3996 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3998 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4000 if (stream->udpsrc[0]) {
4001 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
4002 * so that NAT firewalls will open a hole for us */
4003 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
4004 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
4005 /* configure socket and make sure udpsink does not close it when shutting
4006 * down, it belongs to udpsrc after all. */
4007 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
4008 "close-socket", FALSE, NULL);
4009 g_object_unref (socket);
4012 /* the source for the dummy packets to open up NAT */
4013 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
4014 if (stream->fakesrc == NULL)
4015 goto no_fakesrc_element;
4017 /* random data in 5 buffers, a size of 200 bytes should be fine */
4018 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
4019 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
4021 /* we don't want to consider this a sink */
4022 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
4024 /* keep everything locked */
4025 gst_element_set_locked_state (stream->udpsink[0], TRUE);
4026 gst_element_set_locked_state (stream->fakesrc, TRUE);
4028 gst_object_ref (stream->udpsink[0]);
4029 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
4030 gst_object_ref (stream->fakesrc);
4031 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
4033 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
4034 "sink", GST_PAD_LINK_CHECK_NOTHING);
4037 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
4040 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
4041 stream->udpsink[1] =
4042 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
4044 if (stream->udpsink[1] == NULL)
4045 goto no_sink_element;
4047 /* don't join multicast group, we will have the source socket do that */
4048 /* no sync or async state changes needed */
4049 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
4050 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
4052 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
4054 if (stream->udpsrc[1]) {
4055 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
4056 * because some servers check the port number of where it sends RTCP to identify
4057 * the RTCP packets it receives */
4058 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
4059 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
4060 /* configure socket and make sure udpsink does not close it when shutting
4061 * down, it belongs to udpsrc after all. */
4062 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
4063 "close-socket", FALSE, NULL);
4064 g_object_unref (socket);
4067 /* we don't want to consider this a sink */
4068 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
4070 /* we keep this playing always */
4071 gst_element_set_locked_state (stream->udpsink[1], TRUE);
4072 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
4074 gst_object_ref (stream->udpsink[1]);
4075 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
4077 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
4079 /* get session RTCP pad */
4080 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
4081 pad = gst_element_get_request_pad (src->manager, name);
4086 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
4087 gst_object_unref (pad);
4096 GST_DEBUG_OBJECT (src, "no destination address specified");
4101 GST_DEBUG_OBJECT (src, "no UDP sink element found");
4106 GST_DEBUG_OBJECT (src, "no fakesrc element found");
4111 /* sets up all elements needed for streaming over the specified transport.
4112 * Does not yet expose the element pads, this will be done when there is actuall
4113 * dataflow detected, which might never happen when UDP is blocked in a
4114 * firewall, for example.
4117 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
4118 GstRTSPTransport * transport)
4121 GstPad *outpad = NULL;
4122 GstPadTemplate *template;
4124 const gchar *media_type;
4127 src = stream->parent;
4129 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
4131 /* get the proper media type for this stream now */
4132 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
4133 goto unknown_transport;
4135 goto unknown_transport;
4137 /* configure the final media type */
4138 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
4140 len = stream->ptmap->len;
4141 for (i = 0; i < len; i++) {
4143 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
4145 if (item->caps == NULL)
4148 s = gst_caps_get_structure (item->caps, 0);
4149 gst_structure_set_name (s, media_type);
4150 /* set ssrc if known */
4151 if (transport->ssrc)
4152 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
4155 /* try to get and configure a manager, channelpad[0-1] will be configured with
4156 * the pads for the manager, or NULL when no manager is needed. */
4157 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
4160 switch (transport->lower_transport) {
4161 case GST_RTSP_LOWER_TRANS_TCP:
4162 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
4163 goto transport_failed;
4165 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
4166 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
4167 goto transport_failed;
4168 /* fallthrough, the rest is the same for UDP and MCAST */
4169 case GST_RTSP_LOWER_TRANS_UDP:
4170 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
4171 goto transport_failed;
4172 /* configure udpsinks back to the server for RTCP messages and for the
4173 * dummy RTP messages to open NAT. */
4174 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
4175 goto transport_failed;
4178 goto unknown_transport;
4182 GST_DEBUG_OBJECT (src, "creating ghostpad");
4184 gst_pad_use_fixed_caps (outpad);
4186 /* create ghostpad, don't add just yet, this will be done when we activate
4188 name = g_strdup_printf ("stream_%u", stream->id);
4189 template = gst_static_pad_template_get (&rtptemplate);
4190 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
4191 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
4192 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
4193 gst_object_unref (template);
4196 gst_object_unref (outpad);
4198 /* mark pad as ok */
4199 stream->last_ret = GST_FLOW_OK;
4206 GST_DEBUG_OBJECT (src, "failed to configure transport");
4211 GST_DEBUG_OBJECT (src, "unknown transport");
4216 GST_DEBUG_OBJECT (src, "cannot get a session manager");
4221 /* send a couple of dummy random packets on the receiver RTP port to the server,
4222 * this should make a firewall think we initiated the data transfer and
4223 * hopefully allow packets to go from the sender port to our RTP receiver port */
4225 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
4229 if (src->nat_method != GST_RTSP_NAT_DUMMY)
4232 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4233 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4235 if (stream->fakesrc && stream->udpsink[0]) {
4236 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
4237 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
4238 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
4239 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
4240 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
4246 /* Adds the source pads of all configured streams to the element.
4247 * This code is performed when we detected dataflow.
4249 * We detect dataflow from either the _loop function or with pad probes on the
4253 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
4257 GST_DEBUG_OBJECT (src, "activating streams");
4259 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4260 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4262 if (stream->udpsrc[0]) {
4263 /* remove timeout, we are streaming now and timeouts will be handled by
4264 * the session manager and jitter buffer */
4265 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
4267 if (stream->srcpad) {
4268 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
4269 gst_pad_set_active (stream->srcpad, TRUE);
4271 /* if we don't have a session manager, set the caps now. If we have a
4272 * session, we will get a notification of the pad and the caps. */
4273 if (!src->manager) {
4276 caps = stream_get_caps_for_pt (stream, stream->default_pt);
4277 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
4278 gst_pad_set_caps (stream->srcpad, caps);
4281 if (!stream->added) {
4282 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
4283 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
4284 stream->added = TRUE;
4289 /* unblock all pads */
4290 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4291 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4293 if (stream->blockid) {
4294 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
4295 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
4296 stream->blockid = 0;
4304 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
4305 gboolean reset_manager)
4308 guint64 start, stop;
4309 gdouble play_speed, play_scale;
4311 GST_DEBUG_OBJECT (src, "configuring stream caps");
4313 start = segment->position;
4314 stop = segment->duration;
4315 play_speed = segment->rate;
4316 play_scale = segment->applied_rate;
4318 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4319 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4325 len = stream->ptmap->len;
4326 for (j = 0; j < len; j++) {
4328 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
4330 if (item->caps == NULL)
4333 caps = gst_caps_make_writable (item->caps);
4335 if (stream->timebase != -1)
4336 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
4337 (guint) stream->timebase, NULL);
4338 if (stream->seqbase != -1)
4339 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
4340 (guint) stream->seqbase, NULL);
4341 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
4343 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
4344 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
4345 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
4348 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
4351 if (item->pt == stream->default_pt && stream->udpsrc[0]) {
4352 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4356 if (reset_manager && src->manager) {
4357 GST_DEBUG_OBJECT (src, "clear session");
4358 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
4362 static GstFlowReturn
4363 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
4368 /* store the value */
4369 stream->last_ret = ret;
4371 /* if it's success we can return the value right away */
4372 if (ret == GST_FLOW_OK)
4375 /* any other error that is not-linked can be returned right
4377 if (ret != GST_FLOW_NOT_LINKED)
4380 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4381 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4382 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4384 ret = ostream->last_ret;
4385 /* some other return value (must be SUCCESS but we can return
4386 * other values as well) */
4387 if (ret != GST_FLOW_NOT_LINKED)
4390 /* if we get here, all other pads were unlinked and we return
4391 * NOT_LINKED then */
4397 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4400 gboolean res = TRUE;
4402 /* only streams that have a connection to the outside world */
4406 if (stream->udpsrc[0]) {
4407 gst_event_ref (event);
4408 res = gst_element_send_event (stream->udpsrc[0], event);
4409 } else if (stream->channelpad[0]) {
4410 gst_event_ref (event);
4411 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4412 res = gst_pad_push_event (stream->channelpad[0], event);
4414 res = gst_pad_send_event (stream->channelpad[0], event);
4417 if (stream->udpsrc[1]) {
4418 gst_event_ref (event);
4419 res &= gst_element_send_event (stream->udpsrc[1], event);
4420 } else if (stream->channelpad[1]) {
4421 gst_event_ref (event);
4422 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4423 res &= gst_pad_push_event (stream->channelpad[1], event);
4425 res &= gst_pad_send_event (stream->channelpad[1], event);
4429 gst_event_unref (event);
4435 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4438 gboolean res = TRUE;
4440 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4441 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4443 gst_event_ref (event);
4444 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4446 gst_event_unref (event);
4451 static GstRTSPResult
4452 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4457 if (info->connection == NULL) {
4458 if (info->url == NULL) {
4459 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4460 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4464 /* create connection */
4465 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4466 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4467 goto could_not_create;
4470 g_free (info->url_str);
4471 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4473 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4475 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4476 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4477 src->tls_validation_flags))
4478 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4480 if (src->tls_database)
4481 gst_rtsp_connection_set_tls_database (info->connection,
4484 if (src->tls_interaction)
4485 gst_rtsp_connection_set_tls_interaction (info->connection,
4486 src->tls_interaction);
4489 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4490 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4492 if (src->proxy_host) {
4493 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4495 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4500 if (!info->connected) {
4503 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4504 ("Connecting to %s", info->location));
4505 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4507 gst_rtsp_connection_connect (info->connection,
4508 src->ptcp_timeout)) < 0)
4509 goto could_not_connect;
4511 info->connected = TRUE;
4518 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4523 gchar *str = gst_rtsp_strresult (res);
4524 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4530 gchar *str = gst_rtsp_strresult (res);
4531 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4537 static GstRTSPResult
4538 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4541 GST_RTSP_STATE_LOCK (src);
4542 if (info->connected) {
4543 GST_DEBUG_OBJECT (src, "closing connection...");
4544 gst_rtsp_connection_close (info->connection);
4545 info->connected = FALSE;
4547 if (free && info->connection) {
4548 /* free connection */
4549 GST_DEBUG_OBJECT (src, "freeing connection...");
4550 gst_rtsp_connection_free (info->connection);
4551 info->connection = NULL;
4553 GST_RTSP_STATE_UNLOCK (src);
4557 static GstRTSPResult
4558 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4563 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4564 gst_rtsp_conninfo_close (src, info, FALSE);
4565 res = gst_rtsp_conninfo_connect (src, info, async);
4571 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4575 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4576 GST_RTSP_STATE_LOCK (src);
4577 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4578 GST_DEBUG_OBJECT (src, "connection flush");
4579 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4580 src->conninfo.flushing = flush;
4582 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4583 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4584 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4585 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4586 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4587 stream->conninfo.flushing = flush;
4590 GST_RTSP_STATE_UNLOCK (src);
4593 static GstRTSPResult
4594 gst_rtspsrc_init_request (GstRTSPSrc * src, GstRTSPMessage * msg,
4595 GstRTSPMethod method, const gchar * uri)
4599 res = gst_rtsp_message_init_request (msg, method, uri);
4603 /* set user-agent */
4604 if (src->user_agent)
4605 gst_rtsp_message_add_header (msg, GST_RTSP_HDR_USER_AGENT, src->user_agent);
4610 /* FIXME, handle server request, reply with OK, for now */
4611 static GstRTSPResult
4612 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
4613 GstRTSPMessage * request)
4615 GstRTSPMessage response = { 0 };
4618 GST_DEBUG_OBJECT (src, "got server request message");
4621 gst_rtsp_message_dump (request);
4623 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4625 if (res == GST_RTSP_ENOTIMPL) {
4626 /* default implementation, send OK */
4627 GST_DEBUG_OBJECT (src, "prepare OK reply");
4629 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4634 /* let app parse and reply */
4635 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4636 0, request, &response);
4639 gst_rtsp_message_dump (&response);
4641 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
4645 gst_rtsp_message_unset (&response);
4646 } else if (res == GST_RTSP_EEOF)
4654 gst_rtsp_message_unset (&response);
4659 /* send server keep-alive */
4660 static GstRTSPResult
4661 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4663 GstRTSPMessage request = { 0 };
4665 GstRTSPMethod method;
4666 const gchar *control;
4668 if (src->do_rtsp_keep_alive == FALSE) {
4669 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4670 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4674 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4676 /* find a method to use for keep-alive */
4677 if (src->methods & GST_RTSP_GET_PARAMETER)
4678 method = GST_RTSP_GET_PARAMETER;
4680 method = GST_RTSP_OPTIONS;
4682 control = get_aggregate_control (src);
4683 if (control == NULL)
4686 res = gst_rtspsrc_init_request (src, &request, method, control);
4691 gst_rtsp_message_dump (&request);
4694 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
4699 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4700 gst_rtsp_message_unset (&request);
4707 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4712 gchar *str = gst_rtsp_strresult (res);
4714 gst_rtsp_message_unset (&request);
4715 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4716 ("Could not send keep-alive. (%s)", str));
4722 static GstFlowReturn
4723 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4725 GstFlowReturn ret = GST_FLOW_OK;
4727 GstRTSPStream *stream;
4728 GstPad *outpad = NULL;
4734 channel = message->type_data.data.channel;
4736 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4738 goto unknown_stream;
4740 if (channel == stream->channel[0]) {
4741 outpad = stream->channelpad[0];
4743 } else if (channel == stream->channel[1]) {
4744 outpad = stream->channelpad[1];
4750 /* take a look at the body to figure out what we have */
4751 gst_rtsp_message_get_body (message, &data, &size);
4753 goto invalid_length;
4755 /* channels are not correct on some servers, do extra check */
4756 if (data[1] >= 200 && data[1] <= 204) {
4757 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4758 outpad = stream->channelpad[1];
4762 /* we have no clue what this is, just ignore then. */
4764 goto unknown_stream;
4766 /* take the message body for further processing */
4767 gst_rtsp_message_steal_body (message, &data, &size);
4769 /* strip the trailing \0 */
4772 buf = gst_buffer_new ();
4773 gst_buffer_append_memory (buf,
4774 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4776 /* don't need message anymore */
4777 gst_rtsp_message_unset (message);
4779 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4782 if (src->need_activate) {
4788 guint group_id = gst_util_group_id_next ();
4790 /* generate an SHA256 sum of the URI */
4791 cs = g_checksum_new (G_CHECKSUM_SHA256);
4792 uri = src->conninfo.location;
4793 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4795 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4796 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4800 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4801 event = gst_event_new_stream_start (stream_id);
4802 gst_event_set_group_id (event, group_id);
4805 gst_rtspsrc_stream_push_event (src, ostream, event);
4807 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
4808 /* only streams that have a connection to the outside world */
4809 if (ostream->setup) {
4810 if (ostream->udpsrc[0]) {
4811 gst_element_send_event (ostream->udpsrc[0],
4812 gst_event_new_caps (caps));
4813 } else if (ostream->channelpad[0]) {
4814 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
4815 gst_pad_push_event (ostream->channelpad[0],
4816 gst_event_new_caps (caps));
4818 gst_pad_send_event (ostream->channelpad[0],
4819 gst_event_new_caps (caps));
4822 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4824 if (ostream->udpsrc[1]) {
4825 gst_element_send_event (ostream->udpsrc[1],
4826 gst_event_new_caps (caps));
4827 } else if (ostream->channelpad[1]) {
4828 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
4829 gst_pad_push_event (ostream->channelpad[1],
4830 gst_event_new_caps (caps));
4832 gst_pad_send_event (ostream->channelpad[1],
4833 gst_event_new_caps (caps));
4836 gst_caps_unref (caps);
4840 g_checksum_free (cs);
4842 gst_rtspsrc_activate_streams (src);
4843 src->need_activate = FALSE;
4844 src->need_segment = TRUE;
4847 if (src->base_time == -1) {
4848 /* Take current running_time. This timestamp will be put on
4849 * the first buffer of each stream because we are a live source and so we
4850 * timestamp with the running_time. When we are dealing with TCP, we also
4851 * only timestamp the first buffer (using the DISCONT flag) because a server
4852 * typically bursts data, for which we don't want to compensate by speeding
4853 * up the media. The other timestamps will be interpollated from this one
4854 * using the RTP timestamps. */
4855 GST_OBJECT_LOCK (src);
4856 if (GST_ELEMENT_CLOCK (src)) {
4858 GstClockTime base_time;
4860 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4861 base_time = GST_ELEMENT_CAST (src)->base_time;
4863 src->base_time = now - base_time;
4865 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4866 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4868 GST_OBJECT_UNLOCK (src);
4871 /* If needed send a new segment, don't forget we are live and buffer are
4872 * timestamped with running time */
4873 if (src->need_segment) {
4875 src->need_segment = FALSE;
4876 gst_segment_init (&segment, GST_FORMAT_TIME);
4877 gst_rtspsrc_push_event (src, gst_event_new_segment (&segment));
4880 if (stream->discont && !is_rtcp) {
4881 /* mark first RTP buffer as discont */
4882 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4883 stream->discont = FALSE;
4884 /* first buffer gets the timestamp, other buffers are not timestamped and
4885 * their presentation time will be interpollated from the rtp timestamps. */
4886 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4887 GST_TIME_ARGS (src->base_time));
4889 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4892 /* chain to the peer pad */
4893 if (GST_PAD_IS_SINK (outpad))
4894 ret = gst_pad_chain (outpad, buf);
4896 ret = gst_pad_push (outpad, buf);
4899 /* combine all stream flows for the data transport */
4900 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4907 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4908 gst_rtsp_message_unset (message);
4913 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4914 ("Short message received, ignoring."));
4915 gst_rtsp_message_unset (message);
4920 static GstFlowReturn
4921 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4923 GstRTSPMessage message = { 0 };
4925 GstFlowReturn ret = GST_FLOW_OK;
4926 GTimeVal tv_timeout;
4929 /* get the next timeout interval */
4930 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4932 /* see if the timeout period expired */
4933 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4934 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4935 /* send keep-alive, only act on interrupt, a warning will be posted for
4937 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4939 /* get new timeout */
4940 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4943 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4944 tv_timeout.tv_sec, tv_timeout.tv_usec);
4946 /* protect the connection with the connection lock so that we can see when
4947 * we are finished doing server communication */
4949 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4950 &message, src->ptcp_timeout);
4954 GST_DEBUG_OBJECT (src, "we received a server message");
4956 case GST_RTSP_EINTR:
4957 /* we got interrupted this means we need to stop */
4959 case GST_RTSP_ETIMEOUT:
4960 /* no reply, send keep alive */
4961 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4962 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4966 /* go EOS when the server closed the connection */
4972 switch (message.type) {
4973 case GST_RTSP_MESSAGE_REQUEST:
4974 /* server sends us a request message, handle it */
4976 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4978 if (res == GST_RTSP_EEOF)
4981 goto handle_request_failed;
4983 case GST_RTSP_MESSAGE_RESPONSE:
4984 /* we ignore response messages */
4985 GST_DEBUG_OBJECT (src, "ignoring response message");
4987 gst_rtsp_message_dump (&message);
4989 case GST_RTSP_MESSAGE_DATA:
4990 GST_DEBUG_OBJECT (src, "got data message");
4991 ret = gst_rtspsrc_handle_data (src, &message);
4992 if (ret != GST_FLOW_OK)
4993 goto handle_data_failed;
4996 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5001 g_assert_not_reached ();
5006 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5007 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5008 ("The server closed the connection."));
5009 src->conninfo.connected = FALSE;
5010 gst_rtsp_message_unset (&message);
5011 return GST_FLOW_EOS;
5015 gst_rtsp_message_unset (&message);
5016 GST_DEBUG_OBJECT (src, "got interrupted");
5017 return GST_FLOW_FLUSHING;
5021 gchar *str = gst_rtsp_strresult (res);
5023 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5024 ("Could not receive message. (%s)", str));
5027 gst_rtsp_message_unset (&message);
5028 return GST_FLOW_ERROR;
5030 handle_request_failed:
5032 gchar *str = gst_rtsp_strresult (res);
5034 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5035 ("Could not handle server message. (%s)", str));
5037 gst_rtsp_message_unset (&message);
5038 return GST_FLOW_ERROR;
5042 GST_DEBUG_OBJECT (src, "could no handle data message");
5047 static GstFlowReturn
5048 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
5051 GstRTSPMessage message = { 0 };
5055 GTimeVal tv_timeout;
5057 /* get the next timeout interval */
5058 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
5060 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
5061 (gint) tv_timeout.tv_sec);
5063 gst_rtsp_message_unset (&message);
5065 /* we should continue reading the TCP socket because the server might
5066 * send us requests. When the session timeout expires, we need to send a
5067 * keep-alive request to keep the session open. */
5068 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
5069 &message, &tv_timeout);
5073 GST_DEBUG_OBJECT (src, "we received a server message");
5075 case GST_RTSP_EINTR:
5076 /* we got interrupted, see what we have to do */
5078 case GST_RTSP_ETIMEOUT:
5079 /* send keep-alive, ignore the result, a warning will be posted. */
5080 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
5081 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5085 /* server closed the connection. not very fatal for UDP, reconnect and
5086 * see what happens. */
5087 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5088 ("The server closed the connection."));
5089 if (src->udp_reconnect) {
5091 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
5098 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
5100 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5101 ("Unhandled return value %d.", res));
5105 switch (message.type) {
5106 case GST_RTSP_MESSAGE_REQUEST:
5107 /* server sends us a request message, handle it */
5109 gst_rtspsrc_handle_request (src, src->conninfo.connection,
5111 if (res == GST_RTSP_EEOF)
5114 goto handle_request_failed;
5116 case GST_RTSP_MESSAGE_RESPONSE:
5117 /* we ignore response and data messages */
5118 GST_DEBUG_OBJECT (src, "ignoring response message");
5120 gst_rtsp_message_dump (&message);
5121 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
5122 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
5123 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
5124 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
5125 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
5132 case GST_RTSP_MESSAGE_DATA:
5133 /* we ignore response and data messages */
5134 GST_DEBUG_OBJECT (src, "ignoring data message");
5137 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5142 g_assert_not_reached ();
5144 /* we get here when the connection got interrupted */
5147 gst_rtsp_message_unset (&message);
5148 GST_DEBUG_OBJECT (src, "got interrupted");
5149 return GST_FLOW_FLUSHING;
5153 gchar *str = gst_rtsp_strresult (res);
5156 src->conninfo.connected = FALSE;
5157 if (res != GST_RTSP_EINTR) {
5158 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
5159 ("Could not connect to server. (%s)", str));
5161 ret = GST_FLOW_ERROR;
5163 ret = GST_FLOW_FLUSHING;
5169 gchar *str = gst_rtsp_strresult (res);
5171 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5172 ("Could not receive message. (%s)", str));
5174 return GST_FLOW_ERROR;
5176 handle_request_failed:
5178 gchar *str = gst_rtsp_strresult (res);
5181 gst_rtsp_message_unset (&message);
5182 if (res != GST_RTSP_EINTR) {
5183 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5184 ("Could not handle server message. (%s)", str));
5186 ret = GST_FLOW_ERROR;
5188 ret = GST_FLOW_FLUSHING;
5194 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5195 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5196 ("The server closed the connection."));
5197 src->conninfo.connected = FALSE;
5198 gst_rtsp_message_unset (&message);
5199 return GST_FLOW_EOS;
5203 static GstRTSPResult
5204 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
5206 GstRTSPResult res = GST_RTSP_OK;
5209 GST_DEBUG_OBJECT (src, "doing reconnect");
5211 GST_OBJECT_LOCK (src);
5212 /* only restart when the pads were not yet activated, else we were
5213 * streaming over UDP */
5214 restart = src->need_activate;
5215 GST_OBJECT_UNLOCK (src);
5217 /* no need to restart, we're done */
5221 /* we can try only TCP now */
5222 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
5224 /* close and cleanup our state */
5225 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
5228 /* see if we have TCP left to try. Also don't try TCP when we were configured
5230 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
5233 /* We post a warning message now to inform the user
5234 * that nothing happened. It's most likely a firewall thing. */
5235 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5236 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5237 "firewall is blocking it. Retrying using a TCP connection.",
5238 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
5240 /* open new connection using tcp */
5241 if (gst_rtspsrc_open (src, async) < 0)
5244 /* start playback */
5245 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
5254 src->cur_protocols = 0;
5255 /* no transport possible, post an error and stop */
5256 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5257 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5258 "firewall is blocking it. No other protocols to try.",
5259 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
5260 return GST_RTSP_ERROR;
5264 GST_DEBUG_OBJECT (src, "open failed");
5269 GST_DEBUG_OBJECT (src, "play failed");
5275 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
5279 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
5282 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
5285 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
5288 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
5296 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
5300 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
5303 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
5306 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
5309 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
5317 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
5321 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
5324 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
5327 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
5330 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
5338 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
5342 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
5345 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
5348 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
5351 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
5359 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
5361 if (ret == GST_RTSP_OK)
5362 gst_rtspsrc_loop_complete_cmd (src, cmd);
5363 else if (ret == GST_RTSP_EINTR)
5364 gst_rtspsrc_loop_cancel_cmd (src, cmd);
5366 gst_rtspsrc_loop_error_cmd (src, cmd);
5370 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
5373 gboolean flushed = FALSE;
5375 /* start new request */
5376 gst_rtspsrc_loop_start_cmd (src, cmd);
5378 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
5380 GST_OBJECT_LOCK (src);
5381 old = src->pending_cmd;
5382 if (old == CMD_RECONNECT) {
5383 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
5384 cmd = CMD_RECONNECT;
5386 if (old != CMD_WAIT) {
5387 src->pending_cmd = CMD_WAIT;
5388 GST_OBJECT_UNLOCK (src);
5389 /* cancel previous request */
5390 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
5391 gst_rtspsrc_loop_cancel_cmd (src, old);
5392 GST_OBJECT_LOCK (src);
5394 src->pending_cmd = cmd;
5395 /* interrupt if allowed */
5396 if (src->busy_cmd & mask) {
5397 GST_DEBUG_OBJECT (src, "connection flush busy %s",
5398 cmd_to_string (src->busy_cmd));
5399 gst_rtspsrc_connection_flush (src, TRUE);
5402 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
5403 cmd_to_string (src->busy_cmd));
5406 gst_task_start (src->task);
5407 GST_OBJECT_UNLOCK (src);
5413 gst_rtspsrc_loop (GstRTSPSrc * src)
5417 if (!src->conninfo.connection || !src->conninfo.connected)
5420 if (src->interleaved)
5421 ret = gst_rtspsrc_loop_interleaved (src);
5423 ret = gst_rtspsrc_loop_udp (src);
5425 if (ret != GST_FLOW_OK)
5433 GST_WARNING_OBJECT (src, "we are not connected");
5434 ret = GST_FLOW_FLUSHING;
5439 const gchar *reason = gst_flow_get_name (ret);
5441 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
5442 src->running = FALSE;
5443 if (ret == GST_FLOW_EOS) {
5444 /* perform EOS logic */
5445 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5446 gst_element_post_message (GST_ELEMENT_CAST (src),
5447 gst_message_new_segment_done (GST_OBJECT_CAST (src),
5448 src->segment.format, src->segment.position));
5449 gst_rtspsrc_push_event (src,
5450 gst_event_new_segment_done (src->segment.format,
5451 src->segment.position));
5453 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5455 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
5456 /* for fatal errors we post an error message, post the error before the
5457 * EOS so the app knows about the error first. */
5458 GST_ELEMENT_ERROR (src, STREAM, FAILED,
5459 ("Internal data flow error."),
5460 ("streaming task paused, reason %s (%d)", reason, ret));
5461 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5463 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
5468 #ifndef GST_DISABLE_GST_DEBUG
5469 static const gchar *
5470 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
5474 while (method != 0) {
5491 static const gchar *
5492 gst_rtspsrc_skip_lws (const gchar * s)
5494 while (g_ascii_isspace (*s))
5499 static const gchar *
5500 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
5502 while (s > start && g_ascii_isspace (*(s - 1)))
5507 static const gchar *
5508 gst_rtspsrc_skip_commas (const gchar * s)
5510 /* The grammar allows for multiple commas */
5511 while (g_ascii_isspace (*s) || *s == ',')
5516 static const gchar *
5517 gst_rtspsrc_skip_item (const gchar * s)
5519 gboolean quoted = FALSE;
5520 const gchar *start = s;
5522 /* A list item ends at the last non-whitespace character
5523 * before a comma which is not inside a quoted-string. Or at
5524 * the end of the string.
5530 if (*s == '\\' && *(s + 1))
5539 return gst_rtspsrc_unskip_lws (s, start);
5543 gst_rtsp_decode_quoted_string (gchar * quoted_string)
5547 src = quoted_string + 1;
5548 dst = quoted_string;
5549 while (*src && *src != '"') {
5550 if (*src == '\\' && *(src + 1))
5557 /* Extract the authentication tokens that the server provided for each method
5558 * into an array of structures and give those to the connection object.
5561 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
5562 const gchar * header, gboolean * stale)
5564 GSList *list = NULL, *iter;
5566 gchar *item, *eq, *name_end, *value;
5568 g_return_if_fail (stale != NULL);
5570 gst_rtsp_connection_clear_auth_params (conn);
5573 /* Parse a header whose content is described by RFC2616 as
5574 * "#something", where "something" does not itself contain commas,
5575 * except as part of quoted-strings, into a list of allocated strings.
5577 header = gst_rtspsrc_skip_commas (header);
5579 end = gst_rtspsrc_skip_item (header);
5580 list = g_slist_prepend (list, g_strndup (header, end - header));
5581 header = gst_rtspsrc_skip_commas (end);
5586 list = g_slist_reverse (list);
5587 for (iter = list; iter; iter = iter->next) {
5590 eq = strchr (item, '=');
5592 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
5593 if (name_end == item) {
5594 /* That's no good... */
5601 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
5603 gst_rtsp_decode_quoted_string (value);
5607 if (value && strcmp (item, "stale") == 0 && strcmp (value, "TRUE") == 0)
5609 gst_rtsp_connection_set_auth_param (conn, item, value);
5613 g_slist_free (list);
5616 /* Parse a WWW-Authenticate Response header and determine the
5617 * available authentication methods
5619 * This code should also cope with the fact that each WWW-Authenticate
5620 * header can contain multiple challenge methods + tokens
5622 * At the moment, for Basic auth, we just do a minimal check and don't
5623 * even parse out the realm */
5625 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
5626 GstRTSPConnection * conn, gboolean * stale)
5630 g_return_if_fail (hdr != NULL);
5631 g_return_if_fail (methods != NULL);
5632 g_return_if_fail (stale != NULL);
5634 /* Skip whitespace at the start of the string */
5635 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
5637 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
5638 *methods |= GST_RTSP_AUTH_BASIC;
5639 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
5640 *methods |= GST_RTSP_AUTH_DIGEST;
5641 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
5646 * gst_rtspsrc_setup_auth:
5647 * @src: the rtsp source
5649 * Configure a username and password and auth method on the
5650 * connection object based on a response we received from the
5653 * Currently, this requires that a username and password were supplied
5654 * in the uri. In the future, they may be requested on demand by sending
5655 * a message up the bus.
5657 * Returns: TRUE if authentication information could be set up correctly.
5660 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5664 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5665 GstRTSPAuthMethod method;
5666 GstRTSPResult auth_result;
5668 GstRTSPConnection *conn;
5670 gboolean stale = FALSE;
5672 conn = src->conninfo.connection;
5674 /* Identify the available auth methods and see if any are supported */
5675 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
5676 &hdr, 0) == GST_RTSP_OK) {
5677 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
5680 if (avail_methods == GST_RTSP_AUTH_NONE)
5681 goto no_auth_available;
5683 /* For digest auth, if the response indicates that the session
5684 * data are stale, we just update them in the connection object and
5685 * return TRUE to retry the request */
5687 src->tried_url_auth = FALSE;
5689 url = gst_rtsp_connection_get_url (conn);
5691 /* Do we have username and password available? */
5692 if (url != NULL && !src->tried_url_auth && url->user != NULL
5693 && url->passwd != NULL) {
5696 src->tried_url_auth = TRUE;
5697 GST_DEBUG_OBJECT (src,
5698 "Attempting authentication using credentials from the URL");
5700 user = src->user_id;
5701 pass = src->user_pw;
5702 GST_DEBUG_OBJECT (src,
5703 "Attempting authentication using credentials from the properties");
5706 /* FIXME: If the url didn't contain username and password or we tried them
5707 * already, request a username and passwd from the application via some kind
5708 * of credentials request message */
5710 /* If we don't have a username and passwd at this point, bail out. */
5711 if (user == NULL || pass == NULL)
5714 /* Try to configure for each available authentication method, strongest to
5716 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5717 /* Check if this method is available on the server */
5718 if ((method & avail_methods) == 0)
5721 /* Pass the credentials to the connection to try on the next request */
5722 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5723 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5724 * ignore it and end up retrying later */
5725 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5726 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5727 gst_rtsp_auth_method_to_string (method));
5732 if (method == GST_RTSP_AUTH_NONE)
5733 goto no_auth_available;
5739 /* Output an error indicating that we couldn't connect because there were
5740 * no supported authentication protocols */
5741 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5742 ("No supported authentication protocol was found"));
5747 /* We don't fire an error message, we just return FALSE and let the
5748 * normal NOT_AUTHORIZED error be propagated */
5753 static GstRTSPResult
5754 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5755 GstRTSPMessage * request, GstRTSPMessage * response,
5756 GstRTSPStatusCode * code)
5759 GstRTSPStatusCode thecode;
5760 gchar *content_base = NULL;
5764 if (!src->short_header)
5765 gst_rtsp_ext_list_before_send (src->extensions, request);
5767 GST_DEBUG_OBJECT (src, "sending message");
5770 gst_rtsp_message_dump (request);
5772 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
5776 gst_rtsp_connection_reset_timeout (conn);
5779 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
5784 gst_rtsp_message_dump (response);
5786 switch (response->type) {
5787 case GST_RTSP_MESSAGE_REQUEST:
5788 res = gst_rtspsrc_handle_request (src, conn, response);
5789 if (res == GST_RTSP_EEOF)
5792 goto handle_request_failed;
5794 case GST_RTSP_MESSAGE_RESPONSE:
5795 /* ok, a response is good */
5796 GST_DEBUG_OBJECT (src, "received response message");
5798 case GST_RTSP_MESSAGE_DATA:
5799 /* get next response */
5800 GST_DEBUG_OBJECT (src, "handle data response message");
5801 gst_rtspsrc_handle_data (src, response);
5804 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5809 thecode = response->type_data.response.code;
5811 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5813 /* if the caller wanted the result code, we store it. */
5817 /* If the request didn't succeed, bail out before doing any more */
5818 if (thecode != GST_RTSP_STS_OK)
5821 /* store new content base if any */
5822 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5825 g_free (src->content_base);
5826 src->content_base = g_strdup (content_base);
5828 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5835 gchar *str = gst_rtsp_strresult (res);
5837 if (res != GST_RTSP_EINTR) {
5838 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5839 ("Could not send message. (%s)", str));
5841 GST_WARNING_OBJECT (src, "send interrupted");
5850 GST_WARNING_OBJECT (src, "server closed connection");
5851 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5853 /* if reconnect succeeds, try again */
5855 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5859 /* only try once after reconnect, then fallthrough and error out */
5862 gchar *str = gst_rtsp_strresult (res);
5864 if (res != GST_RTSP_EINTR) {
5865 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5866 ("Could not receive message. (%s)", str));
5868 GST_WARNING_OBJECT (src, "receive interrupted");
5876 handle_request_failed:
5878 /* ERROR was posted */
5879 gst_rtsp_message_unset (response);
5884 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5885 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5886 ("The server closed the connection."));
5887 gst_rtsp_message_unset (response);
5894 * @src: the rtsp source
5895 * @conn: the connection to send on
5896 * @request: must point to a valid request
5897 * @response: must point to an empty #GstRTSPMessage
5898 * @code: an optional code result
5900 * send @request and retrieve the response in @response. optionally @code can be
5901 * non-NULL in which case it will contain the status code of the response.
5903 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5904 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5906 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5907 * @response message) if the response code was not 200 (OK).
5909 * If the attempt results in an authentication failure, then this will attempt
5910 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5913 * Returns: #GST_RTSP_OK if the processing was successful.
5915 static GstRTSPResult
5916 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5917 GstRTSPMessage * request, GstRTSPMessage * response,
5918 GstRTSPStatusCode * code)
5920 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5921 GstRTSPResult res = GST_RTSP_ERROR;
5924 GstRTSPMethod method = GST_RTSP_INVALID;
5930 /* make sure we don't loop forever */
5934 /* save method so we can disable it when the server complains */
5935 method = request->type_data.request.method;
5938 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5942 case GST_RTSP_STS_UNAUTHORIZED:
5943 if (gst_rtspsrc_setup_auth (src, response)) {
5944 /* Try the request/response again after configuring the auth info
5952 } while (retry == TRUE);
5954 /* If the user requested the code, let them handle errors, otherwise
5955 * post an error below */
5958 else if (int_code != GST_RTSP_STS_OK)
5959 goto error_response;
5966 GST_DEBUG_OBJECT (src, "got error %d", res);
5971 res = GST_RTSP_ERROR;
5973 switch (response->type_data.response.code) {
5974 case GST_RTSP_STS_NOT_FOUND:
5975 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5976 response->type_data.response.reason));
5978 case GST_RTSP_STS_UNAUTHORIZED:
5979 GST_ELEMENT_ERROR (src, RESOURCE, NOT_AUTHORIZED, (NULL), ("%s",
5980 response->type_data.response.reason));
5982 case GST_RTSP_STS_MOVED_PERMANENTLY:
5983 case GST_RTSP_STS_MOVE_TEMPORARILY:
5985 gchar *new_location;
5986 GstRTSPLowerTrans transports;
5988 GST_DEBUG_OBJECT (src, "got redirection");
5989 /* if we don't have a Location Header, we must error */
5990 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5991 &new_location, 0) < 0)
5994 /* When we receive a redirect result, we go back to the INIT state after
5995 * parsing the new URI. The caller should do the needed steps to issue
5996 * a new setup when it detects this state change. */
5997 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5999 /* save current transports */
6000 if (src->conninfo.url)
6001 transports = src->conninfo.url->transports;
6003 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
6005 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
6007 /* set old transports */
6008 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
6009 src->conninfo.url->transports = transports;
6011 src->need_redirect = TRUE;
6012 src->state = GST_RTSP_STATE_INIT;
6016 case GST_RTSP_STS_NOT_ACCEPTABLE:
6017 case GST_RTSP_STS_NOT_IMPLEMENTED:
6018 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
6019 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
6020 gst_rtsp_method_as_text (method));
6021 src->methods &= ~method;
6025 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6026 ("Got error response: %d (%s).", response->type_data.response.code,
6027 response->type_data.response.reason));
6030 /* if we return ERROR we should unset the response ourselves */
6031 if (res == GST_RTSP_ERROR)
6032 gst_rtsp_message_unset (response);
6038 static GstRTSPResult
6039 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
6040 GstRTSPMessage * response, GstRTSPSrc * src)
6042 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
6047 /* parse the response and collect all the supported methods. We need this
6048 * information so that we don't try to send an unsupported request to the
6052 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
6054 GstRTSPHeaderField field;
6058 /* reset supported methods */
6061 /* Try Allow Header first */
6062 field = GST_RTSP_HDR_ALLOW;
6065 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6066 if (indx == 0 && !respoptions) {
6067 /* if no Allow header was found then try the Public header... */
6068 field = GST_RTSP_HDR_PUBLIC;
6069 gst_rtsp_message_get_header (response, field, &respoptions, indx);
6074 src->methods |= gst_rtsp_options_from_text (respoptions);
6079 if (src->methods == 0) {
6080 /* neither Allow nor Public are required, assume the server supports
6081 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
6083 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
6084 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
6086 /* always assume PLAY, FIXME, extensions should be able to override
6088 src->methods |= GST_RTSP_PLAY;
6089 /* also assume it will support Range */
6090 src->seekable = TRUE;
6092 /* we need describe and setup */
6093 if (!(src->methods & GST_RTSP_DESCRIBE))
6095 if (!(src->methods & GST_RTSP_SETUP))
6103 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6104 ("Server does not support DESCRIBE."));
6109 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
6110 ("Server does not support SETUP."));
6115 /* masks to be kept in sync with the hardcoded protocol order of preference
6117 static const guint protocol_masks[] = {
6118 GST_RTSP_LOWER_TRANS_UDP,
6119 GST_RTSP_LOWER_TRANS_UDP_MCAST,
6120 GST_RTSP_LOWER_TRANS_TCP,
6124 static GstRTSPResult
6125 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
6126 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
6130 gboolean add_udp_str;
6135 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
6140 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
6142 /* extension listed transports, use those */
6143 if (*transports != NULL)
6146 /* it's the default */
6147 add_udp_str = FALSE;
6149 /* the default RTSP transports */
6150 result = g_string_new ("RTP");
6153 case GST_RTSP_PROFILE_AVP:
6154 g_string_append (result, "/AVP");
6156 case GST_RTSP_PROFILE_SAVP:
6157 g_string_append (result, "/SAVP");
6159 case GST_RTSP_PROFILE_AVPF:
6160 g_string_append (result, "/AVPF");
6162 case GST_RTSP_PROFILE_SAVPF:
6163 g_string_append (result, "/SAVPF");
6169 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
6170 GST_DEBUG_OBJECT (src, "adding UDP unicast");
6172 g_string_append (result, "/UDP");
6173 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
6174 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
6175 GST_DEBUG_OBJECT (src, "adding UDP multicast");
6176 /* we don't have to allocate any UDP ports yet, if the selected transport
6177 * turns out to be multicast we can create them and join the multicast
6178 * group indicated in the transport reply */
6180 g_string_append (result, "/UDP");
6181 g_string_append (result, ";multicast");
6182 if (src->next_port_num != 0) {
6183 if (src->client_port_range.max > 0 &&
6184 src->next_port_num >= src->client_port_range.max)
6187 g_string_append_printf (result, ";client_port=%d-%d",
6188 src->next_port_num, src->next_port_num + 1);
6190 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
6191 GST_DEBUG_OBJECT (src, "adding TCP");
6193 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
6195 *transports = g_string_free (result, FALSE);
6197 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
6204 GST_ERROR ("extension gave error %d", res);
6209 GST_ERROR ("no more ports available");
6210 return GST_RTSP_ERROR;
6214 static GstRTSPResult
6215 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
6216 gint orig_rtpport, gint orig_rtcpport)
6219 gint nr_udp, nr_int;
6221 gint rtpport = 0, rtcpport = 0;
6224 src = stream->parent;
6226 /* find number of placeholders first */
6227 if (strstr (*transports, "%%i2"))
6229 else if (strstr (*transports, "%%i1"))
6234 if (strstr (*transports, "%%u2"))
6236 else if (strstr (*transports, "%%u1"))
6241 if (nr_udp == 0 && nr_int == 0)
6245 if (!orig_rtpport || !orig_rtcpport) {
6246 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
6249 rtpport = orig_rtpport;
6250 rtcpport = orig_rtcpport;
6254 str = g_string_new ("");
6256 while ((next = strstr (p, "%%"))) {
6257 g_string_append_len (str, p, next - p);
6258 if (next[2] == 'u') {
6260 g_string_append_printf (str, "%d", rtpport);
6261 else if (next[3] == '2')
6262 g_string_append_printf (str, "%d", rtcpport);
6264 if (next[2] == 'i') {
6266 g_string_append_printf (str, "%d", src->free_channel);
6267 else if (next[3] == '2')
6268 g_string_append_printf (str, "%d", src->free_channel + 1);
6273 /* append final part */
6274 g_string_append (str, p);
6276 g_free (*transports);
6277 *transports = g_string_free (str, FALSE);
6285 GST_ERROR ("failed to allocate udp ports");
6286 return GST_RTSP_ERROR;
6291 enc_key_length_from_cipher_name (const gchar * cipher)
6293 if (g_strcmp0 (cipher, "aes-128-icm") == 0)
6294 return AES_128_KEY_LEN;
6295 else if (g_strcmp0 (cipher, "aes-256-icm") == 0)
6296 return AES_256_KEY_LEN;
6298 GST_ERROR ("encryption algorithm '%s' not supported", cipher);
6304 auth_key_length_from_auth_name (const gchar * auth)
6306 if (g_strcmp0 (auth, "hmac-sha1-32") == 0)
6307 return HMAC_32_KEY_LEN;
6308 else if (g_strcmp0 (auth, "hmac-sha1-80") == 0)
6309 return HMAC_80_KEY_LEN;
6311 GST_ERROR ("authentication algorithm '%s' not supported", auth);
6317 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
6319 GstCaps *caps = NULL;
6321 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
6325 GST_DEBUG_OBJECT (src, "SRTP parameters received");
6331 default_srtcp_params (void)
6339 /* create a random key */
6340 key_data = g_malloc (KEY_SIZE);
6341 for (i = 0; i < KEY_SIZE; i += 4)
6342 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
6344 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
6346 caps = gst_caps_new_simple ("application/x-srtp",
6347 "srtp-key", GST_TYPE_BUFFER, buf,
6348 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
6349 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
6351 gst_buffer_unref (buf);
6357 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
6360 gchar *result, *base64;
6363 GstMIKEYMessage *msg;
6364 GstMIKEYPayload *payload, *pkd;
6370 const gchar *srtcpcipher, *srtcpauth;
6372 stream->srtcpparams = signal_get_srtcp_params (src, stream);
6373 if (stream->srtcpparams == NULL)
6374 stream->srtcpparams = default_srtcp_params ();
6376 s = gst_caps_get_structure (stream->srtcpparams, 0);
6378 srtcpcipher = gst_structure_get_string (s, "srtcp-cipher");
6379 srtcpauth = gst_structure_get_string (s, "srtcp-auth");
6380 val = gst_structure_get_value (s, "srtp-key");
6382 if (srtcpcipher == NULL || srtcpauth == NULL || val == NULL) {
6383 GST_ERROR_OBJECT (src, "could not find the right SRTP parameters in caps");
6387 srtpkey = gst_value_get_buffer (val);
6389 msg = gst_mikey_message_new ();
6390 /* unencrypted MIKEY message, we send this over TLS so this is allowed */
6391 gst_mikey_message_set_info (msg, GST_MIKEY_VERSION, GST_MIKEY_TYPE_PSK_INIT,
6392 FALSE, GST_MIKEY_PRF_MIKEY_1, g_random_int (), GST_MIKEY_MAP_TYPE_SRTP);
6393 /* add policy '0' for our SSRC */
6394 gst_mikey_message_add_cs_srtp (msg, 0, stream->send_ssrc, 0);
6395 /* timestamp is now */
6396 gst_mikey_message_add_t_now_ntp_utc (msg);
6397 /* add some random data */
6398 gst_mikey_message_add_rand_len (msg, 16);
6400 /* the policy '0' is SRTP */
6401 payload = gst_mikey_payload_new (GST_MIKEY_PT_SP);
6402 gst_mikey_payload_sp_set (payload, 0, GST_MIKEY_SEC_PROTO_SRTP);
6404 /* only AES-CM is supported */
6406 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_ALG, 1, &byte);
6407 /* encryption key length */
6408 byte = enc_key_length_from_cipher_name (srtcpcipher);
6409 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_KEY_LEN, 1,
6411 /* only HMAC-SHA1 */
6412 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_ALG, 1,
6414 /* authentication key length */
6415 byte = auth_key_length_from_auth_name (srtcpauth);
6416 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_KEY_LEN, 1,
6418 /* we enable encryption on RTP and RTCP */
6419 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_ENC, 1,
6421 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTCP_ENC, 1,
6423 /* we enable authentication on RTP and RTCP */
6424 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_AUTH, 1,
6426 gst_mikey_message_add_payload (msg, payload);
6428 /* make unencrypted KEMAC */
6429 payload = gst_mikey_payload_new (GST_MIKEY_PT_KEMAC);
6430 gst_mikey_payload_kemac_set (payload, GST_MIKEY_ENC_NULL, GST_MIKEY_MAC_NULL);
6431 /* add the key in KEMAC */
6432 pkd = gst_mikey_payload_new (GST_MIKEY_PT_KEY_DATA);
6433 gst_buffer_map (srtpkey, &info, GST_MAP_READ);
6434 gst_mikey_payload_key_data_set_key (pkd, GST_MIKEY_KD_TEK, info.size,
6436 gst_buffer_unmap (srtpkey, &info);
6437 gst_mikey_payload_kemac_add_sub (payload, pkd);
6438 gst_mikey_message_add_payload (msg, payload);
6440 /* now serialize this to bytes */
6441 bytes = gst_mikey_message_to_bytes (msg, NULL, NULL);
6442 gst_mikey_message_unref (msg);
6443 /* and make it into base64 */
6444 data = g_bytes_get_data (bytes, &size);
6445 base64 = g_base64_encode (data, size);
6446 g_bytes_unref (bytes);
6448 result = g_strdup_printf ("prot=mikey;uri=\"%s\";data=\"%s\"",
6449 stream->conninfo.location, base64);
6456 /* Perform the SETUP request for all the streams.
6458 * We ask the server for a specific transport, which initially includes all the
6459 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
6460 * two local UDP ports that we send to the server.
6462 * Once the server replied with a transport, we configure the other streams
6463 * with the same transport.
6465 * This function will also configure the stream for the selected transport,
6466 * which basically means creating the pipeline.
6468 static GstRTSPResult
6469 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
6472 GstRTSPResult res = GST_RTSP_ERROR;
6473 GstRTSPMessage request = { 0 };
6474 GstRTSPMessage response = { 0 };
6475 GstRTSPStream *stream = NULL;
6476 GstRTSPLowerTrans protocols;
6477 GstRTSPStatusCode code;
6478 gboolean unsupported_real = FALSE;
6479 gint rtpport, rtcpport;
6483 if (src->conninfo.connection) {
6484 url = gst_rtsp_connection_get_url (src->conninfo.connection);
6485 /* we initially allow all configured lower transports. based on the URL
6486 * transports and the replies from the server we narrow them down. */
6487 protocols = url->transports & src->cur_protocols;
6490 protocols = src->cur_protocols;
6496 /* reset some state */
6497 src->free_channel = 0;
6498 src->interleaved = FALSE;
6499 src->need_activate = FALSE;
6500 /* keep track of next port number, 0 is random */
6501 src->next_port_num = src->client_port_range.min;
6502 rtpport = rtcpport = 0;
6504 if (G_UNLIKELY (src->streams == NULL))
6507 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6508 GstRTSPConnection *conn;
6515 stream = (GstRTSPStream *) walk->data;
6517 caps = stream_get_caps_for_pt (stream, stream->default_pt);
6519 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
6523 if (stream->skipped) {
6524 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
6528 /* see if we need to configure this stream */
6529 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
6530 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
6535 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
6536 stream->id, caps, &selected);
6538 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
6542 /* merge/overwrite global caps */
6547 s = gst_caps_get_structure (caps, 0);
6549 num = gst_structure_n_fields (src->props);
6550 for (j = 0; j < num; j++) {
6554 name = gst_structure_nth_field_name (src->props, j);
6555 val = gst_structure_get_value (src->props, name);
6556 gst_structure_set_value (s, name, val);
6558 GST_DEBUG_OBJECT (src, "copied %s", name);
6562 /* skip setup if we have no URL for it */
6563 if (stream->conninfo.location == NULL) {
6564 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
6568 if (src->conninfo.connection == NULL) {
6569 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
6570 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
6573 conn = stream->conninfo.connection;
6575 conn = src->conninfo.connection;
6577 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
6578 stream->conninfo.location);
6580 /* if we have a multicast connection, only suggest multicast from now on */
6581 if (stream->is_multicast)
6582 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
6585 /* first selectable protocol */
6586 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6588 if (!protocol_masks[mask])
6592 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
6593 protocol_masks[mask]);
6594 /* create a string with first transport in line */
6596 res = gst_rtspsrc_create_transports_string (src,
6597 protocols & protocol_masks[mask], stream->profile, &transports);
6598 if (res < 0 || transports == NULL)
6599 goto setup_transport_failed;
6601 if (strlen (transports) == 0) {
6602 g_free (transports);
6603 GST_DEBUG_OBJECT (src, "no transports found");
6608 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
6610 /* replace placeholders with real values, this function will optionally
6611 * allocate UDP ports and other info needed to execute the setup request */
6612 res = gst_rtspsrc_prepare_transports (stream, &transports,
6613 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
6615 g_free (transports);
6616 goto setup_transport_failed;
6619 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
6621 /* create SETUP request */
6623 gst_rtspsrc_init_request (src, &request, GST_RTSP_SETUP,
6624 stream->conninfo.location);
6626 g_free (transports);
6627 goto create_request_failed;
6630 /* select transport */
6631 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
6634 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
6635 stream->profile == GST_RTSP_PROFILE_SAVPF) {
6636 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
6637 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
6640 /* if the user wants a non default RTP packet size we add the blocksize
6642 if (src->rtp_blocksize > 0) {
6643 hval = g_strdup_printf ("%d", src->rtp_blocksize);
6644 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
6648 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
6651 /* handle the code ourselves */
6652 res = gst_rtspsrc_send (src, conn, &request, &response, &code);
6657 case GST_RTSP_STS_OK:
6659 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
6660 gst_rtsp_message_unset (&request);
6661 gst_rtsp_message_unset (&response);
6662 /* cleanup of leftover transport */
6663 gst_rtspsrc_stream_free_udp (stream);
6664 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
6665 * we might be in this case */
6666 if (stream->container && rtpport && rtcpport && !retry) {
6667 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
6672 /* this transport did not go down well, but we may have others to try
6673 * that we did not send yet, try those and only give up then
6674 * but not without checking for lost cause/extension so we can
6675 * post a nicer/more useful error message later */
6676 if (!unsupported_real)
6677 unsupported_real = stream->is_real;
6678 /* select next available protocol, give up on this stream if none */
6680 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6682 if (!protocol_masks[mask] || unsupported_real)
6687 /* cleanup of leftover transport and move to the next stream */
6688 gst_rtspsrc_stream_free_udp (stream);
6689 goto response_error;
6692 /* parse response transport */
6694 gchar *resptrans = NULL;
6695 GstRTSPTransport transport = { 0 };
6697 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
6700 gst_rtspsrc_stream_free_udp (stream);
6704 /* parse transport, go to next stream on parse error */
6705 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6706 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6710 /* update allowed transports for other streams. once the transport of
6711 * one stream has been determined, we make sure that all other streams
6712 * are configured in the same way */
6713 switch (transport.lower_transport) {
6714 case GST_RTSP_LOWER_TRANS_TCP:
6715 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6716 protocols = GST_RTSP_LOWER_TRANS_TCP;
6717 src->interleaved = TRUE;
6718 /* update free channels */
6720 MAX (transport.interleaved.min, src->free_channel);
6722 MAX (transport.interleaved.max, src->free_channel);
6723 src->free_channel++;
6725 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6726 /* only allow multicast for other streams */
6727 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6728 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6729 /* if the server selected our ports, increment our counters so that
6730 * we select a new port later */
6731 if (src->next_port_num == transport.port.min &&
6732 src->next_port_num + 1 == transport.port.max) {
6733 src->next_port_num += 2;
6736 case GST_RTSP_LOWER_TRANS_UDP:
6737 /* only allow unicast for other streams */
6738 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6739 protocols = GST_RTSP_LOWER_TRANS_UDP;
6742 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6743 transport.lower_transport);
6747 if (!src->interleaved || !retry) {
6748 /* now configure the stream with the selected transport */
6749 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6750 GST_DEBUG_OBJECT (src,
6751 "could not configure stream %p transport, skipping stream",
6754 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
6755 /* retain the first allocated UDP port pair */
6756 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
6757 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
6760 /* we need to activate at least one streams when we detect activity */
6761 src->need_activate = TRUE;
6763 /* stream is setup now */
6764 stream->setup = TRUE;
6769 GstRTSPStream *sskip;
6771 skip = g_list_next (skip);
6775 sskip = (GstRTSPStream *) skip->data;
6777 /* skip all streams with the same control url */
6778 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
6779 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
6780 sskip, sskip->conninfo.location);
6781 sskip->skipped = TRUE;
6786 /* clean up our transport struct */
6787 gst_rtsp_transport_init (&transport);
6788 /* clean up used RTSP messages */
6789 gst_rtsp_message_unset (&request);
6790 gst_rtsp_message_unset (&response);
6794 /* store the transport protocol that was configured */
6795 src->cur_protocols = protocols;
6797 gst_rtsp_ext_list_stream_select (src->extensions, url);
6799 /* if there is nothing to activate, error out */
6800 if (!src->need_activate)
6801 goto nothing_to_activate;
6808 /* no transport possible, post an error and stop */
6809 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6810 ("Could not connect to server, no protocols left"));
6811 return GST_RTSP_ERROR;
6815 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6816 ("SDP contains no streams"));
6817 return GST_RTSP_ERROR;
6819 create_request_failed:
6821 gchar *str = gst_rtsp_strresult (res);
6823 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6824 ("Could not create request. (%s)", str));
6828 setup_transport_failed:
6830 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6831 ("Could not setup transport."));
6832 res = GST_RTSP_ERROR;
6837 const gchar *str = gst_rtsp_status_as_text (code);
6839 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6840 ("Error (%d): %s", code, GST_STR_NULL (str)));
6841 res = GST_RTSP_ERROR;
6846 gchar *str = gst_rtsp_strresult (res);
6848 if (res != GST_RTSP_EINTR) {
6849 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6850 ("Could not send message. (%s)", str));
6852 GST_WARNING_OBJECT (src, "send interrupted");
6859 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6860 ("Server did not select transport."));
6861 res = GST_RTSP_ERROR;
6864 nothing_to_activate:
6866 /* none of the available error codes is really right .. */
6867 if (unsupported_real) {
6868 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6869 (_("No supported stream was found. You might need to install a "
6870 "GStreamer RTSP extension plugin for Real media streams.")),
6873 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6874 (_("No supported stream was found. You might need to allow "
6875 "more transport protocols or may otherwise be missing "
6876 "the right GStreamer RTSP extension plugin.")), (NULL));
6878 return GST_RTSP_ERROR;
6882 gst_rtsp_message_unset (&request);
6883 gst_rtsp_message_unset (&response);
6889 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
6890 GstSegment * segment)
6893 GstRTSPTimeRange *therange;
6896 gst_rtsp_range_free (src->range);
6898 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
6899 GST_DEBUG_OBJECT (src, "parsed range %s", range);
6900 src->range = therange;
6902 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6904 gst_segment_init (segment, GST_FORMAT_TIME);
6908 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6909 therange->min.type, therange->min.seconds, therange->max.type,
6910 therange->max.seconds);
6912 if (therange->min.type == GST_RTSP_TIME_NOW)
6914 else if (therange->min.type == GST_RTSP_TIME_END)
6917 seconds = therange->min.seconds * GST_SECOND;
6919 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6920 GST_TIME_ARGS (seconds));
6922 /* we need to start playback without clipping from the position reported by
6924 segment->start = seconds;
6925 segment->position = seconds;
6927 if (therange->max.type == GST_RTSP_TIME_NOW)
6929 else if (therange->max.type == GST_RTSP_TIME_END)
6932 seconds = therange->max.seconds * GST_SECOND;
6934 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6935 GST_TIME_ARGS (seconds));
6937 /* live (WMS) server might send overflowed large max as its idea of infinity,
6938 * compensate to prevent problems later on */
6939 if (seconds != -1 && seconds < 0) {
6941 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6944 /* live (WMS) might send min == max, which is not worth recording */
6945 if (segment->duration == -1 && seconds == segment->start)
6948 /* don't change duration with unknown value, we might have a valid value
6949 * there that we want to keep. */
6951 segment->duration = seconds;
6956 /* Parse clock profived by the server with following syntax:
6958 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6961 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6963 gboolean res = FALSE;
6965 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6966 gchar **fields = NULL, **parts = NULL;
6967 gchar *remote_ip, *str;
6969 GstClockTime base_time;
6972 fields = g_strsplit (gstclock, " ", 0);
6974 /* wrapped clock, not very interesting for now */
6975 if (fields[1] == NULL)
6978 /* remote IP address and port */
6979 if ((str = fields[2]) == NULL)
6982 parts = g_strsplit (str, ":", 0);
6984 if ((remote_ip = parts[0]) == NULL)
6987 if ((str = parts[1]) == NULL)
6995 if ((str = fields[3]) == NULL)
6998 base_time = g_ascii_strtoull (str, NULL, 10);
7001 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
7004 if (src->provided_clock)
7005 gst_object_unref (src->provided_clock);
7006 src->provided_clock = netclock;
7008 gst_element_post_message (GST_ELEMENT_CAST (src),
7009 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
7010 src->provided_clock, TRUE));
7014 g_strfreev (fields);
7020 /* must be called with the RTSP state lock */
7021 static GstRTSPResult
7022 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
7028 /* prepare global stream caps properties */
7030 gst_structure_remove_all_fields (src->props);
7032 src->props = gst_structure_new_empty ("RTSPProperties");
7035 gst_sdp_message_dump (sdp);
7037 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
7039 /* let the app inspect and change the SDP */
7040 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
7042 gst_segment_init (&src->segment, GST_FORMAT_TIME);
7044 /* parse range for duration reporting. */
7049 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
7053 /* keep track of the range and configure it in the segment */
7054 if (gst_rtspsrc_parse_range (src, range, &src->segment))
7058 /* parse clock information. This is GStreamer specific, a server can tell the
7059 * client what clock it is using and wrap that in a network clock. The
7060 * advantage of that is that we can slave to it. */
7062 const gchar *gstclock;
7065 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
7066 if (gstclock == NULL)
7069 /* parse the clock and expose it in the provide_clock method */
7070 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
7074 /* try to find a global control attribute. Note that a '*' means that we should
7075 * do aggregate control with the current url (so we don't do anything and
7076 * leave the current connection as is) */
7078 const gchar *control;
7081 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
7082 if (control == NULL)
7085 /* only take fully qualified urls */
7086 if (g_str_has_prefix (control, "rtsp://"))
7090 g_free (src->conninfo.location);
7091 src->conninfo.location = g_strdup (control);
7092 /* make a connection for this, if there was a connection already, nothing
7094 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
7095 GST_ERROR_OBJECT (src, "could not connect");
7098 /* we need to keep the control url separate from the connection url because
7099 * the rules for constructing the media control url need it */
7100 g_free (src->control);
7101 src->control = g_strdup (control);
7104 /* create streams */
7105 n_streams = gst_sdp_message_medias_len (sdp);
7106 for (i = 0; i < n_streams; i++) {
7107 gst_rtspsrc_create_stream (src, sdp, i);
7110 src->state = GST_RTSP_STATE_INIT;
7113 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
7116 /* reset our state */
7117 src->need_range = TRUE;
7120 src->state = GST_RTSP_STATE_READY;
7127 GST_ERROR_OBJECT (src, "setup failed");
7128 gst_rtspsrc_cleanup (src);
7133 static GstRTSPResult
7134 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
7138 GstRTSPMessage request = { 0 };
7139 GstRTSPMessage response = { 0 };
7142 gchar *respcont = NULL;
7145 src->need_redirect = FALSE;
7147 /* can't continue without a valid url */
7148 if (G_UNLIKELY (src->conninfo.url == NULL)) {
7149 res = GST_RTSP_EINVAL;
7152 src->tried_url_auth = FALSE;
7154 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
7155 goto connect_failed;
7157 /* create OPTIONS */
7158 GST_DEBUG_OBJECT (src, "create options...");
7160 gst_rtspsrc_init_request (src, &request, GST_RTSP_OPTIONS,
7161 src->conninfo.url_str);
7163 goto create_request_failed;
7166 GST_DEBUG_OBJECT (src, "send options...");
7169 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
7172 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
7177 if (!gst_rtspsrc_parse_methods (src, &response))
7180 /* create DESCRIBE */
7181 GST_DEBUG_OBJECT (src, "create describe...");
7183 gst_rtspsrc_init_request (src, &request, GST_RTSP_DESCRIBE,
7184 src->conninfo.url_str);
7186 goto create_request_failed;
7188 /* we only accept SDP for now */
7189 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
7193 GST_DEBUG_OBJECT (src, "send describe...");
7196 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
7199 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
7203 /* we only perform redirect for the describe, currently */
7204 if (src->need_redirect) {
7205 /* close connection, we don't have to send a TEARDOWN yet, ignore the
7207 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7209 gst_rtsp_message_unset (&request);
7210 gst_rtsp_message_unset (&response);
7216 /* it could be that the DESCRIBE method was not implemented */
7217 if (!(src->methods & GST_RTSP_DESCRIBE))
7220 /* check if reply is SDP */
7221 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
7223 /* could not be set but since the request returned OK, we assume it
7224 * was SDP, else check it. */
7226 if (g_ascii_strcasecmp (respcont, "application/sdp") != 0)
7227 goto wrong_content_type;
7230 /* get message body and parse as SDP */
7231 gst_rtsp_message_get_body (&response, &data, &size);
7232 if (data == NULL || size == 0)
7235 GST_DEBUG_OBJECT (src, "parse SDP...");
7236 gst_sdp_message_new (sdp);
7237 gst_sdp_message_parse_buffer (data, size, *sdp);
7239 /* clean up any messages */
7240 gst_rtsp_message_unset (&request);
7241 gst_rtsp_message_unset (&response);
7248 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
7249 ("No valid RTSP URL was provided"));
7254 gchar *str = gst_rtsp_strresult (res);
7256 if (res != GST_RTSP_EINTR) {
7257 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
7258 ("Failed to connect. (%s)", str));
7260 GST_WARNING_OBJECT (src, "connect interrupted");
7265 create_request_failed:
7267 gchar *str = gst_rtsp_strresult (res);
7269 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7270 ("Could not create request. (%s)", str));
7276 /* Don't post a message - the rtsp_send method will have
7277 * taken care of it because we passed NULL for the response code */
7282 /* error was posted */
7283 res = GST_RTSP_ERROR;
7288 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7289 ("Server does not support SDP, got %s.", respcont));
7290 res = GST_RTSP_ERROR;
7295 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7296 ("Server can not provide an SDP."));
7297 res = GST_RTSP_ERROR;
7302 if (src->conninfo.connection) {
7303 GST_DEBUG_OBJECT (src, "free connection");
7304 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7306 gst_rtsp_message_unset (&request);
7307 gst_rtsp_message_unset (&response);
7312 static GstRTSPResult
7313 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
7318 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
7320 if (src->sdp == NULL) {
7321 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
7325 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
7330 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
7337 GST_WARNING_OBJECT (src, "can't get sdp");
7338 src->open_error = TRUE;
7343 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
7344 src->open_error = TRUE;
7349 static GstRTSPResult
7350 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
7352 GstRTSPMessage request = { 0 };
7353 GstRTSPMessage response = { 0 };
7354 GstRTSPResult res = GST_RTSP_OK;
7356 const gchar *control;
7358 GST_DEBUG_OBJECT (src, "TEARDOWN...");
7360 gst_rtspsrc_set_state (src, GST_STATE_READY);
7362 if (src->state < GST_RTSP_STATE_READY) {
7363 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
7370 /* construct a control url */
7371 control = get_aggregate_control (src);
7373 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
7376 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7377 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7378 const gchar *setup_url;
7379 GstRTSPConnInfo *info;
7381 /* try aggregate control first but do non-aggregate control otherwise */
7383 setup_url = control;
7384 else if ((setup_url = stream->conninfo.location) == NULL)
7387 if (src->conninfo.connection) {
7388 info = &src->conninfo;
7389 } else if (stream->conninfo.connection) {
7390 info = &stream->conninfo;
7394 if (!info->connected)
7399 gst_rtspsrc_init_request (src, &request, GST_RTSP_TEARDOWN, setup_url);
7401 goto create_request_failed;
7404 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
7407 gst_rtspsrc_send (src, info->connection, &request, &response,
7411 /* FIXME, parse result? */
7412 gst_rtsp_message_unset (&request);
7413 gst_rtsp_message_unset (&response);
7416 /* early exit when we did aggregate control */
7422 /* close connections */
7423 GST_DEBUG_OBJECT (src, "closing connection...");
7424 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7425 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7426 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7427 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
7431 gst_rtspsrc_cleanup (src);
7433 src->state = GST_RTSP_STATE_INVALID;
7436 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
7441 create_request_failed:
7443 gchar *str = gst_rtsp_strresult (res);
7445 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7446 ("Could not create request. (%s)", str));
7452 gchar *str = gst_rtsp_strresult (res);
7454 gst_rtsp_message_unset (&request);
7455 if (res != GST_RTSP_EINTR) {
7456 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7457 ("Could not send message. (%s)", str));
7459 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
7466 GST_DEBUG_OBJECT (src,
7467 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
7472 /* RTP-Info is of the format:
7474 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
7476 * rtptime corresponds to the timestamp for the NPT time given in the header
7477 * seqbase corresponds to the next sequence number we received. This number
7478 * indicates the first seqnum after the seek and should be used to discard
7479 * packets that are from before the seek.
7482 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
7487 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
7489 infos = g_strsplit (rtpinfo, ",", 0);
7490 for (i = 0; infos[i]; i++) {
7492 GstRTSPStream *stream;
7496 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
7498 /* init values, types of seqbase and timebase are bigger than needed so we
7499 * can store -1 as uninitialized values */
7504 /* parse url, find stream for url.
7505 * parse seq and rtptime. The seq number should be configured in the rtp
7506 * depayloader or session manager to detect gaps. Same for the rtptime, it
7507 * should be used to create an initial time newsegment. */
7508 fields = g_strsplit (infos[i], ";", 0);
7509 for (j = 0; fields[j]; j++) {
7510 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
7511 /* remove leading whitespace */
7512 fields[j] = g_strchug (fields[j]);
7513 if (g_str_has_prefix (fields[j], "url=")) {
7514 /* get the url and the stream */
7516 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
7517 } else if (g_str_has_prefix (fields[j], "seq=")) {
7518 seqbase = atoi (fields[j] + 4);
7519 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
7520 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
7523 g_strfreev (fields);
7524 /* now we need to store the values for the caps of the stream */
7525 if (stream != NULL) {
7526 GST_DEBUG_OBJECT (src,
7527 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
7528 stream, seqbase, timebase);
7530 /* we have a stream, configure detected params */
7531 stream->seqbase = seqbase;
7532 stream->timebase = timebase;
7541 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
7546 interval = strtoul (rtcp, NULL, 10);
7547 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
7552 interval *= GST_MSECOND;
7554 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7555 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7557 /* already (optionally) retrieved this when configuring manager */
7558 if (stream->session) {
7559 GObject *rtpsession = stream->session;
7561 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
7563 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
7567 /* now it happens that (Xenon) server sending this may also provide bogus
7568 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
7569 * and just use RTP-Info to sync */
7571 GObjectClass *klass;
7573 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
7574 if (g_object_class_find_property (klass, "rtcp-sync")) {
7575 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
7576 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
7582 gst_rtspsrc_get_float (const gchar * dstr)
7584 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7586 /* canonicalise floating point string so we can handle float strings
7587 * in the form "24.930" or "24,930" irrespective of the current locale */
7588 g_strlcpy (s, dstr, sizeof (s));
7589 g_strdelimit (s, ",", '.');
7590 return g_ascii_strtod (s, NULL);
7594 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
7596 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7598 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
7599 g_strlcpy (val_str, "now", sizeof (val_str));
7601 if (segment->position == 0) {
7602 g_strlcpy (val_str, "0", sizeof (val_str));
7604 g_ascii_dtostr (val_str, sizeof (val_str),
7605 ((gdouble) segment->position) / GST_SECOND);
7608 return g_strdup_printf ("npt=%s-", val_str);
7612 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
7616 stream->timebase = -1;
7617 stream->seqbase = -1;
7619 len = stream->ptmap->len;
7620 for (i = 0; i < len; i++) {
7621 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
7624 if (item->caps == NULL)
7627 item->caps = gst_caps_make_writable (item->caps);
7628 s = gst_caps_get_structure (item->caps, 0);
7629 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
7633 static GstRTSPResult
7634 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
7636 GstRTSPResult res = GST_RTSP_OK;
7638 if (src->state < GST_RTSP_STATE_READY) {
7639 res = GST_RTSP_ERROR;
7640 if (src->open_error) {
7641 GST_DEBUG_OBJECT (src, "the stream was in error");
7645 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
7647 if ((res = gst_rtspsrc_open (src, async)) < 0) {
7648 GST_DEBUG_OBJECT (src, "failed to open stream");
7657 static GstRTSPResult
7658 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
7660 GstRTSPMessage request = { 0 };
7661 GstRTSPMessage response = { 0 };
7662 GstRTSPResult res = GST_RTSP_OK;
7666 const gchar *control;
7668 GST_DEBUG_OBJECT (src, "PLAY...");
7670 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7673 if (!(src->methods & GST_RTSP_PLAY))
7676 if (src->state == GST_RTSP_STATE_PLAYING)
7679 if (!src->conninfo.connection || !src->conninfo.connected)
7682 /* send some dummy packets before we activate the receive in the
7684 gst_rtspsrc_send_dummy_packets (src);
7686 /* require new SR packets */
7688 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
7690 /* construct a control url */
7691 control = get_aggregate_control (src);
7693 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7694 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7695 const gchar *setup_url;
7696 GstRTSPConnection *conn;
7698 /* try aggregate control first but do non-aggregate control otherwise */
7700 setup_url = control;
7701 else if ((setup_url = stream->conninfo.location) == NULL)
7704 if (src->conninfo.connection) {
7705 conn = src->conninfo.connection;
7706 } else if (stream->conninfo.connection) {
7707 conn = stream->conninfo.connection;
7713 res = gst_rtspsrc_init_request (src, &request, GST_RTSP_PLAY, setup_url);
7715 goto create_request_failed;
7717 if (src->need_range) {
7718 hval = gen_range_header (src, segment);
7720 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
7722 /* store the newsegment event so it can be sent from the streaming thread. */
7723 src->need_segment = TRUE;
7726 if (segment->rate != 1.0) {
7727 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
7729 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
7731 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
7733 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
7737 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
7739 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7742 /* seek may have silently failed as it is not supported */
7743 if (!(src->methods & GST_RTSP_PLAY)) {
7744 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
7745 /* obviously it is supported as we made it here */
7746 src->methods |= GST_RTSP_PLAY;
7747 src->seekable = FALSE;
7748 /* but there is nothing to parse in the response,
7749 * so convey we have no idea and not to expect anything particular */
7750 clear_rtp_base (src, stream);
7754 /* need to do for all streams */
7755 for (run = src->streams; run; run = g_list_next (run))
7756 clear_rtp_base (src, (GstRTSPStream *) run->data);
7758 /* NOTE the above also disables npt based eos detection */
7759 /* and below forces position to 0,
7760 * which is visible feedback we lost the plot */
7761 segment->start = segment->position = src->last_pos;
7764 gst_rtsp_message_unset (&request);
7766 /* parse RTP npt field. This is the current position in the stream (Normal
7767 * Play Time) and should be put in the NEWSEGMENT position field. */
7768 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
7770 gst_rtspsrc_parse_range (src, hval, segment);
7772 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
7773 segment->rate = 1.0;
7775 /* parse Speed header. This is the intended playback rate of the stream
7776 * and should be put in the NEWSEGMENT rate field. */
7777 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
7778 0) == GST_RTSP_OK) {
7779 segment->rate = gst_rtspsrc_get_float (hval);
7780 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
7781 &hval, 0) == GST_RTSP_OK) {
7782 segment->rate = gst_rtspsrc_get_float (hval);
7785 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
7786 * for the RTP packets. If this is not present, we assume all starts from 0...
7787 * This is info for the RTP session manager that we pass to it in caps. */
7789 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
7790 &hval, hval_idx++) == GST_RTSP_OK)
7791 gst_rtspsrc_parse_rtpinfo (src, hval);
7793 /* some servers indicate RTCP parameters in PLAY response,
7794 * rather than properly in SDP */
7795 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
7796 &hval, 0) == GST_RTSP_OK)
7797 gst_rtspsrc_handle_rtcp_interval (src, hval);
7799 gst_rtsp_message_unset (&response);
7801 /* early exit when we did aggregate control */
7805 /* configure the caps of the streams after we parsed all headers. Only reset
7806 * the manager object when we set a new Range header (we did a seek) */
7807 gst_rtspsrc_configure_caps (src, segment, src->need_range);
7809 /* set to PLAYING after we have configured the caps, otherwise we
7810 * might end up calling request_key (with SRTP) while caps are still
7811 * being configured. */
7812 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
7814 /* set again when needed */
7815 src->need_range = FALSE;
7817 src->running = TRUE;
7818 src->base_time = -1;
7819 src->state = GST_RTSP_STATE_PLAYING;
7822 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
7823 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7824 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7825 stream->discont = TRUE;
7830 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
7837 GST_DEBUG_OBJECT (src, "failed to open stream");
7842 GST_DEBUG_OBJECT (src, "PLAY is not supported");
7847 GST_DEBUG_OBJECT (src, "we were already PLAYING");
7850 create_request_failed:
7852 gchar *str = gst_rtsp_strresult (res);
7854 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7855 ("Could not create request. (%s)", str));
7861 gchar *str = gst_rtsp_strresult (res);
7863 gst_rtsp_message_unset (&request);
7864 if (res != GST_RTSP_EINTR) {
7865 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7866 ("Could not send message. (%s)", str));
7868 GST_WARNING_OBJECT (src, "PLAY interrupted");
7875 static GstRTSPResult
7876 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
7878 GstRTSPResult res = GST_RTSP_OK;
7879 GstRTSPMessage request = { 0 };
7880 GstRTSPMessage response = { 0 };
7882 const gchar *control;
7884 GST_DEBUG_OBJECT (src, "PAUSE...");
7886 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7889 if (!(src->methods & GST_RTSP_PAUSE))
7892 if (src->state == GST_RTSP_STATE_READY)
7895 if (!src->conninfo.connection || !src->conninfo.connected)
7898 /* construct a control url */
7899 control = get_aggregate_control (src);
7901 /* loop over the streams. We might exit the loop early when we could do an
7902 * aggregate control */
7903 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7904 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7905 GstRTSPConnection *conn;
7906 const gchar *setup_url;
7908 /* try aggregate control first but do non-aggregate control otherwise */
7910 setup_url = control;
7911 else if ((setup_url = stream->conninfo.location) == NULL)
7914 if (src->conninfo.connection) {
7915 conn = src->conninfo.connection;
7916 } else if (stream->conninfo.connection) {
7917 conn = stream->conninfo.connection;
7923 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7924 ("Sending PAUSE request"));
7927 gst_rtspsrc_init_request (src, &request, GST_RTSP_PAUSE,
7929 goto create_request_failed;
7931 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7934 gst_rtsp_message_unset (&request);
7935 gst_rtsp_message_unset (&response);
7937 /* exit early when we did agregate control */
7942 /* change element states now */
7943 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7946 src->state = GST_RTSP_STATE_READY;
7950 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7957 GST_DEBUG_OBJECT (src, "failed to open stream");
7962 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7967 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7970 create_request_failed:
7972 gchar *str = gst_rtsp_strresult (res);
7974 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7975 ("Could not create request. (%s)", str));
7981 gchar *str = gst_rtsp_strresult (res);
7983 gst_rtsp_message_unset (&request);
7984 if (res != GST_RTSP_EINTR) {
7985 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7986 ("Could not send message. (%s)", str));
7988 GST_WARNING_OBJECT (src, "PAUSE interrupted");
7996 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
7998 GstRTSPSrc *rtspsrc;
8000 rtspsrc = GST_RTSPSRC (bin);
8002 switch (GST_MESSAGE_TYPE (message)) {
8003 case GST_MESSAGE_EOS:
8004 gst_message_unref (message);
8006 case GST_MESSAGE_ELEMENT:
8008 const GstStructure *s = gst_message_get_structure (message);
8010 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
8011 gboolean ignore_timeout;
8013 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
8015 GST_OBJECT_LOCK (rtspsrc);
8016 ignore_timeout = rtspsrc->ignore_timeout;
8017 rtspsrc->ignore_timeout = TRUE;
8018 GST_OBJECT_UNLOCK (rtspsrc);
8020 /* we only act on the first udp timeout message, others are irrelevant
8021 * and can be ignored. */
8022 if (!ignore_timeout)
8023 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
8025 gst_message_unref (message);
8028 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8031 case GST_MESSAGE_ERROR:
8034 GstRTSPStream *stream;
8037 udpsrc = GST_MESSAGE_SRC (message);
8039 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
8040 GST_ELEMENT_NAME (udpsrc));
8042 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
8046 /* we ignore the RTCP udpsrc */
8047 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
8050 /* if we get error messages from the udp sources, that's not a problem as
8051 * long as not all of them error out. We also don't really know what the
8052 * problem is, the message does not give enough detail... */
8053 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
8054 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
8055 if (ret != GST_FLOW_OK)
8059 gst_message_unref (message);
8063 /* fatal but not our message, forward */
8064 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8069 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
8075 /* the thread where everything happens */
8077 gst_rtspsrc_thread (GstRTSPSrc * src)
8081 GST_OBJECT_LOCK (src);
8082 cmd = src->pending_cmd;
8083 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
8084 || cmd == CMD_LOOP || cmd == CMD_OPEN)
8085 src->pending_cmd = CMD_LOOP;
8087 src->pending_cmd = CMD_WAIT;
8088 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
8090 /* we got the message command, so ensure communication is possible again */
8091 gst_rtspsrc_connection_flush (src, FALSE);
8093 src->busy_cmd = cmd;
8094 GST_OBJECT_UNLOCK (src);
8098 gst_rtspsrc_open (src, TRUE);
8101 gst_rtspsrc_play (src, &src->segment, TRUE);
8104 gst_rtspsrc_pause (src, TRUE);
8107 gst_rtspsrc_close (src, TRUE, FALSE);
8110 gst_rtspsrc_loop (src);
8113 gst_rtspsrc_reconnect (src, FALSE);
8119 GST_OBJECT_LOCK (src);
8120 /* and go back to sleep */
8121 if (src->pending_cmd == CMD_WAIT) {
8123 gst_task_pause (src->task);
8126 src->busy_cmd = CMD_WAIT;
8127 GST_OBJECT_UNLOCK (src);
8131 gst_rtspsrc_start (GstRTSPSrc * src)
8133 GST_DEBUG_OBJECT (src, "starting");
8135 GST_OBJECT_LOCK (src);
8137 src->pending_cmd = CMD_WAIT;
8139 if (src->task == NULL) {
8140 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
8141 if (src->task == NULL)
8144 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
8146 GST_OBJECT_UNLOCK (src);
8153 GST_OBJECT_UNLOCK (src);
8154 GST_ERROR_OBJECT (src, "failed to create task");
8160 gst_rtspsrc_stop (GstRTSPSrc * src)
8164 GST_DEBUG_OBJECT (src, "stopping");
8166 /* also cancels pending task */
8167 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
8169 GST_OBJECT_LOCK (src);
8170 if ((task = src->task)) {
8172 GST_OBJECT_UNLOCK (src);
8174 gst_task_stop (task);
8176 /* make sure it is not running */
8177 GST_RTSP_STREAM_LOCK (src);
8178 GST_RTSP_STREAM_UNLOCK (src);
8180 /* now wait for the task to finish */
8181 gst_task_join (task);
8183 /* and free the task */
8184 gst_object_unref (GST_OBJECT (task));
8186 GST_OBJECT_LOCK (src);
8188 GST_OBJECT_UNLOCK (src);
8190 /* ensure synchronously all is closed and clean */
8191 gst_rtspsrc_close (src, FALSE, TRUE);
8196 static GstStateChangeReturn
8197 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
8199 GstRTSPSrc *rtspsrc;
8200 GstStateChangeReturn ret;
8202 rtspsrc = GST_RTSPSRC (element);
8204 switch (transition) {
8205 case GST_STATE_CHANGE_NULL_TO_READY:
8206 if (!gst_rtspsrc_start (rtspsrc))
8209 case GST_STATE_CHANGE_READY_TO_PAUSED:
8210 /* init some state */
8211 rtspsrc->cur_protocols = rtspsrc->protocols;
8212 /* first attempt, don't ignore timeouts */
8213 rtspsrc->ignore_timeout = FALSE;
8214 rtspsrc->open_error = FALSE;
8215 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
8217 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8218 set_manager_buffer_mode (rtspsrc);
8220 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8221 /* unblock the tcp tasks and make the loop waiting */
8222 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
8223 /* make sure it is waiting before we send PAUSE or PLAY below */
8224 GST_RTSP_STREAM_LOCK (rtspsrc);
8225 GST_RTSP_STREAM_UNLOCK (rtspsrc);
8228 case GST_STATE_CHANGE_PAUSED_TO_READY:
8234 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
8235 if (ret == GST_STATE_CHANGE_FAILURE)
8238 switch (transition) {
8239 case GST_STATE_CHANGE_NULL_TO_READY:
8240 ret = GST_STATE_CHANGE_SUCCESS;
8242 case GST_STATE_CHANGE_READY_TO_PAUSED:
8243 ret = GST_STATE_CHANGE_NO_PREROLL;
8245 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8246 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
8247 ret = GST_STATE_CHANGE_SUCCESS;
8249 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8250 /* send pause request and keep the idle task around */
8251 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
8252 ret = GST_STATE_CHANGE_NO_PREROLL;
8254 case GST_STATE_CHANGE_PAUSED_TO_READY:
8255 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
8256 ret = GST_STATE_CHANGE_SUCCESS;
8258 case GST_STATE_CHANGE_READY_TO_NULL:
8259 gst_rtspsrc_stop (rtspsrc);
8260 ret = GST_STATE_CHANGE_SUCCESS;
8271 GST_DEBUG_OBJECT (rtspsrc, "start failed");
8272 return GST_STATE_CHANGE_FAILURE;
8277 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
8280 GstRTSPSrc *rtspsrc;
8282 rtspsrc = GST_RTSPSRC (element);
8284 if (GST_EVENT_IS_DOWNSTREAM (event)) {
8285 res = gst_rtspsrc_push_event (rtspsrc, event);
8287 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
8294 /*** GSTURIHANDLER INTERFACE *************************************************/
8297 gst_rtspsrc_uri_get_type (GType type)
8302 static const gchar *const *
8303 gst_rtspsrc_uri_get_protocols (GType type)
8305 static const gchar *protocols[] =
8306 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
8307 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
8314 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
8316 GstRTSPSrc *src = GST_RTSPSRC (handler);
8318 /* FIXME: make thread-safe */
8319 return g_strdup (src->conninfo.location);
8323 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
8329 GstRTSPUrl *newurl = NULL;
8330 GstSDPMessage *sdp = NULL;
8332 src = GST_RTSPSRC (handler);
8334 /* same URI, we're fine */
8335 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
8338 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
8339 sres = gst_sdp_message_new (&sdp);
8343 GST_DEBUG_OBJECT (src, "parsing SDP message");
8344 sres = gst_sdp_message_parse_uri (uri, sdp);
8349 GST_DEBUG_OBJECT (src, "parsing URI");
8350 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
8354 /* if worked, free previous and store new url object along with the original
8356 GST_DEBUG_OBJECT (src, "configuring URI");
8357 g_free (src->conninfo.location);
8358 src->conninfo.location = g_strdup (uri);
8359 gst_rtsp_url_free (src->conninfo.url);
8360 src->conninfo.url = newurl;
8361 g_free (src->conninfo.url_str);
8363 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
8365 src->conninfo.url_str = NULL;
8368 gst_sdp_message_free (src->sdp);
8370 src->from_sdp = sdp != NULL;
8372 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
8373 GST_DEBUG_OBJECT (src, "request uri is: %s",
8374 GST_STR_NULL (src->conninfo.url_str));
8381 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
8386 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
8387 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8388 "Could not create SDP");
8393 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
8394 GST_STR_NULL (uri));
8395 gst_sdp_message_free (sdp);
8396 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8402 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
8403 GST_STR_NULL (uri), res);
8404 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8405 "Invalid RTSP URI");
8411 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
8413 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
8415 iface->get_type = gst_rtspsrc_uri_get_type;
8416 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
8417 iface->get_uri = gst_rtspsrc_uri_get_uri;
8418 iface->set_uri = gst_rtspsrc_uri_set_uri;