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);
1665 src->need_segment = FALSE;
1667 if (src->provided_clock) {
1668 gst_object_unref (src->provided_clock);
1669 src->provided_clock = NULL;
1673 #define PARSE_INT(p, del, res) \
1676 p = strstr (p, del); \
1686 #define PARSE_STRING(p, del, res) \
1689 p = strstr (p, del); \
1701 #define SKIP_SPACES(p) \
1702 while (*p && g_ascii_isspace (*p)) \
1707 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1710 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1711 gint * rate, gchar ** params)
1715 p = (gchar *) rtpmap;
1717 PARSE_INT (p, " ", *payload);
1725 PARSE_STRING (p, "/", *name);
1726 if (*name == NULL) {
1727 GST_DEBUG ("no rate, name %s", p);
1728 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1729 * streams seem to omit the rate. */
1736 p = strstr (p, "/");
1754 parse_keymgmt (const gchar * keymgmt, GstCaps * caps)
1756 gboolean res = FALSE;
1760 GstMIKEYMessage *msg;
1761 const GstMIKEYPayload *payload;
1762 const gchar *srtp_cipher;
1763 const gchar *srtp_auth;
1765 p = (gchar *) keymgmt;
1771 PARSE_STRING (p, " ", kmpid);
1772 if (!g_str_equal (kmpid, "mikey"))
1775 data = g_base64_decode (p, &size);
1779 msg = gst_mikey_message_new_from_data (data, size, NULL, NULL);
1784 srtp_cipher = "aes-128-icm";
1785 srtp_auth = "hmac-sha1-80";
1787 /* check the Security policy if any */
1788 if ((payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, 0))) {
1789 GstMIKEYPayloadSP *p = (GstMIKEYPayloadSP *) payload;
1792 if (p->proto != GST_MIKEY_SEC_PROTO_SRTP)
1795 len = gst_mikey_payload_sp_get_n_params (payload);
1796 for (i = 0; i < len; i++) {
1797 const GstMIKEYPayloadSPParam *param =
1798 gst_mikey_payload_sp_get_param (payload, i);
1800 switch (param->type) {
1801 case GST_MIKEY_SP_SRTP_ENC_ALG:
1802 switch (param->val[0]) {
1804 srtp_cipher = "null";
1808 srtp_cipher = "aes-128-icm";
1814 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
1815 switch (param->val[0]) {
1816 case AES_128_KEY_LEN:
1817 srtp_cipher = "aes-128-icm";
1819 case AES_256_KEY_LEN:
1820 srtp_cipher = "aes-256-icm";
1826 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1827 switch (param->val[0]) {
1833 srtp_auth = "hmac-sha1-80";
1839 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
1840 switch (param->val[0]) {
1841 case HMAC_32_KEY_LEN:
1842 srtp_auth = "hmac-sha1-32";
1844 case HMAC_80_KEY_LEN:
1845 srtp_auth = "hmac-sha1-80";
1851 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1853 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1861 if (!(payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_KEMAC, 0)))
1864 GstMIKEYPayloadKEMAC *p = (GstMIKEYPayloadKEMAC *) payload;
1865 const GstMIKEYPayload *sub;
1866 GstMIKEYPayloadKeyData *pkd;
1869 if (p->enc_alg != GST_MIKEY_ENC_NULL || p->mac_alg != GST_MIKEY_MAC_NULL)
1872 if (!(sub = gst_mikey_payload_kemac_get_sub (payload, 0)))
1875 if (sub->type != GST_MIKEY_PT_KEY_DATA)
1878 pkd = (GstMIKEYPayloadKeyData *) sub;
1880 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
1882 gst_caps_set_simple (caps, "srtp-key", GST_TYPE_BUFFER, buf, NULL);
1885 gst_caps_set_simple (caps,
1886 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1887 "srtp-auth", G_TYPE_STRING, srtp_auth,
1888 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1889 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1893 gst_mikey_message_unref (msg);
1899 * Mapping SDP attributes to caps
1901 * prepend 'a-' to IANA registered sdp attributes names
1902 * (ie: not prefixed with 'x-') in order to avoid
1903 * collision with gstreamer standard caps properties names
1906 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1908 if (attributes->len > 0) {
1912 s = gst_caps_get_structure (caps, 0);
1914 for (i = 0; i < attributes->len; i++) {
1915 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1916 gchar *tofree, *key;
1920 /* skip some of the attribute we already handle */
1921 if (!strcmp (key, "fmtp"))
1923 if (!strcmp (key, "rtpmap"))
1925 if (!strcmp (key, "control"))
1927 if (!strcmp (key, "range"))
1929 if (!strcmp (key, "framesize"))
1931 if (g_str_equal (key, "key-mgmt")) {
1932 parse_keymgmt (attr->value, caps);
1936 /* string must be valid UTF8 */
1937 if (!g_utf8_validate (attr->value, -1, NULL))
1940 if (!g_str_has_prefix (key, "x-"))
1941 tofree = key = g_strdup_printf ("a-%s", key);
1945 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1946 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1952 static const gchar *
1953 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
1962 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
1965 if (sscanf (attr, "%d ", &val) != 1)
1975 * Mapping of caps to and from SDP fields:
1977 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1978 * a=framesize:<payload> <width>-<height>
1979 * a=fmtp:<payload> <param>[=<value>];...
1982 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1985 const gchar *rtpmap;
1987 const gchar *framesize;
1990 gchar *params = NULL;
1996 /* get and parse rtpmap */
1997 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
2000 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
2002 g_warning ("error parsing rtpmap, ignoring");
2006 /* dynamic payloads need rtpmap or we fail */
2007 if (rtpmap == NULL && pt >= 96)
2010 /* check if we have a rate, if not, we need to look up the rate from the
2011 * default rates based on the payload types. */
2013 const GstRTPPayloadInfo *info;
2015 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
2016 /* dynamic types, use media and encoding_name */
2017 tmp = g_ascii_strdown (media->media, -1);
2018 info = gst_rtp_payload_info_for_name (tmp, name);
2021 /* static types, use payload type */
2022 info = gst_rtp_payload_info_for_pt (pt);
2026 if ((rate = info->clock_rate) == 0)
2029 /* we fail if we cannot find one */
2034 tmp = g_ascii_strdown (media->media, -1);
2035 caps = gst_caps_new_simple ("application/x-unknown",
2036 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
2038 s = gst_caps_get_structure (caps, 0);
2040 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
2042 /* encoding name must be upper case */
2044 tmp = g_ascii_strup (name, -1);
2045 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
2049 /* params must be lower case */
2050 if (params != NULL) {
2051 tmp = g_ascii_strdown (params, -1);
2052 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
2056 /* parse optional fmtp: field */
2057 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
2063 /* p is now of the format <payload> <param>[=<value>];... */
2064 PARSE_INT (p, " ", payload);
2065 if (payload != -1 && payload == pt) {
2069 /* <param>[=<value>] are separated with ';' */
2070 pairs = g_strsplit (p, ";", 0);
2071 for (i = 0; pairs[i]; i++) {
2073 const gchar *val, *key;
2075 /* the key may not have a '=', the value can have other '='s */
2076 valpos = strstr (pairs[i], "=");
2078 /* we have a '=' and thus a value, remove the '=' with \0 */
2080 /* value is everything between '=' and ';'. We split the pairs at ;
2081 * boundaries so we can take the remainder of the value. Some servers
2082 * put spaces around the value which we strip off here. Alternatively
2083 * we could strip those spaces in the depayloaders should these spaces
2084 * actually carry any meaning in the future. */
2085 val = g_strstrip (valpos + 1);
2087 /* simple <param>;.. is translated into <param>=1;... */
2090 /* strip the key of spaces, convert key to lowercase but not the value. */
2091 key = g_strstrip (pairs[i]);
2092 if (strlen (key) > 1) {
2093 tmp = g_ascii_strdown (key, -1);
2094 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
2102 /* parse framesize: field */
2103 if ((framesize = gst_sdp_media_get_attribute_val (media, "framesize"))) {
2106 /* p is now of the format <payload> <width>-<height> */
2107 p = (gchar *) framesize;
2109 PARSE_INT (p, " ", payload);
2110 if (payload != -1 && payload == pt) {
2111 gst_structure_set (s, "a-framesize", G_TYPE_STRING, p, NULL);
2119 g_warning ("rtpmap type not given for dynamic payload %d", pt);
2124 g_warning ("rate unknown for payload type %d", pt);
2130 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
2131 gint * rtpport, gint * rtcpport)
2134 GstStateChangeReturn ret;
2135 GstElement *udpsrc0, *udpsrc1;
2136 gint tmp_rtp, tmp_rtcp;
2140 src = stream->parent;
2146 /* Start at next port */
2147 tmp_rtp = src->next_port_num;
2149 if (stream->is_ipv6)
2150 host = "udp://[::0]";
2152 host = "udp://0.0.0.0";
2154 /* try to allocate 2 UDP ports, the RTP port should be an even
2155 * number and the RTCP port should be the next (uneven) port */
2158 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
2159 tmp_rtp >= src->client_port_range.max)
2162 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2163 if (udpsrc0 == NULL)
2164 goto no_udp_protocol;
2165 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
2167 if (src->udp_buffer_size != 0)
2168 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
2171 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
2172 if (ret == GST_STATE_CHANGE_FAILURE) {
2174 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
2177 if (++count > src->retry)
2180 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2181 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2182 gst_object_unref (udpsrc0);
2185 GST_DEBUG_OBJECT (src, "retry %d", count);
2188 goto no_udp_protocol;
2191 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
2192 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
2194 /* check if port is even */
2195 if ((tmp_rtp & 0x01) != 0) {
2196 /* port not even, close and allocate another */
2197 if (++count > src->retry)
2200 GST_DEBUG_OBJECT (src, "RTP port not even");
2202 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2203 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2204 gst_object_unref (udpsrc0);
2207 GST_DEBUG_OBJECT (src, "retry %d", count);
2212 /* allocate port+1 for RTCP now */
2213 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2214 if (udpsrc1 == NULL)
2215 goto no_udp_rtcp_protocol;
2218 tmp_rtcp = tmp_rtp + 1;
2219 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
2222 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
2224 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
2225 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
2226 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
2227 if (ret == GST_STATE_CHANGE_FAILURE) {
2228 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
2230 if (++count > src->retry)
2233 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2234 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2235 gst_object_unref (udpsrc0);
2238 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2239 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2240 gst_object_unref (udpsrc1);
2244 GST_DEBUG_OBJECT (src, "retry %d", count);
2248 /* all fine, do port check */
2249 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2250 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2252 /* this should not happen... */
2253 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2256 /* we keep these elements, we configure all in configure_transport when the
2257 * server told us to really use the UDP ports. */
2258 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2259 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2260 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2261 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2263 /* keep track of next available port number when we have a range
2265 if (src->next_port_num != 0)
2266 src->next_port_num = tmp_rtcp + 1;
2273 GST_DEBUG_OBJECT (src, "could not get UDP source");
2278 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2282 no_udp_rtcp_protocol:
2284 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2289 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2290 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2296 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2297 gst_object_unref (udpsrc0);
2300 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2301 gst_object_unref (udpsrc1);
2308 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2313 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2315 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2316 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2319 for (i = 0; i < 2; i++) {
2320 if (stream->udpsrc[i])
2321 gst_element_set_state (stream->udpsrc[i], state);
2327 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2334 event = gst_event_new_flush_start ();
2335 GST_DEBUG_OBJECT (src, "start flush");
2337 state = GST_STATE_PAUSED;
2339 event = gst_event_new_flush_stop (FALSE);
2340 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2343 state = GST_STATE_PLAYING;
2345 state = GST_STATE_PAUSED;
2347 gst_rtspsrc_push_event (src, event);
2348 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2349 gst_rtspsrc_set_state (src, state);
2352 static GstRTSPResult
2353 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2354 GstRTSPMessage * message, GTimeVal * timeout)
2359 ret = gst_rtsp_connection_send (conn, message, timeout);
2361 ret = GST_RTSP_ERROR;
2366 static GstRTSPResult
2367 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2368 GstRTSPMessage * message, GTimeVal * timeout)
2373 ret = gst_rtsp_connection_receive (conn, message, timeout);
2375 ret = GST_RTSP_ERROR;
2381 gst_rtspsrc_get_position (GstRTSPSrc * src)
2386 query = gst_query_new_position (GST_FORMAT_TIME);
2387 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2388 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2389 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2393 if (stream->srcpad) {
2394 if (gst_pad_query (stream->srcpad, query)) {
2395 gst_query_parse_position (query, &fmt, &pos);
2396 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2397 GST_TIME_ARGS (pos));
2398 src->last_pos = pos;
2408 gst_query_unref (query);
2412 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2417 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2419 gboolean flush, skip;
2422 GstSegment seeksegment = { 0, };
2426 GST_DEBUG_OBJECT (src, "doing seek with event");
2428 gst_event_parse_seek (event, &rate, &format, &flags,
2429 &cur_type, &cur, &stop_type, &stop);
2431 /* no negative rates yet */
2435 /* we need TIME format */
2436 if (format != src->segment.format)
2439 GST_DEBUG_OBJECT (src, "doing seek without event");
2441 cur_type = GST_SEEK_TYPE_SET;
2442 stop_type = GST_SEEK_TYPE_SET;
2445 /* get flush flag */
2446 flush = flags & GST_SEEK_FLAG_FLUSH;
2447 skip = flags & GST_SEEK_FLAG_SKIP;
2449 /* now we need to make sure the streaming thread is stopped. We do this by
2450 * either sending a FLUSH_START event downstream which will cause the
2451 * streaming thread to stop with a WRONG_STATE.
2452 * For a non-flushing seek we simply pause the task, which will happen as soon
2453 * as it completes one iteration (and thus might block when the sink is
2454 * blocking in preroll). */
2456 GST_DEBUG_OBJECT (src, "starting flush");
2457 gst_rtspsrc_flush (src, TRUE, FALSE);
2460 gst_task_pause (src->task);
2464 /* we should now be able to grab the streaming thread because we stopped it
2465 * with the above flush/pause code */
2466 GST_RTSP_STREAM_LOCK (src);
2468 GST_DEBUG_OBJECT (src, "stopped streaming");
2470 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2471 gst_rtspsrc_connection_flush (src, FALSE);
2473 /* copy segment, we need this because we still need the old
2474 * segment when we close the current segment. */
2475 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2477 /* configure the seek parameters in the seeksegment. We will then have the
2478 * right values in the segment to perform the seek */
2480 GST_DEBUG_OBJECT (src, "configuring seek");
2481 gst_segment_do_seek (&seeksegment, rate, format, flags,
2482 cur_type, cur, stop_type, stop, &update);
2485 /* figure out the last position we need to play. If it's configured (stop !=
2486 * -1), use that, else we play until the total duration of the file */
2487 if ((stop = seeksegment.stop) == -1)
2488 stop = seeksegment.duration;
2490 playing = (src->state == GST_RTSP_STATE_PLAYING);
2492 /* if we were playing, pause first */
2494 /* obtain current position in case seek fails */
2495 gst_rtspsrc_get_position (src);
2496 gst_rtspsrc_pause (src, FALSE);
2500 src->state = GST_RTSP_STATE_SEEKING;
2502 /* PLAY will add the range header now. */
2503 src->need_range = TRUE;
2505 /* and continue playing */
2507 gst_rtspsrc_play (src, &seeksegment, FALSE);
2509 /* prepare for streaming again */
2511 /* if we started flush, we stop now */
2512 GST_DEBUG_OBJECT (src, "stopping flush");
2513 gst_rtspsrc_flush (src, FALSE, playing);
2516 /* now we did the seek and can activate the new segment values */
2517 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2519 /* if we're doing a segment seek, post a SEGMENT_START message */
2520 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2521 gst_element_post_message (GST_ELEMENT_CAST (src),
2522 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2523 src->segment.format, src->segment.position));
2526 /* now create the newsegment */
2527 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2528 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2531 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2532 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2533 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2534 stream->discont = TRUE;
2537 GST_RTSP_STREAM_UNLOCK (src);
2544 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2549 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2555 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2559 gboolean res = TRUE;
2562 src = GST_RTSPSRC_CAST (parent);
2564 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2565 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2567 switch (GST_EVENT_TYPE (event)) {
2568 case GST_EVENT_SEEK:
2569 res = gst_rtspsrc_perform_seek (src, event);
2573 case GST_EVENT_NAVIGATION:
2574 case GST_EVENT_LATENCY:
2582 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2583 res = gst_pad_send_event (target, event);
2584 gst_object_unref (target);
2586 gst_event_unref (event);
2589 gst_event_unref (event);
2595 /* this is the final event function we receive on the internal source pad when
2596 * we deal with TCP connections */
2598 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2603 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2605 switch (GST_EVENT_TYPE (event)) {
2606 case GST_EVENT_SEEK:
2608 case GST_EVENT_NAVIGATION:
2609 case GST_EVENT_LATENCY:
2611 gst_event_unref (event);
2618 /* this is the final query function we receive on the internal source pad when
2619 * we deal with TCP connections */
2621 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2625 gboolean res = TRUE;
2627 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2629 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2630 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2632 switch (GST_QUERY_TYPE (query)) {
2633 case GST_QUERY_POSITION:
2638 case GST_QUERY_DURATION:
2642 gst_query_parse_duration (query, &format, NULL);
2645 case GST_FORMAT_TIME:
2646 gst_query_set_duration (query, format, src->segment.duration);
2654 case GST_QUERY_LATENCY:
2656 /* we are live with a min latency of 0 and unlimited max latency, this
2657 * result will be updated by the session manager if there is any. */
2658 gst_query_set_latency (query, TRUE, 0, -1);
2668 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2670 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2674 gboolean res = FALSE;
2676 src = GST_RTSPSRC_CAST (parent);
2678 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2679 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2681 switch (GST_QUERY_TYPE (query)) {
2682 case GST_QUERY_DURATION:
2686 gst_query_parse_duration (query, &format, NULL);
2689 case GST_FORMAT_TIME:
2690 gst_query_set_duration (query, format, src->segment.duration);
2698 case GST_QUERY_SEEKING:
2702 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2703 if (format == GST_FORMAT_TIME) {
2705 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2707 /* seeking without duration is unlikely */
2708 seekable = seekable && src->seekable && src->segment.duration &&
2709 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2711 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0,
2712 src->segment.duration);
2721 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2723 gst_query_set_uri (query, uri);
2731 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2733 /* forward the query to the proxy target pad */
2735 res = gst_pad_query (target, query);
2736 gst_object_unref (target);
2745 /* callback for RTCP messages to be sent to the server when operating in TCP
2747 static GstFlowReturn
2748 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2751 GstRTSPStream *stream;
2752 GstFlowReturn res = GST_FLOW_OK;
2757 GstRTSPMessage message = { 0 };
2758 GstRTSPConnection *conn;
2760 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2761 src = stream->parent;
2763 gst_buffer_map (buffer, &map, GST_MAP_READ);
2767 gst_rtsp_message_init_data (&message, stream->channel[1]);
2769 /* lend the body data to the message */
2770 gst_rtsp_message_take_body (&message, data, size);
2772 if (stream->conninfo.connection)
2773 conn = stream->conninfo.connection;
2775 conn = src->conninfo.connection;
2777 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2778 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2779 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2781 /* and steal it away again because we will free it when unreffing the
2783 gst_rtsp_message_steal_body (&message, &data, &size);
2784 gst_rtsp_message_unset (&message);
2786 gst_buffer_unmap (buffer, &map);
2787 gst_buffer_unref (buffer);
2792 static GstPadProbeReturn
2793 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2795 GstRTSPSrc *src = user_data;
2797 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2798 GST_DEBUG_PAD_NAME (pad));
2800 /* activate the streams */
2801 GST_OBJECT_LOCK (src);
2802 if (!src->need_activate)
2805 src->need_activate = FALSE;
2806 GST_OBJECT_UNLOCK (src);
2808 gst_rtspsrc_activate_streams (src);
2810 return GST_PAD_PROBE_OK;
2814 GST_OBJECT_UNLOCK (src);
2815 return GST_PAD_PROBE_OK;
2820 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
2822 GstPad *gpad = GST_PAD_CAST (user_data);
2824 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
2825 gst_pad_store_sticky_event (gpad, *event);
2830 /* this callback is called when the session manager generated a new src pad with
2831 * payloaded RTP packets. We simply ghost the pad here. */
2833 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2836 GstPadTemplate *template;
2839 GstRTSPStream *stream;
2842 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2844 GST_RTSP_STATE_LOCK (src);
2846 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2847 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2848 goto unknown_stream;
2850 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2852 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2854 goto unknown_stream;
2857 stream->ssrc = ssrc;
2859 /* we'll add it later see below */
2860 stream->added = TRUE;
2862 /* check if we added all streams */
2864 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2865 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2867 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
2868 ostream, ostream->container, ostream->added, ostream->setup);
2870 /* if we find a stream for which we did a setup that is not added, we
2871 * need to wait some more */
2872 if (ostream->setup && !ostream->added) {
2877 GST_RTSP_STATE_UNLOCK (src);
2879 /* create a new pad we will use to stream to */
2880 template = gst_static_pad_template_get (&rtptemplate);
2881 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2882 gst_object_unref (template);
2885 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2886 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2887 gst_pad_set_active (stream->srcpad, TRUE);
2888 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
2889 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2892 GST_DEBUG_OBJECT (src, "We added all streams");
2893 /* when we get here, all stream are added and we can fire the no-more-pads
2895 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2903 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2904 GST_RTSP_STATE_UNLOCK (src);
2911 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
2915 len = stream->ptmap->len;
2916 for (i = 0; i < len; i++) {
2917 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2925 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2927 GstRTSPStream *stream;
2930 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2932 GST_RTSP_STATE_LOCK (src);
2933 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2935 goto unknown_stream;
2937 if ((caps = stream_get_caps_for_pt (stream, pt)))
2938 gst_caps_ref (caps);
2939 GST_RTSP_STATE_UNLOCK (src);
2945 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2946 GST_RTSP_STATE_UNLOCK (src);
2952 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2954 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2960 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2966 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2972 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2974 GstRTSPSrc *src = stream->parent;
2977 g_object_get (source, "ssrc", &ssrc, NULL);
2979 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2980 ssrc, stream->ssrc, stream->id);
2982 if (ssrc == stream->ssrc)
2983 gst_rtspsrc_do_stream_eos (src, stream);
2987 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2989 GstRTSPSrc *src = stream->parent;
2992 g_object_get (source, "ssrc", &ssrc, NULL);
2994 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2995 ssrc, stream->ssrc, stream->id);
2997 if (ssrc == stream->ssrc)
2998 gst_rtspsrc_do_stream_eos (src, stream);
3002 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
3004 GstRTSPStream *stream;
3006 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
3008 /* get stream for session */
3009 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3011 gst_rtspsrc_do_stream_eos (src, stream);
3016 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
3018 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
3023 set_manager_buffer_mode (GstRTSPSrc * src)
3025 GObjectClass *klass;
3027 if (src->manager == NULL)
3030 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3032 if (!g_object_class_find_property (klass, "buffer-mode"))
3035 if (src->buffer_mode != BUFFER_MODE_AUTO) {
3036 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
3041 GST_DEBUG_OBJECT (src,
3042 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
3044 if (src->provided_clock) {
3045 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
3047 if (clock == src->provided_clock) {
3048 GST_DEBUG_OBJECT (src, "selected synced");
3049 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
3052 gst_object_unref (clock);
3057 /* Otherwise fall-through and use another buffer mode */
3059 gst_object_unref (clock);
3062 GST_DEBUG_OBJECT (src, "auto buffering mode");
3063 if (src->use_buffering) {
3064 GST_DEBUG_OBJECT (src, "selected buffer");
3065 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
3067 GST_DEBUG_OBJECT (src, "selected slave");
3068 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
3073 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
3075 GST_DEBUG ("request key %u", ssrc);
3076 return gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
3080 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
3082 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3083 if (stream->id != session)
3086 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3087 stream->profile != GST_RTSP_PROFILE_SAVPF)
3090 if (stream->srtpdec == NULL) {
3093 name = g_strdup_printf ("srtpdec_%u", session);
3094 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
3097 g_signal_connect (stream->srtpdec, "request-key",
3098 (GCallback) request_key, stream);
3100 return gst_object_ref (stream->srtpdec);
3104 request_rtcp_encoder (GstElement * rtpbin, guint session,
3105 GstRTSPStream * stream)
3110 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3111 if (stream->id != session)
3114 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3115 stream->profile != GST_RTSP_PROFILE_SAVPF)
3118 if (stream->srtpenc == NULL) {
3121 name = g_strdup_printf ("srtpenc_%u", session);
3122 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
3125 /* get RTCP crypto parameters from caps */
3126 s = gst_caps_get_structure (stream->srtcpparams, 0);
3130 GType ciphertype, authtype;
3131 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
3133 ciphertype = g_type_from_name ("GstSrtpCipherType");
3134 authtype = g_type_from_name ("GstSrtpAuthType");
3135 g_value_init (&rtcp_cipher, ciphertype);
3136 g_value_init (&rtcp_auth, authtype);
3138 str = gst_structure_get_string (s, "srtcp-cipher");
3139 gst_value_deserialize (&rtcp_cipher, str);
3140 str = gst_structure_get_string (s, "srtcp-auth");
3141 gst_value_deserialize (&rtcp_auth, str);
3142 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
3144 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
3146 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
3148 g_object_set (stream->srtpenc, "key", buf, NULL);
3150 g_value_unset (&rtcp_cipher);
3151 g_value_unset (&rtcp_auth);
3152 gst_buffer_unref (buf);
3155 name = g_strdup_printf ("rtcp_sink_%d", session);
3156 pad = gst_element_get_request_pad (stream->srtpenc, name);
3158 gst_object_unref (pad);
3160 return gst_object_ref (stream->srtpenc);
3164 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
3166 GstElement *rtx, *bin;
3169 GstRTSPStream *stream;
3171 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
3173 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
3177 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
3178 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
3179 bin = gst_bin_new (NULL);
3180 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
3181 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
3182 gst_bin_add (GST_BIN (bin), rtx);
3184 pad = gst_element_get_static_pad (rtx, "src");
3185 name = g_strdup_printf ("src_%u", sessid);
3186 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3188 gst_object_unref (pad);
3190 pad = gst_element_get_static_pad (rtx, "sink");
3191 name = g_strdup_printf ("sink_%u", sessid);
3192 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3194 gst_object_unref (pad);
3200 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
3205 if (transport->trans != GST_RTSP_TRANS_RTP)
3208 signal_id = g_signal_lookup ("request-aux-receiver",
3209 G_OBJECT_TYPE (src->manager));
3210 /* there's already something connected */
3211 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
3212 NULL, NULL, NULL) != 0) {
3213 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
3214 "\"request-aux-receiver\" signal is "
3215 "already used by the application");
3219 /* build the retransmission payload type map */
3220 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3221 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3224 if (stream->rtx_pt_map)
3225 gst_structure_free (stream->rtx_pt_map);
3226 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
3228 for (i = 0; i < stream->ptmap->len; i++) {
3229 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3230 GstStructure *s = gst_caps_get_structure (item->caps, 0);
3231 const gchar *encoding;
3233 /* we only care about RTX streams */
3234 if ((encoding = gst_structure_get_string (s, "encoding-name"))
3235 && g_strcmp0 (encoding, "RTX") == 0) {
3236 const gchar *stream_pt_s;
3239 if (gst_structure_get_int (s, "payload", &rtx_pt)
3240 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
3243 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
3250 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
3251 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
3254 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
3256 /* enable RFC4588 retransmission handling by setting rtprtxreceive
3257 * as the "aux" element of rtpbin */
3258 g_signal_connect (src->manager, "request-aux-receiver",
3259 (GCallback) request_aux_receiver, src);
3262 /* try to get and configure a manager */
3264 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3265 GstRTSPTransport * transport)
3267 const gchar *manager;
3269 GstStateChangeReturn ret;
3271 /* find a manager */
3272 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3276 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3278 /* configure the manager */
3279 if (src->manager == NULL) {
3280 GObjectClass *klass;
3282 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3284 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3288 goto use_no_manager;
3290 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3291 goto manager_failed;
3294 /* we manage this element */
3295 gst_element_set_locked_state (src->manager, TRUE);
3296 gst_bin_add (GST_BIN_CAST (src), src->manager);
3298 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3299 if (ret == GST_STATE_CHANGE_FAILURE)
3300 goto start_manager_failure;
3302 g_object_set (src->manager, "latency", src->latency, NULL);
3304 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3306 if (g_object_class_find_property (klass, "ntp-sync")) {
3307 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3310 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3311 g_object_set (src->manager, "use-pipeline-clock",
3312 src->use_pipeline_clock, NULL);
3315 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3316 g_object_set (src->manager, "sdes", src->sdes, NULL);
3319 if (g_object_class_find_property (klass, "drop-on-latency")) {
3320 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3324 /* buffer mode pauses are handled by adding offsets to buffer times,
3325 * but some depayloaders may have a hard time syncing output times
3326 * with such input times, e.g. container ones, most notably ASF */
3327 /* TODO alternatives are having an event that indicates these shifts,
3328 * or having rtsp extensions provide suggestion on buffer mode */
3329 /* valid duration implies not likely live pipeline,
3330 * so slaving in jitterbuffer does not make much sense
3331 * (and might mess things up due to bursts) */
3332 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3333 src->segment.duration && stream->container) {
3334 src->use_buffering = TRUE;
3336 src->use_buffering = FALSE;
3339 set_manager_buffer_mode (src);
3341 /* connect to signals */
3342 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3344 src->manager_sig_id =
3345 g_signal_connect (src->manager, "pad-added",
3346 (GCallback) new_manager_pad, src);
3347 src->manager_ptmap_id =
3348 g_signal_connect (src->manager, "request-pt-map",
3349 (GCallback) request_pt_map, src);
3351 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3354 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3357 if (src->do_retransmission)
3358 add_retransmission (src, transport);
3360 g_signal_connect (src->manager, "request-rtp-decoder",
3361 (GCallback) request_rtp_decoder, stream);
3362 g_signal_connect (src->manager, "request-rtcp-decoder",
3363 (GCallback) request_rtp_decoder, stream);
3364 g_signal_connect (src->manager, "request-rtcp-encoder",
3365 (GCallback) request_rtcp_encoder, stream);
3367 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3368 * into a separate RTP session. */
3369 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3370 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3372 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3373 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3376 /* now configure the bandwidth in the manager */
3377 if (g_signal_lookup ("get-internal-session",
3378 G_OBJECT_TYPE (src->manager)) != 0) {
3379 GObject *rtpsession;
3381 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3384 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3386 stream->session = rtpsession;
3388 if (stream->as_bandwidth != -1) {
3389 GST_INFO_OBJECT (src, "setting AS: %f",
3390 (gdouble) (stream->as_bandwidth * 1000));
3391 g_object_set (rtpsession, "bandwidth",
3392 (gdouble) (stream->as_bandwidth * 1000), NULL);
3394 if (stream->rr_bandwidth != -1) {
3395 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3396 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3399 if (stream->rs_bandwidth != -1) {
3400 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3401 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3405 g_object_set (rtpsession, "probation", src->probation, NULL);
3407 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3409 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3411 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3413 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3415 g_signal_connect (rtpsession, "on-ssrc-active",
3416 (GCallback) on_ssrc_active, stream);
3427 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3432 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3435 start_manager_failure:
3437 GST_DEBUG_OBJECT (src, "could not start session manager");
3442 /* free the UDP sources allocated when negotiating a transport.
3443 * This function is called when the server negotiated to a transport where the
3444 * UDP sources are not needed anymore, such as TCP or multicast. */
3446 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3450 for (i = 0; i < 2; i++) {
3451 if (stream->udpsrc[i]) {
3452 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3453 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3454 gst_object_unref (stream->udpsrc[i]);
3455 stream->udpsrc[i] = NULL;
3460 /* for TCP, create pads to send and receive data to and from the manager and to
3461 * intercept various events and queries
3464 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3465 GstRTSPTransport * transport, GstPad ** outpad)
3468 GstPadTemplate *template;
3469 GstPad *pad0, *pad1;
3471 /* configure for interleaved delivery, nothing needs to be done
3472 * here, the loop function will call the chain functions of the
3473 * session manager. */
3474 stream->channel[0] = transport->interleaved.min;
3475 stream->channel[1] = transport->interleaved.max;
3476 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3477 stream->channel[0], stream->channel[1]);
3479 /* we can remove the allocated UDP ports now */
3480 gst_rtspsrc_stream_free_udp (stream);
3482 /* no session manager, send data to srcpad directly */
3483 if (!stream->channelpad[0]) {
3484 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3486 /* create a new pad we will use to stream to */
3487 name = g_strdup_printf ("stream_%u", stream->id);
3488 template = gst_static_pad_template_get (&rtptemplate);
3489 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3490 gst_object_unref (template);
3493 /* set caps and activate */
3494 gst_pad_use_fixed_caps (stream->channelpad[0]);
3495 gst_pad_set_active (stream->channelpad[0], TRUE);
3497 *outpad = gst_object_ref (stream->channelpad[0]);
3499 GST_DEBUG_OBJECT (src, "using manager source pad");
3501 template = gst_static_pad_template_get (&anysrctemplate);
3503 /* allocate pads for sending the channel data into the manager */
3504 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3505 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3506 gst_object_unref (stream->channelpad[0]);
3507 stream->channelpad[0] = pad0;
3508 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3509 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3510 gst_pad_set_element_private (pad0, src);
3511 gst_pad_set_active (pad0, TRUE);
3513 if (stream->channelpad[1]) {
3514 /* if we have a sinkpad for the other channel, create a pad and link to the
3516 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3517 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3518 gst_pad_link_full (pad1, stream->channelpad[1],
3519 GST_PAD_LINK_CHECK_NOTHING);
3520 gst_object_unref (stream->channelpad[1]);
3521 stream->channelpad[1] = pad1;
3522 gst_pad_set_active (pad1, TRUE);
3524 gst_object_unref (template);
3526 /* setup RTCP transport back to the server if we have to. */
3527 if (src->manager && src->do_rtcp) {
3530 template = gst_static_pad_template_get (&anysinktemplate);
3532 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3533 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3534 gst_pad_set_element_private (stream->rtcppad, stream);
3535 gst_pad_set_active (stream->rtcppad, TRUE);
3537 /* get session RTCP pad */
3538 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3539 pad = gst_element_get_request_pad (src->manager, name);
3544 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3545 gst_object_unref (pad);
3548 gst_object_unref (template);
3554 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3555 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3556 gint * max, guint * ttl)
3558 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3560 if (!(*destination = transport->destination))
3561 *destination = stream->destination;
3564 /* transport first */
3565 *min = transport->port.min;
3566 *max = transport->port.max;
3567 if (*min == -1 && *max == -1) {
3568 /* then try from SDP */
3569 if (stream->port != 0) {
3570 *min = stream->port;
3571 *max = stream->port + 1;
3577 if (!(*ttl = transport->ttl))
3582 /* first take the source, then the endpoint to figure out where to send
3584 if (!(*destination = transport->source)) {
3585 if (src->conninfo.connection)
3586 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3587 else if (stream->conninfo.connection)
3589 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3593 /* for unicast we only expect the ports here */
3594 *min = transport->server_port.min;
3595 *max = transport->server_port.max;
3600 /* For multicast create UDP sources and join the multicast group. */
3602 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3603 GstRTSPTransport * transport, GstPad ** outpad)
3606 const gchar *destination;
3609 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3611 /* we can remove the allocated UDP ports now */
3612 gst_rtspsrc_stream_free_udp (stream);
3614 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3617 /* we need a destination now */
3618 if (destination == NULL)
3619 goto no_destination;
3621 /* we really need ports now or we won't be able to receive anything at all */
3622 if (min == -1 && max == -1)
3625 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3626 destination, min, max);
3628 /* creating UDP source for RTP */
3630 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3632 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3634 if (stream->udpsrc[0] == NULL)
3637 /* take ownership */
3638 gst_object_ref_sink (stream->udpsrc[0]);
3640 if (src->udp_buffer_size != 0)
3641 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3642 src->udp_buffer_size, NULL);
3644 if (src->multi_iface != NULL)
3645 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3646 src->multi_iface, NULL);
3649 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3650 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3653 /* creating another UDP source for RTCP */
3657 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3659 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3661 if (stream->udpsrc[1] == NULL)
3664 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3665 stream->profile == GST_RTSP_PROFILE_SAVPF)
3666 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3668 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3669 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3670 gst_caps_unref (caps);
3672 /* take ownership */
3673 gst_object_ref_sink (stream->udpsrc[1]);
3675 if (src->multi_iface != NULL)
3676 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3677 src->multi_iface, NULL);
3679 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
3686 GST_DEBUG_OBJECT (src, "no UDP source element found");
3691 GST_DEBUG_OBJECT (src, "no destination found");
3696 GST_DEBUG_OBJECT (src, "no ports found");
3701 /* configure the remainder of the UDP ports */
3703 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3704 GstRTSPTransport * transport, GstPad ** outpad)
3706 /* we manage the UDP elements now. For unicast, the UDP sources where
3707 * allocated in the stream when we suggested a transport. */
3708 if (stream->udpsrc[0]) {
3711 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3712 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3714 GST_DEBUG_OBJECT (src, "setting up UDP source");
3716 /* configure a timeout on the UDP port. When the timeout message is
3717 * posted, we assume UDP transport is not possible. We reconnect using TCP
3719 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3720 src->udp_timeout * 1000, NULL);
3722 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
3723 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3725 /* get output pad of the UDP source. */
3726 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3728 /* save it so we can unblock */
3729 stream->blockedpad = *outpad;
3731 /* configure pad block on the pad. As soon as there is dataflow on the
3732 * UDP source, we know that UDP is not blocked by a firewall and we can
3733 * configure all the streams to let the application autoplug decoders. */
3735 gst_pad_add_probe (stream->blockedpad,
3736 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3737 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
3739 if (stream->channelpad[0]) {
3740 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3741 /* configure for UDP delivery, we need to connect the UDP pads to
3742 * the session plugin. */
3743 gst_pad_link_full (*outpad, stream->channelpad[0],
3744 GST_PAD_LINK_CHECK_NOTHING);
3745 gst_object_unref (*outpad);
3747 /* we connected to pad-added signal to get pads from the manager */
3749 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3754 if (stream->udpsrc[1]) {
3757 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3758 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3760 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3761 stream->profile == GST_RTSP_PROFILE_SAVPF)
3762 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3764 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3765 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3766 gst_caps_unref (caps);
3768 if (stream->channelpad[1]) {
3771 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3773 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3774 gst_pad_link_full (pad, stream->channelpad[1],
3775 GST_PAD_LINK_CHECK_NOTHING);
3776 gst_object_unref (pad);
3778 /* leave unlinked */
3784 /* configure the UDP sink back to the server for status reports */
3786 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3787 GstRTSPStream * stream, GstRTSPTransport * transport)
3790 gint rtp_port, rtcp_port;
3791 gboolean do_rtp, do_rtcp;
3792 const gchar *destination;
3797 /* get transport info */
3798 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3799 &rtp_port, &rtcp_port, &ttl);
3801 /* see what we need to do */
3802 do_rtp = (rtp_port != -1);
3803 /* it's possible that the server does not want us to send RTCP in which case
3805 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3807 /* we need a destination when we have RTP or RTCP ports */
3808 if (destination == NULL && (do_rtp || do_rtcp))
3809 goto no_destination;
3811 /* try to construct the fakesrc to the RTP port of the server to open up any
3814 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3817 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3818 stream->udpsink[0] =
3819 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3821 if (stream->udpsink[0] == NULL)
3822 goto no_sink_element;
3824 /* don't join multicast group, we will have the source socket do that */
3825 /* no sync or async state changes needed */
3826 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3827 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3829 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3831 if (stream->udpsrc[0]) {
3832 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3833 * so that NAT firewalls will open a hole for us */
3834 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3835 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3836 /* configure socket and make sure udpsink does not close it when shutting
3837 * down, it belongs to udpsrc after all. */
3838 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3839 "close-socket", FALSE, NULL);
3840 g_object_unref (socket);
3843 /* the source for the dummy packets to open up NAT */
3844 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3845 if (stream->fakesrc == NULL)
3846 goto no_fakesrc_element;
3848 /* random data in 5 buffers, a size of 200 bytes should be fine */
3849 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3850 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3852 /* we don't want to consider this a sink */
3853 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
3855 /* keep everything locked */
3856 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3857 gst_element_set_locked_state (stream->fakesrc, TRUE);
3859 gst_object_ref (stream->udpsink[0]);
3860 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3861 gst_object_ref (stream->fakesrc);
3862 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3864 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3865 "sink", GST_PAD_LINK_CHECK_NOTHING);
3868 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3871 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3872 stream->udpsink[1] =
3873 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3875 if (stream->udpsink[1] == NULL)
3876 goto no_sink_element;
3878 /* don't join multicast group, we will have the source socket do that */
3879 /* no sync or async state changes needed */
3880 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3881 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3883 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3885 if (stream->udpsrc[1]) {
3886 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3887 * because some servers check the port number of where it sends RTCP to identify
3888 * the RTCP packets it receives */
3889 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3890 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3891 /* configure socket and make sure udpsink does not close it when shutting
3892 * down, it belongs to udpsrc after all. */
3893 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3894 "close-socket", FALSE, NULL);
3895 g_object_unref (socket);
3898 /* we don't want to consider this a sink */
3899 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3901 /* we keep this playing always */
3902 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3903 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3905 gst_object_ref (stream->udpsink[1]);
3906 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3908 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3910 /* get session RTCP pad */
3911 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3912 pad = gst_element_get_request_pad (src->manager, name);
3917 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3918 gst_object_unref (pad);
3927 GST_DEBUG_OBJECT (src, "no destination address specified");
3932 GST_DEBUG_OBJECT (src, "no UDP sink element found");
3937 GST_DEBUG_OBJECT (src, "no fakesrc element found");
3942 /* sets up all elements needed for streaming over the specified transport.
3943 * Does not yet expose the element pads, this will be done when there is actuall
3944 * dataflow detected, which might never happen when UDP is blocked in a
3945 * firewall, for example.
3948 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3949 GstRTSPTransport * transport)
3952 GstPad *outpad = NULL;
3953 GstPadTemplate *template;
3955 const gchar *media_type;
3958 src = stream->parent;
3960 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3962 /* get the proper media type for this stream now */
3963 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
3964 goto unknown_transport;
3966 goto unknown_transport;
3968 /* configure the final media type */
3969 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
3971 len = stream->ptmap->len;
3972 for (i = 0; i < len; i++) {
3974 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3976 if (item->caps == NULL)
3979 s = gst_caps_get_structure (item->caps, 0);
3980 gst_structure_set_name (s, media_type);
3981 /* set ssrc if known */
3982 if (transport->ssrc)
3983 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
3986 /* try to get and configure a manager, channelpad[0-1] will be configured with
3987 * the pads for the manager, or NULL when no manager is needed. */
3988 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3991 switch (transport->lower_transport) {
3992 case GST_RTSP_LOWER_TRANS_TCP:
3993 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3994 goto transport_failed;
3996 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3997 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3998 goto transport_failed;
3999 /* fallthrough, the rest is the same for UDP and MCAST */
4000 case GST_RTSP_LOWER_TRANS_UDP:
4001 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
4002 goto transport_failed;
4003 /* configure udpsinks back to the server for RTCP messages and for the
4004 * dummy RTP messages to open NAT. */
4005 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
4006 goto transport_failed;
4009 goto unknown_transport;
4013 GST_DEBUG_OBJECT (src, "creating ghostpad");
4015 gst_pad_use_fixed_caps (outpad);
4017 /* create ghostpad, don't add just yet, this will be done when we activate
4019 name = g_strdup_printf ("stream_%u", stream->id);
4020 template = gst_static_pad_template_get (&rtptemplate);
4021 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
4022 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
4023 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
4024 gst_object_unref (template);
4027 gst_object_unref (outpad);
4029 /* mark pad as ok */
4030 stream->last_ret = GST_FLOW_OK;
4037 GST_DEBUG_OBJECT (src, "failed to configure transport");
4042 GST_DEBUG_OBJECT (src, "unknown transport");
4047 GST_DEBUG_OBJECT (src, "cannot get a session manager");
4052 /* send a couple of dummy random packets on the receiver RTP port to the server,
4053 * this should make a firewall think we initiated the data transfer and
4054 * hopefully allow packets to go from the sender port to our RTP receiver port */
4056 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
4060 if (src->nat_method != GST_RTSP_NAT_DUMMY)
4063 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4064 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4066 if (stream->fakesrc && stream->udpsink[0]) {
4067 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
4068 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
4069 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
4070 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
4071 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
4077 /* Adds the source pads of all configured streams to the element.
4078 * This code is performed when we detected dataflow.
4080 * We detect dataflow from either the _loop function or with pad probes on the
4084 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
4088 GST_DEBUG_OBJECT (src, "activating streams");
4090 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4091 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4093 if (stream->udpsrc[0]) {
4094 /* remove timeout, we are streaming now and timeouts will be handled by
4095 * the session manager and jitter buffer */
4096 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
4098 if (stream->srcpad) {
4099 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
4100 gst_pad_set_active (stream->srcpad, TRUE);
4102 /* if we don't have a session manager, set the caps now. If we have a
4103 * session, we will get a notification of the pad and the caps. */
4104 if (!src->manager) {
4107 caps = stream_get_caps_for_pt (stream, stream->default_pt);
4108 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
4109 gst_pad_set_caps (stream->srcpad, caps);
4112 if (!stream->added) {
4113 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
4114 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
4115 stream->added = TRUE;
4120 /* unblock all pads */
4121 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4122 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4124 if (stream->blockid) {
4125 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
4126 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
4127 stream->blockid = 0;
4135 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
4136 gboolean reset_manager)
4139 guint64 start, stop;
4140 gdouble play_speed, play_scale;
4142 GST_DEBUG_OBJECT (src, "configuring stream caps");
4144 start = segment->position;
4145 stop = segment->duration;
4146 play_speed = segment->rate;
4147 play_scale = segment->applied_rate;
4149 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4150 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4156 len = stream->ptmap->len;
4157 for (j = 0; j < len; j++) {
4159 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
4161 if (item->caps == NULL)
4164 caps = gst_caps_make_writable (item->caps);
4166 if (stream->timebase != -1)
4167 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
4168 (guint) stream->timebase, NULL);
4169 if (stream->seqbase != -1)
4170 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
4171 (guint) stream->seqbase, NULL);
4172 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
4174 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
4175 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
4176 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
4179 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
4182 if (item->pt == stream->default_pt && stream->udpsrc[0]) {
4183 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4187 if (reset_manager && src->manager) {
4188 GST_DEBUG_OBJECT (src, "clear session");
4189 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
4193 static GstFlowReturn
4194 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
4199 /* store the value */
4200 stream->last_ret = ret;
4202 /* if it's success we can return the value right away */
4203 if (ret == GST_FLOW_OK)
4206 /* any other error that is not-linked can be returned right
4208 if (ret != GST_FLOW_NOT_LINKED)
4211 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4212 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4213 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4215 ret = ostream->last_ret;
4216 /* some other return value (must be SUCCESS but we can return
4217 * other values as well) */
4218 if (ret != GST_FLOW_NOT_LINKED)
4221 /* if we get here, all other pads were unlinked and we return
4222 * NOT_LINKED then */
4228 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4231 gboolean res = TRUE;
4233 /* only streams that have a connection to the outside world */
4237 if (stream->udpsrc[0]) {
4238 gst_event_ref (event);
4239 res = gst_element_send_event (stream->udpsrc[0], event);
4240 } else if (stream->channelpad[0]) {
4241 gst_event_ref (event);
4242 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4243 res = gst_pad_push_event (stream->channelpad[0], event);
4245 res = gst_pad_send_event (stream->channelpad[0], event);
4248 if (stream->udpsrc[1]) {
4249 gst_event_ref (event);
4250 res &= gst_element_send_event (stream->udpsrc[1], event);
4251 } else if (stream->channelpad[1]) {
4252 gst_event_ref (event);
4253 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4254 res &= gst_pad_push_event (stream->channelpad[1], event);
4256 res &= gst_pad_send_event (stream->channelpad[1], event);
4260 gst_event_unref (event);
4266 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4269 gboolean res = TRUE;
4271 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4272 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4274 gst_event_ref (event);
4275 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4277 gst_event_unref (event);
4282 static GstRTSPResult
4283 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4288 if (info->connection == NULL) {
4289 if (info->url == NULL) {
4290 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4291 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4295 /* create connection */
4296 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4297 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4298 goto could_not_create;
4301 g_free (info->url_str);
4302 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4304 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4306 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4307 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4308 src->tls_validation_flags))
4309 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4311 if (src->tls_database)
4312 gst_rtsp_connection_set_tls_database (info->connection,
4316 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4317 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4319 if (src->proxy_host) {
4320 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4322 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4327 if (!info->connected) {
4330 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4331 ("Connecting to %s", info->location));
4332 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4334 gst_rtsp_connection_connect (info->connection,
4335 src->ptcp_timeout)) < 0)
4336 goto could_not_connect;
4338 info->connected = TRUE;
4345 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4350 gchar *str = gst_rtsp_strresult (res);
4351 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4357 gchar *str = gst_rtsp_strresult (res);
4358 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4364 static GstRTSPResult
4365 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4368 GST_RTSP_STATE_LOCK (src);
4369 if (info->connected) {
4370 GST_DEBUG_OBJECT (src, "closing connection...");
4371 gst_rtsp_connection_close (info->connection);
4372 info->connected = FALSE;
4374 if (free && info->connection) {
4375 /* free connection */
4376 GST_DEBUG_OBJECT (src, "freeing connection...");
4377 gst_rtsp_connection_free (info->connection);
4378 info->connection = NULL;
4380 GST_RTSP_STATE_UNLOCK (src);
4384 static GstRTSPResult
4385 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4390 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4391 gst_rtsp_conninfo_close (src, info, FALSE);
4392 res = gst_rtsp_conninfo_connect (src, info, async);
4398 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4402 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4403 GST_RTSP_STATE_LOCK (src);
4404 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4405 GST_DEBUG_OBJECT (src, "connection flush");
4406 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4407 src->conninfo.flushing = flush;
4409 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4410 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4411 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4412 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4413 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4414 stream->conninfo.flushing = flush;
4417 GST_RTSP_STATE_UNLOCK (src);
4420 /* FIXME, handle server request, reply with OK, for now */
4421 static GstRTSPResult
4422 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
4423 GstRTSPMessage * request)
4425 GstRTSPMessage response = { 0 };
4428 GST_DEBUG_OBJECT (src, "got server request message");
4431 gst_rtsp_message_dump (request);
4433 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4435 if (res == GST_RTSP_ENOTIMPL) {
4436 /* default implementation, send OK */
4437 GST_DEBUG_OBJECT (src, "prepare OK reply");
4439 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4444 /* let app parse and reply */
4445 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4446 0, request, &response);
4449 gst_rtsp_message_dump (&response);
4451 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
4455 gst_rtsp_message_unset (&response);
4456 } else if (res == GST_RTSP_EEOF)
4464 gst_rtsp_message_unset (&response);
4469 /* send server keep-alive */
4470 static GstRTSPResult
4471 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4473 GstRTSPMessage request = { 0 };
4475 GstRTSPMethod method;
4476 const gchar *control;
4478 if (src->do_rtsp_keep_alive == FALSE) {
4479 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4480 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4484 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4486 /* find a method to use for keep-alive */
4487 if (src->methods & GST_RTSP_GET_PARAMETER)
4488 method = GST_RTSP_GET_PARAMETER;
4490 method = GST_RTSP_OPTIONS;
4492 control = get_aggregate_control (src);
4493 if (control == NULL)
4496 res = gst_rtsp_message_init_request (&request, method, control);
4501 gst_rtsp_message_dump (&request);
4504 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
4509 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4510 gst_rtsp_message_unset (&request);
4517 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4522 gchar *str = gst_rtsp_strresult (res);
4524 gst_rtsp_message_unset (&request);
4525 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4526 ("Could not send keep-alive. (%s)", str));
4532 static GstFlowReturn
4533 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4535 GstFlowReturn ret = GST_FLOW_OK;
4537 GstRTSPStream *stream;
4538 GstPad *outpad = NULL;
4544 channel = message->type_data.data.channel;
4546 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4548 goto unknown_stream;
4550 if (channel == stream->channel[0]) {
4551 outpad = stream->channelpad[0];
4553 } else if (channel == stream->channel[1]) {
4554 outpad = stream->channelpad[1];
4560 /* take a look at the body to figure out what we have */
4561 gst_rtsp_message_get_body (message, &data, &size);
4563 goto invalid_length;
4565 /* channels are not correct on some servers, do extra check */
4566 if (data[1] >= 200 && data[1] <= 204) {
4567 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4568 outpad = stream->channelpad[1];
4572 /* we have no clue what this is, just ignore then. */
4574 goto unknown_stream;
4576 /* take the message body for further processing */
4577 gst_rtsp_message_steal_body (message, &data, &size);
4579 /* strip the trailing \0 */
4582 buf = gst_buffer_new ();
4583 gst_buffer_append_memory (buf,
4584 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4586 /* don't need message anymore */
4587 gst_rtsp_message_unset (message);
4589 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4592 if (src->need_activate) {
4598 guint group_id = gst_util_group_id_next ();
4600 /* generate an SHA256 sum of the URI */
4601 cs = g_checksum_new (G_CHECKSUM_SHA256);
4602 uri = src->conninfo.location;
4603 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4605 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4606 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4610 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4611 event = gst_event_new_stream_start (stream_id);
4612 gst_event_set_group_id (event, group_id);
4615 gst_rtspsrc_stream_push_event (src, ostream, event);
4617 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
4618 /* only streams that have a connection to the outside world */
4619 if (ostream->setup) {
4620 if (ostream->udpsrc[0]) {
4621 gst_element_send_event (ostream->udpsrc[0],
4622 gst_event_new_caps (caps));
4623 } else if (ostream->channelpad[0]) {
4624 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
4625 gst_pad_push_event (ostream->channelpad[0],
4626 gst_event_new_caps (caps));
4628 gst_pad_send_event (ostream->channelpad[0],
4629 gst_event_new_caps (caps));
4632 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4634 if (ostream->udpsrc[1]) {
4635 gst_element_send_event (ostream->udpsrc[1],
4636 gst_event_new_caps (caps));
4637 } else if (ostream->channelpad[1]) {
4638 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
4639 gst_pad_push_event (ostream->channelpad[1],
4640 gst_event_new_caps (caps));
4642 gst_pad_send_event (ostream->channelpad[1],
4643 gst_event_new_caps (caps));
4646 gst_caps_unref (caps);
4650 g_checksum_free (cs);
4652 gst_rtspsrc_activate_streams (src);
4653 src->need_activate = FALSE;
4654 src->need_segment = TRUE;
4657 if (src->base_time == -1) {
4658 /* Take current running_time. This timestamp will be put on
4659 * the first buffer of each stream because we are a live source and so we
4660 * timestamp with the running_time. When we are dealing with TCP, we also
4661 * only timestamp the first buffer (using the DISCONT flag) because a server
4662 * typically bursts data, for which we don't want to compensate by speeding
4663 * up the media. The other timestamps will be interpollated from this one
4664 * using the RTP timestamps. */
4665 GST_OBJECT_LOCK (src);
4666 if (GST_ELEMENT_CLOCK (src)) {
4668 GstClockTime base_time;
4670 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4671 base_time = GST_ELEMENT_CAST (src)->base_time;
4673 src->base_time = now - base_time;
4675 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4676 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4678 GST_OBJECT_UNLOCK (src);
4681 /* If needed send a new segment, don't forget we are live and buffer are
4682 * timestamped with running time */
4683 if (src->need_segment) {
4685 src->need_segment = FALSE;
4686 gst_segment_init (&segment, GST_FORMAT_TIME);
4687 gst_rtspsrc_push_event (src, gst_event_new_segment (&segment));
4690 if (stream->discont && !is_rtcp) {
4691 /* mark first RTP buffer as discont */
4692 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4693 stream->discont = FALSE;
4694 /* first buffer gets the timestamp, other buffers are not timestamped and
4695 * their presentation time will be interpollated from the rtp timestamps. */
4696 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4697 GST_TIME_ARGS (src->base_time));
4699 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4702 /* chain to the peer pad */
4703 if (GST_PAD_IS_SINK (outpad))
4704 ret = gst_pad_chain (outpad, buf);
4706 ret = gst_pad_push (outpad, buf);
4709 /* combine all stream flows for the data transport */
4710 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4717 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4718 gst_rtsp_message_unset (message);
4723 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4724 ("Short message received, ignoring."));
4725 gst_rtsp_message_unset (message);
4730 static GstFlowReturn
4731 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4733 GstRTSPMessage message = { 0 };
4735 GstFlowReturn ret = GST_FLOW_OK;
4736 GTimeVal tv_timeout;
4739 /* get the next timeout interval */
4740 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4742 /* see if the timeout period expired */
4743 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4744 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4745 /* send keep-alive, only act on interrupt, a warning will be posted for
4747 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4749 /* get new timeout */
4750 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4753 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4754 tv_timeout.tv_sec, tv_timeout.tv_usec);
4756 /* protect the connection with the connection lock so that we can see when
4757 * we are finished doing server communication */
4759 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4760 &message, src->ptcp_timeout);
4764 GST_DEBUG_OBJECT (src, "we received a server message");
4766 case GST_RTSP_EINTR:
4767 /* we got interrupted this means we need to stop */
4769 case GST_RTSP_ETIMEOUT:
4770 /* no reply, send keep alive */
4771 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4772 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4776 /* go EOS when the server closed the connection */
4782 switch (message.type) {
4783 case GST_RTSP_MESSAGE_REQUEST:
4784 /* server sends us a request message, handle it */
4786 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4788 if (res == GST_RTSP_EEOF)
4791 goto handle_request_failed;
4793 case GST_RTSP_MESSAGE_RESPONSE:
4794 /* we ignore response messages */
4795 GST_DEBUG_OBJECT (src, "ignoring response message");
4797 gst_rtsp_message_dump (&message);
4799 case GST_RTSP_MESSAGE_DATA:
4800 GST_DEBUG_OBJECT (src, "got data message");
4801 ret = gst_rtspsrc_handle_data (src, &message);
4802 if (ret != GST_FLOW_OK)
4803 goto handle_data_failed;
4806 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4811 g_assert_not_reached ();
4816 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4817 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4818 ("The server closed the connection."));
4819 src->conninfo.connected = FALSE;
4820 gst_rtsp_message_unset (&message);
4821 return GST_FLOW_EOS;
4825 gst_rtsp_message_unset (&message);
4826 GST_DEBUG_OBJECT (src, "got interrupted");
4827 return GST_FLOW_FLUSHING;
4831 gchar *str = gst_rtsp_strresult (res);
4833 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4834 ("Could not receive message. (%s)", str));
4837 gst_rtsp_message_unset (&message);
4838 return GST_FLOW_ERROR;
4840 handle_request_failed:
4842 gchar *str = gst_rtsp_strresult (res);
4844 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4845 ("Could not handle server message. (%s)", str));
4847 gst_rtsp_message_unset (&message);
4848 return GST_FLOW_ERROR;
4852 GST_DEBUG_OBJECT (src, "could no handle data message");
4857 static GstFlowReturn
4858 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4861 GstRTSPMessage message = { 0 };
4865 GTimeVal tv_timeout;
4867 /* get the next timeout interval */
4868 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4870 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4871 (gint) tv_timeout.tv_sec);
4873 gst_rtsp_message_unset (&message);
4875 /* we should continue reading the TCP socket because the server might
4876 * send us requests. When the session timeout expires, we need to send a
4877 * keep-alive request to keep the session open. */
4878 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4879 &message, &tv_timeout);
4883 GST_DEBUG_OBJECT (src, "we received a server message");
4885 case GST_RTSP_EINTR:
4886 /* we got interrupted, see what we have to do */
4888 case GST_RTSP_ETIMEOUT:
4889 /* send keep-alive, ignore the result, a warning will be posted. */
4890 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4891 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4895 /* server closed the connection. not very fatal for UDP, reconnect and
4896 * see what happens. */
4897 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4898 ("The server closed the connection."));
4899 if (src->udp_reconnect) {
4901 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4908 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4910 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4911 ("Unhandled return value %d.", res));
4915 switch (message.type) {
4916 case GST_RTSP_MESSAGE_REQUEST:
4917 /* server sends us a request message, handle it */
4919 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4921 if (res == GST_RTSP_EEOF)
4924 goto handle_request_failed;
4926 case GST_RTSP_MESSAGE_RESPONSE:
4927 /* we ignore response and data messages */
4928 GST_DEBUG_OBJECT (src, "ignoring response message");
4930 gst_rtsp_message_dump (&message);
4931 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4932 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4933 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4934 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4935 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4942 case GST_RTSP_MESSAGE_DATA:
4943 /* we ignore response and data messages */
4944 GST_DEBUG_OBJECT (src, "ignoring data message");
4947 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4952 g_assert_not_reached ();
4954 /* we get here when the connection got interrupted */
4957 gst_rtsp_message_unset (&message);
4958 GST_DEBUG_OBJECT (src, "got interrupted");
4959 return GST_FLOW_FLUSHING;
4963 gchar *str = gst_rtsp_strresult (res);
4966 src->conninfo.connected = FALSE;
4967 if (res != GST_RTSP_EINTR) {
4968 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4969 ("Could not connect to server. (%s)", str));
4971 ret = GST_FLOW_ERROR;
4973 ret = GST_FLOW_FLUSHING;
4979 gchar *str = gst_rtsp_strresult (res);
4981 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4982 ("Could not receive message. (%s)", str));
4984 return GST_FLOW_ERROR;
4986 handle_request_failed:
4988 gchar *str = gst_rtsp_strresult (res);
4991 gst_rtsp_message_unset (&message);
4992 if (res != GST_RTSP_EINTR) {
4993 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4994 ("Could not handle server message. (%s)", str));
4996 ret = GST_FLOW_ERROR;
4998 ret = GST_FLOW_FLUSHING;
5004 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5005 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5006 ("The server closed the connection."));
5007 src->conninfo.connected = FALSE;
5008 gst_rtsp_message_unset (&message);
5009 return GST_FLOW_EOS;
5013 static GstRTSPResult
5014 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
5016 GstRTSPResult res = GST_RTSP_OK;
5019 GST_DEBUG_OBJECT (src, "doing reconnect");
5021 GST_OBJECT_LOCK (src);
5022 /* only restart when the pads were not yet activated, else we were
5023 * streaming over UDP */
5024 restart = src->need_activate;
5025 GST_OBJECT_UNLOCK (src);
5027 /* no need to restart, we're done */
5031 /* we can try only TCP now */
5032 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
5034 /* close and cleanup our state */
5035 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
5038 /* see if we have TCP left to try. Also don't try TCP when we were configured
5040 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
5043 /* We post a warning message now to inform the user
5044 * that nothing happened. It's most likely a firewall thing. */
5045 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5046 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5047 "firewall is blocking it. Retrying using a TCP connection.",
5048 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
5050 /* open new connection using tcp */
5051 if (gst_rtspsrc_open (src, async) < 0)
5054 /* start playback */
5055 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
5064 src->cur_protocols = 0;
5065 /* no transport possible, post an error and stop */
5066 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5067 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5068 "firewall is blocking it. No other protocols to try.",
5069 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
5070 return GST_RTSP_ERROR;
5074 GST_DEBUG_OBJECT (src, "open failed");
5079 GST_DEBUG_OBJECT (src, "play failed");
5085 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
5089 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
5092 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
5095 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
5098 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
5106 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
5110 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
5113 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
5116 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
5119 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
5127 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
5131 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
5134 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
5137 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
5140 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
5148 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
5152 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
5155 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
5158 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
5161 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
5169 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
5171 if (ret == GST_RTSP_OK)
5172 gst_rtspsrc_loop_complete_cmd (src, cmd);
5173 else if (ret == GST_RTSP_EINTR)
5174 gst_rtspsrc_loop_cancel_cmd (src, cmd);
5176 gst_rtspsrc_loop_error_cmd (src, cmd);
5180 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
5183 gboolean flushed = FALSE;
5185 /* start new request */
5186 gst_rtspsrc_loop_start_cmd (src, cmd);
5188 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
5190 GST_OBJECT_LOCK (src);
5191 old = src->pending_cmd;
5192 if (old == CMD_RECONNECT) {
5193 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
5194 cmd = CMD_RECONNECT;
5196 if (old != CMD_WAIT) {
5197 src->pending_cmd = CMD_WAIT;
5198 GST_OBJECT_UNLOCK (src);
5199 /* cancel previous request */
5200 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
5201 gst_rtspsrc_loop_cancel_cmd (src, old);
5202 GST_OBJECT_LOCK (src);
5204 src->pending_cmd = cmd;
5205 /* interrupt if allowed */
5206 if (src->busy_cmd & mask) {
5207 GST_DEBUG_OBJECT (src, "connection flush busy %s",
5208 cmd_to_string (src->busy_cmd));
5209 gst_rtspsrc_connection_flush (src, TRUE);
5212 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
5213 cmd_to_string (src->busy_cmd));
5216 gst_task_start (src->task);
5217 GST_OBJECT_UNLOCK (src);
5223 gst_rtspsrc_loop (GstRTSPSrc * src)
5227 if (!src->conninfo.connection || !src->conninfo.connected)
5230 if (src->interleaved)
5231 ret = gst_rtspsrc_loop_interleaved (src);
5233 ret = gst_rtspsrc_loop_udp (src);
5235 if (ret != GST_FLOW_OK)
5243 GST_WARNING_OBJECT (src, "we are not connected");
5244 ret = GST_FLOW_FLUSHING;
5249 const gchar *reason = gst_flow_get_name (ret);
5251 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
5252 src->running = FALSE;
5253 if (ret == GST_FLOW_EOS) {
5254 /* perform EOS logic */
5255 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5256 gst_element_post_message (GST_ELEMENT_CAST (src),
5257 gst_message_new_segment_done (GST_OBJECT_CAST (src),
5258 src->segment.format, src->segment.position));
5259 gst_rtspsrc_push_event (src,
5260 gst_event_new_segment_done (src->segment.format,
5261 src->segment.position));
5263 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5265 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
5266 /* for fatal errors we post an error message, post the error before the
5267 * EOS so the app knows about the error first. */
5268 GST_ELEMENT_ERROR (src, STREAM, FAILED,
5269 ("Internal data flow error."),
5270 ("streaming task paused, reason %s (%d)", reason, ret));
5271 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5273 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
5278 #ifndef GST_DISABLE_GST_DEBUG
5279 static const gchar *
5280 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
5284 while (method != 0) {
5301 static const gchar *
5302 gst_rtspsrc_skip_lws (const gchar * s)
5304 while (g_ascii_isspace (*s))
5309 static const gchar *
5310 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
5312 while (s > start && g_ascii_isspace (*(s - 1)))
5317 static const gchar *
5318 gst_rtspsrc_skip_commas (const gchar * s)
5320 /* The grammar allows for multiple commas */
5321 while (g_ascii_isspace (*s) || *s == ',')
5326 static const gchar *
5327 gst_rtspsrc_skip_item (const gchar * s)
5329 gboolean quoted = FALSE;
5330 const gchar *start = s;
5332 /* A list item ends at the last non-whitespace character
5333 * before a comma which is not inside a quoted-string. Or at
5334 * the end of the string.
5340 if (*s == '\\' && *(s + 1))
5349 return gst_rtspsrc_unskip_lws (s, start);
5353 gst_rtsp_decode_quoted_string (gchar * quoted_string)
5357 src = quoted_string + 1;
5358 dst = quoted_string;
5359 while (*src && *src != '"') {
5360 if (*src == '\\' && *(src + 1))
5367 /* Extract the authentication tokens that the server provided for each method
5368 * into an array of structures and give those to the connection object.
5371 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
5372 const gchar * header, gboolean * stale)
5374 GSList *list = NULL, *iter;
5376 gchar *item, *eq, *name_end, *value;
5378 g_return_if_fail (stale != NULL);
5380 gst_rtsp_connection_clear_auth_params (conn);
5383 /* Parse a header whose content is described by RFC2616 as
5384 * "#something", where "something" does not itself contain commas,
5385 * except as part of quoted-strings, into a list of allocated strings.
5387 header = gst_rtspsrc_skip_commas (header);
5389 end = gst_rtspsrc_skip_item (header);
5390 list = g_slist_prepend (list, g_strndup (header, end - header));
5391 header = gst_rtspsrc_skip_commas (end);
5396 list = g_slist_reverse (list);
5397 for (iter = list; iter; iter = iter->next) {
5400 eq = strchr (item, '=');
5402 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
5403 if (name_end == item) {
5404 /* That's no good... */
5411 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
5413 gst_rtsp_decode_quoted_string (value);
5417 if (value && strcmp (item, "stale") == 0 && strcmp (value, "TRUE") == 0)
5419 gst_rtsp_connection_set_auth_param (conn, item, value);
5423 g_slist_free (list);
5426 /* Parse a WWW-Authenticate Response header and determine the
5427 * available authentication methods
5429 * This code should also cope with the fact that each WWW-Authenticate
5430 * header can contain multiple challenge methods + tokens
5432 * At the moment, for Basic auth, we just do a minimal check and don't
5433 * even parse out the realm */
5435 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
5436 GstRTSPConnection * conn, gboolean * stale)
5440 g_return_if_fail (hdr != NULL);
5441 g_return_if_fail (methods != NULL);
5442 g_return_if_fail (stale != NULL);
5444 /* Skip whitespace at the start of the string */
5445 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
5447 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
5448 *methods |= GST_RTSP_AUTH_BASIC;
5449 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
5450 *methods |= GST_RTSP_AUTH_DIGEST;
5451 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
5456 * gst_rtspsrc_setup_auth:
5457 * @src: the rtsp source
5459 * Configure a username and password and auth method on the
5460 * connection object based on a response we received from the
5463 * Currently, this requires that a username and password were supplied
5464 * in the uri. In the future, they may be requested on demand by sending
5465 * a message up the bus.
5467 * Returns: TRUE if authentication information could be set up correctly.
5470 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5474 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5475 GstRTSPAuthMethod method;
5476 GstRTSPResult auth_result;
5478 GstRTSPConnection *conn;
5480 gboolean stale = FALSE;
5482 conn = src->conninfo.connection;
5484 /* Identify the available auth methods and see if any are supported */
5485 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
5486 &hdr, 0) == GST_RTSP_OK) {
5487 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
5490 if (avail_methods == GST_RTSP_AUTH_NONE)
5491 goto no_auth_available;
5493 /* For digest auth, if the response indicates that the session
5494 * data are stale, we just update them in the connection object and
5495 * return TRUE to retry the request */
5497 src->tried_url_auth = FALSE;
5499 url = gst_rtsp_connection_get_url (conn);
5501 /* Do we have username and password available? */
5502 if (url != NULL && !src->tried_url_auth && url->user != NULL
5503 && url->passwd != NULL) {
5506 src->tried_url_auth = TRUE;
5507 GST_DEBUG_OBJECT (src,
5508 "Attempting authentication using credentials from the URL");
5510 user = src->user_id;
5511 pass = src->user_pw;
5512 GST_DEBUG_OBJECT (src,
5513 "Attempting authentication using credentials from the properties");
5516 /* FIXME: If the url didn't contain username and password or we tried them
5517 * already, request a username and passwd from the application via some kind
5518 * of credentials request message */
5520 /* If we don't have a username and passwd at this point, bail out. */
5521 if (user == NULL || pass == NULL)
5524 /* Try to configure for each available authentication method, strongest to
5526 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5527 /* Check if this method is available on the server */
5528 if ((method & avail_methods) == 0)
5531 /* Pass the credentials to the connection to try on the next request */
5532 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5533 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5534 * ignore it and end up retrying later */
5535 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5536 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5537 gst_rtsp_auth_method_to_string (method));
5542 if (method == GST_RTSP_AUTH_NONE)
5543 goto no_auth_available;
5549 /* Output an error indicating that we couldn't connect because there were
5550 * no supported authentication protocols */
5551 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5552 ("No supported authentication protocol was found"));
5557 /* We don't fire an error message, we just return FALSE and let the
5558 * normal NOT_AUTHORIZED error be propagated */
5563 static GstRTSPResult
5564 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5565 GstRTSPMessage * request, GstRTSPMessage * response,
5566 GstRTSPStatusCode * code)
5569 GstRTSPStatusCode thecode;
5570 gchar *content_base = NULL;
5574 if (!src->short_header)
5575 gst_rtsp_ext_list_before_send (src->extensions, request);
5577 GST_DEBUG_OBJECT (src, "sending message");
5580 gst_rtsp_message_dump (request);
5582 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
5586 gst_rtsp_connection_reset_timeout (conn);
5589 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
5594 gst_rtsp_message_dump (response);
5596 switch (response->type) {
5597 case GST_RTSP_MESSAGE_REQUEST:
5598 res = gst_rtspsrc_handle_request (src, conn, response);
5599 if (res == GST_RTSP_EEOF)
5602 goto handle_request_failed;
5604 case GST_RTSP_MESSAGE_RESPONSE:
5605 /* ok, a response is good */
5606 GST_DEBUG_OBJECT (src, "received response message");
5608 case GST_RTSP_MESSAGE_DATA:
5609 /* get next response */
5610 GST_DEBUG_OBJECT (src, "handle data response message");
5611 gst_rtspsrc_handle_data (src, response);
5614 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5619 thecode = response->type_data.response.code;
5621 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5623 /* if the caller wanted the result code, we store it. */
5627 /* If the request didn't succeed, bail out before doing any more */
5628 if (thecode != GST_RTSP_STS_OK)
5631 /* store new content base if any */
5632 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5635 g_free (src->content_base);
5636 src->content_base = g_strdup (content_base);
5638 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5645 gchar *str = gst_rtsp_strresult (res);
5647 if (res != GST_RTSP_EINTR) {
5648 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5649 ("Could not send message. (%s)", str));
5651 GST_WARNING_OBJECT (src, "send interrupted");
5660 GST_WARNING_OBJECT (src, "server closed connection");
5661 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5663 /* if reconnect succeeds, try again */
5665 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5669 /* only try once after reconnect, then fallthrough and error out */
5672 gchar *str = gst_rtsp_strresult (res);
5674 if (res != GST_RTSP_EINTR) {
5675 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5676 ("Could not receive message. (%s)", str));
5678 GST_WARNING_OBJECT (src, "receive interrupted");
5686 handle_request_failed:
5688 /* ERROR was posted */
5689 gst_rtsp_message_unset (response);
5694 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5695 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5696 ("The server closed the connection."));
5697 gst_rtsp_message_unset (response);
5704 * @src: the rtsp source
5705 * @conn: the connection to send on
5706 * @request: must point to a valid request
5707 * @response: must point to an empty #GstRTSPMessage
5708 * @code: an optional code result
5710 * send @request and retrieve the response in @response. optionally @code can be
5711 * non-NULL in which case it will contain the status code of the response.
5713 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5714 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5716 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5717 * @response message) if the response code was not 200 (OK).
5719 * If the attempt results in an authentication failure, then this will attempt
5720 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5723 * Returns: #GST_RTSP_OK if the processing was successful.
5725 static GstRTSPResult
5726 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5727 GstRTSPMessage * request, GstRTSPMessage * response,
5728 GstRTSPStatusCode * code)
5730 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5731 GstRTSPResult res = GST_RTSP_ERROR;
5734 GstRTSPMethod method = GST_RTSP_INVALID;
5740 /* make sure we don't loop forever */
5744 /* save method so we can disable it when the server complains */
5745 method = request->type_data.request.method;
5748 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5752 case GST_RTSP_STS_UNAUTHORIZED:
5753 if (gst_rtspsrc_setup_auth (src, response)) {
5754 /* Try the request/response again after configuring the auth info
5762 } while (retry == TRUE);
5764 /* If the user requested the code, let them handle errors, otherwise
5765 * post an error below */
5768 else if (int_code != GST_RTSP_STS_OK)
5769 goto error_response;
5776 GST_DEBUG_OBJECT (src, "got error %d", res);
5781 res = GST_RTSP_ERROR;
5783 switch (response->type_data.response.code) {
5784 case GST_RTSP_STS_NOT_FOUND:
5785 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5786 response->type_data.response.reason));
5788 case GST_RTSP_STS_UNAUTHORIZED:
5789 GST_ELEMENT_ERROR (src, RESOURCE, NOT_AUTHORIZED, (NULL), ("%s",
5790 response->type_data.response.reason));
5792 case GST_RTSP_STS_MOVED_PERMANENTLY:
5793 case GST_RTSP_STS_MOVE_TEMPORARILY:
5795 gchar *new_location;
5796 GstRTSPLowerTrans transports;
5798 GST_DEBUG_OBJECT (src, "got redirection");
5799 /* if we don't have a Location Header, we must error */
5800 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5801 &new_location, 0) < 0)
5804 /* When we receive a redirect result, we go back to the INIT state after
5805 * parsing the new URI. The caller should do the needed steps to issue
5806 * a new setup when it detects this state change. */
5807 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5809 /* save current transports */
5810 if (src->conninfo.url)
5811 transports = src->conninfo.url->transports;
5813 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5815 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5817 /* set old transports */
5818 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5819 src->conninfo.url->transports = transports;
5821 src->need_redirect = TRUE;
5822 src->state = GST_RTSP_STATE_INIT;
5826 case GST_RTSP_STS_NOT_ACCEPTABLE:
5827 case GST_RTSP_STS_NOT_IMPLEMENTED:
5828 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5829 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5830 gst_rtsp_method_as_text (method));
5831 src->methods &= ~method;
5835 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5836 ("Got error response: %d (%s).", response->type_data.response.code,
5837 response->type_data.response.reason));
5840 /* if we return ERROR we should unset the response ourselves */
5841 if (res == GST_RTSP_ERROR)
5842 gst_rtsp_message_unset (response);
5848 static GstRTSPResult
5849 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5850 GstRTSPMessage * response, GstRTSPSrc * src)
5852 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5857 /* parse the response and collect all the supported methods. We need this
5858 * information so that we don't try to send an unsupported request to the
5862 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5864 GstRTSPHeaderField field;
5868 /* reset supported methods */
5871 /* Try Allow Header first */
5872 field = GST_RTSP_HDR_ALLOW;
5875 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5876 if (indx == 0 && !respoptions) {
5877 /* if no Allow header was found then try the Public header... */
5878 field = GST_RTSP_HDR_PUBLIC;
5879 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5884 src->methods |= gst_rtsp_options_from_text (respoptions);
5889 if (src->methods == 0) {
5890 /* neither Allow nor Public are required, assume the server supports
5891 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5893 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5894 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5896 /* always assume PLAY, FIXME, extensions should be able to override
5898 src->methods |= GST_RTSP_PLAY;
5899 /* also assume it will support Range */
5900 src->seekable = TRUE;
5902 /* we need describe and setup */
5903 if (!(src->methods & GST_RTSP_DESCRIBE))
5905 if (!(src->methods & GST_RTSP_SETUP))
5913 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5914 ("Server does not support DESCRIBE."));
5919 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5920 ("Server does not support SETUP."));
5925 /* masks to be kept in sync with the hardcoded protocol order of preference
5927 static const guint protocol_masks[] = {
5928 GST_RTSP_LOWER_TRANS_UDP,
5929 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5930 GST_RTSP_LOWER_TRANS_TCP,
5934 static GstRTSPResult
5935 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5936 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
5940 gboolean add_udp_str;
5945 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5950 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5952 /* extension listed transports, use those */
5953 if (*transports != NULL)
5956 /* it's the default */
5957 add_udp_str = FALSE;
5959 /* the default RTSP transports */
5960 result = g_string_new ("RTP");
5963 case GST_RTSP_PROFILE_AVP:
5964 g_string_append (result, "/AVP");
5966 case GST_RTSP_PROFILE_SAVP:
5967 g_string_append (result, "/SAVP");
5969 case GST_RTSP_PROFILE_AVPF:
5970 g_string_append (result, "/AVPF");
5972 case GST_RTSP_PROFILE_SAVPF:
5973 g_string_append (result, "/SAVPF");
5979 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5980 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5982 g_string_append (result, "/UDP");
5983 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5984 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5985 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5986 /* we don't have to allocate any UDP ports yet, if the selected transport
5987 * turns out to be multicast we can create them and join the multicast
5988 * group indicated in the transport reply */
5990 g_string_append (result, "/UDP");
5991 g_string_append (result, ";multicast");
5992 if (src->next_port_num != 0) {
5993 if (src->client_port_range.max > 0 &&
5994 src->next_port_num >= src->client_port_range.max)
5997 g_string_append_printf (result, ";client_port=%d-%d",
5998 src->next_port_num, src->next_port_num + 1);
6000 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
6001 GST_DEBUG_OBJECT (src, "adding TCP");
6003 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
6005 *transports = g_string_free (result, FALSE);
6007 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
6014 GST_ERROR ("extension gave error %d", res);
6019 GST_ERROR ("no more ports available");
6020 return GST_RTSP_ERROR;
6024 static GstRTSPResult
6025 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
6026 gint orig_rtpport, gint orig_rtcpport)
6029 gint nr_udp, nr_int;
6031 gint rtpport = 0, rtcpport = 0;
6034 src = stream->parent;
6036 /* find number of placeholders first */
6037 if (strstr (*transports, "%%i2"))
6039 else if (strstr (*transports, "%%i1"))
6044 if (strstr (*transports, "%%u2"))
6046 else if (strstr (*transports, "%%u1"))
6051 if (nr_udp == 0 && nr_int == 0)
6055 if (!orig_rtpport || !orig_rtcpport) {
6056 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
6059 rtpport = orig_rtpport;
6060 rtcpport = orig_rtcpport;
6064 str = g_string_new ("");
6066 while ((next = strstr (p, "%%"))) {
6067 g_string_append_len (str, p, next - p);
6068 if (next[2] == 'u') {
6070 g_string_append_printf (str, "%d", rtpport);
6071 else if (next[3] == '2')
6072 g_string_append_printf (str, "%d", rtcpport);
6074 if (next[2] == 'i') {
6076 g_string_append_printf (str, "%d", src->free_channel);
6077 else if (next[3] == '2')
6078 g_string_append_printf (str, "%d", src->free_channel + 1);
6083 /* append final part */
6084 g_string_append (str, p);
6086 g_free (*transports);
6087 *transports = g_string_free (str, FALSE);
6095 GST_ERROR ("failed to allocate udp ports");
6096 return GST_RTSP_ERROR;
6101 enc_key_length_from_cipher_name (const gchar * cipher)
6103 if (g_strcmp0 (cipher, "aes-128-icm") == 0)
6104 return AES_128_KEY_LEN;
6105 else if (g_strcmp0 (cipher, "aes-256-icm") == 0)
6106 return AES_256_KEY_LEN;
6108 GST_ERROR ("encryption algorithm '%s' not supported", cipher);
6114 auth_key_length_from_auth_name (const gchar * auth)
6116 if (g_strcmp0 (auth, "hmac-sha1-32") == 0)
6117 return HMAC_32_KEY_LEN;
6118 else if (g_strcmp0 (auth, "hmac-sha1-80") == 0)
6119 return HMAC_80_KEY_LEN;
6121 GST_ERROR ("authentication algorithm '%s' not supported", auth);
6127 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
6129 GstCaps *caps = NULL;
6131 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
6135 GST_DEBUG_OBJECT (src, "SRTP parameters received");
6141 default_srtcp_params (void)
6149 /* create a random key */
6150 key_data = g_malloc (KEY_SIZE);
6151 for (i = 0; i < KEY_SIZE; i += 4)
6152 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
6154 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
6156 caps = gst_caps_new_simple ("application/x-srtp",
6157 "srtp-key", GST_TYPE_BUFFER, buf,
6158 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
6159 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
6161 gst_buffer_unref (buf);
6167 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
6170 gchar *result, *base64;
6173 GstMIKEYMessage *msg;
6174 GstMIKEYPayload *payload, *pkd;
6180 const gchar *srtcpcipher, *srtcpauth;
6182 stream->srtcpparams = signal_get_srtcp_params (src, stream);
6183 if (stream->srtcpparams == NULL)
6184 stream->srtcpparams = default_srtcp_params ();
6186 s = gst_caps_get_structure (stream->srtcpparams, 0);
6188 srtcpcipher = gst_structure_get_string (s, "srtcp-cipher");
6189 srtcpauth = gst_structure_get_string (s, "srtcp-auth");
6190 val = gst_structure_get_value (s, "srtp-key");
6192 if (srtcpcipher == NULL || srtcpauth == NULL || val == NULL) {
6193 GST_ERROR_OBJECT (src, "could not find the right SRTP parameters in caps");
6197 srtpkey = gst_value_get_buffer (val);
6199 msg = gst_mikey_message_new ();
6200 /* unencrypted MIKEY message, we send this over TLS so this is allowed */
6201 gst_mikey_message_set_info (msg, GST_MIKEY_VERSION, GST_MIKEY_TYPE_PSK_INIT,
6202 FALSE, GST_MIKEY_PRF_MIKEY_1, g_random_int (), GST_MIKEY_MAP_TYPE_SRTP);
6203 /* add policy '0' for our SSRC */
6204 gst_mikey_message_add_cs_srtp (msg, 0, stream->send_ssrc, 0);
6205 /* timestamp is now */
6206 gst_mikey_message_add_t_now_ntp_utc (msg);
6207 /* add some random data */
6208 gst_mikey_message_add_rand_len (msg, 16);
6210 /* the policy '0' is SRTP */
6211 payload = gst_mikey_payload_new (GST_MIKEY_PT_SP);
6212 gst_mikey_payload_sp_set (payload, 0, GST_MIKEY_SEC_PROTO_SRTP);
6214 /* only AES-CM is supported */
6216 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_ALG, 1, &byte);
6217 /* encryption key length */
6218 byte = enc_key_length_from_cipher_name (srtcpcipher);
6219 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_KEY_LEN, 1,
6221 /* only HMAC-SHA1 */
6222 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_ALG, 1,
6224 /* authentication key length */
6225 byte = auth_key_length_from_auth_name (srtcpauth);
6226 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_KEY_LEN, 1,
6228 /* we enable encryption on RTP and RTCP */
6229 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_ENC, 1,
6231 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTCP_ENC, 1,
6233 /* we enable authentication on RTP and RTCP */
6234 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_AUTH, 1,
6236 gst_mikey_message_add_payload (msg, payload);
6238 /* make unencrypted KEMAC */
6239 payload = gst_mikey_payload_new (GST_MIKEY_PT_KEMAC);
6240 gst_mikey_payload_kemac_set (payload, GST_MIKEY_ENC_NULL, GST_MIKEY_MAC_NULL);
6241 /* add the key in KEMAC */
6242 pkd = gst_mikey_payload_new (GST_MIKEY_PT_KEY_DATA);
6243 gst_buffer_map (srtpkey, &info, GST_MAP_READ);
6244 gst_mikey_payload_key_data_set_key (pkd, GST_MIKEY_KD_TEK, info.size,
6246 gst_buffer_unmap (srtpkey, &info);
6247 gst_mikey_payload_kemac_add_sub (payload, pkd);
6248 gst_mikey_message_add_payload (msg, payload);
6250 /* now serialize this to bytes */
6251 bytes = gst_mikey_message_to_bytes (msg, NULL, NULL);
6252 gst_mikey_message_unref (msg);
6253 /* and make it into base64 */
6254 data = g_bytes_get_data (bytes, &size);
6255 base64 = g_base64_encode (data, size);
6256 g_bytes_unref (bytes);
6258 result = g_strdup_printf ("prot=mikey;uri=\"%s\";data=\"%s\"",
6259 stream->conninfo.location, base64);
6266 /* Perform the SETUP request for all the streams.
6268 * We ask the server for a specific transport, which initially includes all the
6269 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
6270 * two local UDP ports that we send to the server.
6272 * Once the server replied with a transport, we configure the other streams
6273 * with the same transport.
6275 * This function will also configure the stream for the selected transport,
6276 * which basically means creating the pipeline.
6278 static GstRTSPResult
6279 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
6282 GstRTSPResult res = GST_RTSP_ERROR;
6283 GstRTSPMessage request = { 0 };
6284 GstRTSPMessage response = { 0 };
6285 GstRTSPStream *stream = NULL;
6286 GstRTSPLowerTrans protocols;
6287 GstRTSPStatusCode code;
6288 gboolean unsupported_real = FALSE;
6289 gint rtpport, rtcpport;
6293 if (src->conninfo.connection) {
6294 url = gst_rtsp_connection_get_url (src->conninfo.connection);
6295 /* we initially allow all configured lower transports. based on the URL
6296 * transports and the replies from the server we narrow them down. */
6297 protocols = url->transports & src->cur_protocols;
6300 protocols = src->cur_protocols;
6306 /* reset some state */
6307 src->free_channel = 0;
6308 src->interleaved = FALSE;
6309 src->need_activate = FALSE;
6310 /* keep track of next port number, 0 is random */
6311 src->next_port_num = src->client_port_range.min;
6312 rtpport = rtcpport = 0;
6314 if (G_UNLIKELY (src->streams == NULL))
6317 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6318 GstRTSPConnection *conn;
6325 stream = (GstRTSPStream *) walk->data;
6327 caps = stream_get_caps_for_pt (stream, stream->default_pt);
6329 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
6333 if (stream->skipped) {
6334 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
6338 /* see if we need to configure this stream */
6339 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
6340 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
6345 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
6346 stream->id, caps, &selected);
6348 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
6352 /* merge/overwrite global caps */
6357 s = gst_caps_get_structure (caps, 0);
6359 num = gst_structure_n_fields (src->props);
6360 for (j = 0; j < num; j++) {
6364 name = gst_structure_nth_field_name (src->props, j);
6365 val = gst_structure_get_value (src->props, name);
6366 gst_structure_set_value (s, name, val);
6368 GST_DEBUG_OBJECT (src, "copied %s", name);
6372 /* skip setup if we have no URL for it */
6373 if (stream->conninfo.location == NULL) {
6374 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
6378 if (src->conninfo.connection == NULL) {
6379 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
6380 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
6383 conn = stream->conninfo.connection;
6385 conn = src->conninfo.connection;
6387 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
6388 stream->conninfo.location);
6390 /* if we have a multicast connection, only suggest multicast from now on */
6391 if (stream->is_multicast)
6392 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
6395 /* first selectable protocol */
6396 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6398 if (!protocol_masks[mask])
6402 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
6403 protocol_masks[mask]);
6404 /* create a string with first transport in line */
6406 res = gst_rtspsrc_create_transports_string (src,
6407 protocols & protocol_masks[mask], stream->profile, &transports);
6408 if (res < 0 || transports == NULL)
6409 goto setup_transport_failed;
6411 if (strlen (transports) == 0) {
6412 g_free (transports);
6413 GST_DEBUG_OBJECT (src, "no transports found");
6418 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
6420 /* replace placeholders with real values, this function will optionally
6421 * allocate UDP ports and other info needed to execute the setup request */
6422 res = gst_rtspsrc_prepare_transports (stream, &transports,
6423 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
6425 g_free (transports);
6426 goto setup_transport_failed;
6429 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
6431 /* create SETUP request */
6433 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
6434 stream->conninfo.location);
6436 g_free (transports);
6437 goto create_request_failed;
6440 /* select transport */
6441 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
6444 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
6445 stream->profile == GST_RTSP_PROFILE_SAVPF) {
6446 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
6447 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
6450 /* if the user wants a non default RTP packet size we add the blocksize
6452 if (src->rtp_blocksize > 0) {
6453 hval = g_strdup_printf ("%d", src->rtp_blocksize);
6454 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
6458 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
6461 /* handle the code ourselves */
6462 res = gst_rtspsrc_send (src, conn, &request, &response, &code);
6467 case GST_RTSP_STS_OK:
6469 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
6470 gst_rtsp_message_unset (&request);
6471 gst_rtsp_message_unset (&response);
6472 /* cleanup of leftover transport */
6473 gst_rtspsrc_stream_free_udp (stream);
6474 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
6475 * we might be in this case */
6476 if (stream->container && rtpport && rtcpport && !retry) {
6477 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
6482 /* this transport did not go down well, but we may have others to try
6483 * that we did not send yet, try those and only give up then
6484 * but not without checking for lost cause/extension so we can
6485 * post a nicer/more useful error message later */
6486 if (!unsupported_real)
6487 unsupported_real = stream->is_real;
6488 /* select next available protocol, give up on this stream if none */
6490 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6492 if (!protocol_masks[mask] || unsupported_real)
6497 /* cleanup of leftover transport and move to the next stream */
6498 gst_rtspsrc_stream_free_udp (stream);
6499 goto response_error;
6502 /* parse response transport */
6504 gchar *resptrans = NULL;
6505 GstRTSPTransport transport = { 0 };
6507 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
6510 gst_rtspsrc_stream_free_udp (stream);
6514 /* parse transport, go to next stream on parse error */
6515 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6516 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6520 /* update allowed transports for other streams. once the transport of
6521 * one stream has been determined, we make sure that all other streams
6522 * are configured in the same way */
6523 switch (transport.lower_transport) {
6524 case GST_RTSP_LOWER_TRANS_TCP:
6525 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6526 protocols = GST_RTSP_LOWER_TRANS_TCP;
6527 src->interleaved = TRUE;
6528 /* update free channels */
6530 MAX (transport.interleaved.min, src->free_channel);
6532 MAX (transport.interleaved.max, src->free_channel);
6533 src->free_channel++;
6535 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6536 /* only allow multicast for other streams */
6537 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6538 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6539 /* if the server selected our ports, increment our counters so that
6540 * we select a new port later */
6541 if (src->next_port_num == transport.port.min &&
6542 src->next_port_num + 1 == transport.port.max) {
6543 src->next_port_num += 2;
6546 case GST_RTSP_LOWER_TRANS_UDP:
6547 /* only allow unicast for other streams */
6548 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6549 protocols = GST_RTSP_LOWER_TRANS_UDP;
6552 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6553 transport.lower_transport);
6557 if (!src->interleaved || !retry) {
6558 /* now configure the stream with the selected transport */
6559 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6560 GST_DEBUG_OBJECT (src,
6561 "could not configure stream %p transport, skipping stream",
6564 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
6565 /* retain the first allocated UDP port pair */
6566 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
6567 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
6570 /* we need to activate at least one streams when we detect activity */
6571 src->need_activate = TRUE;
6573 /* stream is setup now */
6574 stream->setup = TRUE;
6579 GstRTSPStream *sskip;
6581 skip = g_list_next (skip);
6585 sskip = (GstRTSPStream *) skip->data;
6587 /* skip all streams with the same control url */
6588 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
6589 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
6590 sskip, sskip->conninfo.location);
6591 sskip->skipped = TRUE;
6596 /* clean up our transport struct */
6597 gst_rtsp_transport_init (&transport);
6598 /* clean up used RTSP messages */
6599 gst_rtsp_message_unset (&request);
6600 gst_rtsp_message_unset (&response);
6604 /* store the transport protocol that was configured */
6605 src->cur_protocols = protocols;
6607 gst_rtsp_ext_list_stream_select (src->extensions, url);
6609 /* if there is nothing to activate, error out */
6610 if (!src->need_activate)
6611 goto nothing_to_activate;
6618 /* no transport possible, post an error and stop */
6619 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6620 ("Could not connect to server, no protocols left"));
6621 return GST_RTSP_ERROR;
6625 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6626 ("SDP contains no streams"));
6627 return GST_RTSP_ERROR;
6629 create_request_failed:
6631 gchar *str = gst_rtsp_strresult (res);
6633 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6634 ("Could not create request. (%s)", str));
6638 setup_transport_failed:
6640 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6641 ("Could not setup transport."));
6642 res = GST_RTSP_ERROR;
6647 const gchar *str = gst_rtsp_status_as_text (code);
6649 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6650 ("Error (%d): %s", code, GST_STR_NULL (str)));
6651 res = GST_RTSP_ERROR;
6656 gchar *str = gst_rtsp_strresult (res);
6658 if (res != GST_RTSP_EINTR) {
6659 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6660 ("Could not send message. (%s)", str));
6662 GST_WARNING_OBJECT (src, "send interrupted");
6669 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6670 ("Server did not select transport."));
6671 res = GST_RTSP_ERROR;
6674 nothing_to_activate:
6676 /* none of the available error codes is really right .. */
6677 if (unsupported_real) {
6678 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6679 (_("No supported stream was found. You might need to install a "
6680 "GStreamer RTSP extension plugin for Real media streams.")),
6683 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6684 (_("No supported stream was found. You might need to allow "
6685 "more transport protocols or may otherwise be missing "
6686 "the right GStreamer RTSP extension plugin.")), (NULL));
6688 return GST_RTSP_ERROR;
6692 gst_rtsp_message_unset (&request);
6693 gst_rtsp_message_unset (&response);
6699 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
6700 GstSegment * segment)
6703 GstRTSPTimeRange *therange;
6706 gst_rtsp_range_free (src->range);
6708 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
6709 GST_DEBUG_OBJECT (src, "parsed range %s", range);
6710 src->range = therange;
6712 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6714 gst_segment_init (segment, GST_FORMAT_TIME);
6718 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6719 therange->min.type, therange->min.seconds, therange->max.type,
6720 therange->max.seconds);
6722 if (therange->min.type == GST_RTSP_TIME_NOW)
6724 else if (therange->min.type == GST_RTSP_TIME_END)
6727 seconds = therange->min.seconds * GST_SECOND;
6729 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6730 GST_TIME_ARGS (seconds));
6732 /* we need to start playback without clipping from the position reported by
6734 segment->start = seconds;
6735 segment->position = seconds;
6737 if (therange->max.type == GST_RTSP_TIME_NOW)
6739 else if (therange->max.type == GST_RTSP_TIME_END)
6742 seconds = therange->max.seconds * GST_SECOND;
6744 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6745 GST_TIME_ARGS (seconds));
6747 /* live (WMS) server might send overflowed large max as its idea of infinity,
6748 * compensate to prevent problems later on */
6749 if (seconds != -1 && seconds < 0) {
6751 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6754 /* live (WMS) might send min == max, which is not worth recording */
6755 if (segment->duration == -1 && seconds == segment->start)
6758 /* don't change duration with unknown value, we might have a valid value
6759 * there that we want to keep. */
6761 segment->duration = seconds;
6766 /* Parse clock profived by the server with following syntax:
6768 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6771 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6773 gboolean res = FALSE;
6775 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6776 gchar **fields = NULL, **parts = NULL;
6777 gchar *remote_ip, *str;
6779 GstClockTime base_time;
6782 fields = g_strsplit (gstclock, " ", 0);
6784 /* wrapped clock, not very interesting for now */
6785 if (fields[1] == NULL)
6788 /* remote IP address and port */
6789 if ((str = fields[2]) == NULL)
6792 parts = g_strsplit (str, ":", 0);
6794 if ((remote_ip = parts[0]) == NULL)
6797 if ((str = parts[1]) == NULL)
6805 if ((str = fields[3]) == NULL)
6808 base_time = g_ascii_strtoull (str, NULL, 10);
6811 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
6814 if (src->provided_clock)
6815 gst_object_unref (src->provided_clock);
6816 src->provided_clock = netclock;
6818 gst_element_post_message (GST_ELEMENT_CAST (src),
6819 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
6820 src->provided_clock, TRUE));
6824 g_strfreev (fields);
6830 /* must be called with the RTSP state lock */
6831 static GstRTSPResult
6832 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
6838 /* prepare global stream caps properties */
6840 gst_structure_remove_all_fields (src->props);
6842 src->props = gst_structure_new_empty ("RTSPProperties");
6845 gst_sdp_message_dump (sdp);
6847 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
6849 /* let the app inspect and change the SDP */
6850 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6852 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6854 /* parse range for duration reporting. */
6859 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6863 /* keep track of the range and configure it in the segment */
6864 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6868 /* parse clock information. This is GStreamer specific, a server can tell the
6869 * client what clock it is using and wrap that in a network clock. The
6870 * advantage of that is that we can slave to it. */
6872 const gchar *gstclock;
6875 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6876 if (gstclock == NULL)
6879 /* parse the clock and expose it in the provide_clock method */
6880 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6884 /* try to find a global control attribute. Note that a '*' means that we should
6885 * do aggregate control with the current url (so we don't do anything and
6886 * leave the current connection as is) */
6888 const gchar *control;
6891 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6892 if (control == NULL)
6895 /* only take fully qualified urls */
6896 if (g_str_has_prefix (control, "rtsp://"))
6900 g_free (src->conninfo.location);
6901 src->conninfo.location = g_strdup (control);
6902 /* make a connection for this, if there was a connection already, nothing
6904 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6905 GST_ERROR_OBJECT (src, "could not connect");
6908 /* we need to keep the control url separate from the connection url because
6909 * the rules for constructing the media control url need it */
6910 g_free (src->control);
6911 src->control = g_strdup (control);
6914 /* create streams */
6915 n_streams = gst_sdp_message_medias_len (sdp);
6916 for (i = 0; i < n_streams; i++) {
6917 gst_rtspsrc_create_stream (src, sdp, i);
6920 src->state = GST_RTSP_STATE_INIT;
6923 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6926 /* reset our state */
6927 src->need_range = TRUE;
6930 src->state = GST_RTSP_STATE_READY;
6937 GST_ERROR_OBJECT (src, "setup failed");
6938 gst_rtspsrc_cleanup (src);
6943 static GstRTSPResult
6944 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6948 GstRTSPMessage request = { 0 };
6949 GstRTSPMessage response = { 0 };
6952 gchar *respcont = NULL;
6955 src->need_redirect = FALSE;
6957 /* can't continue without a valid url */
6958 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6959 res = GST_RTSP_EINVAL;
6962 src->tried_url_auth = FALSE;
6964 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6965 goto connect_failed;
6967 /* create OPTIONS */
6968 GST_DEBUG_OBJECT (src, "create options...");
6970 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
6971 src->conninfo.url_str);
6973 goto create_request_failed;
6976 GST_DEBUG_OBJECT (src, "send options...");
6979 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6982 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6987 if (!gst_rtspsrc_parse_methods (src, &response))
6990 /* create DESCRIBE */
6991 GST_DEBUG_OBJECT (src, "create describe...");
6993 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
6994 src->conninfo.url_str);
6996 goto create_request_failed;
6998 /* we only accept SDP for now */
6999 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
7003 GST_DEBUG_OBJECT (src, "send describe...");
7006 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
7009 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
7013 /* we only perform redirect for the describe, currently */
7014 if (src->need_redirect) {
7015 /* close connection, we don't have to send a TEARDOWN yet, ignore the
7017 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7019 gst_rtsp_message_unset (&request);
7020 gst_rtsp_message_unset (&response);
7026 /* it could be that the DESCRIBE method was not implemented */
7027 if (!src->methods & GST_RTSP_DESCRIBE)
7030 /* check if reply is SDP */
7031 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
7033 /* could not be set but since the request returned OK, we assume it
7034 * was SDP, else check it. */
7036 if (g_ascii_strcasecmp (respcont, "application/sdp") != 0)
7037 goto wrong_content_type;
7040 /* get message body and parse as SDP */
7041 gst_rtsp_message_get_body (&response, &data, &size);
7042 if (data == NULL || size == 0)
7045 GST_DEBUG_OBJECT (src, "parse SDP...");
7046 gst_sdp_message_new (sdp);
7047 gst_sdp_message_parse_buffer (data, size, *sdp);
7049 /* clean up any messages */
7050 gst_rtsp_message_unset (&request);
7051 gst_rtsp_message_unset (&response);
7058 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
7059 ("No valid RTSP URL was provided"));
7064 gchar *str = gst_rtsp_strresult (res);
7066 if (res != GST_RTSP_EINTR) {
7067 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
7068 ("Failed to connect. (%s)", str));
7070 GST_WARNING_OBJECT (src, "connect interrupted");
7075 create_request_failed:
7077 gchar *str = gst_rtsp_strresult (res);
7079 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7080 ("Could not create request. (%s)", str));
7086 /* Don't post a message - the rtsp_send method will have
7087 * taken care of it because we passed NULL for the response code */
7092 /* error was posted */
7093 res = GST_RTSP_ERROR;
7098 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7099 ("Server does not support SDP, got %s.", respcont));
7100 res = GST_RTSP_ERROR;
7105 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7106 ("Server can not provide an SDP."));
7107 res = GST_RTSP_ERROR;
7112 if (src->conninfo.connection) {
7113 GST_DEBUG_OBJECT (src, "free connection");
7114 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7116 gst_rtsp_message_unset (&request);
7117 gst_rtsp_message_unset (&response);
7122 static GstRTSPResult
7123 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
7128 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
7130 if (src->sdp == NULL) {
7131 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
7135 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
7140 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
7147 GST_WARNING_OBJECT (src, "can't get sdp");
7148 src->open_error = TRUE;
7153 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
7154 src->open_error = TRUE;
7159 static GstRTSPResult
7160 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
7162 GstRTSPMessage request = { 0 };
7163 GstRTSPMessage response = { 0 };
7164 GstRTSPResult res = GST_RTSP_OK;
7166 const gchar *control;
7168 GST_DEBUG_OBJECT (src, "TEARDOWN...");
7170 gst_rtspsrc_set_state (src, GST_STATE_READY);
7172 if (src->state < GST_RTSP_STATE_READY) {
7173 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
7180 /* construct a control url */
7181 control = get_aggregate_control (src);
7183 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
7186 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7187 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7188 const gchar *setup_url;
7189 GstRTSPConnInfo *info;
7191 /* try aggregate control first but do non-aggregate control otherwise */
7193 setup_url = control;
7194 else if ((setup_url = stream->conninfo.location) == NULL)
7197 if (src->conninfo.connection) {
7198 info = &src->conninfo;
7199 } else if (stream->conninfo.connection) {
7200 info = &stream->conninfo;
7204 if (!info->connected)
7209 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
7211 goto create_request_failed;
7214 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
7217 gst_rtspsrc_send (src, info->connection, &request, &response,
7221 /* FIXME, parse result? */
7222 gst_rtsp_message_unset (&request);
7223 gst_rtsp_message_unset (&response);
7226 /* early exit when we did aggregate control */
7232 /* close connections */
7233 GST_DEBUG_OBJECT (src, "closing connection...");
7234 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7235 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7236 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7237 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
7241 gst_rtspsrc_cleanup (src);
7243 src->state = GST_RTSP_STATE_INVALID;
7246 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
7251 create_request_failed:
7253 gchar *str = gst_rtsp_strresult (res);
7255 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7256 ("Could not create request. (%s)", str));
7262 gchar *str = gst_rtsp_strresult (res);
7264 gst_rtsp_message_unset (&request);
7265 if (res != GST_RTSP_EINTR) {
7266 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7267 ("Could not send message. (%s)", str));
7269 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
7276 GST_DEBUG_OBJECT (src,
7277 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
7282 /* RTP-Info is of the format:
7284 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
7286 * rtptime corresponds to the timestamp for the NPT time given in the header
7287 * seqbase corresponds to the next sequence number we received. This number
7288 * indicates the first seqnum after the seek and should be used to discard
7289 * packets that are from before the seek.
7292 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
7297 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
7299 infos = g_strsplit (rtpinfo, ",", 0);
7300 for (i = 0; infos[i]; i++) {
7302 GstRTSPStream *stream;
7306 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
7308 /* init values, types of seqbase and timebase are bigger than needed so we
7309 * can store -1 as uninitialized values */
7314 /* parse url, find stream for url.
7315 * parse seq and rtptime. The seq number should be configured in the rtp
7316 * depayloader or session manager to detect gaps. Same for the rtptime, it
7317 * should be used to create an initial time newsegment. */
7318 fields = g_strsplit (infos[i], ";", 0);
7319 for (j = 0; fields[j]; j++) {
7320 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
7321 /* remove leading whitespace */
7322 fields[j] = g_strchug (fields[j]);
7323 if (g_str_has_prefix (fields[j], "url=")) {
7324 /* get the url and the stream */
7326 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
7327 } else if (g_str_has_prefix (fields[j], "seq=")) {
7328 seqbase = atoi (fields[j] + 4);
7329 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
7330 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
7333 g_strfreev (fields);
7334 /* now we need to store the values for the caps of the stream */
7335 if (stream != NULL) {
7336 GST_DEBUG_OBJECT (src,
7337 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
7338 stream, seqbase, timebase);
7340 /* we have a stream, configure detected params */
7341 stream->seqbase = seqbase;
7342 stream->timebase = timebase;
7351 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
7356 interval = strtoul (rtcp, NULL, 10);
7357 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
7362 interval *= GST_MSECOND;
7364 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7365 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7367 /* already (optionally) retrieved this when configuring manager */
7368 if (stream->session) {
7369 GObject *rtpsession = stream->session;
7371 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
7373 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
7377 /* now it happens that (Xenon) server sending this may also provide bogus
7378 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
7379 * and just use RTP-Info to sync */
7381 GObjectClass *klass;
7383 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
7384 if (g_object_class_find_property (klass, "rtcp-sync")) {
7385 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
7386 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
7392 gst_rtspsrc_get_float (const gchar * dstr)
7394 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7396 /* canonicalise floating point string so we can handle float strings
7397 * in the form "24.930" or "24,930" irrespective of the current locale */
7398 g_strlcpy (s, dstr, sizeof (s));
7399 g_strdelimit (s, ",", '.');
7400 return g_ascii_strtod (s, NULL);
7404 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
7406 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7408 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
7409 g_strlcpy (val_str, "now", sizeof (val_str));
7411 if (segment->position == 0) {
7412 g_strlcpy (val_str, "0", sizeof (val_str));
7414 g_ascii_dtostr (val_str, sizeof (val_str),
7415 ((gdouble) segment->position) / GST_SECOND);
7418 return g_strdup_printf ("npt=%s-", val_str);
7422 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
7426 stream->timebase = -1;
7427 stream->seqbase = -1;
7429 len = stream->ptmap->len;
7430 for (i = 0; i < len; i++) {
7431 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
7434 if (item->caps == NULL)
7437 item->caps = gst_caps_make_writable (item->caps);
7438 s = gst_caps_get_structure (item->caps, 0);
7439 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
7443 static GstRTSPResult
7444 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
7446 GstRTSPResult res = GST_RTSP_OK;
7448 if (src->state < GST_RTSP_STATE_READY) {
7449 res = GST_RTSP_ERROR;
7450 if (src->open_error) {
7451 GST_DEBUG_OBJECT (src, "the stream was in error");
7455 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
7457 if ((res = gst_rtspsrc_open (src, async)) < 0) {
7458 GST_DEBUG_OBJECT (src, "failed to open stream");
7467 static GstRTSPResult
7468 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
7470 GstRTSPMessage request = { 0 };
7471 GstRTSPMessage response = { 0 };
7472 GstRTSPResult res = GST_RTSP_OK;
7476 const gchar *control;
7478 GST_DEBUG_OBJECT (src, "PLAY...");
7480 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7483 if (!(src->methods & GST_RTSP_PLAY))
7486 if (src->state == GST_RTSP_STATE_PLAYING)
7489 if (!src->conninfo.connection || !src->conninfo.connected)
7492 /* send some dummy packets before we activate the receive in the
7494 gst_rtspsrc_send_dummy_packets (src);
7496 /* require new SR packets */
7498 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
7500 /* construct a control url */
7501 control = get_aggregate_control (src);
7503 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7504 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7505 const gchar *setup_url;
7506 GstRTSPConnection *conn;
7508 /* try aggregate control first but do non-aggregate control otherwise */
7510 setup_url = control;
7511 else if ((setup_url = stream->conninfo.location) == NULL)
7514 if (src->conninfo.connection) {
7515 conn = src->conninfo.connection;
7516 } else if (stream->conninfo.connection) {
7517 conn = stream->conninfo.connection;
7523 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
7525 goto create_request_failed;
7527 if (src->need_range) {
7528 hval = gen_range_header (src, segment);
7530 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
7532 /* store the newsegment event so it can be sent from the streaming thread. */
7533 src->need_segment = TRUE;
7536 if (segment->rate != 1.0) {
7537 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
7539 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
7541 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
7543 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
7547 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
7549 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7552 /* seek may have silently failed as it is not supported */
7553 if (!(src->methods & GST_RTSP_PLAY)) {
7554 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
7555 /* obviously it is supported as we made it here */
7556 src->methods |= GST_RTSP_PLAY;
7557 src->seekable = FALSE;
7558 /* but there is nothing to parse in the response,
7559 * so convey we have no idea and not to expect anything particular */
7560 clear_rtp_base (src, stream);
7564 /* need to do for all streams */
7565 for (run = src->streams; run; run = g_list_next (run))
7566 clear_rtp_base (src, (GstRTSPStream *) run->data);
7568 /* NOTE the above also disables npt based eos detection */
7569 /* and below forces position to 0,
7570 * which is visible feedback we lost the plot */
7571 segment->start = segment->position = src->last_pos;
7574 gst_rtsp_message_unset (&request);
7576 /* parse RTP npt field. This is the current position in the stream (Normal
7577 * Play Time) and should be put in the NEWSEGMENT position field. */
7578 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
7580 gst_rtspsrc_parse_range (src, hval, segment);
7582 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
7583 segment->rate = 1.0;
7585 /* parse Speed header. This is the intended playback rate of the stream
7586 * and should be put in the NEWSEGMENT rate field. */
7587 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
7588 0) == GST_RTSP_OK) {
7589 segment->rate = gst_rtspsrc_get_float (hval);
7590 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
7591 &hval, 0) == GST_RTSP_OK) {
7592 segment->rate = gst_rtspsrc_get_float (hval);
7595 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
7596 * for the RTP packets. If this is not present, we assume all starts from 0...
7597 * This is info for the RTP session manager that we pass to it in caps. */
7599 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
7600 &hval, hval_idx++) == GST_RTSP_OK)
7601 gst_rtspsrc_parse_rtpinfo (src, hval);
7603 /* some servers indicate RTCP parameters in PLAY response,
7604 * rather than properly in SDP */
7605 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
7606 &hval, 0) == GST_RTSP_OK)
7607 gst_rtspsrc_handle_rtcp_interval (src, hval);
7609 gst_rtsp_message_unset (&response);
7611 /* early exit when we did aggregate control */
7615 /* configure the caps of the streams after we parsed all headers. Only reset
7616 * the manager object when we set a new Range header (we did a seek) */
7617 gst_rtspsrc_configure_caps (src, segment, src->need_range);
7619 /* set to PLAYING after we have configured the caps, otherwise we
7620 * might end up calling request_key (with SRTP) while caps are still
7621 * being configured. */
7622 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
7624 /* set again when needed */
7625 src->need_range = FALSE;
7627 src->running = TRUE;
7628 src->base_time = -1;
7629 src->state = GST_RTSP_STATE_PLAYING;
7632 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
7633 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7634 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7635 stream->discont = TRUE;
7640 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
7647 GST_DEBUG_OBJECT (src, "failed to open stream");
7652 GST_DEBUG_OBJECT (src, "PLAY is not supported");
7657 GST_DEBUG_OBJECT (src, "we were already PLAYING");
7660 create_request_failed:
7662 gchar *str = gst_rtsp_strresult (res);
7664 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7665 ("Could not create request. (%s)", str));
7671 gchar *str = gst_rtsp_strresult (res);
7673 gst_rtsp_message_unset (&request);
7674 if (res != GST_RTSP_EINTR) {
7675 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7676 ("Could not send message. (%s)", str));
7678 GST_WARNING_OBJECT (src, "PLAY interrupted");
7685 static GstRTSPResult
7686 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
7688 GstRTSPResult res = GST_RTSP_OK;
7689 GstRTSPMessage request = { 0 };
7690 GstRTSPMessage response = { 0 };
7692 const gchar *control;
7694 GST_DEBUG_OBJECT (src, "PAUSE...");
7696 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7699 if (!(src->methods & GST_RTSP_PAUSE))
7702 if (src->state == GST_RTSP_STATE_READY)
7705 if (!src->conninfo.connection || !src->conninfo.connected)
7708 /* construct a control url */
7709 control = get_aggregate_control (src);
7711 /* loop over the streams. We might exit the loop early when we could do an
7712 * aggregate control */
7713 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7714 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7715 GstRTSPConnection *conn;
7716 const gchar *setup_url;
7718 /* try aggregate control first but do non-aggregate control otherwise */
7720 setup_url = control;
7721 else if ((setup_url = stream->conninfo.location) == NULL)
7724 if (src->conninfo.connection) {
7725 conn = src->conninfo.connection;
7726 } else if (stream->conninfo.connection) {
7727 conn = stream->conninfo.connection;
7733 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7734 ("Sending PAUSE request"));
7737 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
7739 goto create_request_failed;
7741 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7744 gst_rtsp_message_unset (&request);
7745 gst_rtsp_message_unset (&response);
7747 /* exit early when we did agregate control */
7752 /* change element states now */
7753 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7756 src->state = GST_RTSP_STATE_READY;
7760 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7767 GST_DEBUG_OBJECT (src, "failed to open stream");
7772 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7777 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7780 create_request_failed:
7782 gchar *str = gst_rtsp_strresult (res);
7784 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7785 ("Could not create request. (%s)", str));
7791 gchar *str = gst_rtsp_strresult (res);
7793 gst_rtsp_message_unset (&request);
7794 if (res != GST_RTSP_EINTR) {
7795 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7796 ("Could not send message. (%s)", str));
7798 GST_WARNING_OBJECT (src, "PAUSE interrupted");
7806 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
7808 GstRTSPSrc *rtspsrc;
7810 rtspsrc = GST_RTSPSRC (bin);
7812 switch (GST_MESSAGE_TYPE (message)) {
7813 case GST_MESSAGE_EOS:
7814 gst_message_unref (message);
7816 case GST_MESSAGE_ELEMENT:
7818 const GstStructure *s = gst_message_get_structure (message);
7820 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
7821 gboolean ignore_timeout;
7823 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
7825 GST_OBJECT_LOCK (rtspsrc);
7826 ignore_timeout = rtspsrc->ignore_timeout;
7827 rtspsrc->ignore_timeout = TRUE;
7828 GST_OBJECT_UNLOCK (rtspsrc);
7830 /* we only act on the first udp timeout message, others are irrelevant
7831 * and can be ignored. */
7832 if (!ignore_timeout)
7833 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
7835 gst_message_unref (message);
7838 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7841 case GST_MESSAGE_ERROR:
7844 GstRTSPStream *stream;
7847 udpsrc = GST_MESSAGE_SRC (message);
7849 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7850 GST_ELEMENT_NAME (udpsrc));
7852 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7856 /* we ignore the RTCP udpsrc */
7857 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7860 /* if we get error messages from the udp sources, that's not a problem as
7861 * long as not all of them error out. We also don't really know what the
7862 * problem is, the message does not give enough detail... */
7863 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7864 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7865 if (ret != GST_FLOW_OK)
7869 gst_message_unref (message);
7873 /* fatal but not our message, forward */
7874 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7879 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7885 /* the thread where everything happens */
7887 gst_rtspsrc_thread (GstRTSPSrc * src)
7891 GST_OBJECT_LOCK (src);
7892 cmd = src->pending_cmd;
7893 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7894 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7895 src->pending_cmd = CMD_LOOP;
7897 src->pending_cmd = CMD_WAIT;
7898 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
7900 /* we got the message command, so ensure communication is possible again */
7901 gst_rtspsrc_connection_flush (src, FALSE);
7903 src->busy_cmd = cmd;
7904 GST_OBJECT_UNLOCK (src);
7908 gst_rtspsrc_open (src, TRUE);
7911 gst_rtspsrc_play (src, &src->segment, TRUE);
7914 gst_rtspsrc_pause (src, TRUE);
7917 gst_rtspsrc_close (src, TRUE, FALSE);
7920 gst_rtspsrc_loop (src);
7923 gst_rtspsrc_reconnect (src, FALSE);
7929 GST_OBJECT_LOCK (src);
7930 /* and go back to sleep */
7931 if (src->pending_cmd == CMD_WAIT) {
7933 gst_task_pause (src->task);
7936 src->busy_cmd = CMD_WAIT;
7937 GST_OBJECT_UNLOCK (src);
7941 gst_rtspsrc_start (GstRTSPSrc * src)
7943 GST_DEBUG_OBJECT (src, "starting");
7945 GST_OBJECT_LOCK (src);
7947 src->pending_cmd = CMD_WAIT;
7949 if (src->task == NULL) {
7950 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7951 if (src->task == NULL)
7954 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7956 GST_OBJECT_UNLOCK (src);
7963 GST_OBJECT_UNLOCK (src);
7964 GST_ERROR_OBJECT (src, "failed to create task");
7970 gst_rtspsrc_stop (GstRTSPSrc * src)
7974 GST_DEBUG_OBJECT (src, "stopping");
7976 /* also cancels pending task */
7977 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7979 GST_OBJECT_LOCK (src);
7980 if ((task = src->task)) {
7982 GST_OBJECT_UNLOCK (src);
7984 gst_task_stop (task);
7986 /* make sure it is not running */
7987 GST_RTSP_STREAM_LOCK (src);
7988 GST_RTSP_STREAM_UNLOCK (src);
7990 /* now wait for the task to finish */
7991 gst_task_join (task);
7993 /* and free the task */
7994 gst_object_unref (GST_OBJECT (task));
7996 GST_OBJECT_LOCK (src);
7998 GST_OBJECT_UNLOCK (src);
8000 /* ensure synchronously all is closed and clean */
8001 gst_rtspsrc_close (src, FALSE, TRUE);
8006 static GstStateChangeReturn
8007 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
8009 GstRTSPSrc *rtspsrc;
8010 GstStateChangeReturn ret;
8012 rtspsrc = GST_RTSPSRC (element);
8014 switch (transition) {
8015 case GST_STATE_CHANGE_NULL_TO_READY:
8016 if (!gst_rtspsrc_start (rtspsrc))
8019 case GST_STATE_CHANGE_READY_TO_PAUSED:
8020 /* init some state */
8021 rtspsrc->cur_protocols = rtspsrc->protocols;
8022 /* first attempt, don't ignore timeouts */
8023 rtspsrc->ignore_timeout = FALSE;
8024 rtspsrc->open_error = FALSE;
8025 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
8027 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8028 set_manager_buffer_mode (rtspsrc);
8030 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8031 /* unblock the tcp tasks and make the loop waiting */
8032 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
8033 /* make sure it is waiting before we send PAUSE or PLAY below */
8034 GST_RTSP_STREAM_LOCK (rtspsrc);
8035 GST_RTSP_STREAM_UNLOCK (rtspsrc);
8038 case GST_STATE_CHANGE_PAUSED_TO_READY:
8044 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
8045 if (ret == GST_STATE_CHANGE_FAILURE)
8048 switch (transition) {
8049 case GST_STATE_CHANGE_NULL_TO_READY:
8050 ret = GST_STATE_CHANGE_SUCCESS;
8052 case GST_STATE_CHANGE_READY_TO_PAUSED:
8053 ret = GST_STATE_CHANGE_NO_PREROLL;
8055 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8056 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
8057 ret = GST_STATE_CHANGE_SUCCESS;
8059 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8060 /* send pause request and keep the idle task around */
8061 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
8062 ret = GST_STATE_CHANGE_NO_PREROLL;
8064 case GST_STATE_CHANGE_PAUSED_TO_READY:
8065 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
8066 ret = GST_STATE_CHANGE_SUCCESS;
8068 case GST_STATE_CHANGE_READY_TO_NULL:
8069 gst_rtspsrc_stop (rtspsrc);
8070 ret = GST_STATE_CHANGE_SUCCESS;
8081 GST_DEBUG_OBJECT (rtspsrc, "start failed");
8082 return GST_STATE_CHANGE_FAILURE;
8087 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
8090 GstRTSPSrc *rtspsrc;
8092 rtspsrc = GST_RTSPSRC (element);
8094 if (GST_EVENT_IS_DOWNSTREAM (event)) {
8095 res = gst_rtspsrc_push_event (rtspsrc, event);
8097 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
8104 /*** GSTURIHANDLER INTERFACE *************************************************/
8107 gst_rtspsrc_uri_get_type (GType type)
8112 static const gchar *const *
8113 gst_rtspsrc_uri_get_protocols (GType type)
8115 static const gchar *protocols[] =
8116 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
8117 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
8124 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
8126 GstRTSPSrc *src = GST_RTSPSRC (handler);
8128 /* FIXME: make thread-safe */
8129 return g_strdup (src->conninfo.location);
8133 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
8139 GstRTSPUrl *newurl = NULL;
8140 GstSDPMessage *sdp = NULL;
8142 src = GST_RTSPSRC (handler);
8144 /* same URI, we're fine */
8145 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
8148 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
8149 sres = gst_sdp_message_new (&sdp);
8153 GST_DEBUG_OBJECT (src, "parsing SDP message");
8154 sres = gst_sdp_message_parse_uri (uri, sdp);
8159 GST_DEBUG_OBJECT (src, "parsing URI");
8160 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
8164 /* if worked, free previous and store new url object along with the original
8166 GST_DEBUG_OBJECT (src, "configuring URI");
8167 g_free (src->conninfo.location);
8168 src->conninfo.location = g_strdup (uri);
8169 gst_rtsp_url_free (src->conninfo.url);
8170 src->conninfo.url = newurl;
8171 g_free (src->conninfo.url_str);
8173 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
8175 src->conninfo.url_str = NULL;
8178 gst_sdp_message_free (src->sdp);
8180 src->from_sdp = sdp != NULL;
8182 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
8183 GST_DEBUG_OBJECT (src, "request uri is: %s",
8184 GST_STR_NULL (src->conninfo.url_str));
8191 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
8196 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
8197 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8198 "Could not create SDP");
8203 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
8204 GST_STR_NULL (uri));
8205 gst_sdp_message_free (sdp);
8206 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8212 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
8213 GST_STR_NULL (uri), res);
8214 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8215 "Invalid RTSP URI");
8221 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
8223 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
8225 iface->get_type = gst_rtspsrc_uri_get_type;
8226 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
8227 iface->get_uri = gst_rtspsrc_uri_get_uri;
8228 iface->set_uri = gst_rtspsrc_uri_set_uri;