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
238 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
240 gst_rtsp_nat_method_get_type (void)
242 static GType rtsp_nat_method_type = 0;
243 static const GEnumValue rtsp_nat_method[] = {
244 {GST_RTSP_NAT_NONE, "None", "none"},
245 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
249 if (!rtsp_nat_method_type) {
250 rtsp_nat_method_type =
251 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
253 return rtsp_nat_method_type;
256 static void gst_rtspsrc_finalize (GObject * object);
258 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
259 const GValue * value, GParamSpec * pspec);
260 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
261 GValue * value, GParamSpec * pspec);
263 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
265 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
266 gpointer iface_data);
268 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
271 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
272 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
274 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
276 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
277 GstStateChange transition);
278 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
279 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
281 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
282 GstRTSPMessage * response);
284 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
286 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
287 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
289 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
290 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
292 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
293 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
294 gboolean only_close);
296 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
297 const gchar * uri, GError ** error);
298 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
300 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
301 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
302 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
303 GstRTSPStream * stream, GstEvent * event);
304 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
305 static void gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush);
313 /* commands we send to out loop to notify it of events */
314 #define CMD_OPEN (1 << 0)
315 #define CMD_PLAY (1 << 1)
316 #define CMD_PAUSE (1 << 2)
317 #define CMD_CLOSE (1 << 3)
318 #define CMD_WAIT (1 << 4)
319 #define CMD_RECONNECT (1 << 5)
320 #define CMD_LOOP (1 << 6)
322 /* mask for all commands */
323 #define CMD_ALL ((CMD_LOOP << 1) - 1)
325 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
327 gchar *__txt = _gst_element_error_printf text; \
328 gst_element_post_message (GST_ELEMENT_CAST (el), \
329 gst_message_new_progress (GST_OBJECT_CAST (el), \
330 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
334 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
336 #define gst_rtspsrc_parent_class parent_class
337 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
338 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
340 #ifndef GST_DISABLE_GST_DEBUG
341 static inline const char *
342 cmd_to_string (guint cmd)
366 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
368 GST_DEBUG_OBJECT (src, "default handler");
373 select_stream_accum (GSignalInvocationHint * ihint,
374 GValue * return_accu, const GValue * handler_return, gpointer data)
378 myboolean = g_value_get_boolean (handler_return);
379 GST_DEBUG ("accum %d", myboolean);
380 g_value_set_boolean (return_accu, myboolean);
382 /* stop emission if FALSE */
387 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
389 GObjectClass *gobject_class;
390 GstElementClass *gstelement_class;
391 GstBinClass *gstbin_class;
393 gobject_class = (GObjectClass *) klass;
394 gstelement_class = (GstElementClass *) klass;
395 gstbin_class = (GstBinClass *) klass;
397 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
399 gobject_class->set_property = gst_rtspsrc_set_property;
400 gobject_class->get_property = gst_rtspsrc_get_property;
402 gobject_class->finalize = gst_rtspsrc_finalize;
404 g_object_class_install_property (gobject_class, PROP_LOCATION,
405 g_param_spec_string ("location", "RTSP Location",
406 "Location of the RTSP url to read",
407 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
409 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
410 g_param_spec_flags ("protocols", "Protocols",
411 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
412 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
414 g_object_class_install_property (gobject_class, PROP_DEBUG,
415 g_param_spec_boolean ("debug", "Debug",
416 "Dump request and response messages to stdout",
417 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
419 g_object_class_install_property (gobject_class, PROP_RETRY,
420 g_param_spec_uint ("retry", "Retry",
421 "Max number of retries when allocating RTP ports.",
422 0, G_MAXUINT16, DEFAULT_RETRY,
423 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
425 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
426 g_param_spec_uint64 ("timeout", "Timeout",
427 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
428 0, G_MAXUINT64, DEFAULT_TIMEOUT,
429 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
431 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
432 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
433 "Fail after timeout microseconds on TCP connections (0 = disabled)",
434 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
435 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
437 g_object_class_install_property (gobject_class, PROP_LATENCY,
438 g_param_spec_uint ("latency", "Buffer latency in ms",
439 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
440 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
442 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
443 g_param_spec_boolean ("drop-on-latency",
444 "Drop buffers when maximum latency is reached",
445 "Tells the jitterbuffer to never exceed the given latency in size",
446 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
448 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
449 g_param_spec_uint64 ("connection-speed", "Connection Speed",
450 "Network connection speed in kbps (0 = unknown)",
451 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
452 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
454 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
455 g_param_spec_enum ("nat-method", "NAT Method",
456 "Method to use for traversing firewalls and NAT",
457 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
458 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
461 * GstRTSPSrc:do-rtcp:
463 * Enable RTCP support. Some old server don't like RTCP and then this property
464 * needs to be set to FALSE.
466 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
467 g_param_spec_boolean ("do-rtcp", "Do RTCP",
468 "Send RTCP packets, disable for old incompatible server.",
469 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
472 * GstRTSPSrc:do-rtsp-keep-alive:
474 * Enable RTSP keep alive support. Some old server don't like RTSP
475 * keep alive and then this property needs to be set to FALSE.
477 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
478 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
479 "Send RTSP keep alive packets, disable for old incompatible server.",
480 DEFAULT_DO_RTSP_KEEP_ALIVE,
481 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
486 * Set the proxy parameters. This has to be a string of the format
487 * [http://][user:passwd@]host[:port].
489 g_object_class_install_property (gobject_class, PROP_PROXY,
490 g_param_spec_string ("proxy", "Proxy",
491 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
492 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
494 * GstRTSPSrc:proxy-id:
496 * Sets the proxy URI user id for authentication. If the URI set via the
497 * "proxy" property contains a user-id already, that will take precedence.
501 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
502 g_param_spec_string ("proxy-id", "proxy-id",
503 "HTTP proxy URI user id for authentication", "",
504 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
506 * GstRTSPSrc:proxy-pw:
508 * Sets the proxy URI password for authentication. If the URI set via the
509 * "proxy" property contains a password already, that will take precedence.
513 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
514 g_param_spec_string ("proxy-pw", "proxy-pw",
515 "HTTP proxy URI user password for authentication", "",
516 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
519 * GstRTSPSrc:rtp-blocksize:
521 * RTP package size to suggest to server.
523 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
524 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
525 "RTP package size to suggest to server (0 = disabled)",
526 0, 65536, DEFAULT_RTP_BLOCKSIZE,
527 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
529 g_object_class_install_property (gobject_class,
531 g_param_spec_string ("user-id", "user-id",
532 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
533 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
534 g_object_class_install_property (gobject_class, PROP_USER_PW,
535 g_param_spec_string ("user-pw", "user-pw",
536 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
537 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
540 * GstRTSPSrc:buffer-mode:
542 * Control the buffering and timestamping mode used by the jitterbuffer.
544 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
545 g_param_spec_enum ("buffer-mode", "Buffer Mode",
546 "Control the buffering algorithm in use",
547 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
548 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
551 * GstRTSPSrc:port-range:
553 * Configure the client port numbers that can be used to recieve RTP and
556 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
557 g_param_spec_string ("port-range", "Port range",
558 "Client port range that can be used to receive RTP and RTCP data, "
559 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
560 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
563 * GstRTSPSrc:udp-buffer-size:
565 * Size of the kernel UDP receive buffer in bytes.
567 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
568 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
569 "Size of the kernel UDP receive buffer in bytes, 0=default",
570 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
571 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
574 * GstRTSPSrc:short-header:
576 * Only send the basic RTSP headers for broken encoders.
578 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
579 g_param_spec_boolean ("short-header", "Short Header",
580 "Only send the basic RTSP headers for broken encoders",
581 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
583 g_object_class_install_property (gobject_class, PROP_PROBATION,
584 g_param_spec_uint ("probation", "Number of probations",
585 "Consecutive packet sequence numbers to accept the source",
586 0, G_MAXUINT, DEFAULT_PROBATION,
587 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
589 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
590 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
591 "Reconnect to the server if RTSP connection is closed when doing UDP",
592 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
594 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
595 g_param_spec_string ("multicast-iface", "Multicast Interface",
596 "The network interface on which to join the multicast group",
597 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
599 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
600 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
601 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
602 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
604 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
605 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
606 "Use the pipeline running-time to set the NTP time in the RTCP SR messages",
607 DEFAULT_USE_PIPELINE_CLOCK,
608 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
610 g_object_class_install_property (gobject_class, PROP_SDES,
611 g_param_spec_boxed ("sdes", "SDES",
612 "The SDES items of this session",
613 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
616 * GstRTSPSrc::tls-validation-flags:
618 * TLS certificate validation flags used to validate server
623 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
624 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
625 "TLS certificate validation flags used to validate the server certificate",
626 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
627 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
630 * GstRTSPSrc::tls-database:
632 * TLS database with anchor certificate authorities used to validate
633 * the server certificate.
637 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
638 g_param_spec_object ("tls-database", "TLS database",
639 "TLS database with anchor certificate authorities used to validate the server certificate",
640 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
643 * GstRTSPSrc::do-retransmission:
645 * Attempt to ask the server to retransmit lost packets according to RFC4588.
647 * Note: currently only works with SSRC-multiplexed retransmission streams
651 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
652 g_param_spec_boolean ("do-retransmission", "Retransmission",
653 "Ask the server to retransmit lost packets",
654 DEFAULT_DO_RETRANSMISSION,
655 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
658 * GstRTSPSrc::handle-request:
659 * @rtspsrc: a #GstRTSPSrc
660 * @request: a #GstRTSPMessage
661 * @response: a #GstRTSPMessage
663 * Handle a server request in @request and prepare @response.
665 * This signal is called from the streaming thread, you should therefore not
666 * do any state changes on @rtspsrc because this might deadlock. If you want
667 * to modify the state as a result of this signal, post a
668 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
673 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
674 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
675 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
676 G_TYPE_POINTER, G_TYPE_POINTER);
679 * GstRTSPSrc::on-sdp:
680 * @rtspsrc: a #GstRTSPSrc
681 * @sdp: a #GstSDPMessage
683 * Emited when the client has retrieved the SDP and before it configures the
684 * streams in the SDP. @sdp can be inspected and modified.
686 * This signal is called from the streaming thread, you should therefore not
687 * do any state changes on @rtspsrc because this might deadlock. If you want
688 * to modify the state as a result of this signal, post a
689 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
694 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
695 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
696 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
697 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
700 * GstRTSPSrc::select-stream:
701 * @rtspsrc: a #GstRTSPSrc
702 * @num: the stream number
703 * @caps: the stream caps
705 * Emited before the client decides to configure the stream @num with
708 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
713 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
714 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
715 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
716 (GCallback) default_select_stream, select_stream_accum, NULL,
717 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
720 * GstRTSPSrc::new-manager:
721 * @rtspsrc: a #GstRTSPSrc
722 * @manager: a #GstElement
724 * Emited after a new manager (like rtpbin) was created and the default
725 * properties were configured.
729 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
730 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
731 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
732 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
735 * GstRTSPSrc::request-rtcp-key:
736 * @rtspsrc: a #GstRTSPSrc
737 * @num: the stream number
739 * Signal emited to get the crypto parameters relevant to the RTCP
740 * stream. User should provide the key and the RTCP encryption ciphers
741 * and authentication, and return them wrapped in a GstCaps.
745 gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY] =
746 g_signal_new ("request-rtcp-key", G_TYPE_FROM_CLASS (klass),
747 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, GST_TYPE_CAPS, 1, G_TYPE_UINT);
749 gstelement_class->send_event = gst_rtspsrc_send_event;
750 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
751 gstelement_class->change_state = gst_rtspsrc_change_state;
753 gst_element_class_add_pad_template (gstelement_class,
754 gst_static_pad_template_get (&rtptemplate));
756 gst_element_class_set_static_metadata (gstelement_class,
757 "RTSP packet receiver", "Source/Network",
758 "Receive data over the network via RTSP (RFC 2326)",
759 "Wim Taymans <wim@fluendo.com>, "
760 "Thijs Vermeir <thijs.vermeir@barco.com>, "
761 "Lutz Mueller <lutz@topfrose.de>");
763 gstbin_class->handle_message = gst_rtspsrc_handle_message;
765 gst_rtsp_ext_list_init ();
769 gst_rtspsrc_init (GstRTSPSrc * src)
771 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
772 src->protocols = DEFAULT_PROTOCOLS;
773 src->debug = DEFAULT_DEBUG;
774 src->retry = DEFAULT_RETRY;
775 src->udp_timeout = DEFAULT_TIMEOUT;
776 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
777 src->latency = DEFAULT_LATENCY_MS;
778 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
779 src->connection_speed = DEFAULT_CONNECTION_SPEED;
780 src->nat_method = DEFAULT_NAT_METHOD;
781 src->do_rtcp = DEFAULT_DO_RTCP;
782 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
783 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
784 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
785 src->user_id = g_strdup (DEFAULT_USER_ID);
786 src->user_pw = g_strdup (DEFAULT_USER_PW);
787 src->buffer_mode = DEFAULT_BUFFER_MODE;
788 src->client_port_range.min = 0;
789 src->client_port_range.max = 0;
790 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
791 src->short_header = DEFAULT_SHORT_HEADER;
792 src->probation = DEFAULT_PROBATION;
793 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
794 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
795 src->ntp_sync = DEFAULT_NTP_SYNC;
796 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
798 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
799 src->tls_database = DEFAULT_TLS_DATABASE;
800 src->do_retransmission = DEFAULT_DO_RETRANSMISSION;
802 /* get a list of all extensions */
803 src->extensions = gst_rtsp_ext_list_get ();
805 /* connect to send signal */
806 gst_rtsp_ext_list_connect (src->extensions, "send",
807 (GCallback) gst_rtspsrc_send_cb, src);
809 /* protects the streaming thread in interleaved mode or the polling
810 * thread in UDP mode. */
811 g_rec_mutex_init (&src->stream_rec_lock);
813 /* protects our state changes from multiple invocations */
814 g_rec_mutex_init (&src->state_rec_lock);
816 src->state = GST_RTSP_STATE_INVALID;
818 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
822 gst_rtspsrc_finalize (GObject * object)
826 rtspsrc = GST_RTSPSRC (object);
828 gst_rtsp_ext_list_free (rtspsrc->extensions);
829 g_free (rtspsrc->conninfo.location);
830 gst_rtsp_url_free (rtspsrc->conninfo.url);
831 g_free (rtspsrc->conninfo.url_str);
832 g_free (rtspsrc->user_id);
833 g_free (rtspsrc->user_pw);
834 g_free (rtspsrc->multi_iface);
837 gst_sdp_message_free (rtspsrc->sdp);
840 if (rtspsrc->provided_clock)
841 gst_object_unref (rtspsrc->provided_clock);
844 gst_structure_free (rtspsrc->sdes);
846 if (rtspsrc->tls_database)
847 g_object_unref (rtspsrc->tls_database);
850 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
851 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
853 G_OBJECT_CLASS (parent_class)->finalize (object);
857 gst_rtspsrc_provide_clock (GstElement * element)
859 GstRTSPSrc *src = GST_RTSPSRC (element);
862 if ((clock = src->provided_clock) != NULL)
863 gst_object_ref (clock);
868 /* a proxy string of the format [user:passwd@]host[:port] */
870 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
874 g_free (rtsp->proxy_user);
875 rtsp->proxy_user = NULL;
876 g_free (rtsp->proxy_passwd);
877 rtsp->proxy_passwd = NULL;
878 g_free (rtsp->proxy_host);
879 rtsp->proxy_host = NULL;
880 rtsp->proxy_port = 0;
887 /* we allow http:// in front but ignore it */
888 if (g_str_has_prefix (p, "http://"))
891 at = strchr (p, '@');
893 /* look for user:passwd */
894 col = strchr (proxy, ':');
895 if (col == NULL || col > at)
898 rtsp->proxy_user = g_strndup (p, col - p);
900 rtsp->proxy_passwd = g_strndup (col, at - col);
905 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
906 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
907 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
908 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
909 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
910 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
911 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
914 col = strchr (p, ':');
917 /* everything before the colon is the hostname */
918 rtsp->proxy_host = g_strndup (p, col - p);
920 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
922 rtsp->proxy_host = g_strdup (p);
923 rtsp->proxy_port = 8080;
929 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
931 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
932 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
935 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
937 rtspsrc->ptcp_timeout = NULL;
941 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
946 rtspsrc = GST_RTSPSRC (object);
950 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
951 g_value_get_string (value), NULL);
954 rtspsrc->protocols = g_value_get_flags (value);
957 rtspsrc->debug = g_value_get_boolean (value);
960 rtspsrc->retry = g_value_get_uint (value);
963 rtspsrc->udp_timeout = g_value_get_uint64 (value);
965 case PROP_TCP_TIMEOUT:
966 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
969 rtspsrc->latency = g_value_get_uint (value);
971 case PROP_DROP_ON_LATENCY:
972 rtspsrc->drop_on_latency = g_value_get_boolean (value);
974 case PROP_CONNECTION_SPEED:
975 rtspsrc->connection_speed = g_value_get_uint64 (value);
977 case PROP_NAT_METHOD:
978 rtspsrc->nat_method = g_value_get_enum (value);
981 rtspsrc->do_rtcp = g_value_get_boolean (value);
983 case PROP_DO_RTSP_KEEP_ALIVE:
984 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
987 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
990 if (rtspsrc->prop_proxy_id)
991 g_free (rtspsrc->prop_proxy_id);
992 rtspsrc->prop_proxy_id = g_value_dup_string (value);
995 if (rtspsrc->prop_proxy_pw)
996 g_free (rtspsrc->prop_proxy_pw);
997 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
999 case PROP_RTP_BLOCKSIZE:
1000 rtspsrc->rtp_blocksize = g_value_get_uint (value);
1003 if (rtspsrc->user_id)
1004 g_free (rtspsrc->user_id);
1005 rtspsrc->user_id = g_value_dup_string (value);
1008 if (rtspsrc->user_pw)
1009 g_free (rtspsrc->user_pw);
1010 rtspsrc->user_pw = g_value_dup_string (value);
1012 case PROP_BUFFER_MODE:
1013 rtspsrc->buffer_mode = g_value_get_enum (value);
1015 case PROP_PORT_RANGE:
1019 str = g_value_get_string (value);
1021 sscanf (str, "%u-%u",
1022 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
1024 rtspsrc->client_port_range.min = 0;
1025 rtspsrc->client_port_range.max = 0;
1029 case PROP_UDP_BUFFER_SIZE:
1030 rtspsrc->udp_buffer_size = g_value_get_int (value);
1032 case PROP_SHORT_HEADER:
1033 rtspsrc->short_header = g_value_get_boolean (value);
1035 case PROP_PROBATION:
1036 rtspsrc->probation = g_value_get_uint (value);
1038 case PROP_UDP_RECONNECT:
1039 rtspsrc->udp_reconnect = g_value_get_boolean (value);
1041 case PROP_MULTICAST_IFACE:
1042 g_free (rtspsrc->multi_iface);
1044 if (g_value_get_string (value) == NULL)
1045 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
1047 rtspsrc->multi_iface = g_value_dup_string (value);
1050 rtspsrc->ntp_sync = g_value_get_boolean (value);
1052 case PROP_USE_PIPELINE_CLOCK:
1053 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
1056 rtspsrc->sdes = g_value_dup_boxed (value);
1058 case PROP_TLS_VALIDATION_FLAGS:
1059 rtspsrc->tls_validation_flags = g_value_get_flags (value);
1061 case PROP_TLS_DATABASE:
1062 g_clear_object (&rtspsrc->tls_database);
1063 rtspsrc->tls_database = g_value_dup_object (value);
1065 case PROP_DO_RETRANSMISSION:
1066 rtspsrc->do_retransmission = g_value_get_boolean (value);
1069 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1075 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1078 GstRTSPSrc *rtspsrc;
1080 rtspsrc = GST_RTSPSRC (object);
1084 g_value_set_string (value, rtspsrc->conninfo.location);
1086 case PROP_PROTOCOLS:
1087 g_value_set_flags (value, rtspsrc->protocols);
1090 g_value_set_boolean (value, rtspsrc->debug);
1093 g_value_set_uint (value, rtspsrc->retry);
1096 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1098 case PROP_TCP_TIMEOUT:
1102 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1103 rtspsrc->tcp_timeout.tv_usec;
1104 g_value_set_uint64 (value, timeout);
1108 g_value_set_uint (value, rtspsrc->latency);
1110 case PROP_DROP_ON_LATENCY:
1111 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1113 case PROP_CONNECTION_SPEED:
1114 g_value_set_uint64 (value, rtspsrc->connection_speed);
1116 case PROP_NAT_METHOD:
1117 g_value_set_enum (value, rtspsrc->nat_method);
1120 g_value_set_boolean (value, rtspsrc->do_rtcp);
1122 case PROP_DO_RTSP_KEEP_ALIVE:
1123 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1129 if (rtspsrc->proxy_host) {
1131 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1135 g_value_take_string (value, str);
1139 g_value_set_string (value, rtspsrc->prop_proxy_id);
1142 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1144 case PROP_RTP_BLOCKSIZE:
1145 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1148 g_value_set_string (value, rtspsrc->user_id);
1151 g_value_set_string (value, rtspsrc->user_pw);
1153 case PROP_BUFFER_MODE:
1154 g_value_set_enum (value, rtspsrc->buffer_mode);
1156 case PROP_PORT_RANGE:
1160 if (rtspsrc->client_port_range.min != 0) {
1161 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1162 rtspsrc->client_port_range.max);
1166 g_value_take_string (value, str);
1169 case PROP_UDP_BUFFER_SIZE:
1170 g_value_set_int (value, rtspsrc->udp_buffer_size);
1172 case PROP_SHORT_HEADER:
1173 g_value_set_boolean (value, rtspsrc->short_header);
1175 case PROP_PROBATION:
1176 g_value_set_uint (value, rtspsrc->probation);
1178 case PROP_UDP_RECONNECT:
1179 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1181 case PROP_MULTICAST_IFACE:
1182 g_value_set_string (value, rtspsrc->multi_iface);
1185 g_value_set_boolean (value, rtspsrc->ntp_sync);
1187 case PROP_USE_PIPELINE_CLOCK:
1188 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1191 g_value_set_boxed (value, rtspsrc->sdes);
1193 case PROP_TLS_VALIDATION_FLAGS:
1194 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1196 case PROP_TLS_DATABASE:
1197 g_value_set_object (value, rtspsrc->tls_database);
1199 case PROP_DO_RETRANSMISSION:
1200 g_value_set_boolean (value, rtspsrc->do_retransmission);
1203 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1209 find_stream_by_id (GstRTSPStream * stream, gint * id)
1211 if (stream->id == *id)
1218 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1220 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1227 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1229 GstElement *src = (GstElement *) a;
1231 if (stream->udpsrc[0] == src)
1233 if (stream->udpsrc[1] == src)
1240 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1242 if (stream->conninfo.location) {
1243 /* check qualified setup_url */
1244 if (!strcmp (stream->conninfo.location, (gchar *) a))
1247 if (stream->control_url) {
1248 /* check original control_url */
1249 if (!strcmp (stream->control_url, (gchar *) a))
1252 /* check if qualified setup_url ends with string */
1253 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1260 static GstRTSPStream *
1261 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1265 /* find and get stream */
1266 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1267 return (GstRTSPStream *) lstream->data;
1272 static const GstSDPBandwidth *
1273 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1274 const GstSDPMedia * media, const gchar * type)
1278 /* first look in the media specific section */
1279 len = gst_sdp_media_bandwidths_len (media);
1280 for (i = 0; i < len; i++) {
1281 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1283 if (strcmp (bw->bwtype, type) == 0)
1286 /* then look in the message specific section */
1287 len = gst_sdp_message_bandwidths_len (sdp);
1288 for (i = 0; i < len; i++) {
1289 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1291 if (strcmp (bw->bwtype, type) == 0)
1298 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1299 const GstSDPMedia * media, GstRTSPStream * stream)
1301 const GstSDPBandwidth *bw;
1303 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1304 stream->as_bandwidth = bw->bandwidth;
1306 stream->as_bandwidth = -1;
1308 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1309 stream->rr_bandwidth = bw->bandwidth;
1311 stream->rr_bandwidth = -1;
1313 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1314 stream->rs_bandwidth = bw->bandwidth;
1316 stream->rs_bandwidth = -1;
1320 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1321 const GstSDPConnection * conn)
1323 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1326 if (conn->addrtype == NULL)
1329 /* check for IPV6 */
1330 if (strcmp (conn->addrtype, "IP4") == 0)
1331 stream->is_ipv6 = FALSE;
1332 else if (strcmp (conn->addrtype, "IP6") == 0)
1333 stream->is_ipv6 = TRUE;
1338 g_free (stream->destination);
1339 stream->destination = g_strdup (conn->address);
1341 /* check for multicast */
1342 stream->is_multicast =
1343 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1345 stream->ttl = conn->ttl;
1348 /* Go over the connections for a stream.
1349 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1351 * - If we are dealing with a localhost address, we disable multicast
1354 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1355 const GstSDPMedia * media, GstRTSPStream * stream)
1357 const GstSDPConnection *conn;
1360 /* first look in the media specific section */
1361 len = gst_sdp_media_connections_len (media);
1362 for (i = 0; i < len; i++) {
1363 conn = gst_sdp_media_get_connection (media, i);
1365 gst_rtspsrc_do_stream_connection (src, stream, conn);
1367 /* then look in the message specific section */
1368 if ((conn = gst_sdp_message_get_connection (sdp))) {
1369 gst_rtspsrc_do_stream_connection (src, stream, conn);
1373 /* m=<media> <UDP port> RTP/AVP <payload>
1376 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
1377 const GstSDPMedia * media, GstRTSPStream * stream)
1383 proto = gst_sdp_media_get_proto (media);
1387 if (g_str_equal (proto, "RTP/AVP"))
1388 stream->profile = GST_RTSP_PROFILE_AVP;
1389 else if (g_str_equal (proto, "RTP/SAVP"))
1390 stream->profile = GST_RTSP_PROFILE_SAVP;
1391 else if (g_str_equal (proto, "RTP/AVPF"))
1392 stream->profile = GST_RTSP_PROFILE_AVPF;
1393 else if (g_str_equal (proto, "RTP/SAVPF"))
1394 stream->profile = GST_RTSP_PROFILE_SAVPF;
1398 len = gst_sdp_media_formats_len (media);
1399 for (i = 0; i < len; i++) {
1406 pt = atoi (gst_sdp_media_get_format (media, i));
1408 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
1411 caps = gst_rtspsrc_media_to_caps (pt, media);
1413 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
1417 /* do some tweaks */
1418 s = gst_caps_get_structure (caps, 0);
1419 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
1420 stream->is_real = (strstr (enc, "-REAL") != NULL);
1421 if (strcmp (enc, "X-ASF-PF") == 0)
1422 stream->container = TRUE;
1424 GST_DEBUG ("mapping sdp session level attributes to caps");
1425 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, caps);
1426 GST_DEBUG ("mapping sdp media level attributes to caps");
1427 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, caps);
1429 /* the first pt will be the default */
1430 if (stream->ptmap->len == 0)
1431 stream->default_pt = pt;
1435 g_array_append_val (stream->ptmap, item);
1441 GST_ERROR_OBJECT (src, "can't find proto in media");
1446 GST_ERROR_OBJECT (src, "unknown proto in media %s", proto);
1451 static const gchar *
1452 get_aggregate_control (GstRTSPSrc * src)
1457 base = src->control;
1458 else if (src->content_base)
1459 base = src->content_base;
1460 else if (src->conninfo.url_str)
1461 base = src->conninfo.url_str;
1469 clear_ptmap_item (PtMapItem * item)
1472 gst_caps_unref (item->caps);
1475 static GstRTSPStream *
1476 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1478 GstRTSPStream *stream;
1479 const gchar *control_url;
1480 const GstSDPMedia *media;
1482 /* get media, should not return NULL */
1483 media = gst_sdp_message_get_media (sdp, idx);
1487 stream = g_new0 (GstRTSPStream, 1);
1488 stream->parent = src;
1489 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1491 stream->last_ret = GST_FLOW_NOT_LINKED;
1492 stream->added = FALSE;
1493 stream->setup = FALSE;
1494 stream->skipped = FALSE;
1496 stream->eos = FALSE;
1497 stream->discont = TRUE;
1498 stream->seqbase = -1;
1499 stream->timebase = -1;
1500 stream->send_ssrc = g_random_int ();
1501 stream->profile = GST_RTSP_PROFILE_AVP;
1502 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1503 g_array_set_clear_func (stream->ptmap, (GDestroyNotify) clear_ptmap_item);
1505 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1506 * session manager to scale RTCP. */
1507 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1509 /* collect connection info */
1510 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1512 /* make the payload type map */
1513 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1515 /* collect port number */
1516 stream->port = gst_sdp_media_get_port (media);
1518 /* get control url to construct the setup url. The setup url is used to
1519 * configure the transport of the stream and is used to identity the stream in
1520 * the RTP-Info header field returned from PLAY. */
1521 control_url = gst_sdp_media_get_attribute_val (media, "control");
1522 if (control_url == NULL)
1523 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1525 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1526 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1527 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1528 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1530 if (control_url != NULL) {
1531 stream->control_url = g_strdup (control_url);
1532 /* Build a fully qualified url using the content_base if any or by prefixing
1533 * the original request.
1534 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1535 * likely build a URL that the server will fail to understand, this is ok,
1536 * we will fail then. */
1537 if (g_str_has_prefix (control_url, "rtsp://"))
1538 stream->conninfo.location = g_strdup (control_url);
1543 if (g_strcmp0 (control_url, "*") == 0)
1546 base = get_aggregate_control (src);
1548 /* check if the base ends or control starts with / */
1549 has_slash = g_str_has_prefix (control_url, "/");
1550 has_slash = has_slash || g_str_has_suffix (base, "/");
1552 /* concatenate the two strings, insert / when not present */
1553 stream->conninfo.location =
1554 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1557 GST_DEBUG_OBJECT (src, " setup: %s",
1558 GST_STR_NULL (stream->conninfo.location));
1560 /* we keep track of all streams */
1561 src->streams = g_list_append (src->streams, stream);
1569 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1573 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1575 g_array_free (stream->ptmap, TRUE);
1577 g_free (stream->destination);
1578 g_free (stream->control_url);
1579 g_free (stream->conninfo.location);
1581 for (i = 0; i < 2; i++) {
1582 if (stream->udpsrc[i]) {
1583 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1584 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1585 gst_object_unref (stream->udpsrc[i]);
1587 if (stream->channelpad[i])
1588 gst_object_unref (stream->channelpad[i]);
1590 if (stream->udpsink[i]) {
1591 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1592 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1593 gst_object_unref (stream->udpsink[i]);
1596 if (stream->fakesrc) {
1597 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1598 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1599 gst_object_unref (stream->fakesrc);
1601 if (stream->srcpad) {
1602 gst_pad_set_active (stream->srcpad, FALSE);
1604 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1606 if (stream->srtpenc)
1607 gst_object_unref (stream->srtpenc);
1608 if (stream->srtpdec)
1609 gst_object_unref (stream->srtpdec);
1610 if (stream->srtcpparams)
1611 gst_caps_unref (stream->srtcpparams);
1612 if (stream->rtcppad)
1613 gst_object_unref (stream->rtcppad);
1614 if (stream->session)
1615 g_object_unref (stream->session);
1616 if (stream->rtx_pt_map)
1617 gst_structure_free (stream->rtx_pt_map);
1622 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1626 GST_DEBUG_OBJECT (src, "cleanup");
1628 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1629 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1631 gst_rtspsrc_stream_free (src, stream);
1633 g_list_free (src->streams);
1634 src->streams = NULL;
1636 if (src->manager_sig_id) {
1637 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1638 src->manager_sig_id = 0;
1640 gst_element_set_state (src->manager, GST_STATE_NULL);
1641 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1642 src->manager = NULL;
1645 gst_structure_free (src->props);
1648 g_free (src->content_base);
1649 src->content_base = NULL;
1651 g_free (src->control);
1652 src->control = NULL;
1655 gst_rtsp_range_free (src->range);
1658 /* don't clear the SDP when it was used in the url */
1659 if (src->sdp && !src->from_sdp) {
1660 gst_sdp_message_free (src->sdp);
1664 src->need_segment = FALSE;
1666 if (src->provided_clock) {
1667 gst_object_unref (src->provided_clock);
1668 src->provided_clock = NULL;
1672 #define PARSE_INT(p, del, res) \
1675 p = strstr (p, del); \
1685 #define PARSE_STRING(p, del, res) \
1688 p = strstr (p, del); \
1700 #define SKIP_SPACES(p) \
1701 while (*p && g_ascii_isspace (*p)) \
1706 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1709 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1710 gint * rate, gchar ** params)
1714 p = (gchar *) rtpmap;
1716 PARSE_INT (p, " ", *payload);
1724 PARSE_STRING (p, "/", *name);
1725 if (*name == NULL) {
1726 GST_DEBUG ("no rate, name %s", p);
1727 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1728 * streams seem to omit the rate. */
1735 p = strstr (p, "/");
1753 parse_keymgmt (const gchar * keymgmt, GstCaps * caps)
1755 gboolean res = FALSE;
1759 GstMIKEYMessage *msg;
1760 const GstMIKEYPayload *payload;
1761 const gchar *srtp_cipher;
1762 const gchar *srtp_auth;
1764 p = (gchar *) keymgmt;
1770 PARSE_STRING (p, " ", kmpid);
1771 if (!g_str_equal (kmpid, "mikey"))
1774 data = g_base64_decode (p, &size);
1778 msg = gst_mikey_message_new_from_data (data, size, NULL, NULL);
1783 srtp_cipher = "aes-128-icm";
1784 srtp_auth = "hmac-sha1-80";
1786 /* check the Security policy if any */
1787 if ((payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_SP, 0))) {
1788 GstMIKEYPayloadSP *p = (GstMIKEYPayloadSP *) payload;
1791 if (p->proto != GST_MIKEY_SEC_PROTO_SRTP)
1794 len = gst_mikey_payload_sp_get_n_params (payload);
1795 for (i = 0; i < len; i++) {
1796 const GstMIKEYPayloadSPParam *param =
1797 gst_mikey_payload_sp_get_param (payload, i);
1799 switch (param->type) {
1800 case GST_MIKEY_SP_SRTP_ENC_ALG:
1801 switch (param->val[0]) {
1803 srtp_cipher = "null";
1807 srtp_cipher = "aes-128-icm";
1813 case GST_MIKEY_SP_SRTP_ENC_KEY_LEN:
1814 switch (param->val[0]) {
1815 case AES_128_KEY_LEN:
1816 srtp_cipher = "aes-128-icm";
1818 case AES_256_KEY_LEN:
1819 srtp_cipher = "aes-256-icm";
1825 case GST_MIKEY_SP_SRTP_AUTH_ALG:
1826 switch (param->val[0]) {
1832 srtp_auth = "hmac-sha1-80";
1838 case GST_MIKEY_SP_SRTP_AUTH_KEY_LEN:
1839 switch (param->val[0]) {
1840 case HMAC_32_KEY_LEN:
1841 srtp_auth = "hmac-sha1-32";
1843 case HMAC_80_KEY_LEN:
1844 srtp_auth = "hmac-sha1-80";
1850 case GST_MIKEY_SP_SRTP_SRTP_ENC:
1852 case GST_MIKEY_SP_SRTP_SRTCP_ENC:
1860 if (!(payload = gst_mikey_message_find_payload (msg, GST_MIKEY_PT_KEMAC, 0)))
1863 GstMIKEYPayloadKEMAC *p = (GstMIKEYPayloadKEMAC *) payload;
1864 const GstMIKEYPayload *sub;
1865 GstMIKEYPayloadKeyData *pkd;
1868 if (p->enc_alg != GST_MIKEY_ENC_NULL || p->mac_alg != GST_MIKEY_MAC_NULL)
1871 if (!(sub = gst_mikey_payload_kemac_get_sub (payload, 0)))
1874 if (sub->type != GST_MIKEY_PT_KEY_DATA)
1877 pkd = (GstMIKEYPayloadKeyData *) sub;
1879 gst_buffer_new_wrapped (g_memdup (pkd->key_data, pkd->key_len),
1881 gst_caps_set_simple (caps, "srtp-key", GST_TYPE_BUFFER, buf, NULL);
1884 gst_caps_set_simple (caps,
1885 "srtp-cipher", G_TYPE_STRING, srtp_cipher,
1886 "srtp-auth", G_TYPE_STRING, srtp_auth,
1887 "srtcp-cipher", G_TYPE_STRING, srtp_cipher,
1888 "srtcp-auth", G_TYPE_STRING, srtp_auth, NULL);
1892 gst_mikey_message_unref (msg);
1898 * Mapping SDP attributes to caps
1900 * prepend 'a-' to IANA registered sdp attributes names
1901 * (ie: not prefixed with 'x-') in order to avoid
1902 * collision with gstreamer standard caps properties names
1905 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1907 if (attributes->len > 0) {
1911 s = gst_caps_get_structure (caps, 0);
1913 for (i = 0; i < attributes->len; i++) {
1914 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1915 gchar *tofree, *key;
1919 /* skip some of the attribute we already handle */
1920 if (!strcmp (key, "fmtp"))
1922 if (!strcmp (key, "rtpmap"))
1924 if (!strcmp (key, "control"))
1926 if (!strcmp (key, "range"))
1928 if (!strcmp (key, "framesize"))
1930 if (g_str_equal (key, "key-mgmt")) {
1931 parse_keymgmt (attr->value, caps);
1935 /* string must be valid UTF8 */
1936 if (!g_utf8_validate (attr->value, -1, NULL))
1939 if (!g_str_has_prefix (key, "x-"))
1940 tofree = key = g_strdup_printf ("a-%s", key);
1944 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1945 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1951 static const gchar *
1952 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
1961 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
1964 if (sscanf (attr, "%d ", &val) != 1)
1974 * Mapping of caps to and from SDP fields:
1976 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1977 * a=framesize:<payload> <width>-<height>
1978 * a=fmtp:<payload> <param>[=<value>];...
1981 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1984 const gchar *rtpmap;
1986 const gchar *framesize;
1989 gchar *params = NULL;
1995 /* get and parse rtpmap */
1996 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
1999 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
2001 g_warning ("error parsing rtpmap, ignoring");
2005 /* dynamic payloads need rtpmap or we fail */
2006 if (rtpmap == NULL && pt >= 96)
2009 /* check if we have a rate, if not, we need to look up the rate from the
2010 * default rates based on the payload types. */
2012 const GstRTPPayloadInfo *info;
2014 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
2015 /* dynamic types, use media and encoding_name */
2016 tmp = g_ascii_strdown (media->media, -1);
2017 info = gst_rtp_payload_info_for_name (tmp, name);
2020 /* static types, use payload type */
2021 info = gst_rtp_payload_info_for_pt (pt);
2025 if ((rate = info->clock_rate) == 0)
2028 /* we fail if we cannot find one */
2033 tmp = g_ascii_strdown (media->media, -1);
2034 caps = gst_caps_new_simple ("application/x-unknown",
2035 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
2037 s = gst_caps_get_structure (caps, 0);
2039 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
2041 /* encoding name must be upper case */
2043 tmp = g_ascii_strup (name, -1);
2044 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
2048 /* params must be lower case */
2049 if (params != NULL) {
2050 tmp = g_ascii_strdown (params, -1);
2051 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
2055 /* parse optional fmtp: field */
2056 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
2062 /* p is now of the format <payload> <param>[=<value>];... */
2063 PARSE_INT (p, " ", payload);
2064 if (payload != -1 && payload == pt) {
2068 /* <param>[=<value>] are separated with ';' */
2069 pairs = g_strsplit (p, ";", 0);
2070 for (i = 0; pairs[i]; i++) {
2072 const gchar *val, *key;
2074 /* the key may not have a '=', the value can have other '='s */
2075 valpos = strstr (pairs[i], "=");
2077 /* we have a '=' and thus a value, remove the '=' with \0 */
2079 /* value is everything between '=' and ';'. We split the pairs at ;
2080 * boundaries so we can take the remainder of the value. Some servers
2081 * put spaces around the value which we strip off here. Alternatively
2082 * we could strip those spaces in the depayloaders should these spaces
2083 * actually carry any meaning in the future. */
2084 val = g_strstrip (valpos + 1);
2086 /* simple <param>;.. is translated into <param>=1;... */
2089 /* strip the key of spaces, convert key to lowercase but not the value. */
2090 key = g_strstrip (pairs[i]);
2091 if (strlen (key) > 1) {
2092 tmp = g_ascii_strdown (key, -1);
2093 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
2101 /* parse framesize: field */
2102 if ((framesize = gst_sdp_media_get_attribute_val (media, "framesize"))) {
2105 /* p is now of the format <payload> <width>-<height> */
2106 p = (gchar *) framesize;
2108 PARSE_INT (p, " ", payload);
2109 if (payload != -1 && payload == pt) {
2110 gst_structure_set (s, "a-framesize", G_TYPE_STRING, p, NULL);
2118 g_warning ("rtpmap type not given for dynamic payload %d", pt);
2123 g_warning ("rate unknown for payload type %d", pt);
2129 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
2130 gint * rtpport, gint * rtcpport)
2133 GstStateChangeReturn ret;
2134 GstElement *udpsrc0, *udpsrc1;
2135 gint tmp_rtp, tmp_rtcp;
2139 src = stream->parent;
2145 /* Start at next port */
2146 tmp_rtp = src->next_port_num;
2148 if (stream->is_ipv6)
2149 host = "udp://[::0]";
2151 host = "udp://0.0.0.0";
2153 /* try to allocate 2 UDP ports, the RTP port should be an even
2154 * number and the RTCP port should be the next (uneven) port */
2157 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
2158 tmp_rtp >= src->client_port_range.max)
2161 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2162 if (udpsrc0 == NULL)
2163 goto no_udp_protocol;
2164 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
2166 if (src->udp_buffer_size != 0)
2167 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
2170 ret = gst_element_set_state (udpsrc0, GST_STATE_READY);
2171 if (ret == GST_STATE_CHANGE_FAILURE) {
2173 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
2176 if (++count > src->retry)
2179 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2180 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2181 gst_object_unref (udpsrc0);
2184 GST_DEBUG_OBJECT (src, "retry %d", count);
2187 goto no_udp_protocol;
2190 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
2191 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
2193 /* check if port is even */
2194 if ((tmp_rtp & 0x01) != 0) {
2195 /* port not even, close and allocate another */
2196 if (++count > src->retry)
2199 GST_DEBUG_OBJECT (src, "RTP port not even");
2201 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2202 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2203 gst_object_unref (udpsrc0);
2206 GST_DEBUG_OBJECT (src, "retry %d", count);
2211 /* allocate port+1 for RTCP now */
2212 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
2213 if (udpsrc1 == NULL)
2214 goto no_udp_rtcp_protocol;
2217 tmp_rtcp = tmp_rtp + 1;
2218 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
2221 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
2223 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
2224 ret = gst_element_set_state (udpsrc1, GST_STATE_READY);
2225 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
2226 if (ret == GST_STATE_CHANGE_FAILURE) {
2227 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
2229 if (++count > src->retry)
2232 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2233 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2234 gst_object_unref (udpsrc0);
2237 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2238 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2239 gst_object_unref (udpsrc1);
2243 GST_DEBUG_OBJECT (src, "retry %d", count);
2247 /* all fine, do port check */
2248 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2249 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2251 /* this should not happen... */
2252 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2255 /* we keep these elements, we configure all in configure_transport when the
2256 * server told us to really use the UDP ports. */
2257 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2258 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2259 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2260 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2262 /* keep track of next available port number when we have a range
2264 if (src->next_port_num != 0)
2265 src->next_port_num = tmp_rtcp + 1;
2272 GST_DEBUG_OBJECT (src, "could not get UDP source");
2277 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2281 no_udp_rtcp_protocol:
2283 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2288 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2289 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2295 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2296 gst_object_unref (udpsrc0);
2299 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2300 gst_object_unref (udpsrc1);
2307 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2312 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2314 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2315 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2318 for (i = 0; i < 2; i++) {
2319 if (stream->udpsrc[i])
2320 gst_element_set_state (stream->udpsrc[i], state);
2326 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2333 event = gst_event_new_flush_start ();
2334 GST_DEBUG_OBJECT (src, "start flush");
2336 state = GST_STATE_PAUSED;
2338 event = gst_event_new_flush_stop (FALSE);
2339 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2342 state = GST_STATE_PLAYING;
2344 state = GST_STATE_PAUSED;
2346 gst_rtspsrc_push_event (src, event);
2347 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2348 gst_rtspsrc_set_state (src, state);
2351 static GstRTSPResult
2352 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2353 GstRTSPMessage * message, GTimeVal * timeout)
2358 ret = gst_rtsp_connection_send (conn, message, timeout);
2360 ret = GST_RTSP_ERROR;
2365 static GstRTSPResult
2366 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2367 GstRTSPMessage * message, GTimeVal * timeout)
2372 ret = gst_rtsp_connection_receive (conn, message, timeout);
2374 ret = GST_RTSP_ERROR;
2380 gst_rtspsrc_get_position (GstRTSPSrc * src)
2385 query = gst_query_new_position (GST_FORMAT_TIME);
2386 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2387 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2388 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2392 if (stream->srcpad) {
2393 if (gst_pad_query (stream->srcpad, query)) {
2394 gst_query_parse_position (query, &fmt, &pos);
2395 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2396 GST_TIME_ARGS (pos));
2397 src->last_pos = pos;
2407 gst_query_unref (query);
2411 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2416 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2418 gboolean flush, skip;
2421 GstSegment seeksegment = { 0, };
2425 GST_DEBUG_OBJECT (src, "doing seek with event");
2427 gst_event_parse_seek (event, &rate, &format, &flags,
2428 &cur_type, &cur, &stop_type, &stop);
2430 /* no negative rates yet */
2434 /* we need TIME format */
2435 if (format != src->segment.format)
2438 GST_DEBUG_OBJECT (src, "doing seek without event");
2440 cur_type = GST_SEEK_TYPE_SET;
2441 stop_type = GST_SEEK_TYPE_SET;
2444 /* get flush flag */
2445 flush = flags & GST_SEEK_FLAG_FLUSH;
2446 skip = flags & GST_SEEK_FLAG_SKIP;
2448 /* now we need to make sure the streaming thread is stopped. We do this by
2449 * either sending a FLUSH_START event downstream which will cause the
2450 * streaming thread to stop with a WRONG_STATE.
2451 * For a non-flushing seek we simply pause the task, which will happen as soon
2452 * as it completes one iteration (and thus might block when the sink is
2453 * blocking in preroll). */
2455 GST_DEBUG_OBJECT (src, "starting flush");
2456 gst_rtspsrc_flush (src, TRUE, FALSE);
2459 gst_task_pause (src->task);
2463 /* we should now be able to grab the streaming thread because we stopped it
2464 * with the above flush/pause code */
2465 GST_RTSP_STREAM_LOCK (src);
2467 GST_DEBUG_OBJECT (src, "stopped streaming");
2469 /* stop flushing the rtsp connection so we can send PAUSE/PLAY below */
2470 gst_rtspsrc_connection_flush (src, FALSE);
2472 /* copy segment, we need this because we still need the old
2473 * segment when we close the current segment. */
2474 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2476 /* configure the seek parameters in the seeksegment. We will then have the
2477 * right values in the segment to perform the seek */
2479 GST_DEBUG_OBJECT (src, "configuring seek");
2480 gst_segment_do_seek (&seeksegment, rate, format, flags,
2481 cur_type, cur, stop_type, stop, &update);
2484 /* figure out the last position we need to play. If it's configured (stop !=
2485 * -1), use that, else we play until the total duration of the file */
2486 if ((stop = seeksegment.stop) == -1)
2487 stop = seeksegment.duration;
2489 playing = (src->state == GST_RTSP_STATE_PLAYING);
2491 /* if we were playing, pause first */
2493 /* obtain current position in case seek fails */
2494 gst_rtspsrc_get_position (src);
2495 gst_rtspsrc_pause (src, FALSE);
2499 src->state = GST_RTSP_STATE_SEEKING;
2501 /* PLAY will add the range header now. */
2502 src->need_range = TRUE;
2504 /* and continue playing */
2506 gst_rtspsrc_play (src, &seeksegment, FALSE);
2508 /* prepare for streaming again */
2510 /* if we started flush, we stop now */
2511 GST_DEBUG_OBJECT (src, "stopping flush");
2512 gst_rtspsrc_flush (src, FALSE, playing);
2515 /* now we did the seek and can activate the new segment values */
2516 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2518 /* if we're doing a segment seek, post a SEGMENT_START message */
2519 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2520 gst_element_post_message (GST_ELEMENT_CAST (src),
2521 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2522 src->segment.format, src->segment.position));
2525 /* now create the newsegment */
2526 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2527 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2530 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2531 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2532 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2533 stream->discont = TRUE;
2536 GST_RTSP_STREAM_UNLOCK (src);
2543 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2548 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2554 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2558 gboolean res = TRUE;
2561 src = GST_RTSPSRC_CAST (parent);
2563 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2564 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2566 switch (GST_EVENT_TYPE (event)) {
2567 case GST_EVENT_SEEK:
2568 res = gst_rtspsrc_perform_seek (src, event);
2572 case GST_EVENT_NAVIGATION:
2573 case GST_EVENT_LATENCY:
2581 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2582 res = gst_pad_send_event (target, event);
2583 gst_object_unref (target);
2585 gst_event_unref (event);
2588 gst_event_unref (event);
2594 /* this is the final event function we receive on the internal source pad when
2595 * we deal with TCP connections */
2597 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2602 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2604 switch (GST_EVENT_TYPE (event)) {
2605 case GST_EVENT_SEEK:
2607 case GST_EVENT_NAVIGATION:
2608 case GST_EVENT_LATENCY:
2610 gst_event_unref (event);
2617 /* this is the final query function we receive on the internal source pad when
2618 * we deal with TCP connections */
2620 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2624 gboolean res = TRUE;
2626 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2628 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2629 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2631 switch (GST_QUERY_TYPE (query)) {
2632 case GST_QUERY_POSITION:
2637 case GST_QUERY_DURATION:
2641 gst_query_parse_duration (query, &format, NULL);
2644 case GST_FORMAT_TIME:
2645 gst_query_set_duration (query, format, src->segment.duration);
2653 case GST_QUERY_LATENCY:
2655 /* we are live with a min latency of 0 and unlimited max latency, this
2656 * result will be updated by the session manager if there is any. */
2657 gst_query_set_latency (query, TRUE, 0, -1);
2667 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2669 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2673 gboolean res = FALSE;
2675 src = GST_RTSPSRC_CAST (parent);
2677 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2678 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2680 switch (GST_QUERY_TYPE (query)) {
2681 case GST_QUERY_DURATION:
2685 gst_query_parse_duration (query, &format, NULL);
2688 case GST_FORMAT_TIME:
2689 gst_query_set_duration (query, format, src->segment.duration);
2697 case GST_QUERY_SEEKING:
2701 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2702 if (format == GST_FORMAT_TIME) {
2704 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2706 /* seeking without duration is unlikely */
2707 seekable = seekable && src->seekable && src->segment.duration &&
2708 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2710 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable, 0,
2711 src->segment.duration);
2720 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2722 gst_query_set_uri (query, uri);
2730 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2732 /* forward the query to the proxy target pad */
2734 res = gst_pad_query (target, query);
2735 gst_object_unref (target);
2744 /* callback for RTCP messages to be sent to the server when operating in TCP
2746 static GstFlowReturn
2747 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2750 GstRTSPStream *stream;
2751 GstFlowReturn res = GST_FLOW_OK;
2756 GstRTSPMessage message = { 0 };
2757 GstRTSPConnection *conn;
2759 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2760 src = stream->parent;
2762 gst_buffer_map (buffer, &map, GST_MAP_READ);
2766 gst_rtsp_message_init_data (&message, stream->channel[1]);
2768 /* lend the body data to the message */
2769 gst_rtsp_message_take_body (&message, data, size);
2771 if (stream->conninfo.connection)
2772 conn = stream->conninfo.connection;
2774 conn = src->conninfo.connection;
2776 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2777 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2778 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2780 /* and steal it away again because we will free it when unreffing the
2782 gst_rtsp_message_steal_body (&message, &data, &size);
2783 gst_rtsp_message_unset (&message);
2785 gst_buffer_unmap (buffer, &map);
2786 gst_buffer_unref (buffer);
2791 static GstPadProbeReturn
2792 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2794 GstRTSPSrc *src = user_data;
2796 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2797 GST_DEBUG_PAD_NAME (pad));
2799 /* activate the streams */
2800 GST_OBJECT_LOCK (src);
2801 if (!src->need_activate)
2804 src->need_activate = FALSE;
2805 GST_OBJECT_UNLOCK (src);
2807 gst_rtspsrc_activate_streams (src);
2809 return GST_PAD_PROBE_OK;
2813 GST_OBJECT_UNLOCK (src);
2814 return GST_PAD_PROBE_OK;
2819 copy_sticky_events (GstPad * pad, GstEvent ** event, gpointer user_data)
2821 GstPad *gpad = GST_PAD_CAST (user_data);
2823 GST_DEBUG_OBJECT (gpad, "store sticky event %" GST_PTR_FORMAT, *event);
2824 gst_pad_store_sticky_event (gpad, *event);
2829 /* this callback is called when the session manager generated a new src pad with
2830 * payloaded RTP packets. We simply ghost the pad here. */
2832 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2835 GstPadTemplate *template;
2838 GstRTSPStream *stream;
2841 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2843 GST_RTSP_STATE_LOCK (src);
2845 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2846 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2847 goto unknown_stream;
2849 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2851 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2853 goto unknown_stream;
2856 stream->ssrc = ssrc;
2858 /* we'll add it later see below */
2859 stream->added = TRUE;
2861 /* check if we added all streams */
2863 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2864 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2866 GST_DEBUG_OBJECT (src, "stream %p, container %d, added %d, setup %d",
2867 ostream, ostream->container, ostream->added, ostream->setup);
2869 /* if we find a stream for which we did a setup that is not added, we
2870 * need to wait some more */
2871 if (ostream->setup && !ostream->added) {
2876 GST_RTSP_STATE_UNLOCK (src);
2878 /* create a new pad we will use to stream to */
2879 template = gst_static_pad_template_get (&rtptemplate);
2880 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2881 gst_object_unref (template);
2884 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2885 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2886 gst_pad_set_active (stream->srcpad, TRUE);
2887 gst_pad_sticky_events_foreach (pad, copy_sticky_events, stream->srcpad);
2888 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2891 GST_DEBUG_OBJECT (src, "We added all streams");
2892 /* when we get here, all stream are added and we can fire the no-more-pads
2894 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2902 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2903 GST_RTSP_STATE_UNLOCK (src);
2910 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
2914 len = stream->ptmap->len;
2915 for (i = 0; i < len; i++) {
2916 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2924 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2926 GstRTSPStream *stream;
2929 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2931 GST_RTSP_STATE_LOCK (src);
2932 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2934 goto unknown_stream;
2936 if ((caps = stream_get_caps_for_pt (stream, pt)))
2937 gst_caps_ref (caps);
2938 GST_RTSP_STATE_UNLOCK (src);
2944 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2945 GST_RTSP_STATE_UNLOCK (src);
2951 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2953 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2959 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2965 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2971 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2973 GstRTSPSrc *src = stream->parent;
2976 g_object_get (source, "ssrc", &ssrc, NULL);
2978 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2979 ssrc, stream->ssrc, stream->id);
2981 if (ssrc == stream->ssrc)
2982 gst_rtspsrc_do_stream_eos (src, stream);
2986 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2988 GstRTSPSrc *src = stream->parent;
2991 g_object_get (source, "ssrc", &ssrc, NULL);
2993 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2994 ssrc, stream->ssrc, stream->id);
2996 if (ssrc == stream->ssrc)
2997 gst_rtspsrc_do_stream_eos (src, stream);
3001 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
3003 GstRTSPStream *stream;
3005 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
3007 /* get stream for session */
3008 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
3010 gst_rtspsrc_do_stream_eos (src, stream);
3015 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
3017 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
3022 set_manager_buffer_mode (GstRTSPSrc * src)
3024 GObjectClass *klass;
3026 if (src->manager == NULL)
3029 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3031 if (!g_object_class_find_property (klass, "buffer-mode"))
3034 if (src->buffer_mode != BUFFER_MODE_AUTO) {
3035 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
3040 GST_DEBUG_OBJECT (src,
3041 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
3043 if (src->provided_clock) {
3044 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
3046 if (clock == src->provided_clock) {
3047 GST_DEBUG_OBJECT (src, "selected synced");
3048 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
3051 gst_object_unref (clock);
3056 /* Otherwise fall-through and use another buffer mode */
3058 gst_object_unref (clock);
3061 GST_DEBUG_OBJECT (src, "auto buffering mode");
3062 if (src->use_buffering) {
3063 GST_DEBUG_OBJECT (src, "selected buffer");
3064 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
3066 GST_DEBUG_OBJECT (src, "selected slave");
3067 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
3072 request_key (GstElement * srtpdec, guint ssrc, GstRTSPStream * stream)
3074 GST_DEBUG ("request key %u", ssrc);
3075 return gst_caps_ref (stream_get_caps_for_pt (stream, stream->default_pt));
3079 request_rtp_decoder (GstElement * rtpbin, guint session, GstRTSPStream * stream)
3081 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3082 if (stream->id != session)
3085 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3086 stream->profile != GST_RTSP_PROFILE_SAVPF)
3089 if (stream->srtpdec == NULL) {
3092 name = g_strdup_printf ("srtpdec_%u", session);
3093 stream->srtpdec = gst_element_factory_make ("srtpdec", name);
3096 g_signal_connect (stream->srtpdec, "request-key",
3097 (GCallback) request_key, stream);
3099 return gst_object_ref (stream->srtpdec);
3103 request_rtcp_encoder (GstElement * rtpbin, guint session,
3104 GstRTSPStream * stream)
3109 GST_DEBUG ("decoder session %u, stream %p, %d", session, stream, stream->id);
3110 if (stream->id != session)
3113 if (stream->profile != GST_RTSP_PROFILE_SAVP &&
3114 stream->profile != GST_RTSP_PROFILE_SAVPF)
3117 if (stream->srtpenc == NULL) {
3120 name = g_strdup_printf ("srtpenc_%u", session);
3121 stream->srtpenc = gst_element_factory_make ("srtpenc", name);
3124 /* get RTCP crypto parameters from caps */
3125 s = gst_caps_get_structure (stream->srtcpparams, 0);
3129 GType ciphertype, authtype;
3130 GValue rtcp_cipher = G_VALUE_INIT, rtcp_auth = G_VALUE_INIT;
3132 ciphertype = g_type_from_name ("GstSrtpCipherType");
3133 authtype = g_type_from_name ("GstSrtpAuthType");
3134 g_value_init (&rtcp_cipher, ciphertype);
3135 g_value_init (&rtcp_auth, authtype);
3137 str = gst_structure_get_string (s, "srtcp-cipher");
3138 gst_value_deserialize (&rtcp_cipher, str);
3139 str = gst_structure_get_string (s, "srtcp-auth");
3140 gst_value_deserialize (&rtcp_auth, str);
3141 gst_structure_get (s, "srtp-key", GST_TYPE_BUFFER, &buf, NULL);
3143 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-cipher",
3145 g_object_set_property (G_OBJECT (stream->srtpenc), "rtcp-auth",
3147 g_object_set (stream->srtpenc, "key", buf, NULL);
3149 g_value_unset (&rtcp_cipher);
3150 g_value_unset (&rtcp_auth);
3151 gst_buffer_unref (buf);
3154 name = g_strdup_printf ("rtcp_sink_%d", session);
3155 pad = gst_element_get_request_pad (stream->srtpenc, name);
3157 gst_object_unref (pad);
3159 return gst_object_ref (stream->srtpenc);
3163 request_aux_receiver (GstElement * rtpbin, guint sessid, GstRTSPSrc * src)
3165 GstElement *rtx, *bin;
3168 GstRTSPStream *stream;
3170 stream = find_stream (src, &sessid, (gpointer) find_stream_by_id);
3172 GST_WARNING_OBJECT (src, "Stream %u not found", sessid);
3176 GST_INFO_OBJECT (src, "creating retransmision receiver for session %u "
3177 "with map %" GST_PTR_FORMAT, sessid, stream->rtx_pt_map);
3178 bin = gst_bin_new (NULL);
3179 rtx = gst_element_factory_make ("rtprtxreceive", NULL);
3180 g_object_set (rtx, "payload-type-map", stream->rtx_pt_map, NULL);
3181 gst_bin_add (GST_BIN (bin), rtx);
3183 pad = gst_element_get_static_pad (rtx, "src");
3184 name = g_strdup_printf ("src_%u", sessid);
3185 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3187 gst_object_unref (pad);
3189 pad = gst_element_get_static_pad (rtx, "sink");
3190 name = g_strdup_printf ("sink_%u", sessid);
3191 gst_element_add_pad (bin, gst_ghost_pad_new (name, pad));
3193 gst_object_unref (pad);
3199 add_retransmission (GstRTSPSrc * src, GstRTSPTransport * transport)
3203 gboolean do_retransmission = FALSE;
3205 if (transport->trans != GST_RTSP_TRANS_RTP)
3207 if (transport->profile != GST_RTSP_PROFILE_AVP &&
3208 transport->profile != GST_RTSP_PROFILE_AVPF)
3211 signal_id = g_signal_lookup ("request-aux-receiver",
3212 G_OBJECT_TYPE (src->manager));
3213 /* there's already something connected */
3214 if (g_signal_handler_find (src->manager, G_SIGNAL_MATCH_ID, signal_id, 0,
3215 NULL, NULL, NULL) != 0) {
3216 GST_DEBUG_OBJECT (src, "Not adding RTX AUX element as "
3217 "\"request-aux-receiver\" signal is "
3218 "already used by the application");
3222 /* build the retransmission payload type map */
3223 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3224 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3225 gboolean do_retransmission_stream = FALSE;
3228 if (stream->rtx_pt_map)
3229 gst_structure_free (stream->rtx_pt_map);
3230 stream->rtx_pt_map = gst_structure_new_empty ("application/x-rtp-pt-map");
3232 for (i = 0; i < stream->ptmap->len; i++) {
3233 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3234 GstStructure *s = gst_caps_get_structure (item->caps, 0);
3235 const gchar *encoding;
3237 /* we only care about RTX streams */
3238 if ((encoding = gst_structure_get_string (s, "encoding-name"))
3239 && g_strcmp0 (encoding, "RTX") == 0) {
3240 const gchar *stream_pt_s;
3243 if (gst_structure_get_int (s, "payload", &rtx_pt)
3244 && (stream_pt_s = gst_structure_get_string (s, "apt"))) {
3247 gst_structure_set (stream->rtx_pt_map, stream_pt_s, G_TYPE_UINT,
3249 do_retransmission_stream = TRUE;
3255 if (do_retransmission_stream) {
3256 GST_DEBUG_OBJECT (src, "built retransmission payload map for stream "
3257 "id %i: %" GST_PTR_FORMAT, stream->id, stream->rtx_pt_map);
3258 do_retransmission = TRUE;
3260 GST_DEBUG_OBJECT (src, "no retransmission payload map for stream "
3261 "id %i", stream->id);
3262 gst_structure_free (stream->rtx_pt_map);
3263 stream->rtx_pt_map = NULL;
3267 if (do_retransmission) {
3268 GST_DEBUG_OBJECT (src, "Enabling retransmissions");
3270 g_object_set (src->manager, "do-retransmission", TRUE, NULL);
3272 /* enable RFC4588 retransmission handling by setting rtprtxreceive
3273 * as the "aux" element of rtpbin */
3274 g_signal_connect (src->manager, "request-aux-receiver",
3275 (GCallback) request_aux_receiver, src);
3277 GST_DEBUG_OBJECT (src,
3278 "Not enabling retransmissions as no stream had a retransmission payload map");
3282 /* try to get and configure a manager */
3284 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
3285 GstRTSPTransport * transport)
3287 const gchar *manager;
3289 GstStateChangeReturn ret;
3291 /* find a manager */
3292 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
3296 GST_DEBUG_OBJECT (src, "using manager %s", manager);
3298 /* configure the manager */
3299 if (src->manager == NULL) {
3300 GObjectClass *klass;
3302 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
3304 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
3308 goto use_no_manager;
3310 if (!(src->manager = gst_element_factory_make (manager, "manager")))
3311 goto manager_failed;
3314 /* we manage this element */
3315 gst_element_set_locked_state (src->manager, TRUE);
3316 gst_bin_add (GST_BIN_CAST (src), src->manager);
3318 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
3319 if (ret == GST_STATE_CHANGE_FAILURE)
3320 goto start_manager_failure;
3322 g_object_set (src->manager, "latency", src->latency, NULL);
3324 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
3326 if (g_object_class_find_property (klass, "ntp-sync")) {
3327 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
3330 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
3331 g_object_set (src->manager, "use-pipeline-clock",
3332 src->use_pipeline_clock, NULL);
3335 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
3336 g_object_set (src->manager, "sdes", src->sdes, NULL);
3339 if (g_object_class_find_property (klass, "drop-on-latency")) {
3340 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
3344 /* buffer mode pauses are handled by adding offsets to buffer times,
3345 * but some depayloaders may have a hard time syncing output times
3346 * with such input times, e.g. container ones, most notably ASF */
3347 /* TODO alternatives are having an event that indicates these shifts,
3348 * or having rtsp extensions provide suggestion on buffer mode */
3349 /* valid duration implies not likely live pipeline,
3350 * so slaving in jitterbuffer does not make much sense
3351 * (and might mess things up due to bursts) */
3352 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
3353 src->segment.duration && stream->container) {
3354 src->use_buffering = TRUE;
3356 src->use_buffering = FALSE;
3359 set_manager_buffer_mode (src);
3361 /* connect to signals */
3362 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
3364 src->manager_sig_id =
3365 g_signal_connect (src->manager, "pad-added",
3366 (GCallback) new_manager_pad, src);
3367 src->manager_ptmap_id =
3368 g_signal_connect (src->manager, "request-pt-map",
3369 (GCallback) request_pt_map, src);
3371 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
3374 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
3377 if (src->do_retransmission)
3378 add_retransmission (src, transport);
3380 g_signal_connect (src->manager, "request-rtp-decoder",
3381 (GCallback) request_rtp_decoder, stream);
3382 g_signal_connect (src->manager, "request-rtcp-decoder",
3383 (GCallback) request_rtp_decoder, stream);
3384 g_signal_connect (src->manager, "request-rtcp-encoder",
3385 (GCallback) request_rtcp_encoder, stream);
3387 /* we stream directly to the manager, get some pads. Each RTSP stream goes
3388 * into a separate RTP session. */
3389 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
3390 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
3392 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
3393 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
3396 /* now configure the bandwidth in the manager */
3397 if (g_signal_lookup ("get-internal-session",
3398 G_OBJECT_TYPE (src->manager)) != 0) {
3399 GObject *rtpsession;
3401 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
3404 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
3406 stream->session = rtpsession;
3408 if (stream->as_bandwidth != -1) {
3409 GST_INFO_OBJECT (src, "setting AS: %f",
3410 (gdouble) (stream->as_bandwidth * 1000));
3411 g_object_set (rtpsession, "bandwidth",
3412 (gdouble) (stream->as_bandwidth * 1000), NULL);
3414 if (stream->rr_bandwidth != -1) {
3415 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
3416 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
3419 if (stream->rs_bandwidth != -1) {
3420 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
3421 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
3425 g_object_set (rtpsession, "probation", src->probation, NULL);
3427 g_object_set (rtpsession, "internal-ssrc", stream->send_ssrc, NULL);
3429 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
3431 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
3433 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
3435 g_signal_connect (rtpsession, "on-ssrc-active",
3436 (GCallback) on_ssrc_active, stream);
3447 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3452 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
3455 start_manager_failure:
3457 GST_DEBUG_OBJECT (src, "could not start session manager");
3462 /* free the UDP sources allocated when negotiating a transport.
3463 * This function is called when the server negotiated to a transport where the
3464 * UDP sources are not needed anymore, such as TCP or multicast. */
3466 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3470 for (i = 0; i < 2; i++) {
3471 if (stream->udpsrc[i]) {
3472 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3473 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3474 gst_object_unref (stream->udpsrc[i]);
3475 stream->udpsrc[i] = NULL;
3480 /* for TCP, create pads to send and receive data to and from the manager and to
3481 * intercept various events and queries
3484 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3485 GstRTSPTransport * transport, GstPad ** outpad)
3488 GstPadTemplate *template;
3489 GstPad *pad0, *pad1;
3491 /* configure for interleaved delivery, nothing needs to be done
3492 * here, the loop function will call the chain functions of the
3493 * session manager. */
3494 stream->channel[0] = transport->interleaved.min;
3495 stream->channel[1] = transport->interleaved.max;
3496 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3497 stream->channel[0], stream->channel[1]);
3499 /* we can remove the allocated UDP ports now */
3500 gst_rtspsrc_stream_free_udp (stream);
3502 /* no session manager, send data to srcpad directly */
3503 if (!stream->channelpad[0]) {
3504 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3506 /* create a new pad we will use to stream to */
3507 name = g_strdup_printf ("stream_%u", stream->id);
3508 template = gst_static_pad_template_get (&rtptemplate);
3509 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3510 gst_object_unref (template);
3513 /* set caps and activate */
3514 gst_pad_use_fixed_caps (stream->channelpad[0]);
3515 gst_pad_set_active (stream->channelpad[0], TRUE);
3517 *outpad = gst_object_ref (stream->channelpad[0]);
3519 GST_DEBUG_OBJECT (src, "using manager source pad");
3521 template = gst_static_pad_template_get (&anysrctemplate);
3523 /* allocate pads for sending the channel data into the manager */
3524 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3525 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3526 gst_object_unref (stream->channelpad[0]);
3527 stream->channelpad[0] = pad0;
3528 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3529 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3530 gst_pad_set_element_private (pad0, src);
3531 gst_pad_set_active (pad0, TRUE);
3533 if (stream->channelpad[1]) {
3534 /* if we have a sinkpad for the other channel, create a pad and link to the
3536 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3537 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3538 gst_pad_link_full (pad1, stream->channelpad[1],
3539 GST_PAD_LINK_CHECK_NOTHING);
3540 gst_object_unref (stream->channelpad[1]);
3541 stream->channelpad[1] = pad1;
3542 gst_pad_set_active (pad1, TRUE);
3544 gst_object_unref (template);
3546 /* setup RTCP transport back to the server if we have to. */
3547 if (src->manager && src->do_rtcp) {
3550 template = gst_static_pad_template_get (&anysinktemplate);
3552 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3553 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3554 gst_pad_set_element_private (stream->rtcppad, stream);
3555 gst_pad_set_active (stream->rtcppad, TRUE);
3557 /* get session RTCP pad */
3558 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3559 pad = gst_element_get_request_pad (src->manager, name);
3564 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3565 gst_object_unref (pad);
3568 gst_object_unref (template);
3574 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3575 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3576 gint * max, guint * ttl)
3578 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3580 if (!(*destination = transport->destination))
3581 *destination = stream->destination;
3584 /* transport first */
3585 *min = transport->port.min;
3586 *max = transport->port.max;
3587 if (*min == -1 && *max == -1) {
3588 /* then try from SDP */
3589 if (stream->port != 0) {
3590 *min = stream->port;
3591 *max = stream->port + 1;
3597 if (!(*ttl = transport->ttl))
3602 /* first take the source, then the endpoint to figure out where to send
3604 if (!(*destination = transport->source)) {
3605 if (src->conninfo.connection)
3606 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3607 else if (stream->conninfo.connection)
3609 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3613 /* for unicast we only expect the ports here */
3614 *min = transport->server_port.min;
3615 *max = transport->server_port.max;
3620 /* For multicast create UDP sources and join the multicast group. */
3622 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3623 GstRTSPTransport * transport, GstPad ** outpad)
3626 const gchar *destination;
3629 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3631 /* we can remove the allocated UDP ports now */
3632 gst_rtspsrc_stream_free_udp (stream);
3634 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3637 /* we need a destination now */
3638 if (destination == NULL)
3639 goto no_destination;
3641 /* we really need ports now or we won't be able to receive anything at all */
3642 if (min == -1 && max == -1)
3645 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3646 destination, min, max);
3648 /* creating UDP source for RTP */
3650 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3652 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3654 if (stream->udpsrc[0] == NULL)
3657 /* take ownership */
3658 gst_object_ref_sink (stream->udpsrc[0]);
3660 if (src->udp_buffer_size != 0)
3661 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3662 src->udp_buffer_size, NULL);
3664 if (src->multi_iface != NULL)
3665 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3666 src->multi_iface, NULL);
3669 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3670 gst_element_set_state (stream->udpsrc[0], GST_STATE_READY);
3673 /* creating another UDP source for RTCP */
3677 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3679 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3681 if (stream->udpsrc[1] == NULL)
3684 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3685 stream->profile == GST_RTSP_PROFILE_SAVPF)
3686 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3688 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3689 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3690 gst_caps_unref (caps);
3692 /* take ownership */
3693 gst_object_ref_sink (stream->udpsrc[1]);
3695 if (src->multi_iface != NULL)
3696 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3697 src->multi_iface, NULL);
3699 gst_element_set_state (stream->udpsrc[1], GST_STATE_READY);
3706 GST_DEBUG_OBJECT (src, "no UDP source element found");
3711 GST_DEBUG_OBJECT (src, "no destination found");
3716 GST_DEBUG_OBJECT (src, "no ports found");
3721 /* configure the remainder of the UDP ports */
3723 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3724 GstRTSPTransport * transport, GstPad ** outpad)
3726 /* we manage the UDP elements now. For unicast, the UDP sources where
3727 * allocated in the stream when we suggested a transport. */
3728 if (stream->udpsrc[0]) {
3731 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3732 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3734 GST_DEBUG_OBJECT (src, "setting up UDP source");
3736 /* configure a timeout on the UDP port. When the timeout message is
3737 * posted, we assume UDP transport is not possible. We reconnect using TCP
3739 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3740 src->udp_timeout * 1000, NULL);
3742 if ((caps = stream_get_caps_for_pt (stream, stream->default_pt)))
3743 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
3745 /* get output pad of the UDP source. */
3746 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3748 /* save it so we can unblock */
3749 stream->blockedpad = *outpad;
3751 /* configure pad block on the pad. As soon as there is dataflow on the
3752 * UDP source, we know that UDP is not blocked by a firewall and we can
3753 * configure all the streams to let the application autoplug decoders. */
3755 gst_pad_add_probe (stream->blockedpad,
3756 GST_PAD_PROBE_TYPE_BLOCK | GST_PAD_PROBE_TYPE_BUFFER |
3757 GST_PAD_PROBE_TYPE_BUFFER_LIST, pad_blocked, src, NULL);
3759 if (stream->channelpad[0]) {
3760 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3761 /* configure for UDP delivery, we need to connect the UDP pads to
3762 * the session plugin. */
3763 gst_pad_link_full (*outpad, stream->channelpad[0],
3764 GST_PAD_LINK_CHECK_NOTHING);
3765 gst_object_unref (*outpad);
3767 /* we connected to pad-added signal to get pads from the manager */
3769 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3774 if (stream->udpsrc[1]) {
3777 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3778 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3780 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
3781 stream->profile == GST_RTSP_PROFILE_SAVPF)
3782 caps = gst_caps_new_empty_simple ("application/x-srtcp");
3784 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3785 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3786 gst_caps_unref (caps);
3788 if (stream->channelpad[1]) {
3791 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3793 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3794 gst_pad_link_full (pad, stream->channelpad[1],
3795 GST_PAD_LINK_CHECK_NOTHING);
3796 gst_object_unref (pad);
3798 /* leave unlinked */
3804 /* configure the UDP sink back to the server for status reports */
3806 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3807 GstRTSPStream * stream, GstRTSPTransport * transport)
3810 gint rtp_port, rtcp_port;
3811 gboolean do_rtp, do_rtcp;
3812 const gchar *destination;
3817 /* get transport info */
3818 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3819 &rtp_port, &rtcp_port, &ttl);
3821 /* see what we need to do */
3822 do_rtp = (rtp_port != -1);
3823 /* it's possible that the server does not want us to send RTCP in which case
3825 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3827 /* we need a destination when we have RTP or RTCP ports */
3828 if (destination == NULL && (do_rtp || do_rtcp))
3829 goto no_destination;
3831 /* try to construct the fakesrc to the RTP port of the server to open up any
3834 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3837 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3838 stream->udpsink[0] =
3839 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3841 if (stream->udpsink[0] == NULL)
3842 goto no_sink_element;
3844 /* don't join multicast group, we will have the source socket do that */
3845 /* no sync or async state changes needed */
3846 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3847 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3849 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3851 if (stream->udpsrc[0]) {
3852 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3853 * so that NAT firewalls will open a hole for us */
3854 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3855 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3856 /* configure socket and make sure udpsink does not close it when shutting
3857 * down, it belongs to udpsrc after all. */
3858 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3859 "close-socket", FALSE, NULL);
3860 g_object_unref (socket);
3863 /* the source for the dummy packets to open up NAT */
3864 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3865 if (stream->fakesrc == NULL)
3866 goto no_fakesrc_element;
3868 /* random data in 5 buffers, a size of 200 bytes should be fine */
3869 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3870 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3872 /* we don't want to consider this a sink */
3873 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
3875 /* keep everything locked */
3876 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3877 gst_element_set_locked_state (stream->fakesrc, TRUE);
3879 gst_object_ref (stream->udpsink[0]);
3880 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3881 gst_object_ref (stream->fakesrc);
3882 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3884 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3885 "sink", GST_PAD_LINK_CHECK_NOTHING);
3888 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3891 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3892 stream->udpsink[1] =
3893 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3895 if (stream->udpsink[1] == NULL)
3896 goto no_sink_element;
3898 /* don't join multicast group, we will have the source socket do that */
3899 /* no sync or async state changes needed */
3900 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3901 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3903 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3905 if (stream->udpsrc[1]) {
3906 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3907 * because some servers check the port number of where it sends RTCP to identify
3908 * the RTCP packets it receives */
3909 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3910 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3911 /* configure socket and make sure udpsink does not close it when shutting
3912 * down, it belongs to udpsrc after all. */
3913 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3914 "close-socket", FALSE, NULL);
3915 g_object_unref (socket);
3918 /* we don't want to consider this a sink */
3919 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3921 /* we keep this playing always */
3922 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3923 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3925 gst_object_ref (stream->udpsink[1]);
3926 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3928 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3930 /* get session RTCP pad */
3931 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3932 pad = gst_element_get_request_pad (src->manager, name);
3937 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3938 gst_object_unref (pad);
3947 GST_DEBUG_OBJECT (src, "no destination address specified");
3952 GST_DEBUG_OBJECT (src, "no UDP sink element found");
3957 GST_DEBUG_OBJECT (src, "no fakesrc element found");
3962 /* sets up all elements needed for streaming over the specified transport.
3963 * Does not yet expose the element pads, this will be done when there is actuall
3964 * dataflow detected, which might never happen when UDP is blocked in a
3965 * firewall, for example.
3968 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3969 GstRTSPTransport * transport)
3972 GstPad *outpad = NULL;
3973 GstPadTemplate *template;
3975 const gchar *media_type;
3978 src = stream->parent;
3980 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3982 /* get the proper media type for this stream now */
3983 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
3984 goto unknown_transport;
3986 goto unknown_transport;
3988 /* configure the final media type */
3989 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
3991 len = stream->ptmap->len;
3992 for (i = 0; i < len; i++) {
3994 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3996 if (item->caps == NULL)
3999 s = gst_caps_get_structure (item->caps, 0);
4000 gst_structure_set_name (s, media_type);
4001 /* set ssrc if known */
4002 if (transport->ssrc)
4003 gst_structure_set (s, "ssrc", G_TYPE_UINT, transport->ssrc, NULL);
4006 /* try to get and configure a manager, channelpad[0-1] will be configured with
4007 * the pads for the manager, or NULL when no manager is needed. */
4008 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
4011 switch (transport->lower_transport) {
4012 case GST_RTSP_LOWER_TRANS_TCP:
4013 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
4014 goto transport_failed;
4016 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
4017 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
4018 goto transport_failed;
4019 /* fallthrough, the rest is the same for UDP and MCAST */
4020 case GST_RTSP_LOWER_TRANS_UDP:
4021 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
4022 goto transport_failed;
4023 /* configure udpsinks back to the server for RTCP messages and for the
4024 * dummy RTP messages to open NAT. */
4025 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
4026 goto transport_failed;
4029 goto unknown_transport;
4033 GST_DEBUG_OBJECT (src, "creating ghostpad");
4035 gst_pad_use_fixed_caps (outpad);
4037 /* create ghostpad, don't add just yet, this will be done when we activate
4039 name = g_strdup_printf ("stream_%u", stream->id);
4040 template = gst_static_pad_template_get (&rtptemplate);
4041 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
4042 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
4043 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
4044 gst_object_unref (template);
4047 gst_object_unref (outpad);
4049 /* mark pad as ok */
4050 stream->last_ret = GST_FLOW_OK;
4057 GST_DEBUG_OBJECT (src, "failed to configure transport");
4062 GST_DEBUG_OBJECT (src, "unknown transport");
4067 GST_DEBUG_OBJECT (src, "cannot get a session manager");
4072 /* send a couple of dummy random packets on the receiver RTP port to the server,
4073 * this should make a firewall think we initiated the data transfer and
4074 * hopefully allow packets to go from the sender port to our RTP receiver port */
4076 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
4080 if (src->nat_method != GST_RTSP_NAT_DUMMY)
4083 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4084 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4086 if (stream->fakesrc && stream->udpsink[0]) {
4087 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
4088 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
4089 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
4090 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
4091 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
4097 /* Adds the source pads of all configured streams to the element.
4098 * This code is performed when we detected dataflow.
4100 * We detect dataflow from either the _loop function or with pad probes on the
4104 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
4108 GST_DEBUG_OBJECT (src, "activating streams");
4110 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4111 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4113 if (stream->udpsrc[0]) {
4114 /* remove timeout, we are streaming now and timeouts will be handled by
4115 * the session manager and jitter buffer */
4116 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
4118 if (stream->srcpad) {
4119 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
4120 gst_pad_set_active (stream->srcpad, TRUE);
4122 /* if we don't have a session manager, set the caps now. If we have a
4123 * session, we will get a notification of the pad and the caps. */
4124 if (!src->manager) {
4127 caps = stream_get_caps_for_pt (stream, stream->default_pt);
4128 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
4129 gst_pad_set_caps (stream->srcpad, caps);
4132 if (!stream->added) {
4133 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
4134 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
4135 stream->added = TRUE;
4140 /* unblock all pads */
4141 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4142 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4144 if (stream->blockid) {
4145 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
4146 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
4147 stream->blockid = 0;
4155 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
4156 gboolean reset_manager)
4159 guint64 start, stop;
4160 gdouble play_speed, play_scale;
4162 GST_DEBUG_OBJECT (src, "configuring stream caps");
4164 start = segment->position;
4165 stop = segment->duration;
4166 play_speed = segment->rate;
4167 play_scale = segment->applied_rate;
4169 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4170 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4176 len = stream->ptmap->len;
4177 for (j = 0; j < len; j++) {
4179 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
4181 if (item->caps == NULL)
4184 caps = gst_caps_make_writable (item->caps);
4186 if (stream->timebase != -1)
4187 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
4188 (guint) stream->timebase, NULL);
4189 if (stream->seqbase != -1)
4190 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
4191 (guint) stream->seqbase, NULL);
4192 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
4194 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
4195 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
4196 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
4199 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
4202 if (item->pt == stream->default_pt && stream->udpsrc[0]) {
4203 g_object_set (stream->udpsrc[0], "caps", caps, NULL);
4207 if (reset_manager && src->manager) {
4208 GST_DEBUG_OBJECT (src, "clear session");
4209 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
4213 static GstFlowReturn
4214 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
4219 /* store the value */
4220 stream->last_ret = ret;
4222 /* if it's success we can return the value right away */
4223 if (ret == GST_FLOW_OK)
4226 /* any other error that is not-linked can be returned right
4228 if (ret != GST_FLOW_NOT_LINKED)
4231 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
4232 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4233 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4235 ret = ostream->last_ret;
4236 /* some other return value (must be SUCCESS but we can return
4237 * other values as well) */
4238 if (ret != GST_FLOW_NOT_LINKED)
4241 /* if we get here, all other pads were unlinked and we return
4242 * NOT_LINKED then */
4248 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
4251 gboolean res = TRUE;
4253 /* only streams that have a connection to the outside world */
4257 if (stream->udpsrc[0]) {
4258 gst_event_ref (event);
4259 res = gst_element_send_event (stream->udpsrc[0], event);
4260 } else if (stream->channelpad[0]) {
4261 gst_event_ref (event);
4262 if (GST_PAD_IS_SRC (stream->channelpad[0]))
4263 res = gst_pad_push_event (stream->channelpad[0], event);
4265 res = gst_pad_send_event (stream->channelpad[0], event);
4268 if (stream->udpsrc[1]) {
4269 gst_event_ref (event);
4270 res &= gst_element_send_event (stream->udpsrc[1], event);
4271 } else if (stream->channelpad[1]) {
4272 gst_event_ref (event);
4273 if (GST_PAD_IS_SRC (stream->channelpad[1]))
4274 res &= gst_pad_push_event (stream->channelpad[1], event);
4276 res &= gst_pad_send_event (stream->channelpad[1], event);
4280 gst_event_unref (event);
4286 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
4289 gboolean res = TRUE;
4291 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4292 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4294 gst_event_ref (event);
4295 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
4297 gst_event_unref (event);
4302 static GstRTSPResult
4303 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4308 if (info->connection == NULL) {
4309 if (info->url == NULL) {
4310 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
4311 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
4315 /* create connection */
4316 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
4317 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
4318 goto could_not_create;
4321 g_free (info->url_str);
4322 info->url_str = gst_rtsp_url_get_request_uri (info->url);
4324 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
4326 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
4327 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
4328 src->tls_validation_flags))
4329 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
4331 if (src->tls_database)
4332 gst_rtsp_connection_set_tls_database (info->connection,
4336 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
4337 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
4339 if (src->proxy_host) {
4340 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
4342 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
4347 if (!info->connected) {
4350 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
4351 ("Connecting to %s", info->location));
4352 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
4354 gst_rtsp_connection_connect (info->connection,
4355 src->ptcp_timeout)) < 0)
4356 goto could_not_connect;
4358 info->connected = TRUE;
4365 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
4370 gchar *str = gst_rtsp_strresult (res);
4371 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
4377 gchar *str = gst_rtsp_strresult (res);
4378 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
4384 static GstRTSPResult
4385 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
4388 GST_RTSP_STATE_LOCK (src);
4389 if (info->connected) {
4390 GST_DEBUG_OBJECT (src, "closing connection...");
4391 gst_rtsp_connection_close (info->connection);
4392 info->connected = FALSE;
4394 if (free && info->connection) {
4395 /* free connection */
4396 GST_DEBUG_OBJECT (src, "freeing connection...");
4397 gst_rtsp_connection_free (info->connection);
4398 info->connection = NULL;
4400 GST_RTSP_STATE_UNLOCK (src);
4404 static GstRTSPResult
4405 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
4410 GST_DEBUG_OBJECT (src, "reconnecting connection...");
4411 gst_rtsp_conninfo_close (src, info, FALSE);
4412 res = gst_rtsp_conninfo_connect (src, info, async);
4418 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
4422 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
4423 GST_RTSP_STATE_LOCK (src);
4424 if (src->conninfo.connection && src->conninfo.flushing != flush) {
4425 GST_DEBUG_OBJECT (src, "connection flush");
4426 gst_rtsp_connection_flush (src->conninfo.connection, flush);
4427 src->conninfo.flushing = flush;
4429 for (walk = src->streams; walk; walk = g_list_next (walk)) {
4430 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
4431 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
4432 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
4433 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
4434 stream->conninfo.flushing = flush;
4437 GST_RTSP_STATE_UNLOCK (src);
4440 /* FIXME, handle server request, reply with OK, for now */
4441 static GstRTSPResult
4442 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
4443 GstRTSPMessage * request)
4445 GstRTSPMessage response = { 0 };
4448 GST_DEBUG_OBJECT (src, "got server request message");
4451 gst_rtsp_message_dump (request);
4453 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
4455 if (res == GST_RTSP_ENOTIMPL) {
4456 /* default implementation, send OK */
4457 GST_DEBUG_OBJECT (src, "prepare OK reply");
4459 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
4464 /* let app parse and reply */
4465 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
4466 0, request, &response);
4469 gst_rtsp_message_dump (&response);
4471 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
4475 gst_rtsp_message_unset (&response);
4476 } else if (res == GST_RTSP_EEOF)
4484 gst_rtsp_message_unset (&response);
4489 /* send server keep-alive */
4490 static GstRTSPResult
4491 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4493 GstRTSPMessage request = { 0 };
4495 GstRTSPMethod method;
4496 const gchar *control;
4498 if (src->do_rtsp_keep_alive == FALSE) {
4499 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4500 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4504 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4506 /* find a method to use for keep-alive */
4507 if (src->methods & GST_RTSP_GET_PARAMETER)
4508 method = GST_RTSP_GET_PARAMETER;
4510 method = GST_RTSP_OPTIONS;
4512 control = get_aggregate_control (src);
4513 if (control == NULL)
4516 res = gst_rtsp_message_init_request (&request, method, control);
4521 gst_rtsp_message_dump (&request);
4524 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
4529 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4530 gst_rtsp_message_unset (&request);
4537 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4542 gchar *str = gst_rtsp_strresult (res);
4544 gst_rtsp_message_unset (&request);
4545 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4546 ("Could not send keep-alive. (%s)", str));
4552 static GstFlowReturn
4553 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4555 GstFlowReturn ret = GST_FLOW_OK;
4557 GstRTSPStream *stream;
4558 GstPad *outpad = NULL;
4564 channel = message->type_data.data.channel;
4566 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4568 goto unknown_stream;
4570 if (channel == stream->channel[0]) {
4571 outpad = stream->channelpad[0];
4573 } else if (channel == stream->channel[1]) {
4574 outpad = stream->channelpad[1];
4580 /* take a look at the body to figure out what we have */
4581 gst_rtsp_message_get_body (message, &data, &size);
4583 goto invalid_length;
4585 /* channels are not correct on some servers, do extra check */
4586 if (data[1] >= 200 && data[1] <= 204) {
4587 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4588 outpad = stream->channelpad[1];
4592 /* we have no clue what this is, just ignore then. */
4594 goto unknown_stream;
4596 /* take the message body for further processing */
4597 gst_rtsp_message_steal_body (message, &data, &size);
4599 /* strip the trailing \0 */
4602 buf = gst_buffer_new ();
4603 gst_buffer_append_memory (buf,
4604 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4606 /* don't need message anymore */
4607 gst_rtsp_message_unset (message);
4609 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4612 if (src->need_activate) {
4618 guint group_id = gst_util_group_id_next ();
4620 /* generate an SHA256 sum of the URI */
4621 cs = g_checksum_new (G_CHECKSUM_SHA256);
4622 uri = src->conninfo.location;
4623 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4625 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4626 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4630 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4631 event = gst_event_new_stream_start (stream_id);
4632 gst_event_set_group_id (event, group_id);
4635 gst_rtspsrc_stream_push_event (src, ostream, event);
4637 if ((caps = stream_get_caps_for_pt (ostream, ostream->default_pt))) {
4638 /* only streams that have a connection to the outside world */
4639 if (ostream->setup) {
4640 if (ostream->udpsrc[0]) {
4641 gst_element_send_event (ostream->udpsrc[0],
4642 gst_event_new_caps (caps));
4643 } else if (ostream->channelpad[0]) {
4644 if (GST_PAD_IS_SRC (ostream->channelpad[0]))
4645 gst_pad_push_event (ostream->channelpad[0],
4646 gst_event_new_caps (caps));
4648 gst_pad_send_event (ostream->channelpad[0],
4649 gst_event_new_caps (caps));
4652 caps = gst_caps_new_empty_simple ("application/x-rtcp");
4654 if (ostream->udpsrc[1]) {
4655 gst_element_send_event (ostream->udpsrc[1],
4656 gst_event_new_caps (caps));
4657 } else if (ostream->channelpad[1]) {
4658 if (GST_PAD_IS_SRC (ostream->channelpad[1]))
4659 gst_pad_push_event (ostream->channelpad[1],
4660 gst_event_new_caps (caps));
4662 gst_pad_send_event (ostream->channelpad[1],
4663 gst_event_new_caps (caps));
4666 gst_caps_unref (caps);
4670 g_checksum_free (cs);
4672 gst_rtspsrc_activate_streams (src);
4673 src->need_activate = FALSE;
4674 src->need_segment = TRUE;
4677 if (src->base_time == -1) {
4678 /* Take current running_time. This timestamp will be put on
4679 * the first buffer of each stream because we are a live source and so we
4680 * timestamp with the running_time. When we are dealing with TCP, we also
4681 * only timestamp the first buffer (using the DISCONT flag) because a server
4682 * typically bursts data, for which we don't want to compensate by speeding
4683 * up the media. The other timestamps will be interpollated from this one
4684 * using the RTP timestamps. */
4685 GST_OBJECT_LOCK (src);
4686 if (GST_ELEMENT_CLOCK (src)) {
4688 GstClockTime base_time;
4690 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4691 base_time = GST_ELEMENT_CAST (src)->base_time;
4693 src->base_time = now - base_time;
4695 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4696 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4698 GST_OBJECT_UNLOCK (src);
4701 /* If needed send a new segment, don't forget we are live and buffer are
4702 * timestamped with running time */
4703 if (src->need_segment) {
4705 src->need_segment = FALSE;
4706 gst_segment_init (&segment, GST_FORMAT_TIME);
4707 gst_rtspsrc_push_event (src, gst_event_new_segment (&segment));
4710 if (stream->discont && !is_rtcp) {
4711 /* mark first RTP buffer as discont */
4712 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4713 stream->discont = FALSE;
4714 /* first buffer gets the timestamp, other buffers are not timestamped and
4715 * their presentation time will be interpollated from the rtp timestamps. */
4716 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4717 GST_TIME_ARGS (src->base_time));
4719 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4722 /* chain to the peer pad */
4723 if (GST_PAD_IS_SINK (outpad))
4724 ret = gst_pad_chain (outpad, buf);
4726 ret = gst_pad_push (outpad, buf);
4729 /* combine all stream flows for the data transport */
4730 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4737 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4738 gst_rtsp_message_unset (message);
4743 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4744 ("Short message received, ignoring."));
4745 gst_rtsp_message_unset (message);
4750 static GstFlowReturn
4751 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4753 GstRTSPMessage message = { 0 };
4755 GstFlowReturn ret = GST_FLOW_OK;
4756 GTimeVal tv_timeout;
4759 /* get the next timeout interval */
4760 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4762 /* see if the timeout period expired */
4763 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4764 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4765 /* send keep-alive, only act on interrupt, a warning will be posted for
4767 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4769 /* get new timeout */
4770 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4773 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4774 tv_timeout.tv_sec, tv_timeout.tv_usec);
4776 /* protect the connection with the connection lock so that we can see when
4777 * we are finished doing server communication */
4779 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4780 &message, src->ptcp_timeout);
4784 GST_DEBUG_OBJECT (src, "we received a server message");
4786 case GST_RTSP_EINTR:
4787 /* we got interrupted this means we need to stop */
4789 case GST_RTSP_ETIMEOUT:
4790 /* no reply, send keep alive */
4791 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4792 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4796 /* go EOS when the server closed the connection */
4802 switch (message.type) {
4803 case GST_RTSP_MESSAGE_REQUEST:
4804 /* server sends us a request message, handle it */
4806 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4808 if (res == GST_RTSP_EEOF)
4811 goto handle_request_failed;
4813 case GST_RTSP_MESSAGE_RESPONSE:
4814 /* we ignore response messages */
4815 GST_DEBUG_OBJECT (src, "ignoring response message");
4817 gst_rtsp_message_dump (&message);
4819 case GST_RTSP_MESSAGE_DATA:
4820 GST_DEBUG_OBJECT (src, "got data message");
4821 ret = gst_rtspsrc_handle_data (src, &message);
4822 if (ret != GST_FLOW_OK)
4823 goto handle_data_failed;
4826 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4831 g_assert_not_reached ();
4836 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4837 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4838 ("The server closed the connection."));
4839 src->conninfo.connected = FALSE;
4840 gst_rtsp_message_unset (&message);
4841 return GST_FLOW_EOS;
4845 gst_rtsp_message_unset (&message);
4846 GST_DEBUG_OBJECT (src, "got interrupted");
4847 return GST_FLOW_FLUSHING;
4851 gchar *str = gst_rtsp_strresult (res);
4853 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4854 ("Could not receive message. (%s)", str));
4857 gst_rtsp_message_unset (&message);
4858 return GST_FLOW_ERROR;
4860 handle_request_failed:
4862 gchar *str = gst_rtsp_strresult (res);
4864 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4865 ("Could not handle server message. (%s)", str));
4867 gst_rtsp_message_unset (&message);
4868 return GST_FLOW_ERROR;
4872 GST_DEBUG_OBJECT (src, "could no handle data message");
4877 static GstFlowReturn
4878 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4881 GstRTSPMessage message = { 0 };
4885 GTimeVal tv_timeout;
4887 /* get the next timeout interval */
4888 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4890 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4891 (gint) tv_timeout.tv_sec);
4893 gst_rtsp_message_unset (&message);
4895 /* we should continue reading the TCP socket because the server might
4896 * send us requests. When the session timeout expires, we need to send a
4897 * keep-alive request to keep the session open. */
4898 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4899 &message, &tv_timeout);
4903 GST_DEBUG_OBJECT (src, "we received a server message");
4905 case GST_RTSP_EINTR:
4906 /* we got interrupted, see what we have to do */
4908 case GST_RTSP_ETIMEOUT:
4909 /* send keep-alive, ignore the result, a warning will be posted. */
4910 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4911 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4915 /* server closed the connection. not very fatal for UDP, reconnect and
4916 * see what happens. */
4917 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4918 ("The server closed the connection."));
4919 if (src->udp_reconnect) {
4921 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4928 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4930 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4931 ("Unhandled return value %d.", res));
4935 switch (message.type) {
4936 case GST_RTSP_MESSAGE_REQUEST:
4937 /* server sends us a request message, handle it */
4939 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4941 if (res == GST_RTSP_EEOF)
4944 goto handle_request_failed;
4946 case GST_RTSP_MESSAGE_RESPONSE:
4947 /* we ignore response and data messages */
4948 GST_DEBUG_OBJECT (src, "ignoring response message");
4950 gst_rtsp_message_dump (&message);
4951 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4952 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4953 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4954 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4955 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4962 case GST_RTSP_MESSAGE_DATA:
4963 /* we ignore response and data messages */
4964 GST_DEBUG_OBJECT (src, "ignoring data message");
4967 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4972 g_assert_not_reached ();
4974 /* we get here when the connection got interrupted */
4977 gst_rtsp_message_unset (&message);
4978 GST_DEBUG_OBJECT (src, "got interrupted");
4979 return GST_FLOW_FLUSHING;
4983 gchar *str = gst_rtsp_strresult (res);
4986 src->conninfo.connected = FALSE;
4987 if (res != GST_RTSP_EINTR) {
4988 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4989 ("Could not connect to server. (%s)", str));
4991 ret = GST_FLOW_ERROR;
4993 ret = GST_FLOW_FLUSHING;
4999 gchar *str = gst_rtsp_strresult (res);
5001 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5002 ("Could not receive message. (%s)", str));
5004 return GST_FLOW_ERROR;
5006 handle_request_failed:
5008 gchar *str = gst_rtsp_strresult (res);
5011 gst_rtsp_message_unset (&message);
5012 if (res != GST_RTSP_EINTR) {
5013 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5014 ("Could not handle server message. (%s)", str));
5016 ret = GST_FLOW_ERROR;
5018 ret = GST_FLOW_FLUSHING;
5024 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5025 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5026 ("The server closed the connection."));
5027 src->conninfo.connected = FALSE;
5028 gst_rtsp_message_unset (&message);
5029 return GST_FLOW_EOS;
5033 static GstRTSPResult
5034 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
5036 GstRTSPResult res = GST_RTSP_OK;
5039 GST_DEBUG_OBJECT (src, "doing reconnect");
5041 GST_OBJECT_LOCK (src);
5042 /* only restart when the pads were not yet activated, else we were
5043 * streaming over UDP */
5044 restart = src->need_activate;
5045 GST_OBJECT_UNLOCK (src);
5047 /* no need to restart, we're done */
5051 /* we can try only TCP now */
5052 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
5054 /* close and cleanup our state */
5055 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
5058 /* see if we have TCP left to try. Also don't try TCP when we were configured
5060 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
5063 /* We post a warning message now to inform the user
5064 * that nothing happened. It's most likely a firewall thing. */
5065 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5066 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5067 "firewall is blocking it. Retrying using a TCP connection.",
5068 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
5070 /* open new connection using tcp */
5071 if (gst_rtspsrc_open (src, async) < 0)
5074 /* start playback */
5075 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
5084 src->cur_protocols = 0;
5085 /* no transport possible, post an error and stop */
5086 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5087 ("Could not receive any UDP packets for %.4f seconds, maybe your "
5088 "firewall is blocking it. No other protocols to try.",
5089 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
5090 return GST_RTSP_ERROR;
5094 GST_DEBUG_OBJECT (src, "open failed");
5099 GST_DEBUG_OBJECT (src, "play failed");
5105 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
5109 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
5112 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
5115 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
5118 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
5126 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
5130 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
5133 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
5136 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
5139 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
5147 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
5151 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
5154 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
5157 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
5160 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
5168 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
5172 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
5175 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
5178 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
5181 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
5189 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
5191 if (ret == GST_RTSP_OK)
5192 gst_rtspsrc_loop_complete_cmd (src, cmd);
5193 else if (ret == GST_RTSP_EINTR)
5194 gst_rtspsrc_loop_cancel_cmd (src, cmd);
5196 gst_rtspsrc_loop_error_cmd (src, cmd);
5200 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
5203 gboolean flushed = FALSE;
5205 /* start new request */
5206 gst_rtspsrc_loop_start_cmd (src, cmd);
5208 GST_DEBUG_OBJECT (src, "sending cmd %s", cmd_to_string (cmd));
5210 GST_OBJECT_LOCK (src);
5211 old = src->pending_cmd;
5212 if (old == CMD_RECONNECT) {
5213 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
5214 cmd = CMD_RECONNECT;
5216 if (old != CMD_WAIT) {
5217 src->pending_cmd = CMD_WAIT;
5218 GST_OBJECT_UNLOCK (src);
5219 /* cancel previous request */
5220 GST_DEBUG_OBJECT (src, "cancel previous request %s", cmd_to_string (old));
5221 gst_rtspsrc_loop_cancel_cmd (src, old);
5222 GST_OBJECT_LOCK (src);
5224 src->pending_cmd = cmd;
5225 /* interrupt if allowed */
5226 if (src->busy_cmd & mask) {
5227 GST_DEBUG_OBJECT (src, "connection flush busy %s",
5228 cmd_to_string (src->busy_cmd));
5229 gst_rtspsrc_connection_flush (src, TRUE);
5232 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %s",
5233 cmd_to_string (src->busy_cmd));
5236 gst_task_start (src->task);
5237 GST_OBJECT_UNLOCK (src);
5243 gst_rtspsrc_loop (GstRTSPSrc * src)
5247 if (!src->conninfo.connection || !src->conninfo.connected)
5250 if (src->interleaved)
5251 ret = gst_rtspsrc_loop_interleaved (src);
5253 ret = gst_rtspsrc_loop_udp (src);
5255 if (ret != GST_FLOW_OK)
5263 GST_WARNING_OBJECT (src, "we are not connected");
5264 ret = GST_FLOW_FLUSHING;
5269 const gchar *reason = gst_flow_get_name (ret);
5271 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
5272 src->running = FALSE;
5273 if (ret == GST_FLOW_EOS) {
5274 /* perform EOS logic */
5275 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
5276 gst_element_post_message (GST_ELEMENT_CAST (src),
5277 gst_message_new_segment_done (GST_OBJECT_CAST (src),
5278 src->segment.format, src->segment.position));
5279 gst_rtspsrc_push_event (src,
5280 gst_event_new_segment_done (src->segment.format,
5281 src->segment.position));
5283 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5285 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
5286 /* for fatal errors we post an error message, post the error before the
5287 * EOS so the app knows about the error first. */
5288 GST_ELEMENT_ERROR (src, STREAM, FAILED,
5289 ("Internal data flow error."),
5290 ("streaming task paused, reason %s (%d)", reason, ret));
5291 gst_rtspsrc_push_event (src, gst_event_new_eos ());
5293 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
5298 #ifndef GST_DISABLE_GST_DEBUG
5299 static const gchar *
5300 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
5304 while (method != 0) {
5321 static const gchar *
5322 gst_rtspsrc_skip_lws (const gchar * s)
5324 while (g_ascii_isspace (*s))
5329 static const gchar *
5330 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
5332 while (s > start && g_ascii_isspace (*(s - 1)))
5337 static const gchar *
5338 gst_rtspsrc_skip_commas (const gchar * s)
5340 /* The grammar allows for multiple commas */
5341 while (g_ascii_isspace (*s) || *s == ',')
5346 static const gchar *
5347 gst_rtspsrc_skip_item (const gchar * s)
5349 gboolean quoted = FALSE;
5350 const gchar *start = s;
5352 /* A list item ends at the last non-whitespace character
5353 * before a comma which is not inside a quoted-string. Or at
5354 * the end of the string.
5360 if (*s == '\\' && *(s + 1))
5369 return gst_rtspsrc_unskip_lws (s, start);
5373 gst_rtsp_decode_quoted_string (gchar * quoted_string)
5377 src = quoted_string + 1;
5378 dst = quoted_string;
5379 while (*src && *src != '"') {
5380 if (*src == '\\' && *(src + 1))
5387 /* Extract the authentication tokens that the server provided for each method
5388 * into an array of structures and give those to the connection object.
5391 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
5392 const gchar * header, gboolean * stale)
5394 GSList *list = NULL, *iter;
5396 gchar *item, *eq, *name_end, *value;
5398 g_return_if_fail (stale != NULL);
5400 gst_rtsp_connection_clear_auth_params (conn);
5403 /* Parse a header whose content is described by RFC2616 as
5404 * "#something", where "something" does not itself contain commas,
5405 * except as part of quoted-strings, into a list of allocated strings.
5407 header = gst_rtspsrc_skip_commas (header);
5409 end = gst_rtspsrc_skip_item (header);
5410 list = g_slist_prepend (list, g_strndup (header, end - header));
5411 header = gst_rtspsrc_skip_commas (end);
5416 list = g_slist_reverse (list);
5417 for (iter = list; iter; iter = iter->next) {
5420 eq = strchr (item, '=');
5422 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
5423 if (name_end == item) {
5424 /* That's no good... */
5431 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
5433 gst_rtsp_decode_quoted_string (value);
5437 if (value && strcmp (item, "stale") == 0 && strcmp (value, "TRUE") == 0)
5439 gst_rtsp_connection_set_auth_param (conn, item, value);
5443 g_slist_free (list);
5446 /* Parse a WWW-Authenticate Response header and determine the
5447 * available authentication methods
5449 * This code should also cope with the fact that each WWW-Authenticate
5450 * header can contain multiple challenge methods + tokens
5452 * At the moment, for Basic auth, we just do a minimal check and don't
5453 * even parse out the realm */
5455 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
5456 GstRTSPConnection * conn, gboolean * stale)
5460 g_return_if_fail (hdr != NULL);
5461 g_return_if_fail (methods != NULL);
5462 g_return_if_fail (stale != NULL);
5464 /* Skip whitespace at the start of the string */
5465 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
5467 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
5468 *methods |= GST_RTSP_AUTH_BASIC;
5469 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
5470 *methods |= GST_RTSP_AUTH_DIGEST;
5471 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
5476 * gst_rtspsrc_setup_auth:
5477 * @src: the rtsp source
5479 * Configure a username and password and auth method on the
5480 * connection object based on a response we received from the
5483 * Currently, this requires that a username and password were supplied
5484 * in the uri. In the future, they may be requested on demand by sending
5485 * a message up the bus.
5487 * Returns: TRUE if authentication information could be set up correctly.
5490 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
5494 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
5495 GstRTSPAuthMethod method;
5496 GstRTSPResult auth_result;
5498 GstRTSPConnection *conn;
5500 gboolean stale = FALSE;
5502 conn = src->conninfo.connection;
5504 /* Identify the available auth methods and see if any are supported */
5505 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
5506 &hdr, 0) == GST_RTSP_OK) {
5507 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
5510 if (avail_methods == GST_RTSP_AUTH_NONE)
5511 goto no_auth_available;
5513 /* For digest auth, if the response indicates that the session
5514 * data are stale, we just update them in the connection object and
5515 * return TRUE to retry the request */
5517 src->tried_url_auth = FALSE;
5519 url = gst_rtsp_connection_get_url (conn);
5521 /* Do we have username and password available? */
5522 if (url != NULL && !src->tried_url_auth && url->user != NULL
5523 && url->passwd != NULL) {
5526 src->tried_url_auth = TRUE;
5527 GST_DEBUG_OBJECT (src,
5528 "Attempting authentication using credentials from the URL");
5530 user = src->user_id;
5531 pass = src->user_pw;
5532 GST_DEBUG_OBJECT (src,
5533 "Attempting authentication using credentials from the properties");
5536 /* FIXME: If the url didn't contain username and password or we tried them
5537 * already, request a username and passwd from the application via some kind
5538 * of credentials request message */
5540 /* If we don't have a username and passwd at this point, bail out. */
5541 if (user == NULL || pass == NULL)
5544 /* Try to configure for each available authentication method, strongest to
5546 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5547 /* Check if this method is available on the server */
5548 if ((method & avail_methods) == 0)
5551 /* Pass the credentials to the connection to try on the next request */
5552 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5553 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5554 * ignore it and end up retrying later */
5555 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5556 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5557 gst_rtsp_auth_method_to_string (method));
5562 if (method == GST_RTSP_AUTH_NONE)
5563 goto no_auth_available;
5569 /* Output an error indicating that we couldn't connect because there were
5570 * no supported authentication protocols */
5571 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5572 ("No supported authentication protocol was found"));
5577 /* We don't fire an error message, we just return FALSE and let the
5578 * normal NOT_AUTHORIZED error be propagated */
5583 static GstRTSPResult
5584 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5585 GstRTSPMessage * request, GstRTSPMessage * response,
5586 GstRTSPStatusCode * code)
5589 GstRTSPStatusCode thecode;
5590 gchar *content_base = NULL;
5594 if (!src->short_header)
5595 gst_rtsp_ext_list_before_send (src->extensions, request);
5597 GST_DEBUG_OBJECT (src, "sending message");
5600 gst_rtsp_message_dump (request);
5602 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
5606 gst_rtsp_connection_reset_timeout (conn);
5609 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
5614 gst_rtsp_message_dump (response);
5616 switch (response->type) {
5617 case GST_RTSP_MESSAGE_REQUEST:
5618 res = gst_rtspsrc_handle_request (src, conn, response);
5619 if (res == GST_RTSP_EEOF)
5622 goto handle_request_failed;
5624 case GST_RTSP_MESSAGE_RESPONSE:
5625 /* ok, a response is good */
5626 GST_DEBUG_OBJECT (src, "received response message");
5628 case GST_RTSP_MESSAGE_DATA:
5629 /* get next response */
5630 GST_DEBUG_OBJECT (src, "handle data response message");
5631 gst_rtspsrc_handle_data (src, response);
5634 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5639 thecode = response->type_data.response.code;
5641 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5643 /* if the caller wanted the result code, we store it. */
5647 /* If the request didn't succeed, bail out before doing any more */
5648 if (thecode != GST_RTSP_STS_OK)
5651 /* store new content base if any */
5652 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5655 g_free (src->content_base);
5656 src->content_base = g_strdup (content_base);
5658 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5665 gchar *str = gst_rtsp_strresult (res);
5667 if (res != GST_RTSP_EINTR) {
5668 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5669 ("Could not send message. (%s)", str));
5671 GST_WARNING_OBJECT (src, "send interrupted");
5680 GST_WARNING_OBJECT (src, "server closed connection");
5681 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5683 /* if reconnect succeeds, try again */
5685 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5689 /* only try once after reconnect, then fallthrough and error out */
5692 gchar *str = gst_rtsp_strresult (res);
5694 if (res != GST_RTSP_EINTR) {
5695 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5696 ("Could not receive message. (%s)", str));
5698 GST_WARNING_OBJECT (src, "receive interrupted");
5706 handle_request_failed:
5708 /* ERROR was posted */
5709 gst_rtsp_message_unset (response);
5714 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5715 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5716 ("The server closed the connection."));
5717 gst_rtsp_message_unset (response);
5724 * @src: the rtsp source
5725 * @conn: the connection to send on
5726 * @request: must point to a valid request
5727 * @response: must point to an empty #GstRTSPMessage
5728 * @code: an optional code result
5730 * send @request and retrieve the response in @response. optionally @code can be
5731 * non-NULL in which case it will contain the status code of the response.
5733 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5734 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5736 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5737 * @response message) if the response code was not 200 (OK).
5739 * If the attempt results in an authentication failure, then this will attempt
5740 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5743 * Returns: #GST_RTSP_OK if the processing was successful.
5745 static GstRTSPResult
5746 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5747 GstRTSPMessage * request, GstRTSPMessage * response,
5748 GstRTSPStatusCode * code)
5750 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5751 GstRTSPResult res = GST_RTSP_ERROR;
5754 GstRTSPMethod method = GST_RTSP_INVALID;
5760 /* make sure we don't loop forever */
5764 /* save method so we can disable it when the server complains */
5765 method = request->type_data.request.method;
5768 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5772 case GST_RTSP_STS_UNAUTHORIZED:
5773 if (gst_rtspsrc_setup_auth (src, response)) {
5774 /* Try the request/response again after configuring the auth info
5782 } while (retry == TRUE);
5784 /* If the user requested the code, let them handle errors, otherwise
5785 * post an error below */
5788 else if (int_code != GST_RTSP_STS_OK)
5789 goto error_response;
5796 GST_DEBUG_OBJECT (src, "got error %d", res);
5801 res = GST_RTSP_ERROR;
5803 switch (response->type_data.response.code) {
5804 case GST_RTSP_STS_NOT_FOUND:
5805 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5806 response->type_data.response.reason));
5808 case GST_RTSP_STS_UNAUTHORIZED:
5809 GST_ELEMENT_ERROR (src, RESOURCE, NOT_AUTHORIZED, (NULL), ("%s",
5810 response->type_data.response.reason));
5812 case GST_RTSP_STS_MOVED_PERMANENTLY:
5813 case GST_RTSP_STS_MOVE_TEMPORARILY:
5815 gchar *new_location;
5816 GstRTSPLowerTrans transports;
5818 GST_DEBUG_OBJECT (src, "got redirection");
5819 /* if we don't have a Location Header, we must error */
5820 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5821 &new_location, 0) < 0)
5824 /* When we receive a redirect result, we go back to the INIT state after
5825 * parsing the new URI. The caller should do the needed steps to issue
5826 * a new setup when it detects this state change. */
5827 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5829 /* save current transports */
5830 if (src->conninfo.url)
5831 transports = src->conninfo.url->transports;
5833 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5835 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5837 /* set old transports */
5838 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5839 src->conninfo.url->transports = transports;
5841 src->need_redirect = TRUE;
5842 src->state = GST_RTSP_STATE_INIT;
5846 case GST_RTSP_STS_NOT_ACCEPTABLE:
5847 case GST_RTSP_STS_NOT_IMPLEMENTED:
5848 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5849 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5850 gst_rtsp_method_as_text (method));
5851 src->methods &= ~method;
5855 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5856 ("Got error response: %d (%s).", response->type_data.response.code,
5857 response->type_data.response.reason));
5860 /* if we return ERROR we should unset the response ourselves */
5861 if (res == GST_RTSP_ERROR)
5862 gst_rtsp_message_unset (response);
5868 static GstRTSPResult
5869 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5870 GstRTSPMessage * response, GstRTSPSrc * src)
5872 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5877 /* parse the response and collect all the supported methods. We need this
5878 * information so that we don't try to send an unsupported request to the
5882 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5884 GstRTSPHeaderField field;
5888 /* reset supported methods */
5891 /* Try Allow Header first */
5892 field = GST_RTSP_HDR_ALLOW;
5895 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5896 if (indx == 0 && !respoptions) {
5897 /* if no Allow header was found then try the Public header... */
5898 field = GST_RTSP_HDR_PUBLIC;
5899 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5904 src->methods |= gst_rtsp_options_from_text (respoptions);
5909 if (src->methods == 0) {
5910 /* neither Allow nor Public are required, assume the server supports
5911 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5913 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5914 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5916 /* always assume PLAY, FIXME, extensions should be able to override
5918 src->methods |= GST_RTSP_PLAY;
5919 /* also assume it will support Range */
5920 src->seekable = TRUE;
5922 /* we need describe and setup */
5923 if (!(src->methods & GST_RTSP_DESCRIBE))
5925 if (!(src->methods & GST_RTSP_SETUP))
5933 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5934 ("Server does not support DESCRIBE."));
5939 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5940 ("Server does not support SETUP."));
5945 /* masks to be kept in sync with the hardcoded protocol order of preference
5947 static const guint protocol_masks[] = {
5948 GST_RTSP_LOWER_TRANS_UDP,
5949 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5950 GST_RTSP_LOWER_TRANS_TCP,
5954 static GstRTSPResult
5955 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5956 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
5960 gboolean add_udp_str;
5965 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5970 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5972 /* extension listed transports, use those */
5973 if (*transports != NULL)
5976 /* it's the default */
5977 add_udp_str = FALSE;
5979 /* the default RTSP transports */
5980 result = g_string_new ("RTP");
5983 case GST_RTSP_PROFILE_AVP:
5984 g_string_append (result, "/AVP");
5986 case GST_RTSP_PROFILE_SAVP:
5987 g_string_append (result, "/SAVP");
5989 case GST_RTSP_PROFILE_AVPF:
5990 g_string_append (result, "/AVPF");
5992 case GST_RTSP_PROFILE_SAVPF:
5993 g_string_append (result, "/SAVPF");
5999 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
6000 GST_DEBUG_OBJECT (src, "adding UDP unicast");
6002 g_string_append (result, "/UDP");
6003 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
6004 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
6005 GST_DEBUG_OBJECT (src, "adding UDP multicast");
6006 /* we don't have to allocate any UDP ports yet, if the selected transport
6007 * turns out to be multicast we can create them and join the multicast
6008 * group indicated in the transport reply */
6010 g_string_append (result, "/UDP");
6011 g_string_append (result, ";multicast");
6012 if (src->next_port_num != 0) {
6013 if (src->client_port_range.max > 0 &&
6014 src->next_port_num >= src->client_port_range.max)
6017 g_string_append_printf (result, ";client_port=%d-%d",
6018 src->next_port_num, src->next_port_num + 1);
6020 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
6021 GST_DEBUG_OBJECT (src, "adding TCP");
6023 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
6025 *transports = g_string_free (result, FALSE);
6027 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
6034 GST_ERROR ("extension gave error %d", res);
6039 GST_ERROR ("no more ports available");
6040 return GST_RTSP_ERROR;
6044 static GstRTSPResult
6045 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
6046 gint orig_rtpport, gint orig_rtcpport)
6049 gint nr_udp, nr_int;
6051 gint rtpport = 0, rtcpport = 0;
6054 src = stream->parent;
6056 /* find number of placeholders first */
6057 if (strstr (*transports, "%%i2"))
6059 else if (strstr (*transports, "%%i1"))
6064 if (strstr (*transports, "%%u2"))
6066 else if (strstr (*transports, "%%u1"))
6071 if (nr_udp == 0 && nr_int == 0)
6075 if (!orig_rtpport || !orig_rtcpport) {
6076 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
6079 rtpport = orig_rtpport;
6080 rtcpport = orig_rtcpport;
6084 str = g_string_new ("");
6086 while ((next = strstr (p, "%%"))) {
6087 g_string_append_len (str, p, next - p);
6088 if (next[2] == 'u') {
6090 g_string_append_printf (str, "%d", rtpport);
6091 else if (next[3] == '2')
6092 g_string_append_printf (str, "%d", rtcpport);
6094 if (next[2] == 'i') {
6096 g_string_append_printf (str, "%d", src->free_channel);
6097 else if (next[3] == '2')
6098 g_string_append_printf (str, "%d", src->free_channel + 1);
6103 /* append final part */
6104 g_string_append (str, p);
6106 g_free (*transports);
6107 *transports = g_string_free (str, FALSE);
6115 GST_ERROR ("failed to allocate udp ports");
6116 return GST_RTSP_ERROR;
6121 enc_key_length_from_cipher_name (const gchar * cipher)
6123 if (g_strcmp0 (cipher, "aes-128-icm") == 0)
6124 return AES_128_KEY_LEN;
6125 else if (g_strcmp0 (cipher, "aes-256-icm") == 0)
6126 return AES_256_KEY_LEN;
6128 GST_ERROR ("encryption algorithm '%s' not supported", cipher);
6134 auth_key_length_from_auth_name (const gchar * auth)
6136 if (g_strcmp0 (auth, "hmac-sha1-32") == 0)
6137 return HMAC_32_KEY_LEN;
6138 else if (g_strcmp0 (auth, "hmac-sha1-80") == 0)
6139 return HMAC_80_KEY_LEN;
6141 GST_ERROR ("authentication algorithm '%s' not supported", auth);
6147 signal_get_srtcp_params (GstRTSPSrc * src, GstRTSPStream * stream)
6149 GstCaps *caps = NULL;
6151 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_REQUEST_RTCP_KEY], 0,
6155 GST_DEBUG_OBJECT (src, "SRTP parameters received");
6161 default_srtcp_params (void)
6169 /* create a random key */
6170 key_data = g_malloc (KEY_SIZE);
6171 for (i = 0; i < KEY_SIZE; i += 4)
6172 GST_WRITE_UINT32_BE (key_data + i, g_random_int ());
6174 buf = gst_buffer_new_wrapped (key_data, KEY_SIZE);
6176 caps = gst_caps_new_simple ("application/x-srtp",
6177 "srtp-key", GST_TYPE_BUFFER, buf,
6178 "srtcp-cipher", G_TYPE_STRING, "aes-128-icm",
6179 "srtcp-auth", G_TYPE_STRING, "hmac-sha1-80", NULL);
6181 gst_buffer_unref (buf);
6187 gst_rtspsrc_stream_make_keymgmt (GstRTSPSrc * src, GstRTSPStream * stream)
6190 gchar *result, *base64;
6193 GstMIKEYMessage *msg;
6194 GstMIKEYPayload *payload, *pkd;
6200 const gchar *srtcpcipher, *srtcpauth;
6202 stream->srtcpparams = signal_get_srtcp_params (src, stream);
6203 if (stream->srtcpparams == NULL)
6204 stream->srtcpparams = default_srtcp_params ();
6206 s = gst_caps_get_structure (stream->srtcpparams, 0);
6208 srtcpcipher = gst_structure_get_string (s, "srtcp-cipher");
6209 srtcpauth = gst_structure_get_string (s, "srtcp-auth");
6210 val = gst_structure_get_value (s, "srtp-key");
6212 if (srtcpcipher == NULL || srtcpauth == NULL || val == NULL) {
6213 GST_ERROR_OBJECT (src, "could not find the right SRTP parameters in caps");
6217 srtpkey = gst_value_get_buffer (val);
6219 msg = gst_mikey_message_new ();
6220 /* unencrypted MIKEY message, we send this over TLS so this is allowed */
6221 gst_mikey_message_set_info (msg, GST_MIKEY_VERSION, GST_MIKEY_TYPE_PSK_INIT,
6222 FALSE, GST_MIKEY_PRF_MIKEY_1, g_random_int (), GST_MIKEY_MAP_TYPE_SRTP);
6223 /* add policy '0' for our SSRC */
6224 gst_mikey_message_add_cs_srtp (msg, 0, stream->send_ssrc, 0);
6225 /* timestamp is now */
6226 gst_mikey_message_add_t_now_ntp_utc (msg);
6227 /* add some random data */
6228 gst_mikey_message_add_rand_len (msg, 16);
6230 /* the policy '0' is SRTP */
6231 payload = gst_mikey_payload_new (GST_MIKEY_PT_SP);
6232 gst_mikey_payload_sp_set (payload, 0, GST_MIKEY_SEC_PROTO_SRTP);
6234 /* only AES-CM is supported */
6236 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_ALG, 1, &byte);
6237 /* encryption key length */
6238 byte = enc_key_length_from_cipher_name (srtcpcipher);
6239 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_ENC_KEY_LEN, 1,
6241 /* only HMAC-SHA1 */
6242 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_ALG, 1,
6244 /* authentication key length */
6245 byte = auth_key_length_from_auth_name (srtcpauth);
6246 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_AUTH_KEY_LEN, 1,
6248 /* we enable encryption on RTP and RTCP */
6249 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_ENC, 1,
6251 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTCP_ENC, 1,
6253 /* we enable authentication on RTP and RTCP */
6254 gst_mikey_payload_sp_add_param (payload, GST_MIKEY_SP_SRTP_SRTP_AUTH, 1,
6256 gst_mikey_message_add_payload (msg, payload);
6258 /* make unencrypted KEMAC */
6259 payload = gst_mikey_payload_new (GST_MIKEY_PT_KEMAC);
6260 gst_mikey_payload_kemac_set (payload, GST_MIKEY_ENC_NULL, GST_MIKEY_MAC_NULL);
6261 /* add the key in KEMAC */
6262 pkd = gst_mikey_payload_new (GST_MIKEY_PT_KEY_DATA);
6263 gst_buffer_map (srtpkey, &info, GST_MAP_READ);
6264 gst_mikey_payload_key_data_set_key (pkd, GST_MIKEY_KD_TEK, info.size,
6266 gst_buffer_unmap (srtpkey, &info);
6267 gst_mikey_payload_kemac_add_sub (payload, pkd);
6268 gst_mikey_message_add_payload (msg, payload);
6270 /* now serialize this to bytes */
6271 bytes = gst_mikey_message_to_bytes (msg, NULL, NULL);
6272 gst_mikey_message_unref (msg);
6273 /* and make it into base64 */
6274 data = g_bytes_get_data (bytes, &size);
6275 base64 = g_base64_encode (data, size);
6276 g_bytes_unref (bytes);
6278 result = g_strdup_printf ("prot=mikey;uri=\"%s\";data=\"%s\"",
6279 stream->conninfo.location, base64);
6286 /* Perform the SETUP request for all the streams.
6288 * We ask the server for a specific transport, which initially includes all the
6289 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
6290 * two local UDP ports that we send to the server.
6292 * Once the server replied with a transport, we configure the other streams
6293 * with the same transport.
6295 * This function will also configure the stream for the selected transport,
6296 * which basically means creating the pipeline.
6298 static GstRTSPResult
6299 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
6302 GstRTSPResult res = GST_RTSP_ERROR;
6303 GstRTSPMessage request = { 0 };
6304 GstRTSPMessage response = { 0 };
6305 GstRTSPStream *stream = NULL;
6306 GstRTSPLowerTrans protocols;
6307 GstRTSPStatusCode code;
6308 gboolean unsupported_real = FALSE;
6309 gint rtpport, rtcpport;
6313 if (src->conninfo.connection) {
6314 url = gst_rtsp_connection_get_url (src->conninfo.connection);
6315 /* we initially allow all configured lower transports. based on the URL
6316 * transports and the replies from the server we narrow them down. */
6317 protocols = url->transports & src->cur_protocols;
6320 protocols = src->cur_protocols;
6326 /* reset some state */
6327 src->free_channel = 0;
6328 src->interleaved = FALSE;
6329 src->need_activate = FALSE;
6330 /* keep track of next port number, 0 is random */
6331 src->next_port_num = src->client_port_range.min;
6332 rtpport = rtcpport = 0;
6334 if (G_UNLIKELY (src->streams == NULL))
6337 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6338 GstRTSPConnection *conn;
6345 stream = (GstRTSPStream *) walk->data;
6347 caps = stream_get_caps_for_pt (stream, stream->default_pt);
6349 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
6353 if (stream->skipped) {
6354 GST_DEBUG_OBJECT (src, "skipping stream %p", stream);
6358 /* see if we need to configure this stream */
6359 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
6360 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
6365 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
6366 stream->id, caps, &selected);
6368 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
6372 /* merge/overwrite global caps */
6377 s = gst_caps_get_structure (caps, 0);
6379 num = gst_structure_n_fields (src->props);
6380 for (j = 0; j < num; j++) {
6384 name = gst_structure_nth_field_name (src->props, j);
6385 val = gst_structure_get_value (src->props, name);
6386 gst_structure_set_value (s, name, val);
6388 GST_DEBUG_OBJECT (src, "copied %s", name);
6392 /* skip setup if we have no URL for it */
6393 if (stream->conninfo.location == NULL) {
6394 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
6398 if (src->conninfo.connection == NULL) {
6399 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
6400 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
6403 conn = stream->conninfo.connection;
6405 conn = src->conninfo.connection;
6407 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
6408 stream->conninfo.location);
6410 /* if we have a multicast connection, only suggest multicast from now on */
6411 if (stream->is_multicast)
6412 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
6415 /* first selectable protocol */
6416 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6418 if (!protocol_masks[mask])
6422 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
6423 protocol_masks[mask]);
6424 /* create a string with first transport in line */
6426 res = gst_rtspsrc_create_transports_string (src,
6427 protocols & protocol_masks[mask], stream->profile, &transports);
6428 if (res < 0 || transports == NULL)
6429 goto setup_transport_failed;
6431 if (strlen (transports) == 0) {
6432 g_free (transports);
6433 GST_DEBUG_OBJECT (src, "no transports found");
6438 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
6440 /* replace placeholders with real values, this function will optionally
6441 * allocate UDP ports and other info needed to execute the setup request */
6442 res = gst_rtspsrc_prepare_transports (stream, &transports,
6443 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
6445 g_free (transports);
6446 goto setup_transport_failed;
6449 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
6451 /* create SETUP request */
6453 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
6454 stream->conninfo.location);
6456 g_free (transports);
6457 goto create_request_failed;
6460 /* select transport */
6461 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
6464 if (stream->profile == GST_RTSP_PROFILE_SAVP ||
6465 stream->profile == GST_RTSP_PROFILE_SAVPF) {
6466 hval = gst_rtspsrc_stream_make_keymgmt (src, stream);
6467 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_KEYMGMT, hval);
6470 /* if the user wants a non default RTP packet size we add the blocksize
6472 if (src->rtp_blocksize > 0) {
6473 hval = g_strdup_printf ("%d", src->rtp_blocksize);
6474 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
6478 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
6481 /* handle the code ourselves */
6482 res = gst_rtspsrc_send (src, conn, &request, &response, &code);
6487 case GST_RTSP_STS_OK:
6489 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
6490 gst_rtsp_message_unset (&request);
6491 gst_rtsp_message_unset (&response);
6492 /* cleanup of leftover transport */
6493 gst_rtspsrc_stream_free_udp (stream);
6494 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
6495 * we might be in this case */
6496 if (stream->container && rtpport && rtcpport && !retry) {
6497 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
6502 /* this transport did not go down well, but we may have others to try
6503 * that we did not send yet, try those and only give up then
6504 * but not without checking for lost cause/extension so we can
6505 * post a nicer/more useful error message later */
6506 if (!unsupported_real)
6507 unsupported_real = stream->is_real;
6508 /* select next available protocol, give up on this stream if none */
6510 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
6512 if (!protocol_masks[mask] || unsupported_real)
6517 /* cleanup of leftover transport and move to the next stream */
6518 gst_rtspsrc_stream_free_udp (stream);
6519 goto response_error;
6522 /* parse response transport */
6524 gchar *resptrans = NULL;
6525 GstRTSPTransport transport = { 0 };
6527 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
6530 gst_rtspsrc_stream_free_udp (stream);
6534 /* parse transport, go to next stream on parse error */
6535 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
6536 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
6540 /* update allowed transports for other streams. once the transport of
6541 * one stream has been determined, we make sure that all other streams
6542 * are configured in the same way */
6543 switch (transport.lower_transport) {
6544 case GST_RTSP_LOWER_TRANS_TCP:
6545 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
6546 protocols = GST_RTSP_LOWER_TRANS_TCP;
6547 src->interleaved = TRUE;
6548 /* update free channels */
6550 MAX (transport.interleaved.min, src->free_channel);
6552 MAX (transport.interleaved.max, src->free_channel);
6553 src->free_channel++;
6555 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
6556 /* only allow multicast for other streams */
6557 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
6558 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
6559 /* if the server selected our ports, increment our counters so that
6560 * we select a new port later */
6561 if (src->next_port_num == transport.port.min &&
6562 src->next_port_num + 1 == transport.port.max) {
6563 src->next_port_num += 2;
6566 case GST_RTSP_LOWER_TRANS_UDP:
6567 /* only allow unicast for other streams */
6568 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
6569 protocols = GST_RTSP_LOWER_TRANS_UDP;
6572 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
6573 transport.lower_transport);
6577 if (!src->interleaved || !retry) {
6578 /* now configure the stream with the selected transport */
6579 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
6580 GST_DEBUG_OBJECT (src,
6581 "could not configure stream %p transport, skipping stream",
6584 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
6585 /* retain the first allocated UDP port pair */
6586 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
6587 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
6590 /* we need to activate at least one streams when we detect activity */
6591 src->need_activate = TRUE;
6593 /* stream is setup now */
6594 stream->setup = TRUE;
6599 GstRTSPStream *sskip;
6601 skip = g_list_next (skip);
6605 sskip = (GstRTSPStream *) skip->data;
6607 /* skip all streams with the same control url */
6608 if (g_str_equal (stream->conninfo.location, sskip->conninfo.location)) {
6609 GST_DEBUG_OBJECT (src, "found stream %p with same control %s",
6610 sskip, sskip->conninfo.location);
6611 sskip->skipped = TRUE;
6616 /* clean up our transport struct */
6617 gst_rtsp_transport_init (&transport);
6618 /* clean up used RTSP messages */
6619 gst_rtsp_message_unset (&request);
6620 gst_rtsp_message_unset (&response);
6624 /* store the transport protocol that was configured */
6625 src->cur_protocols = protocols;
6627 gst_rtsp_ext_list_stream_select (src->extensions, url);
6629 /* if there is nothing to activate, error out */
6630 if (!src->need_activate)
6631 goto nothing_to_activate;
6638 /* no transport possible, post an error and stop */
6639 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
6640 ("Could not connect to server, no protocols left"));
6641 return GST_RTSP_ERROR;
6645 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6646 ("SDP contains no streams"));
6647 return GST_RTSP_ERROR;
6649 create_request_failed:
6651 gchar *str = gst_rtsp_strresult (res);
6653 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6654 ("Could not create request. (%s)", str));
6658 setup_transport_failed:
6660 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6661 ("Could not setup transport."));
6662 res = GST_RTSP_ERROR;
6667 const gchar *str = gst_rtsp_status_as_text (code);
6669 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6670 ("Error (%d): %s", code, GST_STR_NULL (str)));
6671 res = GST_RTSP_ERROR;
6676 gchar *str = gst_rtsp_strresult (res);
6678 if (res != GST_RTSP_EINTR) {
6679 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6680 ("Could not send message. (%s)", str));
6682 GST_WARNING_OBJECT (src, "send interrupted");
6689 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6690 ("Server did not select transport."));
6691 res = GST_RTSP_ERROR;
6694 nothing_to_activate:
6696 /* none of the available error codes is really right .. */
6697 if (unsupported_real) {
6698 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6699 (_("No supported stream was found. You might need to install a "
6700 "GStreamer RTSP extension plugin for Real media streams.")),
6703 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
6704 (_("No supported stream was found. You might need to allow "
6705 "more transport protocols or may otherwise be missing "
6706 "the right GStreamer RTSP extension plugin.")), (NULL));
6708 return GST_RTSP_ERROR;
6712 gst_rtsp_message_unset (&request);
6713 gst_rtsp_message_unset (&response);
6719 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
6720 GstSegment * segment)
6723 GstRTSPTimeRange *therange;
6726 gst_rtsp_range_free (src->range);
6728 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
6729 GST_DEBUG_OBJECT (src, "parsed range %s", range);
6730 src->range = therange;
6732 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6734 gst_segment_init (segment, GST_FORMAT_TIME);
6738 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6739 therange->min.type, therange->min.seconds, therange->max.type,
6740 therange->max.seconds);
6742 if (therange->min.type == GST_RTSP_TIME_NOW)
6744 else if (therange->min.type == GST_RTSP_TIME_END)
6747 seconds = therange->min.seconds * GST_SECOND;
6749 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6750 GST_TIME_ARGS (seconds));
6752 /* we need to start playback without clipping from the position reported by
6754 segment->start = seconds;
6755 segment->position = seconds;
6757 if (therange->max.type == GST_RTSP_TIME_NOW)
6759 else if (therange->max.type == GST_RTSP_TIME_END)
6762 seconds = therange->max.seconds * GST_SECOND;
6764 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6765 GST_TIME_ARGS (seconds));
6767 /* live (WMS) server might send overflowed large max as its idea of infinity,
6768 * compensate to prevent problems later on */
6769 if (seconds != -1 && seconds < 0) {
6771 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6774 /* live (WMS) might send min == max, which is not worth recording */
6775 if (segment->duration == -1 && seconds == segment->start)
6778 /* don't change duration with unknown value, we might have a valid value
6779 * there that we want to keep. */
6781 segment->duration = seconds;
6786 /* Parse clock profived by the server with following syntax:
6788 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6791 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6793 gboolean res = FALSE;
6795 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6796 gchar **fields = NULL, **parts = NULL;
6797 gchar *remote_ip, *str;
6799 GstClockTime base_time;
6802 fields = g_strsplit (gstclock, " ", 0);
6804 /* wrapped clock, not very interesting for now */
6805 if (fields[1] == NULL)
6808 /* remote IP address and port */
6809 if ((str = fields[2]) == NULL)
6812 parts = g_strsplit (str, ":", 0);
6814 if ((remote_ip = parts[0]) == NULL)
6817 if ((str = parts[1]) == NULL)
6825 if ((str = fields[3]) == NULL)
6828 base_time = g_ascii_strtoull (str, NULL, 10);
6831 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
6834 if (src->provided_clock)
6835 gst_object_unref (src->provided_clock);
6836 src->provided_clock = netclock;
6838 gst_element_post_message (GST_ELEMENT_CAST (src),
6839 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
6840 src->provided_clock, TRUE));
6844 g_strfreev (fields);
6850 /* must be called with the RTSP state lock */
6851 static GstRTSPResult
6852 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
6858 /* prepare global stream caps properties */
6860 gst_structure_remove_all_fields (src->props);
6862 src->props = gst_structure_new_empty ("RTSPProperties");
6865 gst_sdp_message_dump (sdp);
6867 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
6869 /* let the app inspect and change the SDP */
6870 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6872 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6874 /* parse range for duration reporting. */
6879 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6883 /* keep track of the range and configure it in the segment */
6884 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6888 /* parse clock information. This is GStreamer specific, a server can tell the
6889 * client what clock it is using and wrap that in a network clock. The
6890 * advantage of that is that we can slave to it. */
6892 const gchar *gstclock;
6895 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6896 if (gstclock == NULL)
6899 /* parse the clock and expose it in the provide_clock method */
6900 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6904 /* try to find a global control attribute. Note that a '*' means that we should
6905 * do aggregate control with the current url (so we don't do anything and
6906 * leave the current connection as is) */
6908 const gchar *control;
6911 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6912 if (control == NULL)
6915 /* only take fully qualified urls */
6916 if (g_str_has_prefix (control, "rtsp://"))
6920 g_free (src->conninfo.location);
6921 src->conninfo.location = g_strdup (control);
6922 /* make a connection for this, if there was a connection already, nothing
6924 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6925 GST_ERROR_OBJECT (src, "could not connect");
6928 /* we need to keep the control url separate from the connection url because
6929 * the rules for constructing the media control url need it */
6930 g_free (src->control);
6931 src->control = g_strdup (control);
6934 /* create streams */
6935 n_streams = gst_sdp_message_medias_len (sdp);
6936 for (i = 0; i < n_streams; i++) {
6937 gst_rtspsrc_create_stream (src, sdp, i);
6940 src->state = GST_RTSP_STATE_INIT;
6943 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6946 /* reset our state */
6947 src->need_range = TRUE;
6950 src->state = GST_RTSP_STATE_READY;
6957 GST_ERROR_OBJECT (src, "setup failed");
6958 gst_rtspsrc_cleanup (src);
6963 static GstRTSPResult
6964 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6968 GstRTSPMessage request = { 0 };
6969 GstRTSPMessage response = { 0 };
6972 gchar *respcont = NULL;
6975 src->need_redirect = FALSE;
6977 /* can't continue without a valid url */
6978 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6979 res = GST_RTSP_EINVAL;
6982 src->tried_url_auth = FALSE;
6984 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6985 goto connect_failed;
6987 /* create OPTIONS */
6988 GST_DEBUG_OBJECT (src, "create options...");
6990 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
6991 src->conninfo.url_str);
6993 goto create_request_failed;
6996 GST_DEBUG_OBJECT (src, "send options...");
6999 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
7002 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
7007 if (!gst_rtspsrc_parse_methods (src, &response))
7010 /* create DESCRIBE */
7011 GST_DEBUG_OBJECT (src, "create describe...");
7013 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
7014 src->conninfo.url_str);
7016 goto create_request_failed;
7018 /* we only accept SDP for now */
7019 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
7023 GST_DEBUG_OBJECT (src, "send describe...");
7026 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
7029 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
7033 /* we only perform redirect for the describe, currently */
7034 if (src->need_redirect) {
7035 /* close connection, we don't have to send a TEARDOWN yet, ignore the
7037 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7039 gst_rtsp_message_unset (&request);
7040 gst_rtsp_message_unset (&response);
7046 /* it could be that the DESCRIBE method was not implemented */
7047 if (!src->methods & GST_RTSP_DESCRIBE)
7050 /* check if reply is SDP */
7051 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
7053 /* could not be set but since the request returned OK, we assume it
7054 * was SDP, else check it. */
7056 if (g_ascii_strcasecmp (respcont, "application/sdp") != 0)
7057 goto wrong_content_type;
7060 /* get message body and parse as SDP */
7061 gst_rtsp_message_get_body (&response, &data, &size);
7062 if (data == NULL || size == 0)
7065 GST_DEBUG_OBJECT (src, "parse SDP...");
7066 gst_sdp_message_new (sdp);
7067 gst_sdp_message_parse_buffer (data, size, *sdp);
7069 /* clean up any messages */
7070 gst_rtsp_message_unset (&request);
7071 gst_rtsp_message_unset (&response);
7078 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
7079 ("No valid RTSP URL was provided"));
7084 gchar *str = gst_rtsp_strresult (res);
7086 if (res != GST_RTSP_EINTR) {
7087 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
7088 ("Failed to connect. (%s)", str));
7090 GST_WARNING_OBJECT (src, "connect interrupted");
7095 create_request_failed:
7097 gchar *str = gst_rtsp_strresult (res);
7099 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7100 ("Could not create request. (%s)", str));
7106 /* Don't post a message - the rtsp_send method will have
7107 * taken care of it because we passed NULL for the response code */
7112 /* error was posted */
7113 res = GST_RTSP_ERROR;
7118 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7119 ("Server does not support SDP, got %s.", respcont));
7120 res = GST_RTSP_ERROR;
7125 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
7126 ("Server can not provide an SDP."));
7127 res = GST_RTSP_ERROR;
7132 if (src->conninfo.connection) {
7133 GST_DEBUG_OBJECT (src, "free connection");
7134 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7136 gst_rtsp_message_unset (&request);
7137 gst_rtsp_message_unset (&response);
7142 static GstRTSPResult
7143 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
7148 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
7150 if (src->sdp == NULL) {
7151 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
7155 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
7160 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
7167 GST_WARNING_OBJECT (src, "can't get sdp");
7168 src->open_error = TRUE;
7173 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
7174 src->open_error = TRUE;
7179 static GstRTSPResult
7180 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
7182 GstRTSPMessage request = { 0 };
7183 GstRTSPMessage response = { 0 };
7184 GstRTSPResult res = GST_RTSP_OK;
7186 const gchar *control;
7188 GST_DEBUG_OBJECT (src, "TEARDOWN...");
7190 gst_rtspsrc_set_state (src, GST_STATE_READY);
7192 if (src->state < GST_RTSP_STATE_READY) {
7193 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
7200 /* construct a control url */
7201 control = get_aggregate_control (src);
7203 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
7206 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7207 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7208 const gchar *setup_url;
7209 GstRTSPConnInfo *info;
7211 /* try aggregate control first but do non-aggregate control otherwise */
7213 setup_url = control;
7214 else if ((setup_url = stream->conninfo.location) == NULL)
7217 if (src->conninfo.connection) {
7218 info = &src->conninfo;
7219 } else if (stream->conninfo.connection) {
7220 info = &stream->conninfo;
7224 if (!info->connected)
7229 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
7231 goto create_request_failed;
7234 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
7237 gst_rtspsrc_send (src, info->connection, &request, &response,
7241 /* FIXME, parse result? */
7242 gst_rtsp_message_unset (&request);
7243 gst_rtsp_message_unset (&response);
7246 /* early exit when we did aggregate control */
7252 /* close connections */
7253 GST_DEBUG_OBJECT (src, "closing connection...");
7254 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
7255 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7256 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7257 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
7261 gst_rtspsrc_cleanup (src);
7263 src->state = GST_RTSP_STATE_INVALID;
7266 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
7271 create_request_failed:
7273 gchar *str = gst_rtsp_strresult (res);
7275 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7276 ("Could not create request. (%s)", str));
7282 gchar *str = gst_rtsp_strresult (res);
7284 gst_rtsp_message_unset (&request);
7285 if (res != GST_RTSP_EINTR) {
7286 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7287 ("Could not send message. (%s)", str));
7289 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
7296 GST_DEBUG_OBJECT (src,
7297 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
7302 /* RTP-Info is of the format:
7304 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
7306 * rtptime corresponds to the timestamp for the NPT time given in the header
7307 * seqbase corresponds to the next sequence number we received. This number
7308 * indicates the first seqnum after the seek and should be used to discard
7309 * packets that are from before the seek.
7312 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
7317 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
7319 infos = g_strsplit (rtpinfo, ",", 0);
7320 for (i = 0; infos[i]; i++) {
7322 GstRTSPStream *stream;
7326 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
7328 /* init values, types of seqbase and timebase are bigger than needed so we
7329 * can store -1 as uninitialized values */
7334 /* parse url, find stream for url.
7335 * parse seq and rtptime. The seq number should be configured in the rtp
7336 * depayloader or session manager to detect gaps. Same for the rtptime, it
7337 * should be used to create an initial time newsegment. */
7338 fields = g_strsplit (infos[i], ";", 0);
7339 for (j = 0; fields[j]; j++) {
7340 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
7341 /* remove leading whitespace */
7342 fields[j] = g_strchug (fields[j]);
7343 if (g_str_has_prefix (fields[j], "url=")) {
7344 /* get the url and the stream */
7346 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
7347 } else if (g_str_has_prefix (fields[j], "seq=")) {
7348 seqbase = atoi (fields[j] + 4);
7349 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
7350 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
7353 g_strfreev (fields);
7354 /* now we need to store the values for the caps of the stream */
7355 if (stream != NULL) {
7356 GST_DEBUG_OBJECT (src,
7357 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
7358 stream, seqbase, timebase);
7360 /* we have a stream, configure detected params */
7361 stream->seqbase = seqbase;
7362 stream->timebase = timebase;
7371 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
7376 interval = strtoul (rtcp, NULL, 10);
7377 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
7382 interval *= GST_MSECOND;
7384 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7385 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7387 /* already (optionally) retrieved this when configuring manager */
7388 if (stream->session) {
7389 GObject *rtpsession = stream->session;
7391 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
7393 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
7397 /* now it happens that (Xenon) server sending this may also provide bogus
7398 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
7399 * and just use RTP-Info to sync */
7401 GObjectClass *klass;
7403 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
7404 if (g_object_class_find_property (klass, "rtcp-sync")) {
7405 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
7406 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
7412 gst_rtspsrc_get_float (const gchar * dstr)
7414 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7416 /* canonicalise floating point string so we can handle float strings
7417 * in the form "24.930" or "24,930" irrespective of the current locale */
7418 g_strlcpy (s, dstr, sizeof (s));
7419 g_strdelimit (s, ",", '.');
7420 return g_ascii_strtod (s, NULL);
7424 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
7426 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
7428 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
7429 g_strlcpy (val_str, "now", sizeof (val_str));
7431 if (segment->position == 0) {
7432 g_strlcpy (val_str, "0", sizeof (val_str));
7434 g_ascii_dtostr (val_str, sizeof (val_str),
7435 ((gdouble) segment->position) / GST_SECOND);
7438 return g_strdup_printf ("npt=%s-", val_str);
7442 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
7446 stream->timebase = -1;
7447 stream->seqbase = -1;
7449 len = stream->ptmap->len;
7450 for (i = 0; i < len; i++) {
7451 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
7454 if (item->caps == NULL)
7457 item->caps = gst_caps_make_writable (item->caps);
7458 s = gst_caps_get_structure (item->caps, 0);
7459 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
7463 static GstRTSPResult
7464 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
7466 GstRTSPResult res = GST_RTSP_OK;
7468 if (src->state < GST_RTSP_STATE_READY) {
7469 res = GST_RTSP_ERROR;
7470 if (src->open_error) {
7471 GST_DEBUG_OBJECT (src, "the stream was in error");
7475 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
7477 if ((res = gst_rtspsrc_open (src, async)) < 0) {
7478 GST_DEBUG_OBJECT (src, "failed to open stream");
7487 static GstRTSPResult
7488 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
7490 GstRTSPMessage request = { 0 };
7491 GstRTSPMessage response = { 0 };
7492 GstRTSPResult res = GST_RTSP_OK;
7496 const gchar *control;
7498 GST_DEBUG_OBJECT (src, "PLAY...");
7500 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7503 if (!(src->methods & GST_RTSP_PLAY))
7506 if (src->state == GST_RTSP_STATE_PLAYING)
7509 if (!src->conninfo.connection || !src->conninfo.connected)
7512 /* send some dummy packets before we activate the receive in the
7514 gst_rtspsrc_send_dummy_packets (src);
7516 /* require new SR packets */
7518 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
7520 /* construct a control url */
7521 control = get_aggregate_control (src);
7523 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7524 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7525 const gchar *setup_url;
7526 GstRTSPConnection *conn;
7528 /* try aggregate control first but do non-aggregate control otherwise */
7530 setup_url = control;
7531 else if ((setup_url = stream->conninfo.location) == NULL)
7534 if (src->conninfo.connection) {
7535 conn = src->conninfo.connection;
7536 } else if (stream->conninfo.connection) {
7537 conn = stream->conninfo.connection;
7543 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
7545 goto create_request_failed;
7547 if (src->need_range) {
7548 hval = gen_range_header (src, segment);
7550 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
7552 /* store the newsegment event so it can be sent from the streaming thread. */
7553 src->need_segment = TRUE;
7556 if (segment->rate != 1.0) {
7557 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
7559 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
7561 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
7563 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
7567 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
7569 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7572 /* seek may have silently failed as it is not supported */
7573 if (!(src->methods & GST_RTSP_PLAY)) {
7574 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
7575 /* obviously it is supported as we made it here */
7576 src->methods |= GST_RTSP_PLAY;
7577 src->seekable = FALSE;
7578 /* but there is nothing to parse in the response,
7579 * so convey we have no idea and not to expect anything particular */
7580 clear_rtp_base (src, stream);
7584 /* need to do for all streams */
7585 for (run = src->streams; run; run = g_list_next (run))
7586 clear_rtp_base (src, (GstRTSPStream *) run->data);
7588 /* NOTE the above also disables npt based eos detection */
7589 /* and below forces position to 0,
7590 * which is visible feedback we lost the plot */
7591 segment->start = segment->position = src->last_pos;
7594 gst_rtsp_message_unset (&request);
7596 /* parse RTP npt field. This is the current position in the stream (Normal
7597 * Play Time) and should be put in the NEWSEGMENT position field. */
7598 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
7600 gst_rtspsrc_parse_range (src, hval, segment);
7602 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
7603 segment->rate = 1.0;
7605 /* parse Speed header. This is the intended playback rate of the stream
7606 * and should be put in the NEWSEGMENT rate field. */
7607 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
7608 0) == GST_RTSP_OK) {
7609 segment->rate = gst_rtspsrc_get_float (hval);
7610 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
7611 &hval, 0) == GST_RTSP_OK) {
7612 segment->rate = gst_rtspsrc_get_float (hval);
7615 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
7616 * for the RTP packets. If this is not present, we assume all starts from 0...
7617 * This is info for the RTP session manager that we pass to it in caps. */
7619 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
7620 &hval, hval_idx++) == GST_RTSP_OK)
7621 gst_rtspsrc_parse_rtpinfo (src, hval);
7623 /* some servers indicate RTCP parameters in PLAY response,
7624 * rather than properly in SDP */
7625 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
7626 &hval, 0) == GST_RTSP_OK)
7627 gst_rtspsrc_handle_rtcp_interval (src, hval);
7629 gst_rtsp_message_unset (&response);
7631 /* early exit when we did aggregate control */
7635 /* configure the caps of the streams after we parsed all headers. Only reset
7636 * the manager object when we set a new Range header (we did a seek) */
7637 gst_rtspsrc_configure_caps (src, segment, src->need_range);
7639 /* set to PLAYING after we have configured the caps, otherwise we
7640 * might end up calling request_key (with SRTP) while caps are still
7641 * being configured. */
7642 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
7644 /* set again when needed */
7645 src->need_range = FALSE;
7647 src->running = TRUE;
7648 src->base_time = -1;
7649 src->state = GST_RTSP_STATE_PLAYING;
7652 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
7653 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7654 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7655 stream->discont = TRUE;
7660 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
7667 GST_DEBUG_OBJECT (src, "failed to open stream");
7672 GST_DEBUG_OBJECT (src, "PLAY is not supported");
7677 GST_DEBUG_OBJECT (src, "we were already PLAYING");
7680 create_request_failed:
7682 gchar *str = gst_rtsp_strresult (res);
7684 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7685 ("Could not create request. (%s)", str));
7691 gchar *str = gst_rtsp_strresult (res);
7693 gst_rtsp_message_unset (&request);
7694 if (res != GST_RTSP_EINTR) {
7695 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7696 ("Could not send message. (%s)", str));
7698 GST_WARNING_OBJECT (src, "PLAY interrupted");
7705 static GstRTSPResult
7706 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
7708 GstRTSPResult res = GST_RTSP_OK;
7709 GstRTSPMessage request = { 0 };
7710 GstRTSPMessage response = { 0 };
7712 const gchar *control;
7714 GST_DEBUG_OBJECT (src, "PAUSE...");
7716 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
7719 if (!(src->methods & GST_RTSP_PAUSE))
7722 if (src->state == GST_RTSP_STATE_READY)
7725 if (!src->conninfo.connection || !src->conninfo.connected)
7728 /* construct a control url */
7729 control = get_aggregate_control (src);
7731 /* loop over the streams. We might exit the loop early when we could do an
7732 * aggregate control */
7733 for (walk = src->streams; walk; walk = g_list_next (walk)) {
7734 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7735 GstRTSPConnection *conn;
7736 const gchar *setup_url;
7738 /* try aggregate control first but do non-aggregate control otherwise */
7740 setup_url = control;
7741 else if ((setup_url = stream->conninfo.location) == NULL)
7744 if (src->conninfo.connection) {
7745 conn = src->conninfo.connection;
7746 } else if (stream->conninfo.connection) {
7747 conn = stream->conninfo.connection;
7753 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7754 ("Sending PAUSE request"));
7757 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
7759 goto create_request_failed;
7761 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7764 gst_rtsp_message_unset (&request);
7765 gst_rtsp_message_unset (&response);
7767 /* exit early when we did agregate control */
7772 /* change element states now */
7773 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7776 src->state = GST_RTSP_STATE_READY;
7780 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7787 GST_DEBUG_OBJECT (src, "failed to open stream");
7792 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7797 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7800 create_request_failed:
7802 gchar *str = gst_rtsp_strresult (res);
7804 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7805 ("Could not create request. (%s)", str));
7811 gchar *str = gst_rtsp_strresult (res);
7813 gst_rtsp_message_unset (&request);
7814 if (res != GST_RTSP_EINTR) {
7815 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7816 ("Could not send message. (%s)", str));
7818 GST_WARNING_OBJECT (src, "PAUSE interrupted");
7826 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
7828 GstRTSPSrc *rtspsrc;
7830 rtspsrc = GST_RTSPSRC (bin);
7832 switch (GST_MESSAGE_TYPE (message)) {
7833 case GST_MESSAGE_EOS:
7834 gst_message_unref (message);
7836 case GST_MESSAGE_ELEMENT:
7838 const GstStructure *s = gst_message_get_structure (message);
7840 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
7841 gboolean ignore_timeout;
7843 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
7845 GST_OBJECT_LOCK (rtspsrc);
7846 ignore_timeout = rtspsrc->ignore_timeout;
7847 rtspsrc->ignore_timeout = TRUE;
7848 GST_OBJECT_UNLOCK (rtspsrc);
7850 /* we only act on the first udp timeout message, others are irrelevant
7851 * and can be ignored. */
7852 if (!ignore_timeout)
7853 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
7855 gst_message_unref (message);
7858 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7861 case GST_MESSAGE_ERROR:
7864 GstRTSPStream *stream;
7867 udpsrc = GST_MESSAGE_SRC (message);
7869 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7870 GST_ELEMENT_NAME (udpsrc));
7872 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7876 /* we ignore the RTCP udpsrc */
7877 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7880 /* if we get error messages from the udp sources, that's not a problem as
7881 * long as not all of them error out. We also don't really know what the
7882 * problem is, the message does not give enough detail... */
7883 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7884 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7885 if (ret != GST_FLOW_OK)
7889 gst_message_unref (message);
7893 /* fatal but not our message, forward */
7894 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7899 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7905 /* the thread where everything happens */
7907 gst_rtspsrc_thread (GstRTSPSrc * src)
7911 GST_OBJECT_LOCK (src);
7912 cmd = src->pending_cmd;
7913 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7914 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7915 src->pending_cmd = CMD_LOOP;
7917 src->pending_cmd = CMD_WAIT;
7918 GST_DEBUG_OBJECT (src, "got command %s", cmd_to_string (cmd));
7920 /* we got the message command, so ensure communication is possible again */
7921 gst_rtspsrc_connection_flush (src, FALSE);
7923 src->busy_cmd = cmd;
7924 GST_OBJECT_UNLOCK (src);
7928 gst_rtspsrc_open (src, TRUE);
7931 gst_rtspsrc_play (src, &src->segment, TRUE);
7934 gst_rtspsrc_pause (src, TRUE);
7937 gst_rtspsrc_close (src, TRUE, FALSE);
7940 gst_rtspsrc_loop (src);
7943 gst_rtspsrc_reconnect (src, FALSE);
7949 GST_OBJECT_LOCK (src);
7950 /* and go back to sleep */
7951 if (src->pending_cmd == CMD_WAIT) {
7953 gst_task_pause (src->task);
7956 src->busy_cmd = CMD_WAIT;
7957 GST_OBJECT_UNLOCK (src);
7961 gst_rtspsrc_start (GstRTSPSrc * src)
7963 GST_DEBUG_OBJECT (src, "starting");
7965 GST_OBJECT_LOCK (src);
7967 src->pending_cmd = CMD_WAIT;
7969 if (src->task == NULL) {
7970 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7971 if (src->task == NULL)
7974 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7976 GST_OBJECT_UNLOCK (src);
7983 GST_OBJECT_UNLOCK (src);
7984 GST_ERROR_OBJECT (src, "failed to create task");
7990 gst_rtspsrc_stop (GstRTSPSrc * src)
7994 GST_DEBUG_OBJECT (src, "stopping");
7996 /* also cancels pending task */
7997 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7999 GST_OBJECT_LOCK (src);
8000 if ((task = src->task)) {
8002 GST_OBJECT_UNLOCK (src);
8004 gst_task_stop (task);
8006 /* make sure it is not running */
8007 GST_RTSP_STREAM_LOCK (src);
8008 GST_RTSP_STREAM_UNLOCK (src);
8010 /* now wait for the task to finish */
8011 gst_task_join (task);
8013 /* and free the task */
8014 gst_object_unref (GST_OBJECT (task));
8016 GST_OBJECT_LOCK (src);
8018 GST_OBJECT_UNLOCK (src);
8020 /* ensure synchronously all is closed and clean */
8021 gst_rtspsrc_close (src, FALSE, TRUE);
8026 static GstStateChangeReturn
8027 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
8029 GstRTSPSrc *rtspsrc;
8030 GstStateChangeReturn ret;
8032 rtspsrc = GST_RTSPSRC (element);
8034 switch (transition) {
8035 case GST_STATE_CHANGE_NULL_TO_READY:
8036 if (!gst_rtspsrc_start (rtspsrc))
8039 case GST_STATE_CHANGE_READY_TO_PAUSED:
8040 /* init some state */
8041 rtspsrc->cur_protocols = rtspsrc->protocols;
8042 /* first attempt, don't ignore timeouts */
8043 rtspsrc->ignore_timeout = FALSE;
8044 rtspsrc->open_error = FALSE;
8045 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
8047 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8048 set_manager_buffer_mode (rtspsrc);
8050 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8051 /* unblock the tcp tasks and make the loop waiting */
8052 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
8053 /* make sure it is waiting before we send PAUSE or PLAY below */
8054 GST_RTSP_STREAM_LOCK (rtspsrc);
8055 GST_RTSP_STREAM_UNLOCK (rtspsrc);
8058 case GST_STATE_CHANGE_PAUSED_TO_READY:
8064 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
8065 if (ret == GST_STATE_CHANGE_FAILURE)
8068 switch (transition) {
8069 case GST_STATE_CHANGE_NULL_TO_READY:
8070 ret = GST_STATE_CHANGE_SUCCESS;
8072 case GST_STATE_CHANGE_READY_TO_PAUSED:
8073 ret = GST_STATE_CHANGE_NO_PREROLL;
8075 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
8076 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
8077 ret = GST_STATE_CHANGE_SUCCESS;
8079 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
8080 /* send pause request and keep the idle task around */
8081 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
8082 ret = GST_STATE_CHANGE_NO_PREROLL;
8084 case GST_STATE_CHANGE_PAUSED_TO_READY:
8085 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
8086 ret = GST_STATE_CHANGE_SUCCESS;
8088 case GST_STATE_CHANGE_READY_TO_NULL:
8089 gst_rtspsrc_stop (rtspsrc);
8090 ret = GST_STATE_CHANGE_SUCCESS;
8101 GST_DEBUG_OBJECT (rtspsrc, "start failed");
8102 return GST_STATE_CHANGE_FAILURE;
8107 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
8110 GstRTSPSrc *rtspsrc;
8112 rtspsrc = GST_RTSPSRC (element);
8114 if (GST_EVENT_IS_DOWNSTREAM (event)) {
8115 res = gst_rtspsrc_push_event (rtspsrc, event);
8117 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
8124 /*** GSTURIHANDLER INTERFACE *************************************************/
8127 gst_rtspsrc_uri_get_type (GType type)
8132 static const gchar *const *
8133 gst_rtspsrc_uri_get_protocols (GType type)
8135 static const gchar *protocols[] =
8136 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
8137 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
8144 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
8146 GstRTSPSrc *src = GST_RTSPSRC (handler);
8148 /* FIXME: make thread-safe */
8149 return g_strdup (src->conninfo.location);
8153 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
8159 GstRTSPUrl *newurl = NULL;
8160 GstSDPMessage *sdp = NULL;
8162 src = GST_RTSPSRC (handler);
8164 /* same URI, we're fine */
8165 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
8168 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
8169 sres = gst_sdp_message_new (&sdp);
8173 GST_DEBUG_OBJECT (src, "parsing SDP message");
8174 sres = gst_sdp_message_parse_uri (uri, sdp);
8179 GST_DEBUG_OBJECT (src, "parsing URI");
8180 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
8184 /* if worked, free previous and store new url object along with the original
8186 GST_DEBUG_OBJECT (src, "configuring URI");
8187 g_free (src->conninfo.location);
8188 src->conninfo.location = g_strdup (uri);
8189 gst_rtsp_url_free (src->conninfo.url);
8190 src->conninfo.url = newurl;
8191 g_free (src->conninfo.url_str);
8193 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
8195 src->conninfo.url_str = NULL;
8198 gst_sdp_message_free (src->sdp);
8200 src->from_sdp = sdp != NULL;
8202 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
8203 GST_DEBUG_OBJECT (src, "request uri is: %s",
8204 GST_STR_NULL (src->conninfo.url_str));
8211 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
8216 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", sres);
8217 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8218 "Could not create SDP");
8223 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", sres,
8224 GST_STR_NULL (uri));
8225 gst_sdp_message_free (sdp);
8226 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8232 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
8233 GST_STR_NULL (uri), res);
8234 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
8235 "Invalid RTSP URI");
8241 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
8243 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
8245 iface->get_type = gst_rtspsrc_uri_get_type;
8246 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
8247 iface->get_uri = gst_rtspsrc_uri_get_uri;
8248 iface->set_uri = gst_rtspsrc_uri_set_uri;