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/gstrtppayloads.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 #define AES_128_KEY_LEN 16
168 #define AES_256_KEY_LEN 32
170 #define HMAC_32_KEY_LEN 4
171 #define HMAC_80_KEY_LEN 10
173 #define DEFAULT_LOCATION NULL
174 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
175 #define DEFAULT_DEBUG FALSE
176 #define DEFAULT_RETRY 20
177 #define DEFAULT_TIMEOUT 5000000
178 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
179 #define DEFAULT_TCP_TIMEOUT 20000000
180 #define DEFAULT_LATENCY_MS 2000
181 #define DEFAULT_DROP_ON_LATENCY FALSE
182 #define DEFAULT_CONNECTION_SPEED 0
183 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
184 #define DEFAULT_DO_RTCP TRUE
185 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
186 #define DEFAULT_PROXY NULL
187 #define DEFAULT_RTP_BLOCKSIZE 0
188 #define DEFAULT_USER_ID NULL
189 #define DEFAULT_USER_PW NULL
190 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
191 #define DEFAULT_PORT_RANGE NULL
192 #define DEFAULT_SHORT_HEADER FALSE
193 #define DEFAULT_PROBATION 2
194 #define DEFAULT_UDP_RECONNECT TRUE
195 #define DEFAULT_MULTICAST_IFACE NULL
196 #define DEFAULT_NTP_SYNC FALSE
197 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
198 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
199 #define DEFAULT_TLS_DATABASE NULL
200 #define DEFAULT_DO_RETRANSMISSION TRUE
212 PROP_DROP_ON_LATENCY,
213 PROP_CONNECTION_SPEED,
216 PROP_DO_RTSP_KEEP_ALIVE,
225 PROP_UDP_BUFFER_SIZE,
229 PROP_MULTICAST_IFACE,
231 PROP_USE_PIPELINE_CLOCK,
233 PROP_TLS_VALIDATION_FLAGS,
235 PROP_DO_RETRANSMISSION,
239 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
241 gst_rtsp_nat_method_get_type (void)
243 static GType rtsp_nat_method_type = 0;
244 static const GEnumValue rtsp_nat_method[] = {
245 {GST_RTSP_NAT_NONE, "None", "none"},
246 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
250 if (!rtsp_nat_method_type) {
251 rtsp_nat_method_type =
252 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
254 return rtsp_nat_method_type;
257 static void gst_rtspsrc_finalize (GObject * object);
259 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
260 const GValue * value, GParamSpec * pspec);
261 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
262 GValue * value, GParamSpec * pspec);
264 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
266 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
267 gpointer iface_data);
269 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
272 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
273 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
275 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
277 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
278 GstStateChange transition);
279 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
280 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
282 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
283 GstRTSPMessage * response);
285 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
287 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
288 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
290 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
291 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
293 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
294 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
295 gboolean only_close);
297 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
298 const gchar * uri, GError ** error);
299 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
301 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
302 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
303 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
304 GstRTSPStream * stream, GstEvent * event);
305 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
306 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
314 /* commands we send to out loop to notify it of events */
315 #define CMD_OPEN (1 << 0)
316 #define CMD_PLAY (1 << 1)
317 #define CMD_PAUSE (1 << 2)
318 #define CMD_CLOSE (1 << 3)
319 #define CMD_WAIT (1 << 4)
320 #define CMD_RECONNECT (1 << 5)
321 #define CMD_LOOP (1 << 6)
323 /* mask for all commands */
324 #define CMD_ALL ((CMD_LOOP << 1) - 1)
326 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
328 gchar *__txt = _gst_element_error_printf text; \
329 gst_element_post_message (GST_ELEMENT_CAST (el), \
330 gst_message_new_progress (GST_OBJECT_CAST (el), \
331 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
335 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
337 #define gst_rtspsrc_parent_class parent_class
338 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
339 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
341 #ifndef GST_DISABLE_GST_DEBUG
342 static inline const char *
343 cmd_to_string (guint cmd)
367 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
369 GST_DEBUG_OBJECT (src, "default handler");
374 select_stream_accum (GSignalInvocationHint * ihint,
375 GValue * return_accu, const GValue * handler_return, gpointer data)
379 myboolean = g_value_get_boolean (handler_return);
380 GST_DEBUG ("accum %d", myboolean);
381 g_value_set_boolean (return_accu, myboolean);
383 /* stop emission if FALSE */
388 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
390 GObjectClass *gobject_class;
391 GstElementClass *gstelement_class;
392 GstBinClass *gstbin_class;
394 gobject_class = (GObjectClass *) klass;
395 gstelement_class = (GstElementClass *) klass;
396 gstbin_class = (GstBinClass *) klass;
398 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
400 gobject_class->set_property = gst_rtspsrc_set_property;
401 gobject_class->get_property = gst_rtspsrc_get_property;
403 gobject_class->finalize = gst_rtspsrc_finalize;
405 g_object_class_install_property (gobject_class, PROP_LOCATION,
406 g_param_spec_string ("location", "RTSP Location",
407 "Location of the RTSP url to read",
408 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
410 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
411 g_param_spec_flags ("protocols", "Protocols",
412 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
413 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
415 g_object_class_install_property (gobject_class, PROP_DEBUG,
416 g_param_spec_boolean ("debug", "Debug",
417 "Dump request and response messages to stdout",
418 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
420 g_object_class_install_property (gobject_class, PROP_RETRY,
421 g_param_spec_uint ("retry", "Retry",
422 "Max number of retries when allocating RTP ports.",
423 0, G_MAXUINT16, DEFAULT_RETRY,
424 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
426 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
427 g_param_spec_uint64 ("timeout", "Timeout",
428 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
429 0, G_MAXUINT64, DEFAULT_TIMEOUT,
430 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
432 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
433 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
434 "Fail after timeout microseconds on TCP connections (0 = disabled)",
435 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
436 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
438 g_object_class_install_property (gobject_class, PROP_LATENCY,
439 g_param_spec_uint ("latency", "Buffer latency in ms",
440 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
441 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
443 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
444 g_param_spec_boolean ("drop-on-latency",
445 "Drop buffers when maximum latency is reached",
446 "Tells the jitterbuffer to never exceed the given latency in size",
447 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
449 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
450 g_param_spec_uint64 ("connection-speed", "Connection Speed",
451 "Network connection speed in kbps (0 = unknown)",
452 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
453 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
455 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
456 g_param_spec_enum ("nat-method", "NAT Method",
457 "Method to use for traversing firewalls and NAT",
458 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
459 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
462 * GstRTSPSrc:do-rtcp:
464 * Enable RTCP support. Some old server don't like RTCP and then this property
465 * needs to be set to FALSE.
467 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
468 g_param_spec_boolean ("do-rtcp", "Do RTCP",
469 "Send RTCP packets, disable for old incompatible server.",
470 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
473 * GstRTSPSrc:do-rtsp-keep-alive:
475 * Enable RTSP keep alive support. Some old server don't like RTSP
476 * keep alive and then this property needs to be set to FALSE.
478 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
479 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
480 "Send RTSP keep alive packets, disable for old incompatible server.",
481 DEFAULT_DO_RTSP_KEEP_ALIVE,
482 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
487 * Set the proxy parameters. This has to be a string of the format
488 * [http://][user:passwd@]host[:port].
490 g_object_class_install_property (gobject_class, PROP_PROXY,
491 g_param_spec_string ("proxy", "Proxy",
492 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
493 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
495 * GstRTSPSrc:proxy-id:
497 * Sets the proxy URI user id for authentication. If the URI set via the
498 * "proxy" property contains a user-id already, that will take precedence.
502 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
503 g_param_spec_string ("proxy-id", "proxy-id",
504 "HTTP proxy URI user id for authentication", "",
505 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
507 * GstRTSPSrc:proxy-pw:
509 * Sets the proxy URI password for authentication. If the URI set via the
510 * "proxy" property contains a password already, that will take precedence.
514 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
515 g_param_spec_string ("proxy-pw", "proxy-pw",
516 "HTTP proxy URI user password for authentication", "",
517 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
520 * GstRTSPSrc:rtp-blocksize:
522 * RTP package size to suggest to server.
524 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
525 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
526 "RTP package size to suggest to server (0 = disabled)",
527 0, 65536, DEFAULT_RTP_BLOCKSIZE,
528 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
530 g_object_class_install_property (gobject_class,
532 g_param_spec_string ("user-id", "user-id",
533 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
534 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
535 g_object_class_install_property (gobject_class, PROP_USER_PW,
536 g_param_spec_string ("user-pw", "user-pw",
537 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
538 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
541 * GstRTSPSrc:buffer-mode:
543 * Control the buffering and timestamping mode used by the jitterbuffer.
545 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
546 g_param_spec_enum ("buffer-mode", "Buffer Mode",
547 "Control the buffering algorithm in use",
548 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
549 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
552 * GstRTSPSrc:port-range:
554 * Configure the client port numbers that can be used to recieve RTP and
557 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
558 g_param_spec_string ("port-range", "Port range",
559 "Client port range that can be used to receive RTP and RTCP data, "
560 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
561 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
564 * GstRTSPSrc:udp-buffer-size:
566 * Size of the kernel UDP receive buffer in bytes.
568 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
569 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
570 "Size of the kernel UDP receive buffer in bytes, 0=default",
571 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
572 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
575 * GstRTSPSrc:short-header:
577 * Only send the basic RTSP headers for broken encoders.
579 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
580 g_param_spec_boolean ("short-header", "Short Header",
581 "Only send the basic RTSP headers for broken encoders",
582 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
584 g_object_class_install_property (gobject_class, PROP_PROBATION,
585 g_param_spec_uint ("probation", "Number of probations",
586 "Consecutive packet sequence numbers to accept the source",
587 0, G_MAXUINT, DEFAULT_PROBATION,
588 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
590 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
591 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
592 "Reconnect to the server if RTSP connection is closed when doing UDP",
593 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
595 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
596 g_param_spec_string ("multicast-iface", "Multicast Interface",
597 "The network interface on which to join the multicast group",
598 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
600 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
601 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
602 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
603 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
605 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
606 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
607 "Use the pipeline running-time to set the NTP time in the RTCP SR messages",
608 DEFAULT_USE_PIPELINE_CLOCK,
609 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
611 g_object_class_install_property (gobject_class, PROP_SDES,
612 g_param_spec_boxed ("sdes", "SDES",
613 "The SDES items of this session",
614 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
617 * GstRTSPSrc::tls-validation-flags:
619 * TLS certificate validation flags used to validate server
624 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
625 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
626 "TLS certificate validation flags used to validate the server certificate",
627 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
628 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
631 * GstRTSPSrc::tls-database:
633 * TLS database with anchor certificate authorities used to validate
634 * the server certificate.
638 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
639 g_param_spec_object ("tls-database", "TLS database",
640 "TLS database with anchor certificate authorities used to validate the server certificate",
641 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
644 * GstRTSPSrc::do-retransmission:
646 * Attempt to ask the server to retransmit lost packets according to RFC4588.
648 * Note: currently only works with SSRC-multiplexed retransmission streams
652 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
653 g_param_spec_boolean ("do-retransmission", "Retransmission",
654 "Ask the server to retransmit lost packets",
655 DEFAULT_DO_RETRANSMISSION,
656 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
659 * GstRTSPSrc::handle-request:
660 * @rtspsrc: a #GstRTSPSrc
661 * @request: a #GstRTSPMessage
662 * @response: a #GstRTSPMessage
664 * Handle a server request in @request and prepare @response.
666 * This signal is called from the streaming thread, you should therefore not
667 * do any state changes on @rtspsrc because this might deadlock. If you want
668 * to modify the state as a result of this signal, post a
669 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
674 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
675 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
676 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
677 G_TYPE_POINTER, G_TYPE_POINTER);
680 * GstRTSPSrc::on-sdp:
681 * @rtspsrc: a #GstRTSPSrc
682 * @sdp: a #GstSDPMessage
684 * Emited when the client has retrieved the SDP and before it configures the
685 * streams in the SDP. @sdp can be inspected and modified.
687 * This signal is called from the streaming thread, you should therefore not
688 * do any state changes on @rtspsrc because this might deadlock. If you want
689 * to modify the state as a result of this signal, post a
690 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
695 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
696 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
697 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
698 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
701 * GstRTSPSrc::select-stream:
702 * @rtspsrc: a #GstRTSPSrc
703 * @num: the stream number
704 * @caps: the stream caps
706 * Emited before the client decides to configure the stream @num with
709 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
714 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
715 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
716 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
717 (GCallback) default_select_stream, select_stream_accum, NULL,
718 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
721 * GstRTSPSrc::new-manager:
722 * @rtspsrc: a #GstRTSPSrc
723 * @manager: a #GstElement
725 * Emited after a new manager (like rtpbin) was created and the default
726 * properties were configured.
730 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
731 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
732 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
733 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
736 * GstRTSPSrc::request-rtcp-key:
737 * @rtspsrc: a #GstRTSPSrc
738 * @num: the stream number
740 * Signal emited to get the crypto parameters relevant to the RTCP
741 * stream. User should provide the key and the RTCP encryption ciphers
742 * and authentication, and return them wrapped in a GstCaps.
746 gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY] =
747 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
748 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
750 gstelement_class->send_event = gst_rtspsrc_send_event;
751 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
752 gstelement_class->change_state = gst_rtspsrc_change_state;
754 gst_element_class_add_pad_template (gstelement_class,
755 gst_static_pad_template_get (&rtptemplate));
757 gst_element_class_set_static_metadata (gstelement_class,
758 "RTSP packet receiver", "Source/Network",
759 "Receive data over the network via RTSP (RFC 2326)",
760 "Wim Taymans <wim@fluendo.com>, "
761 "Thijs Vermeir <thijs.vermeir@barco.com>, "
762 "Lutz Mueller <lutz@topfrose.de>");
764 gstbin_class->handle_message = gst_rtspsrc_handle_message;
766 gst_rtsp_ext_list_init ();
770 gst_rtspsrc_init (GstRTSPSrc * src)
772 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
773 src->protocols = DEFAULT_PROTOCOLS;
774 src->debug = DEFAULT_DEBUG;
775 src->retry = DEFAULT_RETRY;
776 src->udp_timeout = DEFAULT_TIMEOUT;
777 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
778 src->latency = DEFAULT_LATENCY_MS;
779 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
780 src->connection_speed = DEFAULT_CONNECTION_SPEED;
781 src->nat_method = DEFAULT_NAT_METHOD;
782 src->do_rtcp = DEFAULT_DO_RTCP;
783 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
784 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
785 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
786 src->user_id = g_strdup (DEFAULT_USER_ID);
787 src->user_pw = g_strdup (DEFAULT_USER_PW);
788 src->buffer_mode = DEFAULT_BUFFER_MODE;
789 src->client_port_range.min = 0;
790 src->client_port_range.max = 0;
791 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
792 src->short_header = DEFAULT_SHORT_HEADER;
793 src->probation = DEFAULT_PROBATION;
794 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
795 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
796 src->ntp_sync = DEFAULT_NTP_SYNC;
797 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
799 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
800 src->tls_database = DEFAULT_TLS_DATABASE;
801 src->do_retransmission = DEFAULT_DO_RETRANSMISSION;
803 /* get a list of all extensions */
804 src->extensions = gst_rtsp_ext_list_get ();
806 /* connect to send signal */
807 gst_rtsp_ext_list_connect (src->extensions, "send",
808 (GCallback) gst_rtspsrc_send_cb, src);
810 /* protects the streaming thread in interleaved mode or the polling
811 * thread in UDP mode. */
812 g_rec_mutex_init (&src->stream_rec_lock);
814 /* protects our state changes from multiple invocations */
815 g_rec_mutex_init (&src->state_rec_lock);
817 src->state = GST_RTSP_STATE_INVALID;
819 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
823 gst_rtspsrc_finalize (GObject * object)
827 rtspsrc = GST_RTSPSRC (object);
829 gst_rtsp_ext_list_free (rtspsrc->extensions);
830 g_free (rtspsrc->conninfo.location);
831 gst_rtsp_url_free (rtspsrc->conninfo.url);
832 g_free (rtspsrc->conninfo.url_str);
833 g_free (rtspsrc->user_id);
834 g_free (rtspsrc->user_pw);
835 g_free (rtspsrc->multi_iface);
838 gst_sdp_message_free (rtspsrc->sdp);
841 if (rtspsrc->provided_clock)
842 gst_object_unref (rtspsrc->provided_clock);
845 gst_structure_free (rtspsrc->sdes);
847 if (rtspsrc->tls_database)
848 g_object_unref (rtspsrc->tls_database);
851 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
852 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
854 G_OBJECT_CLASS (parent_class)->finalize (object);
858 gst_rtspsrc_provide_clock (GstElement * element)
860 GstRTSPSrc *src = GST_RTSPSRC (element);
863 if ((clock = src->provided_clock) != NULL)
864 gst_object_ref (clock);
869 /* a proxy string of the format [user:passwd@]host[:port] */
871 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
875 g_free (rtsp->proxy_user);
876 rtsp->proxy_user = NULL;
877 g_free (rtsp->proxy_passwd);
878 rtsp->proxy_passwd = NULL;
879 g_free (rtsp->proxy_host);
880 rtsp->proxy_host = NULL;
881 rtsp->proxy_port = 0;
888 /* we allow http:// in front but ignore it */
889 if (g_str_has_prefix (p, "http://"))
892 at = strchr (p, '@');
894 /* look for user:passwd */
895 col = strchr (proxy, ':');
896 if (col == NULL || col > at)
899 rtsp->proxy_user = g_strndup (p, col - p);
901 rtsp->proxy_passwd = g_strndup (col, at - col);
906 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
907 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
908 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
909 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
910 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
911 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
912 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
915 col = strchr (p, ':');
918 /* everything before the colon is the hostname */
919 rtsp->proxy_host = g_strndup (p, col - p);
921 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
923 rtsp->proxy_host = g_strdup (p);
924 rtsp->proxy_port = 8080;
930 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
932 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
933 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
936 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
938 rtspsrc->ptcp_timeout = NULL;
942 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
947 rtspsrc = GST_RTSPSRC (object);
951 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
952 g_value_get_string (value), NULL);
955 rtspsrc->protocols = g_value_get_flags (value);
958 rtspsrc->debug = g_value_get_boolean (value);
961 rtspsrc->retry = g_value_get_uint (value);
964 rtspsrc->udp_timeout = g_value_get_uint64 (value);
966 case PROP_TCP_TIMEOUT:
967 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
970 rtspsrc->latency = g_value_get_uint (value);
972 case PROP_DROP_ON_LATENCY:
973 rtspsrc->drop_on_latency = g_value_get_boolean (value);
975 case PROP_CONNECTION_SPEED:
976 rtspsrc->connection_speed = g_value_get_uint64 (value);
978 case PROP_NAT_METHOD:
979 rtspsrc->nat_method = g_value_get_enum (value);
982 rtspsrc->do_rtcp = g_value_get_boolean (value);
984 case PROP_DO_RTSP_KEEP_ALIVE:
985 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
988 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
991 if (rtspsrc->prop_proxy_id)
992 g_free (rtspsrc->prop_proxy_id);
993 rtspsrc->prop_proxy_id = g_value_dup_string (value);
996 if (rtspsrc->prop_proxy_pw)
997 g_free (rtspsrc->prop_proxy_pw);
998 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
1000 case PROP_RTP_BLOCKSIZE:
1001 rtspsrc->rtp_blocksize = g_value_get_uint (value);
1004 if (rtspsrc->user_id)
1005 g_free (rtspsrc->user_id);
1006 rtspsrc->user_id = g_value_dup_string (value);
1009 if (rtspsrc->user_pw)
1010 g_free (rtspsrc->user_pw);
1011 rtspsrc->user_pw = g_value_dup_string (value);
1013 case PROP_BUFFER_MODE:
1014 rtspsrc->buffer_mode = g_value_get_enum (value);
1016 case PROP_PORT_RANGE:
1020 str = g_value_get_string (value);
1022 sscanf (str, "%u-%u",
1023 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
1025 rtspsrc->client_port_range.min = 0;
1026 rtspsrc->client_port_range.max = 0;
1030 case PROP_UDP_BUFFER_SIZE:
1031 rtspsrc->udp_buffer_size = g_value_get_int (value);
1033 case PROP_SHORT_HEADER:
1034 rtspsrc->short_header = g_value_get_boolean (value);
1036 case PROP_PROBATION:
1037 rtspsrc->probation = g_value_get_uint (value);
1039 case PROP_UDP_RECONNECT:
1040 rtspsrc->udp_reconnect = g_value_get_boolean (value);
1042 case PROP_MULTICAST_IFACE:
1043 g_free (rtspsrc->multi_iface);
1045 if (g_value_get_string (value) == NULL)
1046 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1048 rtspsrc->multi_iface = g_value_dup_string (value);
1051 rtspsrc->ntp_sync = g_value_get_boolean (value);
1053 case PROP_USE_PIPELINE_CLOCK:
1054 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
1057 rtspsrc->sdes = g_value_dup_boxed (value);
1059 case PROP_TLS_VALIDATION_FLAGS:
1060 rtspsrc->tls_validation_flags = g_value_get_flags (value);
1062 case PROP_TLS_DATABASE:
1063 g_clear_object (&rtspsrc->tls_database);
1064 rtspsrc->tls_database = g_value_dup_object (value);
1066 case PROP_DO_RETRANSMISSION:
1067 rtspsrc->do_retransmission = g_value_get_boolean (value);
1070 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1076 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1079 GstRTSPSrc *rtspsrc;
1081 rtspsrc = GST_RTSPSRC (object);
1085 g_value_set_string (value, rtspsrc->conninfo.location);
1087 case PROP_PROTOCOLS:
1088 g_value_set_flags (value, rtspsrc->protocols);
1091 g_value_set_boolean (value, rtspsrc->debug);
1094 g_value_set_uint (value, rtspsrc->retry);
1097 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1099 case PROP_TCP_TIMEOUT:
1103 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1104 rtspsrc->tcp_timeout.tv_usec;
1105 g_value_set_uint64 (value, timeout);
1109 g_value_set_uint (value, rtspsrc->latency);
1111 case PROP_DROP_ON_LATENCY:
1112 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1114 case PROP_CONNECTION_SPEED:
1115 g_value_set_uint64 (value, rtspsrc->connection_speed);
1117 case PROP_NAT_METHOD:
1118 g_value_set_enum (value, rtspsrc->nat_method);
1121 g_value_set_boolean (value, rtspsrc->do_rtcp);
1123 case PROP_DO_RTSP_KEEP_ALIVE:
1124 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1130 if (rtspsrc->proxy_host) {
1132 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1136 g_value_take_string (value, str);
1140 g_value_set_string (value, rtspsrc->prop_proxy_id);
1143 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1145 case PROP_RTP_BLOCKSIZE:
1146 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1149 g_value_set_string (value, rtspsrc->user_id);
1152 g_value_set_string (value, rtspsrc->user_pw);
1154 case PROP_BUFFER_MODE:
1155 g_value_set_enum (value, rtspsrc->buffer_mode);
1157 case PROP_PORT_RANGE:
1161 if (rtspsrc->client_port_range.min != 0) {
1162 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1163 rtspsrc->client_port_range.max);
1167 g_value_take_string (value, str);
1170 case PROP_UDP_BUFFER_SIZE:
1171 g_value_set_int (value, rtspsrc->udp_buffer_size);
1173 case PROP_SHORT_HEADER:
1174 g_value_set_boolean (value, rtspsrc->short_header);
1176 case PROP_PROBATION:
1177 g_value_set_uint (value, rtspsrc->probation);
1179 case PROP_UDP_RECONNECT:
1180 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1182 case PROP_MULTICAST_IFACE:
1183 g_value_set_string (value, rtspsrc->multi_iface);
1186 g_value_set_boolean (value, rtspsrc->ntp_sync);
1188 case PROP_USE_PIPELINE_CLOCK:
1189 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1192 g_value_set_boxed (value, rtspsrc->sdes);
1194 case PROP_TLS_VALIDATION_FLAGS:
1195 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1197 case PROP_TLS_DATABASE:
1198 g_value_set_object (value, rtspsrc->tls_database);
1200 case PROP_DO_RETRANSMISSION:
1201 g_value_set_boolean (value, rtspsrc->do_retransmission);
1204 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1210 find_stream_by_id (GstRTSPStream * stream, gint * id)
1212 if (stream->id == *id)
1219 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1221 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1228 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1230 GstElement *src = (GstElement *) a;
1232 if (stream->udpsrc[0] == src)
1234 if (stream->udpsrc[1] == src)
1241 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1243 if (stream->conninfo.location) {
1244 /* check qualified setup_url */
1245 if (!strcmp (stream->conninfo.location, (gchar *) a))
1248 if (stream->control_url) {
1249 /* check original control_url */
1250 if (!strcmp (stream->control_url, (gchar *) a))
1253 /* check if qualified setup_url ends with string */
1254 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1261 static GstRTSPStream *
1262 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1266 /* find and get stream */
1267 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1268 return (GstRTSPStream *) lstream->data;
1273 static const GstSDPBandwidth *
1274 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1275 const GstSDPMedia * media, const gchar * type)
1279 /* first look in the media specific section */
1280 len = gst_sdp_media_bandwidths_len (media);
1281 for (i = 0; i < len; i++) {
1282 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1284 if (strcmp (bw->bwtype, type) == 0)
1287 /* then look in the message specific section */
1288 len = gst_sdp_message_bandwidths_len (sdp);
1289 for (i = 0; i < len; i++) {
1290 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1292 if (strcmp (bw->bwtype, type) == 0)
1299 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1300 const GstSDPMedia * media, GstRTSPStream * stream)
1302 const GstSDPBandwidth *bw;
1304 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1305 stream->as_bandwidth = bw->bandwidth;
1307 stream->as_bandwidth = -1;
1309 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1310 stream->rr_bandwidth = bw->bandwidth;
1312 stream->rr_bandwidth = -1;
1314 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1315 stream->rs_bandwidth = bw->bandwidth;
1317 stream->rs_bandwidth = -1;
1321 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1322 const GstSDPConnection * conn)
1324 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1327 if (conn->addrtype == NULL)
1330 /* check for IPV6 */
1331 if (strcmp (conn->addrtype, "IP4") == 0)
1332 stream->is_ipv6 = FALSE;
1333 else if (strcmp (conn->addrtype, "IP6") == 0)
1334 stream->is_ipv6 = TRUE;
1339 g_free (stream->destination);
1340 stream->destination = g_strdup (conn->address);
1342 /* check for multicast */
1343 stream->is_multicast =
1344 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1346 stream->ttl = conn->ttl;
1349 /* Go over the connections for a stream.
1350 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1352 * - If we are dealing with a localhost address, we disable multicast
1355 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1356 const GstSDPMedia * media, GstRTSPStream * stream)
1358 const GstSDPConnection *conn;
1361 /* first look in the media specific section */
1362 len = gst_sdp_media_connections_len (media);
1363 for (i = 0; i < len; i++) {
1364 conn = gst_sdp_media_get_connection (media, i);
1366 gst_rtspsrc_do_stream_connection (src, stream, conn);
1368 /* then look in the message specific section */
1369 if ((conn = gst_sdp_message_get_connection (sdp))) {
1370 gst_rtspsrc_do_stream_connection (src, stream, conn);
1374 /* m=<media> <UDP port> RTP/AVP <payload>
1377 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
1378 const GstSDPMedia * media, GstRTSPStream * stream)
1384 proto = gst_sdp_media_get_proto (media);
1388 if (g_str_equal (proto, "RTP/AVP"))
1389 stream->profile = GST_RTSP_PROFILE_AVP;
1390 else if (g_str_equal (proto, "RTP/SAVP"))
1391 stream->profile = GST_RTSP_PROFILE_SAVP;
1392 else if (g_str_equal (proto, "RTP/AVPF"))
1393 stream->profile = GST_RTSP_PROFILE_AVPF;
1394 else if (g_str_equal (proto, "RTP/SAVPF"))
1395 stream->profile = GST_RTSP_PROFILE_SAVPF;
1399 len = gst_sdp_media_formats_len (media);
1400 for (i = 0; i < len; i++) {
1407 pt = atoi (gst_sdp_media_get_format (media, i));
1409 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
1412 caps = gst_rtspsrc_media_to_caps (pt, media);
1414 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
1418 /* do some tweaks */
1419 s = gst_caps_get_structure (caps, 0);
1420 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
1421 stream->is_real = (strstr (enc, "-REAL") != NULL);
1422 if (strcmp (enc, "X-ASF-PF") == 0)
1423 stream->container = TRUE;
1425 GST_DEBUG ("mapping sdp session level attributes to caps");
1426 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, caps);
1427 GST_DEBUG ("mapping sdp media level attributes to caps");
1428 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, caps);
1430 /* the first pt will be the default */
1431 if (stream->ptmap->len == 0)
1432 stream->default_pt = pt;
1436 g_array_append_val (stream->ptmap, item);
1442 GST_ERROR_OBJECT (src, "can't find proto in media");
1447 GST_ERROR_OBJECT (src, "unknown proto in media %s", proto);
1452 static const gchar *
1453 get_aggregate_control (GstRTSPSrc * src)
1458 base = src->control;
1459 else if (src->content_base)
1460 base = src->content_base;
1461 else if (src->conninfo.url_str)
1462 base = src->conninfo.url_str;
1470 clear_ptmap_item (PtMapItem * item)
1473 gst_caps_unref (item->caps);
1476 static GstRTSPStream *
1477 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1479 GstRTSPStream *stream;
1480 const gchar *control_url;
1481 const GstSDPMedia *media;
1483 /* get media, should not return NULL */
1484 media = gst_sdp_message_get_media (sdp, idx);
1488 stream = g_new0 (GstRTSPStream, 1);
1489 stream->parent = src;
1490 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1492 stream->last_ret = GST_FLOW_NOT_LINKED;
1493 stream->added = FALSE;
1494 stream->setup = FALSE;
1495 stream->skipped = FALSE;
1497 stream->eos = FALSE;
1498 stream->discont = TRUE;
1499 stream->seqbase = -1;
1500 stream->timebase = -1;
1501 stream->send_ssrc = g_random_int ();
1502 stream->profile = GST_RTSP_PROFILE_AVP;
1503 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1504 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
1506 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1507 * session manager to scale RTCP. */
1508 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1510 /* collect connection info */
1511 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1513 /* make the payload type map */
1514 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1516 /* collect port number */
1517 stream->port = gst_sdp_media_get_port (media);
1519 /* get control url to construct the setup url. The setup url is used to
1520 * configure the transport of the stream and is used to identity the stream in
1521 * the RTP-Info header field returned from PLAY. */
1522 control_url = gst_sdp_media_get_attribute_val (media, "control");
1523 if (control_url == NULL)
1524 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1526 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1527 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1528 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1529 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1531 if (control_url != NULL) {
1532 stream->control_url = g_strdup (control_url);
1533 /* Build a fully qualified url using the content_base if any or by prefixing
1534 * the original request.
1535 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1536 * likely build a URL that the server will fail to understand, this is ok,
1537 * we will fail then. */
1538 if (g_str_has_prefix (control_url, "rtsp://"))
1539 stream->conninfo.location = g_strdup (control_url);
1544 if (g_strcmp0 (control_url, "*") == 0)
1547 base = get_aggregate_control (src);
1549 /* check if the base ends or control starts with / */
1550 has_slash = g_str_has_prefix (control_url, "/");
1551 has_slash = has_slash || g_str_has_suffix (base, "/");
1553 /* concatenate the two strings, insert / when not present */
1554 stream->conninfo.location =
1555 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1558 GST_DEBUG_OBJECT (src, " setup: %s",
1559 GST_STR_NULL (stream->conninfo.location));
1561 /* we keep track of all streams */
1562 src->streams = g_list_append (src->streams, stream);
1570 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1574 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1576 g_array_free (stream->ptmap, TRUE);
1578 g_free (stream->destination);
1579 g_free (stream->control_url);
1580 g_free (stream->conninfo.location);
1582 for (i = 0; i < 2; i++) {
1583 if (stream->udpsrc[i]) {
1584 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1585 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1586 gst_object_unref (stream->udpsrc[i]);
1588 if (stream->channelpad[i])
1589 gst_object_unref (stream->channelpad[i]);
1591 if (stream->udpsink[i]) {
1592 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1593 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1594 gst_object_unref (stream->udpsink[i]);
1597 if (stream->fakesrc) {
1598 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1599 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1600 gst_object_unref (stream->fakesrc);
1602 if (stream->srcpad) {
1603 gst_pad_set_active (stream->srcpad, FALSE);
1605 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1607 if (stream->srtpenc)
1608 gst_object_unref (stream->srtpenc);
1609 if (stream->srtpdec)
1610 gst_object_unref (stream->srtpdec);
1611 if (stream->srtcpparams)
1612 gst_caps_unref (stream->srtcpparams);
1613 if (stream->rtcppad)
1614 gst_object_unref (stream->rtcppad);
1615 if (stream->session)
1616 g_object_unref (stream->session);
1617 if (stream->rtx_pt_map)
1618 gst_structure_free (stream->rtx_pt_map);
1623 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1627 GST_DEBUG_OBJECT (src, "cleanup");
1629 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1630 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1632 gst_rtspsrc_stream_free (src, stream);
1634 g_list_free (src->streams);
1635 src->streams = NULL;
1637 if (src->manager_sig_id) {
1638 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1639 src->manager_sig_id = 0;
1641 gst_element_set_state (src->manager, GST_STATE_NULL);
1642 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1643 src->manager = NULL;
1646 gst_structure_free (src->props);
1649 g_free (src->content_base);
1650 src->content_base = NULL;
1652 g_free (src->control);
1653 src->control = NULL;
1656 gst_rtsp_range_free (src->range);
1659 /* don't clear the SDP when it was used in the url */
1660 if (src->sdp && !src->from_sdp) {
1661 gst_sdp_message_free (src->sdp);
1664 if (src->start_segment) {
1665 gst_event_unref (src->start_segment);
1666 src->start_segment = NULL;
1668 if (src->provided_clock) {
1669 gst_object_unref (src->provided_clock);
1670 src->provided_clock = NULL;
1674 #define PARSE_INT(p, del, res) \
1677 p = strstr (p, del); \
1687 #define PARSE_STRING(p, del, res) \
1690 p = strstr (p, del); \
1702 #define SKIP_SPACES(p) \
1703 while (*p && g_ascii_isspace (*p)) \
1708 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1711 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1712 gint * rate, gchar ** params)
1716 p = (gchar *) rtpmap;
1718 PARSE_INT (p, " ", *payload);
1726 PARSE_STRING (p, "/", *name);
1727 if (*name == NULL) {
1728 GST_DEBUG ("no rate, name %s", p);
1729 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1730 * streams seem to omit the rate. */
1737 p = strstr (p, "/");
1755 parse_keymgmt (const gchar * keymgmt, GstCaps * caps)
1757 gboolean res = FALSE;
1761 GstMIKEYMessage *msg;
1762 const GstMIKEYPayload *payload;
1763 const gchar *srtp_cipher;
1764 const gchar *srtp_auth;
1766 p = (gchar *) keymgmt;
1772 PARSE_STRING (p, " ", kmpid);
1773 if (!g_str_equal (kmpid, "mikey"))
1776 data = g_base64_decode (p, &size);
1780 msg = gst_mikey_message_new_from_data (data, size, NULL, NULL);
1785 srtp_cipher = "aes-128-icm";
1786 srtp_auth = "hmac-sha1-80";
1788 /* check the Security policy if any */
1789 if ((payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, 0))) {
1790 GstMIKEYPayloadSP *p = (GstMIKEYPayloadSP *) payload;
1793 if (p->proto != GST_MIKEY_SEC_PROTO_SRTP)
1796 len = gst_mikey_payload_sp_get_n_params (payload);
1797 for (i = 0; i < len; i++) {
1798 const GstMIKEYPayloadSPParam *param =
1799 gst_mikey_payload_sp_get_param (payload, i);
1801 switch (param->type) {
1802 case GST_MIKEY_SP_SRTP_ENC_ALG:
1803 switch (param->val[0]) {
1805 srtp_cipher = "null";
1809 srtp_cipher = "aes-128-icm";
1815 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
1816 switch (param->val[0]) {
1817 case AES_128_KEY_LEN:
1818 srtp_cipher = "aes-128-icm";
1820 case AES_256_KEY_LEN:
1821 srtp_cipher = "aes-256-icm";
1827 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1828 switch (param->val[0]) {
1834 srtp_auth = "hmac-sha1-80";
1840 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
1841 switch (param->val[0]) {
1842 case HMAC_32_KEY_LEN:
1843 srtp_auth = "hmac-sha1-32";
1845 case HMAC_80_KEY_LEN:
1846 srtp_auth = "hmac-sha1-80";
1852 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1854 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1862 if (!(payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_KEMAC, 0)))
1865 GstMIKEYPayloadKEMAC *p = (GstMIKEYPayloadKEMAC *) payload;
1866 const GstMIKEYPayload *sub;
1867 GstMIKEYPayloadKeyData *pkd;
1870 if (p->enc_alg != GST_MIKEY_ENC_NULL || p->mac_alg != GST_MIKEY_MAC_NULL)
1873 if (!(sub = gst_mikey_payload_kemac_get_sub (payload, 0)))
1876 if (sub->type != GST_MIKEY_PT_KEY_DATA)
1879 pkd = (GstMIKEYPayloadKeyData *) sub;
1881 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
1883 gst_caps_set_simple (caps, "srtp-key", GST_TYPE_BUFFER, buf, NULL);
1886 gst_caps_set_simple (caps,
1887 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1888 "srtp-auth", G_TYPE_STRING, srtp_auth,
1889 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1890 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1894 gst_mikey_message_unref (msg);
1900 * Mapping SDP attributes to caps
1902 * prepend 'a-' to IANA registered sdp attributes names
1903 * (ie: not prefixed with 'x-') in order to avoid
1904 * collision with gstreamer standard caps properties names
1907 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1909 if (attributes->len > 0) {
1913 s = gst_caps_get_structure (caps, 0);
1915 for (i = 0; i < attributes->len; i++) {
1916 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1917 gchar *tofree, *key;
1921 /* skip some of the attribute we already handle */
1922 if (!strcmp (key, "fmtp"))
1924 if (!strcmp (key, "rtpmap"))
1926 if (!strcmp (key, "control"))
1928 if (!strcmp (key, "range"))
1930 if (!strcmp (key, "framesize"))
1932 if (g_str_equal (key, "key-mgmt")) {
1933 parse_keymgmt (attr->value, caps);
1937 /* string must be valid UTF8 */
1938 if (!g_utf8_validate (attr->value, -1, NULL))
1941 if (!g_str_has_prefix (key, "x-"))
1942 tofree = key = g_strdup_printf ("a-%s", key);
1946 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1947 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1953 static const gchar *
1954 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
1963 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
1966 if (sscanf (attr, "%d ", &val) != 1)
1976 * Mapping of caps to and from SDP fields:
1978 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1979 * a=framesize:<payload> <width>-<height>
1980 * a=fmtp:<payload> <param>[=<value>];...
1983 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1986 const gchar *rtpmap;
1988 const gchar *framesize;
1991 gchar *params = NULL;
1997 /* get and parse rtpmap */
1998 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
2001 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
2003 g_warning ("error parsing rtpmap, ignoring");
2007 /* dynamic payloads need rtpmap or we fail */
2008 if (rtpmap == NULL && pt >= 96)
2011 /* check if we have a rate, if not, we need to look up the rate from the
2012 * default rates based on the payload types. */
2014 const GstRTPPayloadInfo *info;
2016 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
2017 /* dynamic types, use media and encoding_name */
2018 tmp = g_ascii_strdown (media->media, -1);
2019 info = gst_rtp_payload_info_for_name (tmp, name);
2022 /* static types, use payload type */
2023 info = gst_rtp_payload_info_for_pt (pt);
2027 if ((rate = info->clock_rate) == 0)
2030 /* we fail if we cannot find one */
2035 tmp = g_ascii_strdown (media->media, -1);
2036 caps = gst_caps_new_simple ("application/x-unknown",
2037 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
2039 s = gst_caps_get_structure (caps, 0);
2041 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
2043 /* encoding name must be upper case */
2045 tmp = g_ascii_strup (name, -1);
2046 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
2050 /* params must be lower case */
2051 if (params != NULL) {
2052 tmp = g_ascii_strdown (params, -1);
2053 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
2057 /* parse optional fmtp: field */
2058 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
2064 /* p is now of the format <payload> <param>[=<value>];... */
2065 PARSE_INT (p, " ", payload);
2066 if (payload != -1 && payload == pt) {
2070 /* <param>[=<value>] are separated with ';' */
2071 pairs = g_strsplit (p, ";", 0);
2072 for (i = 0; pairs[i]; i++) {
2074 const gchar *val, *key;
2076 /* the key may not have a '=', the value can have other '='s */
2077 valpos = strstr (pairs[i], "=");
2079 /* we have a '=' and thus a value, remove the '=' with \0 */
2081 /* value is everything between '=' and ';'. We split the pairs at ;
2082 * boundaries so we can take the remainder of the value. Some servers
2083 * put spaces around the value which we strip off here. Alternatively
2084 * we could strip those spaces in the depayloaders should these spaces
2085 * actually carry any meaning in the future. */
2086 val = g_strstrip (valpos + 1);
2088 /* simple <param>;.. is translated into <param>=1;... */
2091 /* strip the key of spaces, convert key to lowercase but not the value. */
2092 key = g_strstrip (pairs[i]);
2093 if (strlen (key) > 1) {
2094 tmp = g_ascii_strdown (key, -1);
2095 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
2103 /* parse framesize: field */
2104 if ((framesize = gst_sdp_media_get_attribute_val (media, "framesize"))) {
2107 /* p is now of the format <payload> <width>-<height> */
2108 p = (gchar *) framesize;
2110 PARSE_INT (p, " ", payload);
2111 if (payload != -1 && payload == pt) {
2112 gst_structure_set (s, "a-framesize", G_TYPE_STRING, p, NULL);
2120 g_warning ("rtpmap type not given for dynamic payload %d", pt);
2125 g_warning ("rate unknown for payload type %d", pt);
2131 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
2132 gint * rtpport, gint * rtcpport)
2135 GstStateChangeReturn ret;
2136 GstElement *udpsrc0, *udpsrc1;
2137 gint tmp_rtp, tmp_rtcp;
2141 src = stream->parent;
2147 /* Start at next port */
2148 tmp_rtp = src->next_port_num;
2150 if (stream->is_ipv6)
2151 host = "udp://[::0]";
2153 host = "udp://0.0.0.0";
2155 /* try to allocate 2 UDP ports, the RTP port should be an even
2156 * number and the RTCP port should be the next (uneven) port */
2159 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
2160 tmp_rtp >= src->client_port_range.max)
2163 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2164 if (udpsrc0 == NULL)
2165 goto no_udp_protocol;
2166 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
2168 if (src->udp_buffer_size != 0)
2169 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
2172 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
2173 if (ret == GST_STATE_CHANGE_FAILURE) {
2175 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
2178 if (++count > src->retry)
2181 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2182 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2183 gst_object_unref (udpsrc0);
2186 GST_DEBUG_OBJECT (src, "retry %d", count);
2189 goto no_udp_protocol;
2192 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
2193 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
2195 /* check if port is even */
2196 if ((tmp_rtp & 0x01) != 0) {
2197 /* port not even, close and allocate another */
2198 if (++count > src->retry)
2201 GST_DEBUG_OBJECT (src, "RTP port not even");
2203 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2204 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2205 gst_object_unref (udpsrc0);
2208 GST_DEBUG_OBJECT (src, "retry %d", count);
2213 /* allocate port+1 for RTCP now */
2214 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2215 if (udpsrc1 == NULL)
2216 goto no_udp_rtcp_protocol;
2219 tmp_rtcp = tmp_rtp + 1;
2220 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
2223 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
2225 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
2226 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
2227 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
2228 if (ret == GST_STATE_CHANGE_FAILURE) {
2229 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
2231 if (++count > src->retry)
2234 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2235 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2236 gst_object_unref (udpsrc0);
2239 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2240 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2241 gst_object_unref (udpsrc1);
2245 GST_DEBUG_OBJECT (src, "retry %d", count);
2249 /* all fine, do port check */
2250 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2251 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2253 /* this should not happen... */
2254 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2257 /* we keep these elements, we configure all in configure_transport when the
2258 * server told us to really use the UDP ports. */
2259 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2260 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2261 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2262 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2264 /* keep track of next available port number when we have a range
2266 if (src->next_port_num != 0)
2267 src->next_port_num = tmp_rtcp + 1;
2274 GST_DEBUG_OBJECT (src, "could not get UDP source");
2279 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2283 no_udp_rtcp_protocol:
2285 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2290 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2291 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2297 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2298 gst_object_unref (udpsrc0);
2301 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2302 gst_object_unref (udpsrc1);
2309 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2314 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2316 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2317 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2320 for (i = 0; i < 2; i++) {
2321 if (stream->udpsrc[i])
2322 gst_element_set_state (stream->udpsrc[i], state);
2328 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2335 event = gst_event_new_flush_start ();
2336 GST_DEBUG_OBJECT (src, "start flush");
2338 state = GST_STATE_PAUSED;
2340 event = gst_event_new_flush_stop (FALSE);
2341 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2344 state = GST_STATE_PLAYING;
2346 state = GST_STATE_PAUSED;
2348 gst_rtspsrc_push_event (src, event);
2349 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2350 gst_rtspsrc_set_state (src, state);
2353 static GstRTSPResult
2354 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2355 GstRTSPMessage * message, GTimeVal * timeout)
2360 ret = gst_rtsp_connection_send (conn, message, timeout);
2362 ret = GST_RTSP_ERROR;
2367 static GstRTSPResult
2368 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2369 GstRTSPMessage * message, GTimeVal * timeout)
2374 ret = gst_rtsp_connection_receive (conn, message, timeout);
2376 ret = GST_RTSP_ERROR;
2382 gst_rtspsrc_get_position (GstRTSPSrc * src)
2387 query = gst_query_new_position (GST_FORMAT_TIME);
2388 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2389 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2390 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2394 if (stream->srcpad) {
2395 if (gst_pad_query (stream->srcpad, query)) {
2396 gst_query_parse_position (query, &fmt, &pos);
2397 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2398 GST_TIME_ARGS (pos));
2399 src->last_pos = pos;
2409 gst_query_unref (query);
2413 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
2415 src->state = GST_RTSP_STATE_SEEKING;
2416 /* PLAY will add the range header now. */
2417 src->need_range = TRUE;
2423 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2428 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2430 gboolean flush, skip;
2433 GstSegment seeksegment = { 0, };
2437 GST_DEBUG_OBJECT (src, "doing seek with event");
2439 gst_event_parse_seek (event, &rate, &format, &flags,
2440 &cur_type, &cur, &stop_type, &stop);
2442 /* no negative rates yet */
2446 /* we need TIME format */
2447 if (format != src->segment.format)
2450 GST_DEBUG_OBJECT (src, "doing seek without event");
2452 cur_type = GST_SEEK_TYPE_SET;
2453 stop_type = GST_SEEK_TYPE_SET;
2456 /* get flush flag */
2457 flush = flags & GST_SEEK_FLAG_FLUSH;
2458 skip = flags & GST_SEEK_FLAG_SKIP;
2460 /* now we need to make sure the streaming thread is stopped. We do this by
2461 * either sending a FLUSH_START event downstream which will cause the
2462 * streaming thread to stop with a WRONG_STATE.
2463 * For a non-flushing seek we simply pause the task, which will happen as soon
2464 * as it completes one iteration (and thus might block when the sink is
2465 * blocking in preroll). */
2467 GST_DEBUG_OBJECT (src, "starting flush");
2468 gst_rtspsrc_flush (src, TRUE, FALSE);
2471 gst_task_pause (src->task);
2475 /* we should now be able to grab the streaming thread because we stopped it
2476 * with the above flush/pause code */
2477 GST_RTSP_STREAM_LOCK (src);
2479 GST_DEBUG_OBJECT (src, "stopped streaming");
2481 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2482 gst_rtspsrc_connection_flush (src, FALSE);
2484 /* copy segment, we need this because we still need the old
2485 * segment when we close the current segment. */
2486 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2488 /* configure the seek parameters in the seeksegment. We will then have the
2489 * right values in the segment to perform the seek */
2491 GST_DEBUG_OBJECT (src, "configuring seek");
2492 gst_segment_do_seek (&seeksegment, rate, format, flags,
2493 cur_type, cur, stop_type, stop, &update);
2496 /* figure out the last position we need to play. If it's configured (stop !=
2497 * -1), use that, else we play until the total duration of the file */
2498 if ((stop = seeksegment.stop) == -1)
2499 stop = seeksegment.duration;
2501 playing = (src->state == GST_RTSP_STATE_PLAYING);
2503 /* if we were playing, pause first */
2505 /* obtain current position in case seek fails */
2506 gst_rtspsrc_get_position (src);
2507 gst_rtspsrc_pause (src, FALSE);
2511 gst_rtspsrc_do_seek (src, &seeksegment);
2513 /* and continue playing */
2515 gst_rtspsrc_play (src, &seeksegment, FALSE);
2517 /* prepare for streaming again */
2519 /* if we started flush, we stop now */
2520 GST_DEBUG_OBJECT (src, "stopping flush");
2521 gst_rtspsrc_flush (src, FALSE, playing);
2524 /* now we did the seek and can activate the new segment values */
2525 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2527 /* if we're doing a segment seek, post a SEGMENT_START message */
2528 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2529 gst_element_post_message (GST_ELEMENT_CAST (src),
2530 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2531 src->segment.format, src->segment.position));
2534 /* now create the newsegment */
2535 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2536 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2539 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2540 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2541 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2542 stream->discont = TRUE;
2545 GST_RTSP_STREAM_UNLOCK (src);
2552 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2557 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2563 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2567 gboolean res = TRUE;
2570 src = GST_RTSPSRC_CAST (parent);
2572 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2573 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2575 switch (GST_EVENT_TYPE (event)) {
2576 case GST_EVENT_SEEK:
2577 res = gst_rtspsrc_perform_seek (src, event);
2581 case GST_EVENT_NAVIGATION:
2582 case GST_EVENT_LATENCY:
2590 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2591 res = gst_pad_send_event (target, event);
2592 gst_object_unref (target);
2594 gst_event_unref (event);
2597 gst_event_unref (event);
2603 /* this is the final event function we receive on the internal source pad when
2604 * we deal with TCP connections */
2606 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2611 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2613 switch (GST_EVENT_TYPE (event)) {
2614 case GST_EVENT_SEEK:
2616 case GST_EVENT_NAVIGATION:
2617 case GST_EVENT_LATENCY:
2619 gst_event_unref (event);
2626 /* this is the final query function we receive on the internal source pad when
2627 * we deal with TCP connections */
2629 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2633 gboolean res = TRUE;
2635 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2637 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2638 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2640 switch (GST_QUERY_TYPE (query)) {
2641 case GST_QUERY_POSITION:
2646 case GST_QUERY_DURATION:
2650 gst_query_parse_duration (query, &format, NULL);
2653 case GST_FORMAT_TIME:
2654 gst_query_set_duration (query, format, src->segment.duration);
2662 case GST_QUERY_LATENCY:
2664 /* we are live with a min latency of 0 and unlimited max latency, this
2665 * result will be updated by the session manager if there is any. */
2666 gst_query_set_latency (query, TRUE, 0, -1);
2676 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2678 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2682 gboolean res = FALSE;
2684 src = GST_RTSPSRC_CAST (parent);
2686 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2687 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2689 switch (GST_QUERY_TYPE (query)) {
2690 case GST_QUERY_DURATION:
2694 gst_query_parse_duration (query, &format, NULL);
2697 case GST_FORMAT_TIME:
2698 gst_query_set_duration (query, format, src->segment.duration);
2706 case GST_QUERY_SEEKING:
2710 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2711 if (format == GST_FORMAT_TIME) {
2713 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2715 /* seeking without duration is unlikely */
2716 seekable = seekable && src->seekable && src->segment.duration &&
2717 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2719 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2720 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2721 src->segment.start, src->segment.stop);
2730 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2732 gst_query_set_uri (query, uri);
2740 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2742 /* forward the query to the proxy target pad */
2744 res = gst_pad_query (target, query);
2745 gst_object_unref (target);
2754 /* callback for RTCP messages to be sent to the server when operating in TCP
2756 static GstFlowReturn
2757 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2760 GstRTSPStream *stream;
2761 GstFlowReturn res = GST_FLOW_OK;
2766 GstRTSPMessage message = { 0 };
2767 GstRTSPConnection *conn;
2769 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2770 src = stream->parent;
2772 gst_buffer_map (buffer, &map, GST_MAP_READ);
2776 gst_rtsp_message_init_data (&message, stream->channel[1]);
2778 /* lend the body data to the message */
2779 gst_rtsp_message_take_body (&message, data, size);
2781 if (stream->conninfo.connection)
2782 conn = stream->conninfo.connection;
2784 conn = src->conninfo.connection;
2786 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2787 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2788 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2790 /* and steal it away again because we will free it when unreffing the
2792 gst_rtsp_message_steal_body (&message, &data, &size);
2793 gst_rtsp_message_unset (&message);
2795 gst_buffer_unmap (buffer, &map);
2796 gst_buffer_unref (buffer);
2801 static GstPadProbeReturn
2802 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2804 GstRTSPSrc *src = user_data;
2806 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2807 GST_DEBUG_PAD_NAME (pad));
2809 /* activate the streams */
2810 GST_OBJECT_LOCK (src);
2811 if (!src->need_activate)
2814 src->need_activate = FALSE;
2815 GST_OBJECT_UNLOCK (src);
2817 gst_rtspsrc_activate_streams (src);
2819 return GST_PAD_PROBE_OK;
2823 GST_OBJECT_UNLOCK (src);
2824 return GST_PAD_PROBE_OK;
2829 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
2831 GstPad *gpad = GST_PAD_CAST (user_data);
2833 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
2834 gst_pad_store_sticky_event (gpad, *event);
2839 /* this callback is called when the session manager generated a new src pad with
2840 * payloaded RTP packets. We simply ghost the pad here. */
2842 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2845 GstPadTemplate *template;
2848 GstRTSPStream *stream;
2851 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2853 GST_RTSP_STATE_LOCK (src);
2855 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2856 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2857 goto unknown_stream;
2859 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2861 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2863 goto unknown_stream;
2866 stream->ssrc = ssrc;
2868 /* we'll add it later see below */
2869 stream->added = TRUE;
2871 /* check if we added all streams */
2873 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2874 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2876 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
2877 ostream, ostream->container, ostream->added, ostream->setup);
2879 /* if we find a stream for which we did a setup that is not added, we
2880 * need to wait some more */
2881 if (ostream->setup && !ostream->added) {
2886 GST_RTSP_STATE_UNLOCK (src);
2888 /* create a new pad we will use to stream to */
2889 template = gst_static_pad_template_get (&rtptemplate);
2890 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2891 gst_object_unref (template);
2894 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2895 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2896 gst_pad_set_active (stream->srcpad, TRUE);
2897 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
2898 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2901 GST_DEBUG_OBJECT (src, "We added all streams");
2902 /* when we get here, all stream are added and we can fire the no-more-pads
2904 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2912 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2913 GST_RTSP_STATE_UNLOCK (src);
2920 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
2924 len = stream->ptmap->len;
2925 for (i = 0; i < len; i++) {
2926 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2934 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2936 GstRTSPStream *stream;
2939 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2941 GST_RTSP_STATE_LOCK (src);
2942 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2944 goto unknown_stream;
2946 if ((caps = stream_get_caps_for_pt (stream, pt)))
2947 gst_caps_ref (caps);
2948 GST_RTSP_STATE_UNLOCK (src);
2954 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2955 GST_RTSP_STATE_UNLOCK (src);
2961 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2963 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2969 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2975 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2981 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2983 GstRTSPSrc *src = stream->parent;
2986 g_object_get (source, "ssrc", &ssrc, NULL);
2988 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2989 ssrc, stream->ssrc, stream->id);
2991 if (ssrc == stream->ssrc)
2992 gst_rtspsrc_do_stream_eos (src, stream);
2996 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2998 GstRTSPSrc *src = stream->parent;
3001 g_object_get (source, "ssrc", &ssrc, NULL);
3003 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
3004 ssrc, stream->ssrc, stream->id);
3006 if (ssrc == stream->ssrc)
3007 gst_rtspsrc_do_stream_eos (src, stream);
3011 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
3013 GstRTSPStream *stream;
3015 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
3017 /* get stream for session */
3018 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3020 gst_rtspsrc_do_stream_eos (src, stream);
3025 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
3027 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
3032 set_manager_buffer_mode (GstRTSPSrc * src)
3034 GObjectClass *klass;
3036 if (src->manager == NULL)
3039 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3041 if (!g_object_class_find_property (klass, "buffer-mode"))
3044 if (src->buffer_mode != BUFFER_MODE_AUTO) {
3045 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
3050 GST_DEBUG_OBJECT (src,
3051 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
3053 if (src->provided_clock) {
3054 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
3056 if (clock == src->provided_clock) {
3057 GST_DEBUG_OBJECT (src, "selected synced");
3058 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
3061 gst_object_unref (clock);
3066 /* Otherwise fall-through and use another buffer mode */
3068 gst_object_unref (clock);
3071 GST_DEBUG_OBJECT (src, "auto buffering mode");
3072 if (src->use_buffering) {
3073 GST_DEBUG_OBJECT (src, "selected buffer");
3074 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
3076 GST_DEBUG_OBJECT (src, "selected slave");
3077 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
3082 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
3084 GST_DEBUG ("request key %u", ssrc);
3085 return gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
3089 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
3091 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3092 if (stream->id != session)
3095 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3096 stream->profile != GST_RTSP_PROFILE_SAVPF)
3099 if (stream->srtpdec == NULL) {
3102 name = g_strdup_printf ("srtpdec_%u", session);
3103 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
3106 g_signal_connect (stream->srtpdec, "request-key",
3107 (GCallback) request_key, stream);
3109 return gst_object_ref (stream->srtpdec);
3113 request_rtcp_encoder (GstElement * rtpbin, guint session,
3114 GstRTSPStream * stream)
3119 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3120 if (stream->id != session)
3123 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3124 stream->profile != GST_RTSP_PROFILE_SAVPF)
3127 if (stream->srtpenc == NULL) {
3130 name = g_strdup_printf ("srtpenc_%u", session);
3131 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
3134 /* get RTCP crypto parameters from caps */
3135 s = gst_caps_get_structure (stream->srtcpparams, 0);
3139 GType ciphertype, authtype;
3140 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
3142 ciphertype = g_type_from_name ("GstSrtpCipherType");
3143 authtype = g_type_from_name ("GstSrtpAuthType");
3144 g_value_init (&rtcp_cipher, ciphertype);
3145 g_value_init (&rtcp_auth, authtype);
3147 str = gst_structure_get_string (s, "srtcp-cipher");
3148 gst_value_deserialize (&rtcp_cipher, str);
3149 str = gst_structure_get_string (s, "srtcp-auth");
3150 gst_value_deserialize (&rtcp_auth, str);
3151 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
3153 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
3155 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
3157 g_object_set (stream->srtpenc, "key", buf, NULL);
3159 g_value_unset (&rtcp_cipher);
3160 g_value_unset (&rtcp_auth);
3161 gst_buffer_unref (buf);
3164 name = g_strdup_printf ("rtcp_sink_%d", session);
3165 pad = gst_element_get_request_pad (stream->srtpenc, name);
3167 gst_object_unref (pad);
3169 return gst_object_ref (stream->srtpenc);
3173 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
3175 GstElement *rtx, *bin;
3178 GstRTSPStream *stream;
3180 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
3182 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
3186 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
3187 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
3188 bin = gst_bin_new (NULL);
3189 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
3190 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
3191 gst_bin_add (GST_BIN (bin), rtx);
3193 pad = gst_element_get_static_pad (rtx, "src");
3194 name = g_strdup_printf ("src_%u", sessid);
3195 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3197 gst_object_unref (pad);
3199 pad = gst_element_get_static_pad (rtx, "sink");
3200 name = g_strdup_printf ("sink_%u", sessid);
3201 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3203 gst_object_unref (pad);
3209 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
3214 if (transport->trans != GST_RTSP_TRANS_RTP)
3217 signal_id = g_signal_lookup ("request-aux-receiver",
3218 G_OBJECT_TYPE (src->manager));
3219 /* there's already something connected */
3220 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
3221 NULL, NULL, NULL) != 0) {
3222 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
3223 "\"request-aux-receiver\" signal is "
3224 "already used by the application");
3228 /* build the retransmission payload type map */
3229 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3230 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3233 if (stream->rtx_pt_map)
3234 gst_structure_free (stream->rtx_pt_map);
3235 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
3237 for (i = 0; i < stream->ptmap->len; i++) {
3238 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3239 GstStructure *s = gst_caps_get_structure (item->caps, 0);
3240 const gchar *encoding;
3242 /* we only care about RTX streams */
3243 if ((encoding = gst_structure_get_string (s, "encoding-name"))
3244 && g_strcmp0 (encoding, "RTX") == 0) {
3245 const gchar *stream_pt_s;
3248 if (gst_structure_get_int (s, "payload", &rtx_pt)
3249 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
3252 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
3259 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
3260 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
3263 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
3265 /* enable RFC4588 retransmission handling by setting rtprtxreceive
3266 * as the "aux" element of rtpbin */
3267 g_signal_connect (src->manager, "request-aux-receiver",
3268 (GCallback) request_aux_receiver, src);
3271 /* try to get and configure a manager */
3273 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3274 GstRTSPTransport * transport)
3276 const gchar *manager;
3278 GstStateChangeReturn ret;
3280 /* find a manager */
3281 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3285 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3287 /* configure the manager */
3288 if (src->manager == NULL) {
3289 GObjectClass *klass;
3291 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3293 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3297 goto use_no_manager;
3299 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3300 goto manager_failed;
3303 /* we manage this element */
3304 gst_element_set_locked_state (src->manager, TRUE);
3305 gst_bin_add (GST_BIN_CAST (src), src->manager);
3307 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3308 if (ret == GST_STATE_CHANGE_FAILURE)
3309 goto start_manager_failure;
3311 g_object_set (src->manager, "latency", src->latency, NULL);
3313 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3315 if (g_object_class_find_property (klass, "ntp-sync")) {
3316 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3319 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3320 g_object_set (src->manager, "use-pipeline-clock",
3321 src->use_pipeline_clock, NULL);
3324 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3325 g_object_set (src->manager, "sdes", src->sdes, NULL);
3328 if (g_object_class_find_property (klass, "drop-on-latency")) {
3329 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3333 /* buffer mode pauses are handled by adding offsets to buffer times,
3334 * but some depayloaders may have a hard time syncing output times
3335 * with such input times, e.g. container ones, most notably ASF */
3336 /* TODO alternatives are having an event that indicates these shifts,
3337 * or having rtsp extensions provide suggestion on buffer mode */
3338 /* valid duration implies not likely live pipeline,
3339 * so slaving in jitterbuffer does not make much sense
3340 * (and might mess things up due to bursts) */
3341 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3342 src->segment.duration && stream->container) {
3343 src->use_buffering = TRUE;
3345 src->use_buffering = FALSE;
3348 set_manager_buffer_mode (src);
3350 /* connect to signals */
3351 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3353 src->manager_sig_id =
3354 g_signal_connect (src->manager, "pad-added",
3355 (GCallback) new_manager_pad, src);
3356 src->manager_ptmap_id =
3357 g_signal_connect (src->manager, "request-pt-map",
3358 (GCallback) request_pt_map, src);
3360 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3363 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3366 if (src->do_retransmission)
3367 add_retransmission (src, transport);
3369 g_signal_connect (src->manager, "request-rtp-decoder",
3370 (GCallback) request_rtp_decoder, stream);
3371 g_signal_connect (src->manager, "request-rtcp-decoder",
3372 (GCallback) request_rtp_decoder, stream);
3373 g_signal_connect (src->manager, "request-rtcp-encoder",
3374 (GCallback) request_rtcp_encoder, stream);
3376 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3377 * into a separate RTP session. */
3378 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3379 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3381 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3382 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3385 /* now configure the bandwidth in the manager */
3386 if (g_signal_lookup ("get-internal-session",
3387 G_OBJECT_TYPE (src->manager)) != 0) {
3388 GObject *rtpsession;
3390 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3393 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3395 stream->session = rtpsession;
3397 if (stream->as_bandwidth != -1) {
3398 GST_INFO_OBJECT (src, "setting AS: %f",
3399 (gdouble) (stream->as_bandwidth * 1000));
3400 g_object_set (rtpsession, "bandwidth",
3401 (gdouble) (stream->as_bandwidth * 1000), NULL);
3403 if (stream->rr_bandwidth != -1) {
3404 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3405 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3408 if (stream->rs_bandwidth != -1) {
3409 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3410 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3414 g_object_set (rtpsession, "probation", src->probation, NULL);
3416 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3418 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3420 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3422 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3424 g_signal_connect (rtpsession, "on-ssrc-active",
3425 (GCallback) on_ssrc_active, stream);
3436 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3441 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3444 start_manager_failure:
3446 GST_DEBUG_OBJECT (src, "could not start session manager");
3451 /* free the UDP sources allocated when negotiating a transport.
3452 * This function is called when the server negotiated to a transport where the
3453 * UDP sources are not needed anymore, such as TCP or multicast. */
3455 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3459 for (i = 0; i < 2; i++) {
3460 if (stream->udpsrc[i]) {
3461 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3462 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3463 gst_object_unref (stream->udpsrc[i]);
3464 stream->udpsrc[i] = NULL;
3469 /* for TCP, create pads to send and receive data to and from the manager and to
3470 * intercept various events and queries
3473 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3474 GstRTSPTransport * transport, GstPad ** outpad)
3477 GstPadTemplate *template;
3478 GstPad *pad0, *pad1;
3480 /* configure for interleaved delivery, nothing needs to be done
3481 * here, the loop function will call the chain functions of the
3482 * session manager. */
3483 stream->channel[0] = transport->interleaved.min;
3484 stream->channel[1] = transport->interleaved.max;
3485 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3486 stream->channel[0], stream->channel[1]);
3488 /* we can remove the allocated UDP ports now */
3489 gst_rtspsrc_stream_free_udp (stream);
3491 /* no session manager, send data to srcpad directly */
3492 if (!stream->channelpad[0]) {
3493 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3495 /* create a new pad we will use to stream to */
3496 name = g_strdup_printf ("stream_%u", stream->id);
3497 template = gst_static_pad_template_get (&rtptemplate);
3498 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3499 gst_object_unref (template);
3502 /* set caps and activate */
3503 gst_pad_use_fixed_caps (stream->channelpad[0]);
3504 gst_pad_set_active (stream->channelpad[0], TRUE);
3506 *outpad = gst_object_ref (stream->channelpad[0]);
3508 GST_DEBUG_OBJECT (src, "using manager source pad");
3510 template = gst_static_pad_template_get (&anysrctemplate);
3512 /* allocate pads for sending the channel data into the manager */
3513 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3514 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3515 gst_object_unref (stream->channelpad[0]);
3516 stream->channelpad[0] = pad0;
3517 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3518 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3519 gst_pad_set_element_private (pad0, src);
3520 gst_pad_set_active (pad0, TRUE);
3522 if (stream->channelpad[1]) {
3523 /* if we have a sinkpad for the other channel, create a pad and link to the
3525 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3526 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3527 gst_pad_link_full (pad1, stream->channelpad[1],
3528 GST_PAD_LINK_CHECK_NOTHING);
3529 gst_object_unref (stream->channelpad[1]);
3530 stream->channelpad[1] = pad1;
3531 gst_pad_set_active (pad1, TRUE);
3533 gst_object_unref (template);
3535 /* setup RTCP transport back to the server if we have to. */
3536 if (src->manager && src->do_rtcp) {
3539 template = gst_static_pad_template_get (&anysinktemplate);
3541 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3542 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3543 gst_pad_set_element_private (stream->rtcppad, stream);
3544 gst_pad_set_active (stream->rtcppad, TRUE);
3546 /* get session RTCP pad */
3547 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3548 pad = gst_element_get_request_pad (src->manager, name);
3553 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3554 gst_object_unref (pad);
3557 gst_object_unref (template);
3563 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3564 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3565 gint * max, guint * ttl)
3567 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3569 if (!(*destination = transport->destination))
3570 *destination = stream->destination;
3573 /* transport first */
3574 *min = transport->port.min;
3575 *max = transport->port.max;
3576 if (*min == -1 && *max == -1) {
3577 /* then try from SDP */
3578 if (stream->port != 0) {
3579 *min = stream->port;
3580 *max = stream->port + 1;
3586 if (!(*ttl = transport->ttl))
3591 /* first take the source, then the endpoint to figure out where to send
3593 if (!(*destination = transport->source)) {
3594 if (src->conninfo.connection)
3595 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3596 else if (stream->conninfo.connection)
3598 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3602 /* for unicast we only expect the ports here */
3603 *min = transport->server_port.min;
3604 *max = transport->server_port.max;
3609 /* For multicast create UDP sources and join the multicast group. */
3611 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3612 GstRTSPTransport * transport, GstPad ** outpad)
3615 const gchar *destination;
3618 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3620 /* we can remove the allocated UDP ports now */
3621 gst_rtspsrc_stream_free_udp (stream);
3623 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3626 /* we need a destination now */
3627 if (destination == NULL)
3628 goto no_destination;
3630 /* we really need ports now or we won't be able to receive anything at all */
3631 if (min == -1 && max == -1)
3634 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3635 destination, min, max);
3637 /* creating UDP source for RTP */
3639 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3641 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3643 if (stream->udpsrc[0] == NULL)
3646 /* take ownership */
3647 gst_object_ref_sink (stream->udpsrc[0]);
3649 if (src->udp_buffer_size != 0)
3650 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3651 src->udp_buffer_size, NULL);
3653 if (src->multi_iface != NULL)
3654 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3655 src->multi_iface, NULL);
3658 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3659 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3662 /* creating another UDP source for RTCP */
3666 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3668 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3670 if (stream->udpsrc[1] == NULL)
3673 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3674 stream->profile == GST_RTSP_PROFILE_SAVPF)
3675 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3677 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3678 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3679 gst_caps_unref (caps);
3681 /* take ownership */
3682 gst_object_ref_sink (stream->udpsrc[1]);
3684 if (src->multi_iface != NULL)
3685 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3686 src->multi_iface, NULL);
3688 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
3695 GST_DEBUG_OBJECT (src, "no UDP source element found");
3700 GST_DEBUG_OBJECT (src, "no destination found");
3705 GST_DEBUG_OBJECT (src, "no ports found");
3710 /* configure the remainder of the UDP ports */
3712 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3713 GstRTSPTransport * transport, GstPad ** outpad)
3715 /* we manage the UDP elements now. For unicast, the UDP sources where
3716 * allocated in the stream when we suggested a transport. */
3717 if (stream->udpsrc[0]) {
3720 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3721 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3723 GST_DEBUG_OBJECT (src, "setting up UDP source");
3725 /* configure a timeout on the UDP port. When the timeout message is
3726 * posted, we assume UDP transport is not possible. We reconnect using TCP
3728 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3729 src->udp_timeout * 1000, NULL);
3731 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
3732 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3734 /* get output pad of the UDP source. */
3735 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3737 /* save it so we can unblock */
3738 stream->blockedpad = *outpad;
3740 /* configure pad block on the pad. As soon as there is dataflow on the
3741 * UDP source, we know that UDP is not blocked by a firewall and we can
3742 * configure all the streams to let the application autoplug decoders. */
3744 gst_pad_add_probe (stream->blockedpad,
3745 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3746 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
3748 if (stream->channelpad[0]) {
3749 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3750 /* configure for UDP delivery, we need to connect the UDP pads to
3751 * the session plugin. */
3752 gst_pad_link_full (*outpad, stream->channelpad[0],
3753 GST_PAD_LINK_CHECK_NOTHING);
3754 gst_object_unref (*outpad);
3756 /* we connected to pad-added signal to get pads from the manager */
3758 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3763 if (stream->udpsrc[1]) {
3766 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3767 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3769 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3770 stream->profile == GST_RTSP_PROFILE_SAVPF)
3771 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3773 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3774 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3775 gst_caps_unref (caps);
3777 if (stream->channelpad[1]) {
3780 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3782 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3783 gst_pad_link_full (pad, stream->channelpad[1],
3784 GST_PAD_LINK_CHECK_NOTHING);
3785 gst_object_unref (pad);
3787 /* leave unlinked */
3793 /* configure the UDP sink back to the server for status reports */
3795 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3796 GstRTSPStream * stream, GstRTSPTransport * transport)
3799 gint rtp_port, rtcp_port;
3800 gboolean do_rtp, do_rtcp;
3801 const gchar *destination;
3806 /* get transport info */
3807 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3808 &rtp_port, &rtcp_port, &ttl);
3810 /* see what we need to do */
3811 do_rtp = (rtp_port != -1);
3812 /* it's possible that the server does not want us to send RTCP in which case
3814 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3816 /* we need a destination when we have RTP or RTCP ports */
3817 if (destination == NULL && (do_rtp || do_rtcp))
3818 goto no_destination;
3820 /* try to construct the fakesrc to the RTP port of the server to open up any
3823 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3826 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3827 stream->udpsink[0] =
3828 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3830 if (stream->udpsink[0] == NULL)
3831 goto no_sink_element;
3833 /* don't join multicast group, we will have the source socket do that */
3834 /* no sync or async state changes needed */
3835 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3836 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3838 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3840 if (stream->udpsrc[0]) {
3841 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3842 * so that NAT firewalls will open a hole for us */
3843 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3844 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3845 /* configure socket and make sure udpsink does not close it when shutting
3846 * down, it belongs to udpsrc after all. */
3847 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3848 "close-socket", FALSE, NULL);
3849 g_object_unref (socket);
3852 /* the source for the dummy packets to open up NAT */
3853 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3854 if (stream->fakesrc == NULL)
3855 goto no_fakesrc_element;
3857 /* random data in 5 buffers, a size of 200 bytes should be fine */
3858 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3859 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3861 /* we don't want to consider this a sink */
3862 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
3864 /* keep everything locked */
3865 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3866 gst_element_set_locked_state (stream->fakesrc, TRUE);
3868 gst_object_ref (stream->udpsink[0]);
3869 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3870 gst_object_ref (stream->fakesrc);
3871 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3873 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3874 "sink", GST_PAD_LINK_CHECK_NOTHING);
3877 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3880 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3881 stream->udpsink[1] =
3882 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3884 if (stream->udpsink[1] == NULL)
3885 goto no_sink_element;
3887 /* don't join multicast group, we will have the source socket do that */
3888 /* no sync or async state changes needed */
3889 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3890 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3892 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3894 if (stream->udpsrc[1]) {
3895 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3896 * because some servers check the port number of where it sends RTCP to identify
3897 * the RTCP packets it receives */
3898 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3899 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3900 /* configure socket and make sure udpsink does not close it when shutting
3901 * down, it belongs to udpsrc after all. */
3902 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3903 "close-socket", FALSE, NULL);
3904 g_object_unref (socket);
3907 /* we don't want to consider this a sink */
3908 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3910 /* we keep this playing always */
3911 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3912 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3914 gst_object_ref (stream->udpsink[1]);
3915 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3917 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3919 /* get session RTCP pad */
3920 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3921 pad = gst_element_get_request_pad (src->manager, name);
3926 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3927 gst_object_unref (pad);
3936 GST_DEBUG_OBJECT (src, "no destination address specified");
3941 GST_DEBUG_OBJECT (src, "no UDP sink element found");
3946 GST_DEBUG_OBJECT (src, "no fakesrc element found");
3951 /* sets up all elements needed for streaming over the specified transport.
3952 * Does not yet expose the element pads, this will be done when there is actuall
3953 * dataflow detected, which might never happen when UDP is blocked in a
3954 * firewall, for example.
3957 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3958 GstRTSPTransport * transport)
3961 GstPad *outpad = NULL;
3962 GstPadTemplate *template;
3964 const gchar *media_type;
3967 src = stream->parent;
3969 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3971 /* get the proper media type for this stream now */
3972 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
3973 goto unknown_transport;
3975 goto unknown_transport;
3977 /* configure the final media type */
3978 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
3980 len = stream->ptmap->len;
3981 for (i = 0; i < len; i++) {
3983 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3985 if (item->caps == NULL)
3988 s = gst_caps_get_structure (item->caps, 0);
3989 gst_structure_set_name (s, media_type);
3990 /* set ssrc if known */
3991 if (transport->ssrc)
3992 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
3995 /* try to get and configure a manager, channelpad[0-1] will be configured with
3996 * the pads for the manager, or NULL when no manager is needed. */
3997 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
4000 switch (transport->lower_transport) {
4001 case GST_RTSP_LOWER_TRANS_TCP:
4002 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
4003 goto transport_failed;
4005 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
4006 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
4007 goto transport_failed;
4008 /* fallthrough, the rest is the same for UDP and MCAST */
4009 case GST_RTSP_LOWER_TRANS_UDP:
4010 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
4011 goto transport_failed;
4012 /* configure udpsinks back to the server for RTCP messages and for the
4013 * dummy RTP messages to open NAT. */
4014 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
4015 goto transport_failed;
4018 goto unknown_transport;
4022 GST_DEBUG_OBJECT (src, "creating ghostpad");
4024 gst_pad_use_fixed_caps (outpad);
4026 /* create ghostpad, don't add just yet, this will be done when we activate
4028 name = g_strdup_printf ("stream_%u", stream->id);
4029 template = gst_static_pad_template_get (&rtptemplate);
4030 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
4031 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
4032 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
4033 gst_object_unref (template);
4036 gst_object_unref (outpad);
4038 /* mark pad as ok */
4039 stream->last_ret = GST_FLOW_OK;
4046 GST_DEBUG_OBJECT (src, "failed to configure transport");
4051 GST_DEBUG_OBJECT (src, "unknown transport");
4056 GST_DEBUG_OBJECT (src, "cannot get a session manager");
4061 /* send a couple of dummy random packets on the receiver RTP port to the server,
4062 * this should make a firewall think we initiated the data transfer and
4063 * hopefully allow packets to go from the sender port to our RTP receiver port */
4065 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
4069 if (src->nat_method != GST_RTSP_NAT_DUMMY)
4072 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4073 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4075 if (stream->fakesrc && stream->udpsink[0]) {
4076 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
4077 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
4078 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
4079 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
4080 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
4086 /* Adds the source pads of all configured streams to the element.
4087 * This code is performed when we detected dataflow.
4089 * We detect dataflow from either the _loop function or with pad probes on the
4093 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
4097 GST_DEBUG_OBJECT (src, "activating streams");
4099 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4100 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4102 if (stream->udpsrc[0]) {
4103 /* remove timeout, we are streaming now and timeouts will be handled by
4104 * the session manager and jitter buffer */
4105 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
4107 if (stream->srcpad) {
4108 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
4109 gst_pad_set_active (stream->srcpad, TRUE);
4111 /* if we don't have a session manager, set the caps now. If we have a
4112 * session, we will get a notification of the pad and the caps. */
4113 if (!src->manager) {
4116 caps = stream_get_caps_for_pt (stream, stream->default_pt);
4117 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
4118 gst_pad_set_caps (stream->srcpad, caps);
4121 if (!stream->added) {
4122 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
4123 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
4124 stream->added = TRUE;
4129 /* unblock all pads */
4130 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4131 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4133 if (stream->blockid) {
4134 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
4135 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
4136 stream->blockid = 0;
4144 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
4145 gboolean reset_manager)
4148 guint64 start, stop;
4149 gdouble play_speed, play_scale;
4151 GST_DEBUG_OBJECT (src, "configuring stream caps");
4153 start = segment->position;
4154 stop = segment->duration;
4155 play_speed = segment->rate;
4156 play_scale = segment->applied_rate;
4158 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4159 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4165 len = stream->ptmap->len;
4166 for (j = 0; j < len; j++) {
4168 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
4170 if (item->caps == NULL)
4173 caps = gst_caps_make_writable (item->caps);
4175 if (stream->timebase != -1)
4176 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
4177 (guint) stream->timebase, NULL);
4178 if (stream->seqbase != -1)
4179 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
4180 (guint) stream->seqbase, NULL);
4181 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
4183 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
4184 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
4185 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
4188 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
4191 if (item->pt == stream->default_pt && stream->udpsrc[0]) {
4192 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4196 if (reset_manager && src->manager) {
4197 GST_DEBUG_OBJECT (src, "clear session");
4198 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
4202 static GstFlowReturn
4203 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
4208 /* store the value */
4209 stream->last_ret = ret;
4211 /* if it's success we can return the value right away */
4212 if (ret == GST_FLOW_OK)
4215 /* any other error that is not-linked can be returned right
4217 if (ret != GST_FLOW_NOT_LINKED)
4220 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4221 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4222 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4224 ret = ostream->last_ret;
4225 /* some other return value (must be SUCCESS but we can return
4226 * other values as well) */
4227 if (ret != GST_FLOW_NOT_LINKED)
4230 /* if we get here, all other pads were unlinked and we return
4231 * NOT_LINKED then */
4237 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4240 gboolean res = TRUE;
4242 /* only streams that have a connection to the outside world */
4246 if (stream->udpsrc[0]) {
4247 gst_event_ref (event);
4248 res = gst_element_send_event (stream->udpsrc[0], event);
4249 } else if (stream->channelpad[0]) {
4250 gst_event_ref (event);
4251 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4252 res = gst_pad_push_event (stream->channelpad[0], event);
4254 res = gst_pad_send_event (stream->channelpad[0], event);
4257 if (stream->udpsrc[1]) {
4258 gst_event_ref (event);
4259 res &= gst_element_send_event (stream->udpsrc[1], event);
4260 } else if (stream->channelpad[1]) {
4261 gst_event_ref (event);
4262 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4263 res &= gst_pad_push_event (stream->channelpad[1], event);
4265 res &= gst_pad_send_event (stream->channelpad[1], event);
4269 gst_event_unref (event);
4275 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4278 gboolean res = TRUE;
4280 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4281 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4283 gst_event_ref (event);
4284 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4286 gst_event_unref (event);
4291 static GstRTSPResult
4292 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4297 if (info->connection == NULL) {
4298 if (info->url == NULL) {
4299 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4300 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4304 /* create connection */
4305 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4306 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4307 goto could_not_create;
4310 g_free (info->url_str);
4311 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4313 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4315 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4316 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4317 src->tls_validation_flags))
4318 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4320 if (src->tls_database)
4321 gst_rtsp_connection_set_tls_database (info->connection,
4325 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4326 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4328 if (src->proxy_host) {
4329 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4331 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4336 if (!info->connected) {
4339 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4340 ("Connecting to %s", info->location));
4341 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4343 gst_rtsp_connection_connect (info->connection,
4344 src->ptcp_timeout)) < 0)
4345 goto could_not_connect;
4347 info->connected = TRUE;
4354 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4359 gchar *str = gst_rtsp_strresult (res);
4360 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4366 gchar *str = gst_rtsp_strresult (res);
4367 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4373 static GstRTSPResult
4374 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4377 GST_RTSP_STATE_LOCK (src);
4378 if (info->connected) {
4379 GST_DEBUG_OBJECT (src, "closing connection...");
4380 gst_rtsp_connection_close (info->connection);
4381 info->connected = FALSE;
4383 if (free && info->connection) {
4384 /* free connection */
4385 GST_DEBUG_OBJECT (src, "freeing connection...");
4386 gst_rtsp_connection_free (info->connection);
4387 info->connection = NULL;
4389 GST_RTSP_STATE_UNLOCK (src);
4393 static GstRTSPResult
4394 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4399 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4400 gst_rtsp_conninfo_close (src, info, FALSE);
4401 res = gst_rtsp_conninfo_connect (src, info, async);
4407 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4411 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4412 GST_RTSP_STATE_LOCK (src);
4413 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4414 GST_DEBUG_OBJECT (src, "connection flush");
4415 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4416 src->conninfo.flushing = flush;
4418 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4419 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4420 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4421 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4422 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4423 stream->conninfo.flushing = flush;
4426 GST_RTSP_STATE_UNLOCK (src);
4429 /* FIXME, handle server request, reply with OK, for now */
4430 static GstRTSPResult
4431 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
4432 GstRTSPMessage * request)
4434 GstRTSPMessage response = { 0 };
4437 GST_DEBUG_OBJECT (src, "got server request message");
4440 gst_rtsp_message_dump (request);
4442 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4444 if (res == GST_RTSP_ENOTIMPL) {
4445 /* default implementation, send OK */
4446 GST_DEBUG_OBJECT (src, "prepare OK reply");
4448 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4453 /* let app parse and reply */
4454 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4455 0, request, &response);
4458 gst_rtsp_message_dump (&response);
4460 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
4464 gst_rtsp_message_unset (&response);
4465 } else if (res == GST_RTSP_EEOF)
4473 gst_rtsp_message_unset (&response);
4478 /* send server keep-alive */
4479 static GstRTSPResult
4480 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4482 GstRTSPMessage request = { 0 };
4484 GstRTSPMethod method;
4485 const gchar *control;
4487 if (src->do_rtsp_keep_alive == FALSE) {
4488 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4489 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4493 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4495 /* find a method to use for keep-alive */
4496 if (src->methods & GST_RTSP_GET_PARAMETER)
4497 method = GST_RTSP_GET_PARAMETER;
4499 method = GST_RTSP_OPTIONS;
4501 control = get_aggregate_control (src);
4502 if (control == NULL)
4505 res = gst_rtsp_message_init_request (&request, method, control);
4510 gst_rtsp_message_dump (&request);
4513 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
4518 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4519 gst_rtsp_message_unset (&request);
4526 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4531 gchar *str = gst_rtsp_strresult (res);
4533 gst_rtsp_message_unset (&request);
4534 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4535 ("Could not send keep-alive. (%s)", str));
4541 static GstFlowReturn
4542 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4544 GstFlowReturn ret = GST_FLOW_OK;
4546 GstRTSPStream *stream;
4547 GstPad *outpad = NULL;
4554 channel = message->type_data.data.channel;
4556 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4558 goto unknown_stream;
4560 if (channel == stream->channel[0]) {
4561 outpad = stream->channelpad[0];
4563 } else if (channel == stream->channel[1]) {
4564 outpad = stream->channelpad[1];
4570 /* take a look at the body to figure out what we have */
4571 gst_rtsp_message_get_body (message, &data, &size);
4573 goto invalid_length;
4575 /* channels are not correct on some servers, do extra check */
4576 if (data[1] >= 200 && data[1] <= 204) {
4577 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4578 outpad = stream->channelpad[1];
4582 /* we have no clue what this is, just ignore then. */
4584 goto unknown_stream;
4586 /* take the message body for further processing */
4587 gst_rtsp_message_steal_body (message, &data, &size);
4589 /* strip the trailing \0 */
4592 buf = gst_buffer_new ();
4593 gst_buffer_append_memory (buf,
4594 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4596 /* don't need message anymore */
4597 gst_rtsp_message_unset (message);
4599 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4602 if (src->need_activate) {
4608 guint group_id = gst_util_group_id_next ();
4611 /* generate an SHA256 sum of the URI */
4612 cs = g_checksum_new (G_CHECKSUM_SHA256);
4613 uri = src->conninfo.location;
4614 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4616 gst_segment_init (&segment, GST_FORMAT_TIME);
4618 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4619 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4623 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4624 event = gst_event_new_stream_start (stream_id);
4625 gst_event_set_group_id (event, group_id);
4628 gst_rtspsrc_stream_push_event (src, ostream, event);
4630 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
4631 /* only streams that have a connection to the outside world */
4632 if (ostream->setup) {
4633 if (ostream->udpsrc[0]) {
4634 gst_element_send_event (ostream->udpsrc[0],
4635 gst_event_new_caps (caps));
4636 } else if (ostream->channelpad[0]) {
4637 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
4638 gst_pad_push_event (ostream->channelpad[0],
4639 gst_event_new_caps (caps));
4641 gst_pad_send_event (ostream->channelpad[0],
4642 gst_event_new_caps (caps));
4645 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4647 if (ostream->udpsrc[1]) {
4648 gst_element_send_event (ostream->udpsrc[1],
4649 gst_event_new_caps (caps));
4650 } else if (ostream->channelpad[1]) {
4651 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
4652 gst_pad_push_event (ostream->channelpad[1],
4653 gst_event_new_caps (caps));
4655 gst_pad_send_event (ostream->channelpad[1],
4656 gst_event_new_caps (caps));
4661 /* Push a SEGMENT event if we don't have one pending, if we have one
4662 * pending we will just send that one a few lines below to all pads
4664 if (!src->start_segment)
4665 gst_rtspsrc_stream_push_event (src, ostream,
4666 gst_event_new_segment (&segment));
4668 g_checksum_free (cs);
4670 gst_rtspsrc_activate_streams (src);
4671 src->need_activate = FALSE;
4674 if ((event = src->start_segment) != NULL) {
4675 src->start_segment = NULL;
4676 gst_rtspsrc_push_event (src, event);
4679 if (src->base_time == -1) {
4680 /* Take current running_time. This timestamp will be put on
4681 * the first buffer of each stream because we are a live source and so we
4682 * timestamp with the running_time. When we are dealing with TCP, we also
4683 * only timestamp the first buffer (using the DISCONT flag) because a server
4684 * typically bursts data, for which we don't want to compensate by speeding
4685 * up the media. The other timestamps will be interpollated from this one
4686 * using the RTP timestamps. */
4687 GST_OBJECT_LOCK (src);
4688 if (GST_ELEMENT_CLOCK (src)) {
4690 GstClockTime base_time;
4692 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4693 base_time = GST_ELEMENT_CAST (src)->base_time;
4695 src->base_time = now - base_time;
4697 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4698 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4700 GST_OBJECT_UNLOCK (src);
4703 if (stream->discont && !is_rtcp) {
4704 /* mark first RTP buffer as discont */
4705 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4706 stream->discont = FALSE;
4707 /* first buffer gets the timestamp, other buffers are not timestamped and
4708 * their presentation time will be interpollated from the rtp timestamps. */
4709 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4710 GST_TIME_ARGS (src->base_time));
4712 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4715 /* chain to the peer pad */
4716 if (GST_PAD_IS_SINK (outpad))
4717 ret = gst_pad_chain (outpad, buf);
4719 ret = gst_pad_push (outpad, buf);
4722 /* combine all stream flows for the data transport */
4723 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4730 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4731 gst_rtsp_message_unset (message);
4736 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4737 ("Short message received, ignoring."));
4738 gst_rtsp_message_unset (message);
4743 static GstFlowReturn
4744 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4746 GstRTSPMessage message = { 0 };
4748 GstFlowReturn ret = GST_FLOW_OK;
4749 GTimeVal tv_timeout;
4752 /* get the next timeout interval */
4753 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4755 /* see if the timeout period expired */
4756 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4757 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4758 /* send keep-alive, only act on interrupt, a warning will be posted for
4760 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4762 /* get new timeout */
4763 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4766 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4767 tv_timeout.tv_sec, tv_timeout.tv_usec);
4769 /* protect the connection with the connection lock so that we can see when
4770 * we are finished doing server communication */
4772 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4773 &message, src->ptcp_timeout);
4777 GST_DEBUG_OBJECT (src, "we received a server message");
4779 case GST_RTSP_EINTR:
4780 /* we got interrupted this means we need to stop */
4782 case GST_RTSP_ETIMEOUT:
4783 /* no reply, send keep alive */
4784 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4785 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4789 /* go EOS when the server closed the connection */
4795 switch (message.type) {
4796 case GST_RTSP_MESSAGE_REQUEST:
4797 /* server sends us a request message, handle it */
4799 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4801 if (res == GST_RTSP_EEOF)
4804 goto handle_request_failed;
4806 case GST_RTSP_MESSAGE_RESPONSE:
4807 /* we ignore response messages */
4808 GST_DEBUG_OBJECT (src, "ignoring response message");
4810 gst_rtsp_message_dump (&message);
4812 case GST_RTSP_MESSAGE_DATA:
4813 GST_DEBUG_OBJECT (src, "got data message");
4814 ret = gst_rtspsrc_handle_data (src, &message);
4815 if (ret != GST_FLOW_OK)
4816 goto handle_data_failed;
4819 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4824 g_assert_not_reached ();
4829 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4830 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4831 ("The server closed the connection."));
4832 src->conninfo.connected = FALSE;
4833 gst_rtsp_message_unset (&message);
4834 return GST_FLOW_EOS;
4838 gst_rtsp_message_unset (&message);
4839 GST_DEBUG_OBJECT (src, "got interrupted");
4840 return GST_FLOW_FLUSHING;
4844 gchar *str = gst_rtsp_strresult (res);
4846 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4847 ("Could not receive message. (%s)", str));
4850 gst_rtsp_message_unset (&message);
4851 return GST_FLOW_ERROR;
4853 handle_request_failed:
4855 gchar *str = gst_rtsp_strresult (res);
4857 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4858 ("Could not handle server message. (%s)", str));
4860 gst_rtsp_message_unset (&message);
4861 return GST_FLOW_ERROR;
4865 GST_DEBUG_OBJECT (src, "could no handle data message");
4870 static GstFlowReturn
4871 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4874 GstRTSPMessage message = { 0 };
4878 GTimeVal tv_timeout;
4880 /* get the next timeout interval */
4881 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4883 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4884 (gint) tv_timeout.tv_sec);
4886 gst_rtsp_message_unset (&message);
4888 /* we should continue reading the TCP socket because the server might
4889 * send us requests. When the session timeout expires, we need to send a
4890 * keep-alive request to keep the session open. */
4891 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4892 &message, &tv_timeout);
4896 GST_DEBUG_OBJECT (src, "we received a server message");
4898 case GST_RTSP_EINTR:
4899 /* we got interrupted, see what we have to do */
4901 case GST_RTSP_ETIMEOUT:
4902 /* send keep-alive, ignore the result, a warning will be posted. */
4903 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4904 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4908 /* server closed the connection. not very fatal for UDP, reconnect and
4909 * see what happens. */
4910 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4911 ("The server closed the connection."));
4912 if (src->udp_reconnect) {
4914 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4921 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4923 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4924 ("Unhandled return value %d.", res));
4928 switch (message.type) {
4929 case GST_RTSP_MESSAGE_REQUEST:
4930 /* server sends us a request message, handle it */
4932 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4934 if (res == GST_RTSP_EEOF)
4937 goto handle_request_failed;
4939 case GST_RTSP_MESSAGE_RESPONSE:
4940 /* we ignore response and data messages */
4941 GST_DEBUG_OBJECT (src, "ignoring response message");
4943 gst_rtsp_message_dump (&message);
4944 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4945 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4946 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4947 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4948 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4955 case GST_RTSP_MESSAGE_DATA:
4956 /* we ignore response and data messages */
4957 GST_DEBUG_OBJECT (src, "ignoring data message");
4960 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4965 g_assert_not_reached ();
4967 /* we get here when the connection got interrupted */
4970 gst_rtsp_message_unset (&message);
4971 GST_DEBUG_OBJECT (src, "got interrupted");
4972 return GST_FLOW_FLUSHING;
4976 gchar *str = gst_rtsp_strresult (res);
4979 src->conninfo.connected = FALSE;
4980 if (res != GST_RTSP_EINTR) {
4981 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4982 ("Could not connect to server. (%s)", str));
4984 ret = GST_FLOW_ERROR;
4986 ret = GST_FLOW_FLUSHING;
4992 gchar *str = gst_rtsp_strresult (res);
4994 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4995 ("Could not receive message. (%s)", str));
4997 return GST_FLOW_ERROR;
4999 handle_request_failed:
5001 gchar *str = gst_rtsp_strresult (res);
5004 gst_rtsp_message_unset (&message);
5005 if (res != GST_RTSP_EINTR) {
5006 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5007 ("Could not handle server message. (%s)", str));
5009 ret = GST_FLOW_ERROR;
5011 ret = GST_FLOW_FLUSHING;
5017 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5018 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5019 ("The server closed the connection."));
5020 src->conninfo.connected = FALSE;
5021 gst_rtsp_message_unset (&message);
5022 return GST_FLOW_EOS;
5026 static GstRTSPResult
5027 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
5029 GstRTSPResult res = GST_RTSP_OK;
5032 GST_DEBUG_OBJECT (src, "doing reconnect");
5034 GST_OBJECT_LOCK (src);
5035 /* only restart when the pads were not yet activated, else we were
5036 * streaming over UDP */
5037 restart = src->need_activate;
5038 GST_OBJECT_UNLOCK (src);
5040 /* no need to restart, we're done */
5044 /* we can try only TCP now */
5045 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
5047 /* close and cleanup our state */
5048 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
5051 /* see if we have TCP left to try. Also don't try TCP when we were configured
5053 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
5056 /* We post a warning message now to inform the user
5057 * that nothing happened. It's most likely a firewall thing. */
5058 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5059 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5060 "firewall is blocking it. Retrying using a TCP connection.",
5061 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
5063 /* open new connection using tcp */
5064 if (gst_rtspsrc_open (src, async) < 0)
5067 /* start playback */
5068 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
5077 src->cur_protocols = 0;
5078 /* no transport possible, post an error and stop */
5079 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5080 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5081 "firewall is blocking it. No other protocols to try.",
5082 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
5083 return GST_RTSP_ERROR;
5087 GST_DEBUG_OBJECT (src, "open failed");
5092 GST_DEBUG_OBJECT (src, "play failed");
5098 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
5102 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
5105 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
5108 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
5111 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
5119 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
5123 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
5126 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
5129 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
5132 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
5140 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
5144 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
5147 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
5150 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
5153 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
5161 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
5165 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
5168 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
5171 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
5174 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
5182 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
5184 if (ret == GST_RTSP_OK)
5185 gst_rtspsrc_loop_complete_cmd (src, cmd);
5186 else if (ret == GST_RTSP_EINTR)
5187 gst_rtspsrc_loop_cancel_cmd (src, cmd);
5189 gst_rtspsrc_loop_error_cmd (src, cmd);
5193 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
5196 gboolean flushed = FALSE;
5198 /* start new request */
5199 gst_rtspsrc_loop_start_cmd (src, cmd);
5201 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
5203 GST_OBJECT_LOCK (src);
5204 old = src->pending_cmd;
5205 if (old == CMD_RECONNECT) {
5206 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
5207 cmd = CMD_RECONNECT;
5209 if (old != CMD_WAIT) {
5210 src->pending_cmd = CMD_WAIT;
5211 GST_OBJECT_UNLOCK (src);
5212 /* cancel previous request */
5213 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
5214 gst_rtspsrc_loop_cancel_cmd (src, old);
5215 GST_OBJECT_LOCK (src);
5217 src->pending_cmd = cmd;
5218 /* interrupt if allowed */
5219 if (src->busy_cmd & mask) {
5220 GST_DEBUG_OBJECT (src, "connection flush busy %s",
5221 cmd_to_string (src->busy_cmd));
5222 gst_rtspsrc_connection_flush (src, TRUE);
5225 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
5226 cmd_to_string (src->busy_cmd));
5229 gst_task_start (src->task);
5230 GST_OBJECT_UNLOCK (src);
5236 gst_rtspsrc_loop (GstRTSPSrc * src)
5240 if (!src->conninfo.connection || !src->conninfo.connected)
5243 if (src->interleaved)
5244 ret = gst_rtspsrc_loop_interleaved (src);
5246 ret = gst_rtspsrc_loop_udp (src);
5248 if (ret != GST_FLOW_OK)
5256 GST_WARNING_OBJECT (src, "we are not connected");
5257 ret = GST_FLOW_FLUSHING;
5262 const gchar *reason = gst_flow_get_name (ret);
5264 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
5265 src->running = FALSE;
5266 if (ret == GST_FLOW_EOS) {
5267 /* perform EOS logic */
5268 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5269 gst_element_post_message (GST_ELEMENT_CAST (src),
5270 gst_message_new_segment_done (GST_OBJECT_CAST (src),
5271 src->segment.format, src->segment.position));
5272 gst_rtspsrc_push_event (src,
5273 gst_event_new_segment_done (src->segment.format,
5274 src->segment.position));
5276 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5278 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
5279 /* for fatal errors we post an error message, post the error before the
5280 * EOS so the app knows about the error first. */
5281 GST_ELEMENT_ERROR (src, STREAM, FAILED,
5282 ("Internal data flow error."),
5283 ("streaming task paused, reason %s (%d)", reason, ret));
5284 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5286 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
5291 #ifndef GST_DISABLE_GST_DEBUG
5292 static const gchar *
5293 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
5297 while (method != 0) {
5314 static const gchar *
5315 gst_rtspsrc_skip_lws (const gchar * s)
5317 while (g_ascii_isspace (*s))
5322 static const gchar *
5323 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
5325 while (s > start && g_ascii_isspace (*(s - 1)))
5330 static const gchar *
5331 gst_rtspsrc_skip_commas (const gchar * s)
5333 /* The grammar allows for multiple commas */
5334 while (g_ascii_isspace (*s) || *s == ',')
5339 static const gchar *
5340 gst_rtspsrc_skip_item (const gchar * s)
5342 gboolean quoted = FALSE;
5343 const gchar *start = s;
5345 /* A list item ends at the last non-whitespace character
5346 * before a comma which is not inside a quoted-string. Or at
5347 * the end of the string.
5353 if (*s == '\\' && *(s + 1))
5362 return gst_rtspsrc_unskip_lws (s, start);
5366 gst_rtsp_decode_quoted_string (gchar * quoted_string)
5370 src = quoted_string + 1;
5371 dst = quoted_string;
5372 while (*src && *src != '"') {
5373 if (*src == '\\' && *(src + 1))
5380 /* Extract the authentication tokens that the server provided for each method
5381 * into an array of structures and give those to the connection object.
5384 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
5385 const gchar * header, gboolean * stale)
5387 GSList *list = NULL, *iter;
5389 gchar *item, *eq, *name_end, *value;
5391 g_return_if_fail (stale != NULL);
5393 gst_rtsp_connection_clear_auth_params (conn);
5396 /* Parse a header whose content is described by RFC2616 as
5397 * "#something", where "something" does not itself contain commas,
5398 * except as part of quoted-strings, into a list of allocated strings.
5400 header = gst_rtspsrc_skip_commas (header);
5402 end = gst_rtspsrc_skip_item (header);
5403 list = g_slist_prepend (list, g_strndup (header, end - header));
5404 header = gst_rtspsrc_skip_commas (end);
5409 list = g_slist_reverse (list);
5410 for (iter = list; iter; iter = iter->next) {
5413 eq = strchr (item, '=');
5415 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
5416 if (name_end == item) {
5417 /* That's no good... */
5424 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
5426 gst_rtsp_decode_quoted_string (value);
5430 if (value && strcmp (item, "stale") == 0 && strcmp (value, "TRUE") == 0)
5432 gst_rtsp_connection_set_auth_param (conn, item, value);
5436 g_slist_free (list);
5439 /* Parse a WWW-Authenticate Response header and determine the
5440 * available authentication methods
5442 * This code should also cope with the fact that each WWW-Authenticate
5443 * header can contain multiple challenge methods + tokens
5445 * At the moment, for Basic auth, we just do a minimal check and don't
5446 * even parse out the realm */
5448 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
5449 GstRTSPConnection * conn, gboolean * stale)
5453 g_return_if_fail (hdr != NULL);
5454 g_return_if_fail (methods != NULL);
5455 g_return_if_fail (stale != NULL);
5457 /* Skip whitespace at the start of the string */
5458 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
5460 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
5461 *methods |= GST_RTSP_AUTH_BASIC;
5462 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
5463 *methods |= GST_RTSP_AUTH_DIGEST;
5464 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
5469 * gst_rtspsrc_setup_auth:
5470 * @src: the rtsp source
5472 * Configure a username and password and auth method on the
5473 * connection object based on a response we received from the
5476 * Currently, this requires that a username and password were supplied
5477 * in the uri. In the future, they may be requested on demand by sending
5478 * a message up the bus.
5480 * Returns: TRUE if authentication information could be set up correctly.
5483 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5487 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5488 GstRTSPAuthMethod method;
5489 GstRTSPResult auth_result;
5491 GstRTSPConnection *conn;
5493 gboolean stale = FALSE;
5495 conn = src->conninfo.connection;
5497 /* Identify the available auth methods and see if any are supported */
5498 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
5499 &hdr, 0) == GST_RTSP_OK) {
5500 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
5503 if (avail_methods == GST_RTSP_AUTH_NONE)
5504 goto no_auth_available;
5506 /* For digest auth, if the response indicates that the session
5507 * data are stale, we just update them in the connection object and
5508 * return TRUE to retry the request */
5510 src->tried_url_auth = FALSE;
5512 url = gst_rtsp_connection_get_url (conn);
5514 /* Do we have username and password available? */
5515 if (url != NULL && !src->tried_url_auth && url->user != NULL
5516 && url->passwd != NULL) {
5519 src->tried_url_auth = TRUE;
5520 GST_DEBUG_OBJECT (src,
5521 "Attempting authentication using credentials from the URL");
5523 user = src->user_id;
5524 pass = src->user_pw;
5525 GST_DEBUG_OBJECT (src,
5526 "Attempting authentication using credentials from the properties");
5529 /* FIXME: If the url didn't contain username and password or we tried them
5530 * already, request a username and passwd from the application via some kind
5531 * of credentials request message */
5533 /* If we don't have a username and passwd at this point, bail out. */
5534 if (user == NULL || pass == NULL)
5537 /* Try to configure for each available authentication method, strongest to
5539 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5540 /* Check if this method is available on the server */
5541 if ((method & avail_methods) == 0)
5544 /* Pass the credentials to the connection to try on the next request */
5545 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5546 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5547 * ignore it and end up retrying later */
5548 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5549 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5550 gst_rtsp_auth_method_to_string (method));
5555 if (method == GST_RTSP_AUTH_NONE)
5556 goto no_auth_available;
5562 /* Output an error indicating that we couldn't connect because there were
5563 * no supported authentication protocols */
5564 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5565 ("No supported authentication protocol was found"));
5570 /* We don't fire an error message, we just return FALSE and let the
5571 * normal NOT_AUTHORIZED error be propagated */
5576 static GstRTSPResult
5577 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5578 GstRTSPMessage * request, GstRTSPMessage * response,
5579 GstRTSPStatusCode * code)
5582 GstRTSPStatusCode thecode;
5583 gchar *content_base = NULL;
5587 if (!src->short_header)
5588 gst_rtsp_ext_list_before_send (src->extensions, request);
5590 GST_DEBUG_OBJECT (src, "sending message");
5593 gst_rtsp_message_dump (request);
5595 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
5599 gst_rtsp_connection_reset_timeout (conn);
5602 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
5607 gst_rtsp_message_dump (response);
5609 switch (response->type) {
5610 case GST_RTSP_MESSAGE_REQUEST:
5611 res = gst_rtspsrc_handle_request (src, conn, response);
5612 if (res == GST_RTSP_EEOF)
5615 goto handle_request_failed;
5617 case GST_RTSP_MESSAGE_RESPONSE:
5618 /* ok, a response is good */
5619 GST_DEBUG_OBJECT (src, "received response message");
5621 case GST_RTSP_MESSAGE_DATA:
5622 /* get next response */
5623 GST_DEBUG_OBJECT (src, "handle data response message");
5624 gst_rtspsrc_handle_data (src, response);
5627 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5632 thecode = response->type_data.response.code;
5634 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5636 /* if the caller wanted the result code, we store it. */
5640 /* If the request didn't succeed, bail out before doing any more */
5641 if (thecode != GST_RTSP_STS_OK)
5644 /* store new content base if any */
5645 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5648 g_free (src->content_base);
5649 src->content_base = g_strdup (content_base);
5651 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5658 gchar *str = gst_rtsp_strresult (res);
5660 if (res != GST_RTSP_EINTR) {
5661 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5662 ("Could not send message. (%s)", str));
5664 GST_WARNING_OBJECT (src, "send interrupted");
5673 GST_WARNING_OBJECT (src, "server closed connection");
5674 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5676 /* if reconnect succeeds, try again */
5678 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5682 /* only try once after reconnect, then fallthrough and error out */
5685 gchar *str = gst_rtsp_strresult (res);
5687 if (res != GST_RTSP_EINTR) {
5688 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5689 ("Could not receive message. (%s)", str));
5691 GST_WARNING_OBJECT (src, "receive interrupted");
5699 handle_request_failed:
5701 /* ERROR was posted */
5702 gst_rtsp_message_unset (response);
5707 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5708 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5709 ("The server closed the connection."));
5710 gst_rtsp_message_unset (response);
5717 * @src: the rtsp source
5718 * @conn: the connection to send on
5719 * @request: must point to a valid request
5720 * @response: must point to an empty #GstRTSPMessage
5721 * @code: an optional code result
5723 * send @request and retrieve the response in @response. optionally @code can be
5724 * non-NULL in which case it will contain the status code of the response.
5726 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5727 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5729 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5730 * @response message) if the response code was not 200 (OK).
5732 * If the attempt results in an authentication failure, then this will attempt
5733 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5736 * Returns: #GST_RTSP_OK if the processing was successful.
5738 static GstRTSPResult
5739 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5740 GstRTSPMessage * request, GstRTSPMessage * response,
5741 GstRTSPStatusCode * code)
5743 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5744 GstRTSPResult res = GST_RTSP_ERROR;
5747 GstRTSPMethod method = GST_RTSP_INVALID;
5753 /* make sure we don't loop forever */
5757 /* save method so we can disable it when the server complains */
5758 method = request->type_data.request.method;
5761 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5765 case GST_RTSP_STS_UNAUTHORIZED:
5766 if (gst_rtspsrc_setup_auth (src, response)) {
5767 /* Try the request/response again after configuring the auth info
5775 } while (retry == TRUE);
5777 /* If the user requested the code, let them handle errors, otherwise
5778 * post an error below */
5781 else if (int_code != GST_RTSP_STS_OK)
5782 goto error_response;
5789 GST_DEBUG_OBJECT (src, "got error %d", res);
5794 res = GST_RTSP_ERROR;
5796 switch (response->type_data.response.code) {
5797 case GST_RTSP_STS_NOT_FOUND:
5798 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5799 response->type_data.response.reason));
5801 case GST_RTSP_STS_UNAUTHORIZED:
5802 GST_ELEMENT_ERROR (src, RESOURCE, NOT_AUTHORIZED, (NULL), ("%s",
5803 response->type_data.response.reason));
5805 case GST_RTSP_STS_MOVED_PERMANENTLY:
5806 case GST_RTSP_STS_MOVE_TEMPORARILY:
5808 gchar *new_location;
5809 GstRTSPLowerTrans transports;
5811 GST_DEBUG_OBJECT (src, "got redirection");
5812 /* if we don't have a Location Header, we must error */
5813 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5814 &new_location, 0) < 0)
5817 /* When we receive a redirect result, we go back to the INIT state after
5818 * parsing the new URI. The caller should do the needed steps to issue
5819 * a new setup when it detects this state change. */
5820 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5822 /* save current transports */
5823 if (src->conninfo.url)
5824 transports = src->conninfo.url->transports;
5826 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5828 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5830 /* set old transports */
5831 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5832 src->conninfo.url->transports = transports;
5834 src->need_redirect = TRUE;
5835 src->state = GST_RTSP_STATE_INIT;
5839 case GST_RTSP_STS_NOT_ACCEPTABLE:
5840 case GST_RTSP_STS_NOT_IMPLEMENTED:
5841 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5842 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5843 gst_rtsp_method_as_text (method));
5844 src->methods &= ~method;
5848 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5849 ("Got error response: %d (%s).", response->type_data.response.code,
5850 response->type_data.response.reason));
5853 /* if we return ERROR we should unset the response ourselves */
5854 if (res == GST_RTSP_ERROR)
5855 gst_rtsp_message_unset (response);
5861 static GstRTSPResult
5862 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5863 GstRTSPMessage * response, GstRTSPSrc * src)
5865 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5870 /* parse the response and collect all the supported methods. We need this
5871 * information so that we don't try to send an unsupported request to the
5875 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5877 GstRTSPHeaderField field;
5881 /* reset supported methods */
5884 /* Try Allow Header first */
5885 field = GST_RTSP_HDR_ALLOW;
5888 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5889 if (indx == 0 && !respoptions) {
5890 /* if no Allow header was found then try the Public header... */
5891 field = GST_RTSP_HDR_PUBLIC;
5892 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5897 src->methods |= gst_rtsp_options_from_text (respoptions);
5902 if (src->methods == 0) {
5903 /* neither Allow nor Public are required, assume the server supports
5904 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5906 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5907 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5909 /* always assume PLAY, FIXME, extensions should be able to override
5911 src->methods |= GST_RTSP_PLAY;
5912 /* also assume it will support Range */
5913 src->seekable = TRUE;
5915 /* we need describe and setup */
5916 if (!(src->methods & GST_RTSP_DESCRIBE))
5918 if (!(src->methods & GST_RTSP_SETUP))
5926 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5927 ("Server does not support DESCRIBE."));
5932 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5933 ("Server does not support SETUP."));
5938 /* masks to be kept in sync with the hardcoded protocol order of preference
5940 static const guint protocol_masks[] = {
5941 GST_RTSP_LOWER_TRANS_UDP,
5942 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5943 GST_RTSP_LOWER_TRANS_TCP,
5947 static GstRTSPResult
5948 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5949 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
5953 gboolean add_udp_str;
5958 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5963 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5965 /* extension listed transports, use those */
5966 if (*transports != NULL)
5969 /* it's the default */
5970 add_udp_str = FALSE;
5972 /* the default RTSP transports */
5973 result = g_string_new ("RTP");
5976 case GST_RTSP_PROFILE_AVP:
5977 g_string_append (result, "/AVP");
5979 case GST_RTSP_PROFILE_SAVP:
5980 g_string_append (result, "/SAVP");
5982 case GST_RTSP_PROFILE_AVPF:
5983 g_string_append (result, "/AVPF");
5985 case GST_RTSP_PROFILE_SAVPF:
5986 g_string_append (result, "/SAVPF");
5992 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5993 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5995 g_string_append (result, "/UDP");
5996 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5997 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5998 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5999 /* we don't have to allocate any UDP ports yet, if the selected transport
6000 * turns out to be multicast we can create them and join the multicast
6001 * group indicated in the transport reply */
6003 g_string_append (result, "/UDP");
6004 g_string_append (result, ";multicast");
6005 if (src->next_port_num != 0) {
6006 if (src->client_port_range.max > 0 &&
6007 src->next_port_num >= src->client_port_range.max)
6010 g_string_append_printf (result, ";client_port=%d-%d",
6011 src->next_port_num, src->next_port_num + 1);
6013 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
6014 GST_DEBUG_OBJECT (src, "adding TCP");
6016 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
6018 *transports = g_string_free (result, FALSE);
6020 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
6027 GST_ERROR ("extension gave error %d", res);
6032 GST_ERROR ("no more ports available");
6033 return GST_RTSP_ERROR;
6037 static GstRTSPResult
6038 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
6039 gint orig_rtpport, gint orig_rtcpport)
6042 gint nr_udp, nr_int;
6044 gint rtpport = 0, rtcpport = 0;
6047 src = stream->parent;
6049 /* find number of placeholders first */
6050 if (strstr (*transports, "%%i2"))
6052 else if (strstr (*transports, "%%i1"))
6057 if (strstr (*transports, "%%u2"))
6059 else if (strstr (*transports, "%%u1"))
6064 if (nr_udp == 0 && nr_int == 0)
6068 if (!orig_rtpport || !orig_rtcpport) {
6069 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
6072 rtpport = orig_rtpport;
6073 rtcpport = orig_rtcpport;
6077 str = g_string_new ("");
6079 while ((next = strstr (p, "%%"))) {
6080 g_string_append_len (str, p, next - p);
6081 if (next[2] == 'u') {
6083 g_string_append_printf (str, "%d", rtpport);
6084 else if (next[3] == '2')
6085 g_string_append_printf (str, "%d", rtcpport);
6087 if (next[2] == 'i') {
6089 g_string_append_printf (str, "%d", src->free_channel);
6090 else if (next[3] == '2')
6091 g_string_append_printf (str, "%d", src->free_channel + 1);
6096 /* append final part */
6097 g_string_append (str, p);
6099 g_free (*transports);
6100 *transports = g_string_free (str, FALSE);
6108 GST_ERROR ("failed to allocate udp ports");
6109 return GST_RTSP_ERROR;
6114 enc_key_length_from_cipher_name (const gchar * cipher)
6116 if (g_strcmp0 (cipher, "aes-128-icm") == 0)
6117 return AES_128_KEY_LEN;
6118 else if (g_strcmp0 (cipher, "aes-256-icm") == 0)
6119 return AES_256_KEY_LEN;
6121 GST_ERROR ("encryption algorithm '%s' not supported", cipher);
6127 auth_key_length_from_auth_name (const gchar * auth)
6129 if (g_strcmp0 (auth, "hmac-sha1-32") == 0)
6130 return HMAC_32_KEY_LEN;
6131 else if (g_strcmp0 (auth, "hmac-sha1-80") == 0)
6132 return HMAC_80_KEY_LEN;
6134 GST_ERROR ("authentication algorithm '%s' not supported", auth);
6140 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
6142 GstCaps *caps = NULL;
6144 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
6148 GST_DEBUG_OBJECT (src, "SRTP parameters received");
6154 default_srtcp_params (void)
6162 /* create a random key */
6163 key_data = g_malloc (KEY_SIZE);
6164 for (i = 0; i < KEY_SIZE; i += 4)
6165 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
6167 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
6169 caps = gst_caps_new_simple ("application/x-srtp",
6170 "srtp-key", GST_TYPE_BUFFER, buf,
6171 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
6172 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
6174 gst_buffer_unref (buf);
6180 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
6183 gchar *result, *base64;
6186 GstMIKEYMessage *msg;
6187 GstMIKEYPayload *payload, *pkd;
6193 const gchar *srtcpcipher, *srtcpauth;
6195 stream->srtcpparams = signal_get_srtcp_params (src, stream);
6196 if (stream->srtcpparams == NULL)
6197 stream->srtcpparams = default_srtcp_params ();
6199 s = gst_caps_get_structure (stream->srtcpparams, 0);
6201 srtcpcipher = gst_structure_get_string (s, "srtcp-cipher");
6202 srtcpauth = gst_structure_get_string (s, "srtcp-auth");
6203 val = gst_structure_get_value (s, "srtp-key");
6205 if (srtcpcipher == NULL || srtcpauth == NULL || val == NULL) {
6206 GST_ERROR_OBJECT (src, "could not find the right SRTP parameters in caps");
6210 srtpkey = gst_value_get_buffer (val);
6212 msg = gst_mikey_message_new ();
6213 /* unencrypted MIKEY message, we send this over TLS so this is allowed */
6214 gst_mikey_message_set_info (msg, GST_MIKEY_VERSION, GST_MIKEY_TYPE_PSK_INIT,
6215 FALSE, GST_MIKEY_PRF_MIKEY_1, g_random_int (), GST_MIKEY_MAP_TYPE_SRTP);
6216 /* add policy '0' for our SSRC */
6217 gst_mikey_message_add_cs_srtp (msg, 0, stream->send_ssrc, 0);
6218 /* timestamp is now */
6219 gst_mikey_message_add_t_now_ntp_utc (msg);
6220 /* add some random data */
6221 gst_mikey_message_add_rand_len (msg, 16);
6223 /* the policy '0' is SRTP */
6224 payload = gst_mikey_payload_new (GST_MIKEY_PT_SP);
6225 gst_mikey_payload_sp_set (payload, 0, GST_MIKEY_SEC_PROTO_SRTP);
6227 /* only AES-CM is supported */
6229 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_ALG, 1, &byte);
6230 /* encryption key length */
6231 byte = enc_key_length_from_cipher_name (srtcpcipher);
6232 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_KEY_LEN, 1,
6234 /* only HMAC-SHA1 */
6235 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_ALG, 1,
6237 /* authentication key length */
6238 byte = auth_key_length_from_auth_name (srtcpauth);
6239 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_KEY_LEN, 1,
6241 /* we enable encryption on RTP and RTCP */
6242 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_ENC, 1,
6244 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTCP_ENC, 1,
6246 /* we enable authentication on RTP and RTCP */
6247 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_AUTH, 1,
6249 gst_mikey_message_add_payload (msg, payload);
6251 /* make unencrypted KEMAC */
6252 payload = gst_mikey_payload_new (GST_MIKEY_PT_KEMAC);
6253 gst_mikey_payload_kemac_set (payload, GST_MIKEY_ENC_NULL, GST_MIKEY_MAC_NULL);
6254 /* add the key in KEMAC */
6255 pkd = gst_mikey_payload_new (GST_MIKEY_PT_KEY_DATA);
6256 gst_buffer_map (srtpkey, &info, GST_MAP_READ);
6257 gst_mikey_payload_key_data_set_key (pkd, GST_MIKEY_KD_TEK, info.size,
6259 gst_buffer_unmap (srtpkey, &info);
6260 gst_mikey_payload_kemac_add_sub (payload, pkd);
6261 gst_mikey_message_add_payload (msg, payload);
6263 /* now serialize this to bytes */
6264 bytes = gst_mikey_message_to_bytes (msg, NULL, NULL);
6265 gst_mikey_message_unref (msg);
6266 /* and make it into base64 */
6267 data = g_bytes_get_data (bytes, &size);
6268 base64 = g_base64_encode (data, size);
6269 g_bytes_unref (bytes);
6271 result = g_strdup_printf ("prot=mikey;uri=\"%s\";data=\"%s\"",
6272 stream->conninfo.location, base64);
6279 /* Perform the SETUP request for all the streams.
6281 * We ask the server for a specific transport, which initially includes all the
6282 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
6283 * two local UDP ports that we send to the server.
6285 * Once the server replied with a transport, we configure the other streams
6286 * with the same transport.
6288 * This function will also configure the stream for the selected transport,
6289 * which basically means creating the pipeline.
6291 static GstRTSPResult
6292 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
6295 GstRTSPResult res = GST_RTSP_ERROR;
6296 GstRTSPMessage request = { 0 };
6297 GstRTSPMessage response = { 0 };
6298 GstRTSPStream *stream = NULL;
6299 GstRTSPLowerTrans protocols;
6300 GstRTSPStatusCode code;
6301 gboolean unsupported_real = FALSE;
6302 gint rtpport, rtcpport;
6306 if (src->conninfo.connection) {
6307 url = gst_rtsp_connection_get_url (src->conninfo.connection);
6308 /* we initially allow all configured lower transports. based on the URL
6309 * transports and the replies from the server we narrow them down. */
6310 protocols = url->transports & src->cur_protocols;
6313 protocols = src->cur_protocols;
6319 /* reset some state */
6320 src->free_channel = 0;
6321 src->interleaved = FALSE;
6322 src->need_activate = FALSE;
6323 /* keep track of next port number, 0 is random */
6324 src->next_port_num = src->client_port_range.min;
6325 rtpport = rtcpport = 0;
6327 if (G_UNLIKELY (src->streams == NULL))
6330 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6331 GstRTSPConnection *conn;
6338 stream = (GstRTSPStream *) walk->data;
6340 caps = stream_get_caps_for_pt (stream, stream->default_pt);
6342 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
6346 if (stream->skipped) {
6347 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
6351 /* see if we need to configure this stream */
6352 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
6353 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
6358 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
6359 stream->id, caps, &selected);
6361 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
6365 /* merge/overwrite global caps */
6370 s = gst_caps_get_structure (caps, 0);
6372 num = gst_structure_n_fields (src->props);
6373 for (j = 0; j < num; j++) {
6377 name = gst_structure_nth_field_name (src->props, j);
6378 val = gst_structure_get_value (src->props, name);
6379 gst_structure_set_value (s, name, val);
6381 GST_DEBUG_OBJECT (src, "copied %s", name);
6385 /* skip setup if we have no URL for it */
6386 if (stream->conninfo.location == NULL) {
6387 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
6391 if (src->conninfo.connection == NULL) {
6392 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
6393 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
6396 conn = stream->conninfo.connection;
6398 conn = src->conninfo.connection;
6400 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
6401 stream->conninfo.location);
6403 /* if we have a multicast connection, only suggest multicast from now on */
6404 if (stream->is_multicast)
6405 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
6408 /* first selectable protocol */
6409 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6411 if (!protocol_masks[mask])
6415 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
6416 protocol_masks[mask]);
6417 /* create a string with first transport in line */
6419 res = gst_rtspsrc_create_transports_string (src,
6420 protocols & protocol_masks[mask], stream->profile, &transports);
6421 if (res < 0 || transports == NULL)
6422 goto setup_transport_failed;
6424 if (strlen (transports) == 0) {
6425 g_free (transports);
6426 GST_DEBUG_OBJECT (src, "no transports found");
6431 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
6433 /* replace placeholders with real values, this function will optionally
6434 * allocate UDP ports and other info needed to execute the setup request */
6435 res = gst_rtspsrc_prepare_transports (stream, &transports,
6436 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
6438 g_free (transports);
6439 goto setup_transport_failed;
6442 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
6444 /* create SETUP request */
6446 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
6447 stream->conninfo.location);
6449 g_free (transports);
6450 goto create_request_failed;
6453 /* select transport */
6454 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
6457 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
6458 stream->profile == GST_RTSP_PROFILE_SAVPF) {
6459 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
6460 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
6463 /* if the user wants a non default RTP packet size we add the blocksize
6465 if (src->rtp_blocksize > 0) {
6466 hval = g_strdup_printf ("%d", src->rtp_blocksize);
6467 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
6471 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
6474 /* handle the code ourselves */
6475 res = gst_rtspsrc_send (src, conn, &request, &response, &code);
6480 case GST_RTSP_STS_OK:
6482 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
6483 gst_rtsp_message_unset (&request);
6484 gst_rtsp_message_unset (&response);
6485 /* cleanup of leftover transport */
6486 gst_rtspsrc_stream_free_udp (stream);
6487 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
6488 * we might be in this case */
6489 if (stream->container && rtpport && rtcpport && !retry) {
6490 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
6495 /* this transport did not go down well, but we may have others to try
6496 * that we did not send yet, try those and only give up then
6497 * but not without checking for lost cause/extension so we can
6498 * post a nicer/more useful error message later */
6499 if (!unsupported_real)
6500 unsupported_real = stream->is_real;
6501 /* select next available protocol, give up on this stream if none */
6503 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6505 if (!protocol_masks[mask] || unsupported_real)
6510 /* cleanup of leftover transport and move to the next stream */
6511 gst_rtspsrc_stream_free_udp (stream);
6512 goto response_error;
6515 /* parse response transport */
6517 gchar *resptrans = NULL;
6518 GstRTSPTransport transport = { 0 };
6520 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
6523 gst_rtspsrc_stream_free_udp (stream);
6527 /* parse transport, go to next stream on parse error */
6528 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6529 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6533 /* update allowed transports for other streams. once the transport of
6534 * one stream has been determined, we make sure that all other streams
6535 * are configured in the same way */
6536 switch (transport.lower_transport) {
6537 case GST_RTSP_LOWER_TRANS_TCP:
6538 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6539 protocols = GST_RTSP_LOWER_TRANS_TCP;
6540 src->interleaved = TRUE;
6541 /* update free channels */
6543 MAX (transport.interleaved.min, src->free_channel);
6545 MAX (transport.interleaved.max, src->free_channel);
6546 src->free_channel++;
6548 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6549 /* only allow multicast for other streams */
6550 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6551 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6552 /* if the server selected our ports, increment our counters so that
6553 * we select a new port later */
6554 if (src->next_port_num == transport.port.min &&
6555 src->next_port_num + 1 == transport.port.max) {
6556 src->next_port_num += 2;
6559 case GST_RTSP_LOWER_TRANS_UDP:
6560 /* only allow unicast for other streams */
6561 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6562 protocols = GST_RTSP_LOWER_TRANS_UDP;
6565 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6566 transport.lower_transport);
6570 if (!src->interleaved || !retry) {
6571 /* now configure the stream with the selected transport */
6572 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6573 GST_DEBUG_OBJECT (src,
6574 "could not configure stream %p transport, skipping stream",
6577 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
6578 /* retain the first allocated UDP port pair */
6579 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
6580 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
6583 /* we need to activate at least one streams when we detect activity */
6584 src->need_activate = TRUE;
6586 /* stream is setup now */
6587 stream->setup = TRUE;
6592 GstRTSPStream *sskip;
6594 skip = g_list_next (skip);
6598 sskip = (GstRTSPStream *) skip->data;
6600 /* skip all streams with the same control url */
6601 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
6602 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
6603 sskip, sskip->conninfo.location);
6604 sskip->skipped = TRUE;
6609 /* clean up our transport struct */
6610 gst_rtsp_transport_init (&transport);
6611 /* clean up used RTSP messages */
6612 gst_rtsp_message_unset (&request);
6613 gst_rtsp_message_unset (&response);
6617 /* store the transport protocol that was configured */
6618 src->cur_protocols = protocols;
6620 gst_rtsp_ext_list_stream_select (src->extensions, url);
6622 /* if there is nothing to activate, error out */
6623 if (!src->need_activate)
6624 goto nothing_to_activate;
6631 /* no transport possible, post an error and stop */
6632 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6633 ("Could not connect to server, no protocols left"));
6634 return GST_RTSP_ERROR;
6638 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6639 ("SDP contains no streams"));
6640 return GST_RTSP_ERROR;
6642 create_request_failed:
6644 gchar *str = gst_rtsp_strresult (res);
6646 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6647 ("Could not create request. (%s)", str));
6651 setup_transport_failed:
6653 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6654 ("Could not setup transport."));
6655 res = GST_RTSP_ERROR;
6660 const gchar *str = gst_rtsp_status_as_text (code);
6662 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6663 ("Error (%d): %s", code, GST_STR_NULL (str)));
6664 res = GST_RTSP_ERROR;
6669 gchar *str = gst_rtsp_strresult (res);
6671 if (res != GST_RTSP_EINTR) {
6672 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6673 ("Could not send message. (%s)", str));
6675 GST_WARNING_OBJECT (src, "send interrupted");
6682 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6683 ("Server did not select transport."));
6684 res = GST_RTSP_ERROR;
6687 nothing_to_activate:
6689 /* none of the available error codes is really right .. */
6690 if (unsupported_real) {
6691 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6692 (_("No supported stream was found. You might need to install a "
6693 "GStreamer RTSP extension plugin for Real media streams.")),
6696 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6697 (_("No supported stream was found. You might need to allow "
6698 "more transport protocols or may otherwise be missing "
6699 "the right GStreamer RTSP extension plugin.")), (NULL));
6701 return GST_RTSP_ERROR;
6705 gst_rtsp_message_unset (&request);
6706 gst_rtsp_message_unset (&response);
6712 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
6713 GstSegment * segment)
6716 GstRTSPTimeRange *therange;
6719 gst_rtsp_range_free (src->range);
6721 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
6722 GST_DEBUG_OBJECT (src, "parsed range %s", range);
6723 src->range = therange;
6725 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6727 gst_segment_init (segment, GST_FORMAT_TIME);
6731 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6732 therange->min.type, therange->min.seconds, therange->max.type,
6733 therange->max.seconds);
6735 if (therange->min.type == GST_RTSP_TIME_NOW)
6737 else if (therange->min.type == GST_RTSP_TIME_END)
6740 seconds = therange->min.seconds * GST_SECOND;
6742 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6743 GST_TIME_ARGS (seconds));
6745 /* we need to start playback without clipping from the position reported by
6747 segment->start = seconds;
6748 segment->position = seconds;
6750 if (therange->max.type == GST_RTSP_TIME_NOW)
6752 else if (therange->max.type == GST_RTSP_TIME_END)
6755 seconds = therange->max.seconds * GST_SECOND;
6757 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6758 GST_TIME_ARGS (seconds));
6760 /* live (WMS) server might send overflowed large max as its idea of infinity,
6761 * compensate to prevent problems later on */
6762 if (seconds != -1 && seconds < 0) {
6764 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6767 /* live (WMS) might send min == max, which is not worth recording */
6768 if (segment->duration == -1 && seconds == segment->start)
6771 /* don't change duration with unknown value, we might have a valid value
6772 * there that we want to keep. */
6774 segment->duration = seconds;
6779 /* Parse clock profived by the server with following syntax:
6781 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6784 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6786 gboolean res = FALSE;
6788 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6789 gchar **fields = NULL, **parts = NULL;
6790 gchar *remote_ip, *str;
6792 GstClockTime base_time;
6795 fields = g_strsplit (gstclock, " ", 0);
6797 /* wrapped clock, not very interesting for now */
6798 if (fields[1] == NULL)
6801 /* remote IP address and port */
6802 if ((str = fields[2]) == NULL)
6805 parts = g_strsplit (str, ":", 0);
6807 if ((remote_ip = parts[0]) == NULL)
6810 if ((str = parts[1]) == NULL)
6818 if ((str = fields[3]) == NULL)
6821 base_time = g_ascii_strtoull (str, NULL, 10);
6824 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
6827 if (src->provided_clock)
6828 gst_object_unref (src->provided_clock);
6829 src->provided_clock = netclock;
6831 gst_element_post_message (GST_ELEMENT_CAST (src),
6832 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
6833 src->provided_clock, TRUE));
6837 g_strfreev (fields);
6843 /* must be called with the RTSP state lock */
6844 static GstRTSPResult
6845 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
6851 /* prepare global stream caps properties */
6853 gst_structure_remove_all_fields (src->props);
6855 src->props = gst_structure_new_empty ("RTSPProperties");
6858 gst_sdp_message_dump (sdp);
6860 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
6862 /* let the app inspect and change the SDP */
6863 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6865 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6867 /* parse range for duration reporting. */
6872 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6876 /* keep track of the range and configure it in the segment */
6877 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6881 /* parse clock information. This is GStreamer specific, a server can tell the
6882 * client what clock it is using and wrap that in a network clock. The
6883 * advantage of that is that we can slave to it. */
6885 const gchar *gstclock;
6888 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6889 if (gstclock == NULL)
6892 /* parse the clock and expose it in the provide_clock method */
6893 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6897 /* try to find a global control attribute. Note that a '*' means that we should
6898 * do aggregate control with the current url (so we don't do anything and
6899 * leave the current connection as is) */
6901 const gchar *control;
6904 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6905 if (control == NULL)
6908 /* only take fully qualified urls */
6909 if (g_str_has_prefix (control, "rtsp://"))
6913 g_free (src->conninfo.location);
6914 src->conninfo.location = g_strdup (control);
6915 /* make a connection for this, if there was a connection already, nothing
6917 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6918 GST_ERROR_OBJECT (src, "could not connect");
6921 /* we need to keep the control url separate from the connection url because
6922 * the rules for constructing the media control url need it */
6923 g_free (src->control);
6924 src->control = g_strdup (control);
6927 /* create streams */
6928 n_streams = gst_sdp_message_medias_len (sdp);
6929 for (i = 0; i < n_streams; i++) {
6930 gst_rtspsrc_create_stream (src, sdp, i);
6933 src->state = GST_RTSP_STATE_INIT;
6936 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6939 /* reset our state */
6940 src->need_range = TRUE;
6943 src->state = GST_RTSP_STATE_READY;
6950 GST_ERROR_OBJECT (src, "setup failed");
6951 gst_rtspsrc_cleanup (src);
6956 static GstRTSPResult
6957 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6961 GstRTSPMessage request = { 0 };
6962 GstRTSPMessage response = { 0 };
6965 gchar *respcont = NULL;
6968 src->need_redirect = FALSE;
6970 /* can't continue without a valid url */
6971 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6972 res = GST_RTSP_EINVAL;
6975 src->tried_url_auth = FALSE;
6977 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6978 goto connect_failed;
6980 /* create OPTIONS */
6981 GST_DEBUG_OBJECT (src, "create options...");
6983 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
6984 src->conninfo.url_str);
6986 goto create_request_failed;
6989 GST_DEBUG_OBJECT (src, "send options...");
6992 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6995 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
7000 if (!gst_rtspsrc_parse_methods (src, &response))
7003 /* create DESCRIBE */
7004 GST_DEBUG_OBJECT (src, "create describe...");
7006 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
7007 src->conninfo.url_str);
7009 goto create_request_failed;
7011 /* we only accept SDP for now */
7012 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
7016 GST_DEBUG_OBJECT (src, "send describe...");
7019 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
7022 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
7026 /* we only perform redirect for the describe, currently */
7027 if (src->need_redirect) {
7028 /* close connection, we don't have to send a TEARDOWN yet, ignore the
7030 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7032 gst_rtsp_message_unset (&request);
7033 gst_rtsp_message_unset (&response);
7039 /* it could be that the DESCRIBE method was not implemented */
7040 if (!src->methods & GST_RTSP_DESCRIBE)
7043 /* check if reply is SDP */
7044 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
7046 /* could not be set but since the request returned OK, we assume it
7047 * was SDP, else check it. */
7049 if (g_ascii_strcasecmp (respcont, "application/sdp") != 0)
7050 goto wrong_content_type;
7053 /* get message body and parse as SDP */
7054 gst_rtsp_message_get_body (&response, &data, &size);
7055 if (data == NULL || size == 0)
7058 GST_DEBUG_OBJECT (src, "parse SDP...");
7059 gst_sdp_message_new (sdp);
7060 gst_sdp_message_parse_buffer (data, size, *sdp);
7062 /* clean up any messages */
7063 gst_rtsp_message_unset (&request);
7064 gst_rtsp_message_unset (&response);
7071 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
7072 ("No valid RTSP URL was provided"));
7077 gchar *str = gst_rtsp_strresult (res);
7079 if (res != GST_RTSP_EINTR) {
7080 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
7081 ("Failed to connect. (%s)", str));
7083 GST_WARNING_OBJECT (src, "connect interrupted");
7088 create_request_failed:
7090 gchar *str = gst_rtsp_strresult (res);
7092 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7093 ("Could not create request. (%s)", str));
7099 /* Don't post a message - the rtsp_send method will have
7100 * taken care of it because we passed NULL for the response code */
7105 /* error was posted */
7106 res = GST_RTSP_ERROR;
7111 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7112 ("Server does not support SDP, got %s.", respcont));
7113 res = GST_RTSP_ERROR;
7118 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7119 ("Server can not provide an SDP."));
7120 res = GST_RTSP_ERROR;
7125 if (src->conninfo.connection) {
7126 GST_DEBUG_OBJECT (src, "free connection");
7127 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7129 gst_rtsp_message_unset (&request);
7130 gst_rtsp_message_unset (&response);
7135 static GstRTSPResult
7136 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
7141 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
7143 if (src->sdp == NULL) {
7144 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
7148 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
7153 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
7160 GST_WARNING_OBJECT (src, "can't get sdp");
7161 src->open_error = TRUE;
7166 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
7167 src->open_error = TRUE;
7172 static GstRTSPResult
7173 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
7175 GstRTSPMessage request = { 0 };
7176 GstRTSPMessage response = { 0 };
7177 GstRTSPResult res = GST_RTSP_OK;
7179 const gchar *control;
7181 GST_DEBUG_OBJECT (src, "TEARDOWN...");
7183 gst_rtspsrc_set_state (src, GST_STATE_READY);
7185 if (src->state < GST_RTSP_STATE_READY) {
7186 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
7193 /* construct a control url */
7194 control = get_aggregate_control (src);
7196 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
7199 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7200 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7201 const gchar *setup_url;
7202 GstRTSPConnInfo *info;
7204 /* try aggregate control first but do non-aggregate control otherwise */
7206 setup_url = control;
7207 else if ((setup_url = stream->conninfo.location) == NULL)
7210 if (src->conninfo.connection) {
7211 info = &src->conninfo;
7212 } else if (stream->conninfo.connection) {
7213 info = &stream->conninfo;
7217 if (!info->connected)
7222 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
7224 goto create_request_failed;
7227 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
7230 gst_rtspsrc_send (src, info->connection, &request, &response,
7234 /* FIXME, parse result? */
7235 gst_rtsp_message_unset (&request);
7236 gst_rtsp_message_unset (&response);
7239 /* early exit when we did aggregate control */
7245 /* close connections */
7246 GST_DEBUG_OBJECT (src, "closing connection...");
7247 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7248 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7249 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7250 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
7254 gst_rtspsrc_cleanup (src);
7256 src->state = GST_RTSP_STATE_INVALID;
7259 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
7264 create_request_failed:
7266 gchar *str = gst_rtsp_strresult (res);
7268 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7269 ("Could not create request. (%s)", str));
7275 gchar *str = gst_rtsp_strresult (res);
7277 gst_rtsp_message_unset (&request);
7278 if (res != GST_RTSP_EINTR) {
7279 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7280 ("Could not send message. (%s)", str));
7282 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
7289 GST_DEBUG_OBJECT (src,
7290 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
7295 /* RTP-Info is of the format:
7297 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
7299 * rtptime corresponds to the timestamp for the NPT time given in the header
7300 * seqbase corresponds to the next sequence number we received. This number
7301 * indicates the first seqnum after the seek and should be used to discard
7302 * packets that are from before the seek.
7305 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
7310 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
7312 infos = g_strsplit (rtpinfo, ",", 0);
7313 for (i = 0; infos[i]; i++) {
7315 GstRTSPStream *stream;
7319 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
7321 /* init values, types of seqbase and timebase are bigger than needed so we
7322 * can store -1 as uninitialized values */
7327 /* parse url, find stream for url.
7328 * parse seq and rtptime. The seq number should be configured in the rtp
7329 * depayloader or session manager to detect gaps. Same for the rtptime, it
7330 * should be used to create an initial time newsegment. */
7331 fields = g_strsplit (infos[i], ";", 0);
7332 for (j = 0; fields[j]; j++) {
7333 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
7334 /* remove leading whitespace */
7335 fields[j] = g_strchug (fields[j]);
7336 if (g_str_has_prefix (fields[j], "url=")) {
7337 /* get the url and the stream */
7339 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
7340 } else if (g_str_has_prefix (fields[j], "seq=")) {
7341 seqbase = atoi (fields[j] + 4);
7342 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
7343 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
7346 g_strfreev (fields);
7347 /* now we need to store the values for the caps of the stream */
7348 if (stream != NULL) {
7349 GST_DEBUG_OBJECT (src,
7350 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
7351 stream, seqbase, timebase);
7353 /* we have a stream, configure detected params */
7354 stream->seqbase = seqbase;
7355 stream->timebase = timebase;
7364 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
7369 interval = strtoul (rtcp, NULL, 10);
7370 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
7375 interval *= GST_MSECOND;
7377 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7378 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7380 /* already (optionally) retrieved this when configuring manager */
7381 if (stream->session) {
7382 GObject *rtpsession = stream->session;
7384 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
7386 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
7390 /* now it happens that (Xenon) server sending this may also provide bogus
7391 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
7392 * and just use RTP-Info to sync */
7394 GObjectClass *klass;
7396 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
7397 if (g_object_class_find_property (klass, "rtcp-sync")) {
7398 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
7399 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
7405 gst_rtspsrc_get_float (const gchar * dstr)
7407 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7409 /* canonicalise floating point string so we can handle float strings
7410 * in the form "24.930" or "24,930" irrespective of the current locale */
7411 g_strlcpy (s, dstr, sizeof (s));
7412 g_strdelimit (s, ",", '.');
7413 return g_ascii_strtod (s, NULL);
7417 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
7419 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7421 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
7422 g_strlcpy (val_str, "now", sizeof (val_str));
7424 if (segment->position == 0) {
7425 g_strlcpy (val_str, "0", sizeof (val_str));
7427 g_ascii_dtostr (val_str, sizeof (val_str),
7428 ((gdouble) segment->position) / GST_SECOND);
7431 return g_strdup_printf ("npt=%s-", val_str);
7435 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
7439 stream->timebase = -1;
7440 stream->seqbase = -1;
7442 len = stream->ptmap->len;
7443 for (i = 0; i < len; i++) {
7444 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
7447 if (item->caps == NULL)
7450 item->caps = gst_caps_make_writable (item->caps);
7451 s = gst_caps_get_structure (item->caps, 0);
7452 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
7456 static GstRTSPResult
7457 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
7459 GstRTSPResult res = GST_RTSP_OK;
7461 if (src->state < GST_RTSP_STATE_READY) {
7462 res = GST_RTSP_ERROR;
7463 if (src->open_error) {
7464 GST_DEBUG_OBJECT (src, "the stream was in error");
7468 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
7470 if ((res = gst_rtspsrc_open (src, async)) < 0) {
7471 GST_DEBUG_OBJECT (src, "failed to open stream");
7480 static GstRTSPResult
7481 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
7483 GstRTSPMessage request = { 0 };
7484 GstRTSPMessage response = { 0 };
7485 GstRTSPResult res = GST_RTSP_OK;
7489 const gchar *control;
7491 GST_DEBUG_OBJECT (src, "PLAY...");
7493 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7496 if (!(src->methods & GST_RTSP_PLAY))
7499 if (src->state == GST_RTSP_STATE_PLAYING)
7502 if (!src->conninfo.connection || !src->conninfo.connected)
7505 /* send some dummy packets before we activate the receive in the
7507 gst_rtspsrc_send_dummy_packets (src);
7509 /* require new SR packets */
7511 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
7513 /* construct a control url */
7514 control = get_aggregate_control (src);
7516 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7517 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7518 const gchar *setup_url;
7519 GstRTSPConnection *conn;
7521 /* try aggregate control first but do non-aggregate control otherwise */
7523 setup_url = control;
7524 else if ((setup_url = stream->conninfo.location) == NULL)
7527 if (src->conninfo.connection) {
7528 conn = src->conninfo.connection;
7529 } else if (stream->conninfo.connection) {
7530 conn = stream->conninfo.connection;
7536 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
7538 goto create_request_failed;
7540 if (src->need_range) {
7541 hval = gen_range_header (src, segment);
7543 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
7545 /* store the newsegment event so it can be sent from the streaming thread. */
7546 if (src->start_segment)
7547 gst_event_unref (src->start_segment);
7548 src->start_segment = gst_event_new_segment (segment);
7551 if (segment->rate != 1.0) {
7552 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
7554 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
7556 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
7558 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
7562 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
7564 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7567 /* seek may have silently failed as it is not supported */
7568 if (!(src->methods & GST_RTSP_PLAY)) {
7569 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
7570 /* obviously it is supported as we made it here */
7571 src->methods |= GST_RTSP_PLAY;
7572 src->seekable = FALSE;
7573 /* but there is nothing to parse in the response,
7574 * so convey we have no idea and not to expect anything particular */
7575 clear_rtp_base (src, stream);
7579 /* need to do for all streams */
7580 for (run = src->streams; run; run = g_list_next (run))
7581 clear_rtp_base (src, (GstRTSPStream *) run->data);
7583 /* NOTE the above also disables npt based eos detection */
7584 /* and below forces position to 0,
7585 * which is visible feedback we lost the plot */
7586 segment->start = segment->position = src->last_pos;
7589 gst_rtsp_message_unset (&request);
7591 /* parse RTP npt field. This is the current position in the stream (Normal
7592 * Play Time) and should be put in the NEWSEGMENT position field. */
7593 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
7595 gst_rtspsrc_parse_range (src, hval, segment);
7597 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
7598 segment->rate = 1.0;
7600 /* parse Speed header. This is the intended playback rate of the stream
7601 * and should be put in the NEWSEGMENT rate field. */
7602 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
7603 0) == GST_RTSP_OK) {
7604 segment->rate = gst_rtspsrc_get_float (hval);
7605 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
7606 &hval, 0) == GST_RTSP_OK) {
7607 segment->rate = gst_rtspsrc_get_float (hval);
7610 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
7611 * for the RTP packets. If this is not present, we assume all starts from 0...
7612 * This is info for the RTP session manager that we pass to it in caps. */
7614 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
7615 &hval, hval_idx++) == GST_RTSP_OK)
7616 gst_rtspsrc_parse_rtpinfo (src, hval);
7618 /* some servers indicate RTCP parameters in PLAY response,
7619 * rather than properly in SDP */
7620 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
7621 &hval, 0) == GST_RTSP_OK)
7622 gst_rtspsrc_handle_rtcp_interval (src, hval);
7624 gst_rtsp_message_unset (&response);
7626 /* early exit when we did aggregate control */
7630 /* configure the caps of the streams after we parsed all headers. Only reset
7631 * the manager object when we set a new Range header (we did a seek) */
7632 gst_rtspsrc_configure_caps (src, segment, src->need_range);
7634 /* set to PLAYING after we have configured the caps, otherwise we
7635 * might end up calling request_key (with SRTP) while caps are still
7636 * being configured. */
7637 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
7639 /* set again when needed */
7640 src->need_range = FALSE;
7642 src->running = TRUE;
7643 src->base_time = -1;
7644 src->state = GST_RTSP_STATE_PLAYING;
7647 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
7648 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7649 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7650 stream->discont = TRUE;
7655 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
7662 GST_DEBUG_OBJECT (src, "failed to open stream");
7667 GST_DEBUG_OBJECT (src, "PLAY is not supported");
7672 GST_DEBUG_OBJECT (src, "we were already PLAYING");
7675 create_request_failed:
7677 gchar *str = gst_rtsp_strresult (res);
7679 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7680 ("Could not create request. (%s)", str));
7686 gchar *str = gst_rtsp_strresult (res);
7688 gst_rtsp_message_unset (&request);
7689 if (res != GST_RTSP_EINTR) {
7690 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7691 ("Could not send message. (%s)", str));
7693 GST_WARNING_OBJECT (src, "PLAY interrupted");
7700 static GstRTSPResult
7701 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
7703 GstRTSPResult res = GST_RTSP_OK;
7704 GstRTSPMessage request = { 0 };
7705 GstRTSPMessage response = { 0 };
7707 const gchar *control;
7709 GST_DEBUG_OBJECT (src, "PAUSE...");
7711 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7714 if (!(src->methods & GST_RTSP_PAUSE))
7717 if (src->state == GST_RTSP_STATE_READY)
7720 if (!src->conninfo.connection || !src->conninfo.connected)
7723 /* construct a control url */
7724 control = get_aggregate_control (src);
7726 /* loop over the streams. We might exit the loop early when we could do an
7727 * aggregate control */
7728 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7729 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7730 GstRTSPConnection *conn;
7731 const gchar *setup_url;
7733 /* try aggregate control first but do non-aggregate control otherwise */
7735 setup_url = control;
7736 else if ((setup_url = stream->conninfo.location) == NULL)
7739 if (src->conninfo.connection) {
7740 conn = src->conninfo.connection;
7741 } else if (stream->conninfo.connection) {
7742 conn = stream->conninfo.connection;
7748 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7749 ("Sending PAUSE request"));
7752 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
7754 goto create_request_failed;
7756 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7759 gst_rtsp_message_unset (&request);
7760 gst_rtsp_message_unset (&response);
7762 /* exit early when we did agregate control */
7767 /* change element states now */
7768 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7771 src->state = GST_RTSP_STATE_READY;
7775 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7782 GST_DEBUG_OBJECT (src, "failed to open stream");
7787 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7792 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7795 create_request_failed:
7797 gchar *str = gst_rtsp_strresult (res);
7799 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7800 ("Could not create request. (%s)", str));
7806 gchar *str = gst_rtsp_strresult (res);
7808 gst_rtsp_message_unset (&request);
7809 if (res != GST_RTSP_EINTR) {
7810 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7811 ("Could not send message. (%s)", str));
7813 GST_WARNING_OBJECT (src, "PAUSE interrupted");
7821 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
7823 GstRTSPSrc *rtspsrc;
7825 rtspsrc = GST_RTSPSRC (bin);
7827 switch (GST_MESSAGE_TYPE (message)) {
7828 case GST_MESSAGE_EOS:
7829 gst_message_unref (message);
7831 case GST_MESSAGE_ELEMENT:
7833 const GstStructure *s = gst_message_get_structure (message);
7835 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
7836 gboolean ignore_timeout;
7838 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
7840 GST_OBJECT_LOCK (rtspsrc);
7841 ignore_timeout = rtspsrc->ignore_timeout;
7842 rtspsrc->ignore_timeout = TRUE;
7843 GST_OBJECT_UNLOCK (rtspsrc);
7845 /* we only act on the first udp timeout message, others are irrelevant
7846 * and can be ignored. */
7847 if (!ignore_timeout)
7848 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
7850 gst_message_unref (message);
7853 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7856 case GST_MESSAGE_ERROR:
7859 GstRTSPStream *stream;
7862 udpsrc = GST_MESSAGE_SRC (message);
7864 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7865 GST_ELEMENT_NAME (udpsrc));
7867 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7871 /* we ignore the RTCP udpsrc */
7872 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7875 /* if we get error messages from the udp sources, that's not a problem as
7876 * long as not all of them error out. We also don't really know what the
7877 * problem is, the message does not give enough detail... */
7878 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7879 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7880 if (ret != GST_FLOW_OK)
7884 gst_message_unref (message);
7888 /* fatal but not our message, forward */
7889 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7894 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7900 /* the thread where everything happens */
7902 gst_rtspsrc_thread (GstRTSPSrc * src)
7906 GST_OBJECT_LOCK (src);
7907 cmd = src->pending_cmd;
7908 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7909 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7910 src->pending_cmd = CMD_LOOP;
7912 src->pending_cmd = CMD_WAIT;
7913 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
7915 /* we got the message command, so ensure communication is possible again */
7916 gst_rtspsrc_connection_flush (src, FALSE);
7918 src->busy_cmd = cmd;
7919 GST_OBJECT_UNLOCK (src);
7923 gst_rtspsrc_open (src, TRUE);
7926 gst_rtspsrc_play (src, &src->segment, TRUE);
7929 gst_rtspsrc_pause (src, TRUE);
7932 gst_rtspsrc_close (src, TRUE, FALSE);
7935 gst_rtspsrc_loop (src);
7938 gst_rtspsrc_reconnect (src, FALSE);
7944 GST_OBJECT_LOCK (src);
7945 /* and go back to sleep */
7946 if (src->pending_cmd == CMD_WAIT) {
7948 gst_task_pause (src->task);
7951 src->busy_cmd = CMD_WAIT;
7952 GST_OBJECT_UNLOCK (src);
7956 gst_rtspsrc_start (GstRTSPSrc * src)
7958 GST_DEBUG_OBJECT (src, "starting");
7960 GST_OBJECT_LOCK (src);
7962 src->pending_cmd = CMD_WAIT;
7964 if (src->task == NULL) {
7965 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7966 if (src->task == NULL)
7969 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7971 GST_OBJECT_UNLOCK (src);
7978 GST_OBJECT_UNLOCK (src);
7979 GST_ERROR_OBJECT (src, "failed to create task");
7985 gst_rtspsrc_stop (GstRTSPSrc * src)
7989 GST_DEBUG_OBJECT (src, "stopping");
7991 /* also cancels pending task */
7992 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7994 GST_OBJECT_LOCK (src);
7995 if ((task = src->task)) {
7997 GST_OBJECT_UNLOCK (src);
7999 gst_task_stop (task);
8001 /* make sure it is not running */
8002 GST_RTSP_STREAM_LOCK (src);
8003 GST_RTSP_STREAM_UNLOCK (src);
8005 /* now wait for the task to finish */
8006 gst_task_join (task);
8008 /* and free the task */
8009 gst_object_unref (GST_OBJECT (task));
8011 GST_OBJECT_LOCK (src);
8013 GST_OBJECT_UNLOCK (src);
8015 /* ensure synchronously all is closed and clean */
8016 gst_rtspsrc_close (src, FALSE, TRUE);
8021 static GstStateChangeReturn
8022 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
8024 GstRTSPSrc *rtspsrc;
8025 GstStateChangeReturn ret;
8027 rtspsrc = GST_RTSPSRC (element);
8029 switch (transition) {
8030 case GST_STATE_CHANGE_NULL_TO_READY:
8031 if (!gst_rtspsrc_start (rtspsrc))
8034 case GST_STATE_CHANGE_READY_TO_PAUSED:
8035 /* init some state */
8036 rtspsrc->cur_protocols = rtspsrc->protocols;
8037 /* first attempt, don't ignore timeouts */
8038 rtspsrc->ignore_timeout = FALSE;
8039 rtspsrc->open_error = FALSE;
8040 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
8042 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8043 set_manager_buffer_mode (rtspsrc);
8045 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8046 /* unblock the tcp tasks and make the loop waiting */
8047 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
8048 /* make sure it is waiting before we send PAUSE or PLAY below */
8049 GST_RTSP_STREAM_LOCK (rtspsrc);
8050 GST_RTSP_STREAM_UNLOCK (rtspsrc);
8053 case GST_STATE_CHANGE_PAUSED_TO_READY:
8059 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
8060 if (ret == GST_STATE_CHANGE_FAILURE)
8063 switch (transition) {
8064 case GST_STATE_CHANGE_NULL_TO_READY:
8065 ret = GST_STATE_CHANGE_SUCCESS;
8067 case GST_STATE_CHANGE_READY_TO_PAUSED:
8068 ret = GST_STATE_CHANGE_NO_PREROLL;
8070 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8071 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
8072 ret = GST_STATE_CHANGE_SUCCESS;
8074 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8075 /* send pause request and keep the idle task around */
8076 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
8077 ret = GST_STATE_CHANGE_NO_PREROLL;
8079 case GST_STATE_CHANGE_PAUSED_TO_READY:
8080 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
8081 ret = GST_STATE_CHANGE_SUCCESS;
8083 case GST_STATE_CHANGE_READY_TO_NULL:
8084 gst_rtspsrc_stop (rtspsrc);
8085 ret = GST_STATE_CHANGE_SUCCESS;
8096 GST_DEBUG_OBJECT (rtspsrc, "start failed");
8097 return GST_STATE_CHANGE_FAILURE;
8102 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
8105 GstRTSPSrc *rtspsrc;
8107 rtspsrc = GST_RTSPSRC (element);
8109 if (GST_EVENT_IS_DOWNSTREAM (event)) {
8110 res = gst_rtspsrc_push_event (rtspsrc, event);
8112 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
8119 /*** GSTURIHANDLER INTERFACE *************************************************/
8122 gst_rtspsrc_uri_get_type (GType type)
8127 static const gchar *const *
8128 gst_rtspsrc_uri_get_protocols (GType type)
8130 static const gchar *protocols[] =
8131 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
8132 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
8139 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
8141 GstRTSPSrc *src = GST_RTSPSRC (handler);
8143 /* FIXME: make thread-safe */
8144 return g_strdup (src->conninfo.location);
8148 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
8154 GstRTSPUrl *newurl = NULL;
8155 GstSDPMessage *sdp = NULL;
8157 src = GST_RTSPSRC (handler);
8159 /* same URI, we're fine */
8160 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
8163 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
8164 sres = gst_sdp_message_new (&sdp);
8168 GST_DEBUG_OBJECT (src, "parsing SDP message");
8169 sres = gst_sdp_message_parse_uri (uri, sdp);
8174 GST_DEBUG_OBJECT (src, "parsing URI");
8175 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
8179 /* if worked, free previous and store new url object along with the original
8181 GST_DEBUG_OBJECT (src, "configuring URI");
8182 g_free (src->conninfo.location);
8183 src->conninfo.location = g_strdup (uri);
8184 gst_rtsp_url_free (src->conninfo.url);
8185 src->conninfo.url = newurl;
8186 g_free (src->conninfo.url_str);
8188 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
8190 src->conninfo.url_str = NULL;
8193 gst_sdp_message_free (src->sdp);
8195 src->from_sdp = sdp != NULL;
8197 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
8198 GST_DEBUG_OBJECT (src, "request uri is: %s",
8199 GST_STR_NULL (src->conninfo.url_str));
8206 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
8211 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
8212 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8213 "Could not create SDP");
8218 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
8219 GST_STR_NULL (uri));
8220 gst_sdp_message_free (sdp);
8221 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8227 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
8228 GST_STR_NULL (uri), res);
8229 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8230 "Invalid RTSP URI");
8236 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
8238 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
8240 iface->get_type = gst_rtspsrc_uri_get_type;
8241 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
8242 iface->get_uri = gst_rtspsrc_uri_get_uri;
8243 iface->set_uri = gst_rtspsrc_uri_set_uri;