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,
129 enum _GstRtspSrcRtcpSyncMode
136 enum _GstRtspSrcBufferMode
145 #define GST_TYPE_RTSP_SRC_BUFFER_MODE (gst_rtsp_src_buffer_mode_get_type())
147 gst_rtsp_src_buffer_mode_get_type (void)
149 static GType buffer_mode_type = 0;
150 static const GEnumValue buffer_modes[] = {
151 {BUFFER_MODE_NONE, "Only use RTP timestamps", "none"},
152 {BUFFER_MODE_SLAVE, "Slave receiver to sender clock", "slave"},
153 {BUFFER_MODE_BUFFER, "Do low/high watermark buffering", "buffer"},
154 {BUFFER_MODE_AUTO, "Choose mode depending on stream live", "auto"},
155 {BUFFER_MODE_SYNCED, "Synchronized sender and receiver clocks", "synced"},
159 if (!buffer_mode_type) {
161 g_enum_register_static ("GstRTSPSrcBufferMode", buffer_modes);
163 return buffer_mode_type;
166 #define DEFAULT_LOCATION NULL
167 #define DEFAULT_PROTOCOLS GST_RTSP_LOWER_TRANS_UDP | GST_RTSP_LOWER_TRANS_UDP_MCAST | GST_RTSP_LOWER_TRANS_TCP
168 #define DEFAULT_DEBUG FALSE
169 #define DEFAULT_RETRY 20
170 #define DEFAULT_TIMEOUT 5000000
171 #define DEFAULT_UDP_BUFFER_SIZE 0x80000
172 #define DEFAULT_TCP_TIMEOUT 20000000
173 #define DEFAULT_LATENCY_MS 2000
174 #define DEFAULT_DROP_ON_LATENCY FALSE
175 #define DEFAULT_CONNECTION_SPEED 0
176 #define DEFAULT_NAT_METHOD GST_RTSP_NAT_DUMMY
177 #define DEFAULT_DO_RTCP TRUE
178 #define DEFAULT_DO_RTSP_KEEP_ALIVE TRUE
179 #define DEFAULT_PROXY NULL
180 #define DEFAULT_RTP_BLOCKSIZE 0
181 #define DEFAULT_USER_ID NULL
182 #define DEFAULT_USER_PW NULL
183 #define DEFAULT_BUFFER_MODE BUFFER_MODE_AUTO
184 #define DEFAULT_PORT_RANGE NULL
185 #define DEFAULT_SHORT_HEADER FALSE
186 #define DEFAULT_PROBATION 2
187 #define DEFAULT_UDP_RECONNECT TRUE
188 #define DEFAULT_MULTICAST_IFACE NULL
189 #define DEFAULT_NTP_SYNC FALSE
190 #define DEFAULT_USE_PIPELINE_CLOCK FALSE
191 #define DEFAULT_TLS_VALIDATION_FLAGS G_TLS_CERTIFICATE_VALIDATE_ALL
203 PROP_DROP_ON_LATENCY,
204 PROP_CONNECTION_SPEED,
207 PROP_DO_RTSP_KEEP_ALIVE,
216 PROP_UDP_BUFFER_SIZE,
220 PROP_MULTICAST_IFACE,
222 PROP_USE_PIPELINE_CLOCK,
224 PROP_TLS_VALIDATION_FLAGS,
228 #define GST_TYPE_RTSP_NAT_METHOD (gst_rtsp_nat_method_get_type())
230 gst_rtsp_nat_method_get_type (void)
232 static GType rtsp_nat_method_type = 0;
233 static const GEnumValue rtsp_nat_method[] = {
234 {GST_RTSP_NAT_NONE, "None", "none"},
235 {GST_RTSP_NAT_DUMMY, "Send Dummy packets", "dummy"},
239 if (!rtsp_nat_method_type) {
240 rtsp_nat_method_type =
241 g_enum_register_static ("GstRTSPNatMethod", rtsp_nat_method);
243 return rtsp_nat_method_type;
246 static void gst_rtspsrc_finalize (GObject * object);
248 static void gst_rtspsrc_set_property (GObject * object, guint prop_id,
249 const GValue * value, GParamSpec * pspec);
250 static void gst_rtspsrc_get_property (GObject * object, guint prop_id,
251 GValue * value, GParamSpec * pspec);
253 static GstClock *gst_rtspsrc_provide_clock (GstElement * element);
255 static void gst_rtspsrc_uri_handler_init (gpointer g_iface,
256 gpointer iface_data);
258 static void gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes,
261 static gboolean gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy);
262 static void gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout);
264 static GstCaps *gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media);
266 static GstStateChangeReturn gst_rtspsrc_change_state (GstElement * element,
267 GstStateChange transition);
268 static gboolean gst_rtspsrc_send_event (GstElement * element, GstEvent * event);
269 static void gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message);
271 static gboolean gst_rtspsrc_setup_auth (GstRTSPSrc * src,
272 GstRTSPMessage * response);
274 static gboolean gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd,
276 static GstRTSPResult gst_rtspsrc_send_cb (GstRTSPExtension * ext,
277 GstRTSPMessage * request, GstRTSPMessage * response, GstRTSPSrc * src);
279 static GstRTSPResult gst_rtspsrc_open (GstRTSPSrc * src, gboolean async);
280 static GstRTSPResult gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment,
282 static GstRTSPResult gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async);
283 static GstRTSPResult gst_rtspsrc_close (GstRTSPSrc * src, gboolean async,
284 gboolean only_close);
286 static gboolean gst_rtspsrc_uri_set_uri (GstURIHandler * handler,
287 const gchar * uri, GError ** error);
288 static gchar *gst_rtspsrc_uri_get_uri (GstURIHandler * handler);
290 static gboolean gst_rtspsrc_activate_streams (GstRTSPSrc * src);
291 static gboolean gst_rtspsrc_loop (GstRTSPSrc * src);
292 static gboolean gst_rtspsrc_stream_push_event (GstRTSPSrc * src,
293 GstRTSPStream * stream, GstEvent * event);
294 static gboolean gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event);
296 /* commands we send to out loop to notify it of events */
297 #define CMD_OPEN (1 << 0)
298 #define CMD_PLAY (1 << 1)
299 #define CMD_PAUSE (1 << 2)
300 #define CMD_CLOSE (1 << 3)
301 #define CMD_WAIT (1 << 4)
302 #define CMD_RECONNECT (1 << 5)
303 #define CMD_LOOP (1 << 6)
305 /* mask for all commands */
306 #define CMD_ALL ((CMD_LOOP << 1) - 1)
308 #define GST_ELEMENT_PROGRESS(el, type, code, text) \
310 gchar *__txt = _gst_element_error_printf text; \
311 gst_element_post_message (GST_ELEMENT_CAST (el), \
312 gst_message_new_progress (GST_OBJECT_CAST (el), \
313 GST_PROGRESS_TYPE_ ##type, code, __txt)); \
317 static guint gst_rtspsrc_signals[LAST_SIGNAL] = { 0 };
319 #define gst_rtspsrc_parent_class parent_class
320 G_DEFINE_TYPE_WITH_CODE (GstRTSPSrc, gst_rtspsrc, GST_TYPE_BIN,
321 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_rtspsrc_uri_handler_init));
324 default_select_stream (GstRTSPSrc * src, guint id, GstCaps * caps)
326 GST_DEBUG_OBJECT (src, "default handler");
331 select_stream_accum (GSignalInvocationHint * ihint,
332 GValue * return_accu, const GValue * handler_return, gpointer data)
336 myboolean = g_value_get_boolean (handler_return);
337 GST_DEBUG ("accum %d", myboolean);
338 g_value_set_boolean (return_accu, myboolean);
340 /* stop emission if FALSE */
345 gst_rtspsrc_class_init (GstRTSPSrcClass * klass)
347 GObjectClass *gobject_class;
348 GstElementClass *gstelement_class;
349 GstBinClass *gstbin_class;
351 gobject_class = (GObjectClass *) klass;
352 gstelement_class = (GstElementClass *) klass;
353 gstbin_class = (GstBinClass *) klass;
355 GST_DEBUG_CATEGORY_INIT (rtspsrc_debug, "rtspsrc", 0, "RTSP src");
357 gobject_class->set_property = gst_rtspsrc_set_property;
358 gobject_class->get_property = gst_rtspsrc_get_property;
360 gobject_class->finalize = gst_rtspsrc_finalize;
362 g_object_class_install_property (gobject_class, PROP_LOCATION,
363 g_param_spec_string ("location", "RTSP Location",
364 "Location of the RTSP url to read",
365 DEFAULT_LOCATION, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
367 g_object_class_install_property (gobject_class, PROP_PROTOCOLS,
368 g_param_spec_flags ("protocols", "Protocols",
369 "Allowed lower transport protocols", GST_TYPE_RTSP_LOWER_TRANS,
370 DEFAULT_PROTOCOLS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
372 g_object_class_install_property (gobject_class, PROP_DEBUG,
373 g_param_spec_boolean ("debug", "Debug",
374 "Dump request and response messages to stdout",
375 DEFAULT_DEBUG, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
377 g_object_class_install_property (gobject_class, PROP_RETRY,
378 g_param_spec_uint ("retry", "Retry",
379 "Max number of retries when allocating RTP ports.",
380 0, G_MAXUINT16, DEFAULT_RETRY,
381 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
383 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
384 g_param_spec_uint64 ("timeout", "Timeout",
385 "Retry TCP transport after UDP timeout microseconds (0 = disabled)",
386 0, G_MAXUINT64, DEFAULT_TIMEOUT,
387 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
389 g_object_class_install_property (gobject_class, PROP_TCP_TIMEOUT,
390 g_param_spec_uint64 ("tcp-timeout", "TCP Timeout",
391 "Fail after timeout microseconds on TCP connections (0 = disabled)",
392 0, G_MAXUINT64, DEFAULT_TCP_TIMEOUT,
393 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
395 g_object_class_install_property (gobject_class, PROP_LATENCY,
396 g_param_spec_uint ("latency", "Buffer latency in ms",
397 "Amount of ms to buffer", 0, G_MAXUINT, DEFAULT_LATENCY_MS,
398 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
400 g_object_class_install_property (gobject_class, PROP_DROP_ON_LATENCY,
401 g_param_spec_boolean ("drop-on-latency",
402 "Drop buffers when maximum latency is reached",
403 "Tells the jitterbuffer to never exceed the given latency in size",
404 DEFAULT_DROP_ON_LATENCY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
406 g_object_class_install_property (gobject_class, PROP_CONNECTION_SPEED,
407 g_param_spec_uint64 ("connection-speed", "Connection Speed",
408 "Network connection speed in kbps (0 = unknown)",
409 0, G_MAXUINT64 / 1000, DEFAULT_CONNECTION_SPEED,
410 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
412 g_object_class_install_property (gobject_class, PROP_NAT_METHOD,
413 g_param_spec_enum ("nat-method", "NAT Method",
414 "Method to use for traversing firewalls and NAT",
415 GST_TYPE_RTSP_NAT_METHOD, DEFAULT_NAT_METHOD,
416 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
419 * GstRTSPSrc::do-rtcp
421 * Enable RTCP support. Some old server don't like RTCP and then this property
422 * needs to be set to FALSE.
426 g_object_class_install_property (gobject_class, PROP_DO_RTCP,
427 g_param_spec_boolean ("do-rtcp", "Do RTCP",
428 "Send RTCP packets, disable for old incompatible server.",
429 DEFAULT_DO_RTCP, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
432 * GstRTSPSrc::do-rtsp-keep-alive
434 * Enable RTSP keep laive support. Some old server don't like RTSP
435 * keep alive and then this property needs to be set to FALSE.
439 g_object_class_install_property (gobject_class, PROP_DO_RTSP_KEEP_ALIVE,
440 g_param_spec_boolean ("do-rtsp-keep-alive", "Do RTSP Keep Alive",
441 "Send RTSP keep alive packets, disable for old incompatible server.",
442 DEFAULT_DO_RTSP_KEEP_ALIVE,
443 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
448 * Set the proxy parameters. This has to be a string of the format
449 * [http://][user:passwd@]host[:port].
453 g_object_class_install_property (gobject_class, PROP_PROXY,
454 g_param_spec_string ("proxy", "Proxy",
455 "Proxy settings for HTTP tunneling. Format: [http://][user:passwd@]host[:port]",
456 DEFAULT_PROXY, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
458 * GstRTSPSrc::proxy-id
460 * Sets the proxy URI user id for authentication. If the URI set via the
461 * "proxy" property contains a user-id already, that will take precedence.
465 g_object_class_install_property (gobject_class, PROP_PROXY_ID,
466 g_param_spec_string ("proxy-id", "proxy-id",
467 "HTTP proxy URI user id for authentication", "",
468 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
470 * GstRTSPSrc::proxy-pw
472 * Sets the proxy URI password for authentication. If the URI set via the
473 * "proxy" property contains a password already, that will take precedence.
477 g_object_class_install_property (gobject_class, PROP_PROXY_PW,
478 g_param_spec_string ("proxy-pw", "proxy-pw",
479 "HTTP proxy URI user password for authentication", "",
480 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
483 * GstRTSPSrc::rtp_blocksize
485 * RTP package size to suggest to server.
489 g_object_class_install_property (gobject_class, PROP_RTP_BLOCKSIZE,
490 g_param_spec_uint ("rtp-blocksize", "RTP Blocksize",
491 "RTP package size to suggest to server (0 = disabled)",
492 0, 65536, DEFAULT_RTP_BLOCKSIZE,
493 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
495 g_object_class_install_property (gobject_class,
497 g_param_spec_string ("user-id", "user-id",
498 "RTSP location URI user id for authentication", DEFAULT_USER_ID,
499 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
500 g_object_class_install_property (gobject_class, PROP_USER_PW,
501 g_param_spec_string ("user-pw", "user-pw",
502 "RTSP location URI user password for authentication", DEFAULT_USER_PW,
503 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
506 * GstRTSPSrc::buffer-mode:
508 * Control the buffering and timestamping mode used by the jitterbuffer.
512 g_object_class_install_property (gobject_class, PROP_BUFFER_MODE,
513 g_param_spec_enum ("buffer-mode", "Buffer Mode",
514 "Control the buffering algorithm in use",
515 GST_TYPE_RTSP_SRC_BUFFER_MODE, DEFAULT_BUFFER_MODE,
516 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
519 * GstRTSPSrc::port-range:
521 * Configure the client port numbers that can be used to recieve RTP and
526 g_object_class_install_property (gobject_class, PROP_PORT_RANGE,
527 g_param_spec_string ("port-range", "Port range",
528 "Client port range that can be used to receive RTP and RTCP data, "
529 "eg. 3000-3005 (NULL = no restrictions)", DEFAULT_PORT_RANGE,
530 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
533 * GstRTSPSrc::udp-buffer-size:
535 * Size of the kernel UDP receive buffer in bytes.
539 g_object_class_install_property (gobject_class, PROP_UDP_BUFFER_SIZE,
540 g_param_spec_int ("udp-buffer-size", "UDP Buffer Size",
541 "Size of the kernel UDP receive buffer in bytes, 0=default",
542 0, G_MAXINT, DEFAULT_UDP_BUFFER_SIZE,
543 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
546 * GstRTSPSrc::short-header:
548 * Only send the basic RTSP headers for broken encoders.
552 g_object_class_install_property (gobject_class, PROP_SHORT_HEADER,
553 g_param_spec_boolean ("short-header", "Short Header",
554 "Only send the basic RTSP headers for broken encoders",
555 DEFAULT_SHORT_HEADER, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
557 g_object_class_install_property (gobject_class, PROP_PROBATION,
558 g_param_spec_uint ("probation", "Number of probations",
559 "Consecutive packet sequence numbers to accept the source",
560 0, G_MAXUINT, DEFAULT_PROBATION,
561 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
563 g_object_class_install_property (gobject_class, PROP_UDP_RECONNECT,
564 g_param_spec_boolean ("udp-reconnect", "Reconnect to the server",
565 "Reconnect to the server if RTSP connection is closed when doing UDP",
566 DEFAULT_UDP_RECONNECT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
568 g_object_class_install_property (gobject_class, PROP_MULTICAST_IFACE,
569 g_param_spec_string ("multicast-iface", "Multicast Interface",
570 "The network interface on which to join the multicast group",
571 DEFAULT_MULTICAST_IFACE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
573 g_object_class_install_property (gobject_class, PROP_NTP_SYNC,
574 g_param_spec_boolean ("ntp-sync", "Sync on NTP clock",
575 "Synchronize received streams to the NTP clock", DEFAULT_NTP_SYNC,
576 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
578 g_object_class_install_property (gobject_class, PROP_USE_PIPELINE_CLOCK,
579 g_param_spec_boolean ("use-pipeline-clock", "Use pipeline clock",
580 "Use the pipeline running-time to set the NTP time in the RTCP SR messages",
581 DEFAULT_USE_PIPELINE_CLOCK,
582 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
584 g_object_class_install_property (gobject_class, PROP_SDES,
585 g_param_spec_boxed ("sdes", "SDES",
586 "The SDES items of this session",
587 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
590 * GstRTSPSrc::tls-validation-flags:
592 * TLS certificate validation flags used to validate server
597 g_object_class_install_property (gobject_class, PROP_TLS_VALIDATION_FLAGS,
598 g_param_spec_flags ("tls-validation-flags", "TLS validation flags",
599 "TLS certificate validation flags used to validate the server certificate",
600 G_TYPE_TLS_CERTIFICATE_FLAGS, DEFAULT_TLS_VALIDATION_FLAGS,
601 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
604 * GstRTSPSrc::handle-request:
605 * @rtspsrc: a #GstRTSPSrc
606 * @request: a #GstRTSPMessage
607 * @response: a #GstRTSPMessage
609 * Handle a server request in @request and prepare @response.
611 * This signal is called from the streaming thread, you should therefore not
612 * do any state changes on @rtspsrc because this might deadlock. If you want
613 * to modify the state as a result of this signal, post a
614 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
619 gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST] =
620 g_signal_new ("handle-request", G_TYPE_FROM_CLASS (klass), 0,
621 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 2,
622 G_TYPE_POINTER, G_TYPE_POINTER);
625 * GstRTSPSrc::on-sdp:
626 * @rtspsrc: a #GstRTSPSrc
627 * @sdp: a #GstSDPMessage
629 * Emited when the client has retrieved the SDP and before it configures the
630 * streams in the SDP. @sdp can be inspected and modified.
632 * This signal is called from the streaming thread, you should therefore not
633 * do any state changes on @rtspsrc because this might deadlock. If you want
634 * to modify the state as a result of this signal, post a
635 * #GST_MESSAGE_REQUEST_STATE message on the bus or signal the main thread
640 gst_rtspsrc_signals[SIGNAL_ON_SDP] =
641 g_signal_new ("on-sdp", G_TYPE_FROM_CLASS (klass), 0,
642 0, NULL, NULL, g_cclosure_marshal_generic, G_TYPE_NONE, 1,
643 GST_TYPE_SDP_MESSAGE | G_SIGNAL_TYPE_STATIC_SCOPE);
646 * GstRTSPSrc::select-stream:
647 * @rtspsrc: a #GstRTSPSrc
648 * @num: the stream number
649 * @caps: the stream caps
651 * Emited before the client decides to configure the stream @num with
654 * Returns: %TRUE when the stream should be selected, %FALSE when the stream
659 gst_rtspsrc_signals[SIGNAL_SELECT_STREAM] =
660 g_signal_new_class_handler ("select-stream", G_TYPE_FROM_CLASS (klass),
661 G_SIGNAL_RUN_FIRST | G_SIGNAL_RUN_CLEANUP,
662 (GCallback) default_select_stream, select_stream_accum, NULL,
663 g_cclosure_marshal_generic, G_TYPE_BOOLEAN, 2, G_TYPE_UINT,
666 gstelement_class->send_event = gst_rtspsrc_send_event;
667 gstelement_class->provide_clock = gst_rtspsrc_provide_clock;
668 gstelement_class->change_state = gst_rtspsrc_change_state;
670 gst_element_class_add_pad_template (gstelement_class,
671 gst_static_pad_template_get (&rtptemplate));
673 gst_element_class_set_static_metadata (gstelement_class,
674 "RTSP packet receiver", "Source/Network",
675 "Receive data over the network via RTSP (RFC 2326)",
676 "Wim Taymans <wim@fluendo.com>, "
677 "Thijs Vermeir <thijs.vermeir@barco.com>, "
678 "Lutz Mueller <lutz@topfrose.de>");
680 gstbin_class->handle_message = gst_rtspsrc_handle_message;
682 gst_rtsp_ext_list_init ();
686 gst_rtspsrc_init (GstRTSPSrc * src)
688 src->conninfo.location = g_strdup (DEFAULT_LOCATION);
689 src->protocols = DEFAULT_PROTOCOLS;
690 src->debug = DEFAULT_DEBUG;
691 src->retry = DEFAULT_RETRY;
692 src->udp_timeout = DEFAULT_TIMEOUT;
693 gst_rtspsrc_set_tcp_timeout (src, DEFAULT_TCP_TIMEOUT);
694 src->latency = DEFAULT_LATENCY_MS;
695 src->drop_on_latency = DEFAULT_DROP_ON_LATENCY;
696 src->connection_speed = DEFAULT_CONNECTION_SPEED;
697 src->nat_method = DEFAULT_NAT_METHOD;
698 src->do_rtcp = DEFAULT_DO_RTCP;
699 src->do_rtsp_keep_alive = DEFAULT_DO_RTSP_KEEP_ALIVE;
700 gst_rtspsrc_set_proxy (src, DEFAULT_PROXY);
701 src->rtp_blocksize = DEFAULT_RTP_BLOCKSIZE;
702 src->user_id = g_strdup (DEFAULT_USER_ID);
703 src->user_pw = g_strdup (DEFAULT_USER_PW);
704 src->buffer_mode = DEFAULT_BUFFER_MODE;
705 src->client_port_range.min = 0;
706 src->client_port_range.max = 0;
707 src->udp_buffer_size = DEFAULT_UDP_BUFFER_SIZE;
708 src->short_header = DEFAULT_SHORT_HEADER;
709 src->probation = DEFAULT_PROBATION;
710 src->udp_reconnect = DEFAULT_UDP_RECONNECT;
711 src->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
712 src->ntp_sync = DEFAULT_NTP_SYNC;
713 src->use_pipeline_clock = DEFAULT_USE_PIPELINE_CLOCK;
715 src->tls_validation_flags = DEFAULT_TLS_VALIDATION_FLAGS;
717 /* get a list of all extensions */
718 src->extensions = gst_rtsp_ext_list_get ();
720 /* connect to send signal */
721 gst_rtsp_ext_list_connect (src->extensions, "send",
722 (GCallback) gst_rtspsrc_send_cb, src);
724 /* protects the streaming thread in interleaved mode or the polling
725 * thread in UDP mode. */
726 g_rec_mutex_init (&src->stream_rec_lock);
728 /* protects our state changes from multiple invocations */
729 g_rec_mutex_init (&src->state_rec_lock);
731 src->state = GST_RTSP_STATE_INVALID;
733 GST_OBJECT_FLAG_SET (src, GST_ELEMENT_FLAG_SOURCE);
737 gst_rtspsrc_finalize (GObject * object)
741 rtspsrc = GST_RTSPSRC (object);
743 gst_rtsp_ext_list_free (rtspsrc->extensions);
744 g_free (rtspsrc->conninfo.location);
745 gst_rtsp_url_free (rtspsrc->conninfo.url);
746 g_free (rtspsrc->conninfo.url_str);
747 g_free (rtspsrc->user_id);
748 g_free (rtspsrc->user_pw);
749 g_free (rtspsrc->multi_iface);
752 gst_sdp_message_free (rtspsrc->sdp);
755 if (rtspsrc->provided_clock)
756 gst_object_unref (rtspsrc->provided_clock);
759 gst_structure_free (rtspsrc->sdes);
762 g_rec_mutex_clear (&rtspsrc->stream_rec_lock);
763 g_rec_mutex_clear (&rtspsrc->state_rec_lock);
765 G_OBJECT_CLASS (parent_class)->finalize (object);
769 gst_rtspsrc_provide_clock (GstElement * element)
771 GstRTSPSrc *src = GST_RTSPSRC (element);
774 if ((clock = src->provided_clock) != NULL)
775 gst_object_ref (clock);
780 /* a proxy string of the format [user:passwd@]host[:port] */
782 gst_rtspsrc_set_proxy (GstRTSPSrc * rtsp, const gchar * proxy)
786 g_free (rtsp->proxy_user);
787 rtsp->proxy_user = NULL;
788 g_free (rtsp->proxy_passwd);
789 rtsp->proxy_passwd = NULL;
790 g_free (rtsp->proxy_host);
791 rtsp->proxy_host = NULL;
792 rtsp->proxy_port = 0;
799 /* we allow http:// in front but ignore it */
800 if (g_str_has_prefix (p, "http://"))
803 at = strchr (p, '@');
805 /* look for user:passwd */
806 col = strchr (proxy, ':');
807 if (col == NULL || col > at)
810 rtsp->proxy_user = g_strndup (p, col - p);
812 rtsp->proxy_passwd = g_strndup (col, at - col);
817 if (rtsp->prop_proxy_id != NULL && *rtsp->prop_proxy_id != '\0')
818 rtsp->proxy_user = g_strdup (rtsp->prop_proxy_id);
819 if (rtsp->prop_proxy_pw != NULL && *rtsp->prop_proxy_pw != '\0')
820 rtsp->proxy_passwd = g_strdup (rtsp->prop_proxy_pw);
821 if (rtsp->proxy_user != NULL || rtsp->proxy_passwd != NULL) {
822 GST_LOG_OBJECT (rtsp, "set proxy user/pw from properties: %s:%s",
823 GST_STR_NULL (rtsp->proxy_user), GST_STR_NULL (rtsp->proxy_passwd));
826 col = strchr (p, ':');
829 /* everything before the colon is the hostname */
830 rtsp->proxy_host = g_strndup (p, col - p);
832 rtsp->proxy_port = strtoul (p, (char **) &p, 10);
834 rtsp->proxy_host = g_strdup (p);
835 rtsp->proxy_port = 8080;
841 gst_rtspsrc_set_tcp_timeout (GstRTSPSrc * rtspsrc, guint64 timeout)
843 rtspsrc->tcp_timeout.tv_sec = timeout / G_USEC_PER_SEC;
844 rtspsrc->tcp_timeout.tv_usec = timeout % G_USEC_PER_SEC;
847 rtspsrc->ptcp_timeout = &rtspsrc->tcp_timeout;
849 rtspsrc->ptcp_timeout = NULL;
853 gst_rtspsrc_set_property (GObject * object, guint prop_id, const GValue * value,
858 rtspsrc = GST_RTSPSRC (object);
862 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (rtspsrc),
863 g_value_get_string (value), NULL);
866 rtspsrc->protocols = g_value_get_flags (value);
869 rtspsrc->debug = g_value_get_boolean (value);
872 rtspsrc->retry = g_value_get_uint (value);
875 rtspsrc->udp_timeout = g_value_get_uint64 (value);
877 case PROP_TCP_TIMEOUT:
878 gst_rtspsrc_set_tcp_timeout (rtspsrc, g_value_get_uint64 (value));
881 rtspsrc->latency = g_value_get_uint (value);
883 case PROP_DROP_ON_LATENCY:
884 rtspsrc->drop_on_latency = g_value_get_boolean (value);
886 case PROP_CONNECTION_SPEED:
887 rtspsrc->connection_speed = g_value_get_uint64 (value);
889 case PROP_NAT_METHOD:
890 rtspsrc->nat_method = g_value_get_enum (value);
893 rtspsrc->do_rtcp = g_value_get_boolean (value);
895 case PROP_DO_RTSP_KEEP_ALIVE:
896 rtspsrc->do_rtsp_keep_alive = g_value_get_boolean (value);
899 gst_rtspsrc_set_proxy (rtspsrc, g_value_get_string (value));
902 if (rtspsrc->prop_proxy_id)
903 g_free (rtspsrc->prop_proxy_id);
904 rtspsrc->prop_proxy_id = g_value_dup_string (value);
907 if (rtspsrc->prop_proxy_pw)
908 g_free (rtspsrc->prop_proxy_pw);
909 rtspsrc->prop_proxy_pw = g_value_dup_string (value);
911 case PROP_RTP_BLOCKSIZE:
912 rtspsrc->rtp_blocksize = g_value_get_uint (value);
915 if (rtspsrc->user_id)
916 g_free (rtspsrc->user_id);
917 rtspsrc->user_id = g_value_dup_string (value);
920 if (rtspsrc->user_pw)
921 g_free (rtspsrc->user_pw);
922 rtspsrc->user_pw = g_value_dup_string (value);
924 case PROP_BUFFER_MODE:
925 rtspsrc->buffer_mode = g_value_get_enum (value);
927 case PROP_PORT_RANGE:
931 str = g_value_get_string (value);
933 sscanf (str, "%u-%u",
934 &rtspsrc->client_port_range.min, &rtspsrc->client_port_range.max);
936 rtspsrc->client_port_range.min = 0;
937 rtspsrc->client_port_range.max = 0;
941 case PROP_UDP_BUFFER_SIZE:
942 rtspsrc->udp_buffer_size = g_value_get_int (value);
944 case PROP_SHORT_HEADER:
945 rtspsrc->short_header = g_value_get_boolean (value);
948 rtspsrc->probation = g_value_get_uint (value);
950 case PROP_UDP_RECONNECT:
951 rtspsrc->udp_reconnect = g_value_get_boolean (value);
953 case PROP_MULTICAST_IFACE:
954 g_free (rtspsrc->multi_iface);
956 if (g_value_get_string (value) == NULL)
957 rtspsrc->multi_iface = g_strdup (DEFAULT_MULTICAST_IFACE);
959 rtspsrc->multi_iface = g_value_dup_string (value);
962 rtspsrc->ntp_sync = g_value_get_boolean (value);
964 case PROP_USE_PIPELINE_CLOCK:
965 rtspsrc->use_pipeline_clock = g_value_get_boolean (value);
968 rtspsrc->sdes = g_value_dup_boxed (value);
970 case PROP_TLS_VALIDATION_FLAGS:
971 rtspsrc->tls_validation_flags = g_value_get_flags (value);
974 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
980 gst_rtspsrc_get_property (GObject * object, guint prop_id, GValue * value,
985 rtspsrc = GST_RTSPSRC (object);
989 g_value_set_string (value, rtspsrc->conninfo.location);
992 g_value_set_flags (value, rtspsrc->protocols);
995 g_value_set_boolean (value, rtspsrc->debug);
998 g_value_set_uint (value, rtspsrc->retry);
1001 g_value_set_uint64 (value, rtspsrc->udp_timeout);
1003 case PROP_TCP_TIMEOUT:
1007 timeout = rtspsrc->tcp_timeout.tv_sec * G_USEC_PER_SEC +
1008 rtspsrc->tcp_timeout.tv_usec;
1009 g_value_set_uint64 (value, timeout);
1013 g_value_set_uint (value, rtspsrc->latency);
1015 case PROP_DROP_ON_LATENCY:
1016 g_value_set_boolean (value, rtspsrc->drop_on_latency);
1018 case PROP_CONNECTION_SPEED:
1019 g_value_set_uint64 (value, rtspsrc->connection_speed);
1021 case PROP_NAT_METHOD:
1022 g_value_set_enum (value, rtspsrc->nat_method);
1025 g_value_set_boolean (value, rtspsrc->do_rtcp);
1027 case PROP_DO_RTSP_KEEP_ALIVE:
1028 g_value_set_boolean (value, rtspsrc->do_rtsp_keep_alive);
1034 if (rtspsrc->proxy_host) {
1036 g_strdup_printf ("%s:%d", rtspsrc->proxy_host, rtspsrc->proxy_port);
1040 g_value_take_string (value, str);
1044 g_value_set_string (value, rtspsrc->prop_proxy_id);
1047 g_value_set_string (value, rtspsrc->prop_proxy_pw);
1049 case PROP_RTP_BLOCKSIZE:
1050 g_value_set_uint (value, rtspsrc->rtp_blocksize);
1053 g_value_set_string (value, rtspsrc->user_id);
1056 g_value_set_string (value, rtspsrc->user_pw);
1058 case PROP_BUFFER_MODE:
1059 g_value_set_enum (value, rtspsrc->buffer_mode);
1061 case PROP_PORT_RANGE:
1065 if (rtspsrc->client_port_range.min != 0) {
1066 str = g_strdup_printf ("%u-%u", rtspsrc->client_port_range.min,
1067 rtspsrc->client_port_range.max);
1071 g_value_take_string (value, str);
1074 case PROP_UDP_BUFFER_SIZE:
1075 g_value_set_int (value, rtspsrc->udp_buffer_size);
1077 case PROP_SHORT_HEADER:
1078 g_value_set_boolean (value, rtspsrc->short_header);
1080 case PROP_PROBATION:
1081 g_value_set_uint (value, rtspsrc->probation);
1083 case PROP_UDP_RECONNECT:
1084 g_value_set_boolean (value, rtspsrc->udp_reconnect);
1086 case PROP_MULTICAST_IFACE:
1087 g_value_set_string (value, rtspsrc->multi_iface);
1090 g_value_set_boolean (value, rtspsrc->ntp_sync);
1092 case PROP_USE_PIPELINE_CLOCK:
1093 g_value_set_boolean (value, rtspsrc->use_pipeline_clock);
1096 g_value_set_boxed (value, rtspsrc->sdes);
1098 case PROP_TLS_VALIDATION_FLAGS:
1099 g_value_set_flags (value, rtspsrc->tls_validation_flags);
1102 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1108 find_stream_by_id (GstRTSPStream * stream, gint * id)
1110 if (stream->id == *id)
1117 find_stream_by_channel (GstRTSPStream * stream, gint * channel)
1119 if (stream->channel[0] == *channel || stream->channel[1] == *channel)
1126 find_stream_by_pt (GstRTSPStream * stream, gint * pt)
1128 if (stream->pt == *pt)
1135 find_stream_by_udpsrc (GstRTSPStream * stream, gconstpointer a)
1137 GstElement *src = (GstElement *) a;
1139 if (stream->udpsrc[0] == src)
1141 if (stream->udpsrc[1] == src)
1148 find_stream_by_setup (GstRTSPStream * stream, gconstpointer a)
1150 /* check qualified setup_url */
1151 if (!strcmp (stream->conninfo.location, (gchar *) a))
1153 /* check original control_url */
1154 if (!strcmp (stream->control_url, (gchar *) a))
1157 /* check if qualified setup_url ends with string */
1158 if (g_str_has_suffix (stream->control_url, (gchar *) a))
1164 static GstRTSPStream *
1165 find_stream (GstRTSPSrc * src, gconstpointer data, gconstpointer func)
1169 /* find and get stream */
1170 if ((lstream = g_list_find_custom (src->streams, data, (GCompareFunc) func)))
1171 return (GstRTSPStream *) lstream->data;
1176 static const GstSDPBandwidth *
1177 gst_rtspsrc_get_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1178 const GstSDPMedia * media, const gchar * type)
1182 /* first look in the media specific section */
1183 len = gst_sdp_media_bandwidths_len (media);
1184 for (i = 0; i < len; i++) {
1185 const GstSDPBandwidth *bw = gst_sdp_media_get_bandwidth (media, i);
1187 if (strcmp (bw->bwtype, type) == 0)
1190 /* then look in the message specific section */
1191 len = gst_sdp_message_bandwidths_len (sdp);
1192 for (i = 0; i < len; i++) {
1193 const GstSDPBandwidth *bw = gst_sdp_message_get_bandwidth (sdp, i);
1195 if (strcmp (bw->bwtype, type) == 0)
1202 gst_rtspsrc_collect_bandwidth (GstRTSPSrc * src, const GstSDPMessage * sdp,
1203 const GstSDPMedia * media, GstRTSPStream * stream)
1205 const GstSDPBandwidth *bw;
1207 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_AS)))
1208 stream->as_bandwidth = bw->bandwidth;
1210 stream->as_bandwidth = -1;
1212 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RR)))
1213 stream->rr_bandwidth = bw->bandwidth;
1215 stream->rr_bandwidth = -1;
1217 if ((bw = gst_rtspsrc_get_bandwidth (src, sdp, media, GST_SDP_BWTYPE_RS)))
1218 stream->rs_bandwidth = bw->bandwidth;
1220 stream->rs_bandwidth = -1;
1224 gst_rtspsrc_do_stream_connection (GstRTSPSrc * src, GstRTSPStream * stream,
1225 const GstSDPConnection * conn)
1227 if (conn->nettype == NULL || strcmp (conn->nettype, "IN") != 0)
1230 if (conn->addrtype == NULL)
1233 /* check for IPV6 */
1234 if (strcmp (conn->addrtype, "IP4") == 0)
1235 stream->is_ipv6 = FALSE;
1236 else if (strcmp (conn->addrtype, "IP6") == 0)
1237 stream->is_ipv6 = TRUE;
1242 g_free (stream->destination);
1243 stream->destination = g_strdup (conn->address);
1245 /* check for multicast */
1246 stream->is_multicast =
1247 gst_sdp_address_is_multicast (conn->nettype, conn->addrtype,
1249 stream->ttl = conn->ttl;
1252 /* Go over the connections for a stream.
1253 * - If we are dealing with IPV6, we will setup IPV6 sockets for sending and
1255 * - If we are dealing with a localhost address, we disable multicast
1258 gst_rtspsrc_collect_connections (GstRTSPSrc * src, const GstSDPMessage * sdp,
1259 const GstSDPMedia * media, GstRTSPStream * stream)
1261 const GstSDPConnection *conn;
1264 /* first look in the media specific section */
1265 len = gst_sdp_media_connections_len (media);
1266 for (i = 0; i < len; i++) {
1267 conn = gst_sdp_media_get_connection (media, i);
1269 gst_rtspsrc_do_stream_connection (src, stream, conn);
1271 /* then look in the message specific section */
1272 if ((conn = gst_sdp_message_get_connection (sdp))) {
1273 gst_rtspsrc_do_stream_connection (src, stream, conn);
1277 static GstRTSPStream *
1278 gst_rtspsrc_create_stream (GstRTSPSrc * src, GstSDPMessage * sdp, gint idx)
1280 GstRTSPStream *stream;
1281 const gchar *control_url;
1282 const gchar *payload;
1283 const GstSDPMedia *media;
1285 /* get media, should not return NULL */
1286 media = gst_sdp_message_get_media (sdp, idx);
1290 stream = g_new0 (GstRTSPStream, 1);
1291 stream->parent = src;
1292 /* we mark the pad as not linked, we will mark it as OK when we add the pad to
1294 stream->last_ret = GST_FLOW_NOT_LINKED;
1295 stream->added = FALSE;
1296 stream->disabled = FALSE;
1297 stream->id = src->numstreams++;
1298 stream->eos = FALSE;
1299 stream->discont = TRUE;
1300 stream->seqbase = -1;
1301 stream->timebase = -1;
1303 /* collect bandwidth information for this steam. FIXME, configure in the RTP
1304 * session manager to scale RTCP. */
1305 gst_rtspsrc_collect_bandwidth (src, sdp, media, stream);
1307 /* collect connection info */
1308 gst_rtspsrc_collect_connections (src, sdp, media, stream);
1310 /* we must have a payload. No payload means we cannot create caps */
1311 /* FIXME, handle multiple formats. The problem here is that we just want to
1312 * take the first available format that we can handle but in order to do that
1313 * we need to scan for depayloader plugins. Scanning for payloader plugins is
1314 * also suboptimal because the user maybe just wants to save the raw stream
1315 * and then we don't care. */
1316 if ((payload = gst_sdp_media_get_format (media, 0))) {
1317 stream->pt = atoi (payload);
1319 stream->caps = gst_rtspsrc_media_to_caps (stream->pt, media);
1321 GST_DEBUG ("mapping sdp session level attributes to caps");
1322 gst_rtspsrc_sdp_attributes_to_caps (sdp->attributes, stream->caps);
1323 GST_DEBUG ("mapping sdp media level attributes to caps");
1324 gst_rtspsrc_sdp_attributes_to_caps (media->attributes, stream->caps);
1326 if (stream->pt >= 96) {
1327 /* If we have a dynamic payload type, see if we have a stream with the
1328 * same payload number. If there is one, they are part of the same
1329 * container and we only need to add one pad. */
1330 if (find_stream (src, &stream->pt, (gpointer) find_stream_by_pt)) {
1331 stream->container = TRUE;
1332 GST_DEBUG ("found another stream with pt %d, marking as container",
1337 /* collect port number */
1338 stream->port = gst_sdp_media_get_port (media);
1340 /* get control url to construct the setup url. The setup url is used to
1341 * configure the transport of the stream and is used to identity the stream in
1342 * the RTP-Info header field returned from PLAY. */
1343 control_url = gst_sdp_media_get_attribute_val (media, "control");
1344 if (control_url == NULL)
1345 control_url = gst_sdp_message_get_attribute_val_n (sdp, "control", 0);
1347 GST_DEBUG_OBJECT (src, "stream %d, (%p)", stream->id, stream);
1348 GST_DEBUG_OBJECT (src, " pt: %d", stream->pt);
1349 GST_DEBUG_OBJECT (src, " port: %d", stream->port);
1350 GST_DEBUG_OBJECT (src, " container: %d", stream->container);
1351 GST_DEBUG_OBJECT (src, " caps: %" GST_PTR_FORMAT, stream->caps);
1352 GST_DEBUG_OBJECT (src, " control: %s", GST_STR_NULL (control_url));
1354 if (control_url != NULL) {
1355 stream->control_url = g_strdup (control_url);
1356 /* Build a fully qualified url using the content_base if any or by prefixing
1357 * the original request.
1358 * If the control_url starts with a '/' or a non rtsp: protocol we will most
1359 * likely build a URL that the server will fail to understand, this is ok,
1360 * we will fail then. */
1361 if (g_str_has_prefix (control_url, "rtsp://"))
1362 stream->conninfo.location = g_strdup (control_url);
1367 if (g_strcmp0 (control_url, "*") == 0)
1371 base = src->control;
1372 else if (src->content_base)
1373 base = src->content_base;
1374 else if (src->conninfo.url_str)
1375 base = src->conninfo.url_str;
1379 /* check if the base ends or control starts with / */
1380 has_slash = g_str_has_prefix (control_url, "/");
1381 has_slash = has_slash || g_str_has_suffix (base, "/");
1383 /* concatenate the two strings, insert / when not present */
1384 stream->conninfo.location =
1385 g_strdup_printf ("%s%s%s", base, has_slash ? "" : "/", control_url);
1388 GST_DEBUG_OBJECT (src, " setup: %s",
1389 GST_STR_NULL (stream->conninfo.location));
1391 /* we keep track of all streams */
1392 src->streams = g_list_append (src->streams, stream);
1400 gst_rtspsrc_stream_free (GstRTSPSrc * src, GstRTSPStream * stream)
1404 GST_DEBUG_OBJECT (src, "free stream %p", stream);
1407 gst_caps_unref (stream->caps);
1409 g_free (stream->destination);
1410 g_free (stream->control_url);
1411 g_free (stream->conninfo.location);
1413 for (i = 0; i < 2; i++) {
1414 if (stream->udpsrc[i]) {
1415 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
1416 gst_bin_remove (GST_BIN_CAST (src), stream->udpsrc[i]);
1417 gst_object_unref (stream->udpsrc[i]);
1418 stream->udpsrc[i] = NULL;
1420 if (stream->channelpad[i]) {
1421 gst_object_unref (stream->channelpad[i]);
1422 stream->channelpad[i] = NULL;
1424 if (stream->udpsink[i]) {
1425 gst_element_set_state (stream->udpsink[i], GST_STATE_NULL);
1426 gst_bin_remove (GST_BIN_CAST (src), stream->udpsink[i]);
1427 gst_object_unref (stream->udpsink[i]);
1428 stream->udpsink[i] = NULL;
1431 if (stream->fakesrc) {
1432 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
1433 gst_bin_remove (GST_BIN_CAST (src), stream->fakesrc);
1434 gst_object_unref (stream->fakesrc);
1435 stream->fakesrc = NULL;
1437 if (stream->srcpad) {
1438 gst_pad_set_active (stream->srcpad, FALSE);
1439 if (stream->added) {
1440 gst_element_remove_pad (GST_ELEMENT_CAST (src), stream->srcpad);
1441 stream->added = FALSE;
1443 stream->srcpad = NULL;
1445 if (stream->rtcppad) {
1446 gst_object_unref (stream->rtcppad);
1447 stream->rtcppad = NULL;
1449 if (stream->session) {
1450 g_object_unref (stream->session);
1451 stream->session = NULL;
1457 gst_rtspsrc_cleanup (GstRTSPSrc * src)
1461 GST_DEBUG_OBJECT (src, "cleanup");
1463 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1464 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1466 gst_rtspsrc_stream_free (src, stream);
1468 g_list_free (src->streams);
1469 src->streams = NULL;
1471 if (src->manager_sig_id) {
1472 g_signal_handler_disconnect (src->manager, src->manager_sig_id);
1473 src->manager_sig_id = 0;
1475 gst_element_set_state (src->manager, GST_STATE_NULL);
1476 gst_bin_remove (GST_BIN_CAST (src), src->manager);
1477 src->manager = NULL;
1479 src->numstreams = 0;
1481 gst_structure_free (src->props);
1484 g_free (src->content_base);
1485 src->content_base = NULL;
1487 g_free (src->control);
1488 src->control = NULL;
1491 gst_rtsp_range_free (src->range);
1494 /* don't clear the SDP when it was used in the url */
1495 if (src->sdp && !src->from_sdp) {
1496 gst_sdp_message_free (src->sdp);
1499 if (src->start_segment) {
1500 gst_event_unref (src->start_segment);
1501 src->start_segment = NULL;
1503 if (src->provided_clock) {
1504 gst_object_unref (src->provided_clock);
1505 src->provided_clock = NULL;
1509 #define PARSE_INT(p, del, res) \
1512 p = strstr (p, del); \
1522 #define PARSE_STRING(p, del, res) \
1525 p = strstr (p, del); \
1537 #define SKIP_SPACES(p) \
1538 while (*p && g_ascii_isspace (*p)) \
1543 * <payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1546 gst_rtspsrc_parse_rtpmap (const gchar * rtpmap, gint * payload, gchar ** name,
1547 gint * rate, gchar ** params)
1551 p = (gchar *) rtpmap;
1553 PARSE_INT (p, " ", *payload);
1561 PARSE_STRING (p, "/", *name);
1562 if (*name == NULL) {
1563 GST_DEBUG ("no rate, name %s", p);
1564 /* no rate, assume -1 then, this is not supposed to happen but RealMedia
1565 * streams seem to omit the rate. */
1572 p = strstr (p, "/");
1590 * Mapping SDP attributes to caps
1592 * prepend 'a-' to IANA registered sdp attributes names
1593 * (ie: not prefixed with 'x-') in order to avoid
1594 * collision with gstreamer standard caps properties names
1597 gst_rtspsrc_sdp_attributes_to_caps (GArray * attributes, GstCaps * caps)
1599 if (attributes->len > 0) {
1603 s = gst_caps_get_structure (caps, 0);
1605 for (i = 0; i < attributes->len; i++) {
1606 GstSDPAttribute *attr = &g_array_index (attributes, GstSDPAttribute, i);
1607 gchar *tofree, *key;
1611 /* skip some of the attribute we already handle */
1612 if (!strcmp (key, "fmtp"))
1614 if (!strcmp (key, "rtpmap"))
1616 if (!strcmp (key, "control"))
1618 if (!strcmp (key, "range"))
1621 /* string must be valid UTF8 */
1622 if (!g_utf8_validate (attr->value, -1, NULL))
1625 if (!g_str_has_prefix (key, "x-"))
1626 tofree = key = g_strdup_printf ("a-%s", key);
1630 GST_DEBUG ("adding caps: %s=%s", key, attr->value);
1631 gst_structure_set (s, key, G_TYPE_STRING, attr->value, NULL);
1638 * Mapping of caps to and from SDP fields:
1640 * m=<media> <UDP port> RTP/AVP <payload>
1641 * a=rtpmap:<payload> <encoding_name>/<clock_rate>[/<encoding_params>]
1642 * a=fmtp:<payload> <param>[=<value>];...
1645 gst_rtspsrc_media_to_caps (gint pt, const GstSDPMedia * media)
1648 const gchar *rtpmap;
1652 gchar *params = NULL;
1658 /* get and parse rtpmap */
1659 if ((rtpmap = gst_sdp_media_get_attribute_val (media, "rtpmap"))) {
1660 ret = gst_rtspsrc_parse_rtpmap (rtpmap, &payload, &name, &rate, ¶ms);
1662 if (payload != pt) {
1663 /* we ignore the rtpmap if the payload type is different. */
1664 g_warning ("rtpmap of wrong payload type, ignoring");
1670 /* if we failed to parse the rtpmap for a dynamic payload type, we have an
1674 /* else we can ignore */
1675 g_warning ("error parsing rtpmap, ignoring");
1678 /* dynamic payloads need rtpmap or we fail */
1682 /* check if we have a rate, if not, we need to look up the rate from the
1683 * default rates based on the payload types. */
1685 const GstRTPPayloadInfo *info;
1687 if (GST_RTP_PAYLOAD_IS_DYNAMIC (pt)) {
1688 /* dynamic types, use media and encoding_name */
1689 tmp = g_ascii_strdown (media->media, -1);
1690 info = gst_rtp_payload_info_for_name (tmp, name);
1693 /* static types, use payload type */
1694 info = gst_rtp_payload_info_for_pt (pt);
1698 if ((rate = info->clock_rate) == 0)
1701 /* we fail if we cannot find one */
1706 tmp = g_ascii_strdown (media->media, -1);
1707 caps = gst_caps_new_simple ("application/x-unknown",
1708 "media", G_TYPE_STRING, tmp, "payload", G_TYPE_INT, pt, NULL);
1710 s = gst_caps_get_structure (caps, 0);
1712 gst_structure_set (s, "clock-rate", G_TYPE_INT, rate, NULL);
1714 /* encoding name must be upper case */
1716 tmp = g_ascii_strup (name, -1);
1717 gst_structure_set (s, "encoding-name", G_TYPE_STRING, tmp, NULL);
1721 /* params must be lower case */
1722 if (params != NULL) {
1723 tmp = g_ascii_strdown (params, -1);
1724 gst_structure_set (s, "encoding-params", G_TYPE_STRING, tmp, NULL);
1728 /* parse optional fmtp: field */
1729 if ((fmtp = gst_sdp_media_get_attribute_val (media, "fmtp"))) {
1735 /* p is now of the format <payload> <param>[=<value>];... */
1736 PARSE_INT (p, " ", payload);
1737 if (payload != -1 && payload == pt) {
1741 /* <param>[=<value>] are separated with ';' */
1742 pairs = g_strsplit (p, ";", 0);
1743 for (i = 0; pairs[i]; i++) {
1745 const gchar *val, *key;
1747 /* the key may not have a '=', the value can have other '='s */
1748 valpos = strstr (pairs[i], "=");
1750 /* we have a '=' and thus a value, remove the '=' with \0 */
1752 /* value is everything between '=' and ';'. We split the pairs at ;
1753 * boundaries so we can take the remainder of the value. Some servers
1754 * put spaces around the value which we strip off here. Alternatively
1755 * we could strip those spaces in the depayloaders should these spaces
1756 * actually carry any meaning in the future. */
1757 val = g_strstrip (valpos + 1);
1759 /* simple <param>;.. is translated into <param>=1;... */
1762 /* strip the key of spaces, convert key to lowercase but not the value. */
1763 key = g_strstrip (pairs[i]);
1764 if (strlen (key) > 1) {
1765 tmp = g_ascii_strdown (key, -1);
1766 gst_structure_set (s, tmp, G_TYPE_STRING, val, NULL);
1778 g_warning ("rtpmap type not given for dynamic payload %d", pt);
1783 g_warning ("rate unknown for payload type %d", pt);
1789 gst_rtspsrc_alloc_udp_ports (GstRTSPStream * stream,
1790 gint * rtpport, gint * rtcpport)
1793 GstStateChangeReturn ret;
1794 GstElement *udpsrc0, *udpsrc1;
1795 gint tmp_rtp, tmp_rtcp;
1799 src = stream->parent;
1805 /* Start at next port */
1806 tmp_rtp = src->next_port_num;
1808 if (stream->is_ipv6)
1809 host = "udp://[::0]";
1811 host = "udp://0.0.0.0";
1813 /* try to allocate 2 UDP ports, the RTP port should be an even
1814 * number and the RTCP port should be the next (uneven) port */
1817 if (tmp_rtp != 0 && src->client_port_range.max > 0 &&
1818 tmp_rtp >= src->client_port_range.max)
1821 udpsrc0 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1822 if (udpsrc0 == NULL)
1823 goto no_udp_protocol;
1824 g_object_set (G_OBJECT (udpsrc0), "port", tmp_rtp, "reuse", FALSE, NULL);
1826 if (src->udp_buffer_size != 0)
1827 g_object_set (G_OBJECT (udpsrc0), "buffer-size", src->udp_buffer_size,
1830 ret = gst_element_set_state (udpsrc0, GST_STATE_PAUSED);
1831 if (ret == GST_STATE_CHANGE_FAILURE) {
1833 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTP port %d", tmp_rtp);
1836 if (++count > src->retry)
1839 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1840 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1841 gst_object_unref (udpsrc0);
1844 GST_DEBUG_OBJECT (src, "retry %d", count);
1847 goto no_udp_protocol;
1850 g_object_get (G_OBJECT (udpsrc0), "port", &tmp_rtp, NULL);
1851 GST_DEBUG_OBJECT (src, "got RTP port %d", tmp_rtp);
1853 /* check if port is even */
1854 if ((tmp_rtp & 0x01) != 0) {
1855 /* port not even, close and allocate another */
1856 if (++count > src->retry)
1859 GST_DEBUG_OBJECT (src, "RTP port not even");
1861 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1862 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1863 gst_object_unref (udpsrc0);
1866 GST_DEBUG_OBJECT (src, "retry %d", count);
1871 /* allocate port+1 for RTCP now */
1872 udpsrc1 = gst_element_make_from_uri (GST_URI_SRC, host, NULL, NULL);
1873 if (udpsrc1 == NULL)
1874 goto no_udp_rtcp_protocol;
1877 tmp_rtcp = tmp_rtp + 1;
1878 if (src->client_port_range.max > 0 && tmp_rtcp > src->client_port_range.max)
1881 g_object_set (G_OBJECT (udpsrc1), "port", tmp_rtcp, "reuse", FALSE, NULL);
1883 GST_DEBUG_OBJECT (src, "starting RTCP on port %d", tmp_rtcp);
1884 ret = gst_element_set_state (udpsrc1, GST_STATE_PAUSED);
1885 /* tmp_rtcp port is busy already : retry to make rtp/rtcp pair */
1886 if (ret == GST_STATE_CHANGE_FAILURE) {
1887 GST_DEBUG_OBJECT (src, "Unable to make udpsrc from RTCP port %d", tmp_rtcp);
1889 if (++count > src->retry)
1892 GST_DEBUG_OBJECT (src, "free RTP udpsrc");
1893 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1894 gst_object_unref (udpsrc0);
1897 GST_DEBUG_OBJECT (src, "free RTCP udpsrc");
1898 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1899 gst_object_unref (udpsrc1);
1903 GST_DEBUG_OBJECT (src, "retry %d", count);
1907 /* all fine, do port check */
1908 g_object_get (G_OBJECT (udpsrc0), "port", rtpport, NULL);
1909 g_object_get (G_OBJECT (udpsrc1), "port", rtcpport, NULL);
1911 /* this should not happen... */
1912 if (*rtpport != tmp_rtp || *rtcpport != tmp_rtcp)
1915 /* we keep these elements, we configure all in configure_transport when the
1916 * server told us to really use the UDP ports. */
1917 stream->udpsrc[0] = gst_object_ref_sink (udpsrc0);
1918 stream->udpsrc[1] = gst_object_ref_sink (udpsrc1);
1919 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
1920 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
1922 /* keep track of next available port number when we have a range
1924 if (src->next_port_num != 0)
1925 src->next_port_num = tmp_rtcp + 1;
1932 GST_DEBUG_OBJECT (src, "could not get UDP source");
1937 GST_DEBUG_OBJECT (src, "could not allocate UDP port pair after %d retries",
1941 no_udp_rtcp_protocol:
1943 GST_DEBUG_OBJECT (src, "could not get UDP source for RTCP");
1948 GST_DEBUG_OBJECT (src, "ports don't match rtp: %d<->%d, rtcp: %d<->%d",
1949 tmp_rtp, *rtpport, tmp_rtcp, *rtcpport);
1955 gst_element_set_state (udpsrc0, GST_STATE_NULL);
1956 gst_object_unref (udpsrc0);
1959 gst_element_set_state (udpsrc1, GST_STATE_NULL);
1960 gst_object_unref (udpsrc1);
1967 gst_rtspsrc_set_state (GstRTSPSrc * src, GstState state)
1972 gst_element_set_state (GST_ELEMENT_CAST (src->manager), state);
1974 for (walk = src->streams; walk; walk = g_list_next (walk)) {
1975 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
1978 for (i = 0; i < 2; i++) {
1979 if (stream->udpsrc[i])
1980 gst_element_set_state (stream->udpsrc[i], state);
1986 gst_rtspsrc_flush (GstRTSPSrc * src, gboolean flush, gboolean playing)
1993 event = gst_event_new_flush_start ();
1994 GST_DEBUG_OBJECT (src, "start flush");
1996 state = GST_STATE_PAUSED;
1998 event = gst_event_new_flush_stop (FALSE);
1999 GST_DEBUG_OBJECT (src, "stop flush; playing %d", playing);
2002 state = GST_STATE_PLAYING;
2004 state = GST_STATE_PAUSED;
2006 gst_rtspsrc_push_event (src, event);
2007 gst_rtspsrc_loop_send_cmd (src, cmd, CMD_LOOP);
2008 gst_rtspsrc_set_state (src, state);
2011 static GstRTSPResult
2012 gst_rtspsrc_connection_send (GstRTSPSrc * src, GstRTSPConnection * conn,
2013 GstRTSPMessage * message, GTimeVal * timeout)
2018 ret = gst_rtsp_connection_send (conn, message, timeout);
2020 ret = GST_RTSP_ERROR;
2025 static GstRTSPResult
2026 gst_rtspsrc_connection_receive (GstRTSPSrc * src, GstRTSPConnection * conn,
2027 GstRTSPMessage * message, GTimeVal * timeout)
2032 ret = gst_rtsp_connection_receive (conn, message, timeout);
2034 ret = GST_RTSP_ERROR;
2040 gst_rtspsrc_get_position (GstRTSPSrc * src)
2045 query = gst_query_new_position (GST_FORMAT_TIME);
2046 /* should be known somewhere down the stream (e.g. jitterbuffer) */
2047 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2048 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2052 if (stream->srcpad) {
2053 if (gst_pad_query (stream->srcpad, query)) {
2054 gst_query_parse_position (query, &fmt, &pos);
2055 GST_DEBUG_OBJECT (src, "retaining position %" GST_TIME_FORMAT,
2056 GST_TIME_ARGS (pos));
2057 src->last_pos = pos;
2067 gst_rtspsrc_do_seek (GstRTSPSrc * src, GstSegment * segment)
2069 src->state = GST_RTSP_STATE_SEEKING;
2070 /* PLAY will add the range header now. */
2071 src->need_range = TRUE;
2077 gst_rtspsrc_perform_seek (GstRTSPSrc * src, GstEvent * event)
2082 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
2084 gboolean flush, skip;
2087 GstSegment seeksegment = { 0, };
2091 GST_DEBUG_OBJECT (src, "doing seek with event");
2093 gst_event_parse_seek (event, &rate, &format, &flags,
2094 &cur_type, &cur, &stop_type, &stop);
2096 /* no negative rates yet */
2100 /* we need TIME format */
2101 if (format != src->segment.format)
2104 GST_DEBUG_OBJECT (src, "doing seek without event");
2106 cur_type = GST_SEEK_TYPE_SET;
2107 stop_type = GST_SEEK_TYPE_SET;
2110 /* get flush flag */
2111 flush = flags & GST_SEEK_FLAG_FLUSH;
2112 skip = flags & GST_SEEK_FLAG_SKIP;
2114 /* now we need to make sure the streaming thread is stopped. We do this by
2115 * either sending a FLUSH_START event downstream which will cause the
2116 * streaming thread to stop with a WRONG_STATE.
2117 * For a non-flushing seek we simply pause the task, which will happen as soon
2118 * as it completes one iteration (and thus might block when the sink is
2119 * blocking in preroll). */
2121 GST_DEBUG_OBJECT (src, "starting flush");
2122 gst_rtspsrc_flush (src, TRUE, FALSE);
2125 gst_task_pause (src->task);
2129 /* we should now be able to grab the streaming thread because we stopped it
2130 * with the above flush/pause code */
2131 GST_RTSP_STREAM_LOCK (src);
2133 GST_DEBUG_OBJECT (src, "stopped streaming");
2135 /* copy segment, we need this because we still need the old
2136 * segment when we close the current segment. */
2137 memcpy (&seeksegment, &src->segment, sizeof (GstSegment));
2139 /* configure the seek parameters in the seeksegment. We will then have the
2140 * right values in the segment to perform the seek */
2142 GST_DEBUG_OBJECT (src, "configuring seek");
2143 gst_segment_do_seek (&seeksegment, rate, format, flags,
2144 cur_type, cur, stop_type, stop, &update);
2147 /* figure out the last position we need to play. If it's configured (stop !=
2148 * -1), use that, else we play until the total duration of the file */
2149 if ((stop = seeksegment.stop) == -1)
2150 stop = seeksegment.duration;
2152 playing = (src->state == GST_RTSP_STATE_PLAYING);
2154 /* if we were playing, pause first */
2156 /* obtain current position in case seek fails */
2157 gst_rtspsrc_get_position (src);
2158 gst_rtspsrc_pause (src, FALSE);
2162 gst_rtspsrc_do_seek (src, &seeksegment);
2164 /* and continue playing */
2166 gst_rtspsrc_play (src, &seeksegment, FALSE);
2168 /* prepare for streaming again */
2170 /* if we started flush, we stop now */
2171 GST_DEBUG_OBJECT (src, "stopping flush");
2172 gst_rtspsrc_flush (src, FALSE, playing);
2175 /* now we did the seek and can activate the new segment values */
2176 memcpy (&src->segment, &seeksegment, sizeof (GstSegment));
2178 /* if we're doing a segment seek, post a SEGMENT_START message */
2179 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2180 gst_element_post_message (GST_ELEMENT_CAST (src),
2181 gst_message_new_segment_start (GST_OBJECT_CAST (src),
2182 src->segment.format, src->segment.position));
2185 /* now create the newsegment */
2186 GST_DEBUG_OBJECT (src, "Creating newsegment from %" G_GINT64_FORMAT
2187 " to %" G_GINT64_FORMAT, src->segment.position, stop);
2190 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
2191 for (walk = src->streams; walk; walk = g_list_next (walk)) {
2192 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
2193 stream->discont = TRUE;
2196 GST_RTSP_STREAM_UNLOCK (src);
2203 GST_DEBUG_OBJECT (src, "negative playback rates are not supported yet.");
2208 GST_DEBUG_OBJECT (src, "unsupported format given, seek aborted.");
2214 gst_rtspsrc_handle_src_event (GstPad * pad, GstObject * parent,
2218 gboolean res = TRUE;
2221 src = GST_RTSPSRC_CAST (parent);
2223 GST_DEBUG_OBJECT (src, "pad %s:%s received event %s",
2224 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE_NAME (event));
2226 switch (GST_EVENT_TYPE (event)) {
2227 case GST_EVENT_SEEK:
2228 res = gst_rtspsrc_perform_seek (src, event);
2232 case GST_EVENT_NAVIGATION:
2233 case GST_EVENT_LATENCY:
2241 if ((target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad)))) {
2242 res = gst_pad_send_event (target, event);
2243 gst_object_unref (target);
2245 gst_event_unref (event);
2248 gst_event_unref (event);
2254 /* this is the final event function we receive on the internal source pad when
2255 * we deal with TCP connections */
2257 gst_rtspsrc_handle_internal_src_event (GstPad * pad, GstObject * parent,
2262 GST_DEBUG_OBJECT (pad, "received event %s", GST_EVENT_TYPE_NAME (event));
2264 switch (GST_EVENT_TYPE (event)) {
2265 case GST_EVENT_SEEK:
2267 case GST_EVENT_NAVIGATION:
2268 case GST_EVENT_LATENCY:
2270 gst_event_unref (event);
2277 /* this is the final query function we receive on the internal source pad when
2278 * we deal with TCP connections */
2280 gst_rtspsrc_handle_internal_src_query (GstPad * pad, GstObject * parent,
2284 gboolean res = TRUE;
2286 src = GST_RTSPSRC_CAST (gst_pad_get_element_private (pad));
2288 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2289 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2291 switch (GST_QUERY_TYPE (query)) {
2292 case GST_QUERY_POSITION:
2297 case GST_QUERY_DURATION:
2301 gst_query_parse_duration (query, &format, NULL);
2304 case GST_FORMAT_TIME:
2305 gst_query_set_duration (query, format, src->segment.duration);
2313 case GST_QUERY_LATENCY:
2315 /* we are live with a min latency of 0 and unlimited max latency, this
2316 * result will be updated by the session manager if there is any. */
2317 gst_query_set_latency (query, TRUE, 0, -1);
2327 /* this query is executed on the ghost source pad exposed on rtspsrc. */
2329 gst_rtspsrc_handle_src_query (GstPad * pad, GstObject * parent,
2333 gboolean res = FALSE;
2335 src = GST_RTSPSRC_CAST (parent);
2337 GST_DEBUG_OBJECT (src, "pad %s:%s received query %s",
2338 GST_DEBUG_PAD_NAME (pad), GST_QUERY_TYPE_NAME (query));
2340 switch (GST_QUERY_TYPE (query)) {
2341 case GST_QUERY_DURATION:
2345 gst_query_parse_duration (query, &format, NULL);
2348 case GST_FORMAT_TIME:
2349 gst_query_set_duration (query, format, src->segment.duration);
2357 case GST_QUERY_SEEKING:
2361 gst_query_parse_seeking (query, &format, NULL, NULL, NULL);
2362 if (format == GST_FORMAT_TIME) {
2364 src->cur_protocols != GST_RTSP_LOWER_TRANS_UDP_MCAST;
2366 /* seeking without duration is unlikely */
2367 seekable = seekable && src->seekable && src->segment.duration &&
2368 GST_CLOCK_TIME_IS_VALID (src->segment.duration);
2370 /* FIXME ?? should we have 0 and segment.duration here; see demuxers */
2371 gst_query_set_seeking (query, GST_FORMAT_TIME, seekable,
2372 src->segment.start, src->segment.stop);
2381 uri = gst_rtspsrc_uri_get_uri (GST_URI_HANDLER (src));
2383 gst_query_set_uri (query, uri);
2391 GstPad *target = gst_ghost_pad_get_target (GST_GHOST_PAD_CAST (pad));
2393 /* forward the query to the proxy target pad */
2395 res = gst_pad_query (target, query);
2396 gst_object_unref (target);
2405 /* callback for RTCP messages to be sent to the server when operating in TCP
2407 static GstFlowReturn
2408 gst_rtspsrc_sink_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer)
2411 GstRTSPStream *stream;
2412 GstFlowReturn res = GST_FLOW_OK;
2417 GstRTSPMessage message = { 0 };
2418 GstRTSPConnection *conn;
2420 stream = (GstRTSPStream *) gst_pad_get_element_private (pad);
2421 src = stream->parent;
2423 gst_buffer_map (buffer, &map, GST_MAP_READ);
2427 gst_rtsp_message_init_data (&message, stream->channel[1]);
2429 /* lend the body data to the message */
2430 gst_rtsp_message_take_body (&message, data, size);
2432 if (stream->conninfo.connection)
2433 conn = stream->conninfo.connection;
2435 conn = src->conninfo.connection;
2437 GST_DEBUG_OBJECT (src, "sending %u bytes RTCP", size);
2438 ret = gst_rtspsrc_connection_send (src, conn, &message, NULL);
2439 GST_DEBUG_OBJECT (src, "sent RTCP, %d", ret);
2441 /* and steal it away again because we will free it when unreffing the
2443 gst_rtsp_message_steal_body (&message, &data, &size);
2444 gst_rtsp_message_unset (&message);
2446 gst_buffer_unmap (buffer, &map);
2447 gst_buffer_unref (buffer);
2452 static GstPadProbeReturn
2453 pad_blocked (GstPad * pad, GstPadProbeInfo * info, gpointer user_data)
2455 GstRTSPSrc *src = user_data;
2457 GST_DEBUG_OBJECT (src, "pad %s:%s blocked, activating streams",
2458 GST_DEBUG_PAD_NAME (pad));
2460 /* activate the streams */
2461 GST_OBJECT_LOCK (src);
2462 if (!src->need_activate)
2465 src->need_activate = FALSE;
2466 GST_OBJECT_UNLOCK (src);
2468 gst_rtspsrc_activate_streams (src);
2470 return GST_PAD_PROBE_OK;
2474 GST_OBJECT_UNLOCK (src);
2475 return GST_PAD_PROBE_OK;
2479 /* this callback is called when the session manager generated a new src pad with
2480 * payloaded RTP packets. We simply ghost the pad here. */
2482 new_manager_pad (GstElement * manager, GstPad * pad, GstRTSPSrc * src)
2485 GstPadTemplate *template;
2488 GstRTSPStream *stream;
2491 GST_DEBUG_OBJECT (src, "got new manager pad %" GST_PTR_FORMAT, pad);
2493 GST_RTSP_STATE_LOCK (src);
2495 name = gst_object_get_name (GST_OBJECT_CAST (pad));
2496 if (sscanf (name, "recv_rtp_src_%u_%u_%u", &id, &ssrc, &pt) != 3)
2497 goto unknown_stream;
2499 GST_DEBUG_OBJECT (src, "stream: %u, SSRC %08x, PT %d", id, ssrc, pt);
2501 stream = find_stream (src, &id, (gpointer) find_stream_by_id);
2503 goto unknown_stream;
2506 stream->ssrc = ssrc;
2508 /* we'll add it later see below */
2509 stream->added = TRUE;
2511 /* check if we added all streams */
2513 for (ostreams = src->streams; ostreams; ostreams = g_list_next (ostreams)) {
2514 GstRTSPStream *ostream = (GstRTSPStream *) ostreams->data;
2516 GST_DEBUG_OBJECT (src, "stream %p, container %d, disabled %d, added %d",
2517 ostream, ostream->container, ostream->disabled, ostream->added);
2519 /* a container stream only needs one pad added. Also disabled streams don't
2521 if (!ostream->container && !ostream->disabled && !ostream->added) {
2526 GST_RTSP_STATE_UNLOCK (src);
2528 /* create a new pad we will use to stream to */
2529 template = gst_static_pad_template_get (&rtptemplate);
2530 stream->srcpad = gst_ghost_pad_new_from_template (name, pad, template);
2531 gst_object_unref (template);
2534 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
2535 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
2536 gst_pad_set_active (stream->srcpad, TRUE);
2537 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
2540 GST_DEBUG_OBJECT (src, "We added all streams");
2541 /* when we get here, all stream are added and we can fire the no-more-pads
2543 gst_element_no_more_pads (GST_ELEMENT_CAST (src));
2551 GST_DEBUG_OBJECT (src, "ignoring unknown stream");
2552 GST_RTSP_STATE_UNLOCK (src);
2559 request_pt_map (GstElement * manager, guint session, guint pt, GstRTSPSrc * src)
2561 GstRTSPStream *stream;
2564 GST_DEBUG_OBJECT (src, "getting pt map for pt %d in session %d", pt, session);
2566 GST_RTSP_STATE_LOCK (src);
2567 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2569 goto unknown_stream;
2571 caps = stream->caps;
2573 gst_caps_ref (caps);
2574 GST_RTSP_STATE_UNLOCK (src);
2580 GST_DEBUG_OBJECT (src, "unknown stream %d", session);
2581 GST_RTSP_STATE_UNLOCK (src);
2587 gst_rtspsrc_do_stream_eos (GstRTSPSrc * src, GstRTSPStream * stream)
2589 GST_DEBUG_OBJECT (src, "setting stream for session %u to EOS", stream->id);
2595 gst_rtspsrc_stream_push_event (src, stream, gst_event_new_eos ());
2601 GST_DEBUG_OBJECT (src, "stream for session %u was already EOS", stream->id);
2607 on_bye_ssrc (GObject * session, GObject * source, GstRTSPStream * stream)
2609 GstRTSPSrc *src = stream->parent;
2612 g_object_get (source, "ssrc", &ssrc, NULL);
2614 GST_DEBUG_OBJECT (src, "source %08x, stream %08x, session %u received BYE",
2615 ssrc, stream->ssrc, stream->id);
2617 if (ssrc == stream->ssrc)
2618 gst_rtspsrc_do_stream_eos (src, stream);
2622 on_timeout (GObject * session, GObject * source, GstRTSPStream * stream)
2624 GstRTSPSrc *src = stream->parent;
2627 g_object_get (source, "ssrc", &ssrc, NULL);
2629 GST_WARNING_OBJECT (src, "source %08x, stream %08x in session %u timed out",
2630 ssrc, stream->ssrc, stream->id);
2632 if (ssrc == stream->ssrc)
2633 gst_rtspsrc_do_stream_eos (src, stream);
2637 on_npt_stop (GstElement * rtpbin, guint session, guint ssrc, GstRTSPSrc * src)
2639 GstRTSPStream *stream;
2641 GST_DEBUG_OBJECT (src, "source in session %u reached NPT stop", session);
2643 /* get stream for session */
2644 stream = find_stream (src, &session, (gpointer) find_stream_by_id);
2646 gst_rtspsrc_do_stream_eos (src, stream);
2651 on_ssrc_active (GObject * session, GObject * source, GstRTSPStream * stream)
2653 GST_DEBUG_OBJECT (stream->parent, "source in session %u is active",
2658 set_manager_buffer_mode (GstRTSPSrc * src)
2660 GObjectClass *klass;
2662 g_return_if_fail (G_IS_OBJECT (src->manager));
2664 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2666 if (!g_object_class_find_property (klass, "buffer-mode"))
2669 if (src->buffer_mode != BUFFER_MODE_AUTO) {
2670 g_object_set (src->manager, "buffer-mode", src->buffer_mode, NULL);
2675 GST_DEBUG_OBJECT (src,
2676 "auto buffering mode, have clock %" GST_PTR_FORMAT, src->provided_clock);
2678 if (src->provided_clock) {
2679 GstClock *clock = gst_element_get_clock (GST_ELEMENT_CAST (src));
2681 if (clock == src->provided_clock) {
2682 GST_DEBUG_OBJECT (src, "selected synced");
2683 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SYNCED, NULL);
2686 gst_object_unref (clock);
2691 /* Otherwise fall-through and use another buffer mode */
2693 gst_object_unref (clock);
2696 GST_DEBUG_OBJECT (src, "auto buffering mode");
2697 if (src->use_buffering) {
2698 GST_DEBUG_OBJECT (src, "selected buffer");
2699 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_BUFFER, NULL);
2701 GST_DEBUG_OBJECT (src, "selected slave");
2702 g_object_set (src->manager, "buffer-mode", BUFFER_MODE_SLAVE, NULL);
2706 /* try to get and configure a manager */
2708 gst_rtspsrc_stream_configure_manager (GstRTSPSrc * src, GstRTSPStream * stream,
2709 GstRTSPTransport * transport)
2711 const gchar *manager;
2713 GstStateChangeReturn ret;
2715 /* find a manager */
2716 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 0) < 0)
2720 GST_DEBUG_OBJECT (src, "using manager %s", manager);
2722 /* configure the manager */
2723 if (src->manager == NULL) {
2724 GObjectClass *klass;
2726 const gchar *encoding;
2727 gboolean need_slave;
2729 if (!(src->manager = gst_element_factory_make (manager, "manager"))) {
2731 if (gst_rtsp_transport_get_manager (transport->trans, &manager, 1) < 0)
2735 goto use_no_manager;
2737 if (!(src->manager = gst_element_factory_make (manager, "manager")))
2738 goto manager_failed;
2741 /* we manage this element */
2742 gst_element_set_locked_state (src->manager, TRUE);
2743 gst_bin_add (GST_BIN_CAST (src), src->manager);
2745 ret = gst_element_set_state (src->manager, GST_STATE_PAUSED);
2746 if (ret == GST_STATE_CHANGE_FAILURE)
2747 goto start_manager_failure;
2749 g_object_set (src->manager, "latency", src->latency, NULL);
2751 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
2753 if (g_object_class_find_property (klass, "ntp-sync")) {
2754 g_object_set (src->manager, "ntp-sync", src->ntp_sync, NULL);
2757 if (g_object_class_find_property (klass, "use-pipeline-clock")) {
2758 g_object_set (src->manager, "use-pipeline-clock",
2759 src->use_pipeline_clock, NULL);
2762 if (src->sdes && g_object_class_find_property (klass, "sdes")) {
2763 g_object_set (src->manager, "sdes", src->sdes, NULL);
2766 if (g_object_class_find_property (klass, "drop-on-latency")) {
2767 g_object_set (src->manager, "drop-on-latency", src->drop_on_latency,
2771 /* buffer mode pauses are handled by adding offsets to buffer times,
2772 * but some depayloaders may have a hard time syncing output times
2773 * with such input times, e.g. container ones, most notably ASF */
2774 /* TODO alternatives are having an event that indicates these shifts,
2775 * or having rtsp extensions provide suggestion on buffer mode */
2776 need_slave = stream->container;
2777 if (stream->caps && (s = gst_caps_get_structure (stream->caps, 0)) &&
2778 (encoding = gst_structure_get_string (s, "encoding-name")))
2779 need_slave = need_slave || (strcmp (encoding, "X-ASF-PF") == 0);
2780 /* valid duration implies not likely live pipeline,
2781 * so slaving in jitterbuffer does not make much sense
2782 * (and might mess things up due to bursts) */
2783 if (GST_CLOCK_TIME_IS_VALID (src->segment.duration) &&
2784 src->segment.duration && !need_slave) {
2785 src->use_buffering = TRUE;
2787 src->use_buffering = FALSE;
2790 set_manager_buffer_mode (src);
2792 /* connect to signals if we did not already do so */
2793 GST_DEBUG_OBJECT (src, "connect to signals on session manager, stream %p",
2795 src->manager_sig_id =
2796 g_signal_connect (src->manager, "pad-added",
2797 (GCallback) new_manager_pad, src);
2798 src->manager_ptmap_id =
2799 g_signal_connect (src->manager, "request-pt-map",
2800 (GCallback) request_pt_map, src);
2802 g_signal_connect (src->manager, "on-npt-stop", (GCallback) on_npt_stop,
2806 /* we stream directly to the manager, get some pads. Each RTSP stream goes
2807 * into a separate RTP session. */
2808 name = g_strdup_printf ("recv_rtp_sink_%u", stream->id);
2809 stream->channelpad[0] = gst_element_get_request_pad (src->manager, name);
2811 name = g_strdup_printf ("recv_rtcp_sink_%u", stream->id);
2812 stream->channelpad[1] = gst_element_get_request_pad (src->manager, name);
2815 /* now configure the bandwidth in the manager */
2816 if (g_signal_lookup ("get-internal-session",
2817 G_OBJECT_TYPE (src->manager)) != 0) {
2818 GObject *rtpsession;
2820 g_signal_emit_by_name (src->manager, "get-internal-session", stream->id,
2823 GST_INFO_OBJECT (src, "configure bandwidth in session %p", rtpsession);
2825 stream->session = rtpsession;
2827 if (stream->as_bandwidth != -1) {
2828 GST_INFO_OBJECT (src, "setting AS: %f",
2829 (gdouble) (stream->as_bandwidth * 1000));
2830 g_object_set (rtpsession, "bandwidth",
2831 (gdouble) (stream->as_bandwidth * 1000), NULL);
2833 if (stream->rr_bandwidth != -1) {
2834 GST_INFO_OBJECT (src, "setting RR: %u", stream->rr_bandwidth);
2835 g_object_set (rtpsession, "rtcp-rr-bandwidth", stream->rr_bandwidth,
2838 if (stream->rs_bandwidth != -1) {
2839 GST_INFO_OBJECT (src, "setting RS: %u", stream->rs_bandwidth);
2840 g_object_set (rtpsession, "rtcp-rs-bandwidth", stream->rs_bandwidth,
2844 g_object_set (rtpsession, "probation", src->probation, NULL);
2846 g_signal_connect (rtpsession, "on-bye-ssrc", (GCallback) on_bye_ssrc,
2848 g_signal_connect (rtpsession, "on-bye-timeout", (GCallback) on_timeout,
2850 g_signal_connect (rtpsession, "on-timeout", (GCallback) on_timeout,
2852 g_signal_connect (rtpsession, "on-ssrc-active",
2853 (GCallback) on_ssrc_active, stream);
2864 GST_DEBUG_OBJECT (src, "cannot get a session manager");
2869 GST_DEBUG_OBJECT (src, "no session manager element %s found", manager);
2872 start_manager_failure:
2874 GST_DEBUG_OBJECT (src, "could not start session manager");
2879 /* free the UDP sources allocated when negotiating a transport.
2880 * This function is called when the server negotiated to a transport where the
2881 * UDP sources are not needed anymore, such as TCP or multicast. */
2883 gst_rtspsrc_stream_free_udp (GstRTSPStream * stream)
2887 for (i = 0; i < 2; i++) {
2888 if (stream->udpsrc[i]) {
2889 GST_DEBUG ("free UDP source %d for stream %p", i, stream);
2890 gst_element_set_state (stream->udpsrc[i], GST_STATE_NULL);
2891 gst_object_unref (stream->udpsrc[i]);
2892 stream->udpsrc[i] = NULL;
2897 /* for TCP, create pads to send and receive data to and from the manager and to
2898 * intercept various events and queries
2901 gst_rtspsrc_stream_configure_tcp (GstRTSPSrc * src, GstRTSPStream * stream,
2902 GstRTSPTransport * transport, GstPad ** outpad)
2905 GstPadTemplate *template;
2906 GstPad *pad0, *pad1;
2908 /* configure for interleaved delivery, nothing needs to be done
2909 * here, the loop function will call the chain functions of the
2910 * session manager. */
2911 stream->channel[0] = transport->interleaved.min;
2912 stream->channel[1] = transport->interleaved.max;
2913 GST_DEBUG_OBJECT (src, "stream %p on channels %d-%d", stream,
2914 stream->channel[0], stream->channel[1]);
2916 /* we can remove the allocated UDP ports now */
2917 gst_rtspsrc_stream_free_udp (stream);
2919 /* no session manager, send data to srcpad directly */
2920 if (!stream->channelpad[0]) {
2921 GST_DEBUG_OBJECT (src, "no manager, creating pad");
2923 /* create a new pad we will use to stream to */
2924 name = g_strdup_printf ("stream_%u", stream->id);
2925 template = gst_static_pad_template_get (&rtptemplate);
2926 stream->channelpad[0] = gst_pad_new_from_template (template, name);
2927 gst_object_unref (template);
2930 /* set caps and activate */
2931 gst_pad_use_fixed_caps (stream->channelpad[0]);
2932 gst_pad_set_active (stream->channelpad[0], TRUE);
2934 *outpad = gst_object_ref (stream->channelpad[0]);
2936 GST_DEBUG_OBJECT (src, "using manager source pad");
2938 template = gst_static_pad_template_get (&anysrctemplate);
2940 /* allocate pads for sending the channel data into the manager */
2941 pad0 = gst_pad_new_from_template (template, "internalsrc_0");
2942 gst_pad_link_full (pad0, stream->channelpad[0], GST_PAD_LINK_CHECK_NOTHING);
2943 gst_object_unref (stream->channelpad[0]);
2944 stream->channelpad[0] = pad0;
2945 gst_pad_set_event_function (pad0, gst_rtspsrc_handle_internal_src_event);
2946 gst_pad_set_query_function (pad0, gst_rtspsrc_handle_internal_src_query);
2947 gst_pad_set_element_private (pad0, src);
2948 gst_pad_set_active (pad0, TRUE);
2950 if (stream->channelpad[1]) {
2951 /* if we have a sinkpad for the other channel, create a pad and link to the
2953 pad1 = gst_pad_new_from_template (template, "internalsrc_1");
2954 gst_pad_set_event_function (pad1, gst_rtspsrc_handle_internal_src_event);
2955 gst_pad_link_full (pad1, stream->channelpad[1],
2956 GST_PAD_LINK_CHECK_NOTHING);
2957 gst_object_unref (stream->channelpad[1]);
2958 stream->channelpad[1] = pad1;
2959 gst_pad_set_active (pad1, TRUE);
2961 gst_object_unref (template);
2963 /* setup RTCP transport back to the server if we have to. */
2964 if (src->manager && src->do_rtcp) {
2967 template = gst_static_pad_template_get (&anysinktemplate);
2969 stream->rtcppad = gst_pad_new_from_template (template, "internalsink_0");
2970 gst_pad_set_chain_function (stream->rtcppad, gst_rtspsrc_sink_chain);
2971 gst_pad_set_element_private (stream->rtcppad, stream);
2972 gst_pad_set_active (stream->rtcppad, TRUE);
2974 /* get session RTCP pad */
2975 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
2976 pad = gst_element_get_request_pad (src->manager, name);
2981 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
2982 gst_object_unref (pad);
2985 gst_object_unref (template);
2991 gst_rtspsrc_get_transport_info (GstRTSPSrc * src, GstRTSPStream * stream,
2992 GstRTSPTransport * transport, const gchar ** destination, gint * min,
2993 gint * max, guint * ttl)
2995 if (transport->lower_transport == GST_RTSP_LOWER_TRANS_UDP_MCAST) {
2997 if (!(*destination = transport->destination))
2998 *destination = stream->destination;
3001 /* transport first */
3002 *min = transport->port.min;
3003 *max = transport->port.max;
3004 if (*min == -1 && *max == -1) {
3005 /* then try from SDP */
3006 if (stream->port != 0) {
3007 *min = stream->port;
3008 *max = stream->port + 1;
3014 if (!(*ttl = transport->ttl))
3019 /* first take the source, then the endpoint to figure out where to send
3021 if (!(*destination = transport->source)) {
3022 if (src->conninfo.connection)
3023 *destination = gst_rtsp_connection_get_ip (src->conninfo.connection);
3024 else if (stream->conninfo.connection)
3026 gst_rtsp_connection_get_ip (stream->conninfo.connection);
3030 /* for unicast we only expect the ports here */
3031 *min = transport->server_port.min;
3032 *max = transport->server_port.max;
3037 /* For multicast create UDP sources and join the multicast group. */
3039 gst_rtspsrc_stream_configure_mcast (GstRTSPSrc * src, GstRTSPStream * stream,
3040 GstRTSPTransport * transport, GstPad ** outpad)
3043 const gchar *destination;
3046 GST_DEBUG_OBJECT (src, "creating UDP sources for multicast");
3048 /* we can remove the allocated UDP ports now */
3049 gst_rtspsrc_stream_free_udp (stream);
3051 gst_rtspsrc_get_transport_info (src, stream, transport, &destination, &min,
3054 /* we need a destination now */
3055 if (destination == NULL)
3056 goto no_destination;
3058 /* we really need ports now or we won't be able to receive anything at all */
3059 if (min == -1 && max == -1)
3062 GST_DEBUG_OBJECT (src, "have destination '%s' and ports (%d)-(%d)",
3063 destination, min, max);
3065 /* creating UDP source for RTP */
3067 uri = g_strdup_printf ("udp://%s:%d", destination, min);
3069 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3071 if (stream->udpsrc[0] == NULL)
3074 /* take ownership */
3075 gst_object_ref_sink (stream->udpsrc[0]);
3077 if (src->udp_buffer_size != 0)
3078 g_object_set (G_OBJECT (stream->udpsrc[0]), "buffer-size",
3079 src->udp_buffer_size, NULL);
3081 if (src->multi_iface != NULL)
3082 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3083 src->multi_iface, NULL);
3086 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3087 gst_element_set_state (stream->udpsrc[0], GST_STATE_PAUSED);
3090 /* creating another UDP source for RTCP */
3094 uri = g_strdup_printf ("udp://%s:%d", destination, max);
3096 gst_element_make_from_uri (GST_URI_SRC, uri, NULL, NULL);
3098 if (stream->udpsrc[1] == NULL)
3101 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3102 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3103 gst_caps_unref (caps);
3105 /* take ownership */
3106 gst_object_ref_sink (stream->udpsrc[1]);
3108 if (src->multi_iface != NULL)
3109 g_object_set (G_OBJECT (stream->udpsrc[0]), "multicast-iface",
3110 src->multi_iface, NULL);
3112 gst_element_set_state (stream->udpsrc[1], GST_STATE_PAUSED);
3119 GST_DEBUG_OBJECT (src, "no UDP source element found");
3124 GST_DEBUG_OBJECT (src, "no destination found");
3129 GST_DEBUG_OBJECT (src, "no ports found");
3134 /* configure the remainder of the UDP ports */
3136 gst_rtspsrc_stream_configure_udp (GstRTSPSrc * src, GstRTSPStream * stream,
3137 GstRTSPTransport * transport, GstPad ** outpad)
3139 /* we manage the UDP elements now. For unicast, the UDP sources where
3140 * allocated in the stream when we suggested a transport. */
3141 if (stream->udpsrc[0]) {
3142 gst_element_set_locked_state (stream->udpsrc[0], TRUE);
3143 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[0]);
3145 GST_DEBUG_OBJECT (src, "setting up UDP source");
3147 /* configure a timeout on the UDP port. When the timeout message is
3148 * posted, we assume UDP transport is not possible. We reconnect using TCP
3150 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout",
3151 src->udp_timeout * 1000, NULL);
3153 /* get output pad of the UDP source. */
3154 *outpad = gst_element_get_static_pad (stream->udpsrc[0], "src");
3156 /* save it so we can unblock */
3157 stream->blockedpad = *outpad;
3159 /* configure pad block on the pad. As soon as there is dataflow on the
3160 * UDP source, we know that UDP is not blocked by a firewall and we can
3161 * configure all the streams to let the application autoplug decoders. */
3163 gst_pad_add_probe (stream->blockedpad,
3164 GST_PAD_PROBE_TYPE_BLOCK_DOWNSTREAM, pad_blocked, src, NULL);
3166 if (stream->channelpad[0]) {
3167 GST_DEBUG_OBJECT (src, "connecting UDP source 0 to manager");
3168 /* configure for UDP delivery, we need to connect the UDP pads to
3169 * the session plugin. */
3170 gst_pad_link_full (*outpad, stream->channelpad[0],
3171 GST_PAD_LINK_CHECK_NOTHING);
3172 gst_object_unref (*outpad);
3174 /* we connected to pad-added signal to get pads from the manager */
3176 GST_DEBUG_OBJECT (src, "using UDP src pad as output");
3181 if (stream->udpsrc[1]) {
3184 gst_element_set_locked_state (stream->udpsrc[1], TRUE);
3185 gst_bin_add (GST_BIN_CAST (src), stream->udpsrc[1]);
3187 caps = gst_caps_new_empty_simple ("application/x-rtcp");
3188 g_object_set (stream->udpsrc[1], "caps", caps, NULL);
3189 gst_caps_unref (caps);
3191 if (stream->channelpad[1]) {
3194 GST_DEBUG_OBJECT (src, "connecting UDP source 1 to manager");
3196 pad = gst_element_get_static_pad (stream->udpsrc[1], "src");
3197 gst_pad_link_full (pad, stream->channelpad[1],
3198 GST_PAD_LINK_CHECK_NOTHING);
3199 gst_object_unref (pad);
3201 /* leave unlinked */
3207 /* configure the UDP sink back to the server for status reports */
3209 gst_rtspsrc_stream_configure_udp_sinks (GstRTSPSrc * src,
3210 GstRTSPStream * stream, GstRTSPTransport * transport)
3213 gint rtp_port, rtcp_port;
3214 gboolean do_rtp, do_rtcp;
3215 const gchar *destination;
3220 /* get transport info */
3221 gst_rtspsrc_get_transport_info (src, stream, transport, &destination,
3222 &rtp_port, &rtcp_port, &ttl);
3224 /* see what we need to do */
3225 do_rtp = (rtp_port != -1);
3226 /* it's possible that the server does not want us to send RTCP in which case
3228 do_rtcp = (rtcp_port != -1 && src->manager != NULL && src->do_rtcp);
3230 /* we need a destination when we have RTP or RTCP ports */
3231 if (destination == NULL && (do_rtp || do_rtcp))
3232 goto no_destination;
3234 /* try to construct the fakesrc to the RTP port of the server to open up any
3237 GST_DEBUG_OBJECT (src, "configure RTP UDP sink for %s:%d", destination,
3240 uri = g_strdup_printf ("udp://%s:%d", destination, rtp_port);
3241 stream->udpsink[0] =
3242 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3244 if (stream->udpsink[0] == NULL)
3245 goto no_sink_element;
3247 /* don't join multicast group, we will have the source socket do that */
3248 /* no sync or async state changes needed */
3249 g_object_set (G_OBJECT (stream->udpsink[0]), "auto-multicast", FALSE,
3250 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3252 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3254 if (stream->udpsrc[0]) {
3255 /* configure socket, we give it the same UDP socket as the udpsrc for RTP
3256 * so that NAT firewalls will open a hole for us */
3257 g_object_get (G_OBJECT (stream->udpsrc[0]), "used-socket", &socket, NULL);
3258 GST_DEBUG_OBJECT (src, "RTP UDP src has sock %p", socket);
3259 /* configure socket and make sure udpsink does not close it when shutting
3260 * down, it belongs to udpsrc after all. */
3261 g_object_set (G_OBJECT (stream->udpsink[0]), "socket", socket,
3262 "close-socket", FALSE, NULL);
3263 g_object_unref (socket);
3266 /* the source for the dummy packets to open up NAT */
3267 stream->fakesrc = gst_element_factory_make ("fakesrc", NULL);
3268 if (stream->fakesrc == NULL)
3269 goto no_fakesrc_element;
3271 /* random data in 5 buffers, a size of 200 bytes should be fine */
3272 g_object_set (G_OBJECT (stream->fakesrc), "filltype", 3, "num-buffers", 5,
3273 "sizetype", 2, "sizemax", 200, "silent", TRUE, NULL);
3275 /* we don't want to consider this a sink */
3276 GST_OBJECT_FLAG_UNSET (stream->udpsink[0], GST_ELEMENT_FLAG_SINK);
3278 /* keep everything locked */
3279 gst_element_set_locked_state (stream->udpsink[0], TRUE);
3280 gst_element_set_locked_state (stream->fakesrc, TRUE);
3282 gst_object_ref (stream->udpsink[0]);
3283 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[0]);
3284 gst_object_ref (stream->fakesrc);
3285 gst_bin_add (GST_BIN_CAST (src), stream->fakesrc);
3287 gst_element_link_pads_full (stream->fakesrc, "src", stream->udpsink[0],
3288 "sink", GST_PAD_LINK_CHECK_NOTHING);
3291 GST_DEBUG_OBJECT (src, "configure RTCP UDP sink for %s:%d", destination,
3294 uri = g_strdup_printf ("udp://%s:%d", destination, rtcp_port);
3295 stream->udpsink[1] =
3296 gst_element_make_from_uri (GST_URI_SINK, uri, NULL, NULL);
3298 if (stream->udpsink[1] == NULL)
3299 goto no_sink_element;
3301 /* don't join multicast group, we will have the source socket do that */
3302 /* no sync or async state changes needed */
3303 g_object_set (G_OBJECT (stream->udpsink[1]), "auto-multicast", FALSE,
3304 "loop", FALSE, "sync", FALSE, "async", FALSE, NULL);
3306 g_object_set (G_OBJECT (stream->udpsink[0]), "ttl", ttl, NULL);
3308 if (stream->udpsrc[1]) {
3309 /* configure socket, we give it the same UDP socket as the udpsrc for RTCP
3310 * because some servers check the port number of where it sends RTCP to identify
3311 * the RTCP packets it receives */
3312 g_object_get (G_OBJECT (stream->udpsrc[1]), "used-socket", &socket, NULL);
3313 GST_DEBUG_OBJECT (src, "RTCP UDP src has sock %p", socket);
3314 /* configure socket and make sure udpsink does not close it when shutting
3315 * down, it belongs to udpsrc after all. */
3316 g_object_set (G_OBJECT (stream->udpsink[1]), "socket", socket,
3317 "close-socket", FALSE, NULL);
3318 g_object_unref (socket);
3321 /* we don't want to consider this a sink */
3322 GST_OBJECT_FLAG_UNSET (stream->udpsink[1], GST_ELEMENT_FLAG_SINK);
3324 /* we keep this playing always */
3325 gst_element_set_locked_state (stream->udpsink[1], TRUE);
3326 gst_element_set_state (stream->udpsink[1], GST_STATE_PLAYING);
3328 gst_object_ref (stream->udpsink[1]);
3329 gst_bin_add (GST_BIN_CAST (src), stream->udpsink[1]);
3331 stream->rtcppad = gst_element_get_static_pad (stream->udpsink[1], "sink");
3333 /* get session RTCP pad */
3334 name = g_strdup_printf ("send_rtcp_src_%u", stream->id);
3335 pad = gst_element_get_request_pad (src->manager, name);
3340 gst_pad_link_full (pad, stream->rtcppad, GST_PAD_LINK_CHECK_NOTHING);
3341 gst_object_unref (pad);
3350 GST_DEBUG_OBJECT (src, "no destination address specified");
3355 GST_DEBUG_OBJECT (src, "no UDP sink element found");
3360 GST_DEBUG_OBJECT (src, "no fakesrc element found");
3365 /* sets up all elements needed for streaming over the specified transport.
3366 * Does not yet expose the element pads, this will be done when there is actuall
3367 * dataflow detected, which might never happen when UDP is blocked in a
3368 * firewall, for example.
3371 gst_rtspsrc_stream_configure_transport (GstRTSPStream * stream,
3372 GstRTSPTransport * transport)
3375 GstPad *outpad = NULL;
3376 GstPadTemplate *template;
3381 src = stream->parent;
3383 GST_DEBUG_OBJECT (src, "configuring transport for stream %p", stream);
3385 s = gst_caps_get_structure (stream->caps, 0);
3387 /* get the proper mime type for this stream now */
3388 if (gst_rtsp_transport_get_mime (transport->trans, &mime) < 0)
3389 goto unknown_transport;
3391 goto unknown_transport;
3393 /* configure the final mime type */
3394 GST_DEBUG_OBJECT (src, "setting mime to %s", mime);
3395 gst_structure_set_name (s, mime);
3397 /* try to get and configure a manager, channelpad[0-1] will be configured with
3398 * the pads for the manager, or NULL when no manager is needed. */
3399 if (!gst_rtspsrc_stream_configure_manager (src, stream, transport))
3402 switch (transport->lower_transport) {
3403 case GST_RTSP_LOWER_TRANS_TCP:
3404 if (!gst_rtspsrc_stream_configure_tcp (src, stream, transport, &outpad))
3405 goto transport_failed;
3407 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
3408 if (!gst_rtspsrc_stream_configure_mcast (src, stream, transport, &outpad))
3409 goto transport_failed;
3410 /* fallthrough, the rest is the same for UDP and MCAST */
3411 case GST_RTSP_LOWER_TRANS_UDP:
3412 if (!gst_rtspsrc_stream_configure_udp (src, stream, transport, &outpad))
3413 goto transport_failed;
3414 /* configure udpsinks back to the server for RTCP messages and for the
3415 * dummy RTP messages to open NAT. */
3416 if (!gst_rtspsrc_stream_configure_udp_sinks (src, stream, transport))
3417 goto transport_failed;
3420 goto unknown_transport;
3424 GST_DEBUG_OBJECT (src, "creating ghostpad");
3426 gst_pad_use_fixed_caps (outpad);
3428 /* create ghostpad, don't add just yet, this will be done when we activate
3430 name = g_strdup_printf ("stream_%u", stream->id);
3431 template = gst_static_pad_template_get (&rtptemplate);
3432 stream->srcpad = gst_ghost_pad_new_from_template (name, outpad, template);
3433 gst_pad_set_event_function (stream->srcpad, gst_rtspsrc_handle_src_event);
3434 gst_pad_set_query_function (stream->srcpad, gst_rtspsrc_handle_src_query);
3435 gst_object_unref (template);
3438 gst_object_unref (outpad);
3440 /* mark pad as ok */
3441 stream->last_ret = GST_FLOW_OK;
3448 GST_DEBUG_OBJECT (src, "failed to configure transport");
3453 GST_DEBUG_OBJECT (src, "unknown transport");
3458 GST_DEBUG_OBJECT (src, "cannot get a session manager");
3463 /* send a couple of dummy random packets on the receiver RTP port to the server,
3464 * this should make a firewall think we initiated the data transfer and
3465 * hopefully allow packets to go from the sender port to our RTP receiver port */
3467 gst_rtspsrc_send_dummy_packets (GstRTSPSrc * src)
3471 if (src->nat_method != GST_RTSP_NAT_DUMMY)
3474 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3475 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3477 if (stream->fakesrc && stream->udpsink[0]) {
3478 GST_DEBUG_OBJECT (src, "sending dummy packet to stream %p", stream);
3479 gst_element_set_state (stream->udpsink[0], GST_STATE_NULL);
3480 gst_element_set_state (stream->fakesrc, GST_STATE_NULL);
3481 gst_element_set_state (stream->udpsink[0], GST_STATE_PLAYING);
3482 gst_element_set_state (stream->fakesrc, GST_STATE_PLAYING);
3488 /* Adds the source pads of all configured streams to the element.
3489 * This code is performed when we detected dataflow.
3491 * We detect dataflow from either the _loop function or with pad probes on the
3495 gst_rtspsrc_activate_streams (GstRTSPSrc * src)
3499 GST_DEBUG_OBJECT (src, "activating streams");
3501 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3502 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3504 if (stream->udpsrc[0]) {
3505 /* remove timeout, we are streaming now and timeouts will be handled by
3506 * the session manager and jitter buffer */
3507 g_object_set (G_OBJECT (stream->udpsrc[0]), "timeout", (guint64) 0, NULL);
3509 if (stream->srcpad) {
3510 GST_DEBUG_OBJECT (src, "activating stream pad %p", stream);
3511 gst_pad_set_active (stream->srcpad, TRUE);
3513 /* if we don't have a session manager, set the caps now. If we have a
3514 * session, we will get a notification of the pad and the caps. */
3515 if (!src->manager) {
3516 GST_DEBUG_OBJECT (src, "setting pad caps for stream %p", stream);
3517 gst_pad_set_caps (stream->srcpad, stream->caps);
3520 if (!stream->added) {
3521 GST_DEBUG_OBJECT (src, "adding stream pad %p", stream);
3522 gst_element_add_pad (GST_ELEMENT_CAST (src), stream->srcpad);
3523 stream->added = TRUE;
3528 /* unblock all pads */
3529 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3530 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3532 if (stream->blockid) {
3533 GST_DEBUG_OBJECT (src, "unblocking stream pad %p", stream);
3534 gst_pad_remove_probe (stream->blockedpad, stream->blockid);
3535 stream->blockid = 0;
3543 gst_rtspsrc_configure_caps (GstRTSPSrc * src, GstSegment * segment,
3544 gboolean reset_manager)
3547 guint64 start, stop;
3548 gdouble play_speed, play_scale;
3550 GST_DEBUG_OBJECT (src, "configuring stream caps");
3552 start = segment->position;
3553 stop = segment->duration;
3554 play_speed = segment->rate;
3555 play_scale = segment->applied_rate;
3557 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3558 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3561 if ((caps = stream->caps)) {
3562 caps = gst_caps_make_writable (caps);
3564 if (stream->timebase != -1)
3565 gst_caps_set_simple (caps, "clock-base", G_TYPE_UINT,
3566 (guint) stream->timebase, NULL);
3567 if (stream->seqbase != -1)
3568 gst_caps_set_simple (caps, "seqnum-base", G_TYPE_UINT,
3569 (guint) stream->seqbase, NULL);
3570 gst_caps_set_simple (caps, "npt-start", G_TYPE_UINT64, start, NULL);
3572 gst_caps_set_simple (caps, "npt-stop", G_TYPE_UINT64, stop, NULL);
3573 gst_caps_set_simple (caps, "play-speed", G_TYPE_DOUBLE, play_speed, NULL);
3574 gst_caps_set_simple (caps, "play-scale", G_TYPE_DOUBLE, play_scale, NULL);
3576 stream->caps = caps;
3578 GST_DEBUG_OBJECT (src, "stream %p, caps %" GST_PTR_FORMAT, stream, caps);
3580 if (reset_manager && src->manager) {
3581 GST_DEBUG_OBJECT (src, "clear session");
3582 g_signal_emit_by_name (src->manager, "clear-pt-map", NULL);
3586 static GstFlowReturn
3587 gst_rtspsrc_combine_flows (GstRTSPSrc * src, GstRTSPStream * stream,
3592 /* store the value */
3593 stream->last_ret = ret;
3595 /* if it's success we can return the value right away */
3596 if (ret == GST_FLOW_OK)
3599 /* any other error that is not-linked can be returned right
3601 if (ret != GST_FLOW_NOT_LINKED)
3604 /* only return NOT_LINKED if all other pads returned NOT_LINKED */
3605 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3606 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3608 ret = ostream->last_ret;
3609 /* some other return value (must be SUCCESS but we can return
3610 * other values as well) */
3611 if (ret != GST_FLOW_NOT_LINKED)
3614 /* if we get here, all other pads were unlinked and we return
3615 * NOT_LINKED then */
3621 gst_rtspsrc_stream_push_event (GstRTSPSrc * src, GstRTSPStream * stream,
3624 gboolean res = TRUE;
3626 /* only streams that have a connection to the outside world */
3627 if (stream->container || stream->disabled)
3630 if (stream->udpsrc[0]) {
3631 gst_event_ref (event);
3632 res = gst_element_send_event (stream->udpsrc[0], event);
3633 } else if (stream->channelpad[0]) {
3634 gst_event_ref (event);
3635 if (GST_PAD_IS_SRC (stream->channelpad[0]))
3636 res = gst_pad_push_event (stream->channelpad[0], event);
3638 res = gst_pad_send_event (stream->channelpad[0], event);
3641 if (stream->udpsrc[1]) {
3642 gst_event_ref (event);
3643 res &= gst_element_send_event (stream->udpsrc[1], event);
3644 } else if (stream->channelpad[1]) {
3645 gst_event_ref (event);
3646 if (GST_PAD_IS_SRC (stream->channelpad[1]))
3647 res &= gst_pad_push_event (stream->channelpad[1], event);
3649 res &= gst_pad_send_event (stream->channelpad[1], event);
3653 gst_event_unref (event);
3659 gst_rtspsrc_push_event (GstRTSPSrc * src, GstEvent * event)
3662 gboolean res = TRUE;
3664 for (streams = src->streams; streams; streams = g_list_next (streams)) {
3665 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
3667 gst_event_ref (event);
3668 res &= gst_rtspsrc_stream_push_event (src, ostream, event);
3670 gst_event_unref (event);
3675 static GstRTSPResult
3676 gst_rtsp_conninfo_connect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3681 if (info->connection == NULL) {
3682 if (info->url == NULL) {
3683 GST_DEBUG_OBJECT (src, "parsing uri (%s)...", info->location);
3684 if ((res = gst_rtsp_url_parse (info->location, &info->url)) < 0)
3688 /* create connection */
3689 GST_DEBUG_OBJECT (src, "creating connection (%s)...", info->location);
3690 if ((res = gst_rtsp_connection_create (info->url, &info->connection)) < 0)
3691 goto could_not_create;
3694 g_free (info->url_str);
3695 info->url_str = gst_rtsp_url_get_request_uri (info->url);
3697 GST_DEBUG_OBJECT (src, "sanitized uri %s", info->url_str);
3699 if (info->url->transports & GST_RTSP_LOWER_TRANS_TLS) {
3700 if (!gst_rtsp_connection_set_tls_validation_flags (info->connection,
3701 src->tls_validation_flags))
3702 GST_WARNING_OBJECT (src, "Unable to set TLS validation flags");
3705 if (info->url->transports & GST_RTSP_LOWER_TRANS_HTTP)
3706 gst_rtsp_connection_set_tunneled (info->connection, TRUE);
3708 if (src->proxy_host) {
3709 GST_DEBUG_OBJECT (src, "setting proxy %s:%d", src->proxy_host,
3711 gst_rtsp_connection_set_proxy (info->connection, src->proxy_host,
3716 if (!info->connected) {
3719 GST_ELEMENT_PROGRESS (src, CONTINUE, "connect",
3720 ("Connecting to %s", info->location));
3721 GST_DEBUG_OBJECT (src, "connecting (%s)...", info->location);
3723 gst_rtsp_connection_connect (info->connection,
3724 src->ptcp_timeout)) < 0)
3725 goto could_not_connect;
3727 info->connected = TRUE;
3734 GST_ERROR_OBJECT (src, "No valid RTSP URL was provided");
3739 gchar *str = gst_rtsp_strresult (res);
3740 GST_ERROR_OBJECT (src, "Could not create connection. (%s)", str);
3746 gchar *str = gst_rtsp_strresult (res);
3747 GST_ERROR_OBJECT (src, "Could not connect to server. (%s)", str);
3753 static GstRTSPResult
3754 gst_rtsp_conninfo_close (GstRTSPSrc * src, GstRTSPConnInfo * info,
3757 GST_RTSP_STATE_LOCK (src);
3758 if (info->connected) {
3759 GST_DEBUG_OBJECT (src, "closing connection...");
3760 gst_rtsp_connection_close (info->connection);
3761 info->connected = FALSE;
3763 if (free && info->connection) {
3764 /* free connection */
3765 GST_DEBUG_OBJECT (src, "freeing connection...");
3766 gst_rtsp_connection_free (info->connection);
3767 info->connection = NULL;
3769 GST_RTSP_STATE_UNLOCK (src);
3773 static GstRTSPResult
3774 gst_rtsp_conninfo_reconnect (GstRTSPSrc * src, GstRTSPConnInfo * info,
3779 GST_DEBUG_OBJECT (src, "reconnecting connection...");
3780 gst_rtsp_conninfo_close (src, info, FALSE);
3781 res = gst_rtsp_conninfo_connect (src, info, async);
3787 gst_rtspsrc_connection_flush (GstRTSPSrc * src, gboolean flush)
3791 GST_DEBUG_OBJECT (src, "set flushing %d", flush);
3792 GST_RTSP_STATE_LOCK (src);
3793 if (src->conninfo.connection && src->conninfo.flushing != flush) {
3794 GST_DEBUG_OBJECT (src, "connection flush");
3795 gst_rtsp_connection_flush (src->conninfo.connection, flush);
3796 src->conninfo.flushing = flush;
3798 for (walk = src->streams; walk; walk = g_list_next (walk)) {
3799 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
3800 if (stream->conninfo.connection && stream->conninfo.flushing != flush) {
3801 GST_DEBUG_OBJECT (src, "stream %p flush", stream);
3802 gst_rtsp_connection_flush (stream->conninfo.connection, flush);
3803 stream->conninfo.flushing = flush;
3806 GST_RTSP_STATE_UNLOCK (src);
3809 /* FIXME, handle server request, reply with OK, for now */
3810 static GstRTSPResult
3811 gst_rtspsrc_handle_request (GstRTSPSrc * src, GstRTSPConnection * conn,
3812 GstRTSPMessage * request)
3814 GstRTSPMessage response = { 0 };
3817 GST_DEBUG_OBJECT (src, "got server request message");
3820 gst_rtsp_message_dump (request);
3822 res = gst_rtsp_ext_list_receive_request (src->extensions, request);
3824 if (res == GST_RTSP_ENOTIMPL) {
3825 /* default implementation, send OK */
3826 GST_DEBUG_OBJECT (src, "prepare OK reply");
3828 gst_rtsp_message_init_response (&response, GST_RTSP_STS_OK, "OK",
3833 /* let app parse and reply */
3834 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_HANDLE_REQUEST],
3835 0, request, &response);
3838 gst_rtsp_message_dump (&response);
3840 res = gst_rtspsrc_connection_send (src, conn, &response, NULL);
3844 gst_rtsp_message_unset (&response);
3845 } else if (res == GST_RTSP_EEOF)
3853 gst_rtsp_message_unset (&response);
3858 /* send server keep-alive */
3859 static GstRTSPResult
3860 gst_rtspsrc_send_keep_alive (GstRTSPSrc * src)
3862 GstRTSPMessage request = { 0 };
3864 GstRTSPMethod method;
3867 if (src->do_rtsp_keep_alive == FALSE) {
3868 GST_DEBUG_OBJECT (src, "do-rtsp-keep-alive is FALSE, not sending.");
3869 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3873 GST_DEBUG_OBJECT (src, "creating server keep-alive");
3875 /* find a method to use for keep-alive */
3876 if (src->methods & GST_RTSP_GET_PARAMETER)
3877 method = GST_RTSP_GET_PARAMETER;
3879 method = GST_RTSP_OPTIONS;
3882 control = src->control;
3884 control = src->conninfo.url_str;
3886 if (control == NULL)
3889 res = gst_rtsp_message_init_request (&request, method, control);
3894 gst_rtsp_message_dump (&request);
3897 gst_rtspsrc_connection_send (src, src->conninfo.connection, &request,
3902 gst_rtsp_connection_reset_timeout (src->conninfo.connection);
3903 gst_rtsp_message_unset (&request);
3910 GST_WARNING_OBJECT (src, "no control url to send keepalive");
3915 gchar *str = gst_rtsp_strresult (res);
3917 gst_rtsp_message_unset (&request);
3918 GST_ELEMENT_WARNING (src, RESOURCE, WRITE, (NULL),
3919 ("Could not send keep-alive. (%s)", str));
3925 static GstFlowReturn
3926 gst_rtspsrc_handle_data (GstRTSPSrc * src, GstRTSPMessage * message)
3928 GstFlowReturn ret = GST_FLOW_OK;
3930 GstRTSPStream *stream;
3931 GstPad *outpad = NULL;
3938 channel = message->type_data.data.channel;
3940 stream = find_stream (src, &channel, (gpointer) find_stream_by_channel);
3942 goto unknown_stream;
3944 if (channel == stream->channel[0]) {
3945 outpad = stream->channelpad[0];
3947 } else if (channel == stream->channel[1]) {
3948 outpad = stream->channelpad[1];
3954 /* take a look at the body to figure out what we have */
3955 gst_rtsp_message_get_body (message, &data, &size);
3957 goto invalid_length;
3959 /* channels are not correct on some servers, do extra check */
3960 if (data[1] >= 200 && data[1] <= 204) {
3961 /* hmm RTCP message switch to the RTCP pad of the same stream. */
3962 outpad = stream->channelpad[1];
3966 /* we have no clue what this is, just ignore then. */
3968 goto unknown_stream;
3970 /* take the message body for further processing */
3971 gst_rtsp_message_steal_body (message, &data, &size);
3973 /* strip the trailing \0 */
3976 buf = gst_buffer_new ();
3977 gst_buffer_append_memory (buf,
3978 gst_memory_new_wrapped (0, data, size, 0, size, data, g_free));
3980 /* don't need message anymore */
3981 gst_rtsp_message_unset (message);
3983 GST_DEBUG_OBJECT (src, "pushing data of size %d on channel %d", size,
3986 if (src->need_activate) {
3992 guint group_id = gst_util_group_id_next ();
3994 /* generate an SHA256 sum of the URI */
3995 cs = g_checksum_new (G_CHECKSUM_SHA256);
3996 uri = src->conninfo.location;
3997 g_checksum_update (cs, (const guchar *) uri, strlen (uri));
3999 for (streams = src->streams; streams; streams = g_list_next (streams)) {
4000 GstRTSPStream *ostream = (GstRTSPStream *) streams->data;
4003 g_strdup_printf ("%s/%d", g_checksum_get_string (cs), ostream->id);
4004 event = gst_event_new_stream_start (stream_id);
4005 gst_event_set_group_id (event, group_id);
4008 gst_rtspsrc_stream_push_event (src, ostream, event);
4010 g_checksum_free (cs);
4012 gst_rtspsrc_activate_streams (src);
4013 src->need_activate = FALSE;
4015 if ((event = src->start_segment) != NULL) {
4016 src->start_segment = NULL;
4017 gst_rtspsrc_push_event (src, event);
4020 if (src->base_time == -1) {
4021 /* Take current running_time. This timestamp will be put on
4022 * the first buffer of each stream because we are a live source and so we
4023 * timestamp with the running_time. When we are dealing with TCP, we also
4024 * only timestamp the first buffer (using the DISCONT flag) because a server
4025 * typically bursts data, for which we don't want to compensate by speeding
4026 * up the media. The other timestamps will be interpollated from this one
4027 * using the RTP timestamps. */
4028 GST_OBJECT_LOCK (src);
4029 if (GST_ELEMENT_CLOCK (src)) {
4031 GstClockTime base_time;
4033 now = gst_clock_get_time (GST_ELEMENT_CLOCK (src));
4034 base_time = GST_ELEMENT_CAST (src)->base_time;
4036 src->base_time = now - base_time;
4038 GST_DEBUG_OBJECT (src, "first buffer at time %" GST_TIME_FORMAT ", base %"
4039 GST_TIME_FORMAT, GST_TIME_ARGS (now), GST_TIME_ARGS (base_time));
4041 GST_OBJECT_UNLOCK (src);
4044 if (stream->discont && !is_rtcp) {
4045 /* mark first RTP buffer as discont */
4046 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
4047 stream->discont = FALSE;
4048 /* first buffer gets the timestamp, other buffers are not timestamped and
4049 * their presentation time will be interpollated from the rtp timestamps. */
4050 GST_DEBUG_OBJECT (src, "setting timestamp %" GST_TIME_FORMAT,
4051 GST_TIME_ARGS (src->base_time));
4053 GST_BUFFER_TIMESTAMP (buf) = src->base_time;
4056 /* chain to the peer pad */
4057 if (GST_PAD_IS_SINK (outpad))
4058 ret = gst_pad_chain (outpad, buf);
4060 ret = gst_pad_push (outpad, buf);
4063 /* combine all stream flows for the data transport */
4064 ret = gst_rtspsrc_combine_flows (src, stream, ret);
4071 GST_DEBUG_OBJECT (src, "unknown stream on channel %d, ignored", channel);
4072 gst_rtsp_message_unset (message);
4077 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4078 ("Short message received, ignoring."));
4079 gst_rtsp_message_unset (message);
4084 static GstFlowReturn
4085 gst_rtspsrc_loop_interleaved (GstRTSPSrc * src)
4087 GstRTSPMessage message = { 0 };
4089 GstFlowReturn ret = GST_FLOW_OK;
4090 GTimeVal tv_timeout;
4093 /* get the next timeout interval */
4094 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4096 /* see if the timeout period expired */
4097 if ((tv_timeout.tv_sec | tv_timeout.tv_usec) == 0) {
4098 GST_DEBUG_OBJECT (src, "timout, sending keep-alive");
4099 /* send keep-alive, only act on interrupt, a warning will be posted for
4101 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4103 /* get new timeout */
4104 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4107 GST_DEBUG_OBJECT (src, "doing receive with timeout %ld seconds, %ld usec",
4108 tv_timeout.tv_sec, tv_timeout.tv_usec);
4110 /* protect the connection with the connection lock so that we can see when
4111 * we are finished doing server communication */
4113 gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4114 &message, src->ptcp_timeout);
4118 GST_DEBUG_OBJECT (src, "we received a server message");
4120 case GST_RTSP_EINTR:
4121 /* we got interrupted this means we need to stop */
4123 case GST_RTSP_ETIMEOUT:
4124 /* no reply, send keep alive */
4125 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4126 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4130 /* go EOS when the server closed the connection */
4136 switch (message.type) {
4137 case GST_RTSP_MESSAGE_REQUEST:
4138 /* server sends us a request message, handle it */
4140 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4142 if (res == GST_RTSP_EEOF)
4145 goto handle_request_failed;
4147 case GST_RTSP_MESSAGE_RESPONSE:
4148 /* we ignore response messages */
4149 GST_DEBUG_OBJECT (src, "ignoring response message");
4151 gst_rtsp_message_dump (&message);
4153 case GST_RTSP_MESSAGE_DATA:
4154 GST_DEBUG_OBJECT (src, "got data message");
4155 ret = gst_rtspsrc_handle_data (src, &message);
4156 if (ret != GST_FLOW_OK)
4157 goto handle_data_failed;
4160 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4165 g_assert_not_reached ();
4170 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4171 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4172 ("The server closed the connection."));
4173 src->conninfo.connected = FALSE;
4174 gst_rtsp_message_unset (&message);
4175 return GST_FLOW_EOS;
4179 gst_rtsp_message_unset (&message);
4180 GST_DEBUG_OBJECT (src, "got interrupted");
4181 return GST_FLOW_FLUSHING;
4185 gchar *str = gst_rtsp_strresult (res);
4187 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4188 ("Could not receive message. (%s)", str));
4191 gst_rtsp_message_unset (&message);
4192 return GST_FLOW_ERROR;
4194 handle_request_failed:
4196 gchar *str = gst_rtsp_strresult (res);
4198 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4199 ("Could not handle server message. (%s)", str));
4201 gst_rtsp_message_unset (&message);
4202 return GST_FLOW_ERROR;
4206 GST_DEBUG_OBJECT (src, "could no handle data message");
4211 static GstFlowReturn
4212 gst_rtspsrc_loop_udp (GstRTSPSrc * src)
4215 GstRTSPMessage message = { 0 };
4219 GTimeVal tv_timeout;
4221 /* get the next timeout interval */
4222 gst_rtsp_connection_next_timeout (src->conninfo.connection, &tv_timeout);
4224 GST_DEBUG_OBJECT (src, "doing receive with timeout %d seconds",
4225 (gint) tv_timeout.tv_sec);
4227 gst_rtsp_message_unset (&message);
4229 /* we should continue reading the TCP socket because the server might
4230 * send us requests. When the session timeout expires, we need to send a
4231 * keep-alive request to keep the session open. */
4232 res = gst_rtspsrc_connection_receive (src, src->conninfo.connection,
4233 &message, &tv_timeout);
4237 GST_DEBUG_OBJECT (src, "we received a server message");
4239 case GST_RTSP_EINTR:
4240 /* we got interrupted, see what we have to do */
4242 case GST_RTSP_ETIMEOUT:
4243 /* send keep-alive, ignore the result, a warning will be posted. */
4244 GST_DEBUG_OBJECT (src, "timeout, sending keep-alive");
4245 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4249 /* server closed the connection. not very fatal for UDP, reconnect and
4250 * see what happens. */
4251 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4252 ("The server closed the connection."));
4253 if (src->udp_reconnect) {
4255 gst_rtsp_conninfo_reconnect (src, &src->conninfo, FALSE)) < 0)
4262 GST_DEBUG_OBJECT (src, "An ethernet problem occured.");
4264 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4265 ("Unhandled return value %d.", res));
4269 switch (message.type) {
4270 case GST_RTSP_MESSAGE_REQUEST:
4271 /* server sends us a request message, handle it */
4273 gst_rtspsrc_handle_request (src, src->conninfo.connection,
4275 if (res == GST_RTSP_EEOF)
4278 goto handle_request_failed;
4280 case GST_RTSP_MESSAGE_RESPONSE:
4281 /* we ignore response and data messages */
4282 GST_DEBUG_OBJECT (src, "ignoring response message");
4284 gst_rtsp_message_dump (&message);
4285 if (message.type_data.response.code == GST_RTSP_STS_UNAUTHORIZED) {
4286 GST_DEBUG_OBJECT (src, "but is Unauthorized response ...");
4287 if (gst_rtspsrc_setup_auth (src, &message) && !(retry++)) {
4288 GST_DEBUG_OBJECT (src, "so retrying keep-alive");
4289 if ((res = gst_rtspsrc_send_keep_alive (src)) == GST_RTSP_EINTR)
4296 case GST_RTSP_MESSAGE_DATA:
4297 /* we ignore response and data messages */
4298 GST_DEBUG_OBJECT (src, "ignoring data message");
4301 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4306 g_assert_not_reached ();
4308 /* we get here when the connection got interrupted */
4311 gst_rtsp_message_unset (&message);
4312 GST_DEBUG_OBJECT (src, "got interrupted");
4313 return GST_FLOW_FLUSHING;
4317 gchar *str = gst_rtsp_strresult (res);
4320 src->conninfo.connected = FALSE;
4321 if (res != GST_RTSP_EINTR) {
4322 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
4323 ("Could not connect to server. (%s)", str));
4325 ret = GST_FLOW_ERROR;
4327 ret = GST_FLOW_FLUSHING;
4333 gchar *str = gst_rtsp_strresult (res);
4335 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4336 ("Could not receive message. (%s)", str));
4338 return GST_FLOW_ERROR;
4340 handle_request_failed:
4342 gchar *str = gst_rtsp_strresult (res);
4345 gst_rtsp_message_unset (&message);
4346 if (res != GST_RTSP_EINTR) {
4347 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
4348 ("Could not handle server message. (%s)", str));
4350 ret = GST_FLOW_ERROR;
4352 ret = GST_FLOW_FLUSHING;
4358 GST_DEBUG_OBJECT (src, "we got an eof from the server");
4359 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4360 ("The server closed the connection."));
4361 src->conninfo.connected = FALSE;
4362 gst_rtsp_message_unset (&message);
4363 return GST_FLOW_EOS;
4367 static GstRTSPResult
4368 gst_rtspsrc_reconnect (GstRTSPSrc * src, gboolean async)
4370 GstRTSPResult res = GST_RTSP_OK;
4373 GST_DEBUG_OBJECT (src, "doing reconnect");
4375 GST_OBJECT_LOCK (src);
4376 /* only restart when the pads were not yet activated, else we were
4377 * streaming over UDP */
4378 restart = src->need_activate;
4379 GST_OBJECT_UNLOCK (src);
4381 /* no need to restart, we're done */
4385 /* we can try only TCP now */
4386 src->cur_protocols = GST_RTSP_LOWER_TRANS_TCP;
4388 /* close and cleanup our state */
4389 if ((res = gst_rtspsrc_close (src, async, FALSE)) < 0)
4392 /* see if we have TCP left to try. Also don't try TCP when we were configured
4394 if (!(src->protocols & GST_RTSP_LOWER_TRANS_TCP) || src->from_sdp)
4397 /* We post a warning message now to inform the user
4398 * that nothing happened. It's most likely a firewall thing. */
4399 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
4400 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4401 "firewall is blocking it. Retrying using a TCP connection.",
4402 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4404 /* open new connection using tcp */
4405 if (gst_rtspsrc_open (src, async) < 0)
4408 /* start playback */
4409 if (gst_rtspsrc_play (src, &src->segment, async) < 0)
4418 src->cur_protocols = 0;
4419 /* no transport possible, post an error and stop */
4420 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
4421 ("Could not receive any UDP packets for %.4f seconds, maybe your "
4422 "firewall is blocking it. No other protocols to try.",
4423 gst_guint64_to_gdouble (src->udp_timeout / 1000000.0)));
4424 return GST_RTSP_ERROR;
4428 GST_DEBUG_OBJECT (src, "open failed");
4433 GST_DEBUG_OBJECT (src, "play failed");
4439 gst_rtspsrc_loop_start_cmd (GstRTSPSrc * src, gint cmd)
4443 GST_ELEMENT_PROGRESS (src, START, "open", ("Opening Stream"));
4446 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PLAY request"));
4449 GST_ELEMENT_PROGRESS (src, START, "request", ("Sending PAUSE request"));
4452 GST_ELEMENT_PROGRESS (src, START, "close", ("Closing Stream"));
4460 gst_rtspsrc_loop_complete_cmd (GstRTSPSrc * src, gint cmd)
4464 GST_ELEMENT_PROGRESS (src, COMPLETE, "open", ("Opened Stream"));
4467 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PLAY request"));
4470 GST_ELEMENT_PROGRESS (src, COMPLETE, "request", ("Sent PAUSE request"));
4473 GST_ELEMENT_PROGRESS (src, COMPLETE, "close", ("Closed Stream"));
4481 gst_rtspsrc_loop_cancel_cmd (GstRTSPSrc * src, gint cmd)
4485 GST_ELEMENT_PROGRESS (src, CANCELED, "open", ("Open canceled"));
4488 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PLAY canceled"));
4491 GST_ELEMENT_PROGRESS (src, CANCELED, "request", ("PAUSE canceled"));
4494 GST_ELEMENT_PROGRESS (src, CANCELED, "close", ("Close canceled"));
4502 gst_rtspsrc_loop_error_cmd (GstRTSPSrc * src, gint cmd)
4506 GST_ELEMENT_PROGRESS (src, ERROR, "open", ("Open failed"));
4509 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PLAY failed"));
4512 GST_ELEMENT_PROGRESS (src, ERROR, "request", ("PAUSE failed"));
4515 GST_ELEMENT_PROGRESS (src, ERROR, "close", ("Close failed"));
4523 gst_rtspsrc_loop_end_cmd (GstRTSPSrc * src, gint cmd, GstRTSPResult ret)
4525 if (ret == GST_RTSP_OK)
4526 gst_rtspsrc_loop_complete_cmd (src, cmd);
4527 else if (ret == GST_RTSP_EINTR)
4528 gst_rtspsrc_loop_cancel_cmd (src, cmd);
4530 gst_rtspsrc_loop_error_cmd (src, cmd);
4534 gst_rtspsrc_loop_send_cmd (GstRTSPSrc * src, gint cmd, gint mask)
4537 gboolean flushed = FALSE;
4539 /* start new request */
4540 gst_rtspsrc_loop_start_cmd (src, cmd);
4542 GST_DEBUG_OBJECT (src, "sending cmd %d", cmd);
4544 GST_OBJECT_LOCK (src);
4545 old = src->pending_cmd;
4546 if (old == CMD_RECONNECT) {
4547 GST_DEBUG_OBJECT (src, "ignore, we were reconnecting");
4548 cmd = CMD_RECONNECT;
4550 if (old != CMD_WAIT) {
4551 src->pending_cmd = CMD_WAIT;
4552 GST_OBJECT_UNLOCK (src);
4553 /* cancel previous request */
4554 GST_DEBUG_OBJECT (src, "cancel previous request %d", old);
4555 gst_rtspsrc_loop_cancel_cmd (src, old);
4556 GST_OBJECT_LOCK (src);
4558 src->pending_cmd = cmd;
4559 /* interrupt if allowed */
4560 if (src->busy_cmd & mask) {
4561 GST_DEBUG_OBJECT (src, "connection flush busy %d", src->busy_cmd);
4562 gst_rtspsrc_connection_flush (src, TRUE);
4565 GST_DEBUG_OBJECT (src, "not interrupting busy cmd %d", src->busy_cmd);
4568 gst_task_start (src->task);
4569 GST_OBJECT_UNLOCK (src);
4575 gst_rtspsrc_loop (GstRTSPSrc * src)
4579 if (!src->conninfo.connection || !src->conninfo.connected)
4582 if (src->interleaved)
4583 ret = gst_rtspsrc_loop_interleaved (src);
4585 ret = gst_rtspsrc_loop_udp (src);
4587 if (ret != GST_FLOW_OK)
4595 GST_WARNING_OBJECT (src, "we are not connected");
4596 ret = GST_FLOW_FLUSHING;
4601 const gchar *reason = gst_flow_get_name (ret);
4603 GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
4604 src->running = FALSE;
4605 if (ret == GST_FLOW_EOS) {
4606 /* perform EOS logic */
4607 if (src->segment.flags & GST_SEEK_FLAG_SEGMENT) {
4608 gst_element_post_message (GST_ELEMENT_CAST (src),
4609 gst_message_new_segment_done (GST_OBJECT_CAST (src),
4610 src->segment.format, src->segment.position));
4611 gst_rtspsrc_push_event (src,
4612 gst_event_new_segment_done (src->segment.format,
4613 src->segment.position));
4615 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4617 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
4618 /* for fatal errors we post an error message, post the error before the
4619 * EOS so the app knows about the error first. */
4620 GST_ELEMENT_ERROR (src, STREAM, FAILED,
4621 ("Internal data flow error."),
4622 ("streaming task paused, reason %s (%d)", reason, ret));
4623 gst_rtspsrc_push_event (src, gst_event_new_eos ());
4625 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_LOOP);
4630 #ifndef GST_DISABLE_GST_DEBUG
4631 static const gchar *
4632 gst_rtsp_auth_method_to_string (GstRTSPAuthMethod method)
4636 while (method != 0) {
4653 static const gchar *
4654 gst_rtspsrc_skip_lws (const gchar * s)
4656 while (g_ascii_isspace (*s))
4661 static const gchar *
4662 gst_rtspsrc_unskip_lws (const gchar * s, const gchar * start)
4664 while (s > start && g_ascii_isspace (*(s - 1)))
4669 static const gchar *
4670 gst_rtspsrc_skip_commas (const gchar * s)
4672 /* The grammar allows for multiple commas */
4673 while (g_ascii_isspace (*s) || *s == ',')
4678 static const gchar *
4679 gst_rtspsrc_skip_item (const gchar * s)
4681 gboolean quoted = FALSE;
4682 const gchar *start = s;
4684 /* A list item ends at the last non-whitespace character
4685 * before a comma which is not inside a quoted-string. Or at
4686 * the end of the string.
4692 if (*s == '\\' && *(s + 1))
4701 return gst_rtspsrc_unskip_lws (s, start);
4705 gst_rtsp_decode_quoted_string (gchar * quoted_string)
4709 src = quoted_string + 1;
4710 dst = quoted_string;
4711 while (*src && *src != '"') {
4712 if (*src == '\\' && *(src + 1))
4719 /* Extract the authentication tokens that the server provided for each method
4720 * into an array of structures and give those to the connection object.
4723 gst_rtspsrc_parse_digest_challenge (GstRTSPConnection * conn,
4724 const gchar * header, gboolean * stale)
4726 GSList *list = NULL, *iter;
4728 gchar *item, *eq, *name_end, *value;
4730 g_return_if_fail (stale != NULL);
4732 gst_rtsp_connection_clear_auth_params (conn);
4735 /* Parse a header whose content is described by RFC2616 as
4736 * "#something", where "something" does not itself contain commas,
4737 * except as part of quoted-strings, into a list of allocated strings.
4739 header = gst_rtspsrc_skip_commas (header);
4741 end = gst_rtspsrc_skip_item (header);
4742 list = g_slist_prepend (list, g_strndup (header, end - header));
4743 header = gst_rtspsrc_skip_commas (end);
4748 list = g_slist_reverse (list);
4749 for (iter = list; iter; iter = iter->next) {
4752 eq = strchr (item, '=');
4754 name_end = (gchar *) gst_rtspsrc_unskip_lws (eq, item);
4755 if (name_end == item) {
4756 /* That's no good... */
4763 value = (gchar *) gst_rtspsrc_skip_lws (eq + 1);
4765 gst_rtsp_decode_quoted_string (value);
4769 if (item && (strcmp (item, "stale") == 0) &&
4770 value && (strcmp (value, "TRUE") == 0))
4772 gst_rtsp_connection_set_auth_param (conn, item, value);
4776 g_slist_free (list);
4779 /* Parse a WWW-Authenticate Response header and determine the
4780 * available authentication methods
4782 * This code should also cope with the fact that each WWW-Authenticate
4783 * header can contain multiple challenge methods + tokens
4785 * At the moment, for Basic auth, we just do a minimal check and don't
4786 * even parse out the realm */
4788 gst_rtspsrc_parse_auth_hdr (gchar * hdr, GstRTSPAuthMethod * methods,
4789 GstRTSPConnection * conn, gboolean * stale)
4793 g_return_if_fail (hdr != NULL);
4794 g_return_if_fail (methods != NULL);
4795 g_return_if_fail (stale != NULL);
4797 /* Skip whitespace at the start of the string */
4798 for (start = hdr; start[0] != '\0' && g_ascii_isspace (start[0]); start++);
4800 if (g_ascii_strncasecmp (start, "basic", 5) == 0)
4801 *methods |= GST_RTSP_AUTH_BASIC;
4802 else if (g_ascii_strncasecmp (start, "digest ", 7) == 0) {
4803 *methods |= GST_RTSP_AUTH_DIGEST;
4804 gst_rtspsrc_parse_digest_challenge (conn, &start[7], stale);
4809 * gst_rtspsrc_setup_auth:
4810 * @src: the rtsp source
4812 * Configure a username and password and auth method on the
4813 * connection object based on a response we received from the
4816 * Currently, this requires that a username and password were supplied
4817 * in the uri. In the future, they may be requested on demand by sending
4818 * a message up the bus.
4820 * Returns: TRUE if authentication information could be set up correctly.
4823 gst_rtspsrc_setup_auth (GstRTSPSrc * src, GstRTSPMessage * response)
4827 GstRTSPAuthMethod avail_methods = GST_RTSP_AUTH_NONE;
4828 GstRTSPAuthMethod method;
4829 GstRTSPResult auth_result;
4831 GstRTSPConnection *conn;
4833 gboolean stale = FALSE;
4835 conn = src->conninfo.connection;
4837 /* Identify the available auth methods and see if any are supported */
4838 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_WWW_AUTHENTICATE,
4839 &hdr, 0) == GST_RTSP_OK) {
4840 gst_rtspsrc_parse_auth_hdr (hdr, &avail_methods, conn, &stale);
4843 if (avail_methods == GST_RTSP_AUTH_NONE)
4844 goto no_auth_available;
4846 /* For digest auth, if the response indicates that the session
4847 * data are stale, we just update them in the connection object and
4848 * return TRUE to retry the request */
4850 src->tried_url_auth = FALSE;
4852 url = gst_rtsp_connection_get_url (conn);
4854 /* Do we have username and password available? */
4855 if (url != NULL && !src->tried_url_auth && url->user != NULL
4856 && url->passwd != NULL) {
4859 src->tried_url_auth = TRUE;
4860 GST_DEBUG_OBJECT (src,
4861 "Attempting authentication using credentials from the URL");
4863 user = src->user_id;
4864 pass = src->user_pw;
4865 GST_DEBUG_OBJECT (src,
4866 "Attempting authentication using credentials from the properties");
4869 /* FIXME: If the url didn't contain username and password or we tried them
4870 * already, request a username and passwd from the application via some kind
4871 * of credentials request message */
4873 /* If we don't have a username and passwd at this point, bail out. */
4874 if (user == NULL || pass == NULL)
4877 /* Try to configure for each available authentication method, strongest to
4879 for (method = GST_RTSP_AUTH_MAX; method != GST_RTSP_AUTH_NONE; method >>= 1) {
4880 /* Check if this method is available on the server */
4881 if ((method & avail_methods) == 0)
4884 /* Pass the credentials to the connection to try on the next request */
4885 auth_result = gst_rtsp_connection_set_auth (conn, method, user, pass);
4886 /* INVAL indicates an invalid username/passwd were supplied, so we'll just
4887 * ignore it and end up retrying later */
4888 if (auth_result == GST_RTSP_OK || auth_result == GST_RTSP_EINVAL) {
4889 GST_DEBUG_OBJECT (src, "Attempting %s authentication",
4890 gst_rtsp_auth_method_to_string (method));
4895 if (method == GST_RTSP_AUTH_NONE)
4896 goto no_auth_available;
4902 /* Output an error indicating that we couldn't connect because there were
4903 * no supported authentication protocols */
4904 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
4905 ("No supported authentication protocol was found"));
4910 /* We don't fire an error message, we just return FALSE and let the
4911 * normal NOT_AUTHORIZED error be propagated */
4916 static GstRTSPResult
4917 gst_rtspsrc_try_send (GstRTSPSrc * src, GstRTSPConnection * conn,
4918 GstRTSPMessage * request, GstRTSPMessage * response,
4919 GstRTSPStatusCode * code)
4922 GstRTSPStatusCode thecode;
4923 gchar *content_base = NULL;
4927 if (!src->short_header)
4928 gst_rtsp_ext_list_before_send (src->extensions, request);
4930 GST_DEBUG_OBJECT (src, "sending message");
4933 gst_rtsp_message_dump (request);
4935 res = gst_rtspsrc_connection_send (src, conn, request, src->ptcp_timeout);
4939 gst_rtsp_connection_reset_timeout (conn);
4942 res = gst_rtspsrc_connection_receive (src, conn, response, src->ptcp_timeout);
4947 gst_rtsp_message_dump (response);
4949 switch (response->type) {
4950 case GST_RTSP_MESSAGE_REQUEST:
4951 res = gst_rtspsrc_handle_request (src, conn, response);
4952 if (res == GST_RTSP_EEOF)
4955 goto handle_request_failed;
4957 case GST_RTSP_MESSAGE_RESPONSE:
4958 /* ok, a response is good */
4959 GST_DEBUG_OBJECT (src, "received response message");
4961 case GST_RTSP_MESSAGE_DATA:
4962 /* get next response */
4963 GST_DEBUG_OBJECT (src, "handle data response message");
4964 gst_rtspsrc_handle_data (src, response);
4967 GST_WARNING_OBJECT (src, "ignoring unknown message type %d",
4972 thecode = response->type_data.response.code;
4974 GST_DEBUG_OBJECT (src, "got response message %d", thecode);
4976 /* if the caller wanted the result code, we store it. */
4980 /* If the request didn't succeed, bail out before doing any more */
4981 if (thecode != GST_RTSP_STS_OK)
4984 /* store new content base if any */
4985 gst_rtsp_message_get_header (response, GST_RTSP_HDR_CONTENT_BASE,
4988 g_free (src->content_base);
4989 src->content_base = g_strdup (content_base);
4991 gst_rtsp_ext_list_after_send (src->extensions, request, response);
4998 gchar *str = gst_rtsp_strresult (res);
5000 if (res != GST_RTSP_EINTR) {
5001 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5002 ("Could not send message. (%s)", str));
5004 GST_WARNING_OBJECT (src, "send interrupted");
5013 GST_WARNING_OBJECT (src, "server closed connection");
5014 if ((try == 0) && !src->interleaved && src->udp_reconnect) {
5016 /* if reconnect succeeds, try again */
5018 gst_rtsp_conninfo_reconnect (src, &src->conninfo,
5022 /* only try once after reconnect, then fallthrough and error out */
5025 gchar *str = gst_rtsp_strresult (res);
5027 if (res != GST_RTSP_EINTR) {
5028 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5029 ("Could not receive message. (%s)", str));
5031 GST_WARNING_OBJECT (src, "receive interrupted");
5039 handle_request_failed:
5041 /* ERROR was posted */
5042 gst_rtsp_message_unset (response);
5047 GST_DEBUG_OBJECT (src, "we got an eof from the server");
5048 GST_ELEMENT_WARNING (src, RESOURCE, READ, (NULL),
5049 ("The server closed the connection."));
5050 gst_rtsp_message_unset (response);
5057 * @src: the rtsp source
5058 * @conn: the connection to send on
5059 * @request: must point to a valid request
5060 * @response: must point to an empty #GstRTSPMessage
5061 * @code: an optional code result
5063 * send @request and retrieve the response in @response. optionally @code can be
5064 * non-NULL in which case it will contain the status code of the response.
5066 * If This function returns #GST_RTSP_OK, @response will contain a valid response
5067 * message that should be cleaned with gst_rtsp_message_unset() after usage.
5069 * If @code is NULL, this function will return #GST_RTSP_ERROR (with an invalid
5070 * @response message) if the response code was not 200 (OK).
5072 * If the attempt results in an authentication failure, then this will attempt
5073 * to retrieve authentication credentials via gst_rtspsrc_setup_auth and retry
5076 * Returns: #GST_RTSP_OK if the processing was successful.
5078 static GstRTSPResult
5079 gst_rtspsrc_send (GstRTSPSrc * src, GstRTSPConnection * conn,
5080 GstRTSPMessage * request, GstRTSPMessage * response,
5081 GstRTSPStatusCode * code)
5083 GstRTSPStatusCode int_code = GST_RTSP_STS_OK;
5084 GstRTSPResult res = GST_RTSP_ERROR;
5087 GstRTSPMethod method = GST_RTSP_INVALID;
5093 /* make sure we don't loop forever */
5097 /* save method so we can disable it when the server complains */
5098 method = request->type_data.request.method;
5101 gst_rtspsrc_try_send (src, conn, request, response, &int_code)) < 0)
5105 case GST_RTSP_STS_UNAUTHORIZED:
5106 if (gst_rtspsrc_setup_auth (src, response)) {
5107 /* Try the request/response again after configuring the auth info
5115 } while (retry == TRUE);
5117 /* If the user requested the code, let them handle errors, otherwise
5118 * post an error below */
5121 else if (int_code != GST_RTSP_STS_OK)
5122 goto error_response;
5129 GST_DEBUG_OBJECT (src, "got error %d", res);
5134 res = GST_RTSP_ERROR;
5136 switch (response->type_data.response.code) {
5137 case GST_RTSP_STS_NOT_FOUND:
5138 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL), ("%s",
5139 response->type_data.response.reason));
5141 case GST_RTSP_STS_MOVED_PERMANENTLY:
5142 case GST_RTSP_STS_MOVE_TEMPORARILY:
5144 gchar *new_location;
5145 GstRTSPLowerTrans transports;
5147 GST_DEBUG_OBJECT (src, "got redirection");
5148 /* if we don't have a Location Header, we must error */
5149 if (gst_rtsp_message_get_header (response, GST_RTSP_HDR_LOCATION,
5150 &new_location, 0) < 0)
5153 /* When we receive a redirect result, we go back to the INIT state after
5154 * parsing the new URI. The caller should do the needed steps to issue
5155 * a new setup when it detects this state change. */
5156 GST_DEBUG_OBJECT (src, "redirection to %s", new_location);
5158 /* save current transports */
5159 if (src->conninfo.url)
5160 transports = src->conninfo.url->transports;
5162 transports = GST_RTSP_LOWER_TRANS_UNKNOWN;
5164 gst_rtspsrc_uri_set_uri (GST_URI_HANDLER (src), new_location, NULL);
5166 /* set old transports */
5167 if (src->conninfo.url && transports != GST_RTSP_LOWER_TRANS_UNKNOWN)
5168 src->conninfo.url->transports = transports;
5170 src->need_redirect = TRUE;
5171 src->state = GST_RTSP_STATE_INIT;
5175 case GST_RTSP_STS_NOT_ACCEPTABLE:
5176 case GST_RTSP_STS_NOT_IMPLEMENTED:
5177 case GST_RTSP_STS_METHOD_NOT_ALLOWED:
5178 GST_WARNING_OBJECT (src, "got NOT IMPLEMENTED, disable method %s",
5179 gst_rtsp_method_as_text (method));
5180 src->methods &= ~method;
5184 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5185 ("Got error response: %d (%s).", response->type_data.response.code,
5186 response->type_data.response.reason));
5189 /* if we return ERROR we should unset the response ourselves */
5190 if (res == GST_RTSP_ERROR)
5191 gst_rtsp_message_unset (response);
5197 static GstRTSPResult
5198 gst_rtspsrc_send_cb (GstRTSPExtension * ext, GstRTSPMessage * request,
5199 GstRTSPMessage * response, GstRTSPSrc * src)
5201 return gst_rtspsrc_send (src, src->conninfo.connection, request, response,
5206 /* parse the response and collect all the supported methods. We need this
5207 * information so that we don't try to send an unsupported request to the
5211 gst_rtspsrc_parse_methods (GstRTSPSrc * src, GstRTSPMessage * response)
5213 GstRTSPHeaderField field;
5217 /* reset supported methods */
5220 /* Try Allow Header first */
5221 field = GST_RTSP_HDR_ALLOW;
5224 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5225 if (indx == 0 && !respoptions) {
5226 /* if no Allow header was found then try the Public header... */
5227 field = GST_RTSP_HDR_PUBLIC;
5228 gst_rtsp_message_get_header (response, field, &respoptions, indx);
5233 src->methods |= gst_rtsp_options_from_text (respoptions);
5238 if (src->methods == 0) {
5239 /* neither Allow nor Public are required, assume the server supports
5240 * at least DESCRIBE, SETUP, we always assume it supports PLAY as
5242 GST_DEBUG_OBJECT (src, "could not get OPTIONS");
5243 src->methods = GST_RTSP_DESCRIBE | GST_RTSP_SETUP;
5245 /* always assume PLAY, FIXME, extensions should be able to override
5247 src->methods |= GST_RTSP_PLAY;
5248 /* also assume it will support Range */
5249 src->seekable = TRUE;
5251 /* we need describe and setup */
5252 if (!(src->methods & GST_RTSP_DESCRIBE))
5254 if (!(src->methods & GST_RTSP_SETUP))
5262 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5263 ("Server does not support DESCRIBE."));
5268 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ, (NULL),
5269 ("Server does not support SETUP."));
5274 /* masks to be kept in sync with the hardcoded protocol order of preference
5276 static guint protocol_masks[] = {
5277 GST_RTSP_LOWER_TRANS_UDP,
5278 GST_RTSP_LOWER_TRANS_UDP_MCAST,
5279 GST_RTSP_LOWER_TRANS_TCP,
5283 static GstRTSPResult
5284 gst_rtspsrc_create_transports_string (GstRTSPSrc * src,
5285 GstRTSPLowerTrans protocols, gchar ** transports)
5289 gboolean add_udp_str;
5294 gst_rtsp_ext_list_get_transports (src->extensions, protocols, transports);
5299 GST_DEBUG_OBJECT (src, "got transports %s", GST_STR_NULL (*transports));
5301 /* extension listed transports, use those */
5302 if (*transports != NULL)
5305 /* it's the default */
5306 add_udp_str = FALSE;
5308 /* the default RTSP transports */
5309 result = g_string_new ("");
5310 if (protocols & GST_RTSP_LOWER_TRANS_UDP) {
5311 GST_DEBUG_OBJECT (src, "adding UDP unicast");
5313 g_string_append (result, "RTP/AVP");
5315 g_string_append (result, "/UDP");
5316 g_string_append (result, ";unicast;client_port=%%u1-%%u2");
5317 } else if (protocols & GST_RTSP_LOWER_TRANS_UDP_MCAST) {
5318 GST_DEBUG_OBJECT (src, "adding UDP multicast");
5320 /* we don't have to allocate any UDP ports yet, if the selected transport
5321 * turns out to be multicast we can create them and join the multicast
5322 * group indicated in the transport reply */
5323 if (result->len > 0)
5324 g_string_append (result, ",");
5325 g_string_append (result, "RTP/AVP");
5327 g_string_append (result, "/UDP");
5328 g_string_append (result, ";multicast");
5329 if (src->next_port_num != 0) {
5330 if (src->client_port_range.max > 0 &&
5331 src->next_port_num >= src->client_port_range.max)
5334 g_string_append_printf (result, ";client_port=%d-%d",
5335 src->next_port_num, src->next_port_num + 1);
5337 } else if (protocols & GST_RTSP_LOWER_TRANS_TCP) {
5338 GST_DEBUG_OBJECT (src, "adding TCP");
5340 if (result->len > 0)
5341 g_string_append (result, ",");
5342 g_string_append (result, "RTP/AVP/TCP;unicast;interleaved=%%i1-%%i2");
5344 *transports = g_string_free (result, FALSE);
5346 GST_DEBUG_OBJECT (src, "prepared transports %s", GST_STR_NULL (*transports));
5353 GST_ERROR ("extension gave error %d", res);
5358 GST_ERROR ("no more ports available");
5359 return GST_RTSP_ERROR;
5363 static GstRTSPResult
5364 gst_rtspsrc_prepare_transports (GstRTSPStream * stream, gchar ** transports,
5365 gint orig_rtpport, gint orig_rtcpport)
5368 gint nr_udp, nr_int;
5370 gint rtpport = 0, rtcpport = 0;
5373 src = stream->parent;
5375 /* find number of placeholders first */
5376 if (strstr (*transports, "%%i2"))
5378 else if (strstr (*transports, "%%i1"))
5383 if (strstr (*transports, "%%u2"))
5385 else if (strstr (*transports, "%%u1"))
5390 if (nr_udp == 0 && nr_int == 0)
5394 if (!orig_rtpport || !orig_rtcpport) {
5395 if (!gst_rtspsrc_alloc_udp_ports (stream, &rtpport, &rtcpport))
5398 rtpport = orig_rtpport;
5399 rtcpport = orig_rtcpport;
5403 str = g_string_new ("");
5405 while ((next = strstr (p, "%%"))) {
5406 g_string_append_len (str, p, next - p);
5407 if (next[2] == 'u') {
5409 g_string_append_printf (str, "%d", rtpport);
5410 else if (next[3] == '2')
5411 g_string_append_printf (str, "%d", rtcpport);
5413 if (next[2] == 'i') {
5415 g_string_append_printf (str, "%d", src->free_channel);
5416 else if (next[3] == '2')
5417 g_string_append_printf (str, "%d", src->free_channel + 1);
5422 /* append final part */
5423 g_string_append (str, p);
5425 g_free (*transports);
5426 *transports = g_string_free (str, FALSE);
5434 GST_ERROR ("failed to allocate udp ports");
5435 return GST_RTSP_ERROR;
5440 gst_rtspsrc_stream_is_real_media (GstRTSPStream * stream)
5442 gboolean res = FALSE;
5446 const gchar *enc = NULL;
5448 s = gst_caps_get_structure (stream->caps, 0);
5449 if ((enc = gst_structure_get_string (s, "encoding-name"))) {
5450 res = (strstr (enc, "-REAL") != NULL);
5456 /* Perform the SETUP request for all the streams.
5458 * We ask the server for a specific transport, which initially includes all the
5459 * ones we can support (UDP/TCP/MULTICAST). For the UDP transport we allocate
5460 * two local UDP ports that we send to the server.
5462 * Once the server replied with a transport, we configure the other streams
5463 * with the same transport.
5465 * This function will also configure the stream for the selected transport,
5466 * which basically means creating the pipeline.
5468 static GstRTSPResult
5469 gst_rtspsrc_setup_streams (GstRTSPSrc * src, gboolean async)
5472 GstRTSPResult res = GST_RTSP_ERROR;
5473 GstRTSPMessage request = { 0 };
5474 GstRTSPMessage response = { 0 };
5475 GstRTSPStream *stream = NULL;
5476 GstRTSPLowerTrans protocols;
5477 GstRTSPStatusCode code;
5478 gboolean unsupported_real = FALSE;
5479 gint rtpport, rtcpport;
5483 if (src->conninfo.connection) {
5484 url = gst_rtsp_connection_get_url (src->conninfo.connection);
5485 /* we initially allow all configured lower transports. based on the URL
5486 * transports and the replies from the server we narrow them down. */
5487 protocols = url->transports & src->cur_protocols;
5490 protocols = src->cur_protocols;
5496 /* reset some state */
5497 src->free_channel = 0;
5498 src->interleaved = FALSE;
5499 src->need_activate = FALSE;
5500 /* keep track of next port number, 0 is random */
5501 src->next_port_num = src->client_port_range.min;
5502 rtpport = rtcpport = 0;
5504 if (G_UNLIKELY (src->streams == NULL))
5507 for (walk = src->streams; walk; walk = g_list_next (walk)) {
5508 GstRTSPConnection *conn;
5514 stream = (GstRTSPStream *) walk->data;
5516 /* see if we need to configure this stream */
5517 if (!gst_rtsp_ext_list_configure_stream (src->extensions, stream->caps)) {
5518 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by extension",
5520 stream->disabled = TRUE;
5524 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_SELECT_STREAM], 0,
5525 stream->id, stream->caps, &selected);
5527 GST_DEBUG_OBJECT (src, "skipping stream %p, disabled by signal", stream);
5528 stream->disabled = TRUE;
5531 stream->disabled = FALSE;
5533 /* merge/overwrite global caps */
5538 s = gst_caps_get_structure (stream->caps, 0);
5540 num = gst_structure_n_fields (src->props);
5541 for (j = 0; j < num; j++) {
5545 name = gst_structure_nth_field_name (src->props, j);
5546 val = gst_structure_get_value (src->props, name);
5547 gst_structure_set_value (s, name, val);
5549 GST_DEBUG_OBJECT (src, "copied %s", name);
5553 /* skip setup if we have no URL for it */
5554 if (stream->conninfo.location == NULL) {
5555 GST_DEBUG_OBJECT (src, "skipping stream %p, no setup", stream);
5559 if (src->conninfo.connection == NULL) {
5560 if (!gst_rtsp_conninfo_connect (src, &stream->conninfo, async)) {
5561 GST_DEBUG_OBJECT (src, "skipping stream %p, failed to connect", stream);
5564 conn = stream->conninfo.connection;
5566 conn = src->conninfo.connection;
5568 GST_DEBUG_OBJECT (src, "doing setup of stream %p with %s", stream,
5569 stream->conninfo.location);
5571 /* if we have a multicast connection, only suggest multicast from now on */
5572 if (stream->is_multicast)
5573 protocols &= GST_RTSP_LOWER_TRANS_UDP_MCAST;
5576 /* first selectable protocol */
5577 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5579 if (!protocol_masks[mask])
5583 GST_DEBUG_OBJECT (src, "protocols = 0x%x, protocol mask = 0x%x", protocols,
5584 protocol_masks[mask]);
5585 /* create a string with first transport in line */
5587 res = gst_rtspsrc_create_transports_string (src,
5588 protocols & protocol_masks[mask], &transports);
5589 if (res < 0 || transports == NULL)
5590 goto setup_transport_failed;
5592 if (strlen (transports) == 0) {
5593 g_free (transports);
5594 GST_DEBUG_OBJECT (src, "no transports found");
5599 GST_DEBUG_OBJECT (src, "replace ports in %s", GST_STR_NULL (transports));
5601 /* replace placeholders with real values, this function will optionally
5602 * allocate UDP ports and other info needed to execute the setup request */
5603 res = gst_rtspsrc_prepare_transports (stream, &transports,
5604 retry > 0 ? rtpport : 0, retry > 0 ? rtcpport : 0);
5606 g_free (transports);
5607 goto setup_transport_failed;
5610 GST_DEBUG_OBJECT (src, "transport is now %s", GST_STR_NULL (transports));
5612 /* create SETUP request */
5614 gst_rtsp_message_init_request (&request, GST_RTSP_SETUP,
5615 stream->conninfo.location);
5617 g_free (transports);
5618 goto create_request_failed;
5621 /* select transport */
5622 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_TRANSPORT, transports);
5624 /* if the user wants a non default RTP packet size we add the blocksize
5626 if (src->rtp_blocksize > 0) {
5627 hval = g_strdup_printf ("%d", src->rtp_blocksize);
5628 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_BLOCKSIZE, hval);
5632 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("SETUP stream %d",
5635 /* handle the code ourselves */
5636 if ((res = gst_rtspsrc_send (src, conn, &request, &response, &code) < 0))
5640 case GST_RTSP_STS_OK:
5642 case GST_RTSP_STS_UNSUPPORTED_TRANSPORT:
5643 gst_rtsp_message_unset (&request);
5644 gst_rtsp_message_unset (&response);
5645 /* cleanup of leftover transport */
5646 gst_rtspsrc_stream_free_udp (stream);
5647 /* MS WMServer RTSP MUST use same UDP pair in all SETUP requests;
5648 * we might be in this case */
5649 if (stream->container && rtpport && rtcpport && !retry) {
5650 GST_DEBUG_OBJECT (src, "retrying with original port pair %u-%u",
5655 /* this transport did not go down well, but we may have others to try
5656 * that we did not send yet, try those and only give up then
5657 * but not without checking for lost cause/extension so we can
5658 * post a nicer/more useful error message later */
5659 if (!unsupported_real)
5660 unsupported_real = gst_rtspsrc_stream_is_real_media (stream);
5661 /* select next available protocol, give up on this stream if none */
5663 while (protocol_masks[mask] && !(protocols & protocol_masks[mask]))
5665 if (!protocol_masks[mask] || unsupported_real)
5670 /* cleanup of leftover transport and move to the next stream */
5671 gst_rtspsrc_stream_free_udp (stream);
5672 goto response_error;
5675 /* parse response transport */
5677 gchar *resptrans = NULL;
5678 GstRTSPTransport transport = { 0 };
5680 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_TRANSPORT,
5683 gst_rtspsrc_stream_free_udp (stream);
5687 /* parse transport, go to next stream on parse error */
5688 if (gst_rtsp_transport_parse (resptrans, &transport) != GST_RTSP_OK) {
5689 GST_WARNING_OBJECT (src, "failed to parse transport %s", resptrans);
5693 /* update allowed transports for other streams. once the transport of
5694 * one stream has been determined, we make sure that all other streams
5695 * are configured in the same way */
5696 switch (transport.lower_transport) {
5697 case GST_RTSP_LOWER_TRANS_TCP:
5698 GST_DEBUG_OBJECT (src, "stream %p as TCP interleaved", stream);
5699 protocols = GST_RTSP_LOWER_TRANS_TCP;
5700 src->interleaved = TRUE;
5701 /* update free channels */
5703 MAX (transport.interleaved.min, src->free_channel);
5705 MAX (transport.interleaved.max, src->free_channel);
5706 src->free_channel++;
5708 case GST_RTSP_LOWER_TRANS_UDP_MCAST:
5709 /* only allow multicast for other streams */
5710 GST_DEBUG_OBJECT (src, "stream %p as UDP multicast", stream);
5711 protocols = GST_RTSP_LOWER_TRANS_UDP_MCAST;
5712 /* if the server selected our ports, increment our counters so that
5713 * we select a new port later */
5714 if (src->next_port_num == transport.port.min &&
5715 src->next_port_num + 1 == transport.port.max) {
5716 src->next_port_num += 2;
5719 case GST_RTSP_LOWER_TRANS_UDP:
5720 /* only allow unicast for other streams */
5721 GST_DEBUG_OBJECT (src, "stream %p as UDP unicast", stream);
5722 protocols = GST_RTSP_LOWER_TRANS_UDP;
5725 GST_DEBUG_OBJECT (src, "stream %p unknown transport %d", stream,
5726 transport.lower_transport);
5730 if (!stream->container || (!src->interleaved && !retry)) {
5731 /* now configure the stream with the selected transport */
5732 if (!gst_rtspsrc_stream_configure_transport (stream, &transport)) {
5733 GST_DEBUG_OBJECT (src,
5734 "could not configure stream %p transport, skipping stream",
5737 } else if (stream->udpsrc[0] && stream->udpsrc[1]) {
5738 /* retain the first allocated UDP port pair */
5739 g_object_get (G_OBJECT (stream->udpsrc[0]), "port", &rtpport, NULL);
5740 g_object_get (G_OBJECT (stream->udpsrc[1]), "port", &rtcpport, NULL);
5743 /* we need to activate at least one streams when we detect activity */
5744 src->need_activate = TRUE;
5746 /* clean up our transport struct */
5747 gst_rtsp_transport_init (&transport);
5748 /* clean up used RTSP messages */
5749 gst_rtsp_message_unset (&request);
5750 gst_rtsp_message_unset (&response);
5754 /* store the transport protocol that was configured */
5755 src->cur_protocols = protocols;
5757 gst_rtsp_ext_list_stream_select (src->extensions, url);
5759 /* if there is nothing to activate, error out */
5760 if (!src->need_activate)
5761 goto nothing_to_activate;
5768 /* no transport possible, post an error and stop */
5769 GST_ELEMENT_ERROR (src, RESOURCE, READ, (NULL),
5770 ("Could not connect to server, no protocols left"));
5771 return GST_RTSP_ERROR;
5775 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5776 ("SDP contains no streams"));
5777 return GST_RTSP_ERROR;
5779 create_request_failed:
5781 gchar *str = gst_rtsp_strresult (res);
5783 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
5784 ("Could not create request. (%s)", str));
5788 setup_transport_failed:
5790 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5791 ("Could not setup transport."));
5792 res = GST_RTSP_ERROR;
5797 const gchar *str = gst_rtsp_status_as_text (code);
5799 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5800 ("Error (%d): %s", code, GST_STR_NULL (str)));
5801 res = GST_RTSP_ERROR;
5806 gchar *str = gst_rtsp_strresult (res);
5808 if (res != GST_RTSP_EINTR) {
5809 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
5810 ("Could not send message. (%s)", str));
5812 GST_WARNING_OBJECT (src, "send interrupted");
5819 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
5820 ("Server did not select transport."));
5821 res = GST_RTSP_ERROR;
5824 nothing_to_activate:
5826 /* none of the available error codes is really right .. */
5827 if (unsupported_real) {
5828 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5829 (_("No supported stream was found. You might need to install a "
5830 "GStreamer RTSP extension plugin for Real media streams.")),
5833 GST_ELEMENT_ERROR (src, STREAM, CODEC_NOT_FOUND,
5834 (_("No supported stream was found. You might need to allow "
5835 "more transport protocols or may otherwise be missing "
5836 "the right GStreamer RTSP extension plugin.")), (NULL));
5838 return GST_RTSP_ERROR;
5842 gst_rtsp_message_unset (&request);
5843 gst_rtsp_message_unset (&response);
5849 gst_rtspsrc_parse_range (GstRTSPSrc * src, const gchar * range,
5850 GstSegment * segment)
5853 GstRTSPTimeRange *therange;
5856 gst_rtsp_range_free (src->range);
5858 if (gst_rtsp_range_parse (range, &therange) == GST_RTSP_OK) {
5859 GST_DEBUG_OBJECT (src, "parsed range %s", range);
5860 src->range = therange;
5862 GST_DEBUG_OBJECT (src, "failed to parse range %s", range);
5864 gst_segment_init (segment, GST_FORMAT_TIME);
5868 GST_DEBUG_OBJECT (src, "range: type %d, min %f - type %d, max %f ",
5869 therange->min.type, therange->min.seconds, therange->max.type,
5870 therange->max.seconds);
5872 if (therange->min.type == GST_RTSP_TIME_NOW)
5874 else if (therange->min.type == GST_RTSP_TIME_END)
5877 seconds = therange->min.seconds * GST_SECOND;
5879 GST_DEBUG_OBJECT (src, "range: min %" GST_TIME_FORMAT,
5880 GST_TIME_ARGS (seconds));
5882 /* we need to start playback without clipping from the position reported by
5884 segment->start = seconds;
5885 segment->position = seconds;
5887 if (therange->max.type == GST_RTSP_TIME_NOW)
5889 else if (therange->max.type == GST_RTSP_TIME_END)
5892 seconds = therange->max.seconds * GST_SECOND;
5894 GST_DEBUG_OBJECT (src, "range: max %" GST_TIME_FORMAT,
5895 GST_TIME_ARGS (seconds));
5897 /* live (WMS) server might send overflowed large max as its idea of infinity,
5898 * compensate to prevent problems later on */
5899 if (seconds != -1 && seconds < 0) {
5901 GST_DEBUG_OBJECT (src, "insane range, set to NONE");
5904 /* live (WMS) might send min == max, which is not worth recording */
5905 if (segment->duration == -1 && seconds == segment->start)
5908 /* don't change duration with unknown value, we might have a valid value
5909 * there that we want to keep. */
5911 segment->duration = seconds;
5916 /* Parse clock profived by the server with following syntax:
5918 * "GstNetTimeProvider <wrapped-clock> <server-IP:port> <clock-time>"
5921 gst_rtspsrc_parse_gst_clock (GstRTSPSrc * src, const gchar * gstclock)
5923 gboolean res = FALSE;
5925 if (g_str_has_prefix (gstclock, "GstNetTimeProvider ")) {
5926 gchar **fields = NULL, **parts = NULL;
5927 gchar *remote_ip, *str;
5929 GstClockTime base_time;
5932 fields = g_strsplit (gstclock, " ", 0);
5934 /* wrapped clock, not very interesting for now */
5935 if (fields[1] == NULL)
5938 /* remote IP address and port */
5939 if ((str = fields[2]) == NULL)
5942 parts = g_strsplit (str, ":", 0);
5944 if ((remote_ip = parts[0]) == NULL)
5947 if ((str = parts[1]) == NULL)
5955 if ((str = fields[3]) == NULL)
5958 base_time = g_ascii_strtoull (str, NULL, 10);
5961 gst_net_client_clock_new ((gchar *) "GstRTSPClock", remote_ip, port,
5964 if (src->provided_clock)
5965 gst_object_unref (src->provided_clock);
5966 src->provided_clock = netclock;
5968 gst_element_post_message (GST_ELEMENT_CAST (src),
5969 gst_message_new_clock_provide (GST_OBJECT_CAST (src),
5970 src->provided_clock, TRUE));
5974 g_strfreev (fields);
5980 /* must be called with the RTSP state lock */
5981 static GstRTSPResult
5982 gst_rtspsrc_open_from_sdp (GstRTSPSrc * src, GstSDPMessage * sdp,
5988 /* prepare global stream caps properties */
5990 gst_structure_remove_all_fields (src->props);
5992 src->props = gst_structure_new_empty ("RTSPProperties");
5995 gst_sdp_message_dump (sdp);
5997 gst_rtsp_ext_list_parse_sdp (src->extensions, sdp, src->props);
5999 /* let the app inspect and change the SDP */
6000 g_signal_emit (src, gst_rtspsrc_signals[SIGNAL_ON_SDP], 0, sdp);
6002 gst_segment_init (&src->segment, GST_FORMAT_TIME);
6004 /* parse range for duration reporting. */
6009 range = gst_sdp_message_get_attribute_val_n (sdp, "range", i);
6013 /* keep track of the range and configure it in the segment */
6014 if (gst_rtspsrc_parse_range (src, range, &src->segment))
6018 /* parse clock information. This is GStreamer specific, a server can tell the
6019 * client what clock it is using and wrap that in a network clock. The
6020 * advantage of that is that we can slave to it. */
6022 const gchar *gstclock;
6025 gstclock = gst_sdp_message_get_attribute_val_n (sdp, "x-gst-clock", i);
6026 if (gstclock == NULL)
6029 /* parse the clock and expose it in the provide_clock method */
6030 if (gst_rtspsrc_parse_gst_clock (src, gstclock))
6034 /* try to find a global control attribute. Note that a '*' means that we should
6035 * do aggregate control with the current url (so we don't do anything and
6036 * leave the current connection as is) */
6038 const gchar *control;
6041 control = gst_sdp_message_get_attribute_val_n (sdp, "control", i);
6042 if (control == NULL)
6045 /* only take fully qualified urls */
6046 if (g_str_has_prefix (control, "rtsp://"))
6050 g_free (src->conninfo.location);
6051 src->conninfo.location = g_strdup (control);
6052 /* make a connection for this, if there was a connection already, nothing
6054 if (gst_rtsp_conninfo_connect (src, &src->conninfo, async) < 0) {
6055 GST_ERROR_OBJECT (src, "could not connect");
6058 /* we need to keep the control url separate from the connection url because
6059 * the rules for constructing the media control url need it */
6060 g_free (src->control);
6061 src->control = g_strdup (control);
6064 /* create streams */
6065 n_streams = gst_sdp_message_medias_len (sdp);
6066 for (i = 0; i < n_streams; i++) {
6067 gst_rtspsrc_create_stream (src, sdp, i);
6070 src->state = GST_RTSP_STATE_INIT;
6073 if ((res = gst_rtspsrc_setup_streams (src, async)) < 0)
6076 /* reset our state */
6077 src->need_range = TRUE;
6080 src->state = GST_RTSP_STATE_READY;
6087 GST_ERROR_OBJECT (src, "setup failed");
6088 gst_rtspsrc_cleanup (src);
6093 static GstRTSPResult
6094 gst_rtspsrc_retrieve_sdp (GstRTSPSrc * src, GstSDPMessage ** sdp,
6098 GstRTSPMessage request = { 0 };
6099 GstRTSPMessage response = { 0 };
6102 gchar *respcont = NULL;
6105 src->need_redirect = FALSE;
6107 /* can't continue without a valid url */
6108 if (G_UNLIKELY (src->conninfo.url == NULL)) {
6109 res = GST_RTSP_EINVAL;
6112 src->tried_url_auth = FALSE;
6114 if ((res = gst_rtsp_conninfo_connect (src, &src->conninfo, async)) < 0)
6115 goto connect_failed;
6117 /* create OPTIONS */
6118 GST_DEBUG_OBJECT (src, "create options...");
6120 gst_rtsp_message_init_request (&request, GST_RTSP_OPTIONS,
6121 src->conninfo.url_str);
6123 goto create_request_failed;
6126 GST_DEBUG_OBJECT (src, "send options...");
6129 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving server options"));
6132 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6137 if (!gst_rtspsrc_parse_methods (src, &response))
6140 /* create DESCRIBE */
6141 GST_DEBUG_OBJECT (src, "create describe...");
6143 gst_rtsp_message_init_request (&request, GST_RTSP_DESCRIBE,
6144 src->conninfo.url_str);
6146 goto create_request_failed;
6148 /* we only accept SDP for now */
6149 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_ACCEPT,
6153 GST_DEBUG_OBJECT (src, "send describe...");
6156 GST_ELEMENT_PROGRESS (src, CONTINUE, "open", ("Retrieving media info"));
6159 gst_rtspsrc_send (src, src->conninfo.connection, &request, &response,
6163 /* we only perform redirect for the describe, currently */
6164 if (src->need_redirect) {
6165 /* close connection, we don't have to send a TEARDOWN yet, ignore the
6167 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6169 gst_rtsp_message_unset (&request);
6170 gst_rtsp_message_unset (&response);
6176 /* it could be that the DESCRIBE method was not implemented */
6177 if (!src->methods & GST_RTSP_DESCRIBE)
6180 /* check if reply is SDP */
6181 gst_rtsp_message_get_header (&response, GST_RTSP_HDR_CONTENT_TYPE, &respcont,
6183 /* could not be set but since the request returned OK, we assume it
6184 * was SDP, else check it. */
6186 if (!g_ascii_strcasecmp (respcont, "application/sdp") == 0)
6187 goto wrong_content_type;
6190 /* get message body and parse as SDP */
6191 gst_rtsp_message_get_body (&response, &data, &size);
6192 if (data == NULL || size == 0)
6195 GST_DEBUG_OBJECT (src, "parse SDP...");
6196 gst_sdp_message_new (sdp);
6197 gst_sdp_message_parse_buffer (data, size, *sdp);
6199 /* clean up any messages */
6200 gst_rtsp_message_unset (&request);
6201 gst_rtsp_message_unset (&response);
6208 GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, (NULL),
6209 ("No valid RTSP URL was provided"));
6214 gchar *str = gst_rtsp_strresult (res);
6216 if (res != GST_RTSP_EINTR) {
6217 GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
6218 ("Failed to connect. (%s)", str));
6220 GST_WARNING_OBJECT (src, "connect interrupted");
6225 create_request_failed:
6227 gchar *str = gst_rtsp_strresult (res);
6229 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6230 ("Could not create request. (%s)", str));
6236 /* Don't post a message - the rtsp_send method will have
6237 * taken care of it because we passed NULL for the response code */
6242 /* error was posted */
6243 res = GST_RTSP_ERROR;
6248 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6249 ("Server does not support SDP, got %s.", respcont));
6250 res = GST_RTSP_ERROR;
6255 GST_ELEMENT_ERROR (src, RESOURCE, SETTINGS, (NULL),
6256 ("Server can not provide an SDP."));
6257 res = GST_RTSP_ERROR;
6262 if (src->conninfo.connection) {
6263 GST_DEBUG_OBJECT (src, "free connection");
6264 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6266 gst_rtsp_message_unset (&request);
6267 gst_rtsp_message_unset (&response);
6272 static GstRTSPResult
6273 gst_rtspsrc_open (GstRTSPSrc * src, gboolean async)
6278 GST_RTSP_SETUP | GST_RTSP_PLAY | GST_RTSP_PAUSE | GST_RTSP_TEARDOWN;
6280 if (src->sdp == NULL) {
6281 if ((ret = gst_rtspsrc_retrieve_sdp (src, &src->sdp, async)) < 0)
6285 if ((ret = gst_rtspsrc_open_from_sdp (src, src->sdp, async)) < 0)
6290 gst_rtspsrc_loop_end_cmd (src, CMD_OPEN, ret);
6297 GST_WARNING_OBJECT (src, "can't get sdp");
6298 src->open_error = TRUE;
6303 GST_WARNING_OBJECT (src, "can't setup streaming from sdp");
6304 src->open_error = TRUE;
6309 static GstRTSPResult
6310 gst_rtspsrc_close (GstRTSPSrc * src, gboolean async, gboolean only_close)
6312 GstRTSPMessage request = { 0 };
6313 GstRTSPMessage response = { 0 };
6314 GstRTSPResult res = GST_RTSP_OK;
6318 GST_DEBUG_OBJECT (src, "TEARDOWN...");
6320 gst_rtspsrc_set_state (src, GST_STATE_READY);
6322 if (src->state < GST_RTSP_STATE_READY) {
6323 GST_DEBUG_OBJECT (src, "not ready, doing cleanup");
6330 /* construct a control url */
6332 control = src->control;
6334 control = src->conninfo.url_str;
6336 if (!(src->methods & (GST_RTSP_PLAY | GST_RTSP_TEARDOWN)))
6339 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6340 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6342 GstRTSPConnInfo *info;
6344 /* try aggregate control first but do non-aggregate control otherwise */
6346 setup_url = control;
6347 else if ((setup_url = stream->conninfo.location) == NULL)
6350 if (src->conninfo.connection) {
6351 info = &src->conninfo;
6352 } else if (stream->conninfo.connection) {
6353 info = &stream->conninfo;
6357 if (!info->connected)
6362 gst_rtsp_message_init_request (&request, GST_RTSP_TEARDOWN, setup_url);
6364 goto create_request_failed;
6367 GST_ELEMENT_PROGRESS (src, CONTINUE, "close", ("Closing stream"));
6370 gst_rtspsrc_send (src, info->connection, &request, &response,
6374 /* FIXME, parse result? */
6375 gst_rtsp_message_unset (&request);
6376 gst_rtsp_message_unset (&response);
6379 /* early exit when we did aggregate control */
6385 /* close connections */
6386 GST_DEBUG_OBJECT (src, "closing connection...");
6387 gst_rtsp_conninfo_close (src, &src->conninfo, TRUE);
6388 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6389 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6390 gst_rtsp_conninfo_close (src, &stream->conninfo, TRUE);
6394 gst_rtspsrc_cleanup (src);
6396 src->state = GST_RTSP_STATE_INVALID;
6399 gst_rtspsrc_loop_end_cmd (src, CMD_CLOSE, res);
6404 create_request_failed:
6406 gchar *str = gst_rtsp_strresult (res);
6408 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6409 ("Could not create request. (%s)", str));
6415 gchar *str = gst_rtsp_strresult (res);
6417 gst_rtsp_message_unset (&request);
6418 if (res != GST_RTSP_EINTR) {
6419 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6420 ("Could not send message. (%s)", str));
6422 GST_WARNING_OBJECT (src, "TEARDOWN interrupted");
6429 GST_DEBUG_OBJECT (src,
6430 "TEARDOWN and PLAY not supported, can't do TEARDOWN");
6435 /* RTP-Info is of the format:
6437 * url=<URL>;[seq=<seqbase>;rtptime=<timebase>] [, url=...]
6439 * rtptime corresponds to the timestamp for the NPT time given in the header
6440 * seqbase corresponds to the next sequence number we received. This number
6441 * indicates the first seqnum after the seek and should be used to discard
6442 * packets that are from before the seek.
6445 gst_rtspsrc_parse_rtpinfo (GstRTSPSrc * src, gchar * rtpinfo)
6450 GST_DEBUG_OBJECT (src, "parsing RTP-Info %s", rtpinfo);
6452 infos = g_strsplit (rtpinfo, ",", 0);
6453 for (i = 0; infos[i]; i++) {
6455 GstRTSPStream *stream;
6459 GST_DEBUG_OBJECT (src, "parsing info %s", infos[i]);
6461 /* init values, types of seqbase and timebase are bigger than needed so we
6462 * can store -1 as uninitialized values */
6467 /* parse url, find stream for url.
6468 * parse seq and rtptime. The seq number should be configured in the rtp
6469 * depayloader or session manager to detect gaps. Same for the rtptime, it
6470 * should be used to create an initial time newsegment. */
6471 fields = g_strsplit (infos[i], ";", 0);
6472 for (j = 0; fields[j]; j++) {
6473 GST_DEBUG_OBJECT (src, "parsing field %s", fields[j]);
6474 /* remove leading whitespace */
6475 fields[j] = g_strchug (fields[j]);
6476 if (g_str_has_prefix (fields[j], "url=")) {
6477 /* get the url and the stream */
6479 find_stream (src, (fields[j] + 4), (gpointer) find_stream_by_setup);
6480 } else if (g_str_has_prefix (fields[j], "seq=")) {
6481 seqbase = atoi (fields[j] + 4);
6482 } else if (g_str_has_prefix (fields[j], "rtptime=")) {
6483 timebase = g_ascii_strtoll (fields[j] + 8, NULL, 10);
6486 g_strfreev (fields);
6487 /* now we need to store the values for the caps of the stream */
6488 if (stream != NULL) {
6489 GST_DEBUG_OBJECT (src,
6490 "found stream %p, setting: seqbase %d, timebase %" G_GINT64_FORMAT,
6491 stream, seqbase, timebase);
6493 /* we have a stream, configure detected params */
6494 stream->seqbase = seqbase;
6495 stream->timebase = timebase;
6504 gst_rtspsrc_handle_rtcp_interval (GstRTSPSrc * src, gchar * rtcp)
6509 interval = strtoul (rtcp, NULL, 10);
6510 GST_DEBUG_OBJECT (src, "rtcp interval: %" G_GUINT64_FORMAT " ms", interval);
6515 interval *= GST_MSECOND;
6517 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6518 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6520 /* already (optionally) retrieved this when configuring manager */
6521 if (stream->session) {
6522 GObject *rtpsession = stream->session;
6524 GST_DEBUG_OBJECT (src, "configure rtcp interval in session %p",
6526 g_object_set (rtpsession, "rtcp-min-interval", interval, NULL);
6530 /* now it happens that (Xenon) server sending this may also provide bogus
6531 * RTCP SR sync data (i.e. with quite some jitter), so never mind those
6532 * and just use RTP-Info to sync */
6534 GObjectClass *klass;
6536 klass = G_OBJECT_GET_CLASS (G_OBJECT (src->manager));
6537 if (g_object_class_find_property (klass, "rtcp-sync")) {
6538 GST_DEBUG_OBJECT (src, "configuring rtp sync method");
6539 g_object_set (src->manager, "rtcp-sync", RTCP_SYNC_RTP, NULL);
6545 gst_rtspsrc_get_float (const gchar * dstr)
6547 gchar s[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6549 /* canonicalise floating point string so we can handle float strings
6550 * in the form "24.930" or "24,930" irrespective of the current locale */
6551 g_strlcpy (s, dstr, sizeof (s));
6552 g_strdelimit (s, ",", '.');
6553 return g_ascii_strtod (s, NULL);
6557 gen_range_header (GstRTSPSrc * src, GstSegment * segment)
6559 gchar val_str[G_ASCII_DTOSTR_BUF_SIZE] = { 0, };
6561 if (src->range && src->range->min.type == GST_RTSP_TIME_NOW) {
6562 g_strlcpy (val_str, "now", sizeof (val_str));
6564 if (segment->position == 0) {
6565 g_strlcpy (val_str, "0", sizeof (val_str));
6567 g_ascii_dtostr (val_str, sizeof (val_str),
6568 ((gdouble) segment->position) / GST_SECOND);
6571 return g_strdup_printf ("npt=%s-", val_str);
6575 clear_rtp_base (GstRTSPSrc * src, GstRTSPStream * stream)
6577 stream->timebase = -1;
6578 stream->seqbase = -1;
6582 stream->caps = gst_caps_make_writable (stream->caps);
6583 s = gst_caps_get_structure (stream->caps, 0);
6584 gst_structure_remove_fields (s, "clock-base", "seqnum-base", NULL);
6588 static GstRTSPResult
6589 gst_rtspsrc_ensure_open (GstRTSPSrc * src, gboolean async)
6591 GstRTSPResult res = GST_RTSP_OK;
6593 if (src->state < GST_RTSP_STATE_READY) {
6594 res = GST_RTSP_ERROR;
6595 if (src->open_error) {
6596 GST_DEBUG_OBJECT (src, "the stream was in error");
6600 gst_rtspsrc_loop_start_cmd (src, CMD_OPEN);
6602 if ((res = gst_rtspsrc_open (src, async)) < 0) {
6603 GST_DEBUG_OBJECT (src, "failed to open stream");
6612 static GstRTSPResult
6613 gst_rtspsrc_play (GstRTSPSrc * src, GstSegment * segment, gboolean async)
6615 GstRTSPMessage request = { 0 };
6616 GstRTSPMessage response = { 0 };
6617 GstRTSPResult res = GST_RTSP_OK;
6623 GST_DEBUG_OBJECT (src, "PLAY...");
6625 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6628 if (!(src->methods & GST_RTSP_PLAY))
6631 if (src->state == GST_RTSP_STATE_PLAYING)
6634 if (!src->conninfo.connection || !src->conninfo.connected)
6637 /* send some dummy packets before we activate the receive in the
6639 gst_rtspsrc_send_dummy_packets (src);
6641 /* require new SR packets */
6643 g_signal_emit_by_name (src->manager, "reset-sync", NULL);
6645 gst_rtspsrc_set_state (src, GST_STATE_PLAYING);
6647 /* construct a control url */
6649 control = src->control;
6651 control = src->conninfo.url_str;
6653 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6654 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6656 GstRTSPConnection *conn;
6658 /* try aggregate control first but do non-aggregate control otherwise */
6660 setup_url = control;
6661 else if ((setup_url = stream->conninfo.location) == NULL)
6664 if (src->conninfo.connection) {
6665 conn = src->conninfo.connection;
6666 } else if (stream->conninfo.connection) {
6667 conn = stream->conninfo.connection;
6673 res = gst_rtsp_message_init_request (&request, GST_RTSP_PLAY, setup_url);
6675 goto create_request_failed;
6677 if (src->need_range) {
6678 hval = gen_range_header (src, segment);
6680 gst_rtsp_message_take_header (&request, GST_RTSP_HDR_RANGE, hval);
6682 /* store the newsegment event so it can be sent from the streaming thread. */
6683 if (src->start_segment)
6684 gst_event_unref (src->start_segment);
6685 src->start_segment = gst_event_new_segment (&src->segment);
6688 if (segment->rate != 1.0) {
6689 gchar hval[G_ASCII_DTOSTR_BUF_SIZE];
6691 g_ascii_dtostr (hval, sizeof (hval), segment->rate);
6693 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SCALE, hval);
6695 gst_rtsp_message_add_header (&request, GST_RTSP_HDR_SPEED, hval);
6699 GST_ELEMENT_PROGRESS (src, CONTINUE, "request", ("Sending PLAY request"));
6701 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6704 /* seek may have silently failed as it is not supported */
6705 if (!(src->methods & GST_RTSP_PLAY)) {
6706 GST_DEBUG_OBJECT (src, "PLAY Range not supported; re-enable PLAY");
6707 /* obviously it is supported as we made it here */
6708 src->methods |= GST_RTSP_PLAY;
6709 src->seekable = FALSE;
6710 /* but there is nothing to parse in the response,
6711 * so convey we have no idea and not to expect anything particular */
6712 clear_rtp_base (src, stream);
6716 /* need to do for all streams */
6717 for (run = src->streams; run; run = g_list_next (run))
6718 clear_rtp_base (src, (GstRTSPStream *) run->data);
6720 /* NOTE the above also disables npt based eos detection */
6721 /* and below forces position to 0,
6722 * which is visible feedback we lost the plot */
6723 segment->start = segment->position = src->last_pos;
6726 gst_rtsp_message_unset (&request);
6728 /* parse RTP npt field. This is the current position in the stream (Normal
6729 * Play Time) and should be put in the NEWSEGMENT position field. */
6730 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RANGE, &hval,
6732 gst_rtspsrc_parse_range (src, hval, segment);
6734 /* assume 1.0 rate now, overwrite when the SCALE or SPEED headers are present. */
6735 segment->rate = 1.0;
6737 /* parse Speed header. This is the intended playback rate of the stream
6738 * and should be put in the NEWSEGMENT rate field. */
6739 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SPEED, &hval,
6740 0) == GST_RTSP_OK) {
6741 segment->rate = gst_rtspsrc_get_float (hval);
6742 } else if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_SCALE,
6743 &hval, 0) == GST_RTSP_OK) {
6744 segment->rate = gst_rtspsrc_get_float (hval);
6747 /* parse the RTP-Info header field (if ANY) to get the base seqnum and timestamp
6748 * for the RTP packets. If this is not present, we assume all starts from 0...
6749 * This is info for the RTP session manager that we pass to it in caps. */
6751 while (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTP_INFO,
6752 &hval, hval_idx++) == GST_RTSP_OK)
6753 gst_rtspsrc_parse_rtpinfo (src, hval);
6755 /* some servers indicate RTCP parameters in PLAY response,
6756 * rather than properly in SDP */
6757 if (gst_rtsp_message_get_header (&response, GST_RTSP_HDR_RTCP_INTERVAL,
6758 &hval, 0) == GST_RTSP_OK)
6759 gst_rtspsrc_handle_rtcp_interval (src, hval);
6761 gst_rtsp_message_unset (&response);
6763 /* early exit when we did aggregate control */
6767 /* configure the caps of the streams after we parsed all headers. Only reset
6768 * the manager object when we set a new Range header (we did a seek) */
6769 gst_rtspsrc_configure_caps (src, segment, src->need_range);
6771 /* set again when needed */
6772 src->need_range = FALSE;
6774 src->running = TRUE;
6775 src->base_time = -1;
6776 src->state = GST_RTSP_STATE_PLAYING;
6779 GST_DEBUG_OBJECT (src, "mark DISCONT, we did a seek to another position");
6780 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6781 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6782 stream->discont = TRUE;
6787 gst_rtspsrc_loop_end_cmd (src, CMD_PLAY, res);
6794 GST_DEBUG_OBJECT (src, "failed to open stream");
6799 GST_DEBUG_OBJECT (src, "PLAY is not supported");
6804 GST_DEBUG_OBJECT (src, "we were already PLAYING");
6807 create_request_failed:
6809 gchar *str = gst_rtsp_strresult (res);
6811 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6812 ("Could not create request. (%s)", str));
6818 gchar *str = gst_rtsp_strresult (res);
6820 gst_rtsp_message_unset (&request);
6821 if (res != GST_RTSP_EINTR) {
6822 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6823 ("Could not send message. (%s)", str));
6825 GST_WARNING_OBJECT (src, "PLAY interrupted");
6832 static GstRTSPResult
6833 gst_rtspsrc_pause (GstRTSPSrc * src, gboolean async)
6835 GstRTSPResult res = GST_RTSP_OK;
6836 GstRTSPMessage request = { 0 };
6837 GstRTSPMessage response = { 0 };
6841 GST_DEBUG_OBJECT (src, "PAUSE...");
6843 if ((res = gst_rtspsrc_ensure_open (src, async)) < 0)
6846 if (!(src->methods & GST_RTSP_PAUSE))
6849 if (src->state == GST_RTSP_STATE_READY)
6852 if (!src->conninfo.connection || !src->conninfo.connected)
6855 /* construct a control url */
6857 control = src->control;
6859 control = src->conninfo.url_str;
6861 /* loop over the streams. We might exit the loop early when we could do an
6862 * aggregate control */
6863 for (walk = src->streams; walk; walk = g_list_next (walk)) {
6864 GstRTSPStream *stream = (GstRTSPStream *) walk->data;
6865 GstRTSPConnection *conn;
6868 /* try aggregate control first but do non-aggregate control otherwise */
6870 setup_url = control;
6871 else if ((setup_url = stream->conninfo.location) == NULL)
6874 if (src->conninfo.connection) {
6875 conn = src->conninfo.connection;
6876 } else if (stream->conninfo.connection) {
6877 conn = stream->conninfo.connection;
6883 GST_ELEMENT_PROGRESS (src, CONTINUE, "request",
6884 ("Sending PAUSE request"));
6887 gst_rtsp_message_init_request (&request, GST_RTSP_PAUSE,
6889 goto create_request_failed;
6891 if ((res = gst_rtspsrc_send (src, conn, &request, &response, NULL)) < 0)
6894 gst_rtsp_message_unset (&request);
6895 gst_rtsp_message_unset (&response);
6897 /* exit early when we did agregate control */
6902 /* change element states now */
6903 gst_rtspsrc_set_state (src, GST_STATE_PAUSED);
6906 src->state = GST_RTSP_STATE_READY;
6910 gst_rtspsrc_loop_end_cmd (src, CMD_PAUSE, res);
6917 GST_DEBUG_OBJECT (src, "failed to open stream");
6922 GST_DEBUG_OBJECT (src, "PAUSE is not supported");
6927 GST_DEBUG_OBJECT (src, "we were already PAUSED");
6930 create_request_failed:
6932 gchar *str = gst_rtsp_strresult (res);
6934 GST_ELEMENT_ERROR (src, LIBRARY, INIT, (NULL),
6935 ("Could not create request. (%s)", str));
6941 gchar *str = gst_rtsp_strresult (res);
6943 gst_rtsp_message_unset (&request);
6944 if (res != GST_RTSP_EINTR) {
6945 GST_ELEMENT_ERROR (src, RESOURCE, WRITE, (NULL),
6946 ("Could not send message. (%s)", str));
6948 GST_WARNING_OBJECT (src, "PAUSE interrupted");
6956 gst_rtspsrc_handle_message (GstBin * bin, GstMessage * message)
6958 GstRTSPSrc *rtspsrc;
6960 rtspsrc = GST_RTSPSRC (bin);
6962 switch (GST_MESSAGE_TYPE (message)) {
6963 case GST_MESSAGE_EOS:
6964 gst_message_unref (message);
6966 case GST_MESSAGE_ELEMENT:
6968 const GstStructure *s = gst_message_get_structure (message);
6970 if (gst_structure_has_name (s, "GstUDPSrcTimeout")) {
6971 gboolean ignore_timeout;
6973 GST_DEBUG_OBJECT (bin, "timeout on UDP port");
6975 GST_OBJECT_LOCK (rtspsrc);
6976 ignore_timeout = rtspsrc->ignore_timeout;
6977 rtspsrc->ignore_timeout = TRUE;
6978 GST_OBJECT_UNLOCK (rtspsrc);
6980 /* we only act on the first udp timeout message, others are irrelevant
6981 * and can be ignored. */
6982 if (!ignore_timeout)
6983 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_RECONNECT, CMD_LOOP);
6985 gst_message_unref (message);
6988 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
6991 case GST_MESSAGE_ERROR:
6994 GstRTSPStream *stream;
6997 udpsrc = GST_MESSAGE_SRC (message);
6999 GST_DEBUG_OBJECT (rtspsrc, "got error from %s",
7000 GST_ELEMENT_NAME (udpsrc));
7002 stream = find_stream (rtspsrc, udpsrc, (gpointer) find_stream_by_udpsrc);
7006 /* we ignore the RTCP udpsrc */
7007 if (stream->udpsrc[1] == GST_ELEMENT_CAST (udpsrc))
7010 /* if we get error messages from the udp sources, that's not a problem as
7011 * long as not all of them error out. We also don't really know what the
7012 * problem is, the message does not give enough detail... */
7013 ret = gst_rtspsrc_combine_flows (rtspsrc, stream, GST_FLOW_NOT_LINKED);
7014 GST_DEBUG_OBJECT (rtspsrc, "combined flows: %s", gst_flow_get_name (ret));
7015 if (ret != GST_FLOW_OK)
7019 gst_message_unref (message);
7023 /* fatal but not our message, forward */
7024 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7029 GST_BIN_CLASS (parent_class)->handle_message (bin, message);
7035 /* the thread where everything happens */
7037 gst_rtspsrc_thread (GstRTSPSrc * src)
7041 GST_OBJECT_LOCK (src);
7042 cmd = src->pending_cmd;
7043 if (cmd == CMD_RECONNECT || cmd == CMD_PLAY || cmd == CMD_PAUSE
7044 || cmd == CMD_LOOP || cmd == CMD_OPEN)
7045 src->pending_cmd = CMD_LOOP;
7047 src->pending_cmd = CMD_WAIT;
7048 GST_DEBUG_OBJECT (src, "got command %d", cmd);
7050 /* we got the message command, so ensure communication is possible again */
7051 gst_rtspsrc_connection_flush (src, FALSE);
7053 src->busy_cmd = cmd;
7054 GST_OBJECT_UNLOCK (src);
7058 gst_rtspsrc_open (src, TRUE);
7061 gst_rtspsrc_play (src, &src->segment, TRUE);
7064 gst_rtspsrc_pause (src, TRUE);
7067 gst_rtspsrc_close (src, TRUE, FALSE);
7070 gst_rtspsrc_loop (src);
7073 gst_rtspsrc_reconnect (src, FALSE);
7079 GST_OBJECT_LOCK (src);
7080 /* and go back to sleep */
7081 if (src->pending_cmd == CMD_WAIT) {
7083 gst_task_pause (src->task);
7086 src->busy_cmd = CMD_WAIT;
7087 GST_OBJECT_UNLOCK (src);
7091 gst_rtspsrc_start (GstRTSPSrc * src)
7093 GST_DEBUG_OBJECT (src, "starting");
7095 GST_OBJECT_LOCK (src);
7097 src->pending_cmd = CMD_WAIT;
7099 if (src->task == NULL) {
7100 src->task = gst_task_new ((GstTaskFunction) gst_rtspsrc_thread, src, NULL);
7101 if (src->task == NULL)
7104 gst_task_set_lock (src->task, GST_RTSP_STREAM_GET_LOCK (src));
7106 GST_OBJECT_UNLOCK (src);
7113 GST_ERROR_OBJECT (src, "failed to create task");
7119 gst_rtspsrc_stop (GstRTSPSrc * src)
7123 GST_DEBUG_OBJECT (src, "stopping");
7125 /* also cancels pending task */
7126 gst_rtspsrc_loop_send_cmd (src, CMD_WAIT, CMD_ALL);
7128 GST_OBJECT_LOCK (src);
7129 if ((task = src->task)) {
7131 GST_OBJECT_UNLOCK (src);
7133 gst_task_stop (task);
7135 /* make sure it is not running */
7136 GST_RTSP_STREAM_LOCK (src);
7137 GST_RTSP_STREAM_UNLOCK (src);
7139 /* now wait for the task to finish */
7140 gst_task_join (task);
7142 /* and free the task */
7143 gst_object_unref (GST_OBJECT (task));
7145 GST_OBJECT_LOCK (src);
7147 GST_OBJECT_UNLOCK (src);
7149 /* ensure synchronously all is closed and clean */
7150 gst_rtspsrc_close (src, FALSE, TRUE);
7155 static GstStateChangeReturn
7156 gst_rtspsrc_change_state (GstElement * element, GstStateChange transition)
7158 GstRTSPSrc *rtspsrc;
7159 GstStateChangeReturn ret;
7161 rtspsrc = GST_RTSPSRC (element);
7163 switch (transition) {
7164 case GST_STATE_CHANGE_NULL_TO_READY:
7165 if (!gst_rtspsrc_start (rtspsrc))
7168 case GST_STATE_CHANGE_READY_TO_PAUSED:
7169 /* init some state */
7170 rtspsrc->cur_protocols = rtspsrc->protocols;
7171 /* first attempt, don't ignore timeouts */
7172 rtspsrc->ignore_timeout = FALSE;
7173 rtspsrc->open_error = FALSE;
7174 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_OPEN, 0);
7176 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7177 set_manager_buffer_mode (rtspsrc);
7179 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7180 /* unblock the tcp tasks and make the loop waiting */
7181 if (gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_WAIT, CMD_LOOP)) {
7182 /* make sure it is waiting before we send PAUSE or PLAY below */
7183 GST_RTSP_STREAM_LOCK (rtspsrc);
7184 GST_RTSP_STREAM_UNLOCK (rtspsrc);
7187 case GST_STATE_CHANGE_PAUSED_TO_READY:
7193 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
7194 if (ret == GST_STATE_CHANGE_FAILURE)
7197 switch (transition) {
7198 case GST_STATE_CHANGE_NULL_TO_READY:
7199 ret = GST_STATE_CHANGE_SUCCESS;
7201 case GST_STATE_CHANGE_READY_TO_PAUSED:
7202 ret = GST_STATE_CHANGE_NO_PREROLL;
7204 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
7205 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PLAY, 0);
7206 ret = GST_STATE_CHANGE_SUCCESS;
7208 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
7209 /* send pause request and keep the idle task around */
7210 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_PAUSE, CMD_LOOP);
7211 ret = GST_STATE_CHANGE_NO_PREROLL;
7213 case GST_STATE_CHANGE_PAUSED_TO_READY:
7214 gst_rtspsrc_loop_send_cmd (rtspsrc, CMD_CLOSE, CMD_PAUSE);
7215 ret = GST_STATE_CHANGE_SUCCESS;
7217 case GST_STATE_CHANGE_READY_TO_NULL:
7218 gst_rtspsrc_stop (rtspsrc);
7219 ret = GST_STATE_CHANGE_SUCCESS;
7230 GST_DEBUG_OBJECT (rtspsrc, "start failed");
7231 return GST_STATE_CHANGE_FAILURE;
7236 gst_rtspsrc_send_event (GstElement * element, GstEvent * event)
7239 GstRTSPSrc *rtspsrc;
7241 rtspsrc = GST_RTSPSRC (element);
7243 if (GST_EVENT_IS_DOWNSTREAM (event)) {
7244 res = gst_rtspsrc_push_event (rtspsrc, event);
7246 res = GST_ELEMENT_CLASS (parent_class)->send_event (element, event);
7253 /*** GSTURIHANDLER INTERFACE *************************************************/
7256 gst_rtspsrc_uri_get_type (GType type)
7261 static const gchar *const *
7262 gst_rtspsrc_uri_get_protocols (GType type)
7264 static const gchar *protocols[] =
7265 { "rtsp", "rtspu", "rtspt", "rtsph", "rtsp-sdp",
7266 "rtsps", "rtspsu", "rtspst", "rtspsh", NULL
7273 gst_rtspsrc_uri_get_uri (GstURIHandler * handler)
7275 GstRTSPSrc *src = GST_RTSPSRC (handler);
7277 /* FIXME: make thread-safe */
7278 return g_strdup (src->conninfo.location);
7282 gst_rtspsrc_uri_set_uri (GstURIHandler * handler, const gchar * uri,
7287 GstRTSPUrl *newurl = NULL;
7288 GstSDPMessage *sdp = NULL;
7290 src = GST_RTSPSRC (handler);
7292 /* same URI, we're fine */
7293 if (src->conninfo.location && uri && !strcmp (uri, src->conninfo.location))
7296 if (g_str_has_prefix (uri, "rtsp-sdp://")) {
7297 if ((res = gst_sdp_message_new (&sdp) < 0))
7300 GST_DEBUG_OBJECT (src, "parsing SDP message");
7301 if ((res = gst_sdp_message_parse_uri (uri, sdp) < 0))
7305 GST_DEBUG_OBJECT (src, "parsing URI");
7306 if ((res = gst_rtsp_url_parse (uri, &newurl)) < 0)
7310 /* if worked, free previous and store new url object along with the original
7312 GST_DEBUG_OBJECT (src, "configuring URI");
7313 g_free (src->conninfo.location);
7314 src->conninfo.location = g_strdup (uri);
7315 gst_rtsp_url_free (src->conninfo.url);
7316 src->conninfo.url = newurl;
7317 g_free (src->conninfo.url_str);
7319 src->conninfo.url_str = gst_rtsp_url_get_request_uri (src->conninfo.url);
7321 src->conninfo.url_str = NULL;
7324 gst_sdp_message_free (src->sdp);
7326 src->from_sdp = sdp != NULL;
7328 GST_DEBUG_OBJECT (src, "set uri: %s", GST_STR_NULL (uri));
7329 GST_DEBUG_OBJECT (src, "request uri is: %s",
7330 GST_STR_NULL (src->conninfo.url_str));
7337 GST_DEBUG_OBJECT (src, "URI was ok: '%s'", GST_STR_NULL (uri));
7342 GST_ERROR_OBJECT (src, "Could not create new SDP (%d)", res);
7343 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7344 "Could not create SDP");
7349 GST_ERROR_OBJECT (src, "Not a valid SDP (%d) '%s'", res,
7350 GST_STR_NULL (uri));
7351 gst_sdp_message_free (sdp);
7352 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7358 GST_ERROR_OBJECT (src, "Not a valid RTSP url '%s' (%d)",
7359 GST_STR_NULL (uri), res);
7360 g_set_error_literal (error, GST_URI_ERROR, GST_URI_ERROR_BAD_URI,
7361 "Invalid RTSP URI");
7367 gst_rtspsrc_uri_handler_init (gpointer g_iface, gpointer iface_data)
7369 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
7371 iface->get_type = gst_rtspsrc_uri_get_type;
7372 iface->get_protocols = gst_rtspsrc_uri_get_protocols;
7373 iface->get_uri = gst_rtspsrc_uri_get_uri;
7374 iface->set_uri = gst_rtspsrc_uri_set_uri;