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
77 * Last reviewed on 2006-08-18 (0.10.5)
86 #endif /* HAVE_UNISTD_H */
92 #include <gst/net/gstnet.h>
93 #include <gst/sdp/gstsdpmessage.h>
94 #include <gst/rtp/gstrtppayloads.h>
96 #include "gst/gst-i18n-plugin.h"
98 #include "gstrtspsrc.h"
100 GST_DEBUG_CATEGORY_STATIC (rtspsrc_debug);
101 #define GST_CAT_DEFAULT (rtspsrc_debug)
103 static GstStaticPadTemplate rtptemplate = GST_STATIC_PAD_TEMPLATE ("stream_%u",
106 GST_STATIC_CAPS ("application/x-rtp; application/x-rdt"));
108 /* templates used internally */
109 static GstStaticPadTemplate anysrctemplate =
110 GST_STATIC_PAD_TEMPLATE ("internalsrc_%u",
113 GST_STATIC_CAPS_ANY);
115 static GstStaticPadTemplate anysinktemplate =
116 GST_STATIC_PAD_TEMPLATE ("internalsink_%u",
119 GST_STATIC_CAPS_ANY);
123 SIGNAL_HANDLE_REQUEST,
125 SIGNAL_SELECT_STREAM,
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 DEFAULT_LOCATION NULL
168 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
169 #define DEFAULT_DEBUG FALSE
170 #define DEFAULT_RETRY 20
171 #define DEFAULT_TIMEOUT 5000000
172 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
173 #define DEFAULT_TCP_TIMEOUT 20000000
174 #define DEFAULT_LATENCY_MS 2000
175 #define DEFAULT_DROP_ON_LATENCY FALSE
176 #define DEFAULT_DO_RETRANSMISSION FALSE
177 #define DEFAULT_CONNECTION_SPEED 0
178 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
179 #define DEFAULT_DO_RTCP TRUE
180 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
181 #define DEFAULT_PROXY NULL
182 #define DEFAULT_RTP_BLOCKSIZE 0
183 #define DEFAULT_USER_ID NULL
184 #define DEFAULT_USER_PW NULL
185 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
186 #define DEFAULT_PORT_RANGE NULL
187 #define DEFAULT_SHORT_HEADER FALSE
188 #define DEFAULT_PROBATION 2
189 #define DEFAULT_UDP_RECONNECT TRUE
190 #define DEFAULT_MULTICAST_IFACE NULL
191 #define DEFAULT_NTP_SYNC FALSE
192 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
193 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
205 PROP_DROP_ON_LATENCY,
206 PROP_DO_RETRANSMISSION,
207 PROP_CONNECTION_SPEED,
210 PROP_DO_RTSP_KEEP_ALIVE,
219 PROP_UDP_BUFFER_SIZE,
223 PROP_MULTICAST_IFACE,
225 PROP_USE_PIPELINE_CLOCK,
227 PROP_TLS_VALIDATION_FLAGS,
231 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
233 gst_rtsp_nat_method_get_type (void)
235 static GType rtsp_nat_method_type = 0;
236 static const GEnumValue rtsp_nat_method[] = {
237 {GST_RTSP_NAT_NONE, "None", "none"},
238 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
242 if (!rtsp_nat_method_type) {
243 rtsp_nat_method_type =
244 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
246 return rtsp_nat_method_type;
249 static void gst_rtspsrc_finalize (GObject * object);
251 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
252 const GValue * value, GParamSpec * pspec);
253 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
254 GValue * value, GParamSpec * pspec);
256 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
258 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
259 gpointer iface_data);
261 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
264 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
265 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
267 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
269 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
270 GstStateChange transition);
271 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
272 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
274 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
275 GstRTSPMessage * response);
277 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
279 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
280 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
282 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
283 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
285 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
286 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
287 gboolean only_close);
289 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
290 const gchar * uri, GError ** error);
291 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
293 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
294 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
295 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
296 GstRTSPStream * stream, GstEvent * event);
297 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
299 /* commands we send to out loop to notify it of events */
300 #define CMD_OPEN (1 << 0)
301 #define CMD_PLAY (1 << 1)
302 #define CMD_PAUSE (1 << 2)
303 #define CMD_CLOSE (1 << 3)
304 #define CMD_WAIT (1 << 4)
305 #define CMD_RECONNECT (1 << 5)
306 #define CMD_LOOP (1 << 6)
308 /* mask for all commands */
309 #define CMD_ALL ((CMD_LOOP << 1) - 1)
311 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
313 gchar *__txt = _gst_element_error_printf text; \
314 gst_element_post_message (GST_ELEMENT_CAST (el), \
315 gst_message_new_progress (GST_OBJECT_CAST (el), \
316 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
320 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
322 #define gst_rtspsrc_parent_class parent_class
323 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
324 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
327 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
329 GST_DEBUG_OBJECT (src, "default handler");
334 select_stream_accum (GSignalInvocationHint * ihint,
335 GValue * return_accu, const GValue * handler_return, gpointer data)
339 myboolean = g_value_get_boolean (handler_return);
340 GST_DEBUG ("accum %d", myboolean);
341 g_value_set_boolean (return_accu, myboolean);
343 /* stop emission if FALSE */
348 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
350 GObjectClass *gobject_class;
351 GstElementClass *gstelement_class;
352 GstBinClass *gstbin_class;
354 gobject_class = (GObjectClass *) klass;
355 gstelement_class = (GstElementClass *) klass;
356 gstbin_class = (GstBinClass *) klass;
358 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
360 gobject_class->set_property = gst_rtspsrc_set_property;
361 gobject_class->get_property = gst_rtspsrc_get_property;
363 gobject_class->finalize = gst_rtspsrc_finalize;
365 g_object_class_install_property (gobject_class, PROP_LOCATION,
366 g_param_spec_string ("location", "RTSP Location",
367 "Location of the RTSP url to read",
368 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
370 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
371 g_param_spec_flags ("protocols", "Protocols",
372 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
373 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
375 g_object_class_install_property (gobject_class, PROP_DEBUG,
376 g_param_spec_boolean ("debug", "Debug",
377 "Dump request and response messages to stdout",
378 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
380 g_object_class_install_property (gobject_class, PROP_RETRY,
381 g_param_spec_uint ("retry", "Retry",
382 "Max number of retries when allocating RTP ports.",
383 0, G_MAXUINT16, DEFAULT_RETRY,
384 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
386 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
387 g_param_spec_uint64 ("timeout", "Timeout",
388 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
389 0, G_MAXUINT64, DEFAULT_TIMEOUT,
390 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
392 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
393 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
394 "Fail after timeout microseconds on TCP connections (0 = disabled)",
395 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
396 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398 g_object_class_install_property (gobject_class, PROP_LATENCY,
399 g_param_spec_uint ("latency", "Buffer latency in ms",
400 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
401 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
403 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
404 g_param_spec_boolean ("drop-on-latency",
405 "Drop buffers when maximum latency is reached",
406 "Tells the jitterbuffer to never exceed the given latency in size",
407 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
409 g_object_class_install_property (gobject_class, PROP_DO_RETRANSMISSION,
410 g_param_spec_boolean ("do-retransmission", "Do retransmission",
411 "Send retransmission events upstream when a packet is late",
412 DEFAULT_DO_RETRANSMISSION,
413 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
415 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
416 g_param_spec_uint64 ("connection-speed", "Connection Speed",
417 "Network connection speed in kbps (0 = unknown)",
418 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
419 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
421 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
422 g_param_spec_enum ("nat-method", "NAT Method",
423 "Method to use for traversing firewalls and NAT",
424 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
425 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
428 * GstRTSPSrc:do-rtcp:
430 * Enable RTCP support. Some old server don't like RTCP and then this property
431 * needs to be set to FALSE.
433 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
434 g_param_spec_boolean ("do-rtcp", "Do RTCP",
435 "Send RTCP packets, disable for old incompatible server.",
436 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
439 * GstRTSPSrc:do-rtsp-keep-alive:
441 * Enable RTSP keep alive support. Some old server don't like RTSP
442 * keep alive and then this property needs to be set to FALSE.
444 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
445 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
446 "Send RTSP keep alive packets, disable for old incompatible server.",
447 DEFAULT_DO_RTSP_KEEP_ALIVE,
448 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
453 * Set the proxy parameters. This has to be a string of the format
454 * [http://][user:passwd@]host[:port].
456 g_object_class_install_property (gobject_class, PROP_PROXY,
457 g_param_spec_string ("proxy", "Proxy",
458 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
459 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
461 * GstRTSPSrc:proxy-id:
463 * Sets the proxy URI user id for authentication. If the URI set via the
464 * "proxy" property contains a user-id already, that will take precedence.
468 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
469 g_param_spec_string ("proxy-id", "proxy-id",
470 "HTTP proxy URI user id for authentication", "",
471 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
473 * GstRTSPSrc:proxy-pw:
475 * Sets the proxy URI password for authentication. If the URI set via the
476 * "proxy" property contains a password already, that will take precedence.
480 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
481 g_param_spec_string ("proxy-pw", "proxy-pw",
482 "HTTP proxy URI user password for authentication", "",
483 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
486 * GstRTSPSrc:rtp-blocksize:
488 * RTP package size to suggest to server.
490 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
491 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
492 "RTP package size to suggest to server (0 = disabled)",
493 0, 65536, DEFAULT_RTP_BLOCKSIZE,
494 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
496 g_object_class_install_property (gobject_class,
498 g_param_spec_string ("user-id", "user-id",
499 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
500 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
501 g_object_class_install_property (gobject_class, PROP_USER_PW,
502 g_param_spec_string ("user-pw", "user-pw",
503 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
504 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
507 * GstRTSPSrc:buffer-mode:
509 * Control the buffering and timestamping mode used by the jitterbuffer.
511 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
512 g_param_spec_enum ("buffer-mode", "Buffer Mode",
513 "Control the buffering algorithm in use",
514 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
515 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
518 * GstRTSPSrc:port-range:
520 * Configure the client port numbers that can be used to recieve RTP and
523 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
524 g_param_spec_string ("port-range", "Port range",
525 "Client port range that can be used to receive RTP and RTCP data, "
526 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
527 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
530 * GstRTSPSrc:udp-buffer-size:
532 * Size of the kernel UDP receive buffer in bytes.
534 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
535 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
536 "Size of the kernel UDP receive buffer in bytes, 0=default",
537 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
538 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
541 * GstRTSPSrc:short-header:
543 * Only send the basic RTSP headers for broken encoders.
545 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
546 g_param_spec_boolean ("short-header", "Short Header",
547 "Only send the basic RTSP headers for broken encoders",
548 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
550 g_object_class_install_property (gobject_class, PROP_PROBATION,
551 g_param_spec_uint ("probation", "Number of probations",
552 "Consecutive packet sequence numbers to accept the source",
553 0, G_MAXUINT, DEFAULT_PROBATION,
554 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
556 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
557 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
558 "Reconnect to the server if RTSP connection is closed when doing UDP",
559 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
561 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
562 g_param_spec_string ("multicast-iface", "Multicast Interface",
563 "The network interface on which to join the multicast group",
564 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
566 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
567 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
568 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
569 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
571 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
572 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
573 "Use the pipeline running-time to set the NTP time in the RTCP SR messages",
574 DEFAULT_USE_PIPELINE_CLOCK,
575 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
577 g_object_class_install_property (gobject_class, PROP_SDES,
578 g_param_spec_boxed ("sdes", "SDES",
579 "The SDES items of this session",
580 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
583 * GstRTSPSrc::tls-validation-flags:
585 * TLS certificate validation flags used to validate server
590 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
591 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
592 "TLS certificate validation flags used to validate the server certificate",
593 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
594 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
597 * GstRTSPSrc::handle-request:
598 * @rtspsrc: a #GstRTSPSrc
599 * @request: a #GstRTSPMessage
600 * @response: a #GstRTSPMessage
602 * Handle a server request in @request and prepare @response.
604 * This signal is called from the streaming thread, you should therefore not
605 * do any state changes on @rtspsrc because this might deadlock. If you want
606 * to modify the state as a result of this signal, post a
607 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
612 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
613 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
614 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
615 G_TYPE_POINTER, G_TYPE_POINTER);
618 * GstRTSPSrc::on-sdp:
619 * @rtspsrc: a #GstRTSPSrc
620 * @sdp: a #GstSDPMessage
622 * Emited when the client has retrieved the SDP and before it configures the
623 * streams in the SDP. @sdp can be inspected and modified.
625 * This signal is called from the streaming thread, you should therefore not
626 * do any state changes on @rtspsrc because this might deadlock. If you want
627 * to modify the state as a result of this signal, post a
628 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
633 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
634 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
635 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
636 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
639 * GstRTSPSrc::select-stream:
640 * @rtspsrc: a #GstRTSPSrc
641 * @num: the stream number
642 * @caps: the stream caps
644 * Emited before the client decides to configure the stream @num with
647 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
652 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
653 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
654 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
655 (GCallback) default_select_stream, select_stream_accum, NULL,
656 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
659 * GstRTSPSrc::new-manager:
660 * @rtspsrc: a #GstRTSPSrc
661 * @manager: a #GstElement
663 * Emited after a new manager (like rtpbin) was created and the default
664 * properties were configured.
668 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
669 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
670 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
671 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
673 gstelement_class->send_event = gst_rtspsrc_send_event;
674 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
675 gstelement_class->change_state = gst_rtspsrc_change_state;
677 gst_element_class_add_pad_template (gstelement_class,
678 gst_static_pad_template_get (&rtptemplate));
680 gst_element_class_set_static_metadata (gstelement_class,
681 "RTSP packet receiver", "Source/Network",
682 "Receive data over the network via RTSP (RFC 2326)",
683 "Wim Taymans <wim@fluendo.com>, "
684 "Thijs Vermeir <thijs.vermeir@barco.com>, "
685 "Lutz Mueller <lutz@topfrose.de>");
687 gstbin_class->handle_message = gst_rtspsrc_handle_message;
689 gst_rtsp_ext_list_init ();
693 gst_rtspsrc_init (GstRTSPSrc * src)
695 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
696 src->protocols = DEFAULT_PROTOCOLS;
697 src->debug = DEFAULT_DEBUG;
698 src->retry = DEFAULT_RETRY;
699 src->udp_timeout = DEFAULT_TIMEOUT;
700 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
701 src->latency = DEFAULT_LATENCY_MS;
702 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
703 src->do_retransmission = DEFAULT_DO_RETRANSMISSION;
704 src->connection_speed = DEFAULT_CONNECTION_SPEED;
705 src->nat_method = DEFAULT_NAT_METHOD;
706 src->do_rtcp = DEFAULT_DO_RTCP;
707 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
708 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
709 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
710 src->user_id = g_strdup (DEFAULT_USER_ID);
711 src->user_pw = g_strdup (DEFAULT_USER_PW);
712 src->buffer_mode = DEFAULT_BUFFER_MODE;
713 src->client_port_range.min = 0;
714 src->client_port_range.max = 0;
715 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
716 src->short_header = DEFAULT_SHORT_HEADER;
717 src->probation = DEFAULT_PROBATION;
718 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
719 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
720 src->ntp_sync = DEFAULT_NTP_SYNC;
721 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
723 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
725 /* get a list of all extensions */
726 src->extensions = gst_rtsp_ext_list_get ();
728 /* connect to send signal */
729 gst_rtsp_ext_list_connect (src->extensions, "send",
730 (GCallback) gst_rtspsrc_send_cb, src);
732 /* protects the streaming thread in interleaved mode or the polling
733 * thread in UDP mode. */
734 g_rec_mutex_init (&src->stream_rec_lock);
736 /* protects our state changes from multiple invocations */
737 g_rec_mutex_init (&src->state_rec_lock);
739 src->state = GST_RTSP_STATE_INVALID;
741 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
745 gst_rtspsrc_finalize (GObject * object)
749 rtspsrc = GST_RTSPSRC (object);
751 gst_rtsp_ext_list_free (rtspsrc->extensions);
752 g_free (rtspsrc->conninfo.location);
753 gst_rtsp_url_free (rtspsrc->conninfo.url);
754 g_free (rtspsrc->conninfo.url_str);
755 g_free (rtspsrc->user_id);
756 g_free (rtspsrc->user_pw);
757 g_free (rtspsrc->multi_iface);
760 gst_sdp_message_free (rtspsrc->sdp);
763 if (rtspsrc->provided_clock)
764 gst_object_unref (rtspsrc->provided_clock);
767 gst_structure_free (rtspsrc->sdes);
770 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
771 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
773 G_OBJECT_CLASS (parent_class)->finalize (object);
777 gst_rtspsrc_provide_clock (GstElement * element)
779 GstRTSPSrc *src = GST_RTSPSRC (element);
782 if ((clock = src->provided_clock) != NULL)
783 gst_object_ref (clock);
788 /* a proxy string of the format [user:passwd@]host[:port] */
790 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
794 g_free (rtsp->proxy_user);
795 rtsp->proxy_user = NULL;
796 g_free (rtsp->proxy_passwd);
797 rtsp->proxy_passwd = NULL;
798 g_free (rtsp->proxy_host);
799 rtsp->proxy_host = NULL;
800 rtsp->proxy_port = 0;
807 /* we allow http:// in front but ignore it */
808 if (g_str_has_prefix (p, "http://"))
811 at = strchr (p, '@');
813 /* look for user:passwd */
814 col = strchr (proxy, ':');
815 if (col == NULL || col > at)
818 rtsp->proxy_user = g_strndup (p, col - p);
820 rtsp->proxy_passwd = g_strndup (col, at - col);
825 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
826 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
827 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
828 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
829 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
830 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
831 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
834 col = strchr (p, ':');
837 /* everything before the colon is the hostname */
838 rtsp->proxy_host = g_strndup (p, col - p);
840 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
842 rtsp->proxy_host = g_strdup (p);
843 rtsp->proxy_port = 8080;
849 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
851 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
852 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
855 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
857 rtspsrc->ptcp_timeout = NULL;
861 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
866 rtspsrc = GST_RTSPSRC (object);
870 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
871 g_value_get_string (value), NULL);
874 rtspsrc->protocols = g_value_get_flags (value);
877 rtspsrc->debug = g_value_get_boolean (value);
880 rtspsrc->retry = g_value_get_uint (value);
883 rtspsrc->udp_timeout = g_value_get_uint64 (value);
885 case PROP_TCP_TIMEOUT:
886 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
889 rtspsrc->latency = g_value_get_uint (value);
891 case PROP_DROP_ON_LATENCY:
892 rtspsrc->drop_on_latency = g_value_get_boolean (value);
894 case PROP_DO_RETRANSMISSION:
895 rtspsrc->do_retransmission = g_value_get_boolean (value);
897 case PROP_CONNECTION_SPEED:
898 rtspsrc->connection_speed = g_value_get_uint64 (value);
900 case PROP_NAT_METHOD:
901 rtspsrc->nat_method = g_value_get_enum (value);
904 rtspsrc->do_rtcp = g_value_get_boolean (value);
906 case PROP_DO_RTSP_KEEP_ALIVE:
907 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
910 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
913 if (rtspsrc->prop_proxy_id)
914 g_free (rtspsrc->prop_proxy_id);
915 rtspsrc->prop_proxy_id = g_value_dup_string (value);
918 if (rtspsrc->prop_proxy_pw)
919 g_free (rtspsrc->prop_proxy_pw);
920 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
922 case PROP_RTP_BLOCKSIZE:
923 rtspsrc->rtp_blocksize = g_value_get_uint (value);
926 if (rtspsrc->user_id)
927 g_free (rtspsrc->user_id);
928 rtspsrc->user_id = g_value_dup_string (value);
931 if (rtspsrc->user_pw)
932 g_free (rtspsrc->user_pw);
933 rtspsrc->user_pw = g_value_dup_string (value);
935 case PROP_BUFFER_MODE:
936 rtspsrc->buffer_mode = g_value_get_enum (value);
938 case PROP_PORT_RANGE:
942 str = g_value_get_string (value);
944 sscanf (str, "%u-%u",
945 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
947 rtspsrc->client_port_range.min = 0;
948 rtspsrc->client_port_range.max = 0;
952 case PROP_UDP_BUFFER_SIZE:
953 rtspsrc->udp_buffer_size = g_value_get_int (value);
955 case PROP_SHORT_HEADER:
956 rtspsrc->short_header = g_value_get_boolean (value);
959 rtspsrc->probation = g_value_get_uint (value);
961 case PROP_UDP_RECONNECT:
962 rtspsrc->udp_reconnect = g_value_get_boolean (value);
964 case PROP_MULTICAST_IFACE:
965 g_free (rtspsrc->multi_iface);
967 if (g_value_get_string (value) == NULL)
968 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
970 rtspsrc->multi_iface = g_value_dup_string (value);
973 rtspsrc->ntp_sync = g_value_get_boolean (value);
975 case PROP_USE_PIPELINE_CLOCK:
976 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
979 rtspsrc->sdes = g_value_dup_boxed (value);
981 case PROP_TLS_VALIDATION_FLAGS:
982 rtspsrc->tls_validation_flags = g_value_get_flags (value);
985 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
991 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
996 rtspsrc = GST_RTSPSRC (object);
1000 g_value_set_string (value, rtspsrc->conninfo.location);
1002 case PROP_PROTOCOLS:
1003 g_value_set_flags (value, rtspsrc->protocols);
1006 g_value_set_boolean (value, rtspsrc->debug);
1009 g_value_set_uint (value, rtspsrc->retry);
1012 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1014 case PROP_TCP_TIMEOUT:
1018 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1019 rtspsrc->tcp_timeout.tv_usec;
1020 g_value_set_uint64 (value, timeout);
1024 g_value_set_uint (value, rtspsrc->latency);
1026 case PROP_DROP_ON_LATENCY:
1027 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1029 case PROP_DO_RETRANSMISSION:
1030 g_value_set_boolean (value, rtspsrc->do_retransmission);
1032 case PROP_CONNECTION_SPEED:
1033 g_value_set_uint64 (value, rtspsrc->connection_speed);
1035 case PROP_NAT_METHOD:
1036 g_value_set_enum (value, rtspsrc->nat_method);
1039 g_value_set_boolean (value, rtspsrc->do_rtcp);
1041 case PROP_DO_RTSP_KEEP_ALIVE:
1042 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1048 if (rtspsrc->proxy_host) {
1050 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1054 g_value_take_string (value, str);
1058 g_value_set_string (value, rtspsrc->prop_proxy_id);
1061 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1063 case PROP_RTP_BLOCKSIZE:
1064 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1067 g_value_set_string (value, rtspsrc->user_id);
1070 g_value_set_string (value, rtspsrc->user_pw);
1072 case PROP_BUFFER_MODE:
1073 g_value_set_enum (value, rtspsrc->buffer_mode);
1075 case PROP_PORT_RANGE:
1079 if (rtspsrc->client_port_range.min != 0) {
1080 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1081 rtspsrc->client_port_range.max);
1085 g_value_take_string (value, str);
1088 case PROP_UDP_BUFFER_SIZE:
1089 g_value_set_int (value, rtspsrc->udp_buffer_size);
1091 case PROP_SHORT_HEADER:
1092 g_value_set_boolean (value, rtspsrc->short_header);
1094 case PROP_PROBATION:
1095 g_value_set_uint (value, rtspsrc->probation);
1097 case PROP_UDP_RECONNECT:
1098 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1100 case PROP_MULTICAST_IFACE:
1101 g_value_set_string (value, rtspsrc->multi_iface);
1104 g_value_set_boolean (value, rtspsrc->ntp_sync);
1106 case PROP_USE_PIPELINE_CLOCK:
1107 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1110 g_value_set_boxed (value, rtspsrc->sdes);
1112 case PROP_TLS_VALIDATION_FLAGS:
1113 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1116 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1122 find_stream_by_id (GstRTSPStream * stream, gint * id)
1124 if (stream->id == *id)
1131 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1133 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1140 find_stream_by_pt (GstRTSPStream * stream, gint * pt)
1142 if (stream->pt == *pt)
1149 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1151 GstElement *src = (GstElement *) a;
1153 if (stream->udpsrc[0] == src)
1155 if (stream->udpsrc[1] == src)
1162 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1164 /* check qualified setup_url */
1165 if (!strcmp (stream->conninfo.location, (gchar *) a))
1167 /* check original control_url */
1168 if (!strcmp (stream->control_url, (gchar *) a))
1171 /* check if qualified setup_url ends with string */
1172 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1178 static GstRTSPStream *
1179 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1183 /* find and get stream */
1184 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1185 return (GstRTSPStream *) lstream->data;
1190 static const GstSDPBandwidth *
1191 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1192 const GstSDPMedia * media, const gchar * type)
1196 /* first look in the media specific section */
1197 len = gst_sdp_media_bandwidths_len (media);
1198 for (i = 0; i < len; i++) {
1199 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1201 if (strcmp (bw->bwtype, type) == 0)
1204 /* then look in the message specific section */
1205 len = gst_sdp_message_bandwidths_len (sdp);
1206 for (i = 0; i < len; i++) {
1207 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1209 if (strcmp (bw->bwtype, type) == 0)
1216 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1217 const GstSDPMedia * media, GstRTSPStream * stream)
1219 const GstSDPBandwidth *bw;
1221 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1222 stream->as_bandwidth = bw->bandwidth;
1224 stream->as_bandwidth = -1;
1226 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1227 stream->rr_bandwidth = bw->bandwidth;
1229 stream->rr_bandwidth = -1;
1231 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1232 stream->rs_bandwidth = bw->bandwidth;
1234 stream->rs_bandwidth = -1;
1238 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1239 const GstSDPConnection * conn)
1241 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1244 if (conn->addrtype == NULL)
1247 /* check for IPV6 */
1248 if (strcmp (conn->addrtype, "IP4") == 0)
1249 stream->is_ipv6 = FALSE;
1250 else if (strcmp (conn->addrtype, "IP6") == 0)
1251 stream->is_ipv6 = TRUE;
1256 g_free (stream->destination);
1257 stream->destination = g_strdup (conn->address);
1259 /* check for multicast */
1260 stream->is_multicast =
1261 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1263 stream->ttl = conn->ttl;
1266 /* Go over the connections for a stream.
1267 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1269 * - If we are dealing with a localhost address, we disable multicast
1272 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1273 const GstSDPMedia * media, GstRTSPStream * stream)
1275 const GstSDPConnection *conn;
1278 /* first look in the media specific section */
1279 len = gst_sdp_media_connections_len (media);
1280 for (i = 0; i < len; i++) {
1281 conn = gst_sdp_media_get_connection (media, i);
1283 gst_rtspsrc_do_stream_connection (src, stream, conn);
1285 /* then look in the message specific section */
1286 if ((conn = gst_sdp_message_get_connection (sdp))) {
1287 gst_rtspsrc_do_stream_connection (src, stream, conn);
1291 static const gchar *
1292 get_aggregate_control (GstRTSPSrc * src)
1297 base = src->control;
1298 else if (src->content_base)
1299 base = src->content_base;
1300 else if (src->conninfo.url_str)
1301 base = src->conninfo.url_str;
1308 static GstRTSPStream *
1309 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1311 GstRTSPStream *stream;
1312 const gchar *control_url;
1313 const gchar *payload;
1314 const GstSDPMedia *media;
1316 /* get media, should not return NULL */
1317 media = gst_sdp_message_get_media (sdp, idx);
1321 stream = g_new0 (GstRTSPStream, 1);
1322 stream->parent = src;
1323 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1325 stream->last_ret = GST_FLOW_NOT_LINKED;
1326 stream->added = FALSE;
1327 stream->disabled = FALSE;
1328 stream->id = src->numstreams++;
1329 stream->eos = FALSE;
1330 stream->discont = TRUE;
1331 stream->seqbase = -1;
1332 stream->timebase = -1;
1334 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1335 * session manager to scale RTCP. */
1336 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1338 /* collect connection info */
1339 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1341 /* we must have a payload. No payload means we cannot create caps */
1342 /* FIXME, handle multiple formats. The problem here is that we just want to
1343 * take the first available format that we can handle but in order to do that
1344 * we need to scan for depayloader plugins. Scanning for payloader plugins is
1345 * also suboptimal because the user maybe just wants to save the raw stream
1346 * and then we don't care. */
1347 if ((payload = gst_sdp_media_get_format (media, 0))) {
1348 stream->pt = atoi (payload);
1350 stream->caps = gst_rtspsrc_media_to_caps (stream->pt, media);
1352 GST_DEBUG ("mapping sdp session level attributes to caps");
1353 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, stream->caps);
1354 GST_DEBUG ("mapping sdp media level attributes to caps");
1355 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, stream->caps);
1357 if (stream->pt >= 96) {
1358 /* If we have a dynamic payload type, see if we have a stream with the
1359 * same payload number. If there is one, they are part of the same
1360 * container and we only need to add one pad. */
1361 if (find_stream (src, &stream->pt, (gpointer) find_stream_by_pt)) {
1362 stream->container = TRUE;
1363 GST_DEBUG ("found another stream with pt %d, marking as container",
1368 /* collect port number */
1369 stream->port = gst_sdp_media_get_port (media);
1371 /* get control url to construct the setup url. The setup url is used to
1372 * configure the transport of the stream and is used to identity the stream in
1373 * the RTP-Info header field returned from PLAY. */
1374 control_url = gst_sdp_media_get_attribute_val (media, "control");
1375 if (control_url == NULL)
1376 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1378 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1379 GST_DEBUG_OBJECT (src, " pt: %d", stream->pt);
1380 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1381 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1382 GST_DEBUG_OBJECT (src, " caps: %" GST_PTR_FORMAT, stream->caps);
1383 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1385 if (control_url != NULL) {
1386 stream->control_url = g_strdup (control_url);
1387 /* Build a fully qualified url using the content_base if any or by prefixing
1388 * the original request.
1389 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1390 * likely build a URL that the server will fail to understand, this is ok,
1391 * we will fail then. */
1392 if (g_str_has_prefix (control_url, "rtsp://"))
1393 stream->conninfo.location = g_strdup (control_url);
1398 if (g_strcmp0 (control_url, "*") == 0)
1401 base = get_aggregate_control (src);
1403 /* check if the base ends or control starts with / */
1404 has_slash = g_str_has_prefix (control_url, "/");
1405 has_slash = has_slash || g_str_has_suffix (base, "/");
1407 /* concatenate the two strings, insert / when not present */
1408 stream->conninfo.location =
1409 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1412 GST_DEBUG_OBJECT (src, " setup: %s",
1413 GST_STR_NULL (stream->conninfo.location));
1415 /* we keep track of all streams */
1416 src->streams = g_list_append (src->streams, stream);
1424 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1428 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1431 gst_caps_unref (stream->caps);
1433 g_free (stream->destination);
1434 g_free (stream->control_url);
1435 g_free (stream->conninfo.location);
1437 for (i = 0; i < 2; i++) {
1438 if (stream->udpsrc[i]) {
1439 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1440 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1441 gst_object_unref (stream->udpsrc[i]);
1442 stream->udpsrc[i] = NULL;
1444 if (stream->channelpad[i]) {
1445 gst_object_unref (stream->channelpad[i]);
1446 stream->channelpad[i] = NULL;
1448 if (stream->udpsink[i]) {
1449 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1450 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1451 gst_object_unref (stream->udpsink[i]);
1452 stream->udpsink[i] = NULL;
1455 if (stream->fakesrc) {
1456 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1457 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1458 gst_object_unref (stream->fakesrc);
1459 stream->fakesrc = NULL;
1461 if (stream->srcpad) {
1462 gst_pad_set_active (stream->srcpad, FALSE);
1463 if (stream->added) {
1464 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1465 stream->added = FALSE;
1467 stream->srcpad = NULL;
1469 if (stream->rtcppad) {
1470 gst_object_unref (stream->rtcppad);
1471 stream->rtcppad = NULL;
1473 if (stream->session) {
1474 g_object_unref (stream->session);
1475 stream->session = NULL;
1481 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1485 GST_DEBUG_OBJECT (src, "cleanup");
1487 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1488 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1490 gst_rtspsrc_stream_free (src, stream);
1492 g_list_free (src->streams);
1493 src->streams = NULL;
1495 if (src->manager_sig_id) {
1496 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1497 src->manager_sig_id = 0;
1499 gst_element_set_state (src->manager, GST_STATE_NULL);
1500 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1501 src->manager = NULL;
1503 src->numstreams = 0;
1505 gst_structure_free (src->props);
1508 g_free (src->content_base);
1509 src->content_base = NULL;
1511 g_free (src->control);
1512 src->control = NULL;
1515 gst_rtsp_range_free (src->range);
1518 /* don't clear the SDP when it was used in the url */
1519 if (src->sdp && !src->from_sdp) {
1520 gst_sdp_message_free (src->sdp);
1523 if (src->start_segment) {
1524 gst_event_unref (src->start_segment);
1525 src->start_segment = NULL;
1527 if (src->provided_clock) {
1528 gst_object_unref (src->provided_clock);
1529 src->provided_clock = NULL;
1533 #define PARSE_INT(p, del, res) \
1536 p = strstr (p, del); \
1546 #define PARSE_STRING(p, del, res) \
1549 p = strstr (p, del); \
1561 #define SKIP_SPACES(p) \
1562 while (*p && g_ascii_isspace (*p)) \
1567 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1570 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1571 gint * rate, gchar ** params)
1575 p = (gchar *) rtpmap;
1577 PARSE_INT (p, " ", *payload);
1585 PARSE_STRING (p, "/", *name);
1586 if (*name == NULL) {
1587 GST_DEBUG ("no rate, name %s", p);
1588 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1589 * streams seem to omit the rate. */
1596 p = strstr (p, "/");
1614 * Mapping SDP attributes to caps
1616 * prepend 'a-' to IANA registered sdp attributes names
1617 * (ie: not prefixed with 'x-') in order to avoid
1618 * collision with gstreamer standard caps properties names
1621 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1623 if (attributes->len > 0) {
1627 s = gst_caps_get_structure (caps, 0);
1629 for (i = 0; i < attributes->len; i++) {
1630 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1631 gchar *tofree, *key;
1635 /* skip some of the attribute we already handle */
1636 if (!strcmp (key, "fmtp"))
1638 if (!strcmp (key, "rtpmap"))
1640 if (!strcmp (key, "control"))
1642 if (!strcmp (key, "range"))
1645 /* string must be valid UTF8 */
1646 if (!g_utf8_validate (attr->value, -1, NULL))
1649 if (!g_str_has_prefix (key, "x-"))
1650 tofree = key = g_strdup_printf ("a-%s", key);
1654 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1655 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1662 * Mapping of caps to and from SDP fields:
1664 * m=<media> <UDP port> RTP/AVP <payload>
1665 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1666 * a=fmtp:<payload> <param>[=<value>];...
1669 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1672 const gchar *rtpmap;
1676 gchar *params = NULL;
1682 /* get and parse rtpmap */
1683 if ((rtpmap = gst_sdp_media_get_attribute_val (media, "rtpmap"))) {
1684 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
1686 if (payload != pt) {
1687 /* we ignore the rtpmap if the payload type is different. */
1688 g_warning ("rtpmap of wrong payload type, ignoring");
1694 /* if we failed to parse the rtpmap for a dynamic payload type, we have an
1698 /* else we can ignore */
1699 g_warning ("error parsing rtpmap, ignoring");
1702 /* dynamic payloads need rtpmap or we fail */
1706 /* check if we have a rate, if not, we need to look up the rate from the
1707 * default rates based on the payload types. */
1709 const GstRTPPayloadInfo *info;
1711 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
1712 /* dynamic types, use media and encoding_name */
1713 tmp = g_ascii_strdown (media->media, -1);
1714 info = gst_rtp_payload_info_for_name (tmp, name);
1717 /* static types, use payload type */
1718 info = gst_rtp_payload_info_for_pt (pt);
1722 if ((rate = info->clock_rate) == 0)
1725 /* we fail if we cannot find one */
1730 tmp = g_ascii_strdown (media->media, -1);
1731 caps = gst_caps_new_simple ("application/x-unknown",
1732 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
1734 s = gst_caps_get_structure (caps, 0);
1736 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
1738 /* encoding name must be upper case */
1740 tmp = g_ascii_strup (name, -1);
1741 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
1745 /* params must be lower case */
1746 if (params != NULL) {
1747 tmp = g_ascii_strdown (params, -1);
1748 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
1752 /* parse optional fmtp: field */
1753 if ((fmtp = gst_sdp_media_get_attribute_val (media, "fmtp"))) {
1759 /* p is now of the format <payload> <param>[=<value>];... */
1760 PARSE_INT (p, " ", payload);
1761 if (payload != -1 && payload == pt) {
1765 /* <param>[=<value>] are separated with ';' */
1766 pairs = g_strsplit (p, ";", 0);
1767 for (i = 0; pairs[i]; i++) {
1769 const gchar *val, *key;
1771 /* the key may not have a '=', the value can have other '='s */
1772 valpos = strstr (pairs[i], "=");
1774 /* we have a '=' and thus a value, remove the '=' with \0 */
1776 /* value is everything between '=' and ';'. We split the pairs at ;
1777 * boundaries so we can take the remainder of the value. Some servers
1778 * put spaces around the value which we strip off here. Alternatively
1779 * we could strip those spaces in the depayloaders should these spaces
1780 * actually carry any meaning in the future. */
1781 val = g_strstrip (valpos + 1);
1783 /* simple <param>;.. is translated into <param>=1;... */
1786 /* strip the key of spaces, convert key to lowercase but not the value. */
1787 key = g_strstrip (pairs[i]);
1788 if (strlen (key) > 1) {
1789 tmp = g_ascii_strdown (key, -1);
1790 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
1802 g_warning ("rtpmap type not given for dynamic payload %d", pt);
1807 g_warning ("rate unknown for payload type %d", pt);
1813 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1814 gint * rtpport, gint * rtcpport)
1817 GstStateChangeReturn ret;
1818 GstElement *udpsrc0, *udpsrc1;
1819 gint tmp_rtp, tmp_rtcp;
1823 src = stream->parent;
1829 /* Start at next port */
1830 tmp_rtp = src->next_port_num;
1832 if (stream->is_ipv6)
1833 host = "udp://[::0]";
1835 host = "udp://0.0.0.0";
1837 /* try to allocate 2 UDP ports, the RTP port should be an even
1838 * number and the RTCP port should be the next (uneven) port */
1841 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1842 tmp_rtp >= src->client_port_range.max)
1845 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1846 if (udpsrc0 == NULL)
1847 goto no_udp_protocol;
1848 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1850 if (src->udp_buffer_size != 0)
1851 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
1854 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
1855 if (ret == GST_STATE_CHANGE_FAILURE) {
1857 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
1860 if (++count > src->retry)
1863 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1864 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1865 gst_object_unref (udpsrc0);
1868 GST_DEBUG_OBJECT (src, "retry %d", count);
1871 goto no_udp_protocol;
1874 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
1875 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
1877 /* check if port is even */
1878 if ((tmp_rtp & 0x01) != 0) {
1879 /* port not even, close and allocate another */
1880 if (++count > src->retry)
1883 GST_DEBUG_OBJECT (src, "RTP port not even");
1885 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1886 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1887 gst_object_unref (udpsrc0);
1890 GST_DEBUG_OBJECT (src, "retry %d", count);
1895 /* allocate port+1 for RTCP now */
1896 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1897 if (udpsrc1 == NULL)
1898 goto no_udp_rtcp_protocol;
1901 tmp_rtcp = tmp_rtp + 1;
1902 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
1905 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
1907 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
1908 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
1909 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
1910 if (ret == GST_STATE_CHANGE_FAILURE) {
1911 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
1913 if (++count > src->retry)
1916 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1917 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1918 gst_object_unref (udpsrc0);
1921 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
1922 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1923 gst_object_unref (udpsrc1);
1927 GST_DEBUG_OBJECT (src, "retry %d", count);
1931 /* all fine, do port check */
1932 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
1933 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
1935 /* this should not happen... */
1936 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
1939 /* we keep these elements, we configure all in configure_transport when the
1940 * server told us to really use the UDP ports. */
1941 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
1942 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
1943 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
1944 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
1946 /* keep track of next available port number when we have a range
1948 if (src->next_port_num != 0)
1949 src->next_port_num = tmp_rtcp + 1;
1956 GST_DEBUG_OBJECT (src, "could not get UDP source");
1961 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
1965 no_udp_rtcp_protocol:
1967 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
1972 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
1973 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
1979 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1980 gst_object_unref (udpsrc0);
1983 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1984 gst_object_unref (udpsrc1);
1991 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
1996 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
1998 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1999 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2002 for (i = 0; i < 2; i++) {
2003 if (stream->udpsrc[i])
2004 gst_element_set_state (stream->udpsrc[i], state);
2010 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2017 event = gst_event_new_flush_start ();
2018 GST_DEBUG_OBJECT (src, "start flush");
2020 state = GST_STATE_PAUSED;
2022 event = gst_event_new_flush_stop (FALSE);
2023 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2026 state = GST_STATE_PLAYING;
2028 state = GST_STATE_PAUSED;
2030 gst_rtspsrc_push_event (src, event);
2031 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2032 gst_rtspsrc_set_state (src, state);
2035 static GstRTSPResult
2036 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2037 GstRTSPMessage * message, GTimeVal * timeout)
2042 ret = gst_rtsp_connection_send (conn, message, timeout);
2044 ret = GST_RTSP_ERROR;
2049 static GstRTSPResult
2050 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2051 GstRTSPMessage * message, GTimeVal * timeout)
2056 ret = gst_rtsp_connection_receive (conn, message, timeout);
2058 ret = GST_RTSP_ERROR;
2064 gst_rtspsrc_get_position (GstRTSPSrc * src)
2069 query = gst_query_new_position (GST_FORMAT_TIME);
2070 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2071 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2072 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2076 if (stream->srcpad) {
2077 if (gst_pad_query (stream->srcpad, query)) {
2078 gst_query_parse_position (query, &fmt, &pos);
2079 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2080 GST_TIME_ARGS (pos));
2081 src->last_pos = pos;
2091 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
2093 src->state = GST_RTSP_STATE_SEEKING;
2094 /* PLAY will add the range header now. */
2095 src->need_range = TRUE;
2101 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2106 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2108 gboolean flush, skip;
2111 GstSegment seeksegment = { 0, };
2115 GST_DEBUG_OBJECT (src, "doing seek with event");
2117 gst_event_parse_seek (event, &rate, &format, &flags,
2118 &cur_type, &cur, &stop_type, &stop);
2120 /* no negative rates yet */
2124 /* we need TIME format */
2125 if (format != src->segment.format)
2128 GST_DEBUG_OBJECT (src, "doing seek without event");
2130 cur_type = GST_SEEK_TYPE_SET;
2131 stop_type = GST_SEEK_TYPE_SET;
2134 /* get flush flag */
2135 flush = flags & GST_SEEK_FLAG_FLUSH;
2136 skip = flags & GST_SEEK_FLAG_SKIP;
2138 /* now we need to make sure the streaming thread is stopped. We do this by
2139 * either sending a FLUSH_START event downstream which will cause the
2140 * streaming thread to stop with a WRONG_STATE.
2141 * For a non-flushing seek we simply pause the task, which will happen as soon
2142 * as it completes one iteration (and thus might block when the sink is
2143 * blocking in preroll). */
2145 GST_DEBUG_OBJECT (src, "starting flush");
2146 gst_rtspsrc_flush (src, TRUE, FALSE);
2149 gst_task_pause (src->task);
2153 /* we should now be able to grab the streaming thread because we stopped it
2154 * with the above flush/pause code */
2155 GST_RTSP_STREAM_LOCK (src);
2157 GST_DEBUG_OBJECT (src, "stopped streaming");
2159 /* copy segment, we need this because we still need the old
2160 * segment when we close the current segment. */
2161 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2163 /* configure the seek parameters in the seeksegment. We will then have the
2164 * right values in the segment to perform the seek */
2166 GST_DEBUG_OBJECT (src, "configuring seek");
2167 gst_segment_do_seek (&seeksegment, rate, format, flags,
2168 cur_type, cur, stop_type, stop, &update);
2171 /* figure out the last position we need to play. If it's configured (stop !=
2172 * -1), use that, else we play until the total duration of the file */
2173 if ((stop = seeksegment.stop) == -1)
2174 stop = seeksegment.duration;
2176 playing = (src->state == GST_RTSP_STATE_PLAYING);
2178 /* if we were playing, pause first */
2180 /* obtain current position in case seek fails */
2181 gst_rtspsrc_get_position (src);
2182 gst_rtspsrc_pause (src, FALSE);
2186 gst_rtspsrc_do_seek (src, &seeksegment);
2188 /* and continue playing */
2190 gst_rtspsrc_play (src, &seeksegment, FALSE);
2192 /* prepare for streaming again */
2194 /* if we started flush, we stop now */
2195 GST_DEBUG_OBJECT (src, "stopping flush");
2196 gst_rtspsrc_flush (src, FALSE, playing);
2199 /* now we did the seek and can activate the new segment values */
2200 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2202 /* if we're doing a segment seek, post a SEGMENT_START message */
2203 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2204 gst_element_post_message (GST_ELEMENT_CAST (src),
2205 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2206 src->segment.format, src->segment.position));
2209 /* now create the newsegment */
2210 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2211 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2214 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2215 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2216 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2217 stream->discont = TRUE;
2220 GST_RTSP_STREAM_UNLOCK (src);
2227 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2232 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2238 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2242 gboolean res = TRUE;
2245 src = GST_RTSPSRC_CAST (parent);
2247 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2248 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2250 switch (GST_EVENT_TYPE (event)) {
2251 case GST_EVENT_SEEK:
2252 res = gst_rtspsrc_perform_seek (src, event);
2256 case GST_EVENT_NAVIGATION:
2257 case GST_EVENT_LATENCY:
2265 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2266 res = gst_pad_send_event (target, event);
2267 gst_object_unref (target);
2269 gst_event_unref (event);
2272 gst_event_unref (event);
2278 /* this is the final event function we receive on the internal source pad when
2279 * we deal with TCP connections */
2281 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2286 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2288 switch (GST_EVENT_TYPE (event)) {
2289 case GST_EVENT_SEEK:
2291 case GST_EVENT_NAVIGATION:
2292 case GST_EVENT_LATENCY:
2294 gst_event_unref (event);
2301 /* this is the final query function we receive on the internal source pad when
2302 * we deal with TCP connections */
2304 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2308 gboolean res = TRUE;
2310 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2312 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2313 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2315 switch (GST_QUERY_TYPE (query)) {
2316 case GST_QUERY_POSITION:
2321 case GST_QUERY_DURATION:
2325 gst_query_parse_duration (query, &format, NULL);
2328 case GST_FORMAT_TIME:
2329 gst_query_set_duration (query, format, src->segment.duration);
2337 case GST_QUERY_LATENCY:
2339 /* we are live with a min latency of 0 and unlimited max latency, this
2340 * result will be updated by the session manager if there is any. */
2341 gst_query_set_latency (query, TRUE, 0, -1);
2351 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2353 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2357 gboolean res = FALSE;
2359 src = GST_RTSPSRC_CAST (parent);
2361 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2362 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2364 switch (GST_QUERY_TYPE (query)) {
2365 case GST_QUERY_DURATION:
2369 gst_query_parse_duration (query, &format, NULL);
2372 case GST_FORMAT_TIME:
2373 gst_query_set_duration (query, format, src->segment.duration);
2381 case GST_QUERY_SEEKING:
2385 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2386 if (format == GST_FORMAT_TIME) {
2388 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2390 /* seeking without duration is unlikely */
2391 seekable = seekable && src->seekable && src->segment.duration &&
2392 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2394 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2395 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2396 src->segment.start, src->segment.stop);
2405 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2407 gst_query_set_uri (query, uri);
2415 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2417 /* forward the query to the proxy target pad */
2419 res = gst_pad_query (target, query);
2420 gst_object_unref (target);
2429 /* callback for RTCP messages to be sent to the server when operating in TCP
2431 static GstFlowReturn
2432 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2435 GstRTSPStream *stream;
2436 GstFlowReturn res = GST_FLOW_OK;
2441 GstRTSPMessage message = { 0 };
2442 GstRTSPConnection *conn;
2444 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2445 src = stream->parent;
2447 gst_buffer_map (buffer, &map, GST_MAP_READ);
2451 gst_rtsp_message_init_data (&message, stream->channel[1]);
2453 /* lend the body data to the message */
2454 gst_rtsp_message_take_body (&message, data, size);
2456 if (stream->conninfo.connection)
2457 conn = stream->conninfo.connection;
2459 conn = src->conninfo.connection;
2461 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2462 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2463 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2465 /* and steal it away again because we will free it when unreffing the
2467 gst_rtsp_message_steal_body (&message, &data, &size);
2468 gst_rtsp_message_unset (&message);
2470 gst_buffer_unmap (buffer, &map);
2471 gst_buffer_unref (buffer);
2476 static GstPadProbeReturn
2477 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2479 GstRTSPSrc *src = user_data;
2481 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2482 GST_DEBUG_PAD_NAME (pad));
2484 /* activate the streams */
2485 GST_OBJECT_LOCK (src);
2486 if (!src->need_activate)
2489 src->need_activate = FALSE;
2490 GST_OBJECT_UNLOCK (src);
2492 gst_rtspsrc_activate_streams (src);
2494 return GST_PAD_PROBE_OK;
2498 GST_OBJECT_UNLOCK (src);
2499 return GST_PAD_PROBE_OK;
2503 /* this callback is called when the session manager generated a new src pad with
2504 * payloaded RTP packets. We simply ghost the pad here. */
2506 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2509 GstPadTemplate *template;
2512 GstRTSPStream *stream;
2515 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2517 GST_RTSP_STATE_LOCK (src);
2519 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2520 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2521 goto unknown_stream;
2523 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2525 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2527 goto unknown_stream;
2530 stream->ssrc = ssrc;
2532 /* we'll add it later see below */
2533 stream->added = TRUE;
2535 /* check if we added all streams */
2537 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2538 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2540 GST_DEBUG_OBJECT (src, "stream %p, container %d, disabled %d, added %d",
2541 ostream, ostream->container, ostream->disabled, ostream->added);
2543 /* a container stream only needs one pad added. Also disabled streams don't
2545 if (!ostream->container && !ostream->disabled && !ostream->added) {
2550 GST_RTSP_STATE_UNLOCK (src);
2552 /* create a new pad we will use to stream to */
2553 template = gst_static_pad_template_get (&rtptemplate);
2554 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2555 gst_object_unref (template);
2558 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2559 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2560 gst_pad_set_active (stream->srcpad, TRUE);
2561 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2564 GST_DEBUG_OBJECT (src, "We added all streams");
2565 /* when we get here, all stream are added and we can fire the no-more-pads
2567 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2575 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2576 GST_RTSP_STATE_UNLOCK (src);
2583 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2585 GstRTSPStream *stream;
2588 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2590 GST_RTSP_STATE_LOCK (src);
2591 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2593 goto unknown_stream;
2595 caps = stream->caps;
2597 gst_caps_ref (caps);
2598 GST_RTSP_STATE_UNLOCK (src);
2604 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2605 GST_RTSP_STATE_UNLOCK (src);
2611 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2613 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2619 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2625 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2631 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2633 GstRTSPSrc *src = stream->parent;
2636 g_object_get (source, "ssrc", &ssrc, NULL);
2638 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2639 ssrc, stream->ssrc, stream->id);
2641 if (ssrc == stream->ssrc)
2642 gst_rtspsrc_do_stream_eos (src, stream);
2646 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2648 GstRTSPSrc *src = stream->parent;
2651 g_object_get (source, "ssrc", &ssrc, NULL);
2653 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2654 ssrc, stream->ssrc, stream->id);
2656 if (ssrc == stream->ssrc)
2657 gst_rtspsrc_do_stream_eos (src, stream);
2661 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2663 GstRTSPStream *stream;
2665 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2667 /* get stream for session */
2668 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2670 gst_rtspsrc_do_stream_eos (src, stream);
2675 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2677 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2682 set_manager_buffer_mode (GstRTSPSrc * src)
2684 GObjectClass *klass;
2686 if (src->manager == NULL)
2689 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2691 if (!g_object_class_find_property (klass, "buffer-mode"))
2694 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2695 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2700 GST_DEBUG_OBJECT (src,
2701 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
2703 if (src->provided_clock) {
2704 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
2706 if (clock == src->provided_clock) {
2707 GST_DEBUG_OBJECT (src, "selected synced");
2708 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
2711 gst_object_unref (clock);
2716 /* Otherwise fall-through and use another buffer mode */
2718 gst_object_unref (clock);
2721 GST_DEBUG_OBJECT (src, "auto buffering mode");
2722 if (src->use_buffering) {
2723 GST_DEBUG_OBJECT (src, "selected buffer");
2724 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
2726 GST_DEBUG_OBJECT (src, "selected slave");
2727 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2731 /* try to get and configure a manager */
2733 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
2734 GstRTSPTransport * transport)
2736 const gchar *manager;
2738 GstStateChangeReturn ret;
2740 /* find a manager */
2741 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
2745 GST_DEBUG_OBJECT (src, "using manager %s", manager);
2747 /* configure the manager */
2748 if (src->manager == NULL) {
2749 GObjectClass *klass;
2751 const gchar *encoding;
2752 gboolean need_slave;
2754 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
2756 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
2760 goto use_no_manager;
2762 if (!(src->manager = gst_element_factory_make (manager, "manager")))
2763 goto manager_failed;
2766 /* we manage this element */
2767 gst_element_set_locked_state (src->manager, TRUE);
2768 gst_bin_add (GST_BIN_CAST (src), src->manager);
2770 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
2771 if (ret == GST_STATE_CHANGE_FAILURE)
2772 goto start_manager_failure;
2774 g_object_set (src->manager, "latency", src->latency, NULL);
2776 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2778 if (g_object_class_find_property (klass, "ntp-sync")) {
2779 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
2782 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
2783 g_object_set (src->manager, "use-pipeline-clock",
2784 src->use_pipeline_clock, NULL);
2787 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
2788 g_object_set (src->manager, "sdes", src->sdes, NULL);
2791 if (g_object_class_find_property (klass, "drop-on-latency")) {
2792 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
2796 if (g_object_class_find_property (klass, "do-retransmission")) {
2797 g_object_set (src->manager, "do-retransmission", src->do_retransmission,
2801 /* buffer mode pauses are handled by adding offsets to buffer times,
2802 * but some depayloaders may have a hard time syncing output times
2803 * with such input times, e.g. container ones, most notably ASF */
2804 /* TODO alternatives are having an event that indicates these shifts,
2805 * or having rtsp extensions provide suggestion on buffer mode */
2806 need_slave = stream->container;
2807 if (stream->caps && (s = gst_caps_get_structure (stream->caps, 0)) &&
2808 (encoding = gst_structure_get_string (s, "encoding-name")))
2809 need_slave = need_slave || (strcmp (encoding, "X-ASF-PF") == 0);
2810 /* valid duration implies not likely live pipeline,
2811 * so slaving in jitterbuffer does not make much sense
2812 * (and might mess things up due to bursts) */
2813 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
2814 src->segment.duration && !need_slave) {
2815 src->use_buffering = TRUE;
2817 src->use_buffering = FALSE;
2820 set_manager_buffer_mode (src);
2822 /* connect to signals */
2823 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
2825 src->manager_sig_id =
2826 g_signal_connect (src->manager, "pad-added",
2827 (GCallback) new_manager_pad, src);
2828 src->manager_ptmap_id =
2829 g_signal_connect (src->manager, "request-pt-map",
2830 (GCallback) request_pt_map, src);
2832 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
2835 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
2839 /* we stream directly to the manager, get some pads. Each RTSP stream goes
2840 * into a separate RTP session. */
2841 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
2842 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
2844 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
2845 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
2848 /* now configure the bandwidth in the manager */
2849 if (g_signal_lookup ("get-internal-session",
2850 G_OBJECT_TYPE (src->manager)) != 0) {
2851 GObject *rtpsession;
2853 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
2856 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
2858 stream->session = rtpsession;
2860 if (stream->as_bandwidth != -1) {
2861 GST_INFO_OBJECT (src, "setting AS: %f",
2862 (gdouble) (stream->as_bandwidth * 1000));
2863 g_object_set (rtpsession, "bandwidth",
2864 (gdouble) (stream->as_bandwidth * 1000), NULL);
2866 if (stream->rr_bandwidth != -1) {
2867 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
2868 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
2871 if (stream->rs_bandwidth != -1) {
2872 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
2873 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
2877 g_object_set (rtpsession, "probation", src->probation, NULL);
2879 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2881 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
2883 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
2885 g_signal_connect (rtpsession, "on-ssrc-active",
2886 (GCallback) on_ssrc_active, stream);
2897 GST_DEBUG_OBJECT (src, "cannot get a session manager");
2902 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
2905 start_manager_failure:
2907 GST_DEBUG_OBJECT (src, "could not start session manager");
2912 /* free the UDP sources allocated when negotiating a transport.
2913 * This function is called when the server negotiated to a transport where the
2914 * UDP sources are not needed anymore, such as TCP or multicast. */
2916 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
2920 for (i = 0; i < 2; i++) {
2921 if (stream->udpsrc[i]) {
2922 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
2923 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
2924 gst_object_unref (stream->udpsrc[i]);
2925 stream->udpsrc[i] = NULL;
2930 /* for TCP, create pads to send and receive data to and from the manager and to
2931 * intercept various events and queries
2934 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
2935 GstRTSPTransport * transport, GstPad ** outpad)
2938 GstPadTemplate *template;
2939 GstPad *pad0, *pad1;
2941 /* configure for interleaved delivery, nothing needs to be done
2942 * here, the loop function will call the chain functions of the
2943 * session manager. */
2944 stream->channel[0] = transport->interleaved.min;
2945 stream->channel[1] = transport->interleaved.max;
2946 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
2947 stream->channel[0], stream->channel[1]);
2949 /* we can remove the allocated UDP ports now */
2950 gst_rtspsrc_stream_free_udp (stream);
2952 /* no session manager, send data to srcpad directly */
2953 if (!stream->channelpad[0]) {
2954 GST_DEBUG_OBJECT (src, "no manager, creating pad");
2956 /* create a new pad we will use to stream to */
2957 name = g_strdup_printf ("stream_%u", stream->id);
2958 template = gst_static_pad_template_get (&rtptemplate);
2959 stream->channelpad[0] = gst_pad_new_from_template (template, name);
2960 gst_object_unref (template);
2963 /* set caps and activate */
2964 gst_pad_use_fixed_caps (stream->channelpad[0]);
2965 gst_pad_set_active (stream->channelpad[0], TRUE);
2967 *outpad = gst_object_ref (stream->channelpad[0]);
2969 GST_DEBUG_OBJECT (src, "using manager source pad");
2971 template = gst_static_pad_template_get (&anysrctemplate);
2973 /* allocate pads for sending the channel data into the manager */
2974 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
2975 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
2976 gst_object_unref (stream->channelpad[0]);
2977 stream->channelpad[0] = pad0;
2978 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
2979 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
2980 gst_pad_set_element_private (pad0, src);
2981 gst_pad_set_active (pad0, TRUE);
2983 if (stream->channelpad[1]) {
2984 /* if we have a sinkpad for the other channel, create a pad and link to the
2986 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
2987 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
2988 gst_pad_link_full (pad1, stream->channelpad[1],
2989 GST_PAD_LINK_CHECK_NOTHING);
2990 gst_object_unref (stream->channelpad[1]);
2991 stream->channelpad[1] = pad1;
2992 gst_pad_set_active (pad1, TRUE);
2994 gst_object_unref (template);
2996 /* setup RTCP transport back to the server if we have to. */
2997 if (src->manager && src->do_rtcp) {
3000 template = gst_static_pad_template_get (&anysinktemplate);
3002 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3003 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3004 gst_pad_set_element_private (stream->rtcppad, stream);
3005 gst_pad_set_active (stream->rtcppad, TRUE);
3007 /* get session RTCP pad */
3008 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3009 pad = gst_element_get_request_pad (src->manager, name);
3014 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3015 gst_object_unref (pad);
3018 gst_object_unref (template);
3024 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3025 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3026 gint * max, guint * ttl)
3028 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3030 if (!(*destination = transport->destination))
3031 *destination = stream->destination;
3034 /* transport first */
3035 *min = transport->port.min;
3036 *max = transport->port.max;
3037 if (*min == -1 && *max == -1) {
3038 /* then try from SDP */
3039 if (stream->port != 0) {
3040 *min = stream->port;
3041 *max = stream->port + 1;
3047 if (!(*ttl = transport->ttl))
3052 /* first take the source, then the endpoint to figure out where to send
3054 if (!(*destination = transport->source)) {
3055 if (src->conninfo.connection)
3056 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3057 else if (stream->conninfo.connection)
3059 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3063 /* for unicast we only expect the ports here */
3064 *min = transport->server_port.min;
3065 *max = transport->server_port.max;
3070 /* For multicast create UDP sources and join the multicast group. */
3072 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3073 GstRTSPTransport * transport, GstPad ** outpad)
3076 const gchar *destination;
3079 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3081 /* we can remove the allocated UDP ports now */
3082 gst_rtspsrc_stream_free_udp (stream);
3084 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3087 /* we need a destination now */
3088 if (destination == NULL)
3089 goto no_destination;
3091 /* we really need ports now or we won't be able to receive anything at all */
3092 if (min == -1 && max == -1)
3095 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3096 destination, min, max);
3098 /* creating UDP source for RTP */
3100 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3102 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3104 if (stream->udpsrc[0] == NULL)
3107 /* take ownership */
3108 gst_object_ref_sink (stream->udpsrc[0]);
3110 if (src->udp_buffer_size != 0)
3111 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3112 src->udp_buffer_size, NULL);
3114 if (src->multi_iface != NULL)
3115 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3116 src->multi_iface, NULL);
3119 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3120 gst_element_set_state (stream->udpsrc[0], GST_STATE_PAUSED);
3123 /* creating another UDP source for RTCP */
3127 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3129 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3131 if (stream->udpsrc[1] == NULL)
3134 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3135 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3136 gst_caps_unref (caps);
3138 /* take ownership */
3139 gst_object_ref_sink (stream->udpsrc[1]);
3141 if (src->multi_iface != NULL)
3142 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3143 src->multi_iface, NULL);
3145 gst_element_set_state (stream->udpsrc[1], GST_STATE_PAUSED);
3152 GST_DEBUG_OBJECT (src, "no UDP source element found");
3157 GST_DEBUG_OBJECT (src, "no destination found");
3162 GST_DEBUG_OBJECT (src, "no ports found");
3167 /* configure the remainder of the UDP ports */
3169 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3170 GstRTSPTransport * transport, GstPad ** outpad)
3172 /* we manage the UDP elements now. For unicast, the UDP sources where
3173 * allocated in the stream when we suggested a transport. */
3174 if (stream->udpsrc[0]) {
3175 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3176 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3178 GST_DEBUG_OBJECT (src, "setting up UDP source");
3180 /* configure a timeout on the UDP port. When the timeout message is
3181 * posted, we assume UDP transport is not possible. We reconnect using TCP
3183 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3184 src->udp_timeout * 1000, NULL);
3186 /* get output pad of the UDP source. */
3187 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3189 /* save it so we can unblock */
3190 stream->blockedpad = *outpad;
3192 /* configure pad block on the pad. As soon as there is dataflow on the
3193 * UDP source, we know that UDP is not blocked by a firewall and we can
3194 * configure all the streams to let the application autoplug decoders. */
3196 gst_pad_add_probe (stream->blockedpad,
3197 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, pad_blocked, src, NULL);
3199 if (stream->channelpad[0]) {
3200 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3201 /* configure for UDP delivery, we need to connect the UDP pads to
3202 * the session plugin. */
3203 gst_pad_link_full (*outpad, stream->channelpad[0],
3204 GST_PAD_LINK_CHECK_NOTHING);
3205 gst_object_unref (*outpad);
3207 /* we connected to pad-added signal to get pads from the manager */
3209 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3214 if (stream->udpsrc[1]) {
3217 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3218 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3220 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3221 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3222 gst_caps_unref (caps);
3224 if (stream->channelpad[1]) {
3227 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3229 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3230 gst_pad_link_full (pad, stream->channelpad[1],
3231 GST_PAD_LINK_CHECK_NOTHING);
3232 gst_object_unref (pad);
3234 /* leave unlinked */
3240 /* configure the UDP sink back to the server for status reports */
3242 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3243 GstRTSPStream * stream, GstRTSPTransport * transport)
3246 gint rtp_port, rtcp_port;
3247 gboolean do_rtp, do_rtcp;
3248 const gchar *destination;
3253 /* get transport info */
3254 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3255 &rtp_port, &rtcp_port, &ttl);
3257 /* see what we need to do */
3258 do_rtp = (rtp_port != -1);
3259 /* it's possible that the server does not want us to send RTCP in which case
3261 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3263 /* we need a destination when we have RTP or RTCP ports */
3264 if (destination == NULL && (do_rtp || do_rtcp))
3265 goto no_destination;
3267 /* try to construct the fakesrc to the RTP port of the server to open up any
3270 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3273 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3274 stream->udpsink[0] =
3275 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3277 if (stream->udpsink[0] == NULL)
3278 goto no_sink_element;
3280 /* don't join multicast group, we will have the source socket do that */
3281 /* no sync or async state changes needed */
3282 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3283 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3285 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3287 if (stream->udpsrc[0]) {
3288 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3289 * so that NAT firewalls will open a hole for us */
3290 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3291 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3292 /* configure socket and make sure udpsink does not close it when shutting
3293 * down, it belongs to udpsrc after all. */
3294 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3295 "close-socket", FALSE, NULL);
3296 g_object_unref (socket);
3299 /* the source for the dummy packets to open up NAT */
3300 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3301 if (stream->fakesrc == NULL)
3302 goto no_fakesrc_element;
3304 /* random data in 5 buffers, a size of 200 bytes should be fine */
3305 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3306 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3308 /* we don't want to consider this a sink */
3309 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
3311 /* keep everything locked */
3312 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3313 gst_element_set_locked_state (stream->fakesrc, TRUE);
3315 gst_object_ref (stream->udpsink[0]);
3316 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3317 gst_object_ref (stream->fakesrc);
3318 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3320 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3321 "sink", GST_PAD_LINK_CHECK_NOTHING);
3324 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3327 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3328 stream->udpsink[1] =
3329 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3331 if (stream->udpsink[1] == NULL)
3332 goto no_sink_element;
3334 /* don't join multicast group, we will have the source socket do that */
3335 /* no sync or async state changes needed */
3336 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3337 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3339 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3341 if (stream->udpsrc[1]) {
3342 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3343 * because some servers check the port number of where it sends RTCP to identify
3344 * the RTCP packets it receives */
3345 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3346 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3347 /* configure socket and make sure udpsink does not close it when shutting
3348 * down, it belongs to udpsrc after all. */
3349 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3350 "close-socket", FALSE, NULL);
3351 g_object_unref (socket);
3354 /* we don't want to consider this a sink */
3355 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3357 /* we keep this playing always */
3358 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3359 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3361 gst_object_ref (stream->udpsink[1]);
3362 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3364 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3366 /* get session RTCP pad */
3367 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3368 pad = gst_element_get_request_pad (src->manager, name);
3373 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3374 gst_object_unref (pad);
3383 GST_DEBUG_OBJECT (src, "no destination address specified");
3388 GST_DEBUG_OBJECT (src, "no UDP sink element found");
3393 GST_DEBUG_OBJECT (src, "no fakesrc element found");
3398 /* sets up all elements needed for streaming over the specified transport.
3399 * Does not yet expose the element pads, this will be done when there is actuall
3400 * dataflow detected, which might never happen when UDP is blocked in a
3401 * firewall, for example.
3404 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3405 GstRTSPTransport * transport)
3408 GstPad *outpad = NULL;
3409 GstPadTemplate *template;
3412 const gchar *media_type;
3414 src = stream->parent;
3416 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3418 s = gst_caps_get_structure (stream->caps, 0);
3420 /* get the proper media type for this stream now */
3421 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
3422 goto unknown_transport;
3424 goto unknown_transport;
3426 /* configure the final media type */
3427 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
3428 gst_structure_set_name (s, media_type);
3430 /* try to get and configure a manager, channelpad[0-1] will be configured with
3431 * the pads for the manager, or NULL when no manager is needed. */
3432 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3435 switch (transport->lower_transport) {
3436 case GST_RTSP_LOWER_TRANS_TCP:
3437 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3438 goto transport_failed;
3440 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3441 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3442 goto transport_failed;
3443 /* fallthrough, the rest is the same for UDP and MCAST */
3444 case GST_RTSP_LOWER_TRANS_UDP:
3445 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3446 goto transport_failed;
3447 /* configure udpsinks back to the server for RTCP messages and for the
3448 * dummy RTP messages to open NAT. */
3449 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3450 goto transport_failed;
3453 goto unknown_transport;
3457 GST_DEBUG_OBJECT (src, "creating ghostpad");
3459 gst_pad_use_fixed_caps (outpad);
3461 /* create ghostpad, don't add just yet, this will be done when we activate
3463 name = g_strdup_printf ("stream_%u", stream->id);
3464 template = gst_static_pad_template_get (&rtptemplate);
3465 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3466 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3467 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3468 gst_object_unref (template);
3471 gst_object_unref (outpad);
3473 /* mark pad as ok */
3474 stream->last_ret = GST_FLOW_OK;
3481 GST_DEBUG_OBJECT (src, "failed to configure transport");
3486 GST_DEBUG_OBJECT (src, "unknown transport");
3491 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3496 /* send a couple of dummy random packets on the receiver RTP port to the server,
3497 * this should make a firewall think we initiated the data transfer and
3498 * hopefully allow packets to go from the sender port to our RTP receiver port */
3500 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3504 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3507 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3508 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3510 if (stream->fakesrc && stream->udpsink[0]) {
3511 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3512 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3513 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3514 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3515 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3521 /* Adds the source pads of all configured streams to the element.
3522 * This code is performed when we detected dataflow.
3524 * We detect dataflow from either the _loop function or with pad probes on the
3528 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3532 GST_DEBUG_OBJECT (src, "activating streams");
3534 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3535 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3537 if (stream->udpsrc[0]) {
3538 /* remove timeout, we are streaming now and timeouts will be handled by
3539 * the session manager and jitter buffer */
3540 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3542 if (stream->srcpad) {
3543 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3544 gst_pad_set_active (stream->srcpad, TRUE);
3546 /* if we don't have a session manager, set the caps now. If we have a
3547 * session, we will get a notification of the pad and the caps. */
3548 if (!src->manager) {
3549 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3550 gst_pad_set_caps (stream->srcpad, stream->caps);
3553 if (!stream->added) {
3554 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3555 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3556 stream->added = TRUE;
3561 /* unblock all pads */
3562 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3563 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3565 if (stream->blockid) {
3566 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3567 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3568 stream->blockid = 0;
3576 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
3577 gboolean reset_manager)
3580 guint64 start, stop;
3581 gdouble play_speed, play_scale;
3583 GST_DEBUG_OBJECT (src, "configuring stream caps");
3585 start = segment->position;
3586 stop = segment->duration;
3587 play_speed = segment->rate;
3588 play_scale = segment->applied_rate;
3590 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3591 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3594 if ((caps = stream->caps)) {
3595 caps = gst_caps_make_writable (caps);
3597 if (stream->timebase != -1)
3598 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3599 (guint) stream->timebase, NULL);
3600 if (stream->seqbase != -1)
3601 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3602 (guint) stream->seqbase, NULL);
3603 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3605 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3606 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3607 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3609 stream->caps = caps;
3611 GST_DEBUG_OBJECT (src, "stream %p, caps %" GST_PTR_FORMAT, stream, caps);
3613 if (reset_manager && src->manager) {
3614 GST_DEBUG_OBJECT (src, "clear session");
3615 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3619 static GstFlowReturn
3620 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3625 /* store the value */
3626 stream->last_ret = ret;
3628 /* if it's success we can return the value right away */
3629 if (ret == GST_FLOW_OK)
3632 /* any other error that is not-linked can be returned right
3634 if (ret != GST_FLOW_NOT_LINKED)
3637 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3638 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3639 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3641 ret = ostream->last_ret;
3642 /* some other return value (must be SUCCESS but we can return
3643 * other values as well) */
3644 if (ret != GST_FLOW_NOT_LINKED)
3647 /* if we get here, all other pads were unlinked and we return
3648 * NOT_LINKED then */
3654 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
3657 gboolean res = TRUE;
3659 /* only streams that have a connection to the outside world */
3660 if (stream->container || stream->disabled)
3663 if (stream->udpsrc[0]) {
3664 gst_event_ref (event);
3665 res = gst_element_send_event (stream->udpsrc[0], event);
3666 } else if (stream->channelpad[0]) {
3667 gst_event_ref (event);
3668 if (GST_PAD_IS_SRC (stream->channelpad[0]))
3669 res = gst_pad_push_event (stream->channelpad[0], event);
3671 res = gst_pad_send_event (stream->channelpad[0], event);
3674 if (stream->udpsrc[1]) {
3675 gst_event_ref (event);
3676 res &= gst_element_send_event (stream->udpsrc[1], event);
3677 } else if (stream->channelpad[1]) {
3678 gst_event_ref (event);
3679 if (GST_PAD_IS_SRC (stream->channelpad[1]))
3680 res &= gst_pad_push_event (stream->channelpad[1], event);
3682 res &= gst_pad_send_event (stream->channelpad[1], event);
3686 gst_event_unref (event);
3692 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
3695 gboolean res = TRUE;
3697 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3698 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3700 gst_event_ref (event);
3701 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
3703 gst_event_unref (event);
3708 static GstRTSPResult
3709 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3714 if (info->connection == NULL) {
3715 if (info->url == NULL) {
3716 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
3717 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
3721 /* create connection */
3722 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
3723 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
3724 goto could_not_create;
3727 g_free (info->url_str);
3728 info->url_str = gst_rtsp_url_get_request_uri (info->url);
3730 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
3732 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
3733 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
3734 src->tls_validation_flags))
3735 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
3738 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
3739 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
3741 if (src->proxy_host) {
3742 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
3744 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
3749 if (!info->connected) {
3752 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
3753 ("Connecting to %s", info->location));
3754 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
3756 gst_rtsp_connection_connect (info->connection,
3757 src->ptcp_timeout)) < 0)
3758 goto could_not_connect;
3760 info->connected = TRUE;
3767 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
3772 gchar *str = gst_rtsp_strresult (res);
3773 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
3779 gchar *str = gst_rtsp_strresult (res);
3780 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
3786 static GstRTSPResult
3787 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
3790 GST_RTSP_STATE_LOCK (src);
3791 if (info->connected) {
3792 GST_DEBUG_OBJECT (src, "closing connection...");
3793 gst_rtsp_connection_close (info->connection);
3794 info->connected = FALSE;
3796 if (free && info->connection) {
3797 /* free connection */
3798 GST_DEBUG_OBJECT (src, "freeing connection...");
3799 gst_rtsp_connection_free (info->connection);
3800 info->connection = NULL;
3802 GST_RTSP_STATE_UNLOCK (src);
3806 static GstRTSPResult
3807 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3812 GST_DEBUG_OBJECT (src, "reconnecting connection...");
3813 gst_rtsp_conninfo_close (src, info, FALSE);
3814 res = gst_rtsp_conninfo_connect (src, info, async);
3820 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
3824 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
3825 GST_RTSP_STATE_LOCK (src);
3826 if (src->conninfo.connection && src->conninfo.flushing != flush) {
3827 GST_DEBUG_OBJECT (src, "connection flush");
3828 gst_rtsp_connection_flush (src->conninfo.connection, flush);
3829 src->conninfo.flushing = flush;
3831 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3832 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3833 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
3834 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
3835 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
3836 stream->conninfo.flushing = flush;
3839 GST_RTSP_STATE_UNLOCK (src);
3842 /* FIXME, handle server request, reply with OK, for now */
3843 static GstRTSPResult
3844 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
3845 GstRTSPMessage * request)
3847 GstRTSPMessage response = { 0 };
3850 GST_DEBUG_OBJECT (src, "got server request message");
3853 gst_rtsp_message_dump (request);
3855 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
3857 if (res == GST_RTSP_ENOTIMPL) {
3858 /* default implementation, send OK */
3859 GST_DEBUG_OBJECT (src, "prepare OK reply");
3861 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
3866 /* let app parse and reply */
3867 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
3868 0, request, &response);
3871 gst_rtsp_message_dump (&response);
3873 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
3877 gst_rtsp_message_unset (&response);
3878 } else if (res == GST_RTSP_EEOF)
3886 gst_rtsp_message_unset (&response);
3891 /* send server keep-alive */
3892 static GstRTSPResult
3893 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
3895 GstRTSPMessage request = { 0 };
3897 GstRTSPMethod method;
3898 const gchar *control;
3900 if (src->do_rtsp_keep_alive == FALSE) {
3901 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
3902 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3906 GST_DEBUG_OBJECT (src, "creating server keep-alive");
3908 /* find a method to use for keep-alive */
3909 if (src->methods & GST_RTSP_GET_PARAMETER)
3910 method = GST_RTSP_GET_PARAMETER;
3912 method = GST_RTSP_OPTIONS;
3914 control = get_aggregate_control (src);
3915 if (control == NULL)
3918 res = gst_rtsp_message_init_request (&request, method, control);
3923 gst_rtsp_message_dump (&request);
3926 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
3931 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3932 gst_rtsp_message_unset (&request);
3939 GST_WARNING_OBJECT (src, "no control url to send keepalive");
3944 gchar *str = gst_rtsp_strresult (res);
3946 gst_rtsp_message_unset (&request);
3947 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
3948 ("Could not send keep-alive. (%s)", str));
3954 static GstFlowReturn
3955 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
3957 GstFlowReturn ret = GST_FLOW_OK;
3959 GstRTSPStream *stream;
3960 GstPad *outpad = NULL;
3967 channel = message->type_data.data.channel;
3969 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
3971 goto unknown_stream;
3973 if (channel == stream->channel[0]) {
3974 outpad = stream->channelpad[0];
3976 } else if (channel == stream->channel[1]) {
3977 outpad = stream->channelpad[1];
3983 /* take a look at the body to figure out what we have */
3984 gst_rtsp_message_get_body (message, &data, &size);
3986 goto invalid_length;
3988 /* channels are not correct on some servers, do extra check */
3989 if (data[1] >= 200 && data[1] <= 204) {
3990 /* hmm RTCP message switch to the RTCP pad of the same stream. */
3991 outpad = stream->channelpad[1];
3995 /* we have no clue what this is, just ignore then. */
3997 goto unknown_stream;
3999 /* take the message body for further processing */
4000 gst_rtsp_message_steal_body (message, &data, &size);
4002 /* strip the trailing \0 */
4005 buf = gst_buffer_new ();
4006 gst_buffer_append_memory (buf,
4007 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4009 /* don't need message anymore */
4010 gst_rtsp_message_unset (message);
4012 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4015 if (src->need_activate) {
4021 guint group_id = gst_util_group_id_next ();
4023 /* generate an SHA256 sum of the URI */
4024 cs = g_checksum_new (G_CHECKSUM_SHA256);
4025 uri = src->conninfo.location;
4026 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4028 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4029 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4032 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4033 event = gst_event_new_stream_start (stream_id);
4034 gst_event_set_group_id (event, group_id);
4037 gst_rtspsrc_stream_push_event (src, ostream, event);
4039 g_checksum_free (cs);
4041 gst_rtspsrc_activate_streams (src);
4042 src->need_activate = FALSE;
4044 if ((event = src->start_segment) != NULL) {
4045 src->start_segment = NULL;
4046 gst_rtspsrc_push_event (src, event);
4049 if (src->base_time == -1) {
4050 /* Take current running_time. This timestamp will be put on
4051 * the first buffer of each stream because we are a live source and so we
4052 * timestamp with the running_time. When we are dealing with TCP, we also
4053 * only timestamp the first buffer (using the DISCONT flag) because a server
4054 * typically bursts data, for which we don't want to compensate by speeding
4055 * up the media. The other timestamps will be interpollated from this one
4056 * using the RTP timestamps. */
4057 GST_OBJECT_LOCK (src);
4058 if (GST_ELEMENT_CLOCK (src)) {
4060 GstClockTime base_time;
4062 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4063 base_time = GST_ELEMENT_CAST (src)->base_time;
4065 src->base_time = now - base_time;
4067 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4068 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4070 GST_OBJECT_UNLOCK (src);
4073 if (stream->discont && !is_rtcp) {
4074 /* mark first RTP buffer as discont */
4075 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4076 stream->discont = FALSE;
4077 /* first buffer gets the timestamp, other buffers are not timestamped and
4078 * their presentation time will be interpollated from the rtp timestamps. */
4079 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4080 GST_TIME_ARGS (src->base_time));
4082 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4085 /* chain to the peer pad */
4086 if (GST_PAD_IS_SINK (outpad))
4087 ret = gst_pad_chain (outpad, buf);
4089 ret = gst_pad_push (outpad, buf);
4092 /* combine all stream flows for the data transport */
4093 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4100 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4101 gst_rtsp_message_unset (message);
4106 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4107 ("Short message received, ignoring."));
4108 gst_rtsp_message_unset (message);
4113 static GstFlowReturn
4114 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4116 GstRTSPMessage message = { 0 };
4118 GstFlowReturn ret = GST_FLOW_OK;
4119 GTimeVal tv_timeout;
4122 /* get the next timeout interval */
4123 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4125 /* see if the timeout period expired */
4126 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4127 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4128 /* send keep-alive, only act on interrupt, a warning will be posted for
4130 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4132 /* get new timeout */
4133 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4136 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4137 tv_timeout.tv_sec, tv_timeout.tv_usec);
4139 /* protect the connection with the connection lock so that we can see when
4140 * we are finished doing server communication */
4142 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4143 &message, src->ptcp_timeout);
4147 GST_DEBUG_OBJECT (src, "we received a server message");
4149 case GST_RTSP_EINTR:
4150 /* we got interrupted this means we need to stop */
4152 case GST_RTSP_ETIMEOUT:
4153 /* no reply, send keep alive */
4154 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4155 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4159 /* go EOS when the server closed the connection */
4165 switch (message.type) {
4166 case GST_RTSP_MESSAGE_REQUEST:
4167 /* server sends us a request message, handle it */
4169 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4171 if (res == GST_RTSP_EEOF)
4174 goto handle_request_failed;
4176 case GST_RTSP_MESSAGE_RESPONSE:
4177 /* we ignore response messages */
4178 GST_DEBUG_OBJECT (src, "ignoring response message");
4180 gst_rtsp_message_dump (&message);
4182 case GST_RTSP_MESSAGE_DATA:
4183 GST_DEBUG_OBJECT (src, "got data message");
4184 ret = gst_rtspsrc_handle_data (src, &message);
4185 if (ret != GST_FLOW_OK)
4186 goto handle_data_failed;
4189 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4194 g_assert_not_reached ();
4199 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4200 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4201 ("The server closed the connection."));
4202 src->conninfo.connected = FALSE;
4203 gst_rtsp_message_unset (&message);
4204 return GST_FLOW_EOS;
4208 gst_rtsp_message_unset (&message);
4209 GST_DEBUG_OBJECT (src, "got interrupted");
4210 return GST_FLOW_FLUSHING;
4214 gchar *str = gst_rtsp_strresult (res);
4216 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4217 ("Could not receive message. (%s)", str));
4220 gst_rtsp_message_unset (&message);
4221 return GST_FLOW_ERROR;
4223 handle_request_failed:
4225 gchar *str = gst_rtsp_strresult (res);
4227 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4228 ("Could not handle server message. (%s)", str));
4230 gst_rtsp_message_unset (&message);
4231 return GST_FLOW_ERROR;
4235 GST_DEBUG_OBJECT (src, "could no handle data message");
4240 static GstFlowReturn
4241 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4244 GstRTSPMessage message = { 0 };
4248 GTimeVal tv_timeout;
4250 /* get the next timeout interval */
4251 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4253 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4254 (gint) tv_timeout.tv_sec);
4256 gst_rtsp_message_unset (&message);
4258 /* we should continue reading the TCP socket because the server might
4259 * send us requests. When the session timeout expires, we need to send a
4260 * keep-alive request to keep the session open. */
4261 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4262 &message, &tv_timeout);
4266 GST_DEBUG_OBJECT (src, "we received a server message");
4268 case GST_RTSP_EINTR:
4269 /* we got interrupted, see what we have to do */
4271 case GST_RTSP_ETIMEOUT:
4272 /* send keep-alive, ignore the result, a warning will be posted. */
4273 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4274 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4278 /* server closed the connection. not very fatal for UDP, reconnect and
4279 * see what happens. */
4280 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4281 ("The server closed the connection."));
4282 if (src->udp_reconnect) {
4284 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4291 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4293 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4294 ("Unhandled return value %d.", res));
4298 switch (message.type) {
4299 case GST_RTSP_MESSAGE_REQUEST:
4300 /* server sends us a request message, handle it */
4302 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4304 if (res == GST_RTSP_EEOF)
4307 goto handle_request_failed;
4309 case GST_RTSP_MESSAGE_RESPONSE:
4310 /* we ignore response and data messages */
4311 GST_DEBUG_OBJECT (src, "ignoring response message");
4313 gst_rtsp_message_dump (&message);
4314 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4315 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4316 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4317 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4318 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4325 case GST_RTSP_MESSAGE_DATA:
4326 /* we ignore response and data messages */
4327 GST_DEBUG_OBJECT (src, "ignoring data message");
4330 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4335 g_assert_not_reached ();
4337 /* we get here when the connection got interrupted */
4340 gst_rtsp_message_unset (&message);
4341 GST_DEBUG_OBJECT (src, "got interrupted");
4342 return GST_FLOW_FLUSHING;
4346 gchar *str = gst_rtsp_strresult (res);
4349 src->conninfo.connected = FALSE;
4350 if (res != GST_RTSP_EINTR) {
4351 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4352 ("Could not connect to server. (%s)", str));
4354 ret = GST_FLOW_ERROR;
4356 ret = GST_FLOW_FLUSHING;
4362 gchar *str = gst_rtsp_strresult (res);
4364 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4365 ("Could not receive message. (%s)", str));
4367 return GST_FLOW_ERROR;
4369 handle_request_failed:
4371 gchar *str = gst_rtsp_strresult (res);
4374 gst_rtsp_message_unset (&message);
4375 if (res != GST_RTSP_EINTR) {
4376 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4377 ("Could not handle server message. (%s)", str));
4379 ret = GST_FLOW_ERROR;
4381 ret = GST_FLOW_FLUSHING;
4387 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4388 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4389 ("The server closed the connection."));
4390 src->conninfo.connected = FALSE;
4391 gst_rtsp_message_unset (&message);
4392 return GST_FLOW_EOS;
4396 static GstRTSPResult
4397 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4399 GstRTSPResult res = GST_RTSP_OK;
4402 GST_DEBUG_OBJECT (src, "doing reconnect");
4404 GST_OBJECT_LOCK (src);
4405 /* only restart when the pads were not yet activated, else we were
4406 * streaming over UDP */
4407 restart = src->need_activate;
4408 GST_OBJECT_UNLOCK (src);
4410 /* no need to restart, we're done */
4414 /* we can try only TCP now */
4415 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
4417 /* close and cleanup our state */
4418 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
4421 /* see if we have TCP left to try. Also don't try TCP when we were configured
4423 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
4426 /* We post a warning message now to inform the user
4427 * that nothing happened. It's most likely a firewall thing. */
4428 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4429 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4430 "firewall is blocking it. Retrying using a TCP connection.",
4431 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4433 /* open new connection using tcp */
4434 if (gst_rtspsrc_open (src, async) < 0)
4437 /* start playback */
4438 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
4447 src->cur_protocols = 0;
4448 /* no transport possible, post an error and stop */
4449 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4450 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4451 "firewall is blocking it. No other protocols to try.",
4452 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4453 return GST_RTSP_ERROR;
4457 GST_DEBUG_OBJECT (src, "open failed");
4462 GST_DEBUG_OBJECT (src, "play failed");
4468 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
4472 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
4475 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
4478 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
4481 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
4489 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
4493 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
4496 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
4499 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
4502 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4510 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4514 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4517 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4520 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4523 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4531 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4535 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
4538 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4541 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4544 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4552 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4554 if (ret == GST_RTSP_OK)
4555 gst_rtspsrc_loop_complete_cmd (src, cmd);
4556 else if (ret == GST_RTSP_EINTR)
4557 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4559 gst_rtspsrc_loop_error_cmd (src, cmd);
4563 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
4566 gboolean flushed = FALSE;
4568 /* start new request */
4569 gst_rtspsrc_loop_start_cmd (src, cmd);
4571 GST_DEBUG_OBJECT (src, "sending cmd %d", cmd);
4573 GST_OBJECT_LOCK (src);
4574 old = src->pending_cmd;
4575 if (old == CMD_RECONNECT) {
4576 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
4577 cmd = CMD_RECONNECT;
4579 if (old != CMD_WAIT) {
4580 src->pending_cmd = CMD_WAIT;
4581 GST_OBJECT_UNLOCK (src);
4582 /* cancel previous request */
4583 GST_DEBUG_OBJECT (src, "cancel previous request %d", old);
4584 gst_rtspsrc_loop_cancel_cmd (src, old);
4585 GST_OBJECT_LOCK (src);
4587 src->pending_cmd = cmd;
4588 /* interrupt if allowed */
4589 if (src->busy_cmd & mask) {
4590 GST_DEBUG_OBJECT (src, "connection flush busy %d", src->busy_cmd);
4591 gst_rtspsrc_connection_flush (src, TRUE);
4594 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %d", src->busy_cmd);
4597 gst_task_start (src->task);
4598 GST_OBJECT_UNLOCK (src);
4604 gst_rtspsrc_loop (GstRTSPSrc * src)
4608 if (!src->conninfo.connection || !src->conninfo.connected)
4611 if (src->interleaved)
4612 ret = gst_rtspsrc_loop_interleaved (src);
4614 ret = gst_rtspsrc_loop_udp (src);
4616 if (ret != GST_FLOW_OK)
4624 GST_WARNING_OBJECT (src, "we are not connected");
4625 ret = GST_FLOW_FLUSHING;
4630 const gchar *reason = gst_flow_get_name (ret);
4632 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
4633 src->running = FALSE;
4634 if (ret == GST_FLOW_EOS) {
4635 /* perform EOS logic */
4636 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4637 gst_element_post_message (GST_ELEMENT_CAST (src),
4638 gst_message_new_segment_done (GST_OBJECT_CAST (src),
4639 src->segment.format, src->segment.position));
4640 gst_rtspsrc_push_event (src,
4641 gst_event_new_segment_done (src->segment.format,
4642 src->segment.position));
4644 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4646 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
4647 /* for fatal errors we post an error message, post the error before the
4648 * EOS so the app knows about the error first. */
4649 GST_ELEMENT_ERROR (src, STREAM, FAILED,
4650 ("Internal data flow error."),
4651 ("streaming task paused, reason %s (%d)", reason, ret));
4652 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4654 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
4659 #ifndef GST_DISABLE_GST_DEBUG
4660 static const gchar *
4661 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
4665 while (method != 0) {
4682 static const gchar *
4683 gst_rtspsrc_skip_lws (const gchar * s)
4685 while (g_ascii_isspace (*s))
4690 static const gchar *
4691 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
4693 while (s > start && g_ascii_isspace (*(s - 1)))
4698 static const gchar *
4699 gst_rtspsrc_skip_commas (const gchar * s)
4701 /* The grammar allows for multiple commas */
4702 while (g_ascii_isspace (*s) || *s == ',')
4707 static const gchar *
4708 gst_rtspsrc_skip_item (const gchar * s)
4710 gboolean quoted = FALSE;
4711 const gchar *start = s;
4713 /* A list item ends at the last non-whitespace character
4714 * before a comma which is not inside a quoted-string. Or at
4715 * the end of the string.
4721 if (*s == '\\' && *(s + 1))
4730 return gst_rtspsrc_unskip_lws (s, start);
4734 gst_rtsp_decode_quoted_string (gchar * quoted_string)
4738 src = quoted_string + 1;
4739 dst = quoted_string;
4740 while (*src && *src != '"') {
4741 if (*src == '\\' && *(src + 1))
4748 /* Extract the authentication tokens that the server provided for each method
4749 * into an array of structures and give those to the connection object.
4752 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
4753 const gchar * header, gboolean * stale)
4755 GSList *list = NULL, *iter;
4757 gchar *item, *eq, *name_end, *value;
4759 g_return_if_fail (stale != NULL);
4761 gst_rtsp_connection_clear_auth_params (conn);
4764 /* Parse a header whose content is described by RFC2616 as
4765 * "#something", where "something" does not itself contain commas,
4766 * except as part of quoted-strings, into a list of allocated strings.
4768 header = gst_rtspsrc_skip_commas (header);
4770 end = gst_rtspsrc_skip_item (header);
4771 list = g_slist_prepend (list, g_strndup (header, end - header));
4772 header = gst_rtspsrc_skip_commas (end);
4777 list = g_slist_reverse (list);
4778 for (iter = list; iter; iter = iter->next) {
4781 eq = strchr (item, '=');
4783 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
4784 if (name_end == item) {
4785 /* That's no good... */
4792 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
4794 gst_rtsp_decode_quoted_string (value);
4798 if (item && (strcmp (item, "stale") == 0) &&
4799 value && (strcmp (value, "TRUE") == 0))
4801 gst_rtsp_connection_set_auth_param (conn, item, value);
4805 g_slist_free (list);
4808 /* Parse a WWW-Authenticate Response header and determine the
4809 * available authentication methods
4811 * This code should also cope with the fact that each WWW-Authenticate
4812 * header can contain multiple challenge methods + tokens
4814 * At the moment, for Basic auth, we just do a minimal check and don't
4815 * even parse out the realm */
4817 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
4818 GstRTSPConnection * conn, gboolean * stale)
4822 g_return_if_fail (hdr != NULL);
4823 g_return_if_fail (methods != NULL);
4824 g_return_if_fail (stale != NULL);
4826 /* Skip whitespace at the start of the string */
4827 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
4829 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
4830 *methods |= GST_RTSP_AUTH_BASIC;
4831 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
4832 *methods |= GST_RTSP_AUTH_DIGEST;
4833 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
4838 * gst_rtspsrc_setup_auth:
4839 * @src: the rtsp source
4841 * Configure a username and password and auth method on the
4842 * connection object based on a response we received from the
4845 * Currently, this requires that a username and password were supplied
4846 * in the uri. In the future, they may be requested on demand by sending
4847 * a message up the bus.
4849 * Returns: TRUE if authentication information could be set up correctly.
4852 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
4856 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
4857 GstRTSPAuthMethod method;
4858 GstRTSPResult auth_result;
4860 GstRTSPConnection *conn;
4862 gboolean stale = FALSE;
4864 conn = src->conninfo.connection;
4866 /* Identify the available auth methods and see if any are supported */
4867 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
4868 &hdr, 0) == GST_RTSP_OK) {
4869 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
4872 if (avail_methods == GST_RTSP_AUTH_NONE)
4873 goto no_auth_available;
4875 /* For digest auth, if the response indicates that the session
4876 * data are stale, we just update them in the connection object and
4877 * return TRUE to retry the request */
4879 src->tried_url_auth = FALSE;
4881 url = gst_rtsp_connection_get_url (conn);
4883 /* Do we have username and password available? */
4884 if (url != NULL && !src->tried_url_auth && url->user != NULL
4885 && url->passwd != NULL) {
4888 src->tried_url_auth = TRUE;
4889 GST_DEBUG_OBJECT (src,
4890 "Attempting authentication using credentials from the URL");
4892 user = src->user_id;
4893 pass = src->user_pw;
4894 GST_DEBUG_OBJECT (src,
4895 "Attempting authentication using credentials from the properties");
4898 /* FIXME: If the url didn't contain username and password or we tried them
4899 * already, request a username and passwd from the application via some kind
4900 * of credentials request message */
4902 /* If we don't have a username and passwd at this point, bail out. */
4903 if (user == NULL || pass == NULL)
4906 /* Try to configure for each available authentication method, strongest to
4908 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
4909 /* Check if this method is available on the server */
4910 if ((method & avail_methods) == 0)
4913 /* Pass the credentials to the connection to try on the next request */
4914 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
4915 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
4916 * ignore it and end up retrying later */
4917 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
4918 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
4919 gst_rtsp_auth_method_to_string (method));
4924 if (method == GST_RTSP_AUTH_NONE)
4925 goto no_auth_available;
4931 /* Output an error indicating that we couldn't connect because there were
4932 * no supported authentication protocols */
4933 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4934 ("No supported authentication protocol was found"));
4939 /* We don't fire an error message, we just return FALSE and let the
4940 * normal NOT_AUTHORIZED error be propagated */
4945 static GstRTSPResult
4946 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4947 GstRTSPMessage * request, GstRTSPMessage * response,
4948 GstRTSPStatusCode * code)
4951 GstRTSPStatusCode thecode;
4952 gchar *content_base = NULL;
4956 if (!src->short_header)
4957 gst_rtsp_ext_list_before_send (src->extensions, request);
4959 GST_DEBUG_OBJECT (src, "sending message");
4962 gst_rtsp_message_dump (request);
4964 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
4968 gst_rtsp_connection_reset_timeout (conn);
4971 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
4976 gst_rtsp_message_dump (response);
4978 switch (response->type) {
4979 case GST_RTSP_MESSAGE_REQUEST:
4980 res = gst_rtspsrc_handle_request (src, conn, response);
4981 if (res == GST_RTSP_EEOF)
4984 goto handle_request_failed;
4986 case GST_RTSP_MESSAGE_RESPONSE:
4987 /* ok, a response is good */
4988 GST_DEBUG_OBJECT (src, "received response message");
4990 case GST_RTSP_MESSAGE_DATA:
4991 /* get next response */
4992 GST_DEBUG_OBJECT (src, "handle data response message");
4993 gst_rtspsrc_handle_data (src, response);
4996 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5001 thecode = response->type_data.response.code;
5003 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5005 /* if the caller wanted the result code, we store it. */
5009 /* If the request didn't succeed, bail out before doing any more */
5010 if (thecode != GST_RTSP_STS_OK)
5013 /* store new content base if any */
5014 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5017 g_free (src->content_base);
5018 src->content_base = g_strdup (content_base);
5020 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5027 gchar *str = gst_rtsp_strresult (res);
5029 if (res != GST_RTSP_EINTR) {
5030 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5031 ("Could not send message. (%s)", str));
5033 GST_WARNING_OBJECT (src, "send interrupted");
5042 GST_WARNING_OBJECT (src, "server closed connection");
5043 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5045 /* if reconnect succeeds, try again */
5047 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5051 /* only try once after reconnect, then fallthrough and error out */
5054 gchar *str = gst_rtsp_strresult (res);
5056 if (res != GST_RTSP_EINTR) {
5057 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5058 ("Could not receive message. (%s)", str));
5060 GST_WARNING_OBJECT (src, "receive interrupted");
5068 handle_request_failed:
5070 /* ERROR was posted */
5071 gst_rtsp_message_unset (response);
5076 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5077 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5078 ("The server closed the connection."));
5079 gst_rtsp_message_unset (response);
5086 * @src: the rtsp source
5087 * @conn: the connection to send on
5088 * @request: must point to a valid request
5089 * @response: must point to an empty #GstRTSPMessage
5090 * @code: an optional code result
5092 * send @request and retrieve the response in @response. optionally @code can be
5093 * non-NULL in which case it will contain the status code of the response.
5095 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5096 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5098 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5099 * @response message) if the response code was not 200 (OK).
5101 * If the attempt results in an authentication failure, then this will attempt
5102 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5105 * Returns: #GST_RTSP_OK if the processing was successful.
5107 static GstRTSPResult
5108 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5109 GstRTSPMessage * request, GstRTSPMessage * response,
5110 GstRTSPStatusCode * code)
5112 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5113 GstRTSPResult res = GST_RTSP_ERROR;
5116 GstRTSPMethod method = GST_RTSP_INVALID;
5122 /* make sure we don't loop forever */
5126 /* save method so we can disable it when the server complains */
5127 method = request->type_data.request.method;
5130 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5134 case GST_RTSP_STS_UNAUTHORIZED:
5135 if (gst_rtspsrc_setup_auth (src, response)) {
5136 /* Try the request/response again after configuring the auth info
5144 } while (retry == TRUE);
5146 /* If the user requested the code, let them handle errors, otherwise
5147 * post an error below */
5150 else if (int_code != GST_RTSP_STS_OK)
5151 goto error_response;
5158 GST_DEBUG_OBJECT (src, "got error %d", res);
5163 res = GST_RTSP_ERROR;
5165 switch (response->type_data.response.code) {
5166 case GST_RTSP_STS_NOT_FOUND:
5167 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5168 response->type_data.response.reason));
5170 case GST_RTSP_STS_MOVED_PERMANENTLY:
5171 case GST_RTSP_STS_MOVE_TEMPORARILY:
5173 gchar *new_location;
5174 GstRTSPLowerTrans transports;
5176 GST_DEBUG_OBJECT (src, "got redirection");
5177 /* if we don't have a Location Header, we must error */
5178 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5179 &new_location, 0) < 0)
5182 /* When we receive a redirect result, we go back to the INIT state after
5183 * parsing the new URI. The caller should do the needed steps to issue
5184 * a new setup when it detects this state change. */
5185 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5187 /* save current transports */
5188 if (src->conninfo.url)
5189 transports = src->conninfo.url->transports;
5191 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5193 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5195 /* set old transports */
5196 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5197 src->conninfo.url->transports = transports;
5199 src->need_redirect = TRUE;
5200 src->state = GST_RTSP_STATE_INIT;
5204 case GST_RTSP_STS_NOT_ACCEPTABLE:
5205 case GST_RTSP_STS_NOT_IMPLEMENTED:
5206 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5207 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5208 gst_rtsp_method_as_text (method));
5209 src->methods &= ~method;
5213 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5214 ("Got error response: %d (%s).", response->type_data.response.code,
5215 response->type_data.response.reason));
5218 /* if we return ERROR we should unset the response ourselves */
5219 if (res == GST_RTSP_ERROR)
5220 gst_rtsp_message_unset (response);
5226 static GstRTSPResult
5227 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5228 GstRTSPMessage * response, GstRTSPSrc * src)
5230 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5235 /* parse the response and collect all the supported methods. We need this
5236 * information so that we don't try to send an unsupported request to the
5240 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5242 GstRTSPHeaderField field;
5246 /* reset supported methods */
5249 /* Try Allow Header first */
5250 field = GST_RTSP_HDR_ALLOW;
5253 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5254 if (indx == 0 && !respoptions) {
5255 /* if no Allow header was found then try the Public header... */
5256 field = GST_RTSP_HDR_PUBLIC;
5257 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5262 src->methods |= gst_rtsp_options_from_text (respoptions);
5267 if (src->methods == 0) {
5268 /* neither Allow nor Public are required, assume the server supports
5269 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5271 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5272 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5274 /* always assume PLAY, FIXME, extensions should be able to override
5276 src->methods |= GST_RTSP_PLAY;
5277 /* also assume it will support Range */
5278 src->seekable = TRUE;
5280 /* we need describe and setup */
5281 if (!(src->methods & GST_RTSP_DESCRIBE))
5283 if (!(src->methods & GST_RTSP_SETUP))
5291 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5292 ("Server does not support DESCRIBE."));
5297 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5298 ("Server does not support SETUP."));
5303 /* masks to be kept in sync with the hardcoded protocol order of preference
5305 static guint protocol_masks[] = {
5306 GST_RTSP_LOWER_TRANS_UDP,
5307 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5308 GST_RTSP_LOWER_TRANS_TCP,
5312 static GstRTSPResult
5313 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5314 GstRTSPLowerTrans protocols, gchar ** transports)
5318 gboolean add_udp_str;
5323 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5328 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5330 /* extension listed transports, use those */
5331 if (*transports != NULL)
5334 /* it's the default */
5335 add_udp_str = FALSE;
5337 /* the default RTSP transports */
5338 result = g_string_new ("");
5339 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5340 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5342 g_string_append (result, "RTP/AVP");
5344 g_string_append (result, "/UDP");
5345 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5346 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5347 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5349 /* we don't have to allocate any UDP ports yet, if the selected transport
5350 * turns out to be multicast we can create them and join the multicast
5351 * group indicated in the transport reply */
5352 if (result->len > 0)
5353 g_string_append (result, ",");
5354 g_string_append (result, "RTP/AVP");
5356 g_string_append (result, "/UDP");
5357 g_string_append (result, ";multicast");
5358 if (src->next_port_num != 0) {
5359 if (src->client_port_range.max > 0 &&
5360 src->next_port_num >= src->client_port_range.max)
5363 g_string_append_printf (result, ";client_port=%d-%d",
5364 src->next_port_num, src->next_port_num + 1);
5366 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
5367 GST_DEBUG_OBJECT (src, "adding TCP");
5369 if (result->len > 0)
5370 g_string_append (result, ",");
5371 g_string_append (result, "RTP/AVP/TCP;unicast;interleaved=%%i1-%%i2");
5373 *transports = g_string_free (result, FALSE);
5375 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5382 GST_ERROR ("extension gave error %d", res);
5387 GST_ERROR ("no more ports available");
5388 return GST_RTSP_ERROR;
5392 static GstRTSPResult
5393 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5394 gint orig_rtpport, gint orig_rtcpport)
5397 gint nr_udp, nr_int;
5399 gint rtpport = 0, rtcpport = 0;
5402 src = stream->parent;
5404 /* find number of placeholders first */
5405 if (strstr (*transports, "%%i2"))
5407 else if (strstr (*transports, "%%i1"))
5412 if (strstr (*transports, "%%u2"))
5414 else if (strstr (*transports, "%%u1"))
5419 if (nr_udp == 0 && nr_int == 0)
5423 if (!orig_rtpport || !orig_rtcpport) {
5424 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5427 rtpport = orig_rtpport;
5428 rtcpport = orig_rtcpport;
5432 str = g_string_new ("");
5434 while ((next = strstr (p, "%%"))) {
5435 g_string_append_len (str, p, next - p);
5436 if (next[2] == 'u') {
5438 g_string_append_printf (str, "%d", rtpport);
5439 else if (next[3] == '2')
5440 g_string_append_printf (str, "%d", rtcpport);
5442 if (next[2] == 'i') {
5444 g_string_append_printf (str, "%d", src->free_channel);
5445 else if (next[3] == '2')
5446 g_string_append_printf (str, "%d", src->free_channel + 1);
5451 /* append final part */
5452 g_string_append (str, p);
5454 g_free (*transports);
5455 *transports = g_string_free (str, FALSE);
5463 GST_ERROR ("failed to allocate udp ports");
5464 return GST_RTSP_ERROR;
5469 gst_rtspsrc_stream_is_real_media (GstRTSPStream * stream)
5471 gboolean res = FALSE;
5475 const gchar *enc = NULL;
5477 s = gst_caps_get_structure (stream->caps, 0);
5478 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
5479 res = (strstr (enc, "-REAL") != NULL);
5485 /* Perform the SETUP request for all the streams.
5487 * We ask the server for a specific transport, which initially includes all the
5488 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
5489 * two local UDP ports that we send to the server.
5491 * Once the server replied with a transport, we configure the other streams
5492 * with the same transport.
5494 * This function will also configure the stream for the selected transport,
5495 * which basically means creating the pipeline.
5497 static GstRTSPResult
5498 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
5501 GstRTSPResult res = GST_RTSP_ERROR;
5502 GstRTSPMessage request = { 0 };
5503 GstRTSPMessage response = { 0 };
5504 GstRTSPStream *stream = NULL;
5505 GstRTSPLowerTrans protocols;
5506 GstRTSPStatusCode code;
5507 gboolean unsupported_real = FALSE;
5508 gint rtpport, rtcpport;
5512 if (src->conninfo.connection) {
5513 url = gst_rtsp_connection_get_url (src->conninfo.connection);
5514 /* we initially allow all configured lower transports. based on the URL
5515 * transports and the replies from the server we narrow them down. */
5516 protocols = url->transports & src->cur_protocols;
5519 protocols = src->cur_protocols;
5525 /* reset some state */
5526 src->free_channel = 0;
5527 src->interleaved = FALSE;
5528 src->need_activate = FALSE;
5529 /* keep track of next port number, 0 is random */
5530 src->next_port_num = src->client_port_range.min;
5531 rtpport = rtcpport = 0;
5533 if (G_UNLIKELY (src->streams == NULL))
5536 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5537 GstRTSPConnection *conn;
5543 stream = (GstRTSPStream *) walk->data;
5545 /* see if we need to configure this stream */
5546 if (!gst_rtsp_ext_list_configure_stream (src->extensions, stream->caps)) {
5547 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
5549 stream->disabled = TRUE;
5553 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
5554 stream->id, stream->caps, &selected);
5556 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
5557 stream->disabled = TRUE;
5560 stream->disabled = FALSE;
5562 /* merge/overwrite global caps */
5567 s = gst_caps_get_structure (stream->caps, 0);
5569 num = gst_structure_n_fields (src->props);
5570 for (j = 0; j < num; j++) {
5574 name = gst_structure_nth_field_name (src->props, j);
5575 val = gst_structure_get_value (src->props, name);
5576 gst_structure_set_value (s, name, val);
5578 GST_DEBUG_OBJECT (src, "copied %s", name);
5582 /* skip setup if we have no URL for it */
5583 if (stream->conninfo.location == NULL) {
5584 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
5588 if (src->conninfo.connection == NULL) {
5589 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
5590 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
5593 conn = stream->conninfo.connection;
5595 conn = src->conninfo.connection;
5597 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
5598 stream->conninfo.location);
5600 /* if we have a multicast connection, only suggest multicast from now on */
5601 if (stream->is_multicast)
5602 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
5605 /* first selectable protocol */
5606 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5608 if (!protocol_masks[mask])
5612 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
5613 protocol_masks[mask]);
5614 /* create a string with first transport in line */
5616 res = gst_rtspsrc_create_transports_string (src,
5617 protocols & protocol_masks[mask], &transports);
5618 if (res < 0 || transports == NULL)
5619 goto setup_transport_failed;
5621 if (strlen (transports) == 0) {
5622 g_free (transports);
5623 GST_DEBUG_OBJECT (src, "no transports found");
5628 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
5630 /* replace placeholders with real values, this function will optionally
5631 * allocate UDP ports and other info needed to execute the setup request */
5632 res = gst_rtspsrc_prepare_transports (stream, &transports,
5633 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
5635 g_free (transports);
5636 goto setup_transport_failed;
5639 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
5641 /* create SETUP request */
5643 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
5644 stream->conninfo.location);
5646 g_free (transports);
5647 goto create_request_failed;
5650 /* select transport */
5651 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
5653 /* if the user wants a non default RTP packet size we add the blocksize
5655 if (src->rtp_blocksize > 0) {
5656 hval = g_strdup_printf ("%d", src->rtp_blocksize);
5657 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
5661 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
5664 /* handle the code ourselves */
5665 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
5669 case GST_RTSP_STS_OK:
5671 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
5672 gst_rtsp_message_unset (&request);
5673 gst_rtsp_message_unset (&response);
5674 /* cleanup of leftover transport */
5675 gst_rtspsrc_stream_free_udp (stream);
5676 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
5677 * we might be in this case */
5678 if (stream->container && rtpport && rtcpport && !retry) {
5679 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
5684 /* this transport did not go down well, but we may have others to try
5685 * that we did not send yet, try those and only give up then
5686 * but not without checking for lost cause/extension so we can
5687 * post a nicer/more useful error message later */
5688 if (!unsupported_real)
5689 unsupported_real = gst_rtspsrc_stream_is_real_media (stream);
5690 /* select next available protocol, give up on this stream if none */
5692 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5694 if (!protocol_masks[mask] || unsupported_real)
5699 /* cleanup of leftover transport and move to the next stream */
5700 gst_rtspsrc_stream_free_udp (stream);
5701 goto response_error;
5704 /* parse response transport */
5706 gchar *resptrans = NULL;
5707 GstRTSPTransport transport = { 0 };
5709 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
5712 gst_rtspsrc_stream_free_udp (stream);
5716 /* parse transport, go to next stream on parse error */
5717 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
5718 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
5722 /* update allowed transports for other streams. once the transport of
5723 * one stream has been determined, we make sure that all other streams
5724 * are configured in the same way */
5725 switch (transport.lower_transport) {
5726 case GST_RTSP_LOWER_TRANS_TCP:
5727 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
5728 protocols = GST_RTSP_LOWER_TRANS_TCP;
5729 src->interleaved = TRUE;
5730 /* update free channels */
5732 MAX (transport.interleaved.min, src->free_channel);
5734 MAX (transport.interleaved.max, src->free_channel);
5735 src->free_channel++;
5737 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
5738 /* only allow multicast for other streams */
5739 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
5740 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
5741 /* if the server selected our ports, increment our counters so that
5742 * we select a new port later */
5743 if (src->next_port_num == transport.port.min &&
5744 src->next_port_num + 1 == transport.port.max) {
5745 src->next_port_num += 2;
5748 case GST_RTSP_LOWER_TRANS_UDP:
5749 /* only allow unicast for other streams */
5750 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
5751 protocols = GST_RTSP_LOWER_TRANS_UDP;
5754 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
5755 transport.lower_transport);
5759 if (!stream->container || (!src->interleaved && !retry)) {
5760 /* now configure the stream with the selected transport */
5761 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
5762 GST_DEBUG_OBJECT (src,
5763 "could not configure stream %p transport, skipping stream",
5766 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
5767 /* retain the first allocated UDP port pair */
5768 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
5769 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
5772 /* we need to activate at least one streams when we detect activity */
5773 src->need_activate = TRUE;
5775 /* clean up our transport struct */
5776 gst_rtsp_transport_init (&transport);
5777 /* clean up used RTSP messages */
5778 gst_rtsp_message_unset (&request);
5779 gst_rtsp_message_unset (&response);
5783 /* store the transport protocol that was configured */
5784 src->cur_protocols = protocols;
5786 gst_rtsp_ext_list_stream_select (src->extensions, url);
5788 /* if there is nothing to activate, error out */
5789 if (!src->need_activate)
5790 goto nothing_to_activate;
5797 /* no transport possible, post an error and stop */
5798 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5799 ("Could not connect to server, no protocols left"));
5800 return GST_RTSP_ERROR;
5804 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5805 ("SDP contains no streams"));
5806 return GST_RTSP_ERROR;
5808 create_request_failed:
5810 gchar *str = gst_rtsp_strresult (res);
5812 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5813 ("Could not create request. (%s)", str));
5817 setup_transport_failed:
5819 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5820 ("Could not setup transport."));
5821 res = GST_RTSP_ERROR;
5826 const gchar *str = gst_rtsp_status_as_text (code);
5828 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5829 ("Error (%d): %s", code, GST_STR_NULL (str)));
5830 res = GST_RTSP_ERROR;
5835 gchar *str = gst_rtsp_strresult (res);
5837 if (res != GST_RTSP_EINTR) {
5838 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5839 ("Could not send message. (%s)", str));
5841 GST_WARNING_OBJECT (src, "send interrupted");
5848 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5849 ("Server did not select transport."));
5850 res = GST_RTSP_ERROR;
5853 nothing_to_activate:
5855 /* none of the available error codes is really right .. */
5856 if (unsupported_real) {
5857 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5858 (_("No supported stream was found. You might need to install a "
5859 "GStreamer RTSP extension plugin for Real media streams.")),
5862 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5863 (_("No supported stream was found. You might need to allow "
5864 "more transport protocols or may otherwise be missing "
5865 "the right GStreamer RTSP extension plugin.")), (NULL));
5867 return GST_RTSP_ERROR;
5871 gst_rtsp_message_unset (&request);
5872 gst_rtsp_message_unset (&response);
5878 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
5879 GstSegment * segment)
5882 GstRTSPTimeRange *therange;
5885 gst_rtsp_range_free (src->range);
5887 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
5888 GST_DEBUG_OBJECT (src, "parsed range %s", range);
5889 src->range = therange;
5891 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
5893 gst_segment_init (segment, GST_FORMAT_TIME);
5897 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
5898 therange->min.type, therange->min.seconds, therange->max.type,
5899 therange->max.seconds);
5901 if (therange->min.type == GST_RTSP_TIME_NOW)
5903 else if (therange->min.type == GST_RTSP_TIME_END)
5906 seconds = therange->min.seconds * GST_SECOND;
5908 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
5909 GST_TIME_ARGS (seconds));
5911 /* we need to start playback without clipping from the position reported by
5913 segment->start = seconds;
5914 segment->position = seconds;
5916 if (therange->max.type == GST_RTSP_TIME_NOW)
5918 else if (therange->max.type == GST_RTSP_TIME_END)
5921 seconds = therange->max.seconds * GST_SECOND;
5923 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
5924 GST_TIME_ARGS (seconds));
5926 /* live (WMS) server might send overflowed large max as its idea of infinity,
5927 * compensate to prevent problems later on */
5928 if (seconds != -1 && seconds < 0) {
5930 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
5933 /* live (WMS) might send min == max, which is not worth recording */
5934 if (segment->duration == -1 && seconds == segment->start)
5937 /* don't change duration with unknown value, we might have a valid value
5938 * there that we want to keep. */
5940 segment->duration = seconds;
5945 /* Parse clock profived by the server with following syntax:
5947 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
5950 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
5952 gboolean res = FALSE;
5954 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
5955 gchar **fields = NULL, **parts = NULL;
5956 gchar *remote_ip, *str;
5958 GstClockTime base_time;
5961 fields = g_strsplit (gstclock, " ", 0);
5963 /* wrapped clock, not very interesting for now */
5964 if (fields[1] == NULL)
5967 /* remote IP address and port */
5968 if ((str = fields[2]) == NULL)
5971 parts = g_strsplit (str, ":", 0);
5973 if ((remote_ip = parts[0]) == NULL)
5976 if ((str = parts[1]) == NULL)
5984 if ((str = fields[3]) == NULL)
5987 base_time = g_ascii_strtoull (str, NULL, 10);
5990 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
5993 if (src->provided_clock)
5994 gst_object_unref (src->provided_clock);
5995 src->provided_clock = netclock;
5997 gst_element_post_message (GST_ELEMENT_CAST (src),
5998 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
5999 src->provided_clock, TRUE));
6003 g_strfreev (fields);
6009 /* must be called with the RTSP state lock */
6010 static GstRTSPResult
6011 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
6017 /* prepare global stream caps properties */
6019 gst_structure_remove_all_fields (src->props);
6021 src->props = gst_structure_new_empty ("RTSPProperties");
6024 gst_sdp_message_dump (sdp);
6026 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
6028 /* let the app inspect and change the SDP */
6029 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6031 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6033 /* parse range for duration reporting. */
6038 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6042 /* keep track of the range and configure it in the segment */
6043 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6047 /* parse clock information. This is GStreamer specific, a server can tell the
6048 * client what clock it is using and wrap that in a network clock. The
6049 * advantage of that is that we can slave to it. */
6051 const gchar *gstclock;
6054 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6055 if (gstclock == NULL)
6058 /* parse the clock and expose it in the provide_clock method */
6059 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6063 /* try to find a global control attribute. Note that a '*' means that we should
6064 * do aggregate control with the current url (so we don't do anything and
6065 * leave the current connection as is) */
6067 const gchar *control;
6070 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6071 if (control == NULL)
6074 /* only take fully qualified urls */
6075 if (g_str_has_prefix (control, "rtsp://"))
6079 g_free (src->conninfo.location);
6080 src->conninfo.location = g_strdup (control);
6081 /* make a connection for this, if there was a connection already, nothing
6083 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6084 GST_ERROR_OBJECT (src, "could not connect");
6087 /* we need to keep the control url separate from the connection url because
6088 * the rules for constructing the media control url need it */
6089 g_free (src->control);
6090 src->control = g_strdup (control);
6093 /* create streams */
6094 n_streams = gst_sdp_message_medias_len (sdp);
6095 for (i = 0; i < n_streams; i++) {
6096 gst_rtspsrc_create_stream (src, sdp, i);
6099 src->state = GST_RTSP_STATE_INIT;
6102 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6105 /* reset our state */
6106 src->need_range = TRUE;
6109 src->state = GST_RTSP_STATE_READY;
6116 GST_ERROR_OBJECT (src, "setup failed");
6117 gst_rtspsrc_cleanup (src);
6122 static GstRTSPResult
6123 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6127 GstRTSPMessage request = { 0 };
6128 GstRTSPMessage response = { 0 };
6131 gchar *respcont = NULL;
6134 src->need_redirect = FALSE;
6136 /* can't continue without a valid url */
6137 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6138 res = GST_RTSP_EINVAL;
6141 src->tried_url_auth = FALSE;
6143 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6144 goto connect_failed;
6146 /* create OPTIONS */
6147 GST_DEBUG_OBJECT (src, "create options...");
6149 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
6150 src->conninfo.url_str);
6152 goto create_request_failed;
6155 GST_DEBUG_OBJECT (src, "send options...");
6158 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6161 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6166 if (!gst_rtspsrc_parse_methods (src, &response))
6169 /* create DESCRIBE */
6170 GST_DEBUG_OBJECT (src, "create describe...");
6172 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
6173 src->conninfo.url_str);
6175 goto create_request_failed;
6177 /* we only accept SDP for now */
6178 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
6182 GST_DEBUG_OBJECT (src, "send describe...");
6185 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
6188 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6192 /* we only perform redirect for the describe, currently */
6193 if (src->need_redirect) {
6194 /* close connection, we don't have to send a TEARDOWN yet, ignore the
6196 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6198 gst_rtsp_message_unset (&request);
6199 gst_rtsp_message_unset (&response);
6205 /* it could be that the DESCRIBE method was not implemented */
6206 if (!src->methods & GST_RTSP_DESCRIBE)
6209 /* check if reply is SDP */
6210 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
6212 /* could not be set but since the request returned OK, we assume it
6213 * was SDP, else check it. */
6215 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
6216 goto wrong_content_type;
6219 /* get message body and parse as SDP */
6220 gst_rtsp_message_get_body (&response, &data, &size);
6221 if (data == NULL || size == 0)
6224 GST_DEBUG_OBJECT (src, "parse SDP...");
6225 gst_sdp_message_new (sdp);
6226 gst_sdp_message_parse_buffer (data, size, *sdp);
6228 /* clean up any messages */
6229 gst_rtsp_message_unset (&request);
6230 gst_rtsp_message_unset (&response);
6237 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
6238 ("No valid RTSP URL was provided"));
6243 gchar *str = gst_rtsp_strresult (res);
6245 if (res != GST_RTSP_EINTR) {
6246 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6247 ("Failed to connect. (%s)", str));
6249 GST_WARNING_OBJECT (src, "connect interrupted");
6254 create_request_failed:
6256 gchar *str = gst_rtsp_strresult (res);
6258 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6259 ("Could not create request. (%s)", str));
6265 /* Don't post a message - the rtsp_send method will have
6266 * taken care of it because we passed NULL for the response code */
6271 /* error was posted */
6272 res = GST_RTSP_ERROR;
6277 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6278 ("Server does not support SDP, got %s.", respcont));
6279 res = GST_RTSP_ERROR;
6284 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6285 ("Server can not provide an SDP."));
6286 res = GST_RTSP_ERROR;
6291 if (src->conninfo.connection) {
6292 GST_DEBUG_OBJECT (src, "free connection");
6293 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6295 gst_rtsp_message_unset (&request);
6296 gst_rtsp_message_unset (&response);
6301 static GstRTSPResult
6302 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
6307 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
6309 if (src->sdp == NULL) {
6310 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
6314 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
6319 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
6326 GST_WARNING_OBJECT (src, "can't get sdp");
6327 src->open_error = TRUE;
6332 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
6333 src->open_error = TRUE;
6338 static GstRTSPResult
6339 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
6341 GstRTSPMessage request = { 0 };
6342 GstRTSPMessage response = { 0 };
6343 GstRTSPResult res = GST_RTSP_OK;
6345 const gchar *control;
6347 GST_DEBUG_OBJECT (src, "TEARDOWN...");
6349 gst_rtspsrc_set_state (src, GST_STATE_READY);
6351 if (src->state < GST_RTSP_STATE_READY) {
6352 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
6359 /* construct a control url */
6360 control = get_aggregate_control (src);
6362 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
6365 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6366 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6367 const gchar *setup_url;
6368 GstRTSPConnInfo *info;
6370 /* try aggregate control first but do non-aggregate control otherwise */
6372 setup_url = control;
6373 else if ((setup_url = stream->conninfo.location) == NULL)
6376 if (src->conninfo.connection) {
6377 info = &src->conninfo;
6378 } else if (stream->conninfo.connection) {
6379 info = &stream->conninfo;
6383 if (!info->connected)
6388 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
6390 goto create_request_failed;
6393 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
6396 gst_rtspsrc_send (src, info->connection, &request, &response,
6400 /* FIXME, parse result? */
6401 gst_rtsp_message_unset (&request);
6402 gst_rtsp_message_unset (&response);
6405 /* early exit when we did aggregate control */
6411 /* close connections */
6412 GST_DEBUG_OBJECT (src, "closing connection...");
6413 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6414 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6415 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6416 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
6420 gst_rtspsrc_cleanup (src);
6422 src->state = GST_RTSP_STATE_INVALID;
6425 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
6430 create_request_failed:
6432 gchar *str = gst_rtsp_strresult (res);
6434 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6435 ("Could not create request. (%s)", str));
6441 gchar *str = gst_rtsp_strresult (res);
6443 gst_rtsp_message_unset (&request);
6444 if (res != GST_RTSP_EINTR) {
6445 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6446 ("Could not send message. (%s)", str));
6448 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
6455 GST_DEBUG_OBJECT (src,
6456 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
6461 /* RTP-Info is of the format:
6463 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
6465 * rtptime corresponds to the timestamp for the NPT time given in the header
6466 * seqbase corresponds to the next sequence number we received. This number
6467 * indicates the first seqnum after the seek and should be used to discard
6468 * packets that are from before the seek.
6471 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
6476 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
6478 infos = g_strsplit (rtpinfo, ",", 0);
6479 for (i = 0; infos[i]; i++) {
6481 GstRTSPStream *stream;
6485 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
6487 /* init values, types of seqbase and timebase are bigger than needed so we
6488 * can store -1 as uninitialized values */
6493 /* parse url, find stream for url.
6494 * parse seq and rtptime. The seq number should be configured in the rtp
6495 * depayloader or session manager to detect gaps. Same for the rtptime, it
6496 * should be used to create an initial time newsegment. */
6497 fields = g_strsplit (infos[i], ";", 0);
6498 for (j = 0; fields[j]; j++) {
6499 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
6500 /* remove leading whitespace */
6501 fields[j] = g_strchug (fields[j]);
6502 if (g_str_has_prefix (fields[j], "url=")) {
6503 /* get the url and the stream */
6505 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
6506 } else if (g_str_has_prefix (fields[j], "seq=")) {
6507 seqbase = atoi (fields[j] + 4);
6508 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
6509 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
6512 g_strfreev (fields);
6513 /* now we need to store the values for the caps of the stream */
6514 if (stream != NULL) {
6515 GST_DEBUG_OBJECT (src,
6516 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
6517 stream, seqbase, timebase);
6519 /* we have a stream, configure detected params */
6520 stream->seqbase = seqbase;
6521 stream->timebase = timebase;
6530 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
6535 interval = strtoul (rtcp, NULL, 10);
6536 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
6541 interval *= GST_MSECOND;
6543 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6544 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6546 /* already (optionally) retrieved this when configuring manager */
6547 if (stream->session) {
6548 GObject *rtpsession = stream->session;
6550 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
6552 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
6556 /* now it happens that (Xenon) server sending this may also provide bogus
6557 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
6558 * and just use RTP-Info to sync */
6560 GObjectClass *klass;
6562 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
6563 if (g_object_class_find_property (klass, "rtcp-sync")) {
6564 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
6565 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
6571 gst_rtspsrc_get_float (const gchar * dstr)
6573 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6575 /* canonicalise floating point string so we can handle float strings
6576 * in the form "24.930" or "24,930" irrespective of the current locale */
6577 g_strlcpy (s, dstr, sizeof (s));
6578 g_strdelimit (s, ",", '.');
6579 return g_ascii_strtod (s, NULL);
6583 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
6585 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6587 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
6588 g_strlcpy (val_str, "now", sizeof (val_str));
6590 if (segment->position == 0) {
6591 g_strlcpy (val_str, "0", sizeof (val_str));
6593 g_ascii_dtostr (val_str, sizeof (val_str),
6594 ((gdouble) segment->position) / GST_SECOND);
6597 return g_strdup_printf ("npt=%s-", val_str);
6601 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
6603 stream->timebase = -1;
6604 stream->seqbase = -1;
6608 stream->caps = gst_caps_make_writable (stream->caps);
6609 s = gst_caps_get_structure (stream->caps, 0);
6610 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
6614 static GstRTSPResult
6615 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
6617 GstRTSPResult res = GST_RTSP_OK;
6619 if (src->state < GST_RTSP_STATE_READY) {
6620 res = GST_RTSP_ERROR;
6621 if (src->open_error) {
6622 GST_DEBUG_OBJECT (src, "the stream was in error");
6626 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
6628 if ((res = gst_rtspsrc_open (src, async)) < 0) {
6629 GST_DEBUG_OBJECT (src, "failed to open stream");
6638 static GstRTSPResult
6639 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
6641 GstRTSPMessage request = { 0 };
6642 GstRTSPMessage response = { 0 };
6643 GstRTSPResult res = GST_RTSP_OK;
6647 const gchar *control;
6649 GST_DEBUG_OBJECT (src, "PLAY...");
6651 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6654 if (!(src->methods & GST_RTSP_PLAY))
6657 if (src->state == GST_RTSP_STATE_PLAYING)
6660 if (!src->conninfo.connection || !src->conninfo.connected)
6663 /* send some dummy packets before we activate the receive in the
6665 gst_rtspsrc_send_dummy_packets (src);
6667 /* require new SR packets */
6669 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
6671 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
6673 /* construct a control url */
6674 control = get_aggregate_control (src);
6676 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6677 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6678 const gchar *setup_url;
6679 GstRTSPConnection *conn;
6681 /* try aggregate control first but do non-aggregate control otherwise */
6683 setup_url = control;
6684 else if ((setup_url = stream->conninfo.location) == NULL)
6687 if (src->conninfo.connection) {
6688 conn = src->conninfo.connection;
6689 } else if (stream->conninfo.connection) {
6690 conn = stream->conninfo.connection;
6696 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
6698 goto create_request_failed;
6700 if (src->need_range) {
6701 hval = gen_range_header (src, segment);
6703 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
6705 /* store the newsegment event so it can be sent from the streaming thread. */
6706 if (src->start_segment)
6707 gst_event_unref (src->start_segment);
6708 src->start_segment = gst_event_new_segment (&src->segment);
6711 if (segment->rate != 1.0) {
6712 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
6714 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
6716 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
6718 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
6722 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
6724 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6727 /* seek may have silently failed as it is not supported */
6728 if (!(src->methods & GST_RTSP_PLAY)) {
6729 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
6730 /* obviously it is supported as we made it here */
6731 src->methods |= GST_RTSP_PLAY;
6732 src->seekable = FALSE;
6733 /* but there is nothing to parse in the response,
6734 * so convey we have no idea and not to expect anything particular */
6735 clear_rtp_base (src, stream);
6739 /* need to do for all streams */
6740 for (run = src->streams; run; run = g_list_next (run))
6741 clear_rtp_base (src, (GstRTSPStream *) run->data);
6743 /* NOTE the above also disables npt based eos detection */
6744 /* and below forces position to 0,
6745 * which is visible feedback we lost the plot */
6746 segment->start = segment->position = src->last_pos;
6749 gst_rtsp_message_unset (&request);
6751 /* parse RTP npt field. This is the current position in the stream (Normal
6752 * Play Time) and should be put in the NEWSEGMENT position field. */
6753 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
6755 gst_rtspsrc_parse_range (src, hval, segment);
6757 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
6758 segment->rate = 1.0;
6760 /* parse Speed header. This is the intended playback rate of the stream
6761 * and should be put in the NEWSEGMENT rate field. */
6762 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
6763 0) == GST_RTSP_OK) {
6764 segment->rate = gst_rtspsrc_get_float (hval);
6765 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
6766 &hval, 0) == GST_RTSP_OK) {
6767 segment->rate = gst_rtspsrc_get_float (hval);
6770 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
6771 * for the RTP packets. If this is not present, we assume all starts from 0...
6772 * This is info for the RTP session manager that we pass to it in caps. */
6774 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
6775 &hval, hval_idx++) == GST_RTSP_OK)
6776 gst_rtspsrc_parse_rtpinfo (src, hval);
6778 /* some servers indicate RTCP parameters in PLAY response,
6779 * rather than properly in SDP */
6780 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
6781 &hval, 0) == GST_RTSP_OK)
6782 gst_rtspsrc_handle_rtcp_interval (src, hval);
6784 gst_rtsp_message_unset (&response);
6786 /* early exit when we did aggregate control */
6790 /* configure the caps of the streams after we parsed all headers. Only reset
6791 * the manager object when we set a new Range header (we did a seek) */
6792 gst_rtspsrc_configure_caps (src, segment, src->need_range);
6794 /* set again when needed */
6795 src->need_range = FALSE;
6797 src->running = TRUE;
6798 src->base_time = -1;
6799 src->state = GST_RTSP_STATE_PLAYING;
6802 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
6803 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6804 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6805 stream->discont = TRUE;
6810 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
6817 GST_DEBUG_OBJECT (src, "failed to open stream");
6822 GST_DEBUG_OBJECT (src, "PLAY is not supported");
6827 GST_DEBUG_OBJECT (src, "we were already PLAYING");
6830 create_request_failed:
6832 gchar *str = gst_rtsp_strresult (res);
6834 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6835 ("Could not create request. (%s)", str));
6841 gchar *str = gst_rtsp_strresult (res);
6843 gst_rtsp_message_unset (&request);
6844 if (res != GST_RTSP_EINTR) {
6845 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6846 ("Could not send message. (%s)", str));
6848 GST_WARNING_OBJECT (src, "PLAY interrupted");
6855 static GstRTSPResult
6856 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
6858 GstRTSPResult res = GST_RTSP_OK;
6859 GstRTSPMessage request = { 0 };
6860 GstRTSPMessage response = { 0 };
6862 const gchar *control;
6864 GST_DEBUG_OBJECT (src, "PAUSE...");
6866 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6869 if (!(src->methods & GST_RTSP_PAUSE))
6872 if (src->state == GST_RTSP_STATE_READY)
6875 if (!src->conninfo.connection || !src->conninfo.connected)
6878 /* construct a control url */
6879 control = get_aggregate_control (src);
6881 /* loop over the streams. We might exit the loop early when we could do an
6882 * aggregate control */
6883 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6884 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6885 GstRTSPConnection *conn;
6886 const gchar *setup_url;
6888 /* try aggregate control first but do non-aggregate control otherwise */
6890 setup_url = control;
6891 else if ((setup_url = stream->conninfo.location) == NULL)
6894 if (src->conninfo.connection) {
6895 conn = src->conninfo.connection;
6896 } else if (stream->conninfo.connection) {
6897 conn = stream->conninfo.connection;
6903 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
6904 ("Sending PAUSE request"));
6907 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
6909 goto create_request_failed;
6911 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6914 gst_rtsp_message_unset (&request);
6915 gst_rtsp_message_unset (&response);
6917 /* exit early when we did agregate control */
6922 /* change element states now */
6923 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
6926 src->state = GST_RTSP_STATE_READY;
6930 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
6937 GST_DEBUG_OBJECT (src, "failed to open stream");
6942 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
6947 GST_DEBUG_OBJECT (src, "we were already PAUSED");
6950 create_request_failed:
6952 gchar *str = gst_rtsp_strresult (res);
6954 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6955 ("Could not create request. (%s)", str));
6961 gchar *str = gst_rtsp_strresult (res);
6963 gst_rtsp_message_unset (&request);
6964 if (res != GST_RTSP_EINTR) {
6965 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6966 ("Could not send message. (%s)", str));
6968 GST_WARNING_OBJECT (src, "PAUSE interrupted");
6976 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
6978 GstRTSPSrc *rtspsrc;
6980 rtspsrc = GST_RTSPSRC (bin);
6982 switch (GST_MESSAGE_TYPE (message)) {
6983 case GST_MESSAGE_EOS:
6984 gst_message_unref (message);
6986 case GST_MESSAGE_ELEMENT:
6988 const GstStructure *s = gst_message_get_structure (message);
6990 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
6991 gboolean ignore_timeout;
6993 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
6995 GST_OBJECT_LOCK (rtspsrc);
6996 ignore_timeout = rtspsrc->ignore_timeout;
6997 rtspsrc->ignore_timeout = TRUE;
6998 GST_OBJECT_UNLOCK (rtspsrc);
7000 /* we only act on the first udp timeout message, others are irrelevant
7001 * and can be ignored. */
7002 if (!ignore_timeout)
7003 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
7005 gst_message_unref (message);
7008 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7011 case GST_MESSAGE_ERROR:
7014 GstRTSPStream *stream;
7017 udpsrc = GST_MESSAGE_SRC (message);
7019 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7020 GST_ELEMENT_NAME (udpsrc));
7022 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7026 /* we ignore the RTCP udpsrc */
7027 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7030 /* if we get error messages from the udp sources, that's not a problem as
7031 * long as not all of them error out. We also don't really know what the
7032 * problem is, the message does not give enough detail... */
7033 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7034 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7035 if (ret != GST_FLOW_OK)
7039 gst_message_unref (message);
7043 /* fatal but not our message, forward */
7044 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7049 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7055 /* the thread where everything happens */
7057 gst_rtspsrc_thread (GstRTSPSrc * src)
7061 GST_OBJECT_LOCK (src);
7062 cmd = src->pending_cmd;
7063 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7064 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7065 src->pending_cmd = CMD_LOOP;
7067 src->pending_cmd = CMD_WAIT;
7068 GST_DEBUG_OBJECT (src, "got command %d", cmd);
7070 /* we got the message command, so ensure communication is possible again */
7071 gst_rtspsrc_connection_flush (src, FALSE);
7073 src->busy_cmd = cmd;
7074 GST_OBJECT_UNLOCK (src);
7078 gst_rtspsrc_open (src, TRUE);
7081 gst_rtspsrc_play (src, &src->segment, TRUE);
7084 gst_rtspsrc_pause (src, TRUE);
7087 gst_rtspsrc_close (src, TRUE, FALSE);
7090 gst_rtspsrc_loop (src);
7093 gst_rtspsrc_reconnect (src, FALSE);
7099 GST_OBJECT_LOCK (src);
7100 /* and go back to sleep */
7101 if (src->pending_cmd == CMD_WAIT) {
7103 gst_task_pause (src->task);
7106 src->busy_cmd = CMD_WAIT;
7107 GST_OBJECT_UNLOCK (src);
7111 gst_rtspsrc_start (GstRTSPSrc * src)
7113 GST_DEBUG_OBJECT (src, "starting");
7115 GST_OBJECT_LOCK (src);
7117 src->pending_cmd = CMD_WAIT;
7119 if (src->task == NULL) {
7120 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7121 if (src->task == NULL)
7124 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7126 GST_OBJECT_UNLOCK (src);
7133 GST_ERROR_OBJECT (src, "failed to create task");
7139 gst_rtspsrc_stop (GstRTSPSrc * src)
7143 GST_DEBUG_OBJECT (src, "stopping");
7145 /* also cancels pending task */
7146 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7148 GST_OBJECT_LOCK (src);
7149 if ((task = src->task)) {
7151 GST_OBJECT_UNLOCK (src);
7153 gst_task_stop (task);
7155 /* make sure it is not running */
7156 GST_RTSP_STREAM_LOCK (src);
7157 GST_RTSP_STREAM_UNLOCK (src);
7159 /* now wait for the task to finish */
7160 gst_task_join (task);
7162 /* and free the task */
7163 gst_object_unref (GST_OBJECT (task));
7165 GST_OBJECT_LOCK (src);
7167 GST_OBJECT_UNLOCK (src);
7169 /* ensure synchronously all is closed and clean */
7170 gst_rtspsrc_close (src, FALSE, TRUE);
7175 static GstStateChangeReturn
7176 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
7178 GstRTSPSrc *rtspsrc;
7179 GstStateChangeReturn ret;
7181 rtspsrc = GST_RTSPSRC (element);
7183 switch (transition) {
7184 case GST_STATE_CHANGE_NULL_TO_READY:
7185 if (!gst_rtspsrc_start (rtspsrc))
7188 case GST_STATE_CHANGE_READY_TO_PAUSED:
7189 /* init some state */
7190 rtspsrc->cur_protocols = rtspsrc->protocols;
7191 /* first attempt, don't ignore timeouts */
7192 rtspsrc->ignore_timeout = FALSE;
7193 rtspsrc->open_error = FALSE;
7194 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
7196 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7197 set_manager_buffer_mode (rtspsrc);
7199 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7200 /* unblock the tcp tasks and make the loop waiting */
7201 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
7202 /* make sure it is waiting before we send PAUSE or PLAY below */
7203 GST_RTSP_STREAM_LOCK (rtspsrc);
7204 GST_RTSP_STREAM_UNLOCK (rtspsrc);
7207 case GST_STATE_CHANGE_PAUSED_TO_READY:
7213 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
7214 if (ret == GST_STATE_CHANGE_FAILURE)
7217 switch (transition) {
7218 case GST_STATE_CHANGE_NULL_TO_READY:
7219 ret = GST_STATE_CHANGE_SUCCESS;
7221 case GST_STATE_CHANGE_READY_TO_PAUSED:
7222 ret = GST_STATE_CHANGE_NO_PREROLL;
7224 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7225 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
7226 ret = GST_STATE_CHANGE_SUCCESS;
7228 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7229 /* send pause request and keep the idle task around */
7230 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
7231 ret = GST_STATE_CHANGE_NO_PREROLL;
7233 case GST_STATE_CHANGE_PAUSED_TO_READY:
7234 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
7235 ret = GST_STATE_CHANGE_SUCCESS;
7237 case GST_STATE_CHANGE_READY_TO_NULL:
7238 gst_rtspsrc_stop (rtspsrc);
7239 ret = GST_STATE_CHANGE_SUCCESS;
7250 GST_DEBUG_OBJECT (rtspsrc, "start failed");
7251 return GST_STATE_CHANGE_FAILURE;
7256 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
7259 GstRTSPSrc *rtspsrc;
7261 rtspsrc = GST_RTSPSRC (element);
7263 if (GST_EVENT_IS_DOWNSTREAM (event)) {
7264 res = gst_rtspsrc_push_event (rtspsrc, event);
7266 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
7273 /*** GSTURIHANDLER INTERFACE *************************************************/
7276 gst_rtspsrc_uri_get_type (GType type)
7281 static const gchar *const *
7282 gst_rtspsrc_uri_get_protocols (GType type)
7284 static const gchar *protocols[] =
7285 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
7286 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
7293 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
7295 GstRTSPSrc *src = GST_RTSPSRC (handler);
7297 /* FIXME: make thread-safe */
7298 return g_strdup (src->conninfo.location);
7302 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
7307 GstRTSPUrl *newurl = NULL;
7308 GstSDPMessage *sdp = NULL;
7310 src = GST_RTSPSRC (handler);
7312 /* same URI, we're fine */
7313 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
7316 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
7317 if ((res = gst_sdp_message_new (&sdp) < 0))
7320 GST_DEBUG_OBJECT (src, "parsing SDP message");
7321 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
7325 GST_DEBUG_OBJECT (src, "parsing URI");
7326 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
7330 /* if worked, free previous and store new url object along with the original
7332 GST_DEBUG_OBJECT (src, "configuring URI");
7333 g_free (src->conninfo.location);
7334 src->conninfo.location = g_strdup (uri);
7335 gst_rtsp_url_free (src->conninfo.url);
7336 src->conninfo.url = newurl;
7337 g_free (src->conninfo.url_str);
7339 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
7341 src->conninfo.url_str = NULL;
7344 gst_sdp_message_free (src->sdp);
7346 src->from_sdp = sdp != NULL;
7348 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
7349 GST_DEBUG_OBJECT (src, "request uri is: %s",
7350 GST_STR_NULL (src->conninfo.url_str));
7357 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
7362 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
7363 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7364 "Could not create SDP");
7369 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
7370 GST_STR_NULL (uri));
7371 gst_sdp_message_free (sdp);
7372 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7378 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
7379 GST_STR_NULL (uri), res);
7380 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7381 "Invalid RTSP URI");
7387 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
7389 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
7391 iface->get_type = gst_rtspsrc_uri_get_type;
7392 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
7393 iface->get_uri = gst_rtspsrc_uri_get_uri;
7394 iface->set_uri = gst_rtspsrc_uri_set_uri;