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_CONNECTION_SPEED 0
177 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
178 #define DEFAULT_DO_RTCP TRUE
179 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
180 #define DEFAULT_PROXY NULL
181 #define DEFAULT_RTP_BLOCKSIZE 0
182 #define DEFAULT_USER_ID NULL
183 #define DEFAULT_USER_PW NULL
184 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
185 #define DEFAULT_PORT_RANGE NULL
186 #define DEFAULT_SHORT_HEADER FALSE
187 #define DEFAULT_PROBATION 2
188 #define DEFAULT_UDP_RECONNECT TRUE
189 #define DEFAULT_MULTICAST_IFACE NULL
190 #define DEFAULT_NTP_SYNC FALSE
191 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
192 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
193 #define DEFAULT_TLS_DATABASE NULL
205 PROP_DROP_ON_LATENCY,
206 PROP_CONNECTION_SPEED,
209 PROP_DO_RTSP_KEEP_ALIVE,
218 PROP_UDP_BUFFER_SIZE,
222 PROP_MULTICAST_IFACE,
224 PROP_USE_PIPELINE_CLOCK,
226 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);
305 /* commands we send to out loop to notify it of events */
306 #define CMD_OPEN (1 << 0)
307 #define CMD_PLAY (1 << 1)
308 #define CMD_PAUSE (1 << 2)
309 #define CMD_CLOSE (1 << 3)
310 #define CMD_WAIT (1 << 4)
311 #define CMD_RECONNECT (1 << 5)
312 #define CMD_LOOP (1 << 6)
314 /* mask for all commands */
315 #define CMD_ALL ((CMD_LOOP << 1) - 1)
317 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
319 gchar *__txt = _gst_element_error_printf text; \
320 gst_element_post_message (GST_ELEMENT_CAST (el), \
321 gst_message_new_progress (GST_OBJECT_CAST (el), \
322 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
326 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
328 #define gst_rtspsrc_parent_class parent_class
329 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
330 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
333 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
335 GST_DEBUG_OBJECT (src, "default handler");
340 select_stream_accum (GSignalInvocationHint * ihint,
341 GValue * return_accu, const GValue * handler_return, gpointer data)
345 myboolean = g_value_get_boolean (handler_return);
346 GST_DEBUG ("accum %d", myboolean);
347 g_value_set_boolean (return_accu, myboolean);
349 /* stop emission if FALSE */
354 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
356 GObjectClass *gobject_class;
357 GstElementClass *gstelement_class;
358 GstBinClass *gstbin_class;
360 gobject_class = (GObjectClass *) klass;
361 gstelement_class = (GstElementClass *) klass;
362 gstbin_class = (GstBinClass *) klass;
364 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
366 gobject_class->set_property = gst_rtspsrc_set_property;
367 gobject_class->get_property = gst_rtspsrc_get_property;
369 gobject_class->finalize = gst_rtspsrc_finalize;
371 g_object_class_install_property (gobject_class, PROP_LOCATION,
372 g_param_spec_string ("location", "RTSP Location",
373 "Location of the RTSP url to read",
374 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
376 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
377 g_param_spec_flags ("protocols", "Protocols",
378 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
379 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
381 g_object_class_install_property (gobject_class, PROP_DEBUG,
382 g_param_spec_boolean ("debug", "Debug",
383 "Dump request and response messages to stdout",
384 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
386 g_object_class_install_property (gobject_class, PROP_RETRY,
387 g_param_spec_uint ("retry", "Retry",
388 "Max number of retries when allocating RTP ports.",
389 0, G_MAXUINT16, DEFAULT_RETRY,
390 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
392 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
393 g_param_spec_uint64 ("timeout", "Timeout",
394 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
395 0, G_MAXUINT64, DEFAULT_TIMEOUT,
396 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
398 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
399 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
400 "Fail after timeout microseconds on TCP connections (0 = disabled)",
401 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
402 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
404 g_object_class_install_property (gobject_class, PROP_LATENCY,
405 g_param_spec_uint ("latency", "Buffer latency in ms",
406 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
407 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
409 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
410 g_param_spec_boolean ("drop-on-latency",
411 "Drop buffers when maximum latency is reached",
412 "Tells the jitterbuffer to never exceed the given latency in size",
413 DEFAULT_DROP_ON_LATENCY, 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::tls-database:
599 * TLS database with anchor certificate authorities used to validate
600 * the server certificate.
604 g_object_class_install_property (gobject_class, PROP_TLS_DATABASE,
605 g_param_spec_object ("tls-database", "TLS database",
606 "TLS database with anchor certificate authorities used to validate the server certificate",
607 G_TYPE_TLS_DATABASE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
610 * GstRTSPSrc::handle-request:
611 * @rtspsrc: a #GstRTSPSrc
612 * @request: a #GstRTSPMessage
613 * @response: a #GstRTSPMessage
615 * Handle a server request in @request and prepare @response.
617 * This signal is called from the streaming thread, you should therefore not
618 * do any state changes on @rtspsrc because this might deadlock. If you want
619 * to modify the state as a result of this signal, post a
620 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
625 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
626 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
627 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
628 G_TYPE_POINTER, G_TYPE_POINTER);
631 * GstRTSPSrc::on-sdp:
632 * @rtspsrc: a #GstRTSPSrc
633 * @sdp: a #GstSDPMessage
635 * Emited when the client has retrieved the SDP and before it configures the
636 * streams in the SDP. @sdp can be inspected and modified.
638 * This signal is called from the streaming thread, you should therefore not
639 * do any state changes on @rtspsrc because this might deadlock. If you want
640 * to modify the state as a result of this signal, post a
641 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
646 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
647 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
648 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
649 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
652 * GstRTSPSrc::select-stream:
653 * @rtspsrc: a #GstRTSPSrc
654 * @num: the stream number
655 * @caps: the stream caps
657 * Emited before the client decides to configure the stream @num with
660 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
665 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
666 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
667 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
668 (GCallback) default_select_stream, select_stream_accum, NULL,
669 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
672 * GstRTSPSrc::new-manager:
673 * @rtspsrc: a #GstRTSPSrc
674 * @manager: a #GstElement
676 * Emited after a new manager (like rtpbin) was created and the default
677 * properties were configured.
681 gst_rtspsrc_signals[SIGNAL_NEW_MANAGER] =
682 g_signal_new_class_handler ("new-manager", G_TYPE_FROM_CLASS (klass),
683 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP, 0, NULL, NULL,
684 g_cclosure_marshal_generic, G_TYPE_NONE, 1, GST_TYPE_ELEMENT);
686 gstelement_class->send_event = gst_rtspsrc_send_event;
687 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
688 gstelement_class->change_state = gst_rtspsrc_change_state;
690 gst_element_class_add_pad_template (gstelement_class,
691 gst_static_pad_template_get (&rtptemplate));
693 gst_element_class_set_static_metadata (gstelement_class,
694 "RTSP packet receiver", "Source/Network",
695 "Receive data over the network via RTSP (RFC 2326)",
696 "Wim Taymans <wim@fluendo.com>, "
697 "Thijs Vermeir <thijs.vermeir@barco.com>, "
698 "Lutz Mueller <lutz@topfrose.de>");
700 gstbin_class->handle_message = gst_rtspsrc_handle_message;
702 gst_rtsp_ext_list_init ();
706 gst_rtspsrc_init (GstRTSPSrc * src)
708 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
709 src->protocols = DEFAULT_PROTOCOLS;
710 src->debug = DEFAULT_DEBUG;
711 src->retry = DEFAULT_RETRY;
712 src->udp_timeout = DEFAULT_TIMEOUT;
713 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
714 src->latency = DEFAULT_LATENCY_MS;
715 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
716 src->connection_speed = DEFAULT_CONNECTION_SPEED;
717 src->nat_method = DEFAULT_NAT_METHOD;
718 src->do_rtcp = DEFAULT_DO_RTCP;
719 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
720 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
721 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
722 src->user_id = g_strdup (DEFAULT_USER_ID);
723 src->user_pw = g_strdup (DEFAULT_USER_PW);
724 src->buffer_mode = DEFAULT_BUFFER_MODE;
725 src->client_port_range.min = 0;
726 src->client_port_range.max = 0;
727 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
728 src->short_header = DEFAULT_SHORT_HEADER;
729 src->probation = DEFAULT_PROBATION;
730 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
731 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
732 src->ntp_sync = DEFAULT_NTP_SYNC;
733 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
735 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
736 src->tls_database = DEFAULT_TLS_DATABASE;
738 /* get a list of all extensions */
739 src->extensions = gst_rtsp_ext_list_get ();
741 /* connect to send signal */
742 gst_rtsp_ext_list_connect (src->extensions, "send",
743 (GCallback) gst_rtspsrc_send_cb, src);
745 /* protects the streaming thread in interleaved mode or the polling
746 * thread in UDP mode. */
747 g_rec_mutex_init (&src->stream_rec_lock);
749 /* protects our state changes from multiple invocations */
750 g_rec_mutex_init (&src->state_rec_lock);
752 src->state = GST_RTSP_STATE_INVALID;
754 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
758 gst_rtspsrc_finalize (GObject * object)
762 rtspsrc = GST_RTSPSRC (object);
764 gst_rtsp_ext_list_free (rtspsrc->extensions);
765 g_free (rtspsrc->conninfo.location);
766 gst_rtsp_url_free (rtspsrc->conninfo.url);
767 g_free (rtspsrc->conninfo.url_str);
768 g_free (rtspsrc->user_id);
769 g_free (rtspsrc->user_pw);
770 g_free (rtspsrc->multi_iface);
773 gst_sdp_message_free (rtspsrc->sdp);
776 if (rtspsrc->provided_clock)
777 gst_object_unref (rtspsrc->provided_clock);
780 gst_structure_free (rtspsrc->sdes);
782 if (rtspsrc->tls_database)
783 g_object_unref (rtspsrc->tls_database);
786 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
787 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
789 G_OBJECT_CLASS (parent_class)->finalize (object);
793 gst_rtspsrc_provide_clock (GstElement * element)
795 GstRTSPSrc *src = GST_RTSPSRC (element);
798 if ((clock = src->provided_clock) != NULL)
799 gst_object_ref (clock);
804 /* a proxy string of the format [user:passwd@]host[:port] */
806 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
810 g_free (rtsp->proxy_user);
811 rtsp->proxy_user = NULL;
812 g_free (rtsp->proxy_passwd);
813 rtsp->proxy_passwd = NULL;
814 g_free (rtsp->proxy_host);
815 rtsp->proxy_host = NULL;
816 rtsp->proxy_port = 0;
823 /* we allow http:// in front but ignore it */
824 if (g_str_has_prefix (p, "http://"))
827 at = strchr (p, '@');
829 /* look for user:passwd */
830 col = strchr (proxy, ':');
831 if (col == NULL || col > at)
834 rtsp->proxy_user = g_strndup (p, col - p);
836 rtsp->proxy_passwd = g_strndup (col, at - col);
841 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
842 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
843 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
844 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
845 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
846 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
847 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
850 col = strchr (p, ':');
853 /* everything before the colon is the hostname */
854 rtsp->proxy_host = g_strndup (p, col - p);
856 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
858 rtsp->proxy_host = g_strdup (p);
859 rtsp->proxy_port = 8080;
865 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
867 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
868 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
871 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
873 rtspsrc->ptcp_timeout = NULL;
877 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
882 rtspsrc = GST_RTSPSRC (object);
886 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
887 g_value_get_string (value), NULL);
890 rtspsrc->protocols = g_value_get_flags (value);
893 rtspsrc->debug = g_value_get_boolean (value);
896 rtspsrc->retry = g_value_get_uint (value);
899 rtspsrc->udp_timeout = g_value_get_uint64 (value);
901 case PROP_TCP_TIMEOUT:
902 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
905 rtspsrc->latency = g_value_get_uint (value);
907 case PROP_DROP_ON_LATENCY:
908 rtspsrc->drop_on_latency = g_value_get_boolean (value);
910 case PROP_CONNECTION_SPEED:
911 rtspsrc->connection_speed = g_value_get_uint64 (value);
913 case PROP_NAT_METHOD:
914 rtspsrc->nat_method = g_value_get_enum (value);
917 rtspsrc->do_rtcp = g_value_get_boolean (value);
919 case PROP_DO_RTSP_KEEP_ALIVE:
920 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
923 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
926 if (rtspsrc->prop_proxy_id)
927 g_free (rtspsrc->prop_proxy_id);
928 rtspsrc->prop_proxy_id = g_value_dup_string (value);
931 if (rtspsrc->prop_proxy_pw)
932 g_free (rtspsrc->prop_proxy_pw);
933 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
935 case PROP_RTP_BLOCKSIZE:
936 rtspsrc->rtp_blocksize = g_value_get_uint (value);
939 if (rtspsrc->user_id)
940 g_free (rtspsrc->user_id);
941 rtspsrc->user_id = g_value_dup_string (value);
944 if (rtspsrc->user_pw)
945 g_free (rtspsrc->user_pw);
946 rtspsrc->user_pw = g_value_dup_string (value);
948 case PROP_BUFFER_MODE:
949 rtspsrc->buffer_mode = g_value_get_enum (value);
951 case PROP_PORT_RANGE:
955 str = g_value_get_string (value);
957 sscanf (str, "%u-%u",
958 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
960 rtspsrc->client_port_range.min = 0;
961 rtspsrc->client_port_range.max = 0;
965 case PROP_UDP_BUFFER_SIZE:
966 rtspsrc->udp_buffer_size = g_value_get_int (value);
968 case PROP_SHORT_HEADER:
969 rtspsrc->short_header = g_value_get_boolean (value);
972 rtspsrc->probation = g_value_get_uint (value);
974 case PROP_UDP_RECONNECT:
975 rtspsrc->udp_reconnect = g_value_get_boolean (value);
977 case PROP_MULTICAST_IFACE:
978 g_free (rtspsrc->multi_iface);
980 if (g_value_get_string (value) == NULL)
981 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
983 rtspsrc->multi_iface = g_value_dup_string (value);
986 rtspsrc->ntp_sync = g_value_get_boolean (value);
988 case PROP_USE_PIPELINE_CLOCK:
989 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
992 rtspsrc->sdes = g_value_dup_boxed (value);
994 case PROP_TLS_VALIDATION_FLAGS:
995 rtspsrc->tls_validation_flags = g_value_get_flags (value);
997 case PROP_TLS_DATABASE:
998 g_clear_object (&rtspsrc->tls_database);
999 rtspsrc->tls_database = g_value_dup_object (value);
1002 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1008 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
1011 GstRTSPSrc *rtspsrc;
1013 rtspsrc = GST_RTSPSRC (object);
1017 g_value_set_string (value, rtspsrc->conninfo.location);
1019 case PROP_PROTOCOLS:
1020 g_value_set_flags (value, rtspsrc->protocols);
1023 g_value_set_boolean (value, rtspsrc->debug);
1026 g_value_set_uint (value, rtspsrc->retry);
1029 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1031 case PROP_TCP_TIMEOUT:
1035 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1036 rtspsrc->tcp_timeout.tv_usec;
1037 g_value_set_uint64 (value, timeout);
1041 g_value_set_uint (value, rtspsrc->latency);
1043 case PROP_DROP_ON_LATENCY:
1044 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1046 case PROP_CONNECTION_SPEED:
1047 g_value_set_uint64 (value, rtspsrc->connection_speed);
1049 case PROP_NAT_METHOD:
1050 g_value_set_enum (value, rtspsrc->nat_method);
1053 g_value_set_boolean (value, rtspsrc->do_rtcp);
1055 case PROP_DO_RTSP_KEEP_ALIVE:
1056 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1062 if (rtspsrc->proxy_host) {
1064 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1068 g_value_take_string (value, str);
1072 g_value_set_string (value, rtspsrc->prop_proxy_id);
1075 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1077 case PROP_RTP_BLOCKSIZE:
1078 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1081 g_value_set_string (value, rtspsrc->user_id);
1084 g_value_set_string (value, rtspsrc->user_pw);
1086 case PROP_BUFFER_MODE:
1087 g_value_set_enum (value, rtspsrc->buffer_mode);
1089 case PROP_PORT_RANGE:
1093 if (rtspsrc->client_port_range.min != 0) {
1094 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1095 rtspsrc->client_port_range.max);
1099 g_value_take_string (value, str);
1102 case PROP_UDP_BUFFER_SIZE:
1103 g_value_set_int (value, rtspsrc->udp_buffer_size);
1105 case PROP_SHORT_HEADER:
1106 g_value_set_boolean (value, rtspsrc->short_header);
1108 case PROP_PROBATION:
1109 g_value_set_uint (value, rtspsrc->probation);
1111 case PROP_UDP_RECONNECT:
1112 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1114 case PROP_MULTICAST_IFACE:
1115 g_value_set_string (value, rtspsrc->multi_iface);
1118 g_value_set_boolean (value, rtspsrc->ntp_sync);
1120 case PROP_USE_PIPELINE_CLOCK:
1121 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1124 g_value_set_boxed (value, rtspsrc->sdes);
1126 case PROP_TLS_VALIDATION_FLAGS:
1127 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1129 case PROP_TLS_DATABASE:
1130 g_value_set_object (value, rtspsrc->tls_database);
1133 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1139 find_stream_by_id (GstRTSPStream * stream, gint * id)
1141 if (stream->id == *id)
1148 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1150 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1157 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1159 GstElement *src = (GstElement *) a;
1161 if (stream->udpsrc[0] == src)
1163 if (stream->udpsrc[1] == src)
1170 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1172 /* check qualified setup_url */
1173 if (!strcmp (stream->conninfo.location, (gchar *) a))
1175 /* check original control_url */
1176 if (!strcmp (stream->control_url, (gchar *) a))
1179 /* check if qualified setup_url ends with string */
1180 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1186 static GstRTSPStream *
1187 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1191 /* find and get stream */
1192 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1193 return (GstRTSPStream *) lstream->data;
1198 static const GstSDPBandwidth *
1199 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1200 const GstSDPMedia * media, const gchar * type)
1204 /* first look in the media specific section */
1205 len = gst_sdp_media_bandwidths_len (media);
1206 for (i = 0; i < len; i++) {
1207 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1209 if (strcmp (bw->bwtype, type) == 0)
1212 /* then look in the message specific section */
1213 len = gst_sdp_message_bandwidths_len (sdp);
1214 for (i = 0; i < len; i++) {
1215 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1217 if (strcmp (bw->bwtype, type) == 0)
1224 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1225 const GstSDPMedia * media, GstRTSPStream * stream)
1227 const GstSDPBandwidth *bw;
1229 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1230 stream->as_bandwidth = bw->bandwidth;
1232 stream->as_bandwidth = -1;
1234 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1235 stream->rr_bandwidth = bw->bandwidth;
1237 stream->rr_bandwidth = -1;
1239 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1240 stream->rs_bandwidth = bw->bandwidth;
1242 stream->rs_bandwidth = -1;
1246 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1247 const GstSDPConnection * conn)
1249 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1252 if (conn->addrtype == NULL)
1255 /* check for IPV6 */
1256 if (strcmp (conn->addrtype, "IP4") == 0)
1257 stream->is_ipv6 = FALSE;
1258 else if (strcmp (conn->addrtype, "IP6") == 0)
1259 stream->is_ipv6 = TRUE;
1264 g_free (stream->destination);
1265 stream->destination = g_strdup (conn->address);
1267 /* check for multicast */
1268 stream->is_multicast =
1269 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1271 stream->ttl = conn->ttl;
1274 /* Go over the connections for a stream.
1275 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1277 * - If we are dealing with a localhost address, we disable multicast
1280 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1281 const GstSDPMedia * media, GstRTSPStream * stream)
1283 const GstSDPConnection *conn;
1286 /* first look in the media specific section */
1287 len = gst_sdp_media_connections_len (media);
1288 for (i = 0; i < len; i++) {
1289 conn = gst_sdp_media_get_connection (media, i);
1291 gst_rtspsrc_do_stream_connection (src, stream, conn);
1293 /* then look in the message specific section */
1294 if ((conn = gst_sdp_message_get_connection (sdp))) {
1295 gst_rtspsrc_do_stream_connection (src, stream, conn);
1300 /* m=<media> <UDP port> RTP/AVP <payload>
1303 gst_rtspsrc_collect_payloads (GstRTSPSrc * src, const GstSDPMessage * sdp,
1304 const GstSDPMedia * media, GstRTSPStream * stream)
1310 proto = gst_sdp_media_get_proto (media);
1314 if (g_str_equal (proto, "RTP/AVP"))
1315 stream->profile = GST_RTSP_PROFILE_AVP;
1316 else if (g_str_equal (proto, "RTP/SAVP"))
1317 stream->profile = GST_RTSP_PROFILE_SAVP;
1318 else if (g_str_equal (proto, "RTP/AVPF"))
1319 stream->profile = GST_RTSP_PROFILE_AVPF;
1320 else if (g_str_equal (proto, "RTP/SAVPF"))
1321 stream->profile = GST_RTSP_PROFILE_SAVPF;
1325 len = gst_sdp_media_formats_len (media);
1326 for (i = 0; i < len; i++) {
1333 pt = atoi (gst_sdp_media_get_format (media, i));
1335 GST_DEBUG_OBJECT (src, " looking at %d pt: %d", i, pt);
1338 caps = gst_rtspsrc_media_to_caps (pt, media);
1340 GST_WARNING_OBJECT (src, " skipping pt %d without caps", pt);
1344 /* do some tweaks */
1345 s = gst_caps_get_structure (caps, 0);
1346 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
1347 stream->is_real = (strstr (enc, "-REAL") != NULL);
1348 if (strcmp (enc, "X-ASF-PF") == 0)
1349 stream->container = TRUE;
1351 GST_DEBUG ("mapping sdp session level attributes to caps");
1352 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, caps);
1353 GST_DEBUG ("mapping sdp media level attributes to caps");
1354 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, caps);
1356 /* the first pt will be the default */
1357 if (stream->ptmap->len == 0)
1358 stream->default_pt = pt;
1362 g_array_append_val (stream->ptmap, item);
1365 if (stream->pt >= 96) {
1366 /* If we have a dynamic payload type, see if we have a stream with the
1367 * same payload number. If there is one, they are part of the same
1368 * container and we only need to add one pad. */
1369 if (find_stream (src, &stream->pt, (gpointer) find_stream_by_pt)) {
1370 stream->container = TRUE;
1371 GST_DEBUG ("found another stream with pt %d, marking as container",
1381 GST_ERROR_OBJECT (src, "can't find proto in media");
1386 GST_ERROR_OBJECT (src, "unknown proto in media %s", proto);
1391 static const gchar *
1392 get_aggregate_control (GstRTSPSrc * src)
1397 base = src->control;
1398 else if (src->content_base)
1399 base = src->content_base;
1400 else if (src->conninfo.url_str)
1401 base = src->conninfo.url_str;
1408 static GstRTSPStream *
1409 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1411 GstRTSPStream *stream;
1412 const gchar *control_url;
1413 const GstSDPMedia *media;
1415 /* get media, should not return NULL */
1416 media = gst_sdp_message_get_media (sdp, idx);
1420 stream = g_new0 (GstRTSPStream, 1);
1421 stream->parent = src;
1422 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1424 stream->last_ret = GST_FLOW_NOT_LINKED;
1425 stream->added = FALSE;
1426 stream->disabled = FALSE;
1427 stream->id = src->numstreams++;
1428 stream->eos = FALSE;
1429 stream->discont = TRUE;
1430 stream->seqbase = -1;
1431 stream->timebase = -1;
1432 stream->profile = GST_RTSP_PROFILE_AVP;
1433 stream->ptmap = g_array_new (FALSE, FALSE, sizeof (PtMapItem));
1435 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1436 * session manager to scale RTCP. */
1437 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1439 /* collect connection info */
1440 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1442 /* make the payload type map */
1443 gst_rtspsrc_collect_payloads (src, sdp, media, stream);
1445 /* collect port number */
1446 stream->port = gst_sdp_media_get_port (media);
1448 /* get control url to construct the setup url. The setup url is used to
1449 * configure the transport of the stream and is used to identity the stream in
1450 * the RTP-Info header field returned from PLAY. */
1451 control_url = gst_sdp_media_get_attribute_val (media, "control");
1452 if (control_url == NULL)
1453 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1455 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1456 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1457 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1458 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1460 if (control_url != NULL) {
1461 stream->control_url = g_strdup (control_url);
1462 /* Build a fully qualified url using the content_base if any or by prefixing
1463 * the original request.
1464 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1465 * likely build a URL that the server will fail to understand, this is ok,
1466 * we will fail then. */
1467 if (g_str_has_prefix (control_url, "rtsp://"))
1468 stream->conninfo.location = g_strdup (control_url);
1473 if (g_strcmp0 (control_url, "*") == 0)
1476 base = get_aggregate_control (src);
1478 /* check if the base ends or control starts with / */
1479 has_slash = g_str_has_prefix (control_url, "/");
1480 has_slash = has_slash || g_str_has_suffix (base, "/");
1482 /* concatenate the two strings, insert / when not present */
1483 stream->conninfo.location =
1484 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1487 GST_DEBUG_OBJECT (src, " setup: %s",
1488 GST_STR_NULL (stream->conninfo.location));
1490 /* we keep track of all streams */
1491 src->streams = g_list_append (src->streams, stream);
1499 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1503 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1505 g_array_free (stream->ptmap, TRUE);
1507 g_free (stream->destination);
1508 g_free (stream->control_url);
1509 g_free (stream->conninfo.location);
1511 for (i = 0; i < 2; i++) {
1512 if (stream->udpsrc[i]) {
1513 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1514 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1515 gst_object_unref (stream->udpsrc[i]);
1516 stream->udpsrc[i] = NULL;
1518 if (stream->channelpad[i]) {
1519 gst_object_unref (stream->channelpad[i]);
1520 stream->channelpad[i] = NULL;
1522 if (stream->udpsink[i]) {
1523 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1524 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1525 gst_object_unref (stream->udpsink[i]);
1526 stream->udpsink[i] = NULL;
1529 if (stream->fakesrc) {
1530 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1531 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1532 gst_object_unref (stream->fakesrc);
1533 stream->fakesrc = NULL;
1535 if (stream->srcpad) {
1536 gst_pad_set_active (stream->srcpad, FALSE);
1537 if (stream->added) {
1538 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1539 stream->added = FALSE;
1541 stream->srcpad = NULL;
1543 if (stream->rtcppad) {
1544 gst_object_unref (stream->rtcppad);
1545 stream->rtcppad = NULL;
1547 if (stream->session) {
1548 g_object_unref (stream->session);
1549 stream->session = NULL;
1555 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1559 GST_DEBUG_OBJECT (src, "cleanup");
1561 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1562 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1564 gst_rtspsrc_stream_free (src, stream);
1566 g_list_free (src->streams);
1567 src->streams = NULL;
1569 if (src->manager_sig_id) {
1570 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1571 src->manager_sig_id = 0;
1573 gst_element_set_state (src->manager, GST_STATE_NULL);
1574 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1575 src->manager = NULL;
1577 src->numstreams = 0;
1579 gst_structure_free (src->props);
1582 g_free (src->content_base);
1583 src->content_base = NULL;
1585 g_free (src->control);
1586 src->control = NULL;
1589 gst_rtsp_range_free (src->range);
1592 /* don't clear the SDP when it was used in the url */
1593 if (src->sdp && !src->from_sdp) {
1594 gst_sdp_message_free (src->sdp);
1597 if (src->start_segment) {
1598 gst_event_unref (src->start_segment);
1599 src->start_segment = NULL;
1601 if (src->provided_clock) {
1602 gst_object_unref (src->provided_clock);
1603 src->provided_clock = NULL;
1607 #define PARSE_INT(p, del, res) \
1610 p = strstr (p, del); \
1620 #define PARSE_STRING(p, del, res) \
1623 p = strstr (p, del); \
1635 #define SKIP_SPACES(p) \
1636 while (*p && g_ascii_isspace (*p)) \
1641 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1644 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1645 gint * rate, gchar ** params)
1649 p = (gchar *) rtpmap;
1651 PARSE_INT (p, " ", *payload);
1659 PARSE_STRING (p, "/", *name);
1660 if (*name == NULL) {
1661 GST_DEBUG ("no rate, name %s", p);
1662 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1663 * streams seem to omit the rate. */
1670 p = strstr (p, "/");
1688 * Mapping SDP attributes to caps
1690 * prepend 'a-' to IANA registered sdp attributes names
1691 * (ie: not prefixed with 'x-') in order to avoid
1692 * collision with gstreamer standard caps properties names
1695 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1697 if (attributes->len > 0) {
1701 s = gst_caps_get_structure (caps, 0);
1703 for (i = 0; i < attributes->len; i++) {
1704 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1705 gchar *tofree, *key;
1709 /* skip some of the attribute we already handle */
1710 if (!strcmp (key, "fmtp"))
1712 if (!strcmp (key, "rtpmap"))
1714 if (!strcmp (key, "control"))
1716 if (!strcmp (key, "range"))
1719 /* string must be valid UTF8 */
1720 if (!g_utf8_validate (attr->value, -1, NULL))
1723 if (!g_str_has_prefix (key, "x-"))
1724 tofree = key = g_strdup_printf ("a-%s", key);
1728 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1729 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1735 static const gchar *
1736 rtsp_get_attribute_for_pt (const GstSDPMedia * media, const gchar * name,
1745 if ((attr = gst_sdp_media_get_attribute_val_n (media, name, i)) == NULL)
1748 if (sscanf (attr, "%d ", &val) != 1)
1758 * Mapping of caps to and from SDP fields:
1760 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1761 * a=fmtp:<payload> <param>[=<value>];...
1764 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1767 const gchar *rtpmap;
1771 gchar *params = NULL;
1777 /* get and parse rtpmap */
1778 rtpmap = rtsp_get_attribute_for_pt (media, "rtpmap", pt);
1780 /* dynamic payloads need rtpmap or we fail */
1781 if (rtpmap == NULL && pt >= 96)
1784 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
1786 g_warning ("error parsing rtpmap, ignoring");
1789 /* check if we have a rate, if not, we need to look up the rate from the
1790 * default rates based on the payload types. */
1792 const GstRTPPayloadInfo *info;
1794 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
1795 /* dynamic types, use media and encoding_name */
1796 tmp = g_ascii_strdown (media->media, -1);
1797 info = gst_rtp_payload_info_for_name (tmp, name);
1800 /* static types, use payload type */
1801 info = gst_rtp_payload_info_for_pt (pt);
1805 if ((rate = info->clock_rate) == 0)
1808 /* we fail if we cannot find one */
1813 tmp = g_ascii_strdown (media->media, -1);
1814 caps = gst_caps_new_simple ("application/x-unknown",
1815 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
1817 s = gst_caps_get_structure (caps, 0);
1819 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
1821 /* encoding name must be upper case */
1823 tmp = g_ascii_strup (name, -1);
1824 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
1828 /* params must be lower case */
1829 if (params != NULL) {
1830 tmp = g_ascii_strdown (params, -1);
1831 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
1835 /* parse optional fmtp: field */
1836 if ((fmtp = rtsp_get_attribute_for_pt (media, "fmtp", pt))) {
1842 /* p is now of the format <payload> <param>[=<value>];... */
1843 PARSE_INT (p, " ", payload);
1844 if (payload != -1 && payload == pt) {
1848 /* <param>[=<value>] are separated with ';' */
1849 pairs = g_strsplit (p, ";", 0);
1850 for (i = 0; pairs[i]; i++) {
1852 const gchar *val, *key;
1854 /* the key may not have a '=', the value can have other '='s */
1855 valpos = strstr (pairs[i], "=");
1857 /* we have a '=' and thus a value, remove the '=' with \0 */
1859 /* value is everything between '=' and ';'. We split the pairs at ;
1860 * boundaries so we can take the remainder of the value. Some servers
1861 * put spaces around the value which we strip off here. Alternatively
1862 * we could strip those spaces in the depayloaders should these spaces
1863 * actually carry any meaning in the future. */
1864 val = g_strstrip (valpos + 1);
1866 /* simple <param>;.. is translated into <param>=1;... */
1869 /* strip the key of spaces, convert key to lowercase but not the value. */
1870 key = g_strstrip (pairs[i]);
1871 if (strlen (key) > 1) {
1872 tmp = g_ascii_strdown (key, -1);
1873 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
1885 g_warning ("rtpmap type not given for dynamic payload %d", pt);
1890 g_warning ("rate unknown for payload type %d", pt);
1896 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1897 gint * rtpport, gint * rtcpport)
1900 GstStateChangeReturn ret;
1901 GstElement *udpsrc0, *udpsrc1;
1902 gint tmp_rtp, tmp_rtcp;
1906 src = stream->parent;
1912 /* Start at next port */
1913 tmp_rtp = src->next_port_num;
1915 if (stream->is_ipv6)
1916 host = "udp://[::0]";
1918 host = "udp://0.0.0.0";
1920 /* try to allocate 2 UDP ports, the RTP port should be an even
1921 * number and the RTCP port should be the next (uneven) port */
1924 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1925 tmp_rtp >= src->client_port_range.max)
1928 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1929 if (udpsrc0 == NULL)
1930 goto no_udp_protocol;
1931 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1933 if (src->udp_buffer_size != 0)
1934 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
1937 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
1938 if (ret == GST_STATE_CHANGE_FAILURE) {
1940 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
1943 if (++count > src->retry)
1946 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1947 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1948 gst_object_unref (udpsrc0);
1951 GST_DEBUG_OBJECT (src, "retry %d", count);
1954 goto no_udp_protocol;
1957 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
1958 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
1960 /* check if port is even */
1961 if ((tmp_rtp & 0x01) != 0) {
1962 /* port not even, close and allocate another */
1963 if (++count > src->retry)
1966 GST_DEBUG_OBJECT (src, "RTP port not even");
1968 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1969 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1970 gst_object_unref (udpsrc0);
1973 GST_DEBUG_OBJECT (src, "retry %d", count);
1978 /* allocate port+1 for RTCP now */
1979 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1980 if (udpsrc1 == NULL)
1981 goto no_udp_rtcp_protocol;
1984 tmp_rtcp = tmp_rtp + 1;
1985 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
1988 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
1990 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
1991 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
1992 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
1993 if (ret == GST_STATE_CHANGE_FAILURE) {
1994 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
1996 if (++count > src->retry)
1999 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
2000 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2001 gst_object_unref (udpsrc0);
2004 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
2005 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2006 gst_object_unref (udpsrc1);
2010 GST_DEBUG_OBJECT (src, "retry %d", count);
2014 /* all fine, do port check */
2015 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
2016 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
2018 /* this should not happen... */
2019 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
2022 /* we keep these elements, we configure all in configure_transport when the
2023 * server told us to really use the UDP ports. */
2024 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
2025 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
2026 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
2027 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
2029 /* keep track of next available port number when we have a range
2031 if (src->next_port_num != 0)
2032 src->next_port_num = tmp_rtcp + 1;
2039 GST_DEBUG_OBJECT (src, "could not get UDP source");
2044 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
2048 no_udp_rtcp_protocol:
2050 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
2055 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
2056 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
2062 gst_element_set_state (udpsrc0, GST_STATE_NULL);
2063 gst_object_unref (udpsrc0);
2066 gst_element_set_state (udpsrc1, GST_STATE_NULL);
2067 gst_object_unref (udpsrc1);
2074 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
2079 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
2081 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2082 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2085 for (i = 0; i < 2; i++) {
2086 if (stream->udpsrc[i])
2087 gst_element_set_state (stream->udpsrc[i], state);
2093 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
2100 event = gst_event_new_flush_start ();
2101 GST_DEBUG_OBJECT (src, "start flush");
2103 state = GST_STATE_PAUSED;
2105 event = gst_event_new_flush_stop (FALSE);
2106 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2109 state = GST_STATE_PLAYING;
2111 state = GST_STATE_PAUSED;
2113 gst_rtspsrc_push_event (src, event);
2114 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2115 gst_rtspsrc_set_state (src, state);
2118 static GstRTSPResult
2119 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2120 GstRTSPMessage * message, GTimeVal * timeout)
2125 ret = gst_rtsp_connection_send (conn, message, timeout);
2127 ret = GST_RTSP_ERROR;
2132 static GstRTSPResult
2133 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2134 GstRTSPMessage * message, GTimeVal * timeout)
2139 ret = gst_rtsp_connection_receive (conn, message, timeout);
2141 ret = GST_RTSP_ERROR;
2147 gst_rtspsrc_get_position (GstRTSPSrc * src)
2152 query = gst_query_new_position (GST_FORMAT_TIME);
2153 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2154 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2155 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2159 if (stream->srcpad) {
2160 if (gst_pad_query (stream->srcpad, query)) {
2161 gst_query_parse_position (query, &fmt, &pos);
2162 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2163 GST_TIME_ARGS (pos));
2164 src->last_pos = pos;
2174 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
2176 src->state = GST_RTSP_STATE_SEEKING;
2177 /* PLAY will add the range header now. */
2178 src->need_range = TRUE;
2184 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2189 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2191 gboolean flush, skip;
2194 GstSegment seeksegment = { 0, };
2198 GST_DEBUG_OBJECT (src, "doing seek with event");
2200 gst_event_parse_seek (event, &rate, &format, &flags,
2201 &cur_type, &cur, &stop_type, &stop);
2203 /* no negative rates yet */
2207 /* we need TIME format */
2208 if (format != src->segment.format)
2211 GST_DEBUG_OBJECT (src, "doing seek without event");
2213 cur_type = GST_SEEK_TYPE_SET;
2214 stop_type = GST_SEEK_TYPE_SET;
2217 /* get flush flag */
2218 flush = flags & GST_SEEK_FLAG_FLUSH;
2219 skip = flags & GST_SEEK_FLAG_SKIP;
2221 /* now we need to make sure the streaming thread is stopped. We do this by
2222 * either sending a FLUSH_START event downstream which will cause the
2223 * streaming thread to stop with a WRONG_STATE.
2224 * For a non-flushing seek we simply pause the task, which will happen as soon
2225 * as it completes one iteration (and thus might block when the sink is
2226 * blocking in preroll). */
2228 GST_DEBUG_OBJECT (src, "starting flush");
2229 gst_rtspsrc_flush (src, TRUE, FALSE);
2232 gst_task_pause (src->task);
2236 /* we should now be able to grab the streaming thread because we stopped it
2237 * with the above flush/pause code */
2238 GST_RTSP_STREAM_LOCK (src);
2240 GST_DEBUG_OBJECT (src, "stopped streaming");
2242 /* copy segment, we need this because we still need the old
2243 * segment when we close the current segment. */
2244 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2246 /* configure the seek parameters in the seeksegment. We will then have the
2247 * right values in the segment to perform the seek */
2249 GST_DEBUG_OBJECT (src, "configuring seek");
2250 gst_segment_do_seek (&seeksegment, rate, format, flags,
2251 cur_type, cur, stop_type, stop, &update);
2254 /* figure out the last position we need to play. If it's configured (stop !=
2255 * -1), use that, else we play until the total duration of the file */
2256 if ((stop = seeksegment.stop) == -1)
2257 stop = seeksegment.duration;
2259 playing = (src->state == GST_RTSP_STATE_PLAYING);
2261 /* if we were playing, pause first */
2263 /* obtain current position in case seek fails */
2264 gst_rtspsrc_get_position (src);
2265 gst_rtspsrc_pause (src, FALSE);
2269 gst_rtspsrc_do_seek (src, &seeksegment);
2271 /* and continue playing */
2273 gst_rtspsrc_play (src, &seeksegment, FALSE);
2275 /* prepare for streaming again */
2277 /* if we started flush, we stop now */
2278 GST_DEBUG_OBJECT (src, "stopping flush");
2279 gst_rtspsrc_flush (src, FALSE, playing);
2282 /* now we did the seek and can activate the new segment values */
2283 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2285 /* if we're doing a segment seek, post a SEGMENT_START message */
2286 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2287 gst_element_post_message (GST_ELEMENT_CAST (src),
2288 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2289 src->segment.format, src->segment.position));
2292 /* now create the newsegment */
2293 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2294 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2297 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2298 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2299 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2300 stream->discont = TRUE;
2303 GST_RTSP_STREAM_UNLOCK (src);
2310 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2315 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2321 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2325 gboolean res = TRUE;
2328 src = GST_RTSPSRC_CAST (parent);
2330 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2331 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2333 switch (GST_EVENT_TYPE (event)) {
2334 case GST_EVENT_SEEK:
2335 res = gst_rtspsrc_perform_seek (src, event);
2339 case GST_EVENT_NAVIGATION:
2340 case GST_EVENT_LATENCY:
2348 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2349 res = gst_pad_send_event (target, event);
2350 gst_object_unref (target);
2352 gst_event_unref (event);
2355 gst_event_unref (event);
2361 /* this is the final event function we receive on the internal source pad when
2362 * we deal with TCP connections */
2364 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2369 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2371 switch (GST_EVENT_TYPE (event)) {
2372 case GST_EVENT_SEEK:
2374 case GST_EVENT_NAVIGATION:
2375 case GST_EVENT_LATENCY:
2377 gst_event_unref (event);
2384 /* this is the final query function we receive on the internal source pad when
2385 * we deal with TCP connections */
2387 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2391 gboolean res = TRUE;
2393 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2395 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2396 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2398 switch (GST_QUERY_TYPE (query)) {
2399 case GST_QUERY_POSITION:
2404 case GST_QUERY_DURATION:
2408 gst_query_parse_duration (query, &format, NULL);
2411 case GST_FORMAT_TIME:
2412 gst_query_set_duration (query, format, src->segment.duration);
2420 case GST_QUERY_LATENCY:
2422 /* we are live with a min latency of 0 and unlimited max latency, this
2423 * result will be updated by the session manager if there is any. */
2424 gst_query_set_latency (query, TRUE, 0, -1);
2434 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2436 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2440 gboolean res = FALSE;
2442 src = GST_RTSPSRC_CAST (parent);
2444 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2445 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2447 switch (GST_QUERY_TYPE (query)) {
2448 case GST_QUERY_DURATION:
2452 gst_query_parse_duration (query, &format, NULL);
2455 case GST_FORMAT_TIME:
2456 gst_query_set_duration (query, format, src->segment.duration);
2464 case GST_QUERY_SEEKING:
2468 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2469 if (format == GST_FORMAT_TIME) {
2471 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2473 /* seeking without duration is unlikely */
2474 seekable = seekable && src->seekable && src->segment.duration &&
2475 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2477 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2478 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2479 src->segment.start, src->segment.stop);
2488 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2490 gst_query_set_uri (query, uri);
2498 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2500 /* forward the query to the proxy target pad */
2502 res = gst_pad_query (target, query);
2503 gst_object_unref (target);
2512 /* callback for RTCP messages to be sent to the server when operating in TCP
2514 static GstFlowReturn
2515 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2518 GstRTSPStream *stream;
2519 GstFlowReturn res = GST_FLOW_OK;
2524 GstRTSPMessage message = { 0 };
2525 GstRTSPConnection *conn;
2527 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2528 src = stream->parent;
2530 gst_buffer_map (buffer, &map, GST_MAP_READ);
2534 gst_rtsp_message_init_data (&message, stream->channel[1]);
2536 /* lend the body data to the message */
2537 gst_rtsp_message_take_body (&message, data, size);
2539 if (stream->conninfo.connection)
2540 conn = stream->conninfo.connection;
2542 conn = src->conninfo.connection;
2544 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2545 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2546 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2548 /* and steal it away again because we will free it when unreffing the
2550 gst_rtsp_message_steal_body (&message, &data, &size);
2551 gst_rtsp_message_unset (&message);
2553 gst_buffer_unmap (buffer, &map);
2554 gst_buffer_unref (buffer);
2559 static GstPadProbeReturn
2560 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2562 GstRTSPSrc *src = user_data;
2564 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2565 GST_DEBUG_PAD_NAME (pad));
2567 /* activate the streams */
2568 GST_OBJECT_LOCK (src);
2569 if (!src->need_activate)
2572 src->need_activate = FALSE;
2573 GST_OBJECT_UNLOCK (src);
2575 gst_rtspsrc_activate_streams (src);
2577 return GST_PAD_PROBE_OK;
2581 GST_OBJECT_UNLOCK (src);
2582 return GST_PAD_PROBE_OK;
2586 /* this callback is called when the session manager generated a new src pad with
2587 * payloaded RTP packets. We simply ghost the pad here. */
2589 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2592 GstPadTemplate *template;
2595 GstRTSPStream *stream;
2598 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2600 GST_RTSP_STATE_LOCK (src);
2602 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2603 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2604 goto unknown_stream;
2606 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2608 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2610 goto unknown_stream;
2613 stream->ssrc = ssrc;
2615 /* we'll add it later see below */
2616 stream->added = TRUE;
2618 /* check if we added all streams */
2620 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2621 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2623 GST_DEBUG_OBJECT (src, "stream %p, container %d, disabled %d, added %d",
2624 ostream, ostream->container, ostream->disabled, ostream->added);
2626 /* a container stream only needs one pad added. Also disabled streams don't
2628 if (!ostream->container && !ostream->disabled && !ostream->added) {
2633 GST_RTSP_STATE_UNLOCK (src);
2635 /* create a new pad we will use to stream to */
2636 template = gst_static_pad_template_get (&rtptemplate);
2637 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2638 gst_object_unref (template);
2641 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2642 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2643 gst_pad_set_active (stream->srcpad, TRUE);
2644 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2647 GST_DEBUG_OBJECT (src, "We added all streams");
2648 /* when we get here, all stream are added and we can fire the no-more-pads
2650 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2658 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2659 GST_RTSP_STATE_UNLOCK (src);
2666 stream_get_caps_for_pt (GstRTSPStream * stream, guint pt)
2670 len = stream->ptmap->len;
2671 for (i = 0; i < len; i++) {
2672 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
2680 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2682 GstRTSPStream *stream;
2685 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2687 GST_RTSP_STATE_LOCK (src);
2688 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2690 goto unknown_stream;
2692 if ((caps = stream_get_caps_for_pt (stream, pt)))
2693 gst_caps_ref (caps);
2694 GST_RTSP_STATE_UNLOCK (src);
2700 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2701 GST_RTSP_STATE_UNLOCK (src);
2707 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2709 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2715 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2721 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2727 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2729 GstRTSPSrc *src = stream->parent;
2732 g_object_get (source, "ssrc", &ssrc, NULL);
2734 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2735 ssrc, stream->ssrc, stream->id);
2737 if (ssrc == stream->ssrc)
2738 gst_rtspsrc_do_stream_eos (src, stream);
2742 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2744 GstRTSPSrc *src = stream->parent;
2747 g_object_get (source, "ssrc", &ssrc, NULL);
2749 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2750 ssrc, stream->ssrc, stream->id);
2752 if (ssrc == stream->ssrc)
2753 gst_rtspsrc_do_stream_eos (src, stream);
2757 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2759 GstRTSPStream *stream;
2761 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2763 /* get stream for session */
2764 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2766 gst_rtspsrc_do_stream_eos (src, stream);
2771 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2773 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2778 set_manager_buffer_mode (GstRTSPSrc * src)
2780 GObjectClass *klass;
2782 if (src->manager == NULL)
2785 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2787 if (!g_object_class_find_property (klass, "buffer-mode"))
2790 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2791 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2796 GST_DEBUG_OBJECT (src,
2797 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
2799 if (src->provided_clock) {
2800 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
2802 if (clock == src->provided_clock) {
2803 GST_DEBUG_OBJECT (src, "selected synced");
2804 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
2807 gst_object_unref (clock);
2812 /* Otherwise fall-through and use another buffer mode */
2814 gst_object_unref (clock);
2817 GST_DEBUG_OBJECT (src, "auto buffering mode");
2818 if (src->use_buffering) {
2819 GST_DEBUG_OBJECT (src, "selected buffer");
2820 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
2822 GST_DEBUG_OBJECT (src, "selected slave");
2823 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2827 /* try to get and configure a manager */
2829 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
2830 GstRTSPTransport * transport)
2832 const gchar *manager;
2834 GstStateChangeReturn ret;
2836 /* find a manager */
2837 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
2841 GST_DEBUG_OBJECT (src, "using manager %s", manager);
2843 /* configure the manager */
2844 if (src->manager == NULL) {
2845 GObjectClass *klass;
2847 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
2849 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
2853 goto use_no_manager;
2855 if (!(src->manager = gst_element_factory_make (manager, "manager")))
2856 goto manager_failed;
2859 /* we manage this element */
2860 gst_element_set_locked_state (src->manager, TRUE);
2861 gst_bin_add (GST_BIN_CAST (src), src->manager);
2863 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
2864 if (ret == GST_STATE_CHANGE_FAILURE)
2865 goto start_manager_failure;
2867 g_object_set (src->manager, "latency", src->latency, NULL);
2869 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2871 if (g_object_class_find_property (klass, "ntp-sync")) {
2872 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
2875 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
2876 g_object_set (src->manager, "use-pipeline-clock",
2877 src->use_pipeline_clock, NULL);
2880 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
2881 g_object_set (src->manager, "sdes", src->sdes, NULL);
2884 if (g_object_class_find_property (klass, "drop-on-latency")) {
2885 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
2889 /* buffer mode pauses are handled by adding offsets to buffer times,
2890 * but some depayloaders may have a hard time syncing output times
2891 * with such input times, e.g. container ones, most notably ASF */
2892 /* TODO alternatives are having an event that indicates these shifts,
2893 * or having rtsp extensions provide suggestion on buffer mode */
2894 /* valid duration implies not likely live pipeline,
2895 * so slaving in jitterbuffer does not make much sense
2896 * (and might mess things up due to bursts) */
2897 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
2898 src->segment.duration && !stream->container) {
2899 src->use_buffering = TRUE;
2901 src->use_buffering = FALSE;
2904 set_manager_buffer_mode (src);
2906 /* connect to signals */
2907 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
2909 src->manager_sig_id =
2910 g_signal_connect (src->manager, "pad-added",
2911 (GCallback) new_manager_pad, src);
2912 src->manager_ptmap_id =
2913 g_signal_connect (src->manager, "request-pt-map",
2914 (GCallback) request_pt_map, src);
2916 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
2919 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_NEW_MANAGER], 0,
2923 /* we stream directly to the manager, get some pads. Each RTSP stream goes
2924 * into a separate RTP session. */
2925 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
2926 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
2928 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
2929 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
2932 /* now configure the bandwidth in the manager */
2933 if (g_signal_lookup ("get-internal-session",
2934 G_OBJECT_TYPE (src->manager)) != 0) {
2935 GObject *rtpsession;
2937 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
2940 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
2942 stream->session = rtpsession;
2944 if (stream->as_bandwidth != -1) {
2945 GST_INFO_OBJECT (src, "setting AS: %f",
2946 (gdouble) (stream->as_bandwidth * 1000));
2947 g_object_set (rtpsession, "bandwidth",
2948 (gdouble) (stream->as_bandwidth * 1000), NULL);
2950 if (stream->rr_bandwidth != -1) {
2951 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
2952 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
2955 if (stream->rs_bandwidth != -1) {
2956 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
2957 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
2961 g_object_set (rtpsession, "probation", src->probation, NULL);
2963 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2965 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
2967 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
2969 g_signal_connect (rtpsession, "on-ssrc-active",
2970 (GCallback) on_ssrc_active, stream);
2981 GST_DEBUG_OBJECT (src, "cannot get a session manager");
2986 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
2989 start_manager_failure:
2991 GST_DEBUG_OBJECT (src, "could not start session manager");
2996 /* free the UDP sources allocated when negotiating a transport.
2997 * This function is called when the server negotiated to a transport where the
2998 * UDP sources are not needed anymore, such as TCP or multicast. */
3000 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
3004 for (i = 0; i < 2; i++) {
3005 if (stream->udpsrc[i]) {
3006 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
3007 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
3008 gst_object_unref (stream->udpsrc[i]);
3009 stream->udpsrc[i] = NULL;
3014 /* for TCP, create pads to send and receive data to and from the manager and to
3015 * intercept various events and queries
3018 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
3019 GstRTSPTransport * transport, GstPad ** outpad)
3022 GstPadTemplate *template;
3023 GstPad *pad0, *pad1;
3025 /* configure for interleaved delivery, nothing needs to be done
3026 * here, the loop function will call the chain functions of the
3027 * session manager. */
3028 stream->channel[0] = transport->interleaved.min;
3029 stream->channel[1] = transport->interleaved.max;
3030 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
3031 stream->channel[0], stream->channel[1]);
3033 /* we can remove the allocated UDP ports now */
3034 gst_rtspsrc_stream_free_udp (stream);
3036 /* no session manager, send data to srcpad directly */
3037 if (!stream->channelpad[0]) {
3038 GST_DEBUG_OBJECT (src, "no manager, creating pad");
3040 /* create a new pad we will use to stream to */
3041 name = g_strdup_printf ("stream_%u", stream->id);
3042 template = gst_static_pad_template_get (&rtptemplate);
3043 stream->channelpad[0] = gst_pad_new_from_template (template, name);
3044 gst_object_unref (template);
3047 /* set caps and activate */
3048 gst_pad_use_fixed_caps (stream->channelpad[0]);
3049 gst_pad_set_active (stream->channelpad[0], TRUE);
3051 *outpad = gst_object_ref (stream->channelpad[0]);
3053 GST_DEBUG_OBJECT (src, "using manager source pad");
3055 template = gst_static_pad_template_get (&anysrctemplate);
3057 /* allocate pads for sending the channel data into the manager */
3058 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
3059 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
3060 gst_object_unref (stream->channelpad[0]);
3061 stream->channelpad[0] = pad0;
3062 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
3063 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
3064 gst_pad_set_element_private (pad0, src);
3065 gst_pad_set_active (pad0, TRUE);
3067 if (stream->channelpad[1]) {
3068 /* if we have a sinkpad for the other channel, create a pad and link to the
3070 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
3071 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
3072 gst_pad_link_full (pad1, stream->channelpad[1],
3073 GST_PAD_LINK_CHECK_NOTHING);
3074 gst_object_unref (stream->channelpad[1]);
3075 stream->channelpad[1] = pad1;
3076 gst_pad_set_active (pad1, TRUE);
3078 gst_object_unref (template);
3080 /* setup RTCP transport back to the server if we have to. */
3081 if (src->manager && src->do_rtcp) {
3084 template = gst_static_pad_template_get (&anysinktemplate);
3086 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
3087 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
3088 gst_pad_set_element_private (stream->rtcppad, stream);
3089 gst_pad_set_active (stream->rtcppad, TRUE);
3091 /* get session RTCP pad */
3092 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3093 pad = gst_element_get_request_pad (src->manager, name);
3098 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3099 gst_object_unref (pad);
3102 gst_object_unref (template);
3108 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
3109 GstRTSPTransport * transport, const gchar ** destination, gint * min,
3110 gint * max, guint * ttl)
3112 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
3114 if (!(*destination = transport->destination))
3115 *destination = stream->destination;
3118 /* transport first */
3119 *min = transport->port.min;
3120 *max = transport->port.max;
3121 if (*min == -1 && *max == -1) {
3122 /* then try from SDP */
3123 if (stream->port != 0) {
3124 *min = stream->port;
3125 *max = stream->port + 1;
3131 if (!(*ttl = transport->ttl))
3136 /* first take the source, then the endpoint to figure out where to send
3138 if (!(*destination = transport->source)) {
3139 if (src->conninfo.connection)
3140 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3141 else if (stream->conninfo.connection)
3143 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3147 /* for unicast we only expect the ports here */
3148 *min = transport->server_port.min;
3149 *max = transport->server_port.max;
3154 /* For multicast create UDP sources and join the multicast group. */
3156 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3157 GstRTSPTransport * transport, GstPad ** outpad)
3160 const gchar *destination;
3163 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3165 /* we can remove the allocated UDP ports now */
3166 gst_rtspsrc_stream_free_udp (stream);
3168 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3171 /* we need a destination now */
3172 if (destination == NULL)
3173 goto no_destination;
3175 /* we really need ports now or we won't be able to receive anything at all */
3176 if (min == -1 && max == -1)
3179 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3180 destination, min, max);
3182 /* creating UDP source for RTP */
3184 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3186 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3188 if (stream->udpsrc[0] == NULL)
3191 /* take ownership */
3192 gst_object_ref_sink (stream->udpsrc[0]);
3194 if (src->udp_buffer_size != 0)
3195 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3196 src->udp_buffer_size, NULL);
3198 if (src->multi_iface != NULL)
3199 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3200 src->multi_iface, NULL);
3203 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3204 gst_element_set_state (stream->udpsrc[0], GST_STATE_PAUSED);
3207 /* creating another UDP source for RTCP */
3211 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3213 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3215 if (stream->udpsrc[1] == NULL)
3218 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3219 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3220 gst_caps_unref (caps);
3222 /* take ownership */
3223 gst_object_ref_sink (stream->udpsrc[1]);
3225 if (src->multi_iface != NULL)
3226 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3227 src->multi_iface, NULL);
3229 gst_element_set_state (stream->udpsrc[1], GST_STATE_PAUSED);
3236 GST_DEBUG_OBJECT (src, "no UDP source element found");
3241 GST_DEBUG_OBJECT (src, "no destination found");
3246 GST_DEBUG_OBJECT (src, "no ports found");
3251 /* configure the remainder of the UDP ports */
3253 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3254 GstRTSPTransport * transport, GstPad ** outpad)
3256 /* we manage the UDP elements now. For unicast, the UDP sources where
3257 * allocated in the stream when we suggested a transport. */
3258 if (stream->udpsrc[0]) {
3259 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3260 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3262 GST_DEBUG_OBJECT (src, "setting up UDP source");
3264 /* configure a timeout on the UDP port. When the timeout message is
3265 * posted, we assume UDP transport is not possible. We reconnect using TCP
3267 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3268 src->udp_timeout * 1000, NULL);
3270 /* get output pad of the UDP source. */
3271 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3273 /* save it so we can unblock */
3274 stream->blockedpad = *outpad;
3276 /* configure pad block on the pad. As soon as there is dataflow on the
3277 * UDP source, we know that UDP is not blocked by a firewall and we can
3278 * configure all the streams to let the application autoplug decoders. */
3280 gst_pad_add_probe (stream->blockedpad,
3281 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, pad_blocked, src, NULL);
3283 if (stream->channelpad[0]) {
3284 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3285 /* configure for UDP delivery, we need to connect the UDP pads to
3286 * the session plugin. */
3287 gst_pad_link_full (*outpad, stream->channelpad[0],
3288 GST_PAD_LINK_CHECK_NOTHING);
3289 gst_object_unref (*outpad);
3291 /* we connected to pad-added signal to get pads from the manager */
3293 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3298 if (stream->udpsrc[1]) {
3301 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3302 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3304 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3305 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3306 gst_caps_unref (caps);
3308 if (stream->channelpad[1]) {
3311 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3313 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3314 gst_pad_link_full (pad, stream->channelpad[1],
3315 GST_PAD_LINK_CHECK_NOTHING);
3316 gst_object_unref (pad);
3318 /* leave unlinked */
3324 /* configure the UDP sink back to the server for status reports */
3326 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3327 GstRTSPStream * stream, GstRTSPTransport * transport)
3330 gint rtp_port, rtcp_port;
3331 gboolean do_rtp, do_rtcp;
3332 const gchar *destination;
3337 /* get transport info */
3338 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3339 &rtp_port, &rtcp_port, &ttl);
3341 /* see what we need to do */
3342 do_rtp = (rtp_port != -1);
3343 /* it's possible that the server does not want us to send RTCP in which case
3345 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3347 /* we need a destination when we have RTP or RTCP ports */
3348 if (destination == NULL && (do_rtp || do_rtcp))
3349 goto no_destination;
3351 /* try to construct the fakesrc to the RTP port of the server to open up any
3354 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3357 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3358 stream->udpsink[0] =
3359 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3361 if (stream->udpsink[0] == NULL)
3362 goto no_sink_element;
3364 /* don't join multicast group, we will have the source socket do that */
3365 /* no sync or async state changes needed */
3366 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3367 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3369 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3371 if (stream->udpsrc[0]) {
3372 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3373 * so that NAT firewalls will open a hole for us */
3374 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3375 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3376 /* configure socket and make sure udpsink does not close it when shutting
3377 * down, it belongs to udpsrc after all. */
3378 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3379 "close-socket", FALSE, NULL);
3380 g_object_unref (socket);
3383 /* the source for the dummy packets to open up NAT */
3384 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3385 if (stream->fakesrc == NULL)
3386 goto no_fakesrc_element;
3388 /* random data in 5 buffers, a size of 200 bytes should be fine */
3389 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3390 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3392 /* we don't want to consider this a sink */
3393 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
3395 /* keep everything locked */
3396 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3397 gst_element_set_locked_state (stream->fakesrc, TRUE);
3399 gst_object_ref (stream->udpsink[0]);
3400 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3401 gst_object_ref (stream->fakesrc);
3402 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3404 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3405 "sink", GST_PAD_LINK_CHECK_NOTHING);
3408 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3411 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3412 stream->udpsink[1] =
3413 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3415 if (stream->udpsink[1] == NULL)
3416 goto no_sink_element;
3418 /* don't join multicast group, we will have the source socket do that */
3419 /* no sync or async state changes needed */
3420 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3421 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3423 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3425 if (stream->udpsrc[1]) {
3426 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3427 * because some servers check the port number of where it sends RTCP to identify
3428 * the RTCP packets it receives */
3429 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3430 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3431 /* configure socket and make sure udpsink does not close it when shutting
3432 * down, it belongs to udpsrc after all. */
3433 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3434 "close-socket", FALSE, NULL);
3435 g_object_unref (socket);
3438 /* we don't want to consider this a sink */
3439 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3441 /* we keep this playing always */
3442 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3443 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3445 gst_object_ref (stream->udpsink[1]);
3446 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3448 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3450 /* get session RTCP pad */
3451 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3452 pad = gst_element_get_request_pad (src->manager, name);
3457 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3458 gst_object_unref (pad);
3467 GST_DEBUG_OBJECT (src, "no destination address specified");
3472 GST_DEBUG_OBJECT (src, "no UDP sink element found");
3477 GST_DEBUG_OBJECT (src, "no fakesrc element found");
3482 /* sets up all elements needed for streaming over the specified transport.
3483 * Does not yet expose the element pads, this will be done when there is actuall
3484 * dataflow detected, which might never happen when UDP is blocked in a
3485 * firewall, for example.
3488 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3489 GstRTSPTransport * transport)
3492 GstPad *outpad = NULL;
3493 GstPadTemplate *template;
3495 const gchar *media_type;
3498 src = stream->parent;
3500 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3502 /* get the proper media type for this stream now */
3503 if (gst_rtsp_transport_get_media_type (transport, &media_type) < 0)
3504 goto unknown_transport;
3506 goto unknown_transport;
3508 /* configure the final media type */
3509 GST_DEBUG_OBJECT (src, "setting media type to %s", media_type);
3511 len = stream->ptmap->len;
3512 for (i = 0; i < len; i++) {
3514 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
3516 if (item->caps == NULL)
3519 s = gst_caps_get_structure (item->caps, 0);
3520 gst_structure_set_name (s, media_type);
3523 /* try to get and configure a manager, channelpad[0-1] will be configured with
3524 * the pads for the manager, or NULL when no manager is needed. */
3525 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3528 switch (transport->lower_transport) {
3529 case GST_RTSP_LOWER_TRANS_TCP:
3530 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3531 goto transport_failed;
3533 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3534 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3535 goto transport_failed;
3536 /* fallthrough, the rest is the same for UDP and MCAST */
3537 case GST_RTSP_LOWER_TRANS_UDP:
3538 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3539 goto transport_failed;
3540 /* configure udpsinks back to the server for RTCP messages and for the
3541 * dummy RTP messages to open NAT. */
3542 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3543 goto transport_failed;
3546 goto unknown_transport;
3550 GST_DEBUG_OBJECT (src, "creating ghostpad");
3552 gst_pad_use_fixed_caps (outpad);
3554 /* create ghostpad, don't add just yet, this will be done when we activate
3556 name = g_strdup_printf ("stream_%u", stream->id);
3557 template = gst_static_pad_template_get (&rtptemplate);
3558 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3559 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3560 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3561 gst_object_unref (template);
3564 gst_object_unref (outpad);
3566 /* mark pad as ok */
3567 stream->last_ret = GST_FLOW_OK;
3574 GST_DEBUG_OBJECT (src, "failed to configure transport");
3579 GST_DEBUG_OBJECT (src, "unknown transport");
3584 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3589 /* send a couple of dummy random packets on the receiver RTP port to the server,
3590 * this should make a firewall think we initiated the data transfer and
3591 * hopefully allow packets to go from the sender port to our RTP receiver port */
3593 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3597 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3600 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3601 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3603 if (stream->fakesrc && stream->udpsink[0]) {
3604 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3605 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3606 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3607 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3608 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3614 /* Adds the source pads of all configured streams to the element.
3615 * This code is performed when we detected dataflow.
3617 * We detect dataflow from either the _loop function or with pad probes on the
3621 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3625 GST_DEBUG_OBJECT (src, "activating streams");
3627 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3628 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3630 if (stream->udpsrc[0]) {
3631 /* remove timeout, we are streaming now and timeouts will be handled by
3632 * the session manager and jitter buffer */
3633 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3635 if (stream->srcpad) {
3636 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3637 gst_pad_set_active (stream->srcpad, TRUE);
3639 /* if we don't have a session manager, set the caps now. If we have a
3640 * session, we will get a notification of the pad and the caps. */
3641 if (!src->manager) {
3644 caps = stream_get_caps_for_pt (stream, stream->default_pt);
3645 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3646 gst_pad_set_caps (stream->srcpad, caps);
3649 if (!stream->added) {
3650 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3651 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3652 stream->added = TRUE;
3657 /* unblock all pads */
3658 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3659 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3661 if (stream->blockid) {
3662 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3663 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3664 stream->blockid = 0;
3672 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
3673 gboolean reset_manager)
3676 guint64 start, stop;
3677 gdouble play_speed, play_scale;
3679 GST_DEBUG_OBJECT (src, "configuring stream caps");
3681 start = segment->position;
3682 stop = segment->duration;
3683 play_speed = segment->rate;
3684 play_scale = segment->applied_rate;
3686 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3687 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3690 len = stream->ptmap->len;
3691 for (j = 0; j < len; j++) {
3693 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, j);
3695 if (item->caps == NULL)
3698 caps = gst_caps_make_writable (item->caps);
3700 if (stream->timebase != -1)
3701 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3702 (guint) stream->timebase, NULL);
3703 if (stream->seqbase != -1)
3704 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3705 (guint) stream->seqbase, NULL);
3706 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3708 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3709 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3710 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3713 GST_DEBUG_OBJECT (src, "stream %p, pt %d, caps %" GST_PTR_FORMAT, stream,
3717 if (reset_manager && src->manager) {
3718 GST_DEBUG_OBJECT (src, "clear session");
3719 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3723 static GstFlowReturn
3724 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3729 /* store the value */
3730 stream->last_ret = ret;
3732 /* if it's success we can return the value right away */
3733 if (ret == GST_FLOW_OK)
3736 /* any other error that is not-linked can be returned right
3738 if (ret != GST_FLOW_NOT_LINKED)
3741 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3742 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3743 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3745 ret = ostream->last_ret;
3746 /* some other return value (must be SUCCESS but we can return
3747 * other values as well) */
3748 if (ret != GST_FLOW_NOT_LINKED)
3751 /* if we get here, all other pads were unlinked and we return
3752 * NOT_LINKED then */
3758 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
3761 gboolean res = TRUE;
3763 /* only streams that have a connection to the outside world */
3764 if (stream->container || stream->disabled)
3767 if (stream->udpsrc[0]) {
3768 gst_event_ref (event);
3769 res = gst_element_send_event (stream->udpsrc[0], event);
3770 } else if (stream->channelpad[0]) {
3771 gst_event_ref (event);
3772 if (GST_PAD_IS_SRC (stream->channelpad[0]))
3773 res = gst_pad_push_event (stream->channelpad[0], event);
3775 res = gst_pad_send_event (stream->channelpad[0], event);
3778 if (stream->udpsrc[1]) {
3779 gst_event_ref (event);
3780 res &= gst_element_send_event (stream->udpsrc[1], event);
3781 } else if (stream->channelpad[1]) {
3782 gst_event_ref (event);
3783 if (GST_PAD_IS_SRC (stream->channelpad[1]))
3784 res &= gst_pad_push_event (stream->channelpad[1], event);
3786 res &= gst_pad_send_event (stream->channelpad[1], event);
3790 gst_event_unref (event);
3796 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
3799 gboolean res = TRUE;
3801 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3802 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3804 gst_event_ref (event);
3805 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
3807 gst_event_unref (event);
3812 static GstRTSPResult
3813 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3818 if (info->connection == NULL) {
3819 if (info->url == NULL) {
3820 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
3821 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
3825 /* create connection */
3826 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
3827 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
3828 goto could_not_create;
3831 g_free (info->url_str);
3832 info->url_str = gst_rtsp_url_get_request_uri (info->url);
3834 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
3836 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
3837 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
3838 src->tls_validation_flags))
3839 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
3841 if (src->tls_database)
3842 gst_rtsp_connection_set_tls_database (info->connection,
3846 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
3847 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
3849 if (src->proxy_host) {
3850 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
3852 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
3857 if (!info->connected) {
3860 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
3861 ("Connecting to %s", info->location));
3862 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
3864 gst_rtsp_connection_connect (info->connection,
3865 src->ptcp_timeout)) < 0)
3866 goto could_not_connect;
3868 info->connected = TRUE;
3875 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
3880 gchar *str = gst_rtsp_strresult (res);
3881 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
3887 gchar *str = gst_rtsp_strresult (res);
3888 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
3894 static GstRTSPResult
3895 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
3898 GST_RTSP_STATE_LOCK (src);
3899 if (info->connected) {
3900 GST_DEBUG_OBJECT (src, "closing connection...");
3901 gst_rtsp_connection_close (info->connection);
3902 info->connected = FALSE;
3904 if (free && info->connection) {
3905 /* free connection */
3906 GST_DEBUG_OBJECT (src, "freeing connection...");
3907 gst_rtsp_connection_free (info->connection);
3908 info->connection = NULL;
3910 GST_RTSP_STATE_UNLOCK (src);
3914 static GstRTSPResult
3915 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3920 GST_DEBUG_OBJECT (src, "reconnecting connection...");
3921 gst_rtsp_conninfo_close (src, info, FALSE);
3922 res = gst_rtsp_conninfo_connect (src, info, async);
3928 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
3932 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
3933 GST_RTSP_STATE_LOCK (src);
3934 if (src->conninfo.connection && src->conninfo.flushing != flush) {
3935 GST_DEBUG_OBJECT (src, "connection flush");
3936 gst_rtsp_connection_flush (src->conninfo.connection, flush);
3937 src->conninfo.flushing = flush;
3939 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3940 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3941 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
3942 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
3943 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
3944 stream->conninfo.flushing = flush;
3947 GST_RTSP_STATE_UNLOCK (src);
3950 /* FIXME, handle server request, reply with OK, for now */
3951 static GstRTSPResult
3952 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
3953 GstRTSPMessage * request)
3955 GstRTSPMessage response = { 0 };
3958 GST_DEBUG_OBJECT (src, "got server request message");
3961 gst_rtsp_message_dump (request);
3963 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
3965 if (res == GST_RTSP_ENOTIMPL) {
3966 /* default implementation, send OK */
3967 GST_DEBUG_OBJECT (src, "prepare OK reply");
3969 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
3974 /* let app parse and reply */
3975 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
3976 0, request, &response);
3979 gst_rtsp_message_dump (&response);
3981 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
3985 gst_rtsp_message_unset (&response);
3986 } else if (res == GST_RTSP_EEOF)
3994 gst_rtsp_message_unset (&response);
3999 /* send server keep-alive */
4000 static GstRTSPResult
4001 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
4003 GstRTSPMessage request = { 0 };
4005 GstRTSPMethod method;
4006 const gchar *control;
4008 if (src->do_rtsp_keep_alive == FALSE) {
4009 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
4010 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4014 GST_DEBUG_OBJECT (src, "creating server keep-alive");
4016 /* find a method to use for keep-alive */
4017 if (src->methods & GST_RTSP_GET_PARAMETER)
4018 method = GST_RTSP_GET_PARAMETER;
4020 method = GST_RTSP_OPTIONS;
4022 control = get_aggregate_control (src);
4023 if (control == NULL)
4026 res = gst_rtsp_message_init_request (&request, method, control);
4031 gst_rtsp_message_dump (&request);
4034 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
4039 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
4040 gst_rtsp_message_unset (&request);
4047 GST_WARNING_OBJECT (src, "no control url to send keepalive");
4052 gchar *str = gst_rtsp_strresult (res);
4054 gst_rtsp_message_unset (&request);
4055 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
4056 ("Could not send keep-alive. (%s)", str));
4062 static GstFlowReturn
4063 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
4065 GstFlowReturn ret = GST_FLOW_OK;
4067 GstRTSPStream *stream;
4068 GstPad *outpad = NULL;
4075 channel = message->type_data.data.channel;
4077 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
4079 goto unknown_stream;
4081 if (channel == stream->channel[0]) {
4082 outpad = stream->channelpad[0];
4084 } else if (channel == stream->channel[1]) {
4085 outpad = stream->channelpad[1];
4091 /* take a look at the body to figure out what we have */
4092 gst_rtsp_message_get_body (message, &data, &size);
4094 goto invalid_length;
4096 /* channels are not correct on some servers, do extra check */
4097 if (data[1] >= 200 && data[1] <= 204) {
4098 /* hmm RTCP message switch to the RTCP pad of the same stream. */
4099 outpad = stream->channelpad[1];
4103 /* we have no clue what this is, just ignore then. */
4105 goto unknown_stream;
4107 /* take the message body for further processing */
4108 gst_rtsp_message_steal_body (message, &data, &size);
4110 /* strip the trailing \0 */
4113 buf = gst_buffer_new ();
4114 gst_buffer_append_memory (buf,
4115 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
4117 /* don't need message anymore */
4118 gst_rtsp_message_unset (message);
4120 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
4123 if (src->need_activate) {
4129 guint group_id = gst_util_group_id_next ();
4131 /* generate an SHA256 sum of the URI */
4132 cs = g_checksum_new (G_CHECKSUM_SHA256);
4133 uri = src->conninfo.location;
4134 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
4136 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4137 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4140 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4141 event = gst_event_new_stream_start (stream_id);
4142 gst_event_set_group_id (event, group_id);
4145 gst_rtspsrc_stream_push_event (src, ostream, event);
4147 g_checksum_free (cs);
4149 gst_rtspsrc_activate_streams (src);
4150 src->need_activate = FALSE;
4152 if ((event = src->start_segment) != NULL) {
4153 src->start_segment = NULL;
4154 gst_rtspsrc_push_event (src, event);
4157 if (src->base_time == -1) {
4158 /* Take current running_time. This timestamp will be put on
4159 * the first buffer of each stream because we are a live source and so we
4160 * timestamp with the running_time. When we are dealing with TCP, we also
4161 * only timestamp the first buffer (using the DISCONT flag) because a server
4162 * typically bursts data, for which we don't want to compensate by speeding
4163 * up the media. The other timestamps will be interpollated from this one
4164 * using the RTP timestamps. */
4165 GST_OBJECT_LOCK (src);
4166 if (GST_ELEMENT_CLOCK (src)) {
4168 GstClockTime base_time;
4170 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4171 base_time = GST_ELEMENT_CAST (src)->base_time;
4173 src->base_time = now - base_time;
4175 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4176 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4178 GST_OBJECT_UNLOCK (src);
4181 if (stream->discont && !is_rtcp) {
4182 /* mark first RTP buffer as discont */
4183 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4184 stream->discont = FALSE;
4185 /* first buffer gets the timestamp, other buffers are not timestamped and
4186 * their presentation time will be interpollated from the rtp timestamps. */
4187 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4188 GST_TIME_ARGS (src->base_time));
4190 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4193 /* chain to the peer pad */
4194 if (GST_PAD_IS_SINK (outpad))
4195 ret = gst_pad_chain (outpad, buf);
4197 ret = gst_pad_push (outpad, buf);
4200 /* combine all stream flows for the data transport */
4201 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4208 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4209 gst_rtsp_message_unset (message);
4214 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4215 ("Short message received, ignoring."));
4216 gst_rtsp_message_unset (message);
4221 static GstFlowReturn
4222 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4224 GstRTSPMessage message = { 0 };
4226 GstFlowReturn ret = GST_FLOW_OK;
4227 GTimeVal tv_timeout;
4230 /* get the next timeout interval */
4231 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4233 /* see if the timeout period expired */
4234 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4235 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4236 /* send keep-alive, only act on interrupt, a warning will be posted for
4238 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4240 /* get new timeout */
4241 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4244 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4245 tv_timeout.tv_sec, tv_timeout.tv_usec);
4247 /* protect the connection with the connection lock so that we can see when
4248 * we are finished doing server communication */
4250 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4251 &message, src->ptcp_timeout);
4255 GST_DEBUG_OBJECT (src, "we received a server message");
4257 case GST_RTSP_EINTR:
4258 /* we got interrupted this means we need to stop */
4260 case GST_RTSP_ETIMEOUT:
4261 /* no reply, send keep alive */
4262 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4263 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4267 /* go EOS when the server closed the connection */
4273 switch (message.type) {
4274 case GST_RTSP_MESSAGE_REQUEST:
4275 /* server sends us a request message, handle it */
4277 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4279 if (res == GST_RTSP_EEOF)
4282 goto handle_request_failed;
4284 case GST_RTSP_MESSAGE_RESPONSE:
4285 /* we ignore response messages */
4286 GST_DEBUG_OBJECT (src, "ignoring response message");
4288 gst_rtsp_message_dump (&message);
4290 case GST_RTSP_MESSAGE_DATA:
4291 GST_DEBUG_OBJECT (src, "got data message");
4292 ret = gst_rtspsrc_handle_data (src, &message);
4293 if (ret != GST_FLOW_OK)
4294 goto handle_data_failed;
4297 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4302 g_assert_not_reached ();
4307 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4308 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4309 ("The server closed the connection."));
4310 src->conninfo.connected = FALSE;
4311 gst_rtsp_message_unset (&message);
4312 return GST_FLOW_EOS;
4316 gst_rtsp_message_unset (&message);
4317 GST_DEBUG_OBJECT (src, "got interrupted");
4318 return GST_FLOW_FLUSHING;
4322 gchar *str = gst_rtsp_strresult (res);
4324 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4325 ("Could not receive message. (%s)", str));
4328 gst_rtsp_message_unset (&message);
4329 return GST_FLOW_ERROR;
4331 handle_request_failed:
4333 gchar *str = gst_rtsp_strresult (res);
4335 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4336 ("Could not handle server message. (%s)", str));
4338 gst_rtsp_message_unset (&message);
4339 return GST_FLOW_ERROR;
4343 GST_DEBUG_OBJECT (src, "could no handle data message");
4348 static GstFlowReturn
4349 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4352 GstRTSPMessage message = { 0 };
4356 GTimeVal tv_timeout;
4358 /* get the next timeout interval */
4359 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4361 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4362 (gint) tv_timeout.tv_sec);
4364 gst_rtsp_message_unset (&message);
4366 /* we should continue reading the TCP socket because the server might
4367 * send us requests. When the session timeout expires, we need to send a
4368 * keep-alive request to keep the session open. */
4369 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4370 &message, &tv_timeout);
4374 GST_DEBUG_OBJECT (src, "we received a server message");
4376 case GST_RTSP_EINTR:
4377 /* we got interrupted, see what we have to do */
4379 case GST_RTSP_ETIMEOUT:
4380 /* send keep-alive, ignore the result, a warning will be posted. */
4381 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4382 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4386 /* server closed the connection. not very fatal for UDP, reconnect and
4387 * see what happens. */
4388 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4389 ("The server closed the connection."));
4390 if (src->udp_reconnect) {
4392 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4399 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4401 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4402 ("Unhandled return value %d.", res));
4406 switch (message.type) {
4407 case GST_RTSP_MESSAGE_REQUEST:
4408 /* server sends us a request message, handle it */
4410 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4412 if (res == GST_RTSP_EEOF)
4415 goto handle_request_failed;
4417 case GST_RTSP_MESSAGE_RESPONSE:
4418 /* we ignore response and data messages */
4419 GST_DEBUG_OBJECT (src, "ignoring response message");
4421 gst_rtsp_message_dump (&message);
4422 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4423 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4424 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4425 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4426 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4433 case GST_RTSP_MESSAGE_DATA:
4434 /* we ignore response and data messages */
4435 GST_DEBUG_OBJECT (src, "ignoring data message");
4438 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4443 g_assert_not_reached ();
4445 /* we get here when the connection got interrupted */
4448 gst_rtsp_message_unset (&message);
4449 GST_DEBUG_OBJECT (src, "got interrupted");
4450 return GST_FLOW_FLUSHING;
4454 gchar *str = gst_rtsp_strresult (res);
4457 src->conninfo.connected = FALSE;
4458 if (res != GST_RTSP_EINTR) {
4459 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4460 ("Could not connect to server. (%s)", str));
4462 ret = GST_FLOW_ERROR;
4464 ret = GST_FLOW_FLUSHING;
4470 gchar *str = gst_rtsp_strresult (res);
4472 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4473 ("Could not receive message. (%s)", str));
4475 return GST_FLOW_ERROR;
4477 handle_request_failed:
4479 gchar *str = gst_rtsp_strresult (res);
4482 gst_rtsp_message_unset (&message);
4483 if (res != GST_RTSP_EINTR) {
4484 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4485 ("Could not handle server message. (%s)", str));
4487 ret = GST_FLOW_ERROR;
4489 ret = GST_FLOW_FLUSHING;
4495 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4496 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4497 ("The server closed the connection."));
4498 src->conninfo.connected = FALSE;
4499 gst_rtsp_message_unset (&message);
4500 return GST_FLOW_EOS;
4504 static GstRTSPResult
4505 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4507 GstRTSPResult res = GST_RTSP_OK;
4510 GST_DEBUG_OBJECT (src, "doing reconnect");
4512 GST_OBJECT_LOCK (src);
4513 /* only restart when the pads were not yet activated, else we were
4514 * streaming over UDP */
4515 restart = src->need_activate;
4516 GST_OBJECT_UNLOCK (src);
4518 /* no need to restart, we're done */
4522 /* we can try only TCP now */
4523 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
4525 /* close and cleanup our state */
4526 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
4529 /* see if we have TCP left to try. Also don't try TCP when we were configured
4531 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
4534 /* We post a warning message now to inform the user
4535 * that nothing happened. It's most likely a firewall thing. */
4536 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4537 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4538 "firewall is blocking it. Retrying using a TCP connection.",
4539 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4541 /* open new connection using tcp */
4542 if (gst_rtspsrc_open (src, async) < 0)
4545 /* start playback */
4546 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
4555 src->cur_protocols = 0;
4556 /* no transport possible, post an error and stop */
4557 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4558 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4559 "firewall is blocking it. No other protocols to try.",
4560 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4561 return GST_RTSP_ERROR;
4565 GST_DEBUG_OBJECT (src, "open failed");
4570 GST_DEBUG_OBJECT (src, "play failed");
4576 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
4580 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
4583 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
4586 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
4589 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
4597 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
4601 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
4604 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
4607 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
4610 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4618 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4622 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4625 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4628 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4631 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4639 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4643 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
4646 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4649 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4652 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4660 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4662 if (ret == GST_RTSP_OK)
4663 gst_rtspsrc_loop_complete_cmd (src, cmd);
4664 else if (ret == GST_RTSP_EINTR)
4665 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4667 gst_rtspsrc_loop_error_cmd (src, cmd);
4671 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
4674 gboolean flushed = FALSE;
4676 /* start new request */
4677 gst_rtspsrc_loop_start_cmd (src, cmd);
4679 GST_DEBUG_OBJECT (src, "sending cmd %d", cmd);
4681 GST_OBJECT_LOCK (src);
4682 old = src->pending_cmd;
4683 if (old == CMD_RECONNECT) {
4684 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
4685 cmd = CMD_RECONNECT;
4687 if (old != CMD_WAIT) {
4688 src->pending_cmd = CMD_WAIT;
4689 GST_OBJECT_UNLOCK (src);
4690 /* cancel previous request */
4691 GST_DEBUG_OBJECT (src, "cancel previous request %d", old);
4692 gst_rtspsrc_loop_cancel_cmd (src, old);
4693 GST_OBJECT_LOCK (src);
4695 src->pending_cmd = cmd;
4696 /* interrupt if allowed */
4697 if (src->busy_cmd & mask) {
4698 GST_DEBUG_OBJECT (src, "connection flush busy %d", src->busy_cmd);
4699 gst_rtspsrc_connection_flush (src, TRUE);
4702 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %d", src->busy_cmd);
4705 gst_task_start (src->task);
4706 GST_OBJECT_UNLOCK (src);
4712 gst_rtspsrc_loop (GstRTSPSrc * src)
4716 if (!src->conninfo.connection || !src->conninfo.connected)
4719 if (src->interleaved)
4720 ret = gst_rtspsrc_loop_interleaved (src);
4722 ret = gst_rtspsrc_loop_udp (src);
4724 if (ret != GST_FLOW_OK)
4732 GST_WARNING_OBJECT (src, "we are not connected");
4733 ret = GST_FLOW_FLUSHING;
4738 const gchar *reason = gst_flow_get_name (ret);
4740 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
4741 src->running = FALSE;
4742 if (ret == GST_FLOW_EOS) {
4743 /* perform EOS logic */
4744 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4745 gst_element_post_message (GST_ELEMENT_CAST (src),
4746 gst_message_new_segment_done (GST_OBJECT_CAST (src),
4747 src->segment.format, src->segment.position));
4748 gst_rtspsrc_push_event (src,
4749 gst_event_new_segment_done (src->segment.format,
4750 src->segment.position));
4752 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4754 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
4755 /* for fatal errors we post an error message, post the error before the
4756 * EOS so the app knows about the error first. */
4757 GST_ELEMENT_ERROR (src, STREAM, FAILED,
4758 ("Internal data flow error."),
4759 ("streaming task paused, reason %s (%d)", reason, ret));
4760 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4762 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
4767 #ifndef GST_DISABLE_GST_DEBUG
4768 static const gchar *
4769 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
4773 while (method != 0) {
4790 static const gchar *
4791 gst_rtspsrc_skip_lws (const gchar * s)
4793 while (g_ascii_isspace (*s))
4798 static const gchar *
4799 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
4801 while (s > start && g_ascii_isspace (*(s - 1)))
4806 static const gchar *
4807 gst_rtspsrc_skip_commas (const gchar * s)
4809 /* The grammar allows for multiple commas */
4810 while (g_ascii_isspace (*s) || *s == ',')
4815 static const gchar *
4816 gst_rtspsrc_skip_item (const gchar * s)
4818 gboolean quoted = FALSE;
4819 const gchar *start = s;
4821 /* A list item ends at the last non-whitespace character
4822 * before a comma which is not inside a quoted-string. Or at
4823 * the end of the string.
4829 if (*s == '\\' && *(s + 1))
4838 return gst_rtspsrc_unskip_lws (s, start);
4842 gst_rtsp_decode_quoted_string (gchar * quoted_string)
4846 src = quoted_string + 1;
4847 dst = quoted_string;
4848 while (*src && *src != '"') {
4849 if (*src == '\\' && *(src + 1))
4856 /* Extract the authentication tokens that the server provided for each method
4857 * into an array of structures and give those to the connection object.
4860 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
4861 const gchar * header, gboolean * stale)
4863 GSList *list = NULL, *iter;
4865 gchar *item, *eq, *name_end, *value;
4867 g_return_if_fail (stale != NULL);
4869 gst_rtsp_connection_clear_auth_params (conn);
4872 /* Parse a header whose content is described by RFC2616 as
4873 * "#something", where "something" does not itself contain commas,
4874 * except as part of quoted-strings, into a list of allocated strings.
4876 header = gst_rtspsrc_skip_commas (header);
4878 end = gst_rtspsrc_skip_item (header);
4879 list = g_slist_prepend (list, g_strndup (header, end - header));
4880 header = gst_rtspsrc_skip_commas (end);
4885 list = g_slist_reverse (list);
4886 for (iter = list; iter; iter = iter->next) {
4889 eq = strchr (item, '=');
4891 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
4892 if (name_end == item) {
4893 /* That's no good... */
4900 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
4902 gst_rtsp_decode_quoted_string (value);
4906 if (item && (strcmp (item, "stale") == 0) &&
4907 value && (strcmp (value, "TRUE") == 0))
4909 gst_rtsp_connection_set_auth_param (conn, item, value);
4913 g_slist_free (list);
4916 /* Parse a WWW-Authenticate Response header and determine the
4917 * available authentication methods
4919 * This code should also cope with the fact that each WWW-Authenticate
4920 * header can contain multiple challenge methods + tokens
4922 * At the moment, for Basic auth, we just do a minimal check and don't
4923 * even parse out the realm */
4925 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
4926 GstRTSPConnection * conn, gboolean * stale)
4930 g_return_if_fail (hdr != NULL);
4931 g_return_if_fail (methods != NULL);
4932 g_return_if_fail (stale != NULL);
4934 /* Skip whitespace at the start of the string */
4935 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
4937 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
4938 *methods |= GST_RTSP_AUTH_BASIC;
4939 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
4940 *methods |= GST_RTSP_AUTH_DIGEST;
4941 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
4946 * gst_rtspsrc_setup_auth:
4947 * @src: the rtsp source
4949 * Configure a username and password and auth method on the
4950 * connection object based on a response we received from the
4953 * Currently, this requires that a username and password were supplied
4954 * in the uri. In the future, they may be requested on demand by sending
4955 * a message up the bus.
4957 * Returns: TRUE if authentication information could be set up correctly.
4960 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
4964 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
4965 GstRTSPAuthMethod method;
4966 GstRTSPResult auth_result;
4968 GstRTSPConnection *conn;
4970 gboolean stale = FALSE;
4972 conn = src->conninfo.connection;
4974 /* Identify the available auth methods and see if any are supported */
4975 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
4976 &hdr, 0) == GST_RTSP_OK) {
4977 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
4980 if (avail_methods == GST_RTSP_AUTH_NONE)
4981 goto no_auth_available;
4983 /* For digest auth, if the response indicates that the session
4984 * data are stale, we just update them in the connection object and
4985 * return TRUE to retry the request */
4987 src->tried_url_auth = FALSE;
4989 url = gst_rtsp_connection_get_url (conn);
4991 /* Do we have username and password available? */
4992 if (url != NULL && !src->tried_url_auth && url->user != NULL
4993 && url->passwd != NULL) {
4996 src->tried_url_auth = TRUE;
4997 GST_DEBUG_OBJECT (src,
4998 "Attempting authentication using credentials from the URL");
5000 user = src->user_id;
5001 pass = src->user_pw;
5002 GST_DEBUG_OBJECT (src,
5003 "Attempting authentication using credentials from the properties");
5006 /* FIXME: If the url didn't contain username and password or we tried them
5007 * already, request a username and passwd from the application via some kind
5008 * of credentials request message */
5010 /* If we don't have a username and passwd at this point, bail out. */
5011 if (user == NULL || pass == NULL)
5014 /* Try to configure for each available authentication method, strongest to
5016 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
5017 /* Check if this method is available on the server */
5018 if ((method & avail_methods) == 0)
5021 /* Pass the credentials to the connection to try on the next request */
5022 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
5023 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
5024 * ignore it and end up retrying later */
5025 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
5026 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
5027 gst_rtsp_auth_method_to_string (method));
5032 if (method == GST_RTSP_AUTH_NONE)
5033 goto no_auth_available;
5039 /* Output an error indicating that we couldn't connect because there were
5040 * no supported authentication protocols */
5041 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5042 ("No supported authentication protocol was found"));
5047 /* We don't fire an error message, we just return FALSE and let the
5048 * normal NOT_AUTHORIZED error be propagated */
5053 static GstRTSPResult
5054 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5055 GstRTSPMessage * request, GstRTSPMessage * response,
5056 GstRTSPStatusCode * code)
5059 GstRTSPStatusCode thecode;
5060 gchar *content_base = NULL;
5064 if (!src->short_header)
5065 gst_rtsp_ext_list_before_send (src->extensions, request);
5067 GST_DEBUG_OBJECT (src, "sending message");
5070 gst_rtsp_message_dump (request);
5072 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
5076 gst_rtsp_connection_reset_timeout (conn);
5079 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
5084 gst_rtsp_message_dump (response);
5086 switch (response->type) {
5087 case GST_RTSP_MESSAGE_REQUEST:
5088 res = gst_rtspsrc_handle_request (src, conn, response);
5089 if (res == GST_RTSP_EEOF)
5092 goto handle_request_failed;
5094 case GST_RTSP_MESSAGE_RESPONSE:
5095 /* ok, a response is good */
5096 GST_DEBUG_OBJECT (src, "received response message");
5098 case GST_RTSP_MESSAGE_DATA:
5099 /* get next response */
5100 GST_DEBUG_OBJECT (src, "handle data response message");
5101 gst_rtspsrc_handle_data (src, response);
5104 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
5109 thecode = response->type_data.response.code;
5111 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
5113 /* if the caller wanted the result code, we store it. */
5117 /* If the request didn't succeed, bail out before doing any more */
5118 if (thecode != GST_RTSP_STS_OK)
5121 /* store new content base if any */
5122 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
5125 g_free (src->content_base);
5126 src->content_base = g_strdup (content_base);
5128 gst_rtsp_ext_list_after_send (src->extensions, request, response);
5135 gchar *str = gst_rtsp_strresult (res);
5137 if (res != GST_RTSP_EINTR) {
5138 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5139 ("Could not send message. (%s)", str));
5141 GST_WARNING_OBJECT (src, "send interrupted");
5150 GST_WARNING_OBJECT (src, "server closed connection");
5151 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5153 /* if reconnect succeeds, try again */
5155 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5159 /* only try once after reconnect, then fallthrough and error out */
5162 gchar *str = gst_rtsp_strresult (res);
5164 if (res != GST_RTSP_EINTR) {
5165 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5166 ("Could not receive message. (%s)", str));
5168 GST_WARNING_OBJECT (src, "receive interrupted");
5176 handle_request_failed:
5178 /* ERROR was posted */
5179 gst_rtsp_message_unset (response);
5184 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5185 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5186 ("The server closed the connection."));
5187 gst_rtsp_message_unset (response);
5194 * @src: the rtsp source
5195 * @conn: the connection to send on
5196 * @request: must point to a valid request
5197 * @response: must point to an empty #GstRTSPMessage
5198 * @code: an optional code result
5200 * send @request and retrieve the response in @response. optionally @code can be
5201 * non-NULL in which case it will contain the status code of the response.
5203 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5204 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5206 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5207 * @response message) if the response code was not 200 (OK).
5209 * If the attempt results in an authentication failure, then this will attempt
5210 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5213 * Returns: #GST_RTSP_OK if the processing was successful.
5215 static GstRTSPResult
5216 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5217 GstRTSPMessage * request, GstRTSPMessage * response,
5218 GstRTSPStatusCode * code)
5220 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5221 GstRTSPResult res = GST_RTSP_ERROR;
5224 GstRTSPMethod method = GST_RTSP_INVALID;
5230 /* make sure we don't loop forever */
5234 /* save method so we can disable it when the server complains */
5235 method = request->type_data.request.method;
5238 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5242 case GST_RTSP_STS_UNAUTHORIZED:
5243 if (gst_rtspsrc_setup_auth (src, response)) {
5244 /* Try the request/response again after configuring the auth info
5252 } while (retry == TRUE);
5254 /* If the user requested the code, let them handle errors, otherwise
5255 * post an error below */
5258 else if (int_code != GST_RTSP_STS_OK)
5259 goto error_response;
5266 GST_DEBUG_OBJECT (src, "got error %d", res);
5271 res = GST_RTSP_ERROR;
5273 switch (response->type_data.response.code) {
5274 case GST_RTSP_STS_NOT_FOUND:
5275 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5276 response->type_data.response.reason));
5278 case GST_RTSP_STS_MOVED_PERMANENTLY:
5279 case GST_RTSP_STS_MOVE_TEMPORARILY:
5281 gchar *new_location;
5282 GstRTSPLowerTrans transports;
5284 GST_DEBUG_OBJECT (src, "got redirection");
5285 /* if we don't have a Location Header, we must error */
5286 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5287 &new_location, 0) < 0)
5290 /* When we receive a redirect result, we go back to the INIT state after
5291 * parsing the new URI. The caller should do the needed steps to issue
5292 * a new setup when it detects this state change. */
5293 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5295 /* save current transports */
5296 if (src->conninfo.url)
5297 transports = src->conninfo.url->transports;
5299 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5301 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5303 /* set old transports */
5304 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5305 src->conninfo.url->transports = transports;
5307 src->need_redirect = TRUE;
5308 src->state = GST_RTSP_STATE_INIT;
5312 case GST_RTSP_STS_NOT_ACCEPTABLE:
5313 case GST_RTSP_STS_NOT_IMPLEMENTED:
5314 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5315 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5316 gst_rtsp_method_as_text (method));
5317 src->methods &= ~method;
5321 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5322 ("Got error response: %d (%s).", response->type_data.response.code,
5323 response->type_data.response.reason));
5326 /* if we return ERROR we should unset the response ourselves */
5327 if (res == GST_RTSP_ERROR)
5328 gst_rtsp_message_unset (response);
5334 static GstRTSPResult
5335 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5336 GstRTSPMessage * response, GstRTSPSrc * src)
5338 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5343 /* parse the response and collect all the supported methods. We need this
5344 * information so that we don't try to send an unsupported request to the
5348 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5350 GstRTSPHeaderField field;
5354 /* reset supported methods */
5357 /* Try Allow Header first */
5358 field = GST_RTSP_HDR_ALLOW;
5361 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5362 if (indx == 0 && !respoptions) {
5363 /* if no Allow header was found then try the Public header... */
5364 field = GST_RTSP_HDR_PUBLIC;
5365 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5370 src->methods |= gst_rtsp_options_from_text (respoptions);
5375 if (src->methods == 0) {
5376 /* neither Allow nor Public are required, assume the server supports
5377 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5379 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5380 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5382 /* always assume PLAY, FIXME, extensions should be able to override
5384 src->methods |= GST_RTSP_PLAY;
5385 /* also assume it will support Range */
5386 src->seekable = TRUE;
5388 /* we need describe and setup */
5389 if (!(src->methods & GST_RTSP_DESCRIBE))
5391 if (!(src->methods & GST_RTSP_SETUP))
5399 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5400 ("Server does not support DESCRIBE."));
5405 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5406 ("Server does not support SETUP."));
5411 /* masks to be kept in sync with the hardcoded protocol order of preference
5413 static guint protocol_masks[] = {
5414 GST_RTSP_LOWER_TRANS_UDP,
5415 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5416 GST_RTSP_LOWER_TRANS_TCP,
5420 static GstRTSPResult
5421 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5422 GstRTSPLowerTrans protocols, GstRTSPProfile profile, gchar ** transports)
5426 gboolean add_udp_str;
5431 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5436 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5438 /* extension listed transports, use those */
5439 if (*transports != NULL)
5442 /* it's the default */
5443 add_udp_str = FALSE;
5445 /* the default RTSP transports */
5446 result = g_string_new ("RTP");
5449 case GST_RTSP_PROFILE_AVP:
5450 g_string_append (result, "/AVP");
5452 case GST_RTSP_PROFILE_SAVP:
5453 g_string_append (result, "/SAVP");
5455 case GST_RTSP_PROFILE_AVPF:
5456 g_string_append (result, "/AVPF");
5458 case GST_RTSP_PROFILE_SAVPF:
5459 g_string_append (result, "/SAVPF");
5465 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5466 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5468 g_string_append (result, "/UDP");
5469 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5470 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5471 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5472 /* we don't have to allocate any UDP ports yet, if the selected transport
5473 * turns out to be multicast we can create them and join the multicast
5474 * group indicated in the transport reply */
5476 g_string_append (result, "/UDP");
5477 g_string_append (result, ";multicast");
5478 if (src->next_port_num != 0) {
5479 if (src->client_port_range.max > 0 &&
5480 src->next_port_num >= src->client_port_range.max)
5483 g_string_append_printf (result, ";client_port=%d-%d",
5484 src->next_port_num, src->next_port_num + 1);
5486 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
5487 GST_DEBUG_OBJECT (src, "adding TCP");
5489 g_string_append (result, "/TCP;unicast;interleaved=%%i1-%%i2");
5491 *transports = g_string_free (result, FALSE);
5493 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5500 GST_ERROR ("extension gave error %d", res);
5505 GST_ERROR ("no more ports available");
5506 return GST_RTSP_ERROR;
5510 static GstRTSPResult
5511 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5512 gint orig_rtpport, gint orig_rtcpport)
5515 gint nr_udp, nr_int;
5517 gint rtpport = 0, rtcpport = 0;
5520 src = stream->parent;
5522 /* find number of placeholders first */
5523 if (strstr (*transports, "%%i2"))
5525 else if (strstr (*transports, "%%i1"))
5530 if (strstr (*transports, "%%u2"))
5532 else if (strstr (*transports, "%%u1"))
5537 if (nr_udp == 0 && nr_int == 0)
5541 if (!orig_rtpport || !orig_rtcpport) {
5542 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5545 rtpport = orig_rtpport;
5546 rtcpport = orig_rtcpport;
5550 str = g_string_new ("");
5552 while ((next = strstr (p, "%%"))) {
5553 g_string_append_len (str, p, next - p);
5554 if (next[2] == 'u') {
5556 g_string_append_printf (str, "%d", rtpport);
5557 else if (next[3] == '2')
5558 g_string_append_printf (str, "%d", rtcpport);
5560 if (next[2] == 'i') {
5562 g_string_append_printf (str, "%d", src->free_channel);
5563 else if (next[3] == '2')
5564 g_string_append_printf (str, "%d", src->free_channel + 1);
5569 /* append final part */
5570 g_string_append (str, p);
5572 g_free (*transports);
5573 *transports = g_string_free (str, FALSE);
5581 GST_ERROR ("failed to allocate udp ports");
5582 return GST_RTSP_ERROR;
5586 /* Perform the SETUP request for all the streams.
5588 * We ask the server for a specific transport, which initially includes all the
5589 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
5590 * two local UDP ports that we send to the server.
5592 * Once the server replied with a transport, we configure the other streams
5593 * with the same transport.
5595 * This function will also configure the stream for the selected transport,
5596 * which basically means creating the pipeline.
5598 static GstRTSPResult
5599 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
5602 GstRTSPResult res = GST_RTSP_ERROR;
5603 GstRTSPMessage request = { 0 };
5604 GstRTSPMessage response = { 0 };
5605 GstRTSPStream *stream = NULL;
5606 GstRTSPLowerTrans protocols;
5607 GstRTSPStatusCode code;
5608 gboolean unsupported_real = FALSE;
5609 gint rtpport, rtcpport;
5613 if (src->conninfo.connection) {
5614 url = gst_rtsp_connection_get_url (src->conninfo.connection);
5615 /* we initially allow all configured lower transports. based on the URL
5616 * transports and the replies from the server we narrow them down. */
5617 protocols = url->transports & src->cur_protocols;
5620 protocols = src->cur_protocols;
5626 /* reset some state */
5627 src->free_channel = 0;
5628 src->interleaved = FALSE;
5629 src->need_activate = FALSE;
5630 /* keep track of next port number, 0 is random */
5631 src->next_port_num = src->client_port_range.min;
5632 rtpport = rtcpport = 0;
5634 if (G_UNLIKELY (src->streams == NULL))
5637 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5638 GstRTSPConnection *conn;
5645 stream = (GstRTSPStream *) walk->data;
5646 caps = stream_get_caps_for_pt (stream, stream->default_pt);
5648 GST_DEBUG_OBJECT (src, "skipping stream %p, no caps", stream);
5652 /* see if we need to configure this stream */
5653 if (!gst_rtsp_ext_list_configure_stream (src->extensions, caps)) {
5654 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
5656 stream->disabled = TRUE;
5660 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
5661 stream->id, caps, &selected);
5663 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
5664 stream->disabled = TRUE;
5667 stream->disabled = FALSE;
5669 /* merge/overwrite global caps */
5674 s = gst_caps_get_structure (caps, 0);
5676 num = gst_structure_n_fields (src->props);
5677 for (j = 0; j < num; j++) {
5681 name = gst_structure_nth_field_name (src->props, j);
5682 val = gst_structure_get_value (src->props, name);
5683 gst_structure_set_value (s, name, val);
5685 GST_DEBUG_OBJECT (src, "copied %s", name);
5689 /* skip setup if we have no URL for it */
5690 if (stream->conninfo.location == NULL) {
5691 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
5695 if (src->conninfo.connection == NULL) {
5696 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
5697 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
5700 conn = stream->conninfo.connection;
5702 conn = src->conninfo.connection;
5704 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
5705 stream->conninfo.location);
5707 /* if we have a multicast connection, only suggest multicast from now on */
5708 if (stream->is_multicast)
5709 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
5712 /* first selectable protocol */
5713 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5715 if (!protocol_masks[mask])
5719 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
5720 protocol_masks[mask]);
5721 /* create a string with first transport in line */
5723 res = gst_rtspsrc_create_transports_string (src,
5724 protocols & protocol_masks[mask], stream->profile, &transports);
5725 if (res < 0 || transports == NULL)
5726 goto setup_transport_failed;
5728 if (strlen (transports) == 0) {
5729 g_free (transports);
5730 GST_DEBUG_OBJECT (src, "no transports found");
5735 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
5737 /* replace placeholders with real values, this function will optionally
5738 * allocate UDP ports and other info needed to execute the setup request */
5739 res = gst_rtspsrc_prepare_transports (stream, &transports,
5740 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
5742 g_free (transports);
5743 goto setup_transport_failed;
5746 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
5748 /* create SETUP request */
5750 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
5751 stream->conninfo.location);
5753 g_free (transports);
5754 goto create_request_failed;
5757 /* select transport */
5758 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
5760 /* if the user wants a non default RTP packet size we add the blocksize
5762 if (src->rtp_blocksize > 0) {
5763 hval = g_strdup_printf ("%d", src->rtp_blocksize);
5764 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
5768 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
5771 /* handle the code ourselves */
5772 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
5776 case GST_RTSP_STS_OK:
5778 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
5779 gst_rtsp_message_unset (&request);
5780 gst_rtsp_message_unset (&response);
5781 /* cleanup of leftover transport */
5782 gst_rtspsrc_stream_free_udp (stream);
5783 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
5784 * we might be in this case */
5785 if (stream->container && rtpport && rtcpport && !retry) {
5786 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
5791 /* this transport did not go down well, but we may have others to try
5792 * that we did not send yet, try those and only give up then
5793 * but not without checking for lost cause/extension so we can
5794 * post a nicer/more useful error message later */
5795 if (!unsupported_real)
5796 unsupported_real = stream->is_real;
5797 /* select next available protocol, give up on this stream if none */
5799 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5801 if (!protocol_masks[mask] || unsupported_real)
5806 /* cleanup of leftover transport and move to the next stream */
5807 gst_rtspsrc_stream_free_udp (stream);
5808 goto response_error;
5811 /* parse response transport */
5813 gchar *resptrans = NULL;
5814 GstRTSPTransport transport = { 0 };
5816 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
5819 gst_rtspsrc_stream_free_udp (stream);
5823 /* parse transport, go to next stream on parse error */
5824 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
5825 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
5829 /* update allowed transports for other streams. once the transport of
5830 * one stream has been determined, we make sure that all other streams
5831 * are configured in the same way */
5832 switch (transport.lower_transport) {
5833 case GST_RTSP_LOWER_TRANS_TCP:
5834 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
5835 protocols = GST_RTSP_LOWER_TRANS_TCP;
5836 src->interleaved = TRUE;
5837 /* update free channels */
5839 MAX (transport.interleaved.min, src->free_channel);
5841 MAX (transport.interleaved.max, src->free_channel);
5842 src->free_channel++;
5844 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
5845 /* only allow multicast for other streams */
5846 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
5847 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
5848 /* if the server selected our ports, increment our counters so that
5849 * we select a new port later */
5850 if (src->next_port_num == transport.port.min &&
5851 src->next_port_num + 1 == transport.port.max) {
5852 src->next_port_num += 2;
5855 case GST_RTSP_LOWER_TRANS_UDP:
5856 /* only allow unicast for other streams */
5857 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
5858 protocols = GST_RTSP_LOWER_TRANS_UDP;
5861 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
5862 transport.lower_transport);
5866 if (!stream->container || (!src->interleaved && !retry)) {
5867 /* now configure the stream with the selected transport */
5868 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
5869 GST_DEBUG_OBJECT (src,
5870 "could not configure stream %p transport, skipping stream",
5873 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
5874 /* retain the first allocated UDP port pair */
5875 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
5876 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
5879 /* we need to activate at least one streams when we detect activity */
5880 src->need_activate = TRUE;
5882 /* clean up our transport struct */
5883 gst_rtsp_transport_init (&transport);
5884 /* clean up used RTSP messages */
5885 gst_rtsp_message_unset (&request);
5886 gst_rtsp_message_unset (&response);
5890 /* store the transport protocol that was configured */
5891 src->cur_protocols = protocols;
5893 gst_rtsp_ext_list_stream_select (src->extensions, url);
5895 /* if there is nothing to activate, error out */
5896 if (!src->need_activate)
5897 goto nothing_to_activate;
5904 /* no transport possible, post an error and stop */
5905 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5906 ("Could not connect to server, no protocols left"));
5907 return GST_RTSP_ERROR;
5911 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5912 ("SDP contains no streams"));
5913 return GST_RTSP_ERROR;
5915 create_request_failed:
5917 gchar *str = gst_rtsp_strresult (res);
5919 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5920 ("Could not create request. (%s)", str));
5924 setup_transport_failed:
5926 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5927 ("Could not setup transport."));
5928 res = GST_RTSP_ERROR;
5933 const gchar *str = gst_rtsp_status_as_text (code);
5935 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5936 ("Error (%d): %s", code, GST_STR_NULL (str)));
5937 res = GST_RTSP_ERROR;
5942 gchar *str = gst_rtsp_strresult (res);
5944 if (res != GST_RTSP_EINTR) {
5945 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5946 ("Could not send message. (%s)", str));
5948 GST_WARNING_OBJECT (src, "send interrupted");
5955 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5956 ("Server did not select transport."));
5957 res = GST_RTSP_ERROR;
5960 nothing_to_activate:
5962 /* none of the available error codes is really right .. */
5963 if (unsupported_real) {
5964 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5965 (_("No supported stream was found. You might need to install a "
5966 "GStreamer RTSP extension plugin for Real media streams.")),
5969 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5970 (_("No supported stream was found. You might need to allow "
5971 "more transport protocols or may otherwise be missing "
5972 "the right GStreamer RTSP extension plugin.")), (NULL));
5974 return GST_RTSP_ERROR;
5978 gst_rtsp_message_unset (&request);
5979 gst_rtsp_message_unset (&response);
5985 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
5986 GstSegment * segment)
5989 GstRTSPTimeRange *therange;
5992 gst_rtsp_range_free (src->range);
5994 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
5995 GST_DEBUG_OBJECT (src, "parsed range %s", range);
5996 src->range = therange;
5998 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
6000 gst_segment_init (segment, GST_FORMAT_TIME);
6004 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
6005 therange->min.type, therange->min.seconds, therange->max.type,
6006 therange->max.seconds);
6008 if (therange->min.type == GST_RTSP_TIME_NOW)
6010 else if (therange->min.type == GST_RTSP_TIME_END)
6013 seconds = therange->min.seconds * GST_SECOND;
6015 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
6016 GST_TIME_ARGS (seconds));
6018 /* we need to start playback without clipping from the position reported by
6020 segment->start = seconds;
6021 segment->position = seconds;
6023 if (therange->max.type == GST_RTSP_TIME_NOW)
6025 else if (therange->max.type == GST_RTSP_TIME_END)
6028 seconds = therange->max.seconds * GST_SECOND;
6030 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
6031 GST_TIME_ARGS (seconds));
6033 /* live (WMS) server might send overflowed large max as its idea of infinity,
6034 * compensate to prevent problems later on */
6035 if (seconds != -1 && seconds < 0) {
6037 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
6040 /* live (WMS) might send min == max, which is not worth recording */
6041 if (segment->duration == -1 && seconds == segment->start)
6044 /* don't change duration with unknown value, we might have a valid value
6045 * there that we want to keep. */
6047 segment->duration = seconds;
6052 /* Parse clock profived by the server with following syntax:
6054 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
6057 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
6059 gboolean res = FALSE;
6061 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
6062 gchar **fields = NULL, **parts = NULL;
6063 gchar *remote_ip, *str;
6065 GstClockTime base_time;
6068 fields = g_strsplit (gstclock, " ", 0);
6070 /* wrapped clock, not very interesting for now */
6071 if (fields[1] == NULL)
6074 /* remote IP address and port */
6075 if ((str = fields[2]) == NULL)
6078 parts = g_strsplit (str, ":", 0);
6080 if ((remote_ip = parts[0]) == NULL)
6083 if ((str = parts[1]) == NULL)
6091 if ((str = fields[3]) == NULL)
6094 base_time = g_ascii_strtoull (str, NULL, 10);
6097 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
6100 if (src->provided_clock)
6101 gst_object_unref (src->provided_clock);
6102 src->provided_clock = netclock;
6104 gst_element_post_message (GST_ELEMENT_CAST (src),
6105 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
6106 src->provided_clock, TRUE));
6110 g_strfreev (fields);
6116 /* must be called with the RTSP state lock */
6117 static GstRTSPResult
6118 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
6124 /* prepare global stream caps properties */
6126 gst_structure_remove_all_fields (src->props);
6128 src->props = gst_structure_new_empty ("RTSPProperties");
6131 gst_sdp_message_dump (sdp);
6133 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
6135 /* let the app inspect and change the SDP */
6136 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6138 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6140 /* parse range for duration reporting. */
6145 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6149 /* keep track of the range and configure it in the segment */
6150 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6154 /* parse clock information. This is GStreamer specific, a server can tell the
6155 * client what clock it is using and wrap that in a network clock. The
6156 * advantage of that is that we can slave to it. */
6158 const gchar *gstclock;
6161 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6162 if (gstclock == NULL)
6165 /* parse the clock and expose it in the provide_clock method */
6166 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6170 /* try to find a global control attribute. Note that a '*' means that we should
6171 * do aggregate control with the current url (so we don't do anything and
6172 * leave the current connection as is) */
6174 const gchar *control;
6177 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6178 if (control == NULL)
6181 /* only take fully qualified urls */
6182 if (g_str_has_prefix (control, "rtsp://"))
6186 g_free (src->conninfo.location);
6187 src->conninfo.location = g_strdup (control);
6188 /* make a connection for this, if there was a connection already, nothing
6190 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6191 GST_ERROR_OBJECT (src, "could not connect");
6194 /* we need to keep the control url separate from the connection url because
6195 * the rules for constructing the media control url need it */
6196 g_free (src->control);
6197 src->control = g_strdup (control);
6200 /* create streams */
6201 n_streams = gst_sdp_message_medias_len (sdp);
6202 for (i = 0; i < n_streams; i++) {
6203 gst_rtspsrc_create_stream (src, sdp, i);
6206 src->state = GST_RTSP_STATE_INIT;
6209 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6212 /* reset our state */
6213 src->need_range = TRUE;
6216 src->state = GST_RTSP_STATE_READY;
6223 GST_ERROR_OBJECT (src, "setup failed");
6224 gst_rtspsrc_cleanup (src);
6229 static GstRTSPResult
6230 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6234 GstRTSPMessage request = { 0 };
6235 GstRTSPMessage response = { 0 };
6238 gchar *respcont = NULL;
6241 src->need_redirect = FALSE;
6243 /* can't continue without a valid url */
6244 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6245 res = GST_RTSP_EINVAL;
6248 src->tried_url_auth = FALSE;
6250 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6251 goto connect_failed;
6253 /* create OPTIONS */
6254 GST_DEBUG_OBJECT (src, "create options...");
6256 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
6257 src->conninfo.url_str);
6259 goto create_request_failed;
6262 GST_DEBUG_OBJECT (src, "send options...");
6265 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6268 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6273 if (!gst_rtspsrc_parse_methods (src, &response))
6276 /* create DESCRIBE */
6277 GST_DEBUG_OBJECT (src, "create describe...");
6279 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
6280 src->conninfo.url_str);
6282 goto create_request_failed;
6284 /* we only accept SDP for now */
6285 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
6289 GST_DEBUG_OBJECT (src, "send describe...");
6292 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
6295 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6299 /* we only perform redirect for the describe, currently */
6300 if (src->need_redirect) {
6301 /* close connection, we don't have to send a TEARDOWN yet, ignore the
6303 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6305 gst_rtsp_message_unset (&request);
6306 gst_rtsp_message_unset (&response);
6312 /* it could be that the DESCRIBE method was not implemented */
6313 if (!src->methods & GST_RTSP_DESCRIBE)
6316 /* check if reply is SDP */
6317 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
6319 /* could not be set but since the request returned OK, we assume it
6320 * was SDP, else check it. */
6322 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
6323 goto wrong_content_type;
6326 /* get message body and parse as SDP */
6327 gst_rtsp_message_get_body (&response, &data, &size);
6328 if (data == NULL || size == 0)
6331 GST_DEBUG_OBJECT (src, "parse SDP...");
6332 gst_sdp_message_new (sdp);
6333 gst_sdp_message_parse_buffer (data, size, *sdp);
6335 /* clean up any messages */
6336 gst_rtsp_message_unset (&request);
6337 gst_rtsp_message_unset (&response);
6344 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
6345 ("No valid RTSP URL was provided"));
6350 gchar *str = gst_rtsp_strresult (res);
6352 if (res != GST_RTSP_EINTR) {
6353 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6354 ("Failed to connect. (%s)", str));
6356 GST_WARNING_OBJECT (src, "connect interrupted");
6361 create_request_failed:
6363 gchar *str = gst_rtsp_strresult (res);
6365 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6366 ("Could not create request. (%s)", str));
6372 /* Don't post a message - the rtsp_send method will have
6373 * taken care of it because we passed NULL for the response code */
6378 /* error was posted */
6379 res = GST_RTSP_ERROR;
6384 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6385 ("Server does not support SDP, got %s.", respcont));
6386 res = GST_RTSP_ERROR;
6391 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6392 ("Server can not provide an SDP."));
6393 res = GST_RTSP_ERROR;
6398 if (src->conninfo.connection) {
6399 GST_DEBUG_OBJECT (src, "free connection");
6400 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6402 gst_rtsp_message_unset (&request);
6403 gst_rtsp_message_unset (&response);
6408 static GstRTSPResult
6409 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
6414 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
6416 if (src->sdp == NULL) {
6417 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
6421 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
6426 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
6433 GST_WARNING_OBJECT (src, "can't get sdp");
6434 src->open_error = TRUE;
6439 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
6440 src->open_error = TRUE;
6445 static GstRTSPResult
6446 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
6448 GstRTSPMessage request = { 0 };
6449 GstRTSPMessage response = { 0 };
6450 GstRTSPResult res = GST_RTSP_OK;
6452 const gchar *control;
6454 GST_DEBUG_OBJECT (src, "TEARDOWN...");
6456 gst_rtspsrc_set_state (src, GST_STATE_READY);
6458 if (src->state < GST_RTSP_STATE_READY) {
6459 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
6466 /* construct a control url */
6467 control = get_aggregate_control (src);
6469 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
6472 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6473 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6474 const gchar *setup_url;
6475 GstRTSPConnInfo *info;
6477 /* try aggregate control first but do non-aggregate control otherwise */
6479 setup_url = control;
6480 else if ((setup_url = stream->conninfo.location) == NULL)
6483 if (src->conninfo.connection) {
6484 info = &src->conninfo;
6485 } else if (stream->conninfo.connection) {
6486 info = &stream->conninfo;
6490 if (!info->connected)
6495 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
6497 goto create_request_failed;
6500 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
6503 gst_rtspsrc_send (src, info->connection, &request, &response,
6507 /* FIXME, parse result? */
6508 gst_rtsp_message_unset (&request);
6509 gst_rtsp_message_unset (&response);
6512 /* early exit when we did aggregate control */
6518 /* close connections */
6519 GST_DEBUG_OBJECT (src, "closing connection...");
6520 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6521 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6522 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6523 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
6527 gst_rtspsrc_cleanup (src);
6529 src->state = GST_RTSP_STATE_INVALID;
6532 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
6537 create_request_failed:
6539 gchar *str = gst_rtsp_strresult (res);
6541 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6542 ("Could not create request. (%s)", str));
6548 gchar *str = gst_rtsp_strresult (res);
6550 gst_rtsp_message_unset (&request);
6551 if (res != GST_RTSP_EINTR) {
6552 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6553 ("Could not send message. (%s)", str));
6555 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
6562 GST_DEBUG_OBJECT (src,
6563 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
6568 /* RTP-Info is of the format:
6570 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
6572 * rtptime corresponds to the timestamp for the NPT time given in the header
6573 * seqbase corresponds to the next sequence number we received. This number
6574 * indicates the first seqnum after the seek and should be used to discard
6575 * packets that are from before the seek.
6578 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
6583 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
6585 infos = g_strsplit (rtpinfo, ",", 0);
6586 for (i = 0; infos[i]; i++) {
6588 GstRTSPStream *stream;
6592 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
6594 /* init values, types of seqbase and timebase are bigger than needed so we
6595 * can store -1 as uninitialized values */
6600 /* parse url, find stream for url.
6601 * parse seq and rtptime. The seq number should be configured in the rtp
6602 * depayloader or session manager to detect gaps. Same for the rtptime, it
6603 * should be used to create an initial time newsegment. */
6604 fields = g_strsplit (infos[i], ";", 0);
6605 for (j = 0; fields[j]; j++) {
6606 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
6607 /* remove leading whitespace */
6608 fields[j] = g_strchug (fields[j]);
6609 if (g_str_has_prefix (fields[j], "url=")) {
6610 /* get the url and the stream */
6612 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
6613 } else if (g_str_has_prefix (fields[j], "seq=")) {
6614 seqbase = atoi (fields[j] + 4);
6615 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
6616 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
6619 g_strfreev (fields);
6620 /* now we need to store the values for the caps of the stream */
6621 if (stream != NULL) {
6622 GST_DEBUG_OBJECT (src,
6623 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
6624 stream, seqbase, timebase);
6626 /* we have a stream, configure detected params */
6627 stream->seqbase = seqbase;
6628 stream->timebase = timebase;
6637 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
6642 interval = strtoul (rtcp, NULL, 10);
6643 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
6648 interval *= GST_MSECOND;
6650 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6651 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6653 /* already (optionally) retrieved this when configuring manager */
6654 if (stream->session) {
6655 GObject *rtpsession = stream->session;
6657 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
6659 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
6663 /* now it happens that (Xenon) server sending this may also provide bogus
6664 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
6665 * and just use RTP-Info to sync */
6667 GObjectClass *klass;
6669 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
6670 if (g_object_class_find_property (klass, "rtcp-sync")) {
6671 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
6672 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
6678 gst_rtspsrc_get_float (const gchar * dstr)
6680 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6682 /* canonicalise floating point string so we can handle float strings
6683 * in the form "24.930" or "24,930" irrespective of the current locale */
6684 g_strlcpy (s, dstr, sizeof (s));
6685 g_strdelimit (s, ",", '.');
6686 return g_ascii_strtod (s, NULL);
6690 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
6692 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6694 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
6695 g_strlcpy (val_str, "now", sizeof (val_str));
6697 if (segment->position == 0) {
6698 g_strlcpy (val_str, "0", sizeof (val_str));
6700 g_ascii_dtostr (val_str, sizeof (val_str),
6701 ((gdouble) segment->position) / GST_SECOND);
6704 return g_strdup_printf ("npt=%s-", val_str);
6708 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
6712 stream->timebase = -1;
6713 stream->seqbase = -1;
6715 len = stream->ptmap->len;
6716 for (i = 0; i < len; i++) {
6717 PtMapItem *item = &g_array_index (stream->ptmap, PtMapItem, i);
6720 if (item->caps == NULL)
6723 item->caps = gst_caps_make_writable (item->caps);
6724 s = gst_caps_get_structure (item->caps, 0);
6725 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
6729 static GstRTSPResult
6730 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
6732 GstRTSPResult res = GST_RTSP_OK;
6734 if (src->state < GST_RTSP_STATE_READY) {
6735 res = GST_RTSP_ERROR;
6736 if (src->open_error) {
6737 GST_DEBUG_OBJECT (src, "the stream was in error");
6741 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
6743 if ((res = gst_rtspsrc_open (src, async)) < 0) {
6744 GST_DEBUG_OBJECT (src, "failed to open stream");
6753 static GstRTSPResult
6754 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
6756 GstRTSPMessage request = { 0 };
6757 GstRTSPMessage response = { 0 };
6758 GstRTSPResult res = GST_RTSP_OK;
6762 const gchar *control;
6764 GST_DEBUG_OBJECT (src, "PLAY...");
6766 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6769 if (!(src->methods & GST_RTSP_PLAY))
6772 if (src->state == GST_RTSP_STATE_PLAYING)
6775 if (!src->conninfo.connection || !src->conninfo.connected)
6778 /* send some dummy packets before we activate the receive in the
6780 gst_rtspsrc_send_dummy_packets (src);
6782 /* require new SR packets */
6784 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
6786 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
6788 /* construct a control url */
6789 control = get_aggregate_control (src);
6791 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6792 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6793 const gchar *setup_url;
6794 GstRTSPConnection *conn;
6796 /* try aggregate control first but do non-aggregate control otherwise */
6798 setup_url = control;
6799 else if ((setup_url = stream->conninfo.location) == NULL)
6802 if (src->conninfo.connection) {
6803 conn = src->conninfo.connection;
6804 } else if (stream->conninfo.connection) {
6805 conn = stream->conninfo.connection;
6811 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
6813 goto create_request_failed;
6815 if (src->need_range) {
6816 hval = gen_range_header (src, segment);
6818 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
6820 /* store the newsegment event so it can be sent from the streaming thread. */
6821 if (src->start_segment)
6822 gst_event_unref (src->start_segment);
6823 src->start_segment = gst_event_new_segment (&src->segment);
6826 if (segment->rate != 1.0) {
6827 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
6829 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
6831 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
6833 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
6837 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
6839 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6842 /* seek may have silently failed as it is not supported */
6843 if (!(src->methods & GST_RTSP_PLAY)) {
6844 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
6845 /* obviously it is supported as we made it here */
6846 src->methods |= GST_RTSP_PLAY;
6847 src->seekable = FALSE;
6848 /* but there is nothing to parse in the response,
6849 * so convey we have no idea and not to expect anything particular */
6850 clear_rtp_base (src, stream);
6854 /* need to do for all streams */
6855 for (run = src->streams; run; run = g_list_next (run))
6856 clear_rtp_base (src, (GstRTSPStream *) run->data);
6858 /* NOTE the above also disables npt based eos detection */
6859 /* and below forces position to 0,
6860 * which is visible feedback we lost the plot */
6861 segment->start = segment->position = src->last_pos;
6864 gst_rtsp_message_unset (&request);
6866 /* parse RTP npt field. This is the current position in the stream (Normal
6867 * Play Time) and should be put in the NEWSEGMENT position field. */
6868 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
6870 gst_rtspsrc_parse_range (src, hval, segment);
6872 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
6873 segment->rate = 1.0;
6875 /* parse Speed header. This is the intended playback rate of the stream
6876 * and should be put in the NEWSEGMENT rate field. */
6877 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
6878 0) == GST_RTSP_OK) {
6879 segment->rate = gst_rtspsrc_get_float (hval);
6880 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
6881 &hval, 0) == GST_RTSP_OK) {
6882 segment->rate = gst_rtspsrc_get_float (hval);
6885 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
6886 * for the RTP packets. If this is not present, we assume all starts from 0...
6887 * This is info for the RTP session manager that we pass to it in caps. */
6889 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
6890 &hval, hval_idx++) == GST_RTSP_OK)
6891 gst_rtspsrc_parse_rtpinfo (src, hval);
6893 /* some servers indicate RTCP parameters in PLAY response,
6894 * rather than properly in SDP */
6895 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
6896 &hval, 0) == GST_RTSP_OK)
6897 gst_rtspsrc_handle_rtcp_interval (src, hval);
6899 gst_rtsp_message_unset (&response);
6901 /* early exit when we did aggregate control */
6905 /* configure the caps of the streams after we parsed all headers. Only reset
6906 * the manager object when we set a new Range header (we did a seek) */
6907 gst_rtspsrc_configure_caps (src, segment, src->need_range);
6909 /* set again when needed */
6910 src->need_range = FALSE;
6912 src->running = TRUE;
6913 src->base_time = -1;
6914 src->state = GST_RTSP_STATE_PLAYING;
6917 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
6918 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6919 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6920 stream->discont = TRUE;
6925 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
6932 GST_DEBUG_OBJECT (src, "failed to open stream");
6937 GST_DEBUG_OBJECT (src, "PLAY is not supported");
6942 GST_DEBUG_OBJECT (src, "we were already PLAYING");
6945 create_request_failed:
6947 gchar *str = gst_rtsp_strresult (res);
6949 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6950 ("Could not create request. (%s)", str));
6956 gchar *str = gst_rtsp_strresult (res);
6958 gst_rtsp_message_unset (&request);
6959 if (res != GST_RTSP_EINTR) {
6960 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6961 ("Could not send message. (%s)", str));
6963 GST_WARNING_OBJECT (src, "PLAY interrupted");
6970 static GstRTSPResult
6971 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
6973 GstRTSPResult res = GST_RTSP_OK;
6974 GstRTSPMessage request = { 0 };
6975 GstRTSPMessage response = { 0 };
6977 const gchar *control;
6979 GST_DEBUG_OBJECT (src, "PAUSE...");
6981 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6984 if (!(src->methods & GST_RTSP_PAUSE))
6987 if (src->state == GST_RTSP_STATE_READY)
6990 if (!src->conninfo.connection || !src->conninfo.connected)
6993 /* construct a control url */
6994 control = get_aggregate_control (src);
6996 /* loop over the streams. We might exit the loop early when we could do an
6997 * aggregate control */
6998 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6999 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
7000 GstRTSPConnection *conn;
7001 const gchar *setup_url;
7003 /* try aggregate control first but do non-aggregate control otherwise */
7005 setup_url = control;
7006 else if ((setup_url = stream->conninfo.location) == NULL)
7009 if (src->conninfo.connection) {
7010 conn = src->conninfo.connection;
7011 } else if (stream->conninfo.connection) {
7012 conn = stream->conninfo.connection;
7018 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
7019 ("Sending PAUSE request"));
7022 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
7024 goto create_request_failed;
7026 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
7029 gst_rtsp_message_unset (&request);
7030 gst_rtsp_message_unset (&response);
7032 /* exit early when we did agregate control */
7037 /* change element states now */
7038 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
7041 src->state = GST_RTSP_STATE_READY;
7045 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
7052 GST_DEBUG_OBJECT (src, "failed to open stream");
7057 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
7062 GST_DEBUG_OBJECT (src, "we were already PAUSED");
7065 create_request_failed:
7067 gchar *str = gst_rtsp_strresult (res);
7069 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
7070 ("Could not create request. (%s)", str));
7076 gchar *str = gst_rtsp_strresult (res);
7078 gst_rtsp_message_unset (&request);
7079 if (res != GST_RTSP_EINTR) {
7080 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
7081 ("Could not send message. (%s)", str));
7083 GST_WARNING_OBJECT (src, "PAUSE interrupted");
7091 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
7093 GstRTSPSrc *rtspsrc;
7095 rtspsrc = GST_RTSPSRC (bin);
7097 switch (GST_MESSAGE_TYPE (message)) {
7098 case GST_MESSAGE_EOS:
7099 gst_message_unref (message);
7101 case GST_MESSAGE_ELEMENT:
7103 const GstStructure *s = gst_message_get_structure (message);
7105 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
7106 gboolean ignore_timeout;
7108 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
7110 GST_OBJECT_LOCK (rtspsrc);
7111 ignore_timeout = rtspsrc->ignore_timeout;
7112 rtspsrc->ignore_timeout = TRUE;
7113 GST_OBJECT_UNLOCK (rtspsrc);
7115 /* we only act on the first udp timeout message, others are irrelevant
7116 * and can be ignored. */
7117 if (!ignore_timeout)
7118 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
7120 gst_message_unref (message);
7123 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7126 case GST_MESSAGE_ERROR:
7129 GstRTSPStream *stream;
7132 udpsrc = GST_MESSAGE_SRC (message);
7134 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7135 GST_ELEMENT_NAME (udpsrc));
7137 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7141 /* we ignore the RTCP udpsrc */
7142 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7145 /* if we get error messages from the udp sources, that's not a problem as
7146 * long as not all of them error out. We also don't really know what the
7147 * problem is, the message does not give enough detail... */
7148 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7149 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7150 if (ret != GST_FLOW_OK)
7154 gst_message_unref (message);
7158 /* fatal but not our message, forward */
7159 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7164 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7170 /* the thread where everything happens */
7172 gst_rtspsrc_thread (GstRTSPSrc * src)
7176 GST_OBJECT_LOCK (src);
7177 cmd = src->pending_cmd;
7178 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7179 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7180 src->pending_cmd = CMD_LOOP;
7182 src->pending_cmd = CMD_WAIT;
7183 GST_DEBUG_OBJECT (src, "got command %d", cmd);
7185 /* we got the message command, so ensure communication is possible again */
7186 gst_rtspsrc_connection_flush (src, FALSE);
7188 src->busy_cmd = cmd;
7189 GST_OBJECT_UNLOCK (src);
7193 gst_rtspsrc_open (src, TRUE);
7196 gst_rtspsrc_play (src, &src->segment, TRUE);
7199 gst_rtspsrc_pause (src, TRUE);
7202 gst_rtspsrc_close (src, TRUE, FALSE);
7205 gst_rtspsrc_loop (src);
7208 gst_rtspsrc_reconnect (src, FALSE);
7214 GST_OBJECT_LOCK (src);
7215 /* and go back to sleep */
7216 if (src->pending_cmd == CMD_WAIT) {
7218 gst_task_pause (src->task);
7221 src->busy_cmd = CMD_WAIT;
7222 GST_OBJECT_UNLOCK (src);
7226 gst_rtspsrc_start (GstRTSPSrc * src)
7228 GST_DEBUG_OBJECT (src, "starting");
7230 GST_OBJECT_LOCK (src);
7232 src->pending_cmd = CMD_WAIT;
7234 if (src->task == NULL) {
7235 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7236 if (src->task == NULL)
7239 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7241 GST_OBJECT_UNLOCK (src);
7248 GST_OBJECT_UNLOCK (src);
7249 GST_ERROR_OBJECT (src, "failed to create task");
7255 gst_rtspsrc_stop (GstRTSPSrc * src)
7259 GST_DEBUG_OBJECT (src, "stopping");
7261 /* also cancels pending task */
7262 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7264 GST_OBJECT_LOCK (src);
7265 if ((task = src->task)) {
7267 GST_OBJECT_UNLOCK (src);
7269 gst_task_stop (task);
7271 /* make sure it is not running */
7272 GST_RTSP_STREAM_LOCK (src);
7273 GST_RTSP_STREAM_UNLOCK (src);
7275 /* now wait for the task to finish */
7276 gst_task_join (task);
7278 /* and free the task */
7279 gst_object_unref (GST_OBJECT (task));
7281 GST_OBJECT_LOCK (src);
7283 GST_OBJECT_UNLOCK (src);
7285 /* ensure synchronously all is closed and clean */
7286 gst_rtspsrc_close (src, FALSE, TRUE);
7291 static GstStateChangeReturn
7292 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
7294 GstRTSPSrc *rtspsrc;
7295 GstStateChangeReturn ret;
7297 rtspsrc = GST_RTSPSRC (element);
7299 switch (transition) {
7300 case GST_STATE_CHANGE_NULL_TO_READY:
7301 if (!gst_rtspsrc_start (rtspsrc))
7304 case GST_STATE_CHANGE_READY_TO_PAUSED:
7305 /* init some state */
7306 rtspsrc->cur_protocols = rtspsrc->protocols;
7307 /* first attempt, don't ignore timeouts */
7308 rtspsrc->ignore_timeout = FALSE;
7309 rtspsrc->open_error = FALSE;
7310 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
7312 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7313 set_manager_buffer_mode (rtspsrc);
7315 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7316 /* unblock the tcp tasks and make the loop waiting */
7317 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
7318 /* make sure it is waiting before we send PAUSE or PLAY below */
7319 GST_RTSP_STREAM_LOCK (rtspsrc);
7320 GST_RTSP_STREAM_UNLOCK (rtspsrc);
7323 case GST_STATE_CHANGE_PAUSED_TO_READY:
7329 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
7330 if (ret == GST_STATE_CHANGE_FAILURE)
7333 switch (transition) {
7334 case GST_STATE_CHANGE_NULL_TO_READY:
7335 ret = GST_STATE_CHANGE_SUCCESS;
7337 case GST_STATE_CHANGE_READY_TO_PAUSED:
7338 ret = GST_STATE_CHANGE_NO_PREROLL;
7340 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7341 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
7342 ret = GST_STATE_CHANGE_SUCCESS;
7344 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7345 /* send pause request and keep the idle task around */
7346 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
7347 ret = GST_STATE_CHANGE_NO_PREROLL;
7349 case GST_STATE_CHANGE_PAUSED_TO_READY:
7350 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
7351 ret = GST_STATE_CHANGE_SUCCESS;
7353 case GST_STATE_CHANGE_READY_TO_NULL:
7354 gst_rtspsrc_stop (rtspsrc);
7355 ret = GST_STATE_CHANGE_SUCCESS;
7366 GST_DEBUG_OBJECT (rtspsrc, "start failed");
7367 return GST_STATE_CHANGE_FAILURE;
7372 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
7375 GstRTSPSrc *rtspsrc;
7377 rtspsrc = GST_RTSPSRC (element);
7379 if (GST_EVENT_IS_DOWNSTREAM (event)) {
7380 res = gst_rtspsrc_push_event (rtspsrc, event);
7382 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
7389 /*** GSTURIHANDLER INTERFACE *************************************************/
7392 gst_rtspsrc_uri_get_type (GType type)
7397 static const gchar *const *
7398 gst_rtspsrc_uri_get_protocols (GType type)
7400 static const gchar *protocols[] =
7401 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
7402 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
7409 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
7411 GstRTSPSrc *src = GST_RTSPSRC (handler);
7413 /* FIXME: make thread-safe */
7414 return g_strdup (src->conninfo.location);
7418 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
7423 GstRTSPUrl *newurl = NULL;
7424 GstSDPMessage *sdp = NULL;
7426 src = GST_RTSPSRC (handler);
7428 /* same URI, we're fine */
7429 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
7432 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
7433 if ((res = gst_sdp_message_new (&sdp) < 0))
7436 GST_DEBUG_OBJECT (src, "parsing SDP message");
7437 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
7441 GST_DEBUG_OBJECT (src, "parsing URI");
7442 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
7446 /* if worked, free previous and store new url object along with the original
7448 GST_DEBUG_OBJECT (src, "configuring URI");
7449 g_free (src->conninfo.location);
7450 src->conninfo.location = g_strdup (uri);
7451 gst_rtsp_url_free (src->conninfo.url);
7452 src->conninfo.url = newurl;
7453 g_free (src->conninfo.url_str);
7455 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
7457 src->conninfo.url_str = NULL;
7460 gst_sdp_message_free (src->sdp);
7462 src->from_sdp = sdp != NULL;
7464 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
7465 GST_DEBUG_OBJECT (src, "request uri is: %s",
7466 GST_STR_NULL (src->conninfo.url_str));
7473 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
7478 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
7479 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7480 "Could not create SDP");
7485 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
7486 GST_STR_NULL (uri));
7487 gst_sdp_message_free (sdp);
7488 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7494 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
7495 GST_STR_NULL (uri), res);
7496 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7497 "Invalid RTSP URI");
7503 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
7505 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
7507 iface->get_type = gst_rtspsrc_uri_get_type;
7508 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
7509 iface->get_uri = gst_rtspsrc_uri_get_uri;
7510 iface->set_uri = gst_rtspsrc_uri_set_uri;